forked from youngrichu/infra_runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
482 lines (409 loc) · 18.5 KB
/
game.js
File metadata and controls
482 lines (409 loc) · 18.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
import * as THREE from 'three';
import { Player } from './player.js';
import { DirectModelEnvironment } from './direct-model-environment.js';
import { ObstacleManager } from './obstacles.js';
import { CollectableManager } from './collectables.js';
import { PowerUpManager } from './powerups.js';
import { UIManager } from './ui.js';
import { InputManager } from './input.js';
import { GAME_CONFIG, SCORING, SPAWN_CONFIG, PHYSICS } from './constants.js';
import { GameStateManager, STATES } from './game-state.js';
import { LeaderboardManager } from './leaderboard.js';
export class Game {
constructor() {
// Core Three.js components
this.scene = null;
this.camera = null;
this.renderer = null;
// Game managers
this.player = null;
this.environment = null;
this.obstacleManager = null;
this.collectableManager = null;
this.powerUpManager = null;
this.uiManager = null;
this.inputManager = null;
// New managers for state & leaderboard
this.stateManager = new GameStateManager();
this.leaderboardManager = new LeaderboardManager();
// Performance optimization: Cache playing state to avoid checking every frame
this.isCurrentlyPlaying = false;
// Game state
this.gameActive = true;
this.gameSpeed = { value: GAME_CONFIG.INITIAL_SPEED }; // Using object for reference
// CRITICAL FIX: Improved spawning system to handle dual tracking
this.improvedSpawningFix = {
reset: () => {
// Reset any additional spawning state if needed
if (this.collectableManager && this.collectableManager.spawnHistory) {
this.collectableManager.spawnHistory.lastPowerUpDistance = -100;
console.log('✅ Improved spawning system reset - dual tracking synchronized');
}
}
};
this.init();
}
async init() { // Make init async
this.setupThreeJS();
await this.createManagers(); // Await manager creation
this.setupInputManager();
this.setupStateHandlers();
this.startSpawning();
this.animate();
// Kick off splash -> menu flow
this.stateManager.startSplashScreen();
}
setupThreeJS() {
// Scene setup
this.scene = new THREE.Scene();
// Camera setup
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
this.camera.position.z = 5;
this.camera.position.y = 2;
this.camera.lookAt(0, 0, 0);
// Renderer setup
this.renderer = new THREE.WebGLRenderer({ antialias: true });
this.renderer.setSize(window.innerWidth, window.innerHeight);
this.renderer.shadowMap.enabled = true;
document.getElementById('game-container').appendChild(this.renderer.domElement);
}
async createManagers() { // Make createManagers async
// Create managers in dependency order
this.environment = new DirectModelEnvironment(this.scene);
this.player = new Player(this.scene);
await this.player.initialize(); // Await player initialization
this.obstacleManager = new ObstacleManager(this.scene);
this.collectableManager = new CollectableManager(this.scene);
this.powerUpManager = new PowerUpManager(this.scene, this.player);
this.uiManager = new UIManager();
// Set game controller references so managers can access current game state
this.environment.setGameController(this);
this.obstacleManager.setGameController(this);
this.collectableManager.setGameController(this);
// Set game speed reference for power-ups
this.powerUpManager.setGameSpeedReference(this.gameSpeed);
// Set collectable manager reference for power-ups (to remove aerial stars)
this.powerUpManager.setCollectableManager(this.collectableManager);
}
setupInputManager() {
this.inputManager = new InputManager(this.player, this);
// Optional: Enable mobile controls
this.inputManager.setupMobileControls();
}
// ------------------------------------------------------------------
// STATE & UI EVENT HANDLERS
// ------------------------------------------------------------------
setupStateHandlers() {
// React to state changes
this.stateManager.onStateChange((state) => {
// Performance optimization: Cache playing state
this.isCurrentlyPlaying = (state === STATES.PLAYING);
switch (state) {
case STATES.SPLASH:
this.uiManager.showSplash();
break;
case STATES.START_MENU:
this.uiManager.hideSplash();
this.uiManager.hideLeaderboard();
this.uiManager.hideUserInfo();
this.uiManager.showStartMenu();
// Pause game logic
this.gameActive = false;
break;
case STATES.PLAYING:
this.uiManager.hideStartMenu();
this.uiManager.hideLeaderboard();
this.uiManager.hideUserInfo();
this.restartGame(); // Ensure fresh run
this.gameActive = true;
break;
case STATES.USER_INFO:
this.uiManager.showUserInfo();
break;
case STATES.LEADERBOARD:
// Update table then show
this.uiManager.updateLeaderboard(this.leaderboardManager.getScores());
this.uiManager.hideUserInfo();
this.uiManager.showLeaderboard();
// Stop gameplay until menu
this.gameActive = false;
break;
}
});
// Wire UI events
this.uiManager.onStartButtonClicked = () => this.handleStartGame();
this.uiManager.onUserInfoSubmitted = (name) => this.handleUserInfo(name);
this.uiManager.onLeaderboardBack = () => this.stateManager.returnToMenu();
}
handleStartGame() {
this.stateManager.startGame(); // Triggers PLAYING state
}
handleUserInfo(name) {
// Save info into state manager
this.stateManager.saveUserInfo(name);
// Persist to leaderboard
this.leaderboardManager.addScore(this.stateManager.getPlayerData());
// Show leaderboard
this.stateManager.showLeaderboard();
}
getGameSpeed() {
return this.gameSpeed.value;
}
// Public methods for managers to access current game state
// getPlayerPosition() is already defined below
// isGameActive() is already defined below
startSpawning() {
this.obstacleManager.startSpawning();
this.environment.startSpawning();
this.collectableManager.startSpawning();
}
updateGameLogic() {
// Performance optimization: Use cached playing state instead of checking every frame
if (!this.isCurrentlyPlaying || !this.gameActive) return;
// Pause most game logic if player is stumbling
if (this.player.isStumbling) {
// Only update player position to handle stumble animation, but pause everything else
this.player.updatePosition(
this.powerUpManager.getFlyingStatus(),
this.powerUpManager.getWaterSlideObjects(),
this.gameSpeed.value
);
return; // Skip all other game logic during stumble
}
// Update player position
this.player.updatePosition(
this.powerUpManager.getFlyingStatus(),
this.powerUpManager.getWaterSlideObjects(),
this.gameSpeed.value // Pass game speed for animation state management
);
// Update all objects
this.updateAllObjects();
// Check collisions
this.checkCollisions();
// NEW: Fair power-up spawning system (like Subway Surfers)
if (this.collectableManager.shouldSpawnPowerUp()) {
const playerZ = this.player.getPosition().z;
const obstacles = this.obstacleManager.getObstacles();
this.collectableManager.createPowerUp(playerZ, obstacles);
this.collectableManager.markPowerUpSpawned();
}
// Handle aerial collectibles when flying
if (this.powerUpManager.getFlyingStatus() && Math.random() < SPAWN_CONFIG.AERIAL_SPAWN_CHANCE) {
this.collectableManager.createAerialCollectable(this.player.getPosition());
}
// Handle solar orbs when solar boost is active
if (this.powerUpManager.getSolarBoostStatus() && Math.random() < SPAWN_CONFIG.SOLAR_ORB_SPAWN_CHANCE) {
this.collectableManager.createSolarOrb(this.player.getPosition());
}
// Update camera
this.updateCamera();
// Update game speed and score
this.updateGameSpeed();
// Update power-up timers
const frameTime = 1000 / 60; // Assuming 60 FPS
this.powerUpManager.updateTimers(frameTime);
// Update UI timers
this.uiManager.updatePowerUpTimers();
// Apply magnet effect if solar boost is active
if (this.powerUpManager.getSolarBoostStatus()) {
// Performance: Only log occasionally to avoid console spam
if (!this.magnetLogCounter) this.magnetLogCounter = 0;
if (this.magnetLogCounter++ % 300 === 0) {
console.log('✨ DEBUG: Applying magnet effect - Solar boost is active');
}
this.collectableManager.applyMagnetEffect(
this.player.getPosition(),
PHYSICS.MAGNET_RADIUS,
PHYSICS.MAGNET_PULL_SPEED
);
}
}
updateAllObjects() {
// Update obstacles and gain score for passed obstacles
const obstacleScore = this.obstacleManager.updateObstacles(
this.gameSpeed.value,
this.camera.position.z,
this.gameActive
);
this.uiManager.updateScore(obstacleScore);
// Update buildings
this.environment.updateBuildings(this.gameSpeed.value, this.camera.position.z);
// Update collectables
this.collectableManager.updateCollectables(this.gameSpeed.value, this.camera.position.z);
// Update environment
this.environment.updateGround(this.camera.position.z);
// Update water slide if active
this.powerUpManager.updateWaterSlidePosition(this.gameSpeed.value);
}
checkCollisions() {
const playerBox = this.player.getCollisionBox();
// Check obstacle collisions (if not invincible, not stumbling, and not flying)
if (!this.powerUpManager.getInvincibilityStatus() && !this.player.isStumbling && !this.powerUpManager.getFlyingStatus()) {
const collision = this.obstacleManager.checkCollisions(
playerBox,
this.powerUpManager.getWaterSlideObjects(),
this.powerUpManager.getWaterSlideStatus()
);
if (collision) {
// Performance: Reduce collision logging
console.log('*** COLLISION DETECTED ***');
// Try to trigger stumble animation with game over callback
const stumbleTriggered = this.player.triggerStumble(() => this.gameOver());
if (!stumbleTriggered) {
// If stumble animation not available, immediate game over
this.gameOver();
return;
}
return;
}
}
// Check collectable collisions
const collectedItems = this.collectableManager.checkCollisions(playerBox);
this.handleCollectedItems(collectedItems);
}
handleCollectedItems(collectedItems) {
for (const itemType of collectedItems) {
switch (itemType) {
// Regular collectibles
case 'blueprint':
this.uiManager.updateCollectables('blueprint', SCORING.BLUEPRINT);
break;
case 'waterDrop':
this.uiManager.updateCollectables('waterDrop', SCORING.WATER_DROP);
break;
case 'energyCell':
this.uiManager.updateCollectables('energyCell', SCORING.ENERGY_CELL);
break;
case 'aerialStar':
this.uiManager.updateScore(SCORING.AERIAL_STAR);
break;
case 'solarOrb':
this.uiManager.updateScore(SCORING.SOLAR_ORB);
break;
// Power-ups
case 'hardHat':
this.uiManager.updateScore(SCORING.POWER_UP);
this.powerUpManager.activateInvincibility();
this.uiManager.addPowerUpToUI('🛡️ Hard Hat Shield', 5);
this.collectableManager.markPowerUpSpawned(); // Reset fair spawning timer
break;
case 'helicopter':
this.uiManager.updateScore(SCORING.POWER_UP);
this.powerUpManager.activateHelicopter();
this.uiManager.addPowerUpToUI('🚁 Helicopter Ride', 10);
this.collectableManager.markPowerUpSpawned(); // Reset fair spawning timer
break;
case 'solarPower':
this.uiManager.updateScore(SCORING.POWER_UP);
this.powerUpManager.activateSolarPower();
this.uiManager.addPowerUpToUI('🌟 Solar Power Boost', 8);
this.collectableManager.markPowerUpSpawned(); // Reset fair spawning timer
break;
case 'windPower':
this.uiManager.updateScore(SCORING.POWER_UP);
this.powerUpManager.activateWindPower();
this.uiManager.addPowerUpToUI('💨 Wind Power', 15);
this.collectableManager.markPowerUpSpawned(); // Reset fair spawning timer
break;
case 'waterPipeline':
this.uiManager.updateScore(SCORING.POWER_UP);
this.powerUpManager.activateWaterSlide();
this.uiManager.addPowerUpToUI('🚰 Water Pipeline', 12);
this.collectableManager.markPowerUpSpawned(); // Reset fair spawning timer
break;
}
}
}
updateCamera() {
// Smoothly update camera's x position to follow the player
const cameraTargetX = this.player.getPosition().x;
this.camera.position.x += (cameraTargetX - this.camera.position.x) * GAME_CONFIG.CAMERA_FOLLOW_SPEED;
// Enhanced camera Y tracking - different behavior for flying vs. ground
let cameraTargetY;
if (this.powerUpManager && this.powerUpManager.getFlyingStatus()) {
// During flying: higher camera position with smoother tracking
cameraTargetY = this.player.getPosition().y + 1.0; // Closer to flying player
this.camera.position.y += (cameraTargetY - this.camera.position.y) * (GAME_CONFIG.CAMERA_FOLLOW_SPEED * 0.8); // Smoother for flying
} else {
// Normal ground tracking
cameraTargetY = this.player.getPosition().y + 1.5;
this.camera.position.y += (cameraTargetY - this.camera.position.y) * GAME_CONFIG.CAMERA_FOLLOW_SPEED;
}
// Move camera forward
this.camera.position.z -= this.gameSpeed.value;
this.player.setPosition(
this.player.getPosition().x,
this.player.getPosition().y,
this.camera.position.z - 5
);
}
updateGameSpeed() {
// Increase game speed gradually
this.gameSpeed.value += GAME_CONFIG.SPEED_INCREMENT;
// Update score based on solar boost status
if (this.powerUpManager.getSolarBoostStatus()) {
this.uiManager.updateScore(SCORING.SOLAR_BOOST_RATE);
} else {
this.uiManager.updateScore(SCORING.BASE_RATE);
}
}
gameOver() {
this.gameActive = false;
// Capture stats & hand off to state manager
const finalScore = this.uiManager.getScore();
const stats = this.uiManager.getCollectableStats();
this.stateManager.endGame(finalScore, stats);
// Directly transition to user info capture
this.stateManager.showUserInfoScreen();
}
restartGame() {
// Reset game state
this.gameActive = true;
this.gameSpeed.value = GAME_CONFIG.INITIAL_SPEED;
// Reset camera position
this.camera.position.z = 5;
this.camera.position.x = 0;
this.camera.position.y = 2;
// Reset all managers
this.player.reset();
this.environment.reset();
this.obstacleManager.reset();
this.collectableManager.reset();
this.powerUpManager.reset();
this.uiManager.reset();
this.inputManager.reset();
// CRITICAL FIX: Reset improved spawning system that was blocking power-ups
if (this.improvedSpawningFix) {
this.improvedSpawningFix.reset();
console.log('✅ Reset improved spawning system - power-ups will now spawn after restart');
}
// Restart spawning
this.startSpawning();
// Ensure score counters start at zero
this.uiManager.updateScoreDisplay && this.uiManager.updateScoreDisplay();
}
handleWindowResize() {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
}
// Public methods for managers to access current game state
isGameActive() {
return this.gameActive;
}
getPlayerPosition() {
return this.player.getPosition();
}
getObstacles() {
return this.obstacleManager.getObstacles();
}
animate() {
requestAnimationFrame(() => this.animate());
this.updateGameLogic();
this.renderer.render(this.scene, this.camera);
}
}
// Initialize the game when the page loads
document.addEventListener('DOMContentLoaded', () => {
window.game = new Game(); // Make game globally accessible for debugging
});