eznd commited on
Commit
89bc6e5
·
verified ·
1 Parent(s): d54c2be

Upload 9 files

Browse files
public/index.html CHANGED
@@ -196,6 +196,7 @@
196
  <div id="game-screen">
197
  <canvas id="game-canvas"></canvas>
198
  <button class="back-btn" id="back-to-lobby-btn">← Back to Lobby</button>
 
199
  <div id="ui-overlay">
200
  <h1 id="game-title">BAO</h1>
201
  <p id="ui-message">Waiting for game to start...</p>
 
196
  <div id="game-screen">
197
  <canvas id="game-canvas"></canvas>
198
  <button class="back-btn" id="back-to-lobby-btn">← Back to Lobby</button>
199
+ <button class="env-btn" id="switch-env-btn">🌍 Switch Environment</button>
200
  <div id="ui-overlay">
201
  <h1 id="game-title">BAO</h1>
202
  <p id="ui-message">Waiting for game to start...</p>
public/js/EnvironmentManager.js ADDED
@@ -0,0 +1,322 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import * as THREE from 'three';
2
+
3
+ export default class EnvironmentManager {
4
+ constructor(scene, renderer) {
5
+ this.scene = scene;
6
+ this.renderer = renderer;
7
+
8
+ // Ensure color management is correct
9
+ this.pmremGenerator = new THREE.PMREMGenerator(renderer);
10
+ this.pmremGenerator.compileEquirectangularShader();
11
+
12
+ this.environments = ['zanzibar', 'serengeti', 'kilwa'];
13
+ this.currentEnvIndex = 0;
14
+
15
+ // Shared lighting and floor objects
16
+ this.dirLight = new THREE.DirectionalLight(0xffffff, 2.0);
17
+ this.dirLight.castShadow = true;
18
+ this.dirLight.shadow.mapSize.width = 2048;
19
+ this.dirLight.shadow.mapSize.height = 2048;
20
+ // The shadow camera size should be large enough to cover the board
21
+ this.dirLight.shadow.camera.near = 0.5;
22
+ this.dirLight.shadow.camera.far = 1500;
23
+ this.dirLight.shadow.camera.left = -500;
24
+ this.dirLight.shadow.camera.right = 500;
25
+ this.dirLight.shadow.camera.top = 500;
26
+ this.dirLight.shadow.camera.bottom = -500;
27
+ this.scene.add(this.dirLight);
28
+
29
+ this.hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 0.6);
30
+ this.scene.add(this.hemiLight);
31
+
32
+ const floorGeo = new THREE.PlaneGeometry(5000, 5000);
33
+ this.floorMat = new THREE.MeshStandardMaterial({ roughness: 0.9, metalness: 0.1 });
34
+ this.floorMesh = new THREE.Mesh(floorGeo, this.floorMat);
35
+ this.floorMesh.rotation.x = -Math.PI / 2;
36
+ this.floorMesh.position.y = -50; // Just below the board
37
+ this.floorMesh.receiveShadow = true;
38
+ this.scene.add(this.floorMesh);
39
+
40
+ // For Kilwa night torches
41
+ this.pointLights = [];
42
+
43
+ // For environment specific decorations (grass, fish)
44
+ this.decorations = new THREE.Group();
45
+ this.scene.add(this.decorations);
46
+ }
47
+
48
+ // Call this to set a specific environment
49
+ setEnvironment(name) {
50
+ // Cleanup old torch lights
51
+ this.pointLights.forEach(l => this.scene.remove(l));
52
+ this.pointLights = [];
53
+
54
+ // Cleanup decorations
55
+ this.decorations.clear();
56
+
57
+ if (name === 'zanzibar') {
58
+ this.setupZanzibar();
59
+ } else if (name === 'serengeti') {
60
+ this.setupSerengeti();
61
+ } else if (name === 'kilwa') {
62
+ this.setupKilwa();
63
+ }
64
+
65
+ this.updateEnvironmentMap();
66
+ }
67
+
68
+ cycleEnvironment() {
69
+ this.currentEnvIndex = (this.currentEnvIndex + 1) % this.environments.length;
70
+ const nextEnv = this.environments[this.currentEnvIndex];
71
+ this.setEnvironment(nextEnv);
72
+ return nextEnv;
73
+ }
74
+
75
+ updateEnvironmentMap() {
76
+ // We create a temporary scene to capture the sky and lights for PBR reflections
77
+ // This is much faster than loading a large HDR file
78
+ const pmremScene = new THREE.Scene();
79
+ pmremScene.background = this.scene.background;
80
+ const envMap = this.pmremGenerator.fromScene(pmremScene).texture;
81
+ this.scene.environment = envMap;
82
+ }
83
+
84
+ generateNoiseTexture(baseColorHex, noiseColorHex, density, scale = 1.0) {
85
+ const canvas = document.createElement('canvas');
86
+ canvas.width = 512;
87
+ canvas.height = 512;
88
+ const ctx = canvas.getContext('2d');
89
+
90
+ ctx.fillStyle = baseColorHex;
91
+ ctx.fillRect(0, 0, 512, 512);
92
+
93
+ // Add simple noise
94
+ for (let i = 0; i < 512 * 512 * density; i++) {
95
+ const x = Math.random() * 512;
96
+ const y = Math.random() * 512;
97
+ ctx.fillStyle = noiseColorHex;
98
+ ctx.fillRect(x, y, scale, scale);
99
+ }
100
+
101
+ const texture = new THREE.CanvasTexture(canvas);
102
+ texture.wrapS = THREE.RepeatWrapping;
103
+ texture.wrapT = THREE.RepeatWrapping;
104
+ texture.repeat.set(20, 20);
105
+ texture.colorSpace = THREE.SRGBColorSpace;
106
+ return texture;
107
+ }
108
+
109
+ generateStoneTexture() {
110
+ const canvas = document.createElement('canvas');
111
+ canvas.width = 512;
112
+ canvas.height = 512;
113
+ const ctx = canvas.getContext('2d');
114
+
115
+ ctx.fillStyle = '#2c2e2c'; // Dark mortar
116
+ ctx.fillRect(0, 0, 512, 512);
117
+
118
+ // Draw rough stones
119
+ const cols = 8;
120
+ const rows = 8;
121
+ const cellW = 512 / cols;
122
+ const cellH = 512 / rows;
123
+
124
+ for (let r = 0; r < rows; r++) {
125
+ for (let c = 0; c < cols; c++) {
126
+ // Random offset for organic look
127
+ const ox = (Math.random() - 0.5) * 10;
128
+ const oy = (Math.random() - 0.5) * 10;
129
+
130
+ const stoneBaseColor = 100 + Math.random() * 40; // grey variations
131
+ ctx.fillStyle = `rgb(${stoneBaseColor}, ${stoneBaseColor + 5}, ${stoneBaseColor + 10})`;
132
+
133
+ ctx.beginPath();
134
+ // Draw irregular polygon in cell
135
+ const margin = 6;
136
+ const x = c * cellW + margin + ox;
137
+ const y = r * cellH + margin + oy;
138
+ const w = cellW - margin * 2;
139
+ const h = cellH - margin * 2;
140
+
141
+ ctx.moveTo(x + Math.random()*10, y + Math.random()*10);
142
+ ctx.lineTo(x + w - Math.random()*10, y + Math.random()*10);
143
+ ctx.lineTo(x + w - Math.random()*10, y + h - Math.random()*10);
144
+ ctx.lineTo(x + Math.random()*10, y + h - Math.random()*10);
145
+ ctx.closePath();
146
+ ctx.fill();
147
+ }
148
+ }
149
+
150
+ // Add some noise over everything
151
+ for (let i = 0; i < 10000; i++) {
152
+ ctx.fillStyle = `rgba(0,0,0,${Math.random() * 0.3})`;
153
+ ctx.fillRect(Math.random() * 512, Math.random() * 512, 2, 2);
154
+ }
155
+
156
+ const texture = new THREE.CanvasTexture(canvas);
157
+ texture.wrapS = THREE.RepeatWrapping;
158
+ texture.wrapT = THREE.RepeatWrapping;
159
+ texture.repeat.set(15, 15);
160
+ texture.colorSpace = THREE.SRGBColorSpace;
161
+ return texture;
162
+ }
163
+
164
+ setupZanzibar() {
165
+ // Sunset atmosphere
166
+ this.scene.background = new THREE.Color(0xffaa66); // Warm sunset orange/pink
167
+ this.scene.fog = new THREE.FogExp2(0xffaa66, 0.0005);
168
+
169
+ this.dirLight.color.setHex(0xffddaa);
170
+ this.dirLight.intensity = 2.0;
171
+ this.dirLight.position.set(-150, 100, 300);
172
+
173
+ this.hemiLight.color.setHex(0xffaa66);
174
+ this.hemiLight.groundColor.setHex(0x4444cc); // Blueish water/sky reflection
175
+ this.hemiLight.intensity = 1.0;
176
+
177
+ // Sand texture
178
+ const sandTex = this.generateNoiseTexture('#e6d0ab', '#cbb083', 0.15, 2);
179
+ this.floorMat.map = sandTex;
180
+ this.floorMat.roughness = 0.9;
181
+ this.floorMat.needsUpdate = true;
182
+
183
+ this.addZanzibarFishes();
184
+ }
185
+
186
+ setupSerengeti() {
187
+ // Midday savanna
188
+ this.scene.background = new THREE.Color(0x88ccff); // Clear sky blue
189
+ this.scene.fog = new THREE.FogExp2(0x88ccff, 0.0003);
190
+
191
+ this.dirLight.color.setHex(0xffffff);
192
+ this.dirLight.intensity = 2.5;
193
+ this.dirLight.position.set(100, 400, 100);
194
+
195
+ this.hemiLight.color.setHex(0xffffff);
196
+ this.hemiLight.groundColor.setHex(0x8b7765); // Dirt
197
+ this.hemiLight.intensity = 0.8;
198
+
199
+ // Dry grass / dirt texture
200
+ const dirtTex = this.generateNoiseTexture('#8b7765', '#a2907c', 0.2, 1.5);
201
+ this.floorMat.map = dirtTex;
202
+ this.floorMat.roughness = 1.0;
203
+ this.floorMat.needsUpdate = true;
204
+
205
+ this.addSerengetiGrass();
206
+ }
207
+
208
+ setupKilwa() {
209
+ // Dusk/Night ruins
210
+ this.scene.background = new THREE.Color(0x1a2b4c); // Deep night blue
211
+ this.scene.fog = new THREE.FogExp2(0x1a2b4c, 0.001);
212
+
213
+ // Moonlight
214
+ this.dirLight.color.setHex(0xaaccff);
215
+ this.dirLight.intensity = 2.5; // Increased significantly to make board visible
216
+ this.dirLight.position.set(-200, 300, -200);
217
+
218
+ this.hemiLight.color.setHex(0x2a3b5c);
219
+ this.hemiLight.groundColor.setHex(0x222222);
220
+ this.hemiLight.intensity = 1.2; // Increased ambient light
221
+
222
+ // Stone texture
223
+ const stoneTex = this.generateStoneTexture();
224
+ this.floorMat.map = stoneTex;
225
+ this.floorMat.roughness = 0.8;
226
+ this.floorMat.needsUpdate = true;
227
+
228
+ // Add warm torches (point lights)
229
+ const torchColors = [0xffaa00, 0xff8800];
230
+ const torchPositions = [
231
+ [-300, 50, -300],
232
+ [300, 50, 300],
233
+ [-200, 50, 300],
234
+ [300, 50, -200]
235
+ ];
236
+
237
+ torchPositions.forEach(pos => {
238
+ const light = new THREE.PointLight(torchColors[Math.floor(Math.random()*torchColors.length)], 50000, 600);
239
+ light.position.set(pos[0], pos[1], pos[2]);
240
+ this.scene.add(light);
241
+ this.pointLights.push(light);
242
+ });
243
+ }
244
+
245
+ addSerengetiGrass() {
246
+ const grassGeo = new THREE.ConeGeometry(4, 20, 3);
247
+ const grassMat = new THREE.MeshStandardMaterial({ color: 0x6ab04c, roughness: 1.0 });
248
+ const dummy = new THREE.Object3D();
249
+
250
+ const count = 300;
251
+ const instancedMesh = new THREE.InstancedMesh(grassGeo, grassMat, count);
252
+ instancedMesh.castShadow = true;
253
+ instancedMesh.receiveShadow = true;
254
+
255
+ for (let i = 0; i < count; i++) {
256
+ // Randomly scatter, avoiding the center board
257
+ let x, z;
258
+ do {
259
+ x = (Math.random() - 0.5) * 2000;
260
+ z = (Math.random() - 0.5) * 2000;
261
+ } while (Math.abs(x) < 250 && Math.abs(z) < 200);
262
+
263
+ dummy.position.set(x, -40, z);
264
+ // Random slight tilt
265
+ dummy.rotation.x = (Math.random() - 0.5) * 0.2;
266
+ dummy.rotation.z = (Math.random() - 0.5) * 0.2;
267
+ dummy.rotation.y = Math.random() * Math.PI * 2;
268
+
269
+ // Random scale
270
+ const scale = 0.5 + Math.random() * 1.5;
271
+ dummy.scale.set(scale, scale, scale);
272
+
273
+ dummy.updateMatrix();
274
+ instancedMesh.setMatrixAt(i, dummy.matrix);
275
+ }
276
+ this.decorations.add(instancedMesh);
277
+ }
278
+
279
+ addZanzibarFishes() {
280
+ // Create a simple cartoonish fish
281
+ const bodyGeo = new THREE.SphereGeometry(10, 16, 16);
282
+ bodyGeo.scale(1.5, 0.8, 0.5); // Stretch into fish shape
283
+ const tailGeo = new THREE.ConeGeometry(5, 10, 3);
284
+ tailGeo.rotateZ(-Math.PI / 2);
285
+ tailGeo.translate(-15, 0, 0);
286
+
287
+ // Merge into one fish geometry by using group
288
+ const colors = [0xff5555, 0x55ff55, 0x5555ff, 0xffff55, 0xffaa00];
289
+
290
+ for (let i = 0; i < 40; i++) {
291
+ const fishGroup = new THREE.Group();
292
+
293
+ const mat = new THREE.MeshStandardMaterial({
294
+ color: colors[Math.floor(Math.random() * colors.length)],
295
+ roughness: 0.3
296
+ });
297
+ const body = new THREE.Mesh(bodyGeo, mat);
298
+ const tail = new THREE.Mesh(tailGeo, mat);
299
+
300
+ body.castShadow = true;
301
+ tail.castShadow = true;
302
+ fishGroup.add(body);
303
+ fishGroup.add(tail);
304
+
305
+ // Randomly scatter
306
+ let x, z;
307
+ do {
308
+ x = (Math.random() - 0.5) * 1500;
309
+ z = (Math.random() - 0.5) * 1500;
310
+ } while (Math.abs(x) < 250 && Math.abs(z) < 200);
311
+
312
+ fishGroup.position.set(x, -45 + Math.random() * 10, z); // Floating just above/on sand
313
+ fishGroup.rotation.y = Math.random() * Math.PI * 2;
314
+
315
+ // Random scale
316
+ const scale = 0.4 + Math.random() * 0.8;
317
+ fishGroup.scale.set(scale, scale, scale);
318
+
319
+ this.decorations.add(fishGroup);
320
+ }
321
+ }
322
+ }
public/js/GameScene.js CHANGED
@@ -1,6 +1,6 @@
1
  import * as THREE from 'three';
