forked from youngrichu/infra_runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-state.js
More file actions
138 lines (114 loc) · 3.87 KB
/
game-state.js
File metadata and controls
138 lines (114 loc) · 3.87 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
// game-state.js
// Manages different game states and transitions between them
// Game state constants
export const STATES = {
SPLASH: 'splash', // Initial loading/splash screen
START_MENU: 'startMenu', // Main menu screen
PLAYING: 'playing', // Active gameplay
GAME_OVER: 'gameOver', // Game over screen with score display
USER_INFO: 'userInfo', // Collecting user info for leaderboard
LEADERBOARD: 'leaderboard' // Leaderboard display
};
export class GameStateManager {
constructor() {
// Initialize with splash screen state
this.currentState = STATES.SPLASH;
this.previousState = null;
// State change callbacks
this.onStateChangeCallbacks = [];
// Player data for leaderboard
this.playerData = {
name: '',
score: 0,
blueprints: 0,
waterDrops: 0,
energyCells: 0,
date: null
};
// Timer for splash screen auto-transition
this.splashTimer = null;
}
// Change to a new state
changeState(newState) {
if (newState === this.currentState) return;
this.previousState = this.currentState;
this.currentState = newState;
// Notify all listeners about state change
this.notifyStateChange();
// Performance: Only log state changes in debug mode
if (window.gameDebug || this.currentState === 'playing') {
console.log(`Game state changed: ${this.previousState} -> ${this.currentState}`);
}
}
// Get current state
getCurrentState() {
return this.currentState;
}
// Check if a specific state is active
isState(state) {
return this.currentState === state;
}
// Register callback for state changes
onStateChange(callback) {
if (typeof callback === 'function') {
this.onStateChangeCallbacks.push(callback);
}
}
// Notify all listeners about state change
notifyStateChange() {
for (const callback of this.onStateChangeCallbacks) {
callback(this.currentState, this.previousState);
}
}
// Start splash screen with auto-transition to start menu
startSplashScreen(duration = 3000) {
this.changeState(STATES.SPLASH);
// Auto-transition to start menu after duration
this.splashTimer = setTimeout(() => {
this.changeState(STATES.START_MENU);
}, duration);
}
// Start the game
startGame() {
this.changeState(STATES.PLAYING);
}
// End the game and show game over screen
endGame(score, collectableStats) {
// Store score data
this.playerData.score = score;
this.playerData.blueprints = collectableStats.blueprints;
this.playerData.waterDrops = collectableStats.waterDrops;
this.playerData.energyCells = collectableStats.energyCells;
this.playerData.date = new Date();
this.changeState(STATES.GAME_OVER);
}
// Show user info collection screen
showUserInfoScreen() {
this.changeState(STATES.USER_INFO);
}
// Save user info and show leaderboard
saveUserInfo(name) {
this.playerData.name = name;
this.changeState(STATES.LEADERBOARD);
}
// Show leaderboard screen
showLeaderboard() {
this.changeState(STATES.LEADERBOARD);
}
// Return to start menu
returnToMenu() {
this.changeState(STATES.START_MENU);
}
// Get player data
getPlayerData() {
return { ...this.playerData };
}
// Clean up resources
destroy() {
if (this.splashTimer) {
clearTimeout(this.splashTimer);
this.splashTimer = null;
}
this.onStateChangeCallbacks = [];
}
}