-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
151 lines (126 loc) · 4.77 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
class Region {
constructor(id, x, y, size, wsUri, scene) {
this.x = x
this.y = y
this.size = size
this.color = getRandomColor()
this.gridHelper = new THREE.GridHelper(size, size / 10, this.color, this.color)
this.gridHelper.position.set(x + size / 2, y, -size / 2)
this.players = new Map()
this.energies = new Map()
this.wsUri = wsUri
this.websocket = new WebSocket(wsUri)
this.websocket.onopen = this.onOpen
this.websocket.onclose = this.onClose
this.websocket.onmessage = this.onMessage
this.websocket.onerror = this.onError
this.websocket.region = this //TODO: please forgive me but Websocket override the this scope so it's not possible to use region methods without this trick
this.scene = scene
}
grid() {
return this.gridHelper
}
_updatePlayers(playersInfos) {
console.log("Number of players: ", playersInfos.length)
playersInfos.forEach(p => {
let player = this.players.get(p.ref)
if (player !== undefined) {
//console.log(`player ${p.ref} moved at position x: ${p.position.x} y: ${p.position.y} and weight of ${p.weight}`)
player.position = p.position
player.velocity = p.velocity
player.weight = p.weight
player.mesh.geometry.radius = p.weight
player.mesh.position.set(p.position.y, p.weight / 2, -p.position.x)
}
else {
let player = new Player(p.position, 0, p.weight, p.ref)
this.players.set(p.ref, player)
this.scene.add(player.getMesh())
}
})
//remove killed players
this.players.forEach(p => {
if (playersInfos.find(player => player.ref === p.actorRef) === undefined) {
console.log("remove player: ", p.actorRef)
this.scene.remove(p.getMesh())
this.players.delete(p.actorRef)
}
})
}
_updateEnergies(energiesInfos) {
energiesInfos.forEach(e => {
console.log("number of energies", energiesInfos.length)
let energy = this.energies.get(e.ref)
if (energy !== undefined) {
// Should not change
energy.position = e.position
energy.value = e.value
energy.mesh.position.set(e.position.y, 4, -e.position.x)
}
else {
let energy = new Energy(e.position, e.value, e.ref)
this.energies.set(e.ref, energy)
this.scene.add(energy.mesh)
}
})
//remove consumed energies
this.energies.forEach(e => {
if (energiesInfos.find(energy => energy.ref === e.actorRef) === undefined) {
console.log("remove energy: ", e.actorRef)
this.scene.remove(e.getMesh())
this.energies.delete(e.actorRef)
}
})
}
onOpen(evt) {
console.log(`trying to connect on ${this.url}...`, evt)
}
onClose(evt) {
console.log(`disconnected to ${this.url}`, evt)
}
onMessage(rawData) {
let data = JSON.parse(rawData.data);
this.region._updatePlayers(data[0])
this.region._updateEnergies(data[1])
}
onError(evt) {
this.websocket.close()
console.log(evt)
}
}
class Player {
constructor(position, velocity, weight, actorRef) {
console.log(`create the new player ${actorRef} at x:${position.x} y:${position.y} and with a weight of ${weight}`)
this.position = position
this.velocity = velocity
this.weight = weight
this.color = getRandomColor()
this.actorRef = actorRef
let geometry = new THREE.SphereGeometry(this.weight, 32, 32)
let material = new THREE.MeshBasicMaterial({ color: this.color })
this.mesh = new THREE.Mesh(geometry, material)
this.mesh.position.set(position.y, weight / 2, -position.x)
}
getMesh() {
return this.mesh
}
}
class Energy {
constructor(position, value, actorRef) {
console.log(`create the new energy ${actorRef} at x:${position.x} y:${position.y} and with a value of ${value}`)
this.position = position
this.value = value
this.color = getRandomColor()
this.actorRef = actorRef
let geometry = new THREE.BoxGeometry(8, 8, 8)
let material = new THREE.MeshBasicMaterial({ color: this.color })
this.mesh = new THREE.Mesh(geometry, material)
this.mesh.position.set(position.y, 4, -position.x)
}
getMesh() {
return this.mesh
}
}
function getRandomColor() {
return new THREE.Color(Math.random() * 0xffffff)
}