This repository has been archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclient_test.js
463 lines (399 loc) · 14.2 KB
/
client_test.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
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
////////////////////////////////////////////////////////////////////////////////
// Pirate Ship Battles //
////////////////////////////////////////////////////////////////////////////////
const express = require('express');
const unique = require('node-uuid');
const SAT = require('sat');
const Player = require('./objects/player.js');
const Box = require('./objects/box.js');
const SafeZone = require('./objects/safe_zone.js');
const Island = require('./objects/island.js');
const Stone = require('./objects/stone.js');
const ScoreBoard = require('./objects/score_board.js');
const aux = require('./objects/_aux.js');
let app = express();
let serv = require('http').Server(app);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.use('/client', express.static(__dirname + '/client'));
serv.listen({
host: '0.0.0.0',
port: 2000,
exclusive: true
});
console.log("Test server started.");
const UPDATE_TIME = 0.06; // sec
const BULLET_LIFETIME = 1000; // ms
// Create a new game instance
const game = {
// List of players in the game
playerList: {},
/** @type Bullet{}*/
bulletList: {},
//List of islands in the game
islandList: {},
//List of stones in the game
stoneList: {},
// boxes object list
boxList: {},
// The list of scores form active players
score_board: new ScoreBoard(),
// The max number of pickable boxes in the game
boxesMax: 1,
// Size of the boxes list
numOfBoxes: 0,
// The max number of islands in the game
islandMax: 1,
// The max number of stones in the game
stoneMax: 1,
// Game height
canvasHeight: 2000,
// Game width
canvasWidth: 2000,
// Advances by one each game update cycle (related to player invulnerability)
delta: 1,
// Arbitrary integer variable, used to define invulnerability time
mod: 120
};
circle = new SafeZone(1000, 1000, 1000, game.canvasWidth, game.canvasHeight);
setInterval(updateGame, 1000 * UPDATE_TIME);
////////////////////////////////////////////////////////////////////////////////
function updateGame () {
// Update players
for (let k in game.playerList) {
if (!(k in game.playerList))
continue;
let p = game.playerList[k];
p.updatePos(UPDATE_TIME);
if (p.inputs.shootLeft && !p.leftHoldStart && p.canShoot(false))
p.leftHoldStart = Date.now();
if (p.inputs.shootRight && !p.rightHoldStart && p.canShoot(true))
p.rightHoldStart = Date.now();
if (!p.inputs.shootLeft && p.leftHoldStart) {
let newBullets = p.tryToShoot(false);
for (const b of newBullets) {
game.bulletList[b.id] = b;
io.in('game').emit("bullet_create", b);
}
p.leftHoldStart = 0;
}
if (!p.inputs.shootRight && p.rightHoldStart) {
let newBullets = p.tryToShoot(true);
for (const b of newBullets) {
game.bulletList[b.id] = b;
io.in('game').emit("bullet_create", b);
}
p.rightHoldStart = 0;
}
//checking if outside safe-zone
if (!circle.in_circle(p)) {
p.takeDamage(game.delta, game.mod);
if (p.life <= 0) {
playerKilled(p);
}
}
}
// Update bullets
for (const kb in game.bulletList) {
if (!(kb in game.bulletList))
continue;
let bullet = game.bulletList[kb];
bullet.updatePos(UPDATE_TIME);
//if (Date.now() > bullet.timeCreated + BULLET_LIFETIME) {
if (bullet.z <= 0) {
delete game.bulletList[bullet.id];
io.in('game').emit('bullet_remove', bullet);
}
}
// Do collisions
for (const k1 in game.playerList) {
let p1 = game.playerList[k1];
for (const k2 in game.playerList) {
p2 = game.playerList[k2];
if (p2.id < p1.id)
collidePlayers(p1, p2);
}
for (const kb in game.boxList)
collidePlayerAndBox(p1, game.boxList[kb]);
for (const kb in game.bulletList)
collidePlayerAndBullet(p1, game.bulletList[kb]);
for (const kb in game.islandList) {
collidePlayerAndIslandRestore(p1, game.islandList[kb]);
}
for (const kb in game.stoneList) {
collidePlayerAndStone(p1, game.stoneList[kb]);
}
}
io.in('game').emit("update_game", {playerList: game.playerList,
bulletList: game.bulletList,
score_board: game.score_board});
}
////////////////////////////////////////////////////////////////////////////////
// Create the pickable boxes there are missing at the game
function addBox (x, y) {
let n = game.boxesMax - game.numOfBoxes;
if (n > 0) {
let boxentity = new Box(game.canvasWidth, game.canvasHeight, 'box');
boxentity.x = 200
boxentity.y = 200
game.boxList[boxentity.id] = boxentity;
io.in('game').emit("item_create", boxentity);
game.numOfBoxes++;
}
}
////////////////////////////////////////////////////////////////////////////////
function addIslands (temp_x, temp_y) {
let n = game.islandMax - Object.keys(game.islandList).length;
if (n > 0) {
let islandentity = new Island(temp_x, temp_y, 100, "bullet_island", game.canvasWidth, game.canvasHeight);
game.islandList[islandentity.id] = islandentity;
io.in('game').emit("island_create", islandentity);
}
}
////////////////////////////////////////////////////////////////////////////////
function addStones (temp_x, temp_y) {
let n = game.stoneMax - Object.keys(game.stoneList).length;
if (n > 0) {
let stoneentity = new Stone(temp_x, temp_y, 65, game.canvasWidth, game.canvasHeight);
game.stoneList[stoneentity.id] = stoneentity;
io.in('game').emit("stone_create", stoneentity);
}
}
////////////////////////////////////////////////////////////////////////////////
// Called after the player entered its name
function onEntername (data) {
console.log(`Received joinning request from ${this.id}, size: ${data.config.width}:${data.config.height}`);
if (data.username.length > 0 && data.username.length < 15)
this.emit('join_game', {username: data.username, id: this.id});
else if (data.username.length <= 0)
this.emit('throw_error', {message: "Name can't be null"});
else if (data.username.length >= 15)
this.emit('throw_error', {message: "Name is too long"});
}
////////////////////////////////////////////////////////////////////////////////
function colliding (newPlayer) {
let minPlayerDist = 130*130;
let minIslandDist = 300*300;
let minStoneDist = 200*200;
// Check for players
for (const k in game.playerList) {
if (aux.distSq(newPlayer, game.playerList[k]) < minPlayerDist)
return true;
}
for (const i in game.islandList) {
if (aux.distSq(newPlayer, game.islandList[i]) < minIslandDist)
return true;
}
for (const i in game.stoneList) {
if (aux.distSq(newPlayer, game.stoneList[i]) < minStoneDist)
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
// Called when a new player connects to the server
function onNewPlayer (data) {
if (this.id in game.playerList) {
console.log(`Player with id ${this.id} already exists`);
return;
}
let newPlayer = new Player(250, 250, Math.PI / 2, this.id, data.username);
console.log("Created new player with id " + this.id);
this.emit('create_player', data);
let current_info = {
id: newPlayer.id,
x: newPlayer.x,
y: newPlayer.y,
angle: newPlayer.angle,
username: newPlayer.username,
};
for (let k in game.playerList) {
existingPlayer = game.playerList[k];
let player_info = {
id: existingPlayer.id,
username: existingPlayer.username,
x: existingPlayer.x,
y: existingPlayer.y,
angle: existingPlayer.angle,
};
this.emit("new_enemyPlayer", player_info);
}
game.playerList[this.id] = newPlayer;
game.score_board.add_player(this.id, data.username);
for (let k in game.boxList)
this.emit('item_create', game.boxList[k]);
for (let k in game.bulletList)
this.emit('bullet_create', game.bulletList[k]);
for (let k in game.islandList)
this.emit('island_create', game.islandList[k]);
for (let k in game.stoneList)
this.emit('stone_create', game.stoneList[k]);
//send message to every connected client except the sender
this.broadcast.emit('new_enemyPlayer', current_info);
}
////////////////////////////////////////////////////////////////////////////////
// Called when someone fired an input
function onInputFired (data) {
let movePlayer = game.playerList[this.id];
if (!(this.id in game.playerList) || game.playerList[this.id].dead)
return;
movePlayer.inputs.up = data.up;
movePlayer.inputs.left = data.left;
movePlayer.inputs.right = data.right;
movePlayer.inputs.shootLeft = data.shootLeft;
movePlayer.inputs.shootRight = data.shootRight;
}
////////////////////////////////////////////////////////////////////////////////
// Called to verify if two players collide
function collidePlayers (p1, p2) {
if (!(p2.id in game.playerList) || !(p1.id in game.playerList)
|| p1.dead || p2.dead)
return;
if (SAT.testPolygonPolygon(p1.poly, p2.poly)) {
if (SAT.testPolygonPolygon(p1.prowLine, p2.poly)) {
if (SAT.testPolygonPolygon(p1.poly, p2.prowLine)) {
playerKilled(p1);
playerKilled(p2);
} else {
playerKilled(p2);
}
} else if (SAT.testPolygonPolygon(p1.poly, p2.prowLine)) {
playerKilled(p1);
} else if (SAT.testPolygonPolygon(p1.middleLine, p2.poly)) {
if (SAT.testPolygonPolygon(p1.poly, p2.middleLine)) {
playerKilled(p1);
playerKilled(p2);
} else {
playerKilled(p2);
}
} else if (SAT.testPolygonPolygon(p1.poly, p2.middleLine)) {
playerKilled(p1);
} else {
console.log("Could not threat the collision D:");
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Called to verify if an item is picked
function collidePlayerAndBox (p1, bx) {
if (!(p1.id in game.playerList) || !(bx.id in game.boxList))
return;
if (SAT.testPolygonCircle(p1.poly, bx.poly)) {
p1.bullets += bx.bullets;
console.log(`Box with ${bx.bullets} bullets picked`);
delete game.boxList[bx.id];
game.numOfBoxes--;
io.in('game').emit('item_remove', bx);
addBox();
}
}
////////////////////////////////////////////////////////////////////////////////
// Called to verify if a bullet collide with a player
function collidePlayerAndBullet (p1, bullet) {
if (!(p1.id in game.playerList) || !(bullet.id in game.bulletList) || bullet.creator == p1.id)
return;
if (SAT.testPolygonCircle(p1.poly, bullet.poly)) {
game.score_board.update_score(bullet.creator);
delete game.bulletList[bullet.id];
io.in('game').emit('bullet_remove', bullet);
console.log(`Bullet hit ${p1.username}`);
p1.life--;
if (p1.life <= 0)
playerKilled(p1);
}
}
////////////////////////////////////////////////////////////////////////////////
// Called to verify player is in island restore area
function collidePlayerAndIslandRestore (p1, isl) {
if (!(p1.id in game.playerList) || !(isl.id in game.islandList))
return;
if (SAT.testPolygonCircle(p1.poly, isl.restore_poly)) {
// Player has touched island area, and is waiting
if (p1.anchored_timer < 180) {
io.to(p1.id).emit("disable_inputs");
p1.speed = 0;
p1.accel = 0;
p1.anchored_timer += 1;
}
// Player is ready to be freed
else {
// Move player on server and client
del_x = p1.x - isl.x;
del_y = p1.y - isl.y;
theta = Math.atan2(del_y, del_x);
// Setting anchored_timer to zero here may cause issues, because
// the player is still in contact with the island's restore poly.
// We should make this state known to the game (something like, moving_away).
p1.angle = theta + Math.PI/2;
if (SAT.testPolygonCircle(p1.poly, isl.restore_poly)) {
//Waiting for player to move out of the area
p1.addPos(Math.sign(Math.cos(theta)) * 0.5, Math.sign(Math.sin(theta)) * 0.5);
t_p1_poly = JSON.parse(JSON.stringify(p1.poly));
t_p1_poly.pos.x = p1.x;
t_p1_poly.pos.y = p2.y;
// Player left the area after this movement
if (!SAT.testPolygonCircle(t_p1_poly, isl.restore_poly)) {
p1.gainResource(game.delta, game.mod, isl.type);
p1.anchored_timer = 0;
}
}
}
}
}
// Called to verify player is in stone area
function collidePlayerAndStone (p1, stn) {
if (!(p1.id in game.playerList) || !(stn.id in game.stoneList))
return;
if (SAT.testPolygonCircle(p1.poly, stn.collision_poly)) {
playerKilled(p1);
}
}
////////////////////////////////////////////////////////////////////////////////
// Called when a someone dies
function playerKilled (player) {
console.log(`${player.username} died!`);
if (player.id in game.playerList) {
console.log(`${player.username} was removed`);
game.score_board.remove_player(player.id);
delete game.playerList[player.id];
io.in('game').emit('remove_player', player);
io.sockets.sockets[player.id].leave('game');
io.sockets.sockets[player.id].join('login');
}
player.dead = true;
}
////////////////////////////////////////////////////////////////////////////////
// Called when a client disconnects to tell the clients, except sender, to
// remove the disconnected player
function onClientDisconnect () {
console.log('disconnect');
if (this.id in game.playerList) {
game.score_board.remove_player(this.id);
delete game.playerList[this.id];
}
console.log("removing player " + this.id);
this.broadcast.emit('remove_player', {id: this.id});
}
let io = require('socket.io')(serv,{});
////////////////////////////////////////////////////////////////////////////////
io.sockets.on('connection', function(socket) {
console.log("socket connected");
socket.join('login');
socket.on('enter_name', onEntername);
socket.on('logged_in', function(data) {
this.emit('enter_game', {username: data.username});
socket.leave('login');
socket.join('game');
});
socket.on('disconnect', onClientDisconnect);
socket.on("new_player", onNewPlayer);
socket.on("input_fired", onInputFired);
});
// Prepare the boxes
addBox();
// Prepare the islands
addIslands();
// Prepare the stones
addStones();
////////////////////////////////////////////////////////////////////////////////