-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
169 lines (149 loc) · 6.07 KB
/
app.js
File metadata and controls
169 lines (149 loc) · 6.07 KB
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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
function Player(socketId){
this.socketId=socketId;
this.position={x:0 ,y:0};
this.radius=0;
}
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
var listFood=[];
for(var i=0; i<30; i++){
listFood.push({x:getRandomArbitrary(0,1600), y:getRandomArbitrary(0,1200)});
}
var players=[];
var lvl2 = false;
function findPlayerIndex(socketId){
for(var n=0; n<players.length; n++){
if(socketId==players[n].socketId){
return n;
}
}
}
function searchEmptyPosition() {
for(var n=0; n<players.length; n++){
if(players[n].socketId==null){
return n;
}
}
return -1;
}
//Our js files
app.use("/js", express.static('js'));
app.use("/img", express.static('images'));
app.get('/', function(req, res){
res.sendFile(__dirname + '/game.html');
});
io.on('connection', function(socket){
console.log('a user connected');
var emptyPosition = searchEmptyPosition(); //Search if an empty position is available in de players list
if(emptyPosition == -1){players.push(new Player(socket.id));}
else{players[emptyPosition].socketId = socket.id}
socket.emit('initFood', listFood);
socket.emit('initPlayers', players);
socket.on('new_player', function (x,y,radius) {
console.log('new player');
var playerIndex = findPlayerIndex(socket.id);
console.log('index: '+playerIndex);
players[playerIndex].position.x=x;
players[playerIndex].position.y=y;
players[playerIndex].radius=radius;
console.log('x: '+ players[playerIndex].position.x + ' y: '+ players[playerIndex].position.y);
console.log('radius: '+players[playerIndex].radius);
socket.emit('index', playerIndex);
socket.broadcast.emit('new_enemy', playerIndex, players[playerIndex]);
});
socket.on('overlap_food', function(playerIndex, foodIndex, x, y){
console.log('overlap_food');
if(listFood[foodIndex].x == x && listFood[foodIndex].y == y){ //Correct overlap
listFood[foodIndex].x = getRandomArbitrary(0,1600);
listFood[foodIndex].y = getRandomArbitrary(0,1200);
console.log('player index: '+playerIndex);
console.log('player radius: '+players[playerIndex].radius);
players[playerIndex].radius += 1;
io.emit('update_particle', foodIndex, listFood[foodIndex]);
io.emit('update_player_size', playerIndex, players[playerIndex].radius);
if(((players[playerIndex].radius - 20) * 10 >= 600) && !lvl2){
socket.emit('winner');
socket.broadcast.emit('loser');
lvl2 = true;
for(k=0; k<players.length; k++){
players[k].radius = 20;
}
}
}
});
socket.on('overlap_enemies', function (playerIndex, player2Index) {
console.log('overlap_enemies');
var minimumDistance = players[playerIndex].radius + players[player2Index].radius;
var x1=players[playerIndex].position.x;
var x2=players[player2Index].position.x;
var y1=players[playerIndex].position.y;
var y2=players[player2Index].position.y;
var distance = Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
if(distance <= minimumDistance){ //Correct overlap
if(players[playerIndex].radius > players[player2Index].radius){
console.log('dead player index: '+player2Index);
players[playerIndex].radius += players[player2Index].radius-19;
io.to(players[player2Index].socketId).emit('player_killed');
io.emit('update_player_size', playerIndex, players[playerIndex].radius);
if(((players[playerIndex].radius - 20) * 10 >= 600) && !lvl2){
socket.emit('winner');
socket.broadcast.emit('loser');
lvl2 = true;
for(k=0; k<players.length; k++){
players[k].radius = 20;
}
}
}
else if(players[playerIndex].radius < players[player2Index].radius){
console.log('dead player index: '+playerIndex);
players[player2Index].radius += players[playerIndex].radius-19;
socket.emit('player_killed');
io.emit('update_player_size', player2Index, players[player2Index].radius);
if(((players[player2Index].radius - 20) * 10 >= 600) && !lvl2){
socket.emit('winner');
socket.broadcast.emit('loser');
lvl2 = true;
for(k=0; k<players.length; k++){
players[k].radius = 20;
}
}
}
}
});
socket.on('new_position',function(new_x,new_y,playerIndex){
socket.broadcast.emit('update_player_position', playerIndex, new_x, new_y);
players[playerIndex].position.x = new_x;
players[playerIndex].position.y = new_y;
});
socket.on('new_radius',function(playerIndex, radius){
players[playerIndex].radius = radius;
io.emit('update_player_size', playerIndex, players[playerIndex].radius);
});
socket.on('game_over',function(winner){
if(winner) {
socket.broadcast.emit('loser');
}
else {
socket.broadcast.emit('winner');
}
});
socket.on('disconnect', function(){
console.log('user disconnected');
var playerIndex = findPlayerIndex(socket.id);
players[playerIndex].radius = 10; //Radius not possible (wont be displayed as enemy on the client side)
players[playerIndex].socketId=null;
socket.broadcast.emit('delete_enemy', playerIndex);
});
});
setTimeout(function() {
io.emit('players', players);
}, 1000);//Phaser default frame rate 60fps = 16.6ms
// Server start
http.listen(3000, function(){
console.log('listening on *:3000');
});