This commit is contained in:
2025-10-14 18:36:02 +03:00
parent a6827f4ddb
commit a60d94fe66
16 changed files with 1225 additions and 33 deletions
@@ -0,0 +1,143 @@
import { ImageObject } from "./ImageObject";
import { Hint } from "./Hint";
import { Group, AnimationMixer, LoopPingPong, Vector3 } from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { Utils } from "./Utils";
import { Game1 } from "./PuzzleGame1";
import { Game2 } from "./PuzzleGame2";
// import { Game3 } from "./games/Game3";
import { Game4 } from "./PuzzleGame4";
// import { Game5 } from "./games/Game5";
// import { Game6 } from "./games/Game6";
import { TextObject } from "./TextObject";
const games = {Game1, Game2, Game4};
class InteractiveObject {
constructor(obj, context) {
this.name = obj.name;
this.ready = new Promise((resolve, reject) => {
let mesh;
switch (obj.type) {
case 'group':
mesh = new Group();
obj.group.forEach(g => {
let go = new InteractiveObject(g, context);
go.ready.then((gameMesh) => {
mesh.add(gameMesh);
});
});
resolve(mesh);
break;
case 'text':
let text = new TextObject(obj, context);
resolve(text.mesh);
break;
case 'mesh':
mesh = obj.value;
resolve(mesh);
break;
case 'image':
let imo = new ImageObject(obj, context);
mesh = imo.mesh;
resolve(mesh);
break;
case 'hint':
let hint = new Hint(obj, context);
mesh = hint.mesh;
resolve(mesh);
break;
case 'gltf':
new GLTFLoader().load(obj.value, (gltf) => {
let gltfObj = gltf.scene;
gltf.scene.traverse(function (object) {
object.frustumCulled = false;
if (obj.name && obj.name == object.name) {
gltfObj = object;
}
// object.castShadow = true;
// object.receiveShadow = true;
});
Utils.assignMaterial(gltfObj, obj, context);
if (gltf.animations && gltf.animations.length) {
let mixer = new AnimationMixer(gltfObj);
context.mixers.push(mixer);
let action = mixer.clipAction(gltf.animations[0]);
action.setLoop(LoopPingPong);
action.play();
}
resolve(gltfObj);
});
break;
case 'asset':
mesh = context.assets[obj.value].clone();
Utils.assignMaterial(mesh, obj, context);
resolve(mesh);
break;
case 'Game1':
case 'Game2':
case 'Game3':
case 'Game4':
case 'Game5':
case 'Game6':
var game = new games[obj.type](context, obj.args[0], obj.args[1], obj.args[2]);
mesh = game.game;
mesh.game = game;
resolve(mesh);
break;
}
});
this.ready.then((mesh) => {
mesh.go = {};
let restriction;
if (!context.disableRestrictions && obj.restriction) {
restriction = {
type: 'deny',
a: [obj.room.localToWorld(new Vector3().fromArray(obj.restriction[0])), obj.room.localToWorld(new Vector3().fromArray(obj.restriction[1]))]
};
context.areas.push(restriction);
}
mesh.go.finish = () => {
if (obj.finish) {
var f;
if (obj.finish.nextAction) {
var next = obj.finish.nextAction;
delete obj.finish.nextAction;
f = () => {
if (next.activate) {
context.activate(next.activate);
}
};
}
var me = obj.finish._ || {};
delete obj.finish._;
context.motionEngine.add({ o: mesh, a: obj.finish, t: me.t || 1, f: me.f || f, d: me.d || 0 });
}
if (restriction) context.areas.splice(context.areas.indexOf(restriction), 1);
};
if (mesh.game) mesh.game.onfinish = mesh.go.finish;
Utils.assignParams(mesh, obj);
obj.animation && context.motionEngine.add({
o: mesh,
a: obj.animation.motion,
r: obj.animation.repeat,
t: obj.animation.duration || 1
});
this.mesh = mesh;
});
}
}
// function textObject(text, context){
// const geometry = new TextGeometry( text, {
// font: context.font,
// size: .05,
// height: .01,
// curveSegments: 1
// } );
// return new Mesh(geometry, context.fontMaterial);
// }
export {InteractiveObject}