40 lines
5.7 KiB
TypeScript
40 lines
5.7 KiB
TypeScript
import * as T from 'three';
|
|
import { buildAppearance, disposeAppearance } from './appearance-model';
|
|
import type {Entity, HouseScene, Snapshot} from './generated';
|
|
|
|
/** Indoor presentation only. Positions and furniture geometry come from Go. */
|
|
export class HouseView {
|
|
readonly scene=new T.Scene();
|
|
readonly camera=new T.OrthographicCamera(-10,10,10,-10,.1,100);
|
|
private room=new T.Group();private actors=new Map<string,{root:T.Group;target:T.Vector3;dispose:()=>void}>();
|
|
private signature='';private active=false;private size=12;private ray=new T.Raycaster();
|
|
private disposables:(()=>void)[]=[];
|
|
constructor(){this.scene.background=new T.Color('#172329');this.scene.add(new T.HemisphereLight('#fff5db','#4b3930',2));const sun=new T.DirectionalLight('#ffdfaa',3);sun.position.set(2,9,6);this.scene.add(sun,this.room)}
|
|
private box(w:number,h:number,d:number,x:number,y:number,z:number,color:string,parent:T.Object3D=this.room){const geometry=new T.BoxGeometry(w,h,d),material=new T.MeshStandardMaterial({color,roughness:.85});const mesh=new T.Mesh(geometry,material);mesh.position.set(x,y,z);parent.add(mesh);this.disposables.push(()=>{geometry.dispose();material.dispose()});return mesh}
|
|
update(snapshot:Snapshot,model?:T.Group){
|
|
const house=snapshot.house;if(!house){if(this.active)this.clear();return}this.active=true;this.size=house.size;
|
|
const signature=JSON.stringify([house.id,house.size,house.furniture]);if(this.signature!==signature){this.clear();this.active=true;this.signature=signature;this.build(house)}
|
|
const entities=[...house.people.filter(p=>p.id!==snapshot.self.entity.id),{...snapshot.self.entity,position:snapshot.self.housePosition??{x:0,y:0,z:0}}];
|
|
const ids=new Set(entities.map(p=>p.id));for(const [id,v] of this.actors)if(!ids.has(id)){this.scene.remove(v.root);v.dispose();this.actors.delete(id)}
|
|
for(const e of entities){let actor=this.actors.get(e.id);const appearanceKey=JSON.stringify(e.appearance??null);if(actor&&actor.root.userData.appearance!==appearanceKey){this.scene.remove(actor.root);actor.dispose();this.actors.delete(e.id);actor=undefined}if(!actor){const firstDisposable=this.disposables.length,root=new T.Group();root.userData.appearance=appearanceKey;if(e.appearance){const visual=buildAppearance(e.appearance);root.add(visual);this.disposables.push(()=>disposeAppearance(visual))}else if(model)root.add(model.clone(true));else{this.box(.55,1.25,.45,0,.8,0,e.id===snapshot.self.entity.id?'#bdd1ad':'#88a9c6',root);this.box(.4,.4,.4,0,1.65,0,'#d6b49a',root)}
|
|
const canvas=document.createElement('canvas');canvas.width=256;canvas.height=48;const ctx=canvas.getContext('2d')!;ctx.fillStyle='#13201ee6';ctx.fillRect(0,0,256,48);ctx.fillStyle='#fff5df';ctx.textAlign='center';ctx.font='24px sans-serif';ctx.fillText(e.name,128,32,248);const texture=new T.CanvasTexture(canvas),material=new T.SpriteMaterial({map:texture,depthTest:false}),name=new T.Sprite(material);name.position.y=2.2;name.scale.set(2.5,.47,1);root.add(name);this.disposables.push(()=>{texture.dispose();material.dispose()});root.position.set(e.position.x,e.position.y,e.position.z);const cleanup=this.disposables.splice(firstDisposable);actor={root,target:root.position.clone(),dispose:()=>cleanup.forEach(f=>f())};this.actors.set(e.id,actor);this.scene.add(root)
|
|
}actor.target.set(e.position.x,e.position.y,e.position.z)
|
|
}
|
|
}
|
|
private build(h:HouseScene){
|
|
this.box(h.size,.18,h.size,0,-.09,0,'#907451');this.box(h.size,2.8,.2,0,1.4,-h.size/2,'#bab297');this.box(.2,2.8,h.size,-h.size/2,1.4,0,'#9e9986');
|
|
for(let x=-h.size/2;x<h.size/2;x+=1)this.box(.015,.006,h.size,x,.006,0,'#77613f');
|
|
const door=this.box(1.5,2.3,.15,0,1.15,h.size/2,'#548f81');door.userData.entity={id:'house-exit',kind:'house-exit',name:'Return to hub',position:{x:0,y:0,z:h.size/2},hp:0,maxHp:0,state:'active'} satisfies Entity;
|
|
for(const f of h.furniture){const group=new T.Group();this.room.add(group);group.position.set(f.x,0,f.z);group.userData.entity={id:f.id,kind:'furniture',name:f.name,position:{x:f.x,y:0,z:f.z},hp:0,maxHp:0,state:'active'} satisfies Entity;
|
|
if(f.container){this.box(f.width,f.height,f.depth,0,f.height/2,0,'#805232',group);this.box(f.width+.03,.1,f.depth+.03,0,f.height-.05,0,'#b28851',group);this.box(.12,.22,.025,0,f.height*.65,f.depth/2+.02,'#e0c06e',group)}
|
|
else{this.box(f.width,.12,f.depth,0,f.height-.06,0,'#b39362',group);for(const x of [-1,1])for(const z of [-1,1])this.box(.12,f.height-.12,.12,x*(f.width/2-.12),(f.height-.12)/2,z*(f.depth/2-.12),'#694a31',group)}
|
|
}
|
|
}
|
|
render(renderer:T.WebGLRenderer,dt:number){
|
|
const aspect=innerWidth/innerHeight,size=(this.size*.64+1)/Math.min(1,aspect);this.camera.left=-size*aspect;this.camera.right=size*aspect;this.camera.top=size;this.camera.bottom=-size;this.camera.position.set(0,this.size*1.15,this.size*1.05);this.camera.lookAt(0,.4,0);this.camera.updateProjectionMatrix();
|
|
for(const a of this.actors.values()){const delta=a.target.clone().sub(a.root.position);if(delta.x*delta.x+delta.z*delta.z>.002)a.root.rotation.y=Math.atan2(-delta.x,-delta.z);a.root.position.lerp(a.target,1-Math.exp(-14*dt))}renderer.render(this.scene,this.camera);
|
|
}
|
|
pick(canvas:HTMLCanvasElement,x:number,y:number):{entity?:Entity;point?:T.Vector3}{const r=canvas.getBoundingClientRect();this.ray.setFromCamera(new T.Vector2((x-r.left)/r.width*2-1,-(y-r.top)/r.height*2+1),this.camera);for(const hit of this.ray.intersectObject(this.room,true)){let o:T.Object3D|null=hit.object;while(o){if(o.userData.entity)return{entity:o.userData.entity as Entity};o=o.parent}if(Math.abs(hit.point.y)<.02)return{point:hit.point}}return{}}
|
|
clear(){for(const dispose of this.disposables)dispose();this.disposables=[];this.room.clear();for(const a of this.actors.values()){this.scene.remove(a.root);a.dispose()}this.actors.clear();this.signature='';this.active=false}
|
|
}
|