-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bullet.js
44 lines (33 loc) · 1007 Bytes
/
Bullet.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
const uuid = require('uuid/v1')
class Bullet {
constructor(x, y, damage, speed, direction, socket) {
// console.log(x)
this.speed = speed
this.radius = 5
this.damage = damage
this.direction = direction
this.x = x
this.y = y
this.socket = socket
this.destroyed = false
this.maxLifeTime = 500
this.lifeTime = 0
this.id = uuid()
}
update() {
if (!this.destroyed) {
this.y += this.speed * this.direction
// console.log(this.x)
this.socket.emit('bulletUpdate', {x: this.x, y: this.y, id: this.id})
this.socket.broadcast.emit('enemyBulletUpdate', {x: this.x, y: this.y, id: this.id})
this.lifeTime++
if (this.lifeTime >= this.maxLifeTime) {
this.destroyed = true
this.socket.emit('bulletDestroyed', {id: this.id})
this.socket.broadcast.emit('enemyBulletDestroyed', {id: this.id})
console.log('Bullet life time expired, destroying')
}
}
}
}
module.exports = Bullet