finish events
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Group, Vector3, Matrix4, Mesh, Quaternion, PlaneGeometry, MeshStandardMaterial, DoubleSide } from 'three';
|
||||
import { InteractiveObject } from '../InteractiveObject';
|
||||
import { ActiveEvents } from '@dimforge/rapier3d';
|
||||
|
||||
class MazeObject {
|
||||
constructor(engine, def, params = {}){
|
||||
@@ -32,16 +33,21 @@ class MazeObject {
|
||||
|
||||
let o = {};
|
||||
|
||||
function addPhysics(matrix, position, size, placement = 'side'){
|
||||
function addPhysics(matrix, position, size, placement = 'side', isSensor = false, userData){
|
||||
let quat = new Quaternion().setFromRotationMatrix(matrix);
|
||||
let v = new Vector3(...position).applyMatrix4(matrix);
|
||||
if (typeof size == 'number'){
|
||||
size = { width: 0.1, height:1, depth:size/2 }
|
||||
}
|
||||
let po = engine.phy.add(
|
||||
{position: v}, 'fixed', false, undefined, 'cuboid',{ width: 0.01, height:1, depth:size/2 }
|
||||
{position: v}, 'fixed', false, undefined, 'cuboid',
|
||||
{ ...size, isSensor, userData }
|
||||
)
|
||||
if (placement == 'front') {
|
||||
quat.multiply(new Quaternion(0, 0.7071068, 0, 0.7071068)) //rotate by 90deg
|
||||
}
|
||||
po.rigidBody.setRotation(quat, true)
|
||||
return po;
|
||||
}
|
||||
|
||||
function addRoom(elements, def, offsetZ){
|
||||
@@ -105,6 +111,9 @@ class MazeObject {
|
||||
// mazeMeshes.push(e);
|
||||
addRoom(['floor', 'wall', 'wall', 'door', 'wall'], def, -context.wallSize)
|
||||
}
|
||||
if (def.userData?.answer !== undefined){
|
||||
addPhysics(def.matrix, [0,0,0], context.wallSize, 'front', true, def.userData)
|
||||
}
|
||||
for (let i = 0; i < def.len; i++) {
|
||||
let t = o.tunnel.clone();
|
||||
t.position.set(0, 0, i * context.tubeSize);
|
||||
@@ -125,8 +134,10 @@ class MazeObject {
|
||||
addPhysics(def.matrix, [-context.tubeSize / 2, 0.6, offsetZ/2], offsetZ)
|
||||
|
||||
addRoom(['floor', 'door', def.r ? 'door' : 'wall', def.f ? 'door' : 'wall', def.l ? 'door' : 'wall'], def, offsetZ)
|
||||
|
||||
console.log('loadingggg', def.objects)
|
||||
if (def.userData?.qid !== undefined || def.userData?.finish){
|
||||
addPhysics(def.matrix, [0,0,offsetZ + context.wallSize/2], { width: context.wallSize/2, height: context.wallSize/2, depth: context.wallSize/2}, 'side', true, def.userData)
|
||||
}
|
||||
//console.log('loadingggg', def.objects)
|
||||
def.objects?.forEach(async obj => {
|
||||
obj.room = room;
|
||||
// let go = new GameObject(obj, context);
|
||||
@@ -172,7 +183,7 @@ class MazeObject {
|
||||
// //engine.phy.add(mesh, 'fixed')
|
||||
// })
|
||||
|
||||
console.log(bbox, 'bbox')
|
||||
//console.log(bbox, 'bbox')
|
||||
const floorGeometry = new PlaneGeometry(bbox.r - bbox.l + 10*scale, bbox.f + 10*scale);
|
||||
const floor = new Mesh(floorGeometry,new MeshStandardMaterial({
|
||||
roughness: 0, metalness:1, color: 0x00ffff, side: DoubleSide
|
||||
|
||||
@@ -20,6 +20,24 @@ class MazeQuizGame {
|
||||
constructor(engine, context, questions) {
|
||||
let def = this.generate(questions);
|
||||
this.mazeObject = new MazeObject(engine, def)
|
||||
engine.addEventListener('collision', async e=>{
|
||||
let ud = engine.phy.world.getCollider(e.handle2)?.parent()?.userData;
|
||||
if (ud?.finish && engine.hero?.animationsMap?.win){
|
||||
if (e.started){
|
||||
engine.hero.animationsMap._idle = engine.hero.animationsMap.idle
|
||||
engine.hero.animationsMap.idle = engine.hero.animationsMap.win
|
||||
engine.hero.characterControls.cameraDelta = Math.PI;
|
||||
await Utils.wait(1000);
|
||||
engine.hero.characterControls.direction += Math.PI;
|
||||
await Utils.wait(10000);
|
||||
this.onfinish?.()
|
||||
}else{
|
||||
engine.hero.animationsMap.idle = engine.hero.animationsMap._idle
|
||||
engine.hero.characterControls.cameraDelta = 0
|
||||
}
|
||||
}
|
||||
//console.log(e, ud, engine.hero?.animationsMap);
|
||||
})
|
||||
}
|
||||
|
||||
async load(){
|
||||
@@ -32,7 +50,7 @@ class MazeQuizGame {
|
||||
let question = questions[qid]
|
||||
if (!question) return {
|
||||
len:3,
|
||||
finish: true,
|
||||
userData: { finish: true },
|
||||
objects:[
|
||||
{
|
||||
type: 'gltf',
|
||||
@@ -52,7 +70,7 @@ class MazeQuizGame {
|
||||
let directions = Utils.shuffleArray( ['l', 'r', 'f'] )
|
||||
|
||||
let mo = {
|
||||
len, question, qid,
|
||||
len, userData: { question, qid },
|
||||
objects:[
|
||||
{
|
||||
type: 'text', text: question.q, fontSize:0.033, width:0.5, position:[0,.33,len + .96], rotation:[0,Math.PI, 0]
|
||||
@@ -74,11 +92,13 @@ class MazeQuizGame {
|
||||
}
|
||||
if (i == 0){
|
||||
mo[d] = {
|
||||
len: 3,
|
||||
answer: i,
|
||||
len: 4,
|
||||
[dd]: this.generate(questions, qid + 1, 3)
|
||||
}
|
||||
}else{
|
||||
mo[d] = {
|
||||
userData: { question, qid, answer: i },
|
||||
len: 4,
|
||||
[dd]: {
|
||||
len: 2,
|
||||
|
||||
Reference in New Issue
Block a user