-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
562 lines (497 loc) · 20.1 KB
/
server.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
var fs = require('fs');
//Import server-client-commons
eval(fs.readFileSync('public/server-client-commons.js') + '');
//Server Setup
var express = require('express');
var app = express();
var server = app.listen(8888);
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var password = "Basic dXNlcjpwYXNz";
//Parameters
var worldDimensions = {
x: 1600,
y: 1600
};
var playerSpeed = 10;
var bulletSpeed = 2.5 * playerSpeed;
var defaultPlayerSize = 80;
var fps = 20;
var bulletDamage = 7;
var lifeBoostGain = 5;
var startingHealth = 100;
var groundBulletDensity = 20000; // less is more
var numOfGroundBullets = worldDimensions.x * worldDimensions.y / groundBulletDensity;
var lifeBoostDensity = 960000; // less is more
var numOfLifeBoosts = worldDimensions.x * worldDimensions.y / lifeBoostDensity;
var bulletLifeTime = 8;
var timeBetweenRegenerations = 100;
var regenerateAmount = 0.3;
var playerCollisionDamage = bulletDamage;
var maxCollisionGroup = -1000000;
var currPlayerCollisionGroup = -1;
//Import matter-js (physics engine)
var Matter = require('matter-js');
var engine = Matter.Engine.create();
//Set gravity
engine.world.gravity = {
x: 0,
y: 0
};
var wallWidth = 50;
//Make the walls
Matter.World.add(engine.world, [
Matter.Bodies.rectangle(worldDimensions.x / 2, -wallWidth / 2, worldDimensions.x + 2 * wallWidth, wallWidth, {
isStatic: true
}),
Matter.Bodies.rectangle(worldDimensions.x / 2, worldDimensions.y + wallWidth / 2, worldDimensions.x + 2 * wallWidth, wallWidth, {
isStatic: true
}),
Matter.Bodies.rectangle(worldDimensions.x + wallWidth / 2, worldDimensions.y / 2, wallWidth, worldDimensions.y + 2 * wallWidth, {
isStatic: true
}),
Matter.Bodies.rectangle(-wallWidth / 2, worldDimensions.y / 2, wallWidth, worldDimensions.y + 2 * wallWidth, {
isStatic: true
})
]);
//Name the walls wall and make them white
for (var i = 0; i < engine.world.bodies.length; i++) {
engine.world.bodies[i].label = 'wall';
engine.world.bodies[i].render.fillStyle = '#ffffff'
}
//Add Ground Bullets and Life Boosts
createGroundBullets();
createLifeBoosts();
//Server setup
app.use(express.static('public'));
//Socket.io (live communication engine) setup
var socket = require('socket.io');
var io = socket(server);
//Send the world to the players every frame
setInterval(function () {
//Update bullet life time and delete old ones
for (var i = 0; i < engine.world.bodies.length; i++) {
if (engine.world.bodies[i].label === 'bullet') {
engine.world.bodies[i].timeSinceBorn += 1 / fps
}
if (engine.world.bodies[i].timeSinceBorn >= bulletLifeTime) {
Matter.World.remove(engine.world, engine.world.bodies[i])
}
}
//Tell the physics engine to move forward one tick
Matter.Engine.update(engine, 1000 / fps);
//Change bodies array to not refer itself because otherwise it becomes infinitely large
var simplifiedBodies = simplifyBodies(engine.world.bodies);
//Send the bodies array to each client so that they can display it
io.sockets.volatile.emit('worldUpdate', simplifiedBodies);
}, 1000 / fps);
//Makes ground bullets disappear on contact and add themselves to player
Matter.Events.on(engine, 'collisionStart', function (event) {
//Try catch because sometimes the pairs are undefined for some reason
try {
var pairs = event.pairs;
//Loop through all the pairs of collisions
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];
//Check for both cases of player-groundBullet or groundBullet-player
if (pair.bodyA.label === 'player' && pair.bodyB.label === 'groundBullet') {
//Give the player a bullet
pair.bodyA.numOfBullets++;
//Remove the ground bullet
respawn(pair.bodyB);
}
if (pair.bodyA.label === 'groundBullet' && pair.bodyB.label === 'player') {
respawn(pair.bodyA);
pair.bodyB.numOfBullets++;
}
}
} catch (e) {
}
});
//makes life boosts disappear on contact and add themselves to player
Matter.Events.on(engine, 'collisionStart', function (event) {
//Try catch because sometimes the pairs are undefined for some reason
try {
var pairs = event.pairs;
//Loop through all the pairs of collisions
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];
//Check for both cases of player-lifeBoost or lifeBoost-player
if (pair.bodyA.label === 'player' && pair.bodyB.label === 'lifeBoost') {
//Increase the player's maxHealth
pair.bodyA.maxHealth += lifeBoostGain;
//Increase the player's health by the same amount
pair.bodyA.health += lifeBoostGain;
//Remove the life boost
respawn(pair.bodyB);
}
if (pair.bodyA.label === 'lifeBoost' && pair.bodyB.label === 'player') {
respawn(pair.bodyA);
pair.bodyB.maxHealth += lifeBoostGain;
pair.bodyB.health += lifeBoostGain;
}
}
} catch (e) {
}
});
//Hurt people when they hit a bullet
Matter.Events.on(engine, 'collisionStart', function (event) {
//Try catch because sometimes the pairs are undefined for some reason
try {
var pairs = event.pairs;
//Loop through all the pairs of collisions
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];
//Check for both cases of player-bullet or bullet-player
if (pair.bodyA.label === 'player' && pair.bodyB.label === 'bullet' && pair.bodyA.socketID !== pair.bodyB.socketID) {
//Deal the damage to the player
pair.bodyA.health -= bulletDamage;
//Remove the bullet
Matter.World.remove(engine.world, pair.bodyB);
//If the player has no more health, disconnect them
if (pair.bodyA.health <= 0) {
io.sockets.connected[pair.bodyA.socketID].disconnect(); //Disconnect on death
}
}
if (pair.bodyA.label === 'bullet' && pair.bodyB.label === 'player' && pair.bodyA.socketID !== pair.bodyB.socketID) {
Matter.World.remove(engine.world, pair.bodyA);
pair.bodyB.health -= bulletDamage;
if (pair.bodyB.health <= 0) {
io.sockets.connected[pair.bodyB.socketID].disconnect(); //Disconnect on death
}
}
}
} catch (e) {
}
});
//Make bombs explode when a bullets hits them
Matter.Events.on(engine, 'collisionStart', function (event) {
//Try catch because sometimes the pairs are undefined for some reason
var bomb;
var other;
try {
var pairs = event.pairs;
//Loop through all the pairs of collisions
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];
//Check for both cases of bomb-bullet or bullet-bomb
if (pair.bodyA.label === 'bomb') {
bomb = pair.bodyA;
other = pair.bodyB;
switch (bomb.trigger) {
case 0: //Any bullet hits
if (other.label === 'bullet') {
bulletCircle(bomb.position, bomb.angle, bomb.numOfBullets, bomb.socketID, bomb.collisionGroup);
Matter.World.remove(engine.world, bomb);
Matter.World.remove(engine.world, other)
}
break;
case 1: //Bullet from player
if (other.label === 'bullet' && bomb.socketID === other.socketID) {
bulletCircle(bomb.position, bomb.angle, bomb.numOfBullets, bomb.socketID, bomb.collisionGroup);
Matter.World.remove(engine.world, bomb);
Matter.World.remove(engine.world, other);
}
break;
case 2: //A player hits that is not the bomb's placer
if (other.label === 'player' && other.socketID !== bomb.socketID) {
bulletCircle(bomb.position, bomb.angle, bomb.numOfBullets, bomb.socketID, bomb.collisionGroup);
Matter.World.remove(engine.world, bomb);
Matter.World.remove(engine.world, other);
}
break;
}
}
if (pair.bodyB.label === 'bomb') {
bomb = pair.bodyB;
other = pair.bodyA;
switch (bomb.trigger) {
case 0: //Any bullet hits
if (other.label === 'bullet') {
bulletCircle(bomb.position, bomb.angle, bomb.numOfBullets, bomb.socketID, bomb.collisionGroup);
Matter.World.remove(engine.world, bomb);
Matter.World.remove(engine.world, other)
}
break;
case 1: //Bullet from player
if (other.label === 'bullet' && bomb.socketID === other.socketID) {
bulletCircle(bomb.position, bomb.angle, bomb.numOfBullets, bomb.socketID, bomb.collisionGroup);
Matter.World.remove(engine.world, bomb);
Matter.World.remove(engine.world, other)
}
break;
case 2: //A player hits that is not the bomb's placer
if (other.label === 'player' && other.socketID !== bomb.socketID) {
bulletCircle(bomb.position, bomb.angle, bomb.numOfBullets, bomb.socketID, bomb.collisionGroup);
Matter.World.remove(engine.world, bomb);
}
break;
}
}
}
} catch (e) {
}
});
//Damage players when they hit each other
Matter.Events.on(engine, 'collisionStart', function (event) {
//Try catch because sometimes the pairs are undefined for some reason
try {
var pairs = event.pairs;
//Loop through all the pairs of collisions
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i];
//Check player-player
if (pair.bodyA.label === 'player' && pair.bodyB.label === 'player') {
//Hurt both players
pair.bodyA.health -= playerCollisionDamage;
pair.bodyB.health -= playerCollisionDamage;
}
}
} catch (e) {
}
});
// Regenerate Life
setInterval(function () {
//Loop through all the bodies in the world
for (var i = 0; i < engine.world.bodies.length; i++) {
//Check if they are a player
if (engine.world.bodies[i].label === 'player') {
var body = engine.world.bodies[i];
//If they regeneration would give the player too much health then just max out their health
if (body.health + regenerateAmount > body.maxHealth) {
body.health = body.maxHealth
}
// otherwise increase it by the regenerate amount
else {
body.health += regenerateAmount
}
}
}
}, timeBetweenRegenerations);
// Reset ground bullets and life boosts periodically
setInterval(function () {
checkForEscapedObjects();
}, 500);
function checkForEscapedObjects() {
try {
for (var i = 0; i < engine.world.bodies.length; i++) {
if ((engine.world.bodies[i].position.x < 0 || engine.world.bodies[i].position.x > worldDimensions.x || engine.world.bodies[i].position.y < 0 || engine.world.bodies[i].position.y > worldDimensions.y) && engine.world.bodies[i].label !== "wall") {
if (engine.world.bodies[i].label !== "bullet" && engine.world.bodies[i].label !== "bomb") {
Matter.Body.setPosition(engine.world.bodies[i], randomPosition(100));
setTimeout(function () {
Matter.Body.setVelocity(engine.world.bodies[i], {'x': 0, 'y': 0});
}, 1000 / fps)
} else {
Matter.World.remove(engine.world, engine.world.bodies[i]);
}
}
}
} catch (e) {
}
}
function createBomb(position, socketID, bullets, trigger, visible, angle, collisionGroup) {
var bombHeading = {
x: Math.cos(angle),
y: Math.sin(angle)
};
//Make sure the vector is a unit vector
bombHeading = getUnitVector(bombHeading);
var bomb = Matter.Bodies.polygon(position.x - bombHeading.x * defaultPlayerSize, position.y - bombHeading.y * defaultPlayerSize, 8, 25, {
chamfer: {
radius: 10
}
});
bomb.label = "bomb";
bomb.visible = visible;
bomb.numOfBullets = bullets;
bomb.trigger = trigger;
bomb.restitution = 0.8;
bomb.socketID = socketID;
bomb.collisionGroup = collisionGroup;
Matter.Body.setAngle(bomb, angle);
Matter.World.add(engine.world, bomb);
}
function bulletCircle(position, angle, numOfBullets, socketID, collisionGroup) {
for (var i = -Math.PI; i < Math.PI; i += Math.PI / (numOfBullets / 2)) {
newBullet(i + angle, position, socketID, collisionGroup)
}
}
function newBullet(angle, position, socketID, collisionGroup) {
//Calculate a vector for the bullets direction
var bulletHeading = {
x: Math.cos(angle),
y: Math.sin(angle)
};
//Make sure the vector is a unit vector
bulletHeading = getUnitVector(bulletHeading);
//Place the bullet a little bit away from the starting position with the specified default bullet size
var bullet = Matter.Bodies.polygon(position.x + bulletHeading.x * defaultPlayerSize, position.y + bulletHeading.y * defaultPlayerSize, 3, 10);
//Make it have very little friction
bullet.frictionAir = 0.001;
//Name it bullet
bullet.label = 'bullet';
//Start the clock of how long it has existed
bullet.timeSinceBorn = 0;
//Set the bullets origin
bullet.socketID = socketID;
bullet.collisionFilter.group = collisionGroup;
Matter.Body.setAngle(bullet, angle + Math.PI / 3);
//Make the vector be the bullet's speed
bulletHeading.x *= bulletSpeed;
bulletHeading.y *= bulletSpeed;
//Set the bullet's speed
Matter.Body.setVelocity(bullet, bulletHeading);
//Make the bullet bouncy
bullet.restitution = 0.8;
//Add the bullet to the world
Matter.World.add(engine.world, bullet)
}
io.sockets.on('connection', newConnection);
//Detect new connections
function newConnection(socket) {
//Create the player
var player = Matter.Bodies.rectangle(randomPosition(defaultPlayerSize * 2).x, randomPosition(defaultPlayerSize * 2).y, defaultPlayerSize, defaultPlayerSize);
player.label = 'player';
player.socketID = socket.id;
player.name = 'Unnamed';
player.numOfBullets = 0;
player.skinID = random(numOfSkins) - 1;
player.health = startingHealth;
player.maxHealth = player.health;
player.restitution = 0.8;
if (currPlayerCollisionGroup < maxCollisionGroup) {
currPlayerCollisionGroup = -1;
}
player.collisionFilter.group = currPlayerCollisionGroup;
currPlayerCollisionGroup--;
//Add the player to the world
Matter.World.add(engine.world, player);
//The the world dimensions to the client
socket.emit('worldDimensions', worldDimensions);
socket.on('name', setName);
//Assign the player's name
function setName(newName) {
player.name = newName
}
socket.on('heading', updateHeading);
//Update the player's direction
function updateHeading(heading) {
//get a unit vector
heading = getUnitVector(heading);
//set the player's speed
heading.x *= playerSpeed;
heading.y *= playerSpeed;
//Calculate the player's angle
player.angle = Math.atan2(heading.y, heading.x);
//Set the player's velocity in the right direction
Matter.Body.setVelocity(player, heading)
}
//Create the bullet if the player has enough ground bullets
socket.on('newBullet', function () {
if (player.numOfBullets > 0) {
//Create the bullets in the player's direction
newBullet(player.angle, player.position, player.socketID, player.collisionFilter.group);
//Remove a bullet from the player
player.numOfBullets--;
}
});
socket.on('newBomb', function (bullets, trigger, visible) {
var cost = bombCostCalc(bullets, trigger, visible);
if (player.numOfBullets >= cost) {
createBomb(player.position, player.socketID, bullets, trigger, visible, player.angle, player.collisionFilter.group);
player.numOfBullets -= cost;
}
});
//Remove the player on disconnect
socket.on('disconnect', function () {
Matter.World.remove(engine.world, player)
});
}
function random(max) {
return Math.floor(Math.random() * max + 1);
}
function getUnitVector(v) {
var scale = Math.sqrt(v.x * v.x + v.y * v.y);
if (scale !== 0) {
v.x = v.x / scale;
v.y = v.y / scale
}
return v;
}
//Removes the self referring parts of the bodies array by only taking specific ones
function simplifyBodies(bodies) {
var newBodies = [];
for (var i = 0; i < bodies.length; i++) {
var oldBody = bodies[i];
if (oldBody.visible !== false) {
var vertices;
if (oldBody.label in objectVertices) {
vertices = oldBody.label;
} else {
vertices = [];
for (var j = 0; j < oldBody.vertices.length; j++) {
vertices.push({
x: oldBody.vertices[j].x,
y: oldBody.vertices[j].y
})
}
}
newBodies.push({
'angle': oldBody.angle,
'position': oldBody.position,
'label': oldBody.label,
'render': oldBody.render,
'vertices': vertices,
'numOfBullets': oldBody.numOfBullets,
'health': oldBody.health,
'maxHealth': oldBody.maxHealth,
'skinID': oldBody.skinID,
'name': oldBody.name,
'socketID': oldBody.socketID
})
}
}
return newBodies;
}
function randomPosition(padding) {
var position = {'x': 0, 'y': 0};
position.x = random(worldDimensions.x - padding * 2) + padding;
position.y = random(worldDimensions.y - padding * 2) + padding;
return position;
}
function createGroundBullets() {
//Create the specified number of ground bullets
for (var i = 0; i < numOfGroundBullets; i++) {
//Create the bullet at a random position with the specified ground bullet size
var groundBullet = Matter.Bodies.fromVertices(randomPosition(100).x, randomPosition(100).y, Matter.Vertices.create(objectVertices['groundBullet']));
//Rotate it by a random amount
Matter.Body.rotate(groundBullet, Math.random() * 2 * Math.PI);
//Name it groundBullet
groundBullet.label = "groundBullet";
//Make it bouncy
groundBullet.restitution = 0.8;
//Add it to the world
Matter.World.add(engine.world, groundBullet);
}
}
function createLifeBoosts() {
for (var i = 0; i < numOfLifeBoosts; i++) {
//Create the life boost based off of the vertices
var lifeBoost = Matter.Bodies.polygon(randomPosition(100).x, randomPosition(100).y, 5, 10);
Matter.Body.rotate(lifeBoost, Math.random() * 2 * Math.PI);
lifeBoost.label = "lifeBoost";
lifeBoost.restitution = 0.8;
Matter.World.add(engine.world, lifeBoost);
}
}
function respawn(body) {
Matter.Body.setPosition(body, randomPosition(100));
setTimeout(function () {
Matter.Body.setVelocity(body, {'x': 0, 'y': 0});
}, 1000 / fps)
}
//API
eval(fs.readFileSync('api.js') + '');