forked from OmarShehata/tutsplus-glitch-multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
90 lines (78 loc) · 3.13 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
var express = require('express'); // Express contains some boilerplate to for routing and such
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http); // Here's where we include socket.io as a node module
// Serve the index page
app.get("/", function (request, response) {
response.sendFile(__dirname + '/index.html');
});
// Serve the assets directory
app.use('/assets',express.static('assets'))
// Listen on port 5000
app.set('port', (process.env.PORT || 5000));
http.listen(app.get('port'), function(){
console.log('listening on port',app.get('port'));
});
var players = {}; //Keeps a table of all players, the key is the socket id
var bullet_array = []; // Keeps track of all the bullets to update them on the server
// Tell Socket.io to start accepting connections
io.on('connection', function(socket){
// Listen for a new player trying to connect
socket.on('new-player',function(state){
console.log("New player joined with state:",state);
players[socket.id] = state;
// Broadcast a signal to everyone containing the updated players list
io.emit('update-players',players);
})
// Listen for a disconnection and update our player table
socket.on('disconnect',function(state){
delete players[socket.id];
io.emit('update-players',players);
})
// Listen for move events and tell all other clients that something has moved
socket.on('move-player',function(position_data){
if(players[socket.id] == undefined) return; // Happens if the server restarts and a client is still connected
players[socket.id].x = position_data.x;
players[socket.id].y = position_data.y;
players[socket.id].angle = position_data.angle;
io.emit('update-players',players);
})
// Listen for shoot-bullet events and add it to our bullet array
socket.on('shoot-bullet',function(data){
if(players[socket.id] == undefined) return;
var new_bullet = data;
data.owner_id = socket.id; // Attach id of the player to the bullet
if(Math.abs(data.speed_x) > 20 || Math.abs(data.speed_y) > 20){
console.log("Player",socket.id,"is cheating!");
}
bullet_array.push(new_bullet);
});
})
// Update the bullets 60 times per frame and send updates
function ServerGameLoop(){
for(var i=0;i<bullet_array.length;i++){
var bullet = bullet_array[i];
bullet.x += bullet.speed_x;
bullet.y += bullet.speed_y;
// Check if this bullet is close enough to hit any player
for(var id in players){
if(bullet.owner_id != id){
// And your own bullet shouldn't kill you
var dx = players[id].x - bullet.x;
var dy = players[id].y - bullet.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if(dist < 70){
io.emit('player-hit',id); // Tell everyone this player got hit
}
}
}
// Remove if it goes too far off screen
if(bullet.x < -10 || bullet.x > 1000 || bullet.y < -10 || bullet.y > 1000){
bullet_array.splice(i,1);
i--;
}
}
// Tell everyone where all the bullets are by sending the whole array
io.emit("bullets-update",bullet_array);
}
setInterval(ServerGameLoop, 16);