forked from endel/tanx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.js
60 lines (47 loc) · 1.05 KB
/
state.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
class State {
constructor (world) {
this.world = world
this.score = 0
this.winner = {
team: null,
scores: []
}
// add 4 teams
this.teams = []
for (var i=0; i<4; i++) {
this.teams.push({ id: i, score: 0, tanks: 0 })
}
}
reset () {
// team scores
for(var i = 0; i < 4; i++) {
this.teams[i].score = 0;
}
// tanks scores
this.world.forEach('tank', (tank) => {
tank.score = 0;
tank.killer = null;
tank.respawn();
});
// room score
this.score = 0;
}
toJSON () {
var pickables = {}
this.world.forEach('pickable', (item) => pickables[item.id] = item )
var tanks = {}
this.world.forEach('tank', (item) => tanks[item.id] = item )
var bullets = {}
this.world.forEach('bullet', (item) => bullets[item.id] = item )
return {
score: this.score,
winner: this.winner,
teams: this.teams,
// world state
pickables: pickables,
tanks: tanks,
bullets: bullets
}
}
}
module.exports = State