-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.js
More file actions
42 lines (40 loc) · 784 Bytes
/
state.js
File metadata and controls
42 lines (40 loc) · 784 Bytes
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
class Player {
constructor(id, x, y) {
this.id = id;
this.x = x;
this.y = y;
}
}
var state = {
params: {
moveSpeed: 5,
},
players: {},
};
module.exports = {
getState: function () {
return state;
},
createNewPlayer: function (id) {
state.players[id] = new Player(id, 300, 300);
},
removePlayer: function (id) {
delete state.players[id];
},
movePlayer: function (id, direction) {
var player = state.players[id] || {};
var displacement = state.params.moveSpeed;
if (direction.left) {
player.x -= displacement;
}
if (direction.up) {
player.y -= displacement;
}
if (direction.right) {
player.x += displacement;
}
if (direction.down) {
player.y += displacement;
}
},
};