-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
157 lines (122 loc) · 3.57 KB
/
game.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
var Physics = require('./physicsjs-full-0.6.0.min');
module.exports = function(io) {
var world,
players = {},
viewWidth = 512,
viewHeight = 512,
radius = viewWidth / 2,
acceleration = 0.00025,
movementVectors = {
up: new Physics.vector(0, -acceleration),
down: new Physics.vector(0, acceleration),
left: new Physics.vector(-acceleration, 0),
right: new Physics.vector(acceleration, 0)
};
(function init() {
world = Physics();
world.add(Physics.behavior('body-impulse-response'));
world.add(Physics.behavior('body-collision-detection'));
world.add(Physics.behavior('sweep-prune'));
function physicsUpdate() {
var time = Date.now();
world.step(time);
for(var id in players) {
var player = players[id];
var body = player.body,
x = player.body.state.pos.get(0),
y = player.body.state.pos.get(1),
outside = distance(viewWidth / 2, viewHeight / 2, x, y) > radius + 16;
if (!player.outside && outside) resetPlayer(player); //Meaning we _just_ left
player.outside = outside;
if (!player.outside) {
for(var direction in movementVectors) {
if (player.movement[direction]) {
body.accelerate(movementVectors[direction]);
}
}
}
}
setTimeout(physicsUpdate, 1000 / 66);
}
physicsUpdate();
})();
function resetPlayer(player) {
setTimeout(function() {
var coords = respawnCoordinates();
player.startOfLife = Date.now();
player.body.state.pos.set(coords.x, coords.y);
player.body.state.vel.set(0, 0);
player.body.state.acc.set(0, 0);
}, 3000);
}
function serverUpdate() {
var data = [];
for(var id in players) {
var player = players[id];
var timeAlive = player.outside ? 0 : ((Date.now() - player.startOfLife) / 1000) | 0;
var playerData = {
id: id,
x: player.body.state.pos.get(0),
y: player.body.state.pos.get(1),
name: player.info.name,
timeAlive: timeAlive,
outside: player.outside
};
data.push(playerData);
if (Date.now() - player.lastMessage > 60000) {
world.removeBody(player.body);
delete players[id];
io.emit('leave', id);
}
}
io.emit('update', data);
setTimeout(serverUpdate, 1000 / 22);
}
serverUpdate();
function distance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
function respawnCoordinates() {
var x = 0, y = 0;
do {
x = Math.random() * viewWidth;
y = Math.random() * viewHeight;
}
while(distance(viewWidth / 2, viewHeight / 2, x, y) > radius - 32);
return {x: x, y: y};
}
io.on('connection', function(socket) {
console.log("Client", socket.id, "connected");
var coords = respawnCoordinates();
var body = Physics.body('circle', {
x: coords.x,
y: coords.y,
cof: 1,
radius: 16
});
world.add(body);
var player = players[socket.id] = {
info: socket.id,
movement: [],
startOfLife: Date.now(),
outside: false,
body: body,
lastMessage: Date.now()
};
socket.on('disconnect', function() {
console.log("Client", player.info.name, "disconnected");
world.removeBody(body);
delete players[socket.id];
io.emit('leave', socket.id);
});
socket.on('info', function(data) {
player.info = data;
player.info.name = player.info.name.substr(0, 16);
console.log("Client", socket.id, "identified as", player.info.name);
});
socket.on('move', function(data) {
player.movement = data;
player.lastMessage = Date.now();
});
});
};