@@ -5,8 +5,9 @@ import { HDRLoader } from "three/examples/jsm/loaders/HDRLoader.js";
55import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js" ;
66import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js" ;
77import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPass.js" ;
8- import { isMobile } from "./device" ;
9- import { Match } from "./game/match" ;
8+ import { getRole , isMobile } from "./device" ;
9+ import { GameServer } from "./game/game-match" ;
10+ import { fromVec3 , toVec3 , type PlayerAction , type Vec3 } from "./game/player" ;
1011
1112// Required for Github Pages deployment
1213THREE . DefaultLoadingManager . setURLModifier ( ( url ) => {
@@ -55,7 +56,7 @@ controls.mouseButtons = {
5556 RIGHT : THREE . MOUSE . ROTATE
5657} ;
5758
58- // Lights
59+ // === Lights ===
5960const radianceMap = await new HDRLoader ( ) . loadAsync ( '/hdri/kloppenheim_02_puresky_1k.hdr' ) ;
6061scene . environment = ( await buildPrefilteredRadianceMap ( radianceMap , renderer ) ) . texture ;
6162
@@ -65,20 +66,6 @@ dirLight.castShadow = true;
6566dirLight . shadow . mapSize . set ( 1024 , 1024 ) ;
6667scene . add ( dirLight ) ;
6768
68-
69- // const dirLight = new THREE.DirectionalLight(0xffffff, 1.5);
70- // dirLight.position.set(-5, 10, -5);
71- // dirLight.castShadow = true;
72- // dirLight.shadow.mapSize.set(1024, 1024);
73- // scene.add(dirLight);
74-
75- // const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 1);
76- // scene.add(hemiLight);
77-
78- // const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
79- // scene.add(ambientLight);
80-
81-
8269// === Board ===
8370const boardSize = 8 ;
8471const tileSize = 1 ;
@@ -101,10 +88,11 @@ const oddMat = new THREE.MeshStandardMaterial({
10188
10289// === Scene setup ===
10390async function init ( ) {
104- const game = isMobile ( ) ? await Match . join ( ) : await Match . host ( ) ;
105- game . onAction = ( data ) => { currentTarget = new THREE . Vector3 ( - 4 + Math . random ( ) * 8 , currentTarget ?. y , - 4 + Math . random ( ) * 8 ) } ;
106- window . addEventListener ( 'pagehide' , ( ) => game . dispose ( ) , { once : true } ) ;
107- window . addEventListener ( 'beforeunload' , ( ) => game . dispose ( ) , { once : true } ) ;
91+ const server = await createMatchAsync ( ) ;
92+ if ( server ) {
93+ server . onAction = action => playActionLocal ( action ) ;
94+ window . addEventListener ( 'pagehide' , ( ) => server . dispose ( ) , { once : true } ) ;
95+ }
10896
10997 const tileSrc = ( await loadGLB ( "/models/box.glb" ) ) . children [ 0 ] as THREE . Mesh ;
11098 const tileGeometry = ( tileSrc . geometry as THREE . BufferGeometry ) . clone ( ) ;
@@ -215,12 +203,11 @@ async function init() {
215203 const intersects = raycaster . intersectObjects ( tiles , false ) ;
216204
217205 if ( intersects . length > 0 ) {
218- game . play ( { type : 'Move' } ) ;
219206 const picked = intersects [ 0 ] ?. object as THREE . Mesh ;
220207 currentTarget = picked . getWorldPosition ( new THREE . Vector3 ( ) ) ;
221208 currentTarget . x += 0.4 ;
222209 currentTarget . y = scar . position . y ;
223-
210+ playAction ( { type : 'Move' , targetPosition : toVec3 ( currentTarget ) } ) ;
224211 if ( currentPick ) {
225212 // restore material
226213 currentPick . material = currentMaterial ! ;
@@ -307,6 +294,30 @@ async function init() {
307294 }
308295 return el ;
309296 }
297+
298+ async function createMatchAsync ( ) : Promise < GameServer | undefined > {
299+ const role = getRole ( ) ;
300+ switch ( role ) {
301+ case 'client' : return GameServer . join ( ) ;
302+ case 'host' : return GameServer . host ( ) ;
303+ case undefined : undefined ;
304+ }
305+ }
306+
307+ function playAction ( action : PlayerAction ) {
308+ server ?. send ( action ) ;
309+ playActionLocal ( action ) ;
310+ }
311+
312+ function playActionLocal ( action : PlayerAction ) {
313+ switch ( action . type ) {
314+ case 'Move' : currentTarget = mapXZFrom ( fromVec3 ( action . targetPosition ! ) , scar . position ) ;
315+ }
316+ }
317+
318+ function mapXZFrom ( source : THREE . Vector3 , target : THREE . Vector3 ) {
319+ return new THREE . Vector3 ( source . x , target . y , source . z ) ;
320+ }
310321}
311322
312323init ( ) . catch ( console . error ) ;
0 commit comments