-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
81 lines (62 loc) · 1.45 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
var
express = require('express'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http),
uid = 0;
count = 0;
pool = new Array();
app.use(express.static(__dirname + '/'));
app.get('/', function(req, res){
res.sendfile('index.html',{root:__dirname});
});
io.on('connection', function(socket){
++uid;
var tank = {
id:uid,
initX:Math.random() * 500,
initY:Math.random() * 500,
color:'#'+Math.floor(Math.random()*16777215).toString(16)
}
socket.tank = tank;
for(var i = 0; i < count; i++)
{
pool[i].emit('create', socket.tank);
}
pool.push(socket);
count = pool.length;
for(var i = 0; i < count; i++)
{
socket.emit('create', pool[i].tank);
}
socket.emit('control', socket.tank.id);
console.log("Total players " + count);
socket.on('disconnect', function(){
var des = socket.tank.id;
pool.splice(pool.indexOf(socket), 1);
count = pool.length;
for(var j = 0; j < count; j++)
{
pool[j].emit('destroy', socket.tank);
}
console.log("Total players " + count);
});
socket.on('move', function(tank){
for(var j = 0; j < count; j++)
{
if(pool[j].tank.id != tank.id)
{
pool[j].emit('move', tank);
}
}
});
socket.on('fire', function(tank){
for(var j = 0; j < count; j++)
{
pool[j].emit('fire', tank);
}
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});