mazegame
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
import { Group, Vector3, Matrix4, Mesh, DoubleSide } from 'three';
|
||||
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
|
||||
import { TextObject } from '../TextObject';
|
||||
|
||||
class MazeObject {
|
||||
constructor(engine, def, params = {}){
|
||||
let room = new Group();
|
||||
let scene = room;
|
||||
this.object = room;
|
||||
let context = {};
|
||||
context.wallSize = params.wallSize || .65;
|
||||
context.tubeSize = params.tubeSize || .8;
|
||||
context.wallDepth = params.wallDepth || .1;
|
||||
context.fontPath = params.fontPath || '/static/fonts/ZapfChanceryC.otf';
|
||||
|
||||
const cameraNear = .2;
|
||||
|
||||
this.context = context;
|
||||
let _tf = {
|
||||
rotation: {
|
||||
r: 3 * Math.PI / 2, f: 0, l: Math.PI / 2, b: Math.PI
|
||||
},
|
||||
position: {
|
||||
r: [-context.wallSize, context.wallSize],
|
||||
f: [0, 2 * context.wallSize],
|
||||
l: [context.wallSize, context.wallSize]
|
||||
},
|
||||
pNext: {
|
||||
r: [-context.wallSize - (context.wallSize + context.wallDepth) / 2, context.wallSize],
|
||||
f: [0, 2 * context.wallSize + context.wallSize / 2],
|
||||
l: [context.wallSize + (context.wallSize + context.wallDepth) / 2, context.wallSize]
|
||||
}
|
||||
};
|
||||
|
||||
let o = {};
|
||||
let mazeGeometries = [], areas = [];
|
||||
|
||||
this.mazeObject = function(def, room, step = 0) {
|
||||
let offsetZ = 0, e;
|
||||
def.len = def.len || 0;
|
||||
if (step == 0) {
|
||||
e = o.door.geometry.clone();
|
||||
e.rotateY(_tf.rotation.f);
|
||||
mazeGeometries.push(e);
|
||||
}
|
||||
for (let i = 0; i < def.len; i++) {
|
||||
let t = o.tunnel.geometry.clone();
|
||||
t.translate(0, 0, i * context.tubeSize + context.wallDepth / 2);
|
||||
def.matrix && t.applyMatrix4(def.matrix);
|
||||
|
||||
mazeGeometries.push(t);
|
||||
}
|
||||
offsetZ = context.wallDepth + def.len * context.tubeSize - context.tubeSize / 2;
|
||||
if (!def.len) offsetZ = -.275;
|
||||
areas.push({
|
||||
a: [
|
||||
room.localToWorld(new Vector3(-context.tubeSize / 2 + cameraNear, 0, -context.tubeSize / 2 - cameraNear)),
|
||||
room.localToWorld(new Vector3(context.tubeSize / 2 - cameraNear, 0, offsetZ + cameraNear))
|
||||
]
|
||||
});
|
||||
if (def.type == 'area') {
|
||||
def.area.forEach(ar => {
|
||||
areas.push({
|
||||
a: [
|
||||
room.localToWorld(new Vector3(ar[0] + cameraNear, 0, offsetZ + ar[1] - cameraNear)),
|
||||
room.localToWorld(new Vector3(ar[2] - cameraNear, 0, offsetZ + ar[3] + cameraNear))
|
||||
]
|
||||
});
|
||||
});
|
||||
} else {
|
||||
if (def.noRoom) {
|
||||
// e = o.wall.geometry.clone();
|
||||
// e.rotateY(_tf.rotation.f);
|
||||
// e.translate(0,0,offsetZ + 0);
|
||||
// def.matrix && e.applyMatrix4(def.matrix);
|
||||
// mazeGeometries.push(e);
|
||||
} else {
|
||||
e = [o.floor.geometry.clone(), o.door.geometry.clone(), o[def.r ? 'door' : 'wall'].geometry.clone(),
|
||||
o[def.f ? 'door' : 'wall'].geometry.clone(), o[def.l ? 'door' : 'wall'].geometry.clone()];
|
||||
e[0].translate(0, 0, offsetZ + context.wallSize);
|
||||
|
||||
e[1].rotateY(_tf.rotation.b);
|
||||
e[2].rotateY(_tf.rotation.r);
|
||||
e[3].rotateY(_tf.rotation.f);
|
||||
e[4].rotateY(_tf.rotation.l);
|
||||
|
||||
e[1].translate(0, 0, offsetZ + 0);
|
||||
e[2].translate(-context.wallSize, 0, offsetZ + context.wallSize);
|
||||
e[3].translate(0, 0, offsetZ + context.wallSize * 2);
|
||||
e[4].translate(context.wallSize, 0, offsetZ + context.wallSize);
|
||||
|
||||
e.forEach(g => {
|
||||
def.matrix && g.applyMatrix4(def.matrix);
|
||||
mazeGeometries.push(g);
|
||||
});
|
||||
areas.push({
|
||||
a: [
|
||||
room.localToWorld(new Vector3(-context.wallSize + cameraNear, 0, offsetZ + cameraNear)),
|
||||
room.localToWorld(new Vector3(context.wallSize - cameraNear, 0, offsetZ + context.wallSize * 2 - cameraNear))
|
||||
]
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
def.objects && def.objects.forEach(obj => {
|
||||
obj.room = room;
|
||||
// let go = new GameObject(obj, context);
|
||||
let go = new TextObject(obj, context)
|
||||
room.add(go.mesh);
|
||||
// go.ready.then(mesh => {
|
||||
// room.add(mesh);
|
||||
// });
|
||||
});
|
||||
|
||||
def.room = room;
|
||||
['r', 'f', 'l'].forEach((d, i) => {
|
||||
if (!def[d]) return;
|
||||
let mtx = new Matrix4();
|
||||
mtx.makeRotationY(_tf.rotation[d]);
|
||||
mtx.setPosition(_tf.pNext[d][0], 0, _tf.pNext[d][1] + offsetZ);
|
||||
let rr = new Group();
|
||||
scene.add(rr);
|
||||
def[d].matrix = mtx.premultiply(def.matrix || new Matrix4());
|
||||
rr.applyMatrix4(def[d].matrix);
|
||||
rr.updateMatrixWorld();
|
||||
this.mazeObject(def[d], rr, step + 1);
|
||||
});
|
||||
};
|
||||
|
||||
this.load = async function(){
|
||||
let mazeAsset = await engine.load('/static/meshes/maze-reed.gltf');
|
||||
['tunnel', 'wall', 'door', 'floor'].forEach(e => {
|
||||
o[e] = mazeAsset.scene.getObjectByName(e);
|
||||
o[e].frustumCulled = false;
|
||||
});
|
||||
o.tunnel.material.depthWrite = false
|
||||
this.mazeObject(def, room);
|
||||
mazeGeometries.forEach(mg=>{
|
||||
scene.add(new Mesh(mg, o.tunnel.material));
|
||||
})
|
||||
//scene.add(new Mesh(BufferGeometryUtils.mergeGeometries(mazeGeometries, false), o.tunnel.material));
|
||||
console.log(room);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { MazeObject }
|
||||
Reference in New Issue
Block a user