-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
476 lines (423 loc) · 19 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
import { Bullet } from "./bullet.js"
import { Wall } from "./wall.js"
import { Entity } from "./entity.js"
import { Weapon } from "./weapon.js"
import { Interactable } from "./interactable.js"
import express from "express"
import { Server } from "socket.io"
import { Room } from "./room.js"
import { Item } from "./item.js"
import { SimpleEntity } from "./simpleEntity.js"
import { SimpleBullet } from "./simpleBullet.js"
import { Rect } from "./rect.js"
import { EnemyStats } from "./enemyStats.js"
import { LevelGeneration } from "./levelGeneration.js"
// var express = require('express');
var app = express();
var server = app.listen(process.env.PORT || 3000);
app.use(express.static('public'));
console.log("MY server is running!");
// var socket = require('socket.io');
var io = new Server(server);
io.sockets.on('connection', newConnection);
let d = new Date();
let gameTime = d.getTime();
// setInterval(updateGameTime, 10);
// function updateGameTime(){
// d = new Date();
// gameTime = d.getTime()
// }
var entities = {};
var game = {
n:0
};
var rooms = []
//entities[-1] = new Entity("Enemy", "npc", 100, 100, 20, 30, 100, "pistol", "purple", -1, gameTime,-1, 0.5,6);
var walls = []
var bullets = []
var particles = []
var interactables = [];
interactables.push(new Interactable("Goblin Forest", 1200, 230, 30, 40, "cyan", "portal", gameTime, game.n))
game.n++;
interactables.push(new Interactable("Loot", 100, 100, 15, 15, "brown", "bag", gameTime))
createSection("lobby", 0, 0, 1650, 500, -2)
function newConnection(socket){
socket.on("disconnect", () => {
delete entities[socket.id];
});
entities[socket.id] = new Entity("", "Player", 100 + randint(-20, 20), 100, 20, 30, 100, ["", ""],
"purple", 0, gameTime, socket.id,1,6)
socket.on("sendName", processName);
socket.on("key", keyMsg);
socket.on("interact", interact);
socket.on("openInventory", openInventory);
socket.on("shoot", triggerBullet);
socket.on("castAbility", triggerAbility)
socket.on("pickUpItem", pickUpItem);
function processName (idInfo){
if (idInfo[1].length == 1 && entities[idInfo[0]].name.length < 12){
entities[idInfo[0]].name += idInfo[1];
} else if (idInfo[1] == "Enter"){
io.to(idInfo[0]).emit('gameStart', true)
} else if (idInfo[1] == "Backspace"){
entities[idInfo[0]].name = entities[idInfo[0]].name.slice(0, -1);
}
io.to(idInfo[0]).emit('displayName', entities[idInfo[0]].name)
};
function keyMsg(key) {
//data is the key pressed
if (Object.keys(entities).indexOf(socket.id) != -1 && entities[socket.id].deathTime == 0) {
entities[socket.id].move(key);
// socket.broadcast.emit('key', data); // this sends to everyone minus the client that sent the message
}
}
function triggerAbility(aimPos){
if (entities[socket.id] != null && entities[socket.id].deathTime == 0 &&
entities[socket.id].inventory.inventoryOpen == false){
entities[socket.id].attackInfo.preformAttack(entities[socket.id].attacks[1][0],1,
bullets,entities, entities[socket.id], gameTime,
aimPos[0], aimPos[1], particles, game, walls)
}
}
function triggerBullet(aimPos){
if (entities[socket.id] != null && entities[socket.id].deathTime == 0 &&
entities[socket.id].inventory.inventoryOpen == false){
entities[socket.id].attackInfo.preformAttack(entities[socket.id].attacks[0][0],0,
bullets,entities, entities[socket.id], gameTime,
aimPos[0], aimPos[1], particles, game, walls)
}
}
function interact(id){
if (entities[id].interact != null && entities[socket.id].deathTime == 0){
if (entities[id].interact.type == "portal"){
findPortalDestination(id)
} else if (entities[id].interact.type == "bag"){
entities[id].inventory.inventoryOpen = true;
} else if (entities[id].interact.type == "healStation"){
if (!entities[id].interact.checkForDuplicate(bullets)){
bullets.push(new Bullet(entities[id].interact.x + entities[id].interact.length,
entities[id].interact.y, 0, 0, 0,-1,"rect", -1, -1,gameTime, entities[id].interact.id,"green", 100, 20, true, true));
}
} else if (entities[id].interact.type == "button"){
var wallIndex = findWall(entities[id].interact.id, walls)
if (wallIndex != -1){
walls.splice(wallIndex, 1);
}
}
}
}
function openInventory(id){
if(entities[id] != null && entities[socket.id].deathTime == 0){
if (entities[id].inventory.inventoryOpen && entities[id].inventory.itemSelected != null){
var returnedItem = false;
for (var i = 0; i < entities[id].inventory.items.length && !returnedItem; i++){
// return the item to its original slot if they have their inventory open
if (entities[id].inventory.items[i].itemName == "" && (Number.isInteger(entities[id].inventory.items[i].slot)
|| entities[id].inventory.items[i].slot == entities[id].inventory.items[i].item.slot)){
entities[id].inventory.items[i].itemName = entities[id].inventory.itemSelected;
entities[id].inventory.items[i].refreshItem();
returnedItem = true;
}
}
if (!returnedItem){
interactables.push(new Interactable("Loot", entities[id].x,
entities[id].y, 15, 15, "brown", "bag", gameTime));
interactables[interactables.length -1].addToInventory(entities[id].inventory.itemSelected)
entities[id].inventory.itemSelected = null
entities[id].updateStats();
}
}
entities[id].inventory.inventoryOpen = !entities[id].inventory.inventoryOpen;
entities[id].inventory.itemSelected = null;
}
}
function pickUpItem(list){
if (entities[list.id].interact != null){
takeItem(list, entities[list.id].interact.inventory)
}
takeItem(list, entities[list.id].inventory)
dropItem(list, entities[list.id].inventory, entities[list.id].interact)
}
}
function dropItem(list, inventory1, inventory2){
if (entities[list.id].inventory.itemSelected != null){
var dropped = true;
if (inventory2 != null && inventory2.type == "bag"){
inventory2 = inventory2.inventory
for (var i = 0; i < inventory1.rects.length; i ++){
if (inventory2 != null){
for (var c = 0; c < inventory2.rects.length; c ++){
if (contains(list.x, list.y, inventory1.rects[i]) || contains(list.x, list.y, inventory2.rects[c])){
dropped = false;
}
}
}
}
} else {
for (var i = 0; i < inventory1.rects.length; i ++){
if (contains(list.x, list.y, inventory1.rects[i])){
dropped = false;
}
}
}
if (dropped){
interactables.push(new Interactable("Loot", entities[list.id].x,
entities[list.id].y, 15, 15, "brown", "bag", gameTime));
interactables[interactables.length -1].addToInventory(entities[list.id].inventory.itemSelected)
entities[list.id].inventory.itemSelected = null
entities[list.id].updateStats();
}
}
}
function takeItem(list, inventory){
// when they pick up the item
if (inventory != null){
for (var i = 0; i < inventory.items.length; i++){
if ( entities[list.id].inventory.itemSelected == null){
if (contains(list.x, list.y, inventory.items[i])
&& inventory.items[i].itemName != ""){
entities[list.id].inventory.itemSelected = inventory.items[i].itemName;
inventory.items[i].itemName = ""
inventory.items[i].refreshItem();
entities[list.id].updateStats();
}
} else {
if (contains(list.x, list.y, inventory.items[i])
&& (Number.isInteger(inventory.items[i].slot)||
inventory.items[i].slot == new Item(inventory.itemSelected).slot)){
var temp = entities[list.id].inventory.itemSelected
entities[list.id].inventory.itemSelected = inventory.items[i].itemName;
inventory.items[i].itemName = temp
inventory.items[i].refreshItem();
if (entities[list.id].inventory.itemSelected == ""){
entities[list.id].inventory.itemSelected = null
}
entities[list.id].updateStats();
}
}
}
}
}
setInterval(update, 1);
var oldTime = 0
// UPDATE THE SERVER ---------------------------------------------------------------------------------//
function update(){
d = new Date();
gameTime = d.getTime()
if (gameTime - oldTime > 14){
oldTime = gameTime
if (Object.keys(entities).length != 0){
Object.keys(entities).forEach(function(key) {
if (entities[key].type == "Player"){
// send update, optimize entities
var sendEntities = {};
if (Object.keys(entities).length != 0){
Object.keys(entities).forEach(function(id) {
if (distance(entities[key].x + entities[key].length/2, entities[key].y + entities[key].width/2,
entities[id].x + entities[id].length/2, entities[id].y + entities[id].width/2) < 1450){
sendEntities[id] = new SimpleEntity(entities[id].name, entities[id].type,
entities[id].x, entities[id].y, entities[id].length,
entities[id].width, entities[id].dir, entities[id].stats,
entities[id].colour, entities[id].location, entities[id].interact,
entities[id].shake, entities[id].inventory, entities[id].deathTime, entities[id].deathDuration,
entities[id].speech, entities[id].closestBoss, entities[id].effects, entities[id].negativeEffects,
entities[id].gold);
}
});
}
var simpleBullets = [];
for (var i = 0; i < bullets.length; i ++){
simpleBullets.push(new SimpleBullet(bullets[i].x, bullets[i].y, bullets[i].colour,
bullets[i].length, bullets[i].width, bullets[i].aimX, bullets[i].aimY, bullets[i].delay));
}
io.to(key).emit('sendingUpdate', [gameTime, sendEntities, walls, bullets, interactables, particles])
}
if (entities[key] != null){
entities[key].update(walls);
entities[key].findBoss(entities, entities[key]);
entities[key].checkEffects(gameTime);
entities[key].setRoom(rooms);
entities[key].accelerate();
entities[key].checkInteract(interactables);
entities[key].aiMovement(entities, entities[key], bullets, gameTime, particles,game, walls);
if (entities[key] != null)
entities[key].checkDeath(entities, gameTime, interactables, particles, bullets, walls, game);
game.n ++;
}
});
}
for (var i = 0; i < bullets.length; i ++){
bullets[i].updateBulletLocation(entities, walls, bullets, gameTime, particles)
}
for (var i = 0; i < bullets.length; i ++){
bullets[i].checkBulletDuration(bullets, gameTime, entities);
}
for (var i = 0; i < walls.length; i ++){
walls[i].checkExist(entities, walls);
}
for (var i = particles.length-1; i >= 0; i --){
particles[i].accelerate();
particles[i].update(walls)
particles[i].checkExpire(gameTime, particles,particles[i], entities);
}
sortList(interactables, "bag")
for (var i = interactables.length-1; i >= 0; i --){
interactables[i].update(walls);
interactables[i].checkExpire(gameTime, interactables);
if (interactables[i] != null && interactables[i].type == "bag" && interactables[i].checkEmpty()){
interactables.splice(i, 1);
}
}
for (var i = rooms.length-1; i >= 0; i --){
var listOfDungeons = ["Goblin Forest", "Crusader Encampment", "The Theatre"] // "Crusader Encampment"
for (var c = 0; c < listOfDungeons.length; c ++){
if (rooms[i].name == "lobby" && !rooms[i].checkForPortal(interactables, listOfDungeons[c]) && randint(1, 1000) < 5){
interactables.push(new Interactable(listOfDungeons[c], randint(1000, 1300), 230, 30, 40, "cyan", "portal", gameTime, game.n))
while (interactables[interactables.length-1].touching(interactables)){
interactables[interactables.length-1].x = randint(1000, 1300);
}
game.n ++;
}
}
if (gameTime - rooms[i].lastChecked > 100){
rooms[i].lastChecked = gameTime;
if (rooms[i].checkEmpty(entities, rooms[i]) && rooms[i].name != "lobby"){
rooms[i].deleteArray(walls);
rooms[i].deleteArray(interactables);
rooms[i].deleteDictionary(entities, rooms[i]);
rooms.splice(i, 1);
}
}
}
}
}
function randint(min, max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function rectRectDetect(rect, rect2){
var leftSide = rect.x;
var rightSide = rect.x + rect.length;
var topSide = rect.y;
var botSide = rect.y + rect.width;
if (rect2.x + rect2.length > leftSide && rect2.x < rightSide && rect2.y + rect2.width> topSide && rect2.y < botSide){
return true;
} else {
return false;
}
}
function rectCircDetect(rect, circle){
leftSide = rect.x;
rightSide = rect.x + rect.length;
topSide = rect.y;
botSide = rect.y + rect.width;
if (circle.x + circle.r/2 > leftSide && circle.x - circle.r/2 < rightSide && circle.y + circle.r/2> topSide && circle.y - circle.r/2< botSide){
return true;
} else {
return false;
}
}
function createBullet(x, y, aimX, aimY, speed, damage, type, duration, team){
bullets.push(new Bullet(x, y, aimX, aimY, speed, damage, type, duration, team, gameTime));
}
function contains(x, y, rect){
return (x >= rect.x && x <= rect.x + rect.length && y >= rect.y && y <= rect.y + rect.width)
}
function distance(x1, y1, x2, y2){
return Math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
}
function findPortalDestination(id){
var foundRoom = false;
for (var i = 0; i < rooms.length; i ++){
if (entities[id].interact.id == rooms[i].id){
entities[id].x = rooms[i].x + 100
entities[id].y = rooms[i].y + 50;
// CHANGE THIS LATER DO NOT DO THIS!
if (rooms[i].name != "lobby" && rooms[i].name != "Warlord's Lair"&& rooms[i].name != "High Priest's Quarters"){
entities[id].interact.expireTime = 10000;
entities[id].interact.creationTime = gameTime;
}
foundRoom = true;
}
}
if (!foundRoom){
createSection(entities[id].interact.name, randint(0, 10000), randint(0, 10000), entities[id].interact.id);
}
}
function checkAvailable (rect, arrayOfRect){
for(var i = 0; i < arrayOfRect.length; i ++){
if (rectRectDetect(rect, arrayOfRect[i]) && rect != arrayOfRect[i]){
return false;
}
}
return true;
}
function createSection(name, x, y, id){
switch(name){
case "lobby":
var length = 1650;
var width = 500;
var id = -2
break;
case "Goblin Forest":
var length = 6400;
var width = 500;
break;
case "Warlord's Lair":
var length = 1200;
var width = 500;
break;
case "Crusader Encampment":
var length = 7000;
var width = 500;
break;
case "High Priest's Quarters":
var length = 2000;
var width = 1000;
break;
case "The Theatre":
var length = 8000;
var width = 600;
break;
case "The Stage":
var length = 1600;
var width = 800;
break;
}
rooms.push(new Room(name, x, y, length + 100, width + 50, gameTime, id));
while(!checkAvailable(rooms[rooms.length-1], rooms)){
rooms[rooms.length-1].x = randint(0, 1000);
rooms[rooms.length-1].y = randint(0, 1000);
}
x = rooms[rooms.length-1].x
y = rooms[rooms.length-1].y
walls.push(new Wall("wall", x, y, length, 20,"silver"));
walls.push(new Wall("wall", x, y, 20, width,"silver"));
walls.push(new Wall("wall", x, y + width, length, 70,"silver"));
walls.push(new Wall("wall", x+ length, y, 100, width,"silver"));
var levelGeneration = new LevelGeneration();
levelGeneration.generateLevel(name, x, y + width, length, walls, entities, game, interactables, gameTime, rooms[rooms.length-1]);
}
function sortList(list, keyInBack){
var changed = true;
while(changed){
changed = false
for(var i = 0; i < list.length - 1; i ++){
if(list[i] != null && list[i+1] != null){
if (list[i].type == keyInBack && list[i+1].type != keyInBack){
var temp = list[i];
list[i] = list[i+1];
list[i+1] = temp
changed = true;
}
}
}
}
}
function findWall(id){
for (var i = 0; i < walls.length; i ++){
if (walls[i].id != null && walls[i].id == id){
return i
}
}
return -1;
}