-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.js
114 lines (102 loc) · 2.81 KB
/
enemy.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
import particles from '../res/sprites/particles/*.png'
import { GameObjects, Math } from 'phaser'
export class Enemy extends GameObjects.Sprite {
constructor(scene) {
super(scene, 0, 0, 'enemy-basic')
scene.physics.world.enable(this)
scene.add.existing(this)
scene.physics.add.overlap(this, scene.groups.bullets, this.onOverlap, () => {}, this)
scene.physics.add.overlap(this, scene.groups.player, this.onOverlap, () => {}, this)
scene.groups.enemies.add(this)
this.setData('behaviors', [])
this.setData('health', 0)
this.setData('score', 1)
this.dead = false
this.body.setVelocityY(0)
this.setActive(false)
this.setVisible(false)
this.body.enabled = false
this.setDepth(1)
}
kill() {
this.body.setVelocityY(0)
this.setActive(false)
this.setVisible(false)
this.body.enabled = false
this.getData('behaviors').forEach((behavior) => {
behavior.destroy()
})
this.setData('behaviors', [])
}
onOverlap(me, them) {
this.data.inc('health', -1)
if (this.data.get('health') <= 0) {
this.dead = true
}
}
update(time, delta) {
if (this.dead) {
this.scene.data.inc('score', this.data.get('score'))
this.scene.sound.play('explosion', {
detune: Math.FloatBetween(0, 1000),
volume: 0.35 * game.registry.get('sfxVolume')
})
const hitParticles = this.scene.add.particles(Object.keys(particles)[Math.Between(0, Object.keys(particles).length - 1)]).createEmitter({
x: this.x,
y: this.y,
accelerationX: -10,
accelerationY: -10,
speed: {
min: 75,
max: 100
},
angle: {
min: 0,
max: 360
},
rotate: {
min: 0,
max: 20
},
radial: true,
blendMode: 'SCREEN',
lifespan: 500,
alpha: {
start: 1,
end: 0
},
scale: {
start: 0.5,
end: 0
}
})
hitParticles.explode(15)
this.kill()
return
}
if (this.y >= 700) {
this.kill()
return
}
if (this.data.get('behaviors').length) {
this.data.get('behaviors').forEach(behavior => behavior.update(time, delta))
}
}
spawn(name, x, y) {
this.dead = false
this.setPosition(x, y)
this.body.setVelocityY(200)
this.setActive(true)
this.setVisible(true)
this.body.enabled = true
this.enemyData = this.scene.dataController.enemies.find(enemy => enemy.name === name)
this.data.set('behaviors', this.scene.dataController.createBehaviors(this, name))
this.data.set('health', this.enemyData.health)
this.data.set('score', this.enemyData.score)
this.setTexture(`enemy-${name}`)
this.anims.play({
key: `enemy-${name}-idle`,
repeat: -1
})
}
}