change event dispatcher

This commit is contained in:
2025-11-11 15:15:13 +02:00
parent 35fa1863ff
commit 351c6097f3
10 changed files with 71 additions and 67 deletions
@@ -1,7 +1,9 @@
import { getBoundingBox, getBoundingBoxCenterPoint, getBoundingBoxMaxLength, centerOrigin } from "@/lib/MeshUtils";
import { EventDispatcher } from "three";
class GenericObject{
class GenericObject extends EventDispatcher{
constructor(engine, data){
super();
return new Promise(async(resolve, reject)=>{
this.source = await engine.load(data.$go.asset.name);
this.object = centerOrigin(this.source.scene)
@@ -34,7 +36,7 @@ class GenericObject{
}
if (data.description){
engine.dashboard.updateText(this.object._active ? data.description : '', (d)=>{
console.log('DONETEXT', d)
d && this.dispatchEvent({type:'finish'})
})
}
}
@@ -1,4 +1,4 @@
import { Group } from "three";
import { Group, EventDispatcher } from "three";
import { GenericObject } from "./GenenricObject";
import { TextObject } from "./TextObject";
@@ -16,12 +16,13 @@ import { Particles } from "./Particles";
import { assignMaterial, assignParams } from "@/lib/MeshUtils";
const InteractiveObjectsImports = {
PuzzleGame1, PuzzleGame2, PuzzleGame4, VideoPlayer,
GenericObject, CharacterObject, MazeQuizGame, Particles
GenericObject, CharacterObject, TextObject, ImageObject, VideoPlayer, Particles,
PuzzleGame1, PuzzleGame2, PuzzleGame4, MazeQuizGame
};
class InteractiveObject {
class InteractiveObject extends EventDispatcher{
constructor(gameEngine, obj) {
super();
this.name = obj.name;
return new Promise(async (resolve, reject) => {
switch (obj.type || 'GenericObject') {
@@ -32,14 +33,6 @@ class InteractiveObject {
this.object.add(gameMesh.object);
}
break;
case 'Text':
this.source = new TextObject(gameEngine, obj);
this.object = this.source.object;
break;
case 'Image':
this.source = new ImageObject(gameEngine, obj);
this.object = this.source.object;
break;
case 'Gltf':
let gltf = await gameEngine.load(obj.value, '');
let gltfObj = gltf.scene;
@@ -63,27 +56,20 @@ class InteractiveObject {
this.object = gltfObj;
this.source = gltf;
break;
case 'Text':
case 'Image':
case 'GenericObject':
case 'CharacterObject':
let go = await new InteractiveObjectsImports[obj.type](gameEngine, obj) //await gameEngine.load(obj.$go.asset.name);
this.source = go.source;
this.object = go.object;
break;
case 'PuzzleGame3':
case 'PuzzleGame4':
case 'PuzzleGame5':
case 'PuzzleGame6':
let game = new InteractiveObjectsImports[obj.type](gameEngine, obj.args[0], obj.args[1], obj.args[2]);
this.object = game.object;
this.object.game = game;
break;
case 'VideoPlayer':
case 'PuzzleGame1':
case 'PuzzleGame2':
case 'MazeQuizGame':
case 'Particles':
this.source = await new InteractiveObjectsImports[obj.type](gameEngine, obj);
this.object = this.source.object;
this.io = await new InteractiveObjectsImports[obj.type](gameEngine, obj);
this.source = this.io.source || this.io;
this.object = this.io.object;
this.io.addEventListener?.('finish', this.dispatchEvent.bind(this))
break;
}
assignParams(this.object, obj);
@@ -1,5 +1,6 @@
import { MazeObject } from "./MazeObject";
import Utils from "@/lib/Utils";
import { EventDispatcher } from "three";
const params = {
scale: 5,
@@ -34,7 +35,7 @@ const defaults = {
const tl = 4;
class MazeQuizGame {
class MazeQuizGame extends EventDispatcher {
constructor(engine, data) {
data.noPhysics = true;
return new Promise(async (resolve, reject)=>{
@@ -57,7 +58,7 @@ class MazeQuizGame {
engine.hero.characterControls.direction += Math.PI;
//engine.hero.model.rotation.y += Math.PI;
await Utils.wait(10000);
this.onfinish?.()
this.dispatchEvent({type:'finish'})
}else{
engine.hero.animationsMap.idle = engine.hero.animationsMap._idle
engine.hero.characterControls.cameraDelta = 0
@@ -1,9 +1,10 @@
import { BoxGeometry, Mesh, MeshStandardMaterial, Group } from 'three';
import { BoxGeometry, Mesh, MeshStandardMaterial, Group, EventDispatcher } from 'three';
import { MotionEngine } from '../../lib/MotionEngine';
import { centerOrigin } from '@/lib/MeshUtils';
class PuzzleGame1 {
class PuzzleGame1 extends EventDispatcher {
constructor(engine, data) {
super();
return new Promise(async (resolve, reject)=>{
let w = data.w, h = data.h, wh = w*h;
let container = new Group();
@@ -92,7 +93,9 @@ class PuzzleGame1 {
o: c,
a: { position: { x: i % w, y: Math.trunc(i/w)} },
t: 1,
f: i == 0 && this.onfinish || undefined
f: i == 0 ? ()=>{
this.dispatchEvent({type:'finish'})
} : undefined
});
});
//engine.dashboard.addPoints(10);
@@ -1,9 +1,10 @@
import { BoxGeometry, Mesh, MeshBasicMaterial, Group } from 'three';
import { BoxGeometry, Mesh, MeshBasicMaterial, Group, EventDispatcher } from 'three';
import { MotionEngine } from '../../lib/MotionEngine';
import { centerOrigin } from '@/lib/MeshUtils';
class PuzzleGame2 {
class PuzzleGame2 extends EventDispatcher {
constructor(engine, data) {
super();
return new Promise(async (resolve, reject)=>{
let w = data.w, h = data.h, wh = w*h;
const texture = await engine.loadTexture(data.$go.asset.name);
@@ -137,7 +138,9 @@ class PuzzleGame2 {
o: c,
a: { position: { x: i % w, y: ~~(i / h) } },
t: 1,
f: i == 0 ? this.onfinish : undefined
f: i == 0 ? ()=>{
this.dispatchEvent({type:'finish'})
} : undefined
});
});
//engine.dashboard.addPoints(10);
@@ -115,7 +115,7 @@ var PuzzleGame4 = function(context, gltf, w, h){
d:1+0.1*i
})
})
this.onfinish && this.onfinish();
this.dispatchEvent({type:'finish'})
//context.dashboard.addPoints(10);
}
}
@@ -1,25 +1,28 @@
import * as THREE from 'three';
import {
PlaneGeometry, MeshStandardMaterial, SRGBColorSpace,
VideoTexture, DoubleSide, Mesh, EventDispatcher
} from 'three';
class VideoPlayer {
class VideoPlayer extends EventDispatcher {
constructor(engine, data){
super();
let vi, plane;
return new Promise((resolve, reject)=>{
vi = document.createElement('video');
vi.src = engine.assetPath + data.$go.asset.name;
vi.addEventListener('loadedmetadata', ()=>{
this.aspect = vi.videoWidth / vi.videoHeight;
let geometry = new THREE.PlaneGeometry( this.aspect, 1 );
let map = new THREE.VideoTexture( vi );
map.colorSpace = THREE.SRGBColorSpace;
let material = new THREE.MeshStandardMaterial( {
let geometry = new PlaneGeometry( this.aspect, 1 );
let map = new VideoTexture( vi );
map.colorSpace = SRGBColorSpace;
let material = new MeshStandardMaterial( {
color: 0xffffff,
map,
transparent: true,
opacity: 0.5,
side: THREE.DoubleSide
side: DoubleSide
} );
plane = new THREE.Mesh( geometry, material );
plane = new Mesh( geometry, material );
this.object = plane;
engine.clickable.add(plane, ()=>{
if (vi.paused){
@@ -46,7 +49,7 @@ class VideoPlayer {
}
})
vi.addEventListener('ended', ()=>{
this.onfinish?.();
this.dispatchEvent({type:'finish'})
})
this.video = vi;