forked from jasonliu19/MultiplayerBrowserGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·135 lines (115 loc) · 3.76 KB
/
app.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
//External modules
var p2 = require('p2');
//Internal modules
var socketHandler = require('./server/sockethandler.js');
var Player = require('./server/player.js');
var Enemy = require('./server/enemy.js');
var EnemyManager = require('./server/enemymanager.js');
var constants = require('./server/constants.js');
var Block = require('./server/block.js');
var GroundItem = require('./server/grounditem.js');
var world = require('./server/physicshandler.js');
var CollisionHandler = require('./server/collisionhandler.js');
var SOCKET_LIST = socketHandler.SOCKET_LIST;
var io = socketHandler.io;
var lastConnectedSocket = null;
Block.initMap();
setInterval(function () {
if(lastConnectedSocket != socketHandler.getLastConnectedSocket()){
var socket = socketHandler.getLastConnectedSocket();
lastConnectedSocket = socket;
SOCKET_LIST[socket.id] = socket;
Player.onConnect(socket);
Enemy.onPlayerConnect(socket);
Block.onPlayerConnect(socket);
GroundItem.onPlayerConnect(socket);
socket.on('disconnect', function () {
socket.broadcast.emit('playerDisconnect', socket.id);
Enemy.onPlayerDisconnect(socket);
Player.onDisconnect(socket);
delete SOCKET_LIST[socket.id];
});
}
}, 1000/10);
//Physics loop
var lastTime = Date.now();
var zombieSpawnTimer = 0;
var wavePauseTime = 60000;
var waveLength = 30000;
var waveNumber = 1;
setInterval(function () {
var delta = Date.now() - lastTime;
lastTime = Date.now();
Player.update();
world.step(delta/1000);
CollisionHandler.update();
// limit time between waves to be 10 seconds at shortest
// if(zombieSpawnTimer >= wavePauseTime && wavePauseTime >= 10000) {
// zombieSpawnTimer = 0;
// waveLength += 3000;
// waveNumber++;
// // spawn waveNumber extra enemies at the beginning of each wave
// for(i = 0; i < waveNumber; i++) {
// for(j = 0; j < waveNumber; j++) {
// EnemyManager.randomGenerateEnemy();
// }
// }
// }
// else if(zombieSpawnTimer >= wavePauseTime) {
// // spawn waveNumber extra enemies at the beginning of each wave
// for(i = 0; i < waveNumber; i++) {
// for(j = 0; j < waveNumber; j++) {
// EnemyManager.randomGenerateEnemy();
// }
// }
// zombieSpawnTimer = 0;
// wavePauseTime -= 3000;
// waveLength += 3000;
// waveNumber++;
// }
if(zombieSpawnTimer >= 45){// && zombieSpawnTimer <= waveLength){
zombieSpawnTimer = 0;
EnemyManager.randomGenerateEnemy();
GroundItem.randomlySpawnAmmo();
}
// if(zombieSpawnTimer == 0){
// EnemyManager.randomGenerateEnemy();
// }
// if(zombieSpawnTimer%500 == 0){
// GroundItem.randomlySpawnAmmo();
// }
//Check if server is empty
// var isEmpty = true;
// for(var i in Player.list) {
// if (Player.list.hasOwnProperty(i)) {
// isEmpty = false;
// break;
// }
// }
// if(isEmpty){
// wavePauseTime = 60000;
// waveLength = 30000;
// waveNumber = 1;
// }
zombieSpawnTimer++;
}, 1000/60);
//Update clients loop
setInterval(function () {
for(var i in SOCKET_LIST){
try {
var pack = Player.generateCurrentStatusPackage();
SOCKET_LIST[i].emit('updateClientOnPlayers', pack);
pack = Enemy.generateCurrentStatusPackage();
SOCKET_LIST[i].emit('updateClientOnEnemies', pack);
}
catch(error) {
console.log(error);
}
}
}, 1000/40);
setInterval(function () {
Block.update();
}, 5000);
setInterval(function () {
EnemyManager.update();
}, 200);