-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameStateManager.js
More file actions
95 lines (74 loc) · 2.03 KB
/
GameStateManager.js
File metadata and controls
95 lines (74 loc) · 2.03 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
const GameStates = Object.freeze({
INITIAL: "initial",
RUNNING: "running",
PAUSED: "paused",
FINAL: "final"
});
const balloonSmoothFactors = {
pink: 2,
blue: 3,
green: 4,
orange: 5,
};
class GameStateManager {
constructor() {
this.state = null;
this.callbacks = [];
this.player = {
name: "Player",
balloonColor: "blue",
time: "N/A",
}
this.opponent = {
name: "Opponent",
balloonColor: "orange",
time: "N/A",
smoothFactor: 5,
}
this.laps = 1;
this.startPoint = "A";
this.winner = null;
}
getState() {
return this.state;
}
startGame(playerBalloonColor, playerName, opponentBalloonColor, laps, startPoint) {
this.player.balloonColor = playerBalloonColor;
this.player.name = playerName;
this.opponent.balloonColor = opponentBalloonColor;
this.laps = laps;
this.startPoint = startPoint;
this.opponent.smoothFactor = balloonSmoothFactors[opponentBalloonColor];
this.setState(GameStates.RUNNING);
}
restartGame() {
this.setState(GameStates.RUNNING);
}
endGame(winner, winnerTime) {
this.winner = winner;
this.winner.time = winnerTime;
this.setState(GameStates.FINAL);
}
startMainMenu() {
this.setState(GameStates.INITIAL);
}
setState(newState) {
if (!Object.values(GameStates).includes(newState)) {
console.error('Invalid GameState: ' + newState);
return;
}
if (this.state === newState) {
return;
}
this.state = newState;
this.callbacks.forEach(callback => callback(newState));
}
onStateChange(callback) {
if (typeof callback === 'function') {
this.callbacks.push(callback);
} else {
console.error('Callback must be a function.');
}
}
}
export { GameStateManager, GameStates };