-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmilitia.ts
137 lines (118 loc) · 4.76 KB
/
militia.ts
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
class Militia extends Enemy {
static walkRightAnimation: Image[] = assets.animation`Militia Walk`
static walkLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Militia Walk`)
static attackRightAnimation: Image[] = assets.animation`Militia Shoot`
static attackLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Militia Shoot`)
static deathRightAnimation: Image[] = assets.animation`Militia Die`
static deathLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Militia Die`)
static parrySound: music.SoundEffect = music.createSoundEffect(WaveShape.Noise, 5000, 5000, 255, 0, 100, SoundExpressionEffect.Vibrato, InterpolationCurve.Curve)
static directionChangeInterval: number = 1000
static attackDelayMin: number = 4000
static attackDelayMax: number = 6000
constructor({ x, y, target, riches }: { x: number, y: number, target?: Pirate, riches?: number }) {
super({ x, y, target, sprite: sprites.create(assets.animation`Militia Walk`[0]), riches })
// Most often we spawn to the right, so walk left
this.walk('left')
}
public hit({ attacker, damage }: { attacker: Pirate, damage: number }): boolean {
super.hit({ attacker, damage })
if (this.health <= 0) {
animation.runImageAnimation(
this.sprite,
this._facing === 'right' ? Militia.deathRightAnimation : Militia.deathLeftAnimation,
100,
false
)
}
return true
}
public render() {
// No Undead walking!
if (this.health <= 0 || this._isAttacking) return
super.render()
// Attack randomly
if ((control.millis() - this._lastAttackTick) > this._nextAttackTime) {
this._lastAttackTick = control.millis()
this._nextAttackTime = Math.randomRange(Militia.attackDelayMin, Militia.attackDelayMax)
this.attack()
}
}
public lootTheBody() {
if (this.riches > 0) {
TreasureStats.currentTreasure = {
onBoat: TreasureStats.currentTreasure.onBoat,
onIsland: TreasureStats.currentTreasure.onIsland,
inPocket: TreasureStats.currentTreasure.inPocket + this.riches
}
TreasureStats.show({})
this.riches = 0
const oldX = this.sprite.x
const oldY = this.sprite.y
this.sprite.destroy()
if (this._facing === 'left') {
this.sprite = sprites.create(assets.image`Militia Broken and Broke Left`)
} else {
this.sprite = sprites.create(assets.image`Militia Broken and Broke`)
}
this.sprite.x = oldX
this.sprite.y = oldY
}
}
protected walk(direction?: 'left' | 'right') {
super.walk(direction)
if (this._facing === 'left') {
animation.runImageAnimation(
this.sprite,
Militia.walkLeftAnimation,
500,
true
)
} else {
animation.runImageAnimation(
this.sprite,
Militia.walkRightAnimation,
500,
true
)
}
}
protected attack() {
super.attack()
// Play the fire animation
if (this._facing === 'right') {
animation.runImageAnimation(
this.sprite,
Militia.attackRightAnimation,
100,
false
)
} else {
animation.runImageAnimation(
this.sprite,
Militia.attackLeftAnimation,
100,
false
)
}
// Slightly after the animation we check to see if we hit
setTimeout(() => {
// Make sure we didn't die in this tiny delay:
if (this.health > 0 && this._currentTarget && this.sprite) {
// bigCrash or sonar....
music.play(music.melodyPlayable(music.bigCrash), music.PlaybackMode.InBackground)
// Check to see that our target is in range and fire the hit
if (Math.abs(this.sprite.y - this._currentTarget.sprite.y) < 20) {
// scene.cameraShake(2, 500)
this._currentTarget.hit(this, 1)
}
}
}, Militia.attackRightAnimation.length / 2 * 100)
// Resume walking
setTimeout(() => {
this._isAttacking = false
if (this.health > 0 && this._currentTarget && this.sprite) {
this.walk()
}
}, Militia.attackRightAnimation.length * 100)
}
}