-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.js
More file actions
560 lines (466 loc) · 17.5 KB
/
Copy pathobjects.js
File metadata and controls
560 lines (466 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import * as THREE from 'three';
import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils.js';
import { wumpa_animation, nitro_animation, dropped_item_animation, gear_animation } from './objects_animations.js';
// Shared texture & material — loaded once, reused across all StandardBox instances.
const _textureLoader = new THREE.TextureLoader();
const BASE = import.meta.env.BASE_URL;
const _texture = _textureLoader.load(`${BASE}textures/blocks/standard.jpg`);
const _textureNitro = _textureLoader.load(`${BASE}textures/blocks/nitro.jpg`);
const _textureNitroUpper = _textureLoader.load(`${BASE}textures/blocks/nitro_upper.jpg`);
const _textureBurubuga = _textureLoader.load(`${BASE}textures/blocks/burubuga.jpg`);
const _textureNewLife = _textureLoader.load(`${BASE}textures/blocks/newlife.png`);
const _textureCrashFace = _textureLoader.load(`${BASE}textures/crashFace.png`);
const _textureQuestionBox = _textureLoader.load(`${BASE}textures/blocks/question_block.png`);
const _material = new THREE.MeshStandardMaterial({ map: _texture });
const _materialNitro = new THREE.MeshStandardMaterial({ map: _textureNitro });
const _materialNitroUpper = new THREE.MeshStandardMaterial({ map: _textureNitroUpper });
const _materialBurubuga = new THREE.MeshStandardMaterial({ map: _textureBurubuga });
const _materialNewLife = new THREE.MeshStandardMaterial({ map: _textureNewLife });
const _materialCrashFace = new THREE.MeshStandardMaterial({ map: _textureCrashFace });
const _materialQuestionBox = new THREE.MeshStandardMaterial({ map: _textureQuestionBox });
const _materialsNitro = [
_materialNitro, // 0
_materialNitro, // 1
_materialNitroUpper, // 2 -> upper face
_materialNitro, // 3
_materialNitro, // 4
_materialNitro, // 5
];
const _materialsBurubuga = [
_materialBurubuga, // 0
_materialBurubuga, // 1
_material, // 2
_materialBurubuga, // 3
_materialBurubuga, // 4
_material, // 5
]
const _materialsQuestionBox = [
_materialQuestionBox, // 0
_materialQuestionBox, // 1
_materialQuestionBox, // 2
_materialQuestionBox, // 3
_materialQuestionBox, // 4
_materialQuestionBox, // 5
]
const _materialsNewLife = [
_materialNewLife, // 0
_materialNewLife, // 1
_materialNewLife, // 2
_materialNewLife, // 3
_materialNewLife, // 4
_materialNewLife, // 5
]
let _wumpaModelCache = null;
// Gem model scenes
const GEM_TYPES = ['gem_blue', 'gem_green', 'gem_purple', 'gem_red', 'gem_yellow'];
const _gemModelCaches = {};
let _newLifeModelCache = null;
// Boundary model scenes
const _boundaryModelCaches = [];
// Gear model scene
let _gearModelCache = null;
// BOX SIZE CONSTANTS
// All boxes use the same world-unit size (2×2×2) so they fit comfortably
// inside a 5-unit lane cell without overlapping neighbouring cells.
const BOX_SIZE = 2;
const BOX_HALF = BOX_SIZE / 2;
const BOX_GEOMETRY = new THREE.BoxGeometry(BOX_SIZE, BOX_SIZE, BOX_SIZE);
/**
* Helper: compute world position and hitbox for a grid-placed box.
* Shared by StandardBox, NitroBox, and BurubugaBox.
*/
function _placeBox(mesh, meshSize, row, col, cumulativePosition) {
// Forward axis: X (same formula as WumpaFruit)
mesh.position.x = (row + cumulativePosition) * meshSize;
// Lane axis: Z (col 0,1,2 → lanes -1,0,1)
mesh.position.z = (col - 1) * meshSize;
// Sit on top of the ground (ground surface is at y = 0)
mesh.position.y = BOX_HALF;
// Axis-aligned hitbox for collision detection
mesh.userData.hitbox = new THREE.Box3(
new THREE.Vector3(
mesh.position.x - BOX_HALF,
mesh.position.y - BOX_HALF,
mesh.position.z - BOX_HALF
),
new THREE.Vector3(
mesh.position.x + BOX_HALF,
mesh.position.y + BOX_HALF,
mesh.position.z + BOX_HALF
)
);
}
/**
* A textured cube with `standard.jpg` applied to all six faces.
*
* Extends THREE.Mesh so it can be added directly to any Object3D / scene:
* tile.add(new StandardBox(meshSize, row, col, cumulativePosition));
*
* The texture and material are shared (created once at module load time)
* to avoid redundant GPU uploads across instances.
*
* @extends THREE.Mesh
*/
export class StandardBox extends THREE.Mesh {
/**
* @param {number} meshSize - World-space size of one grid cell.
* @param {number} row - Row index within the current tile.
* @param {number} col - Column index (0 = left, 1 = center, 2 = right).
* @param {number} cumulativePosition - Tile depth offset along the X axis (in grid units).
*/
constructor(meshSize, row, col, cumulativePosition) {
super(BOX_GEOMETRY, _material);
this.name = 'standard_box';
this.castShadow = true;
this.receiveShadow = true;
_placeBox(this, meshSize, row, col, cumulativePosition);
}
}
/**
* A nitro crate with `nitro.jpg` on the five side/bottom faces
* and `nitro_upper.jpg` on the top (+Y) face.
*
* @extends THREE.Mesh
*/
export class NitroBox extends THREE.Mesh {
constructor(meshSize, row, col, cumulativePosition) {
super(BOX_GEOMETRY, _materialsNitro);
this.name = 'nitro_box';
this.castShadow = true;
this.receiveShadow = true;
_placeBox(this, meshSize, row, col, cumulativePosition);
nitro_animation(this);
}
}
/**
* A burubuga crate with mixed `burubuga.jpg` / `standard.jpg` faces.
*
* @extends THREE.Mesh
*/
export class BurubugaBox extends THREE.Mesh {
constructor(meshSize, row, col, cumulativePosition) {
super(BOX_GEOMETRY, _materialsBurubuga);
this.name = 'burubuga_box';
this.castShadow = true;
this.receiveShadow = true;
_placeBox(this, meshSize, row, col, cumulativePosition);
}
}
export class QuestionBox extends THREE.Mesh {
constructor(meshSize, row, col, cumulativePosition) {
super(BOX_GEOMETRY, _materialsQuestionBox);
this.name = 'question_box';
this.castShadow = true;
this.receiveShadow = true;
_placeBox(this, meshSize, row, col, cumulativePosition);
}
}
export class NewLife extends THREE.Mesh {
constructor(meshSize, row, col, cumulativePosition) {
super(BOX_GEOMETRY, _materialsNewLife);
this.name = 'new_life';
this.castShadow = true;
this.receiveShadow = true;
_placeBox(this, meshSize, row, col, cumulativePosition);
}
}
/**
* Injects the pre-loaded wumpa GLTF scene so WumpaFruit instances can clone it.
* Must be called before the first WumpaFruit is constructed.
*
* @param {THREE.Object3D} model - The root scene from the wumpa GLTF.
*/
export function setWumpaModel(model) {
_wumpaModelCache = model;
}
/**
* Injects the pre-loaded gem GLB scene and parses out the five colour
* variants (gem_blue, gem_green, gem_purple, gem_red, gem_yellow).
* Must be called before the first DroppedGem is constructed.
*
* @param {THREE.Object3D} model - The root scene from the gem GLB.
*/
export function setGemModel(model) {
for (const name of GEM_TYPES) {
const found = model.getObjectByName(name);
if (found) {
_gemModelCaches[name] = found;
} else {
console.warn(`setGemModel: could not find child "${name}" in GLB`);
}
}
}
/**
* Returns one of the remaining gem type names at random.
* @returns {string|null} A gem type, or null if all gems have been collected.
*/
export function getRandomGemType() {
if (GEM_TYPES.length === 0) return null;
return GEM_TYPES[Math.floor(Math.random() * GEM_TYPES.length)];
}
/**
* Removes a gem colour from the available pool so it cannot drop again.
* Safe to call multiple times with the same type.
* @param {string} gemType - e.g. 'gem_purple'
*/
export function removeGemType(gemType) {
const idx = GEM_TYPES.indexOf(gemType);
if (idx !== -1) {
GEM_TYPES.splice(idx, 1);
console.log(`Gem pool updated — remaining: [${GEM_TYPES.join(', ')}]`);
}
}
/**
* Injects the pre-loaded newlife GLTF scene so DroppedLife instances can clone it.
* Must be called before the first DroppedLife is constructed.
*
* @param {THREE.Object3D} model - The root scene from the newlife GLTF.
*/
export function setNewLifeModel(model) {
_newLifeModelCache = model;
}
/**
* Injects the pre-loaded boundary GLTF scenes so BoundaryObject instances can clone them.
* Must be called before the first BoundaryObject is constructed.
*
* @param {{model: THREE.Object3D, config: {name: string, scale: number}}[]} entries
*/
export function setBoundaryModels(entries) {
_boundaryModelCaches.length = 0;
for (const entry of entries) {
_boundaryModelCaches.push({
model: entry.model,
config: {
name: entry.config.name,
scale: entry.config.scale,
},
});
}
}
/**
* Injects the pre-loaded gear glb scene so Gear instances can clone it.
* Must be called before the first Gear is constructed.
*
* @param {THREE.Object3D} model - The root scene from the gear glb.
*/
export function setGearModel(model) {
_gearModelCache = model;
}
/**
* A single Wumpa fruit placed in the world.
*
* Extends THREE.Object3D so it can be added directly to a tile group:
* tile.add(new WumpaFruit(meshSize, row, col, cumulativePosition));
*
* The instance is named "wumpa_fruit" so collision detection in main.js can
* identify it by traversing the scene graph.
*
* @extends THREE.Object3D
*/
export class WumpaFruit extends THREE.Object3D {
/**
* @param {number} meshSize - World-space size of one grid cell.
* @param {number} row - Row index within the current tile.
* @param {number} col - Column index (0 = left, 1 = center, 2 = right).
* @param {number} cumulativePosition - Tile depth offset along the X axis (in grid units).
*/
constructor(meshSize, row, col, cumulativePosition) {
super();
this.name = 'wumpa_fruit';
// Clone the pre-loaded wumpa GLTF model.
// SkeletonUtils.clone() is used instead of .clone() to correctly
// preserve material groups and texture bindings on GLTF scenes.
const object = SkeletonUtils.clone(_wumpaModelCache);
object.scale.set(0.3, 0.3, 0.3);
this.add(object);
// Forward axis: X (row offset within this tile)
this.position.x = (row + cumulativePosition) * meshSize;
// Lane axis: Z (col → 0,1,2 mapped to -1,0,1)
this.position.z = (col - 1) * meshSize;
this.position.y = 1.5; // Height above ground
// Hitbox: axis-aligned box centred on the wumpa's world position.
// Half-extents are intentionally tight (1.2 units) so collection
// feels precise but not frustrating. Queried each frame in main.js.
const halfSize = 1.2;
this.userData.hitbox = new THREE.Box3(
new THREE.Vector3(
this.position.x - halfSize,
this.position.y - halfSize,
this.position.z - halfSize
),
new THREE.Vector3(
this.position.x + halfSize,
this.position.y + halfSize,
this.position.z + halfSize
)
);
// Start the idle animation immediately — every wumpa spins and bobs
// from the moment it is created, no external call required.
wumpa_animation(this);
}
}
/**
* A wumpa fruit spawned at an absolute world position (not grid-based).
* Used when a standard box is broken — the wumpa drops in front of the
* character and can be collected by the existing wumpa collision system
* because it is named 'wumpa_fruit'.
*
* @extends THREE.Object3D
*/
export class DroppedWumpa extends THREE.Object3D {
/**
* @param {number} x - World X position.
* @param {number} y - World Y position.
* @param {number} z - World Z position.
*/
constructor(x, y, z) {
super();
this.name = 'wumpa_fruit';
const object = SkeletonUtils.clone(_wumpaModelCache);
object.scale.set(0.3, 0.3, 0.3);
this.add(object);
this.position.set(x, y, z);
// Tight hitbox identical to the grid-based WumpaFruit
const halfSize = 1.2;
this.userData.hitbox = new THREE.Box3(
new THREE.Vector3(x - halfSize, y - halfSize, z - halfSize),
new THREE.Vector3(x + halfSize, y + halfSize, z + halfSize)
);
// Spawn with a small upward arc then idle spin+bob
dropped_item_animation(this);
}
}
/**
* A 1-Up drop spawned at an absolute world position when a new_life box
* is broken. Rendered using the newlife.glb GLTF model.
* Named 'dropped_life' — collected via dedicated collision logic.
*
* @extends THREE.Object3D
*/
export class DroppedLife extends THREE.Object3D {
/**
* @param {number} x - World X position.
* @param {number} y - World Y position.
* @param {number} z - World Z position.
*/
constructor(x, y, z) {
super();
this.name = 'dropped_life';
const object = SkeletonUtils.clone(_newLifeModelCache);
object.scale.set(1.5, 1.5, 1.5);
object.rotateY(Math.PI / 2);
this.add(object);
this.position.set(x, y, z);
// Tight hitbox for collection
const halfSize = 1.2;
this.userData.hitbox = new THREE.Box3(
new THREE.Vector3(x - halfSize, y - halfSize, z - halfSize),
new THREE.Vector3(x + halfSize, y + halfSize, z + halfSize)
);
// Same upward arc + idle spin+bob as other dropped items
dropped_item_animation(this);
}
}
/**
* A gem drop spawned at an absolute world position when a question_box
* is broken and the random roll selects a gem.
* Uses one of the five pre-loaded gem colour models (set via setGemModel()).
* Named 'dropped_gem' — collected via dedicated collision logic.
*
* @extends THREE.Object3D
*/
export class DroppedGem extends THREE.Object3D {
/**
* @param {number} x - World X position.
* @param {number} y - World Y position.
* @param {number} z - World Z position.
* @param {string} [gemType] - One of GEM_TYPES (e.g. 'gem_red').
* If omitted a random type is chosen.
*/
constructor(x, y, z, gemType) {
super();
this.name = 'dropped_gem';
// Pick a gem type (fall back to random if not provided or invalid)
const type = (gemType && _gemModelCaches[gemType])
? gemType
: getRandomGemType();
this.userData.gemType = type;
const object = SkeletonUtils.clone(_gemModelCaches[type]);
object.scale.set(5, 5, 5);
this.add(object);
this.position.set(x, y, z);
// Tight hitbox for collection
const halfSize = 1.2;
this.userData.hitbox = new THREE.Box3(
new THREE.Vector3(x - halfSize, y - halfSize, z - halfSize),
new THREE.Vector3(x + halfSize, y + halfSize, z + halfSize)
);
// Same upward arc + idle spin+bob as other dropped items
dropped_item_animation(this);
}
}
/**
* Helper function to configure shadows, snap the model to the ground (Y=0),
* and optimize matrices for static decorative objects.
* * @param {THREE.Object3D} object - The cloned GLTF scene object.
*/
function setupStaticModel(object) {
// 1. Traverse the model to enable shadows on all underlying meshes
object.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
const box = new THREE.Box3().setFromObject(object);
const center = new THREE.Vector3();
box.getCenter(center);
object.position.x -= center.x;
object.position.z -= center.z;
object.position.y -= box.min.y;
object.matrixAutoUpdate = false;
object.updateMatrix();
}
/**
* A decorative object placed on the side strips of the map.
* Uses absolute world coordinates (xPos, zPos) and does not participate in collision detection.
*
* @extends THREE.Object3D
*/
export class BoundaryObject extends THREE.Object3D {
/**
* @param {number} modelIndex - Index of the preloaded boundary model.
* @param {number} xPos - Absolute world X position.
* @param {number} zPos - Absolute world Z position.
*/
constructor(modelIndex, xPos, zPos) {
super();
const entry = _boundaryModelCaches[modelIndex];
if (!entry) {
console.warn(`[BoundaryObject] Missing boundary model at index ${modelIndex}`);
return;
}
this.name = entry.config.name;
const object = SkeletonUtils.clone(entry.model);
object.scale.setScalar(entry.config.scale);
object.rotation.y = Math.random() * Math.PI * 2;
setupStaticModel(object);
this.add(object);
this.position.set(xPos, 0, zPos);
this.matrixAutoUpdate = false;
this.updateMatrix();
}
}
export class Gear extends THREE.Object3D {
constructor(xPos, zPos) {
super();
this.name = 'gear';
const object = SkeletonUtils.clone(_gearModelCache);
object.scale.set(5, 5, 5);
// Process shadows, grounding, and matrix optimizations
setupStaticModel(object);
this.add(object);
this.position.set(xPos, 0, zPos);
this.userData.hitboxRadius = 3.5;
}
}