-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
284 lines (237 loc) · 6.41 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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('public'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
io.on('connection', function(socket){
var playerId;
socket.on('createPlayer', function (playerName) {
console.log('connected', io.engine.clientsCount);
var playerTypeKey = Object.keys(PLAYER_TYPES)[Math.floor(Math.random() * 5)];
var player = new Player(PLAYER_TYPES[playerTypeKey], playerName, socket);
socket.on('changeDirection', function (direction) {
var playerId = player.id;
//players.forEach(function (player) {
//if (playerId == player.id) {
//player.changeDirection(direction);
//}
//});
player.changeDirection(direction);
});
playerId = player.id;
socket.emit('playerCreated', playerId);
players.push(player);
livingPlayers[player.id] = socket;
console.log('createPlayer', playerId);
});
socket.on('disconect', function () {
console.log('userDisconected', playerId);
players = players.filter(function (player) {
return player.id !== playerId;
});
delete livingPlayers[playerId];
});
console.log('a user connected');
});
// Here comes the game logic
var PLAYER_TYPES = {
burn: 'BURN',// Reduces enemies size
icy: 'ICY',// Reduces enemies speed
tempo: 'TEMPO',// Freezes enemies
mirror: 'MIRROR',
void: 'VOID',
npc: 'NPC'
}
var WORLD_SIZE = 10000;
var MAX_PLAYER_SIZE = 1000;
var players = [];
var livingPlayers = {};
var lastPlayerCount = 0;
var MIN_PLAYERS = 50;
function getSafePosition(npc) {
return {
x: Math.floor(Math.random() * WORLD_SIZE),
y: Math.floor(Math.random() * WORLD_SIZE)
};
}
function Player(type, name, socket) {
this.name = name;
this.type = type;
this.speed = {
dx: 0,
dy: 0
};
this.position = getSafePosition(type === PLAYER_TYPES.npc);
this.sizeDisambiguation = Math.random();
if (type === PLAYER_TYPES.npc) {
this.size = (Math.random() * 10) + 10;
} else {
this.size = 40;
this.id = 'player' + lastPlayerCount;
lastPlayerCount += 1;
}
function getDistance(player1, player2) {
return Math.sqrt(
Math.pow(player2.position.x - player1.position.x, 2) +
Math.pow(player2.position.y - player1.position.y, 2)
);
}
function colides(player2) {
return getDistance(this, player2) < (player2.size + this.size);
}
function getNPCSpeed(defaultSpeed) {
var speeds = [
{
dx: 0,
dy: 1
},
{
dx: 0,
dy: -1
},
{
dx: 1,
dy: 0
},
{
dx: -1,
dy: 0
},
{
dx: 1,
dy: 1
},
{
dx: -1,
dy: -1
},
{
dx: 1,
dy: -1
},
{
dx: -1,
dy: 1
}
];
if (Math.floor(Math.random() * 4) === 0) {
return speeds[Math.floor(Math.random() * speeds.length)];
}
return defaultSpeed;
}
function updateSpeed() {
if (this.type === PLAYER_TYPES.npc) {
this.speed = getNPCSpeed(this.speed);
if (this.size > MAX_PLAYER_SIZE / 2) {
this.size = MAX_PLAYER_SIZE / 2;
}
}
var xS = this.speed.dx / Math.abs(this.speed.dx || 1),
yS = this.speed.dy / Math.abs(this.speed.dy || 1);
if (this.size >= MAX_PLAYER_SIZE) {
this.size = MAX_PLAYER_SIZE - 5;
}
this.speed.dx = (MAX_PLAYER_SIZE - this.size) * xS;
this.speed.dy = (MAX_PLAYER_SIZE - this.size) * yS;
}
function setSpeed(dx, dy) {
this.speed.dx = dx;
this.speed.dy = dy;
}
function normalizePosition() {
if (this.position.x - (this.size) < 0) {
this.position.x = this.size;
} else if (this.position.x + this.size > WORLD_SIZE) {
this.position.x = WORLD_SIZE - this.size;
}
if (this.position.y - this.size < 0) {
this.position.y = this.size;
} else if (this.position.y + this.size > WORLD_SIZE) {
this.position.y = WORLD_SIZE - this.size;
}
}
function eat(player) {
player.die();
this.size += (player.size / 2);
}
function move() {
updateSpeed.call(this);
var sX = Math.abs(this.speed.dx) / (this.speed.dx || 1);
var sY = Math.abs(this.speed.dy) / (this.speed.dy || 1);
this.position.x += Math.max(Math.abs(this.speed.dx), 20) / 20 * sX;
this.position.y += Math.max(Math.abs(this.speed.dy), 20) / 20 * sY;
normalizePosition.call(this);
}
function changeDirection(direction) {
console.log('changeDirection', direction);
if (direction === 'Left') {
this.speed.dx = -1;
this.speed.dy = 0;
} else if (direction === 'Right') {
this.speed.dx = 1;
this.speed.dy = 0;
} else if (direction === 'Up') {
this.speed.dx = 0;
this.speed.dy = -1;
} else if (direction === 'Down') {
this.speed.dx = 0;
this.speed.dy = 1;
}
}
function die() {
if (socket) {
socket.emit('gameOver');
}
}
this.changeDirection = changeDirection;
this.setSpeed = setSpeed;
this.colides = colides;
this.move = move;
this.eat = eat;
this.die = die;
}
function getSurvivingPlayers(players) {
return players.filter(function (player1, i) {
return !players.some(function (player2, j) {
var theyColide = player1.colides(player2);
var dies = theyColide && (player2.size > player1.size || (player2.size === player1.size && player2.sizeDisambiguation > player1.sizeDisambiguation));
if (dies) {
player2.eat(player1);
}
return dies;
});
});
}
function sendUpdatedPlayers(players) {
io.emit('updatePlayers', players);
}
function updatePlayers(players) {
players.forEach(function (player) {
player.move();
});
}
function createNPCPlayers(howMany) {
var i, npcPlayers = [];
for (i = 0; i < howMany; i+= 1) {
npcPlayers.push(new Player(PLAYER_TYPES.npc, 'NPC'));
}
return npcPlayers;
}
function tick() {
var newPlayers = getSurvivingPlayers(players);
if (newPlayers.length < MIN_PLAYERS) {
newPlayers = newPlayers.concat(createNPCPlayers(MIN_PLAYERS - newPlayers.length));
}
//TODO: move npc players
updatePlayers(newPlayers);
sendUpdatedPlayers(newPlayers);
players = newPlayers;
}
//players.push(new Player(PLAYER_TYPES.burn, 'pablito', 500, 500));
setInterval(tick, 15);