-
Notifications
You must be signed in to change notification settings - Fork 14
/
player.js
96 lines (74 loc) · 2.18 KB
/
player.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
var players = {},
names = {};
this.add = function(chunkX, chunkY, name, pos, socket) {
var key = chunkX + ',' + chunkY;
if(!players[key]) players[key] = [];
players[key].push({name: name, pos: pos, socket: socket});
return key;
}
this.updatePosition = function(oldPos, name, newPos, socket) {
if(!oldPos) {
console.log("No position", name, newPos);
return;
}
var i = 0, p = players[oldPos], l = p.length,
chunk = newPos.chunk,
pos = newPos.pos;
//loop over the chunk looking for the socket
for(; i < l; ++i) {
if(p[i].name == name) {
p.splice(i, 1);
break;
}
}
//tell nearby users of the move
broadcast(chunk[0], chunk[1], "playermove", {name: name, pos: newPos}, name);
return this.add(chunk[0], chunk[1], name, pos, socket);
}
this.register = function(name) {
if(name == "") return false;
if(names[name]) return false;
names[name] = true;
return true;
}
this.remove = function(name) {
var i, l, p, key, chunk, found = false;
//loop everything
for(key in players) {
p = players[key];
if(!p) continue;
for(i = 0, l = p.length; i < l; ++i) {
if(p[i].name === name) {
p.splice(i, 1);
found = true;
break;
}
}
if(found) break;
}
if(key) {
chunk = key.split(",");
//remove from names registry
delete names[name];
broadcast(chunk[0], chunk[1], "playerleave", name, name);
}
}
function broadcast(chunkX, chunkY, event, msg, name) {
var audience = [], i = 0, l;
//broadcast to all users in surronding chunks
audience = audience.concat(players[chunkX + "," + chunkY],
players[chunkX + "," + chunkY-1],
players[chunkX+1 + "," + chunkY-1],
players[chunkX+1 + "," + chunkY],
players[chunkX+1 + "," + chunkY+1],
players[chunkX + "," + chunkY+1],
players[chunkX-1 + "," + chunkY+1],
players[chunkX-1 + "," + chunkY],
players[chunkX-1 + "," + chunkY-1]);
//loop over and broadcast to the sockets
for(l = audience.length; i < l; ++i) {
if(!audience[i]) continue;
if(audience[i].name === name) continue;
audience[i].socket.emit(event, msg);
}
}