-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameState.js
118 lines (113 loc) · 3.21 KB
/
gameState.js
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
const STARTING_PLAYER_LIVES = 3;
const STARTING_BALL_VELOCITY = 3;
global.GameState = {
players: [],
gameActive: false,
getPlayers: function() {
return this.players;
},
getPlayerById: function(playerId) {
return this.players.find(p => p.id === playerId);
},
createPlayer: function(player) {
this.players = this.players.concat([{
...player,
isReady: false,
isCanvasReady: false,
lives: STARTING_PLAYER_LIVES
}]);
},
removePlayerById: function(playerId) {
this.players = this.players.filter(player => player.id !== playerId);
},
removeLifeFromPlayer: function (playerId) {
this.players = this.players.map((player) => {
if (player.id === playerId) {
return {
...player,
lives: player.lives - 1
}
};
return player;
});
return this.players.find(p => p.id === playerId);
},
setPlayerById: function (targetPlayerId, newPlayerProps) {
this.players = this.players.map(player => {
if (player.id === targetPlayerId) {
return Object.assign({}, player, newPlayerProps);
}
return player;
});
},
allPlayersReady: function() {
return this.players.every(player => player.isReady);
},
isGameActive: function() {
return this.gameActive;
},
activateGame: function() {
this.gameActive = true;
this.ballVecity = STARTING_BALL_VELOCITY;
},
resetGame: function() {
this.gameActive = false;
this.players = this.players.map(player => ({
...player,
isReady: false,
isCanvasReady: false,
lives: STARTING_PLAYER_LIVES
}))
},
setPlayerCanvasReady: function(playerId) {
this.players = this.players.map(player => {
if (player.id === playerId) {
return {
...player,
isCanvasReady: true,
lives: STARTING_PLAYER_LIVES
}
}
return player;
});
},
allCanvasesReady: function() {
return this.players.filter(p => p.isReady).every(p => p.isCanvasReady);
},
serveBallToRandomPlayer: function(excludeId) {
const participatingPlayers = this.players.filter(p => p.isReady && p.isCanvasReady && p.lives > 0);
const nextServeCandidates = excludeId ? participatingPlayers.filter(p => p.id !== excludeId) : participatingPlayers;
return nextServeCandidates[Math.floor(Math.random() * nextServeCandidates.length)]
},
increaseBallVelocity: function(smashed) {
const currentBallVelocity = this.ballVecity || STARTING_BALL_VELOCITY;
this.ballVecity = currentBallVelocity * (smashed ? 2 : 1.2);
},
resetBallVelocity: function() {
this.ballVecity = STARTING_BALL_VELOCITY;
},
getBallVelocity: function() {
return this.ballVecity;
},
resetVolley: function() {
this.volleyTalley = [];
},
tallyVolley: function(playerId) {
const playerWhoHit = this.players.find(p => p.id === playerId);
this.volleyTalley = this.volleyTalley.concat([{
name: playerWhoHit.name
}])
},
getVolleyTally: function() {
return this.volleyTalley;
},
resetEverything: function() {
this.players = this.players.map(p => ({
...p,
isReady: false,
isCanvasReady: false,
lives: STARTING_PLAYER_LIVES
})),
this.gameActive = false;
}
}