-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.js
More file actions
1206 lines (986 loc) · 47.2 KB
/
player.js
File metadata and controls
1206 lines (986 loc) · 47.2 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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { LANES, COLORS, GAME_CONFIG, PHYSICS } from './constants.js';
export class Player {
constructor(scene) {
this.scene = scene;
this.mesh = null;
this.lane = LANES.CENTER;
this.isJumping = false;
this.velocityY = 0;
this.hasDoubleJumped = false;
this.canDoubleJump = false;
// Sliding state
this.isSliding = false;
this.slideStartTime = 0;
this.slideDuration = 800; // 0.8 seconds
// Animation system - running + flying + stumble + sliding
this.mixer = null;
this.currentAction = null;
this.stumbleAction = null;
this.stumbleMesh = null;
this.stumbleMixer = null;
this.flyingAction = null;
this.flyingMesh = null;
this.flyingMixer = null;
this.slidingAction = null;
this.slidingMesh = null;
this.slidingMixer = null;
this.clock = new THREE.Clock();
// Flying state
this.isFlying = false;
this.flyingHoverOffset = 0; // For subtle hovering motion
this.flyingTime = 0; // Track time spent flying
// Stumble state
this.isStumbling = false;
this.stumbleEndTime = 0;
this.stumbleAnimationDuration = 1500; // Default 1.5 seconds max, will be updated when animation loads
this.stumbleSpeedMultiplier = 1.0; // Speed multiplier for animation playback
this.gameOverCallback = null; // Callback to trigger game over after stumble
// Ready state (for countdown)
this.isReady = false;
this.readyAnimationTime = 0;
}
async initialize() {
await this.createSimplePlayer();
}
async createSimplePlayer() {
// Load running animation first
await this.loadRunningAnimation();
// Then load flying animation
await this.loadFlyingAnimation();
// Load stumble animation
await this.loadStumbleAnimation();
// Finally load sliding animation
await this.loadSlidingAnimation();
}
async loadRunningAnimation() {
return new Promise((resolve, reject) => {
const loader = new GLTFLoader();
// Setup DRACO loader for compressed GLB files
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/');
loader.setDRACOLoader(dracoLoader);
loader.load(
'/assets/models/Running.glb',
(gltf) => {
this.mesh = gltf.scene;
this.mesh.scale.set(0.8, 0.8, 0.8);
this.mesh.rotation.y = Math.PI;
this.mesh.visible = true;
// Position properly
const visualOffset = GAME_CONFIG.PLAYER_VISUAL_OFFSET;
this.mesh.position.set(0, GAME_CONFIG.GROUND_HEIGHT + visualOffset, 0);
// Setup shadows consistently
this.mesh.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
this.scene.add(this.mesh);
// Setup running animation
if (gltf.animations && gltf.animations.length > 0) {
this.mixer = new THREE.AnimationMixer(this.mesh);
this.currentAction = this.mixer.clipAction(gltf.animations[0]);
this.currentAction.setLoop(THREE.LoopRepeat);
this.currentAction.play();
}
resolve();
},
undefined,
(error) => {
this.createFallbackPlayer();
resolve();
}
);
});
}
async loadSlidingAnimation() {
return new Promise((resolve, reject) => {
const loader = new GLTFLoader();
// Setup DRACO loader for compressed GLB files
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/');
loader.setDRACOLoader(dracoLoader);
console.log('Loading sliding animation from: /assets/models/Running-Slide.glb');
loader.load(
'/assets/models/Running-Slide.glb',
(gltf) => {
console.log('Sliding animation loaded successfully:', gltf);
// Create sliding mesh with appropriate scale
this.slidingMesh = gltf.scene;
this.slidingMesh.scale.set(0.8, 0.8, 0.8); // Match running mesh scale
this.slidingMesh.rotation.y = Math.PI;
this.slidingMesh.visible = false; // Hide initially
// Position same as running mesh
const visualOffset = GAME_CONFIG.PLAYER_VISUAL_OFFSET;
this.slidingMesh.position.set(0, GAME_CONFIG.GROUND_HEIGHT + visualOffset, 0);
// Store reference to running character materials for copying
this.runningMaterials = [];
if (this.mesh) {
this.mesh.traverse((child) => {
if (child.isMesh && child.material) {
this.runningMaterials.push(child.material);
}
});
}
// Setup shadows and lighting to match running character
let materialIndex = 0;
this.slidingMesh.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
// Copy material from running character if available
if (this.runningMaterials[materialIndex] && child.material) {
const runningMaterial = this.runningMaterials[materialIndex];
// Clone the running character's material
child.material = runningMaterial.clone();
child.material.needsUpdate = true;
}
materialIndex++;
}
});
this.scene.add(this.slidingMesh);
console.log('Sliding mesh added to scene');
console.log('Sliding mesh details:', {
position: this.slidingMesh.position,
scale: this.slidingMesh.scale,
rotation: this.slidingMesh.rotation,
visible: this.slidingMesh.visible,
children: this.slidingMesh.children.length
});
// Setup sliding animation
if (gltf.animations && gltf.animations.length > 0) {
this.slidingMixer = new THREE.AnimationMixer(this.slidingMesh);
this.slidingAction = this.slidingMixer.clipAction(gltf.animations[0]);
this.slidingAction.setLoop(THREE.LoopRepeat);
console.log('Sliding animation setup complete');
// Don't play yet - will be triggered when sliding starts
} else {
console.warn('No animations found in sliding model');
}
resolve();
},
(progress) => {
console.log('Loading sliding animation progress:', progress);
},
(error) => {
console.error('Failed to load sliding animation:', error);
resolve(); // Continue without sliding animation
}
);
});
}
async loadFlyingAnimation() {
return new Promise((resolve, reject) => {
const loader = new GLTFLoader();
// Setup DRACO loader for compressed GLB files
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/');
loader.setDRACOLoader(dracoLoader);
loader.load(
'/assets/models/Flying.glb',
(gltf) => {
// Create flying mesh
this.flyingMesh = gltf.scene;
this.flyingMesh.scale.set(0.08, 0.08, 0.08); // Much smaller to prevent parade balloon effect
this.flyingMesh.rotation.y = Math.PI;
this.flyingMesh.visible = false; // Hide initially
// Position same as running mesh
const visualOffset = GAME_CONFIG.PLAYER_VISUAL_OFFSET;
this.flyingMesh.position.set(0, GAME_CONFIG.GROUND_HEIGHT + visualOffset, 0);
// Setup shadows consistently
this.flyingMesh.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
this.scene.add(this.flyingMesh);
// Create flying mixer and action for static pose
if (gltf.animations && gltf.animations.length > 0) {
this.flyingMixer = new THREE.AnimationMixer(this.flyingMesh);
this.flyingAction = this.flyingMixer.clipAction(gltf.animations[0]);
this.flyingAction.setLoop(THREE.LoopOnce); // Only play once
this.flyingAction.clampWhenFinished = true; // Stay at end frame
}
resolve();
},
undefined,
(error) => {
resolve(); // Continue without flying animation
}
);
});
}
async loadStumbleAnimation() {
return new Promise((resolve, reject) => {
const loader = new GLTFLoader();
// Setup DRACO loader for compressed GLB files
const dracoLoader = new DRACOLoader();
dracoLoader.setDecoderPath('https://www.gstatic.com/draco/versioned/decoders/1.5.6/');
loader.setDRACOLoader(dracoLoader);
loader.load(
'/assets/models/Stumble Backwards.glb',
(gltf) => {
// Create stumble animation action if we have a mixer
if (this.mixer && gltf.animations && gltf.animations.length > 0) {
// Create a separate mesh for stumble animation
this.stumbleMesh = gltf.scene;
// Use even smaller scale to make stumble character smaller
this.stumbleMesh.scale.set(0.008, 0.008, 0.008);
this.stumbleMesh.rotation.y = Math.PI;
this.stumbleMesh.visible = false; // Hide initially
// Position same as running mesh with tiny offset for extremely small model
this.stumbleMesh.position.copy(this.mesh.position);
this.stumbleMesh.position.y -= 0.015; // Smaller offset for smaller 0.008 scale model
// Store reference to running character materials for copying
this.runningMaterials = [];
if (this.mesh) {
this.mesh.traverse((child) => {
if (child.isMesh && child.material) {
this.runningMaterials.push(child.material);
}
});
}
// Setup shadows and lighting to match running character
let materialIndex = 0;
this.stumbleMesh.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
// Copy material from running character if available
if (this.runningMaterials[materialIndex] && child.material) {
const runningMaterial = this.runningMaterials[materialIndex];
// Clone the running character's material
child.material = runningMaterial.clone();
child.material.needsUpdate = true;
}
materialIndex++;
}
});
this.scene.add(this.stumbleMesh);
// Create stumble mixer and action
this.stumbleMixer = new THREE.AnimationMixer(this.stumbleMesh);
this.stumbleAction = this.stumbleMixer.clipAction(gltf.animations[0]);
this.stumbleAction.setLoop(THREE.LoopOnce);
this.stumbleAction.clampWhenFinished = true;
// Store the animation duration for proper timing, but cap it at 1.5 seconds max for better game flow
const actualDuration = gltf.animations[0].duration * 1000; // Convert to milliseconds
this.stumbleAnimationDuration = Math.min(actualDuration, 1500); // Cap at 1.5 seconds maximum
// Calculate speed multiplier to fit animation in shorter time if needed
this.stumbleSpeedMultiplier = actualDuration / this.stumbleAnimationDuration;
} else {
}
resolve();
},
undefined,
(error) => {
// Continue without stumble animation
resolve();
}
);
});
}
createFallbackPlayer() {
const geometry = new THREE.BoxGeometry(0.5, 1, 0.5);
const material = new THREE.MeshStandardMaterial({ color: COLORS.PLAYER.DEFAULT });
this.mesh = new THREE.Mesh(geometry, material);
const visualOffset = GAME_CONFIG.PLAYER_VISUAL_OFFSET;
this.mesh.position.set(0, GAME_CONFIG.GROUND_HEIGHT + visualOffset, 0);
this.mesh.castShadow = true;
this.mesh.receiveShadow = true;
this.mesh.visible = true;
this.scene.add(this.mesh);
}
// Method to trigger stumble animation when hitting obstacles
triggerStumble(gameOverCallback = null) {
if (!this.stumbleAction || !this.stumbleMesh) {
return false; // Return false to indicate we should do game over instead
}
// Store the game over callback for when stumble ends
this.gameOverCallback = gameOverCallback;
this.isStumbling = true;
this.stumbleEndTime = Date.now() + this.stumbleAnimationDuration; // Use actual animation duration
// Sync positions before switching (with tiny stumble offset)
this.stumbleMesh.position.copy(this.mesh.position);
this.stumbleMesh.position.y -= 0.015; // Apply smaller stumble-specific offset
// Hide running mesh and show stumble mesh
this.mesh.visible = false;
this.stumbleMesh.visible = true;
// Start stumble animation with explicit configuration
this.stumbleAction.reset();
this.stumbleAction.time = 0; // Start from beginning
this.stumbleAction.enabled = true;
this.stumbleAction.setEffectiveWeight(1.0);
// Speed up animation if needed to fit in shorter duration
const timeScale = this.stumbleSpeedMultiplier || 1.0;
this.stumbleAction.setEffectiveTimeScale(timeScale);
this.stumbleAction.play();
// Force the mixer to update immediately
this.stumbleMixer.update(0);
// Debug animation state
// Pause/stop running animation completely
if (this.currentAction) {
this.currentAction.stop();
}
if (this.mixer) {
this.mixer.stopAllAction();
}
return true; // Return true to indicate stumble was triggered
}
// Method to end stumble and trigger game over
endStumble() {
this.isStumbling = false;
// Stop stumble animation
if (this.stumbleAction) {
this.stumbleAction.stop();
}
// Hide stumble mesh
if (this.stumbleMesh) {
this.stumbleMesh.visible = false;
}
// Trigger game over through callback
if (this.gameOverCallback) {
this.gameOverCallback();
this.gameOverCallback = null; // Clear the callback
} else {
}
}
moveLeft() {
if (this.lane > LANES.LEFT) {
this.lane--;
}
}
moveRight() {
if (this.lane < LANES.RIGHT) {
this.lane++;
}
}
jump() {
// Can't jump while sliding
if (this.isSliding) return;
// Regular jump if not jumping
if (!this.isJumping) {
console.log('👍 DEBUG: Regular jump executed');
this.isJumping = true;
this.velocityY = GAME_CONFIG.INITIAL_JUMP_VELOCITY;
this.hasDoubleJumped = false;
}
// Double jump if Wind Power is active and we haven't used double jump yet
else if (this.canDoubleJump && !this.hasDoubleJumped) {
console.log(`✨ DEBUG: Double jump executed! (canDoubleJump: ${this.canDoubleJump}, hasDoubleJumped: ${this.hasDoubleJumped})`);
this.velocityY = GAME_CONFIG.DOUBLE_JUMP_VELOCITY;
this.hasDoubleJumped = true;
this.createJumpEffect();
}
}
slide() {
// Can't slide while jumping or already sliding
if (this.isJumping || this.isSliding) return;
this.isSliding = true;
this.slideStartTime = Date.now();
// Debug logging
console.log('Started sliding - slidingMesh:', !!this.slidingMesh, 'slidingAction:', !!this.slidingAction);
if (this.slidingMesh) {
console.log('Sliding mesh visible before switch:', this.slidingMesh.visible);
console.log('Sliding mesh position:', this.slidingMesh.position);
console.log('Sliding mesh scale:', this.slidingMesh.scale);
console.log('Running mesh position for comparison:', this.mesh.position);
// Remove temporary test code that was interfering with animation switching
}
// Immediately switch to sliding animation
this.updateAnimationState(false);
console.log('Started sliding');
}
// Fallback methods for when sliding model is not available
startSlideAnimation() {
if (!this.mesh) return;
// Create sliding pose: crouch down and lean forward
this.originalScale = this.mesh.scale.clone();
this.originalRotation = this.mesh.rotation.clone();
this.originalPosition = this.mesh.position.clone();
// Animate to sliding pose
this.animateToSlide();
}
animateToSlide() {
if (!this.mesh || !this.isSliding) return;
const slideProgress = Math.min((Date.now() - this.slideStartTime) / 200, 1); // 200ms to reach full slide
// Scale down vertically to simulate crouching
const targetScaleY = 0.6;
this.mesh.scale.y = THREE.MathUtils.lerp(this.originalScale.y, targetScaleY, slideProgress);
// Lean forward
const targetRotationX = 0.3; // Lean forward
this.mesh.rotation.x = THREE.MathUtils.lerp(this.originalRotation.x, targetRotationX, slideProgress);
// Lower the position slightly
const targetY = this.originalPosition.y - 0.15;
this.mesh.position.y = THREE.MathUtils.lerp(this.originalPosition.y, targetY, slideProgress);
if (slideProgress < 1) {
requestAnimationFrame(() => this.animateToSlide());
}
}
endSlideAnimation() {
if (!this.mesh || !this.originalScale) return;
// Animate back to normal pose
this.animateFromSlide();
}
animateFromSlide() {
if (!this.mesh || this.isSliding) return;
const returnDuration = 200; // 200ms to return to normal
const startTime = Date.now();
const animate = () => {
if (!this.mesh || this.isSliding) return;
const progress = Math.min((Date.now() - startTime) / returnDuration, 1);
// Return to original scale
this.mesh.scale.y = THREE.MathUtils.lerp(this.mesh.scale.y, this.originalScale.y, progress);
// Return to original rotation
this.mesh.rotation.x = THREE.MathUtils.lerp(this.mesh.rotation.x, this.originalRotation.x, progress);
// Return to original position
this.mesh.position.y = THREE.MathUtils.lerp(this.mesh.position.y, this.originalPosition.y, progress);
if (progress < 1) {
requestAnimationFrame(animate);
} else {
// Ensure exact original values
this.mesh.scale.copy(this.originalScale);
this.mesh.rotation.copy(this.originalRotation);
this.mesh.position.copy(this.originalPosition);
}
};
animate();
}
createJumpEffect() {
const particleCount = 10;
const particles = [];
const particleGeometry = new THREE.SphereGeometry(0.05, 8, 8);
const particleMaterial = new THREE.MeshStandardMaterial({
color: COLORS.PLAYER.WIND_POWER,
transparent: true,
opacity: 0.7
});
for (let i = 0; i < particleCount; i++) {
const particle = new THREE.Mesh(particleGeometry, particleMaterial);
particle.position.copy(this.mesh.position);
particle.userData = {
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 0.1,
Math.random() * 0.1,
(Math.random() - 0.5) * 0.1
),
life: 30
};
this.scene.add(particle);
particles.push(particle);
}
const updateParticles = () => {
let allDead = true;
for (let i = particles.length - 1; i >= 0; i--) {
const particle = particles[i];
particle.position.add(particle.userData.velocity);
particle.userData.life--;
particle.material.opacity = particle.userData.life / 30 * 0.7;
if (particle.userData.life <= 0) {
this.scene.remove(particle);
particles.splice(i, 1);
} else {
allDead = false;
}
}
if (!allDead) {
requestAnimationFrame(updateParticles);
}
};
updateParticles();
}
updatePosition(isFlying, waterSlideObjects, gameSpeed) {
if (!this.mesh) return;
// Handle ready state animation during countdown
if (this.isReady) {
this.updateReadyAnimation();
return; // Exit early - no movement during ready state
}
// Check if stumble should end
if (this.isStumbling && Date.now() > this.stumbleEndTime) {
this.endStumble();
}
// Handle sliding state
if (this.isSliding) {
const currentTime = Date.now();
if (currentTime - this.slideStartTime > this.slideDuration) {
this.isSliding = false;
// Immediately switch back to running animation
this.updateAnimationState(isFlying);
console.log('Ended sliding');
}
}
// Handle animation switching based on state
this.updateAnimationState(isFlying);
// If stumbling, only update animation mixers and exit - no movement allowed
if (this.isStumbling) {
// Update animation mixers - only stumble mixer during stumble
const deltaTime = this.clock.getDelta();
// Don't update main mixer during stumble to save CPU
if (this.stumbleMixer) {
this.stumbleMixer.update(deltaTime);
// Debug stumble animation during playback (commented out for cleaner logs)
// if (this.stumbleAction) {
// }
}
return; // Exit early - no other movement during stumble
}
const targetX = LANES.POSITIONS[this.lane];
const currentX = this.mesh.position.x;
const distance = targetX - currentX;
// --- IMPROVED FIX: Better lane switching with multiple safeguards ---
const absDistance = Math.abs(distance);
if (absDistance < 0.05) {
// Snap to target if very close
this.mesh.position.x = targetX;
} else {
// Calculate movement for this frame
const movement = distance * GAME_CONFIG.LANE_SWITCH_SPEED;
// Ensure minimum movement to prevent getting stuck
const minMovement = 0.02;
if (absDistance > minMovement && Math.abs(movement) < minMovement) {
// Force minimum movement in the correct direction
this.mesh.position.x += Math.sign(distance) * minMovement;
} else {
this.mesh.position.x += movement;
}
// Safety check: if we overshot, snap to target
const newDistance = targetX - this.mesh.position.x;
if (Math.sign(distance) !== Math.sign(newDistance)) {
this.mesh.position.x = targetX;
}
}
// Update all mesh positions (running, flying, stumble, sliding)
if (this.flyingMesh) {
// Always keep flying mesh in sync with main position, but add floating effect when flying
this.flyingMesh.position.x = this.mesh.position.x;
this.flyingMesh.position.z = this.mesh.position.z;
}
if (this.stumbleMesh) {
this.stumbleMesh.position.x = this.mesh.position.x; // Keep X in sync
this.stumbleMesh.position.y = this.mesh.position.y - 0.015; // Maintain smaller stumble Y offset
this.stumbleMesh.position.z = this.mesh.position.z; // Keep Z in sync
}
if (this.slidingMesh) {
this.slidingMesh.position.x = this.mesh.position.x; // Keep X in sync
this.slidingMesh.position.z = this.mesh.position.z; // Keep Z in sync
// Y position is handled by the sliding animation itself
}
// Handle helicopter flying power-up with smooth hovering
if (isFlying) {
this.isFlying = true;
this.flyingTime += 0.016; // Approximate delta time for smooth hovering
const flyHeight = PHYSICS.FLYING_HEIGHT;
// Create more pronounced hovering motion since animation is static
const primaryHover = Math.sin(this.flyingTime * 1.2) * 0.25; // More pronounced
const secondaryHover = Math.sin(this.flyingTime * 2.5) * 0.08; // Subtle secondary
this.flyingHoverOffset = primaryHover + secondaryHover;
const targetHeight = flyHeight + this.flyingHoverOffset;
this.mesh.position.y += (targetHeight - this.mesh.position.y) * 0.08;
if (this.flyingMesh) {
this.flyingMesh.position.y = this.mesh.position.y;
// Log every 60 frames (about once per second)
if (Math.floor(this.flyingTime * 60) % 60 === 0) {
}
}
if (this.stumbleMesh) {
this.stumbleMesh.position.y = this.mesh.position.y - 0.015;
}
} else {
this.isFlying = false;
this.flyingTime = 0;
this.flyingHoverOffset = 0;
}
// Handle normal jumping or falling (but not while stumbling or flying)
if (this.isJumping && !this.isStumbling && !isFlying) {
this.mesh.position.y += this.velocityY;
if (this.flyingMesh) {
this.flyingMesh.position.y = this.mesh.position.y;
}
if (this.stumbleMesh) {
this.stumbleMesh.position.y = this.mesh.position.y - 0.015; // Maintain smaller stumble Y offset
}
this.velocityY += GAME_CONFIG.GRAVITY;
const visualGroundHeight = GAME_CONFIG.GROUND_HEIGHT + GAME_CONFIG.PLAYER_VISUAL_OFFSET;
if (this.mesh.position.y <= visualGroundHeight) {
this.mesh.position.y = visualGroundHeight;
if (this.flyingMesh) {
this.flyingMesh.position.y = visualGroundHeight;
}
if (this.stumbleMesh) {
this.stumbleMesh.position.y = visualGroundHeight - 0.015; // Maintain smaller stumble Y offset
}
this.isJumping = false;
this.velocityY = 0;
this.hasDoubleJumped = false;
}
}
// Water slide guidance
if (waterSlideObjects && waterSlideObjects.length > 0) {
const slideSegment = waterSlideObjects[0];
if (slideSegment) {
let closestLane = 0;
let minDistance = Math.abs(LANES.POSITIONS[0] - slideSegment.position.x);
for (let i = 1; i < LANES.POSITIONS.length; i++) {
const distance = Math.abs(LANES.POSITIONS[i] - slideSegment.position.x);
if (distance < minDistance) {
minDistance = distance;
closestLane = i;
}
}
if (this.lane !== closestLane) {
this.mesh.position.x += (LANES.POSITIONS[closestLane] - this.mesh.position.x) * 0.05;
if (this.flyingMesh) {
this.flyingMesh.position.x = this.mesh.position.x;
}
if (this.stumbleMesh) {
this.stumbleMesh.position.x = this.mesh.position.x; // Keep X in sync only
}
}
}
}
// Update animation mixers with performance optimization
const deltaTime = this.clock.getDelta();
// Only update active mixers to save CPU
if (this.mixer && this.mesh && this.mesh.visible) {
this.mixer.update(deltaTime);
}
// Only update flying mixer when needed
if (this.flyingMixer && this.isFlying && this.flyingMesh && this.flyingMesh.visible) {
// For static flying pose, update much less frequently
if (Math.random() < 0.1) { // Only 10% of frames
this.flyingMixer.update(0);
}
}
// Update sliding mixer when sliding
if (this.slidingMixer && this.isSliding && this.slidingMesh && this.slidingMesh.visible) {
this.slidingMixer.update(deltaTime);
}
// Note: stumbleMixer is only updated during stumble (handled above)
}
updateAnimationState(isFlying) {
// Don't change animations while stumbling
if (this.isStumbling) return;
// Switch to sliding animation
if (this.isSliding && this.slidingMesh && this.slidingAction && !this.slidingMesh.visible) {
console.log('Switching to sliding animation - conditions met');
console.log('Running mesh visible before:', this.mesh.visible);
// Sync position and rotation BEFORE making the mesh visible
this.slidingMesh.position.copy(this.mesh.position);
this.slidingMesh.rotation.copy(this.mesh.rotation);
// Hide running mesh, show sliding mesh
this.mesh.visible = false;
this.slidingMesh.visible = true;
console.log('After visibility switch - running mesh:', this.mesh.visible, 'sliding mesh:', this.slidingMesh.visible);
// Stop running animation
if (this.currentAction) {
this.currentAction.stop();
}
// Start sliding animation
this.slidingAction.reset();
this.slidingAction.enabled = true;
this.slidingAction.setEffectiveWeight(1.0);
this.slidingAction.play();
// Force update to apply the animation immediately
if (this.slidingMixer) {
this.slidingMixer.update(0);
}
console.log('Switched to sliding animation');
}
// Switch back from sliding to running
else if (!this.isSliding && this.slidingMesh && this.slidingMesh.visible) {
// Sync position and rotation BEFORE making the mesh visible
this.mesh.position.copy(this.slidingMesh.position);
this.mesh.rotation.copy(this.slidingMesh.rotation);
// Hide sliding mesh, show running mesh
this.slidingMesh.visible = false;
this.mesh.visible = true;
// Stop sliding animation
if (this.slidingAction) {
this.slidingAction.stop();
}
// Restart running animation
if (this.currentAction) {
this.currentAction.reset();
this.currentAction.enabled = true;
this.currentAction.setEffectiveWeight(1.0);
this.currentAction.play();
}
console.log('Switched back to running animation');
}
// Switch to flying animation
if (isFlying && this.flyingMesh && this.flyingAction && !this.flyingMesh.visible) {
// --- FIX: Sync position and rotation BEFORE making the mesh visible ---
this.flyingMesh.position.copy(this.mesh.position);
this.flyingMesh.rotation.copy(this.mesh.rotation);
// Hide running mesh, show flying mesh
this.mesh.visible = false;
this.flyingMesh.visible = true;
// Stop running animation
if (this.currentAction) {
this.currentAction.stop();
}
// Create completely static flying pose - lock to a good frame
this.flyingAction.reset();
this.flyingAction.enabled = true;
this.flyingAction.setEffectiveWeight(1.0);
// Try to find a good flying pose frame (middle of animation)
const animationDuration = this.flyingAction.getClip().duration;
const goodPoseTime = animationDuration * 0.3; // 30% through animation
this.flyingAction.time = goodPoseTime;
this.flyingAction.setEffectiveTimeScale(0); // Stop animation completely
this.flyingAction.play();
// Force update to apply the pose
if (this.flyingMixer) {
this.flyingMixer.update(0);
}
}
// Switch back to running animation
else if (!isFlying && this.flyingMesh && this.flyingMesh.visible) {
// --- FIX: Sync position and rotation BEFORE making the mesh visible ---
this.mesh.position.copy(this.flyingMesh.position);
this.mesh.rotation.copy(this.flyingMesh.rotation);
// Hide flying mesh, show running mesh
this.flyingMesh.visible = false;
this.mesh.visible = true;
// Reset flying animation for next use
if (this.flyingAction) {
this.flyingAction.stop();
this.flyingAction.setEffectiveTimeScale(1.0); // Reset time scale
}
// Restart running animation
if (this.currentAction) {
this.currentAction.reset();
this.currentAction.enabled = true;
this.currentAction.setEffectiveWeight(1.0);
this.currentAction.play();
}
}
}
getCollisionBox() {
if (!this.mesh) return new THREE.Box3();
// Use the position of the currently visible mesh
let activeMesh = this.mesh;
if (this.isStumbling && this.stumbleMesh && this.stumbleMesh.visible) {
activeMesh = this.stumbleMesh;
} else if (this.flyingMesh && this.flyingMesh.visible) {
activeMesh = this.flyingMesh;
} else if (this.isSliding && this.slidingMesh && this.slidingMesh.visible) {
activeMesh = this.slidingMesh;
}
const playerPos = activeMesh.position;
// Debug logging for collision box
if (this.isStumbling) {
}
// Dynamically adjust collision box width during lane changes
let boxWidth = 0.2; // Base width
let boxHeight = 1.0;
let boxDepth = 0.3;
// Reduce collision box height when sliding
if (this.isSliding) {
boxHeight *= 0.4; // Make collision box 60% shorter when sliding
}
// Shrink collision box width during lane changes
const targetX = LANES.POSITIONS[this.lane];
const currentX = this.mesh.position.x;
const distance = Math.abs(targetX - currentX);
if (distance > 0.05) { // If we're switching lanes
boxWidth *= 0.6; // Make collision box 40% narrower during lane changes
}
const modelVisualCenter = playerPos.y + (boxHeight * 0.15);
const playerBox = new THREE.Box3(
new THREE.Vector3(
playerPos.x - boxWidth / 2,
modelVisualCenter - boxHeight / 2,
playerPos.z - boxDepth / 2
),
new THREE.Vector3(
playerPos.x + boxWidth / 2,
modelVisualCenter + boxHeight / 2,
playerPos.z + boxDepth / 2
)
);
if (GAME_CONFIG.DEBUG_COLLISIONS && this.scene) {
if (!this.collisionBoxHelper) {
this.collisionBoxHelper = new THREE.Box3Helper(playerBox, 0xffff00);
this.scene.add(this.collisionBoxHelper);
} else {
this.collisionBoxHelper.box.copy(playerBox);
}
}
return playerBox;