2
  import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
3
- import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
4
  import { loadGameAssets } from './gameObjects.js';
5
 
6
  export default class GameScene {
@@ -293,36 +293,10 @@ export default class GameScene {
293
  this.controls.minDistance = 150; this.controls.maxDistance = 1000;
294
  this.controls.minPolarAngle = Math.PI / 4; this.controls.maxPolarAngle = Math.PI / 2.2;
295
 
296
- this._setLoadingProgress(10, 'Loading environment…');
297
 
298
- const rgbeLoader = new RGBELoader();
299
- const textureLoader = new THREE.TextureLoader();
300
- const GCS_ASSET_URL = './assets/';
301
-
302
- // Load HDR environment
303
- await new Promise((resolve) => {
304
- rgbeLoader.load(GCS_ASSET_URL + 'room_env.hdr', (t) => {
305
- t.mapping = THREE.EquirectangularReflectionMapping;
306
- this.scene.background = t;
307
- this.scene.environment = t;
308
- resolve();
309
- });
310
- });
311
-
312
- this._setLoadingProgress(35, 'Loading table texture…');
313
-
314
- const directionalLight = new THREE.DirectionalLight(0xffffff, 2.0);
315
- directionalLight.position.set(-150, 200, 300); directionalLight.castShadow = true;
316
- directionalLight.shadow.mapSize.width = 4096; directionalLight.shadow.mapSize.height = 4096;
317
- this.scene.add(directionalLight);
318
-
319
- const floorGeometry = new THREE.PlaneGeometry(5000, 5000);
320
- const floorTexture = await textureLoader.loadAsync(GCS_ASSET_URL + 'table_texture.jpg');
321
- floorTexture.wrapS = THREE.RepeatWrapping; floorTexture.wrapT = THREE.RepeatWrapping; floorTexture.repeat.set(5, 5);
322
- const floorMaterial = new THREE.MeshStandardMaterial({ map: floorTexture, roughness: 0.7 });
323
- const floorMesh = new THREE.Mesh(floorGeometry, floorMaterial);
324
- floorMesh.rotation.x = -Math.PI / 2; floorMesh.position.y = -50; floorMesh.receiveShadow = true;
325
- this.scene.add(floorMesh);
326
 
327
  this._setLoadingProgress(55, 'Loading board model…');
328
 
@@ -352,6 +326,12 @@ export default class GameScene {
352
  this._hideLoadingOverlay();
353
  }
354
 
 
 
 
 
 
 
355
  // --- Update the player panel with P1/P2 labels, YOU badge, and turn indicator ---
356
  _updatePlayerPanel(gameState, myProfileId) {
357
  const isPlayer1 = myProfileId === gameState.player1_id;
 
1
  import * as THREE from 'three';
2
  import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
3
+ import EnvironmentManager from './EnvironmentManager.js';
4
  import { loadGameAssets } from './gameObjects.js';
5
 
6
  export default class GameScene {
 
293
  this.controls.minDistance = 150; this.controls.maxDistance = 1000;
294
  this.controls.minPolarAngle = Math.PI / 4; this.controls.maxPolarAngle = Math.PI / 2.2;
295
 
296
+ this._setLoadingProgress(20, 'Building environment…');
297
 
298
+ this.envManager = new EnvironmentManager(this.scene, this.renderer);
299
+ this.envManager.setEnvironment('zanzibar'); // Default environment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  this._setLoadingProgress(55, 'Loading board model…');
302
 
 
326
  this._hideLoadingOverlay();
327
  }
328
 
329
+ cycleEnvironment() {
330
+ if (this.envManager) {
331
+ return this.envManager.cycleEnvironment();
332
+ }
333
+ }
334
+
335
  // --- Update the player panel with P1/P2 labels, YOU badge, and turn indicator ---
336
  _updatePlayerPanel(gameState, myProfileId) {
337
  const isPlayer1 = myProfileId === gameState.player1_id;
public/js/gameObjects.js CHANGED
@@ -58,6 +58,7 @@ if (object.isMesh && object.name === 'BaoBoard_WoodBoard_0') {
58
  console.log("Found the main interactive board mesh:", object.name);
59
  object.material = mainBoardMaterial;
60
  object.receiveShadow = true;
 
61
 
62
  // Store this single mesh for raycasting.
63
  interactiveBoardMesh = object;
 
58
  console.log("Found the main interactive board mesh:", object.name);
59
  object.material = mainBoardMaterial;
60
  object.receiveShadow = true;
61
+ object.castShadow = true;
62
 
63
  // Store this single mesh for raycasting.
64
  interactiveBoardMesh = object;
public/js/main.js CHANGED
@@ -382,6 +382,19 @@ document.getElementById('back-to-lobby-btn').addEventListener('click', async ()
382
  await showLobby();
383
  });
384
 
 
 
 
 
 
 
 
 
 
 
 
 
 
385
  // =========================================================
386
  // GAME: pit click & state update
387
  // =========================================================
 
382
  await showLobby();
383
  });
384
 
385
+ document.getElementById('switch-env-btn').addEventListener('click', () => {
386
+ if (gameSceneInstance) {
387
+ const nextEnv = gameSceneInstance.cycleEnvironment();
388
+ const btn = document.getElementById('switch-env-btn');
389
+ const labels = {
390
+ 'zanzibar': '🌍 Environment: Zanzibar Coast',
391
+ 'serengeti': '🌍 Environment: Serengeti Plains',
392
+ 'kilwa': '🌍 Environment: Kilwa Ruins'
393
+ };
394
+ btn.textContent = labels[nextEnv] || '🌍 Switch Environment';
395
+ }
396
+ });
397
+
398
  // =========================================================
399
  // GAME: pit click & state update
400
  // =========================================================
public/style.css CHANGED
@@ -461,7 +461,7 @@ input::placeholder { color: var(--cream-dim); opacity: 0.5; }
461
 
462
  #ui-overlay {
463
  position: absolute;
464
- top: 20px; left: 20px;
465
  color: var(--cream);
466
  text-shadow: 1px 1px 4px rgba(0,0,0,0.8);
467
  pointer-events: none;
@@ -491,6 +491,24 @@ input::placeholder { color: var(--cream-dim); opacity: 0.5; }
491
  }
492
  .back-btn:hover { border-color: var(--gold-dim); color: var(--gold); }
493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
  #game-info-container {
495
  position: fixed;
496
  bottom: 20px; left: 20px;
 
461
 
462
  #ui-overlay {
463
  position: absolute;
464
+ top: 60px; left: 20px;
465
  color: var(--cream);
466
  text-shadow: 1px 1px 4px rgba(0,0,0,0.8);
467
  pointer-events: none;
 
491
  }
492
  .back-btn:hover { border-color: var(--gold-dim); color: var(--gold); }
493
 
494
+ .env-btn {
495
+ position: fixed;
496
+ top: 18px;
497
+ left: 20px;
498
+ z-index: 100;
499
+ padding: 9px 18px;
500
+ background: var(--glass);
501
+ border: 1px solid var(--glass-border);
502
+ border-radius: 20px;
503
+ color: var(--cream);
504
+ font-family: 'Outfit', sans-serif;
505
+ font-size: 0.88rem;
506
+ cursor: pointer;
507
+ transition: var(--transition);
508
+ backdrop-filter: blur(10px);
509
+ }
510
+ .env-btn:hover { border-color: var(--gold-dim); color: var(--gold); }
511
+
512
  #game-info-container {
513
  position: fixed;
514
  bottom: 20px; left: 20px;