-
Notifications
You must be signed in to change notification settings - Fork 87
/
index.js
executable file
·201 lines (164 loc) · 4.41 KB
/
index.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
var express = require('express');
var app = express();
var counter = 0;
var BALL_SPEED = 10;
var WIDTH = 1100;
var HEIGHT = 580;
var TANK_INIT_HP = 100;
//Static resources server
app.use(express.static(__dirname + '/www'));
var server = app.listen(process.env.PORT || 8082, function () {
var port = server.address().port;
console.log('Server running at port %s', port);
});
var io = require('socket.io')(server);
function GameServer(){
this.tanks = [];
this.balls = [];
this.lastBallId = 0;
}
GameServer.prototype = {
addTank: function(tank){
this.tanks.push(tank);
},
addBall: function(ball){
this.balls.push(ball);
},
removeTank: function(tankId){
//Remove tank object
this.tanks = this.tanks.filter( function(t){return t.id != tankId} );
},
//Sync tank with new data received from a client
syncTank: function(newTankData){
this.tanks.forEach( function(tank){
if(tank.id == newTankData.id){
tank.x = newTankData.x;
tank.y = newTankData.y;
tank.baseAngle = newTankData.baseAngle;
tank.cannonAngle = newTankData.cannonAngle;
}
});
},
//The app has absolute control of the balls and their movement
syncBalls: function(){
var self = this;
//Detect when ball is out of bounds
this.balls.forEach( function(ball){
self.detectCollision(ball);
if(ball.x < 0 || ball.x > WIDTH
|| ball.y < 0 || ball.y > HEIGHT){
ball.out = true;
}else{
ball.fly();
}
});
},
//Detect if ball collides with any tank
detectCollision: function(ball){
var self = this;
this.tanks.forEach( function(tank){
if(tank.id != ball.ownerId
&& Math.abs(tank.x - ball.x) < 30
&& Math.abs(tank.y - ball.y) < 30){
//Hit tank
self.hurtTank(tank);
ball.out = true;
ball.exploding = true;
}
});
},
hurtTank: function(tank){
tank.hp -= 2;
},
getData: function(){
var gameData = {};
gameData.tanks = this.tanks;
gameData.balls = this.balls;
return gameData;
},
cleanDeadTanks: function(){
this.tanks = this.tanks.filter(function(t){
return t.hp > 0;
});
},
cleanDeadBalls: function(){
this.balls = this.balls.filter(function(ball){
return !ball.out;
});
},
increaseLastBallId: function(){
this.lastBallId ++;
if(this.lastBallId > 1000){
this.lastBallId = 0;
}
}
}
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
var game = new GameServer();
/* Connection events */
io.on('connection', function(client) {
console.log('User connected');
client.on('joinGame', function(tank){
console.log(tank.name + ' joined the game');
var initX = getRandomInt(40, 900);
var initY = getRandomInt(40, 500);
var tankId = guid();
client.emit('addTank', { id: tankId, name: tank.name, type: tank.type, isLocal: true, x: initX, y: initY, hp: TANK_INIT_HP });
client.broadcast.emit('addTank', { id: tankId, name: tank.name, type: tank.type, isLocal: false, x: initX, y: initY, hp: TANK_INIT_HP} );
game.addTank({ id: tankId, name: tank.name, type: tank.type, hp: TANK_INIT_HP});
});
client.on('sync', function(data){
//Receive data from clients
if(data.tank != undefined){
game.syncTank(data.tank);
}
//update ball positions
game.syncBalls();
//Broadcast data to clients
client.emit('sync', game.getData());
client.broadcast.emit('sync', game.getData());
//I do the cleanup after sending data, so the clients know
//when the tank dies and when the balls explode
game.cleanDeadTanks();
game.cleanDeadBalls();
counter ++;
});
client.on('shoot', function(ball){
var ball = new Ball(ball.ownerId, ball.alpha, ball.x, ball.y );
game.addBall(ball);
});
client.on('leaveGame', function(tankId){
console.log(tankId + ' has left the game');
game.removeTank(tankId);
client.broadcast.emit('removeTank', tankId);
});
});
function Ball(ownerId, alpha, x, y){
this.id = game.lastBallId;
game.increaseLastBallId();
this.ownerId = ownerId;
this.alpha = alpha; //angle of shot in radians
this.x = x;
this.y = y;
this.out = false;
}
Ball.prototype = {
fly: function(){
//move to trayectory
var speedX = BALL_SPEED * Math.sin(this.alpha);
var speedY = -BALL_SPEED * Math.cos(this.alpha);
this.x += speedX;
this.y += speedY;
}
};
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}