-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
323 lines (272 loc) · 10.1 KB
/
main.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
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
class Game
{
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Setup
constructor()
{
// Settings
this.log_level_ = 1; // 0 - 2
this.server_url_ = self.location.hostname; // URL to the websocket server
this.server_port_ = '8080'; // Port of the websocket server
this.framerate_ = 15; // In frames per second
this.frametime_ = 1/this.framerate_ * 1000; // In milliseconds
this.fieldsize_ = [1000, 1000]; // Size of the playing field
this.max_players_ = 4; // Maximum number of players
// States
this.state_ = 'Lobby';
this.start_requested_ = false;
this.reset_requested_ = false;
this.wall_inactive_for_ = 0;
this.last_update_ = undefined;
this.interval_ = undefined;
this.wallTimeout = undefined;
// Players
this.players_local_ = [undefined];
this.players_remote_ = [];
// Storage
this.storage_ = new Storage();
// Essentials
this.logger_ = new Logger(this.log_level_);
this.communicator_ = new Communicator(this.server_url_, this.server_port_, this.logger_, this);
this.input_handler_ = new InputHandler(this);
this.collision_detector_ = new CollisionDetector(document.getElementById('canvas'), this.fieldsize_);
this.ui_handler_ = new UiHandler(document.getElementById('container-player-cards'),
document.getElementById('container-alerts'),
document.getElementById('container-stats'),
this.players_local_,
this.players_remote_);
this.drawer_ = new Drawer(document.getElementById('canvas'));
this.audio_player_ = new AudioPlayer();
this.power_up_ = new PowerUp(this);
this.ui_handler_.updateStats(this.storage_.win_count_, this.storage_.units_traveled_);
this.communicator_.registerToMessageType('Alert', this);
// Create game
this.setupGame();
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Methods for handling messages
handleMessage(message)
{
switch(message.type)
{
case 'Alert':
this.ui_handler_.generateAlert(message.content.title, message.content.text, false)
break;
case 'RemotePlayerHello':
// Get remote player id
let remote_player_hello_id = message.content;
// Check if remote player is local player
let remote_player_not_known = true;
if(this.players_local_[0].id_ == remote_player_hello_id)
{
remote_player_not_known = false;
}
// Check if remote player already known
this.players_remote_.forEach(function(player_remote)
{
if(player_remote.id_ == remote_player_hello_id)
{
remote_player_not_known = false;
}
});
// If not known
if(remote_player_not_known)
{
let new_player_remote = new Player(remote_player_hello_id, this.fieldsize_, this.collision_detector_,
this.drawer_, this.communicator_, this.ui_handler_, this.logger_, this.storage_);
this.players_remote_.push(new_player_remote);
this.communicator_.registerToMessageType('PositionUpdate', new_player_remote);
this.players_local_[0].sendMessageRemotePlayerHello();
this.ui_handler_.updatePlayerCards();
}
break;
case 'StartGame':
this.startGame();
break;
case 'WallInactiveTime':
this.power_up_.addWallInactiveTime(message.content);
break;
case 'Audio':
break;
case 'RemotePlayerDeath':
// Get remote player id
let remote_player_death_id = message.content;
// Set remote player alive state
this.players_remote_.forEach(function(player_remote)
{
if(player_remote.id_ == remote_player_death_id)
{
player_remote.alive_ = false;
}
});
// Check win condition
this.checkWinCondition();
break;
case 'RemotePlayerGoodbye':
// Get remote player id
let remote_player_id = message.content;
// Check if remote player known
let i = 0;
let that = this;
this.players_remote_.forEach(function(player_remote)
{
if(player_remote.id_ == remote_player_id)
{
if(that.state_ == 'Game')
{
player_remote.alive_ = false;
that.checkWinCondition();
}
that.players_remote_.splice(i, 1);
that.communicator_.unregisterFromMessageType('PositionUpdate', remote_player_id);
that.ui_handler_.resetPlayerCards();
that.ui_handler_.updatePlayerCards();
}
i++;
});
break;
case 'EndGame':
let winner_player_id = message.content;
let game_end_message = "You lose.";
if(this.players_local_[0].id_ == winner_player_id)
{
this.storage_.increaseWinCount();
this.ui_handler_.updateStats(this.storage_.win_count_, this.storage_.units_traveled_);
game_end_message = "You win.";
this.players_local_[0].alive_ = false;
}
this.ui_handler_.generateAlert("Game over", game_end_message, false);
this.stopGame();
break;
case 'ResetGame':
this.resetGame();
break;
default:
this.logger_.log(1, 'Unknown message type')
break;
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Methods for game logic
checkWinCondition()
{
// Check if all other players dead
let all_other_players_dead = true;
this.players_remote_.forEach(function(player_remote)
{
if(player_remote.alive_ == true)
{
all_other_players_dead = false;
}
});
// If all other players dead - Game ends and this player wins.
if(all_other_players_dead)
{
this.audio_player_.playGameEndSound();
this.communicator_.sendMessage('RequestEndGame', 'Global', undefined);
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Methods for game operation
setupGame()
{
// Check connection
if(this.communicator_.connection_open_)
{
this.logger_.log(1, 'Creating game');
// Draw border
this.drawer_.drawBorder();
// Listen to creation of other players
this.communicator_.registerToMessageType('RemotePlayerHello', this);
this.communicator_.registerToMessageType('RemotePlayerDeath', this);
this.communicator_.registerToMessageType('RemotePlayerGoodbye', this);
// Create local player
this.players_local_[0] = new Player('local', this.fieldsize_, this.collision_detector_, this.drawer_,
this.communicator_, this.ui_handler_, this.logger_, this.storage_);
// Listen to start and end of the game
this.communicator_.registerToMessageType('StartGame', this);
this.communicator_.registerToMessageType('EndGame', this);
// Call run function periodically
let event_target = this;
}
else
{
this.logger_.log(1, 'Could not create game, network not ready, retrying');
// Call create function one more time in one second
let event_target = this;
window.setTimeout(function(){event_target.setupGame.call(event_target);}, 1000);
}
}
requestStartGame()
{
if(this.start_requested_ == false)
{
this.logger_.log(1, 'startGame request');
this.communicator_.sendMessage('RequestStartGame', 'Global', undefined);
this.start_requested_ = true;
}
}
startGame()
{
this.logger_.log(1, 'startGame');
this.state_ = 'Game';
this.reset_requested_ = false;
this.last_update_ = Date.now();
this.drawer_.clear();
this.drawer_.drawBorder();
let event_target = this;
this.interval_ = window.setInterval(function(){event_target.runGame.call(event_target);}, this.frametime_);
this.communicator_.registerToMessageType('WallInactiveTime', this);
}
runGame()
{
// Calculate delta to last iteration
let now = Date.now();
let delta_ms = now - this.last_update_;
this.last_update_ = now;
// Update local and remote players
this.players_local_[0].updateAllIfAlive(delta_ms, this.input_handler_.getDirection());
this.players_remote_.forEach(
function(player_remote)
{
player_remote.drawPendingDrawRequests();
}
);
this.input_handler_.pollController();
}
stopGame()
{
this.communicator_.registerToMessageType('ResetGame', this);
this.logger_.log(1, 'stopGame');
this.state_ = 'LobbyGameOver';
this.last_update_ = Date.now();
window.clearInterval(this.interval_);
}
requestResetGame()
{
if(this.reset_requested_ == false && this.state_ == 'LobbyGameOver')
{
this.logger_.log(1, 'resetGame Request');
this.communicator_.sendMessage('RequestResetGame', 'Global', undefined);
this.reset_requested_ = true;
}
}
resetGame()
{
this.logger_.log(1, 'resetGame');
// Reset players
this.players_local_[0].reset();
this.players_remote_.forEach(function(player_remote){player_remote.reset()});
// Reset canvas
this.drawer_.clear();
this.drawer_.drawBorder();
// Reset UI
this.ui_handler_.resetAlerts();
// Reset states
this.start_requested_ = false;
this.state_ = 'Lobby';
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Execute
let game = new Game();