-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemyPirate.ts
145 lines (126 loc) · 5.53 KB
/
enemyPirate.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
138
139
140
141
142
143
144
145
class EnemyPirate extends Enemy {
static walkRightAnimation: Image[] = Utils.swapAnimationColors(assets.animation`Pirate Walk`, 14, 2)
static walkLeftAnimation: Image[] = Utils.flipAnimation(Utils.swapAnimationColors(assets.animation`Pirate Walk`, 14, 2))
static attackRightAnimation: Image[] = Utils.swapAnimationColors(assets.animation`Pirate Swing w Sword`, 14, 2)
static attackLeftAnimation: Image[] = Utils.flipAnimation(Utils.swapAnimationColors(assets.animation`Pirate Swing w Sword`, 14, 2))
static deathRightAnimation: Image[] = Utils.swapAnimationColors(assets.animation`Pirate Dead`, 14, 2)
static deathLeftAnimation: Image[] = Utils.flipAnimation(Utils.swapAnimationColors(assets.animation`Pirate Dead`, 14, 2))
static parryRightAnimation: Image[] = Utils.swapAnimationColors([assets.animation`Pirate Swing w Sword`[0]], 14, 2)
static parryLeftAnimation: Image[] = Utils.swapAnimationColors(Utils.flipAnimation([assets.animation`Pirate Swing w Sword`[0]]), 14, 2)
static parrySound: music.SoundEffect = music.createSoundEffect(WaveShape.Noise, 5000, 5000, 255, 0, 100, SoundExpressionEffect.Vibrato, InterpolationCurve.Curve)
static directionChangeInterval: number = 1000
static attackDelayMin: number = 3000
static attackDelayMax: number = 5000
static chanceOfParry: number = 66
private _isParrying: boolean = false
constructor({ x, y, target, riches }: { x: number, y: number, target?: EnemyTarget, riches?: number }) {
super({ x, y, target, sprite: sprites.create(assets.animation`Pirate Walk`[0]), riches, speed: 20, minDistanceFromTarget: 10 })
// Most often we spawn to the right, so walk left
this.walk('left')
}
public hit({ attacker, damage }: { attacker: Pirate, damage: number }): boolean {
if (this._isParrying) return false
if (Math.randomRange(0, 100) < EnemyPirate.chanceOfParry) {
this._isParrying = true
// Face the attacker
if (attacker.sprite.x > this.sprite.x) {
this.walk('right')
} else {
this.walk('left')
}
// Pause walking:
this.sprite.follow(this._currentTarget.sprite, 0)
music.play(EnemyPirate.parrySound, music.PlaybackMode.InBackground)
animation.runImageAnimation(
this.sprite,
this._facing === 'right' ? EnemyPirate.parryRightAnimation : EnemyPirate.parryLeftAnimation,
500,
false
)
setTimeout(() => {
this._isParrying = false
this.walk()
}, EnemyPirate.parryRightAnimation.length * 500 + 1000)
return false
} else {
this._isParrying = false
super.hit({ attacker, damage })
if (this.health <= 0) {
animation.runImageAnimation(
this.sprite,
this._facing === 'right' ? EnemyPirate.deathRightAnimation : EnemyPirate.deathLeftAnimation,
100,
false
)
}
return true
}
}
public render() {
// No Undead walking!
if (this.health <= 0 || this._isAttacking || this._isParrying) return
super.render()
// Attack randomly
if ((control.millis() - this._lastAttackTick) > this._nextAttackTime
&& Utils.getDistance(this.sprite, this._currentTarget.sprite) < 10) {
this._lastAttackTick = control.millis()
this._nextAttackTime = Math.randomRange(EnemyPirate.attackDelayMin, EnemyPirate.attackDelayMax)
this.attack()
}
}
protected walk(direction?: 'left' | 'right') {
super.walk(direction)
if (this._facing === 'left') {
animation.runImageAnimation(
this.sprite,
EnemyPirate.walkLeftAnimation,
500,
true
)
} else {
animation.runImageAnimation(
this.sprite,
EnemyPirate.walkRightAnimation,
500,
true
)
}
}
protected attack() {
super.attack()
// Play the swing animation
if (this._facing === 'right') {
animation.runImageAnimation(
this.sprite,
EnemyPirate.attackRightAnimation,
100,
false
)
} else {
animation.runImageAnimation(
this.sprite,
EnemyPirate.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) {
// music.play(Pirate.parrySound, music.PlaybackMode.InBackground)
// Check to see that our target is in range and fire the hit
if (Utils.getDistance(this.sprite, this._currentTarget.sprite) < 15) {
this._currentTarget.hit(this, 1)
}
}
}, EnemyPirate.attackRightAnimation.length / 2 * 100)
// Resume walking
setTimeout(() => {
this._isAttacking = false
if (this.health > 0 && this._currentTarget && this.sprite) {
this.walk()
}
}, EnemyPirate.attackRightAnimation.length * 100)
}
}