-
Notifications
You must be signed in to change notification settings - Fork 0
/
pirate.ts
340 lines (305 loc) · 12.6 KB
/
pirate.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
type ActionObject = {
attack: () => void
parry: () => void
faceLeft: () => void
faceRight: () => void
}
type AttackCallbackParams = { pirate: Pirate }
class Pirate {
static _attackDelay: number = 400
static parrySound: music.SoundEffect = music.createSoundEffect(WaveShape.Noise, 5000, 5000, 255, 0, 100, SoundExpressionEffect.Vibrato, InterpolationCurve.Curve)
static deathSound: music.SoundEffect = music.createSoundEffect(WaveShape.Triangle, 2202, 476, 129, 0, 861, SoundExpressionEffect.Warble, InterpolationCurve.Logarithmic)
static hitSound: music.SoundEffect = music.createSoundEffect(WaveShape.Noise, 462, 1, 150, 0, 100, SoundExpressionEffect.Tremolo, InterpolationCurve.Logarithmic)
static heartIcon: Image = assets.image`Heart`
private idleRightAnimation: Image[] = assets.animation`Pirate Stand`
private idleLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Stand`)
private attackLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Swing w Sword`)
private attackRightAnimation: Image[] = assets.animation`Pirate Swing w Sword`
private parryLeftSprite: Image = assets.image`Pirate`
private parryRightSprite: Image = assets.image`Pirate`
private walkRightAnimation: Image[] = assets.animation`Pirate Walk`
private walkLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Walk`)
private hurtRightAnimation: Image[] = assets.animation`Pirate Hurt`
private hurtLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Hurt`)
private deathRightAnimation: Image[] = assets.animation`Pirate Dead`
private deathLeftAnimation: Image[] = Utils.flipAnimation(assets.animation`Pirate Dead`)
private facing: 'left' | 'right'
private controller: controller.Controller
private isAttacking?: 'left' | 'right'
private isGettingHurt?: boolean = false
private isParrying?: 'left' | 'right'
private _boundaries: number[] = [0, 0, 160, 120]
private _lastAttackTick: number = 0
private _isAttackingTimeout: number
private _statLocation?: number[] = [0,0]
private _healthSprites: Sprite[] = []
private _onDieCallback: (T: { pirate: Pirate }) => void
public sprite: Sprite
// This action object is for registering event listeners
// It keeps all functions stable per this class so we can removeEventListners
public action: ActionObject = {
attack: () => undefined,
parry: () => undefined,
faceLeft: () => undefined,
faceRight: () => undefined
}
public health: number
// Makes "facing" aka "direction" read only
// pirate.direction
public get direction() {
return this.facing
}
constructor({ control, playerNumber, onAttack, onDie, boundaries, statLocation }: {
control: controller.Controller,
playerNumber: 0 | 1,
onAttack: (T: AttackCallbackParams) => void,
onDie: (T: { pirate: Pirate }) => void,
boundaries: number[],
statLocation?: number[]
}) {
this.health = 3
this.facing = 'right'
this._boundaries = boundaries
this._statLocation = statLocation
this._onDieCallback = onDie
this.sprite = sprites.create(assets.image`Pirate`, SpriteKind.Player)
this.sprite.setStayInScreen(true)
if (playerNumber === 1) {
this._setupAnimationColors(4)
}
this._setupAnimations()
this._updateStats()
// Setup multiplayer
mp.setPlayerSprite(mp.getPlayerByNumber(playerNumber), this.sprite)
// Setup the controller handlers
this.controller = control
// We can't simply do `this.attack` as the event listeners can't handle a "lambda"
// And we need to keep a reference to the callback function so we can removeEventListner
this.action.attack = () => this.attack(onAttack)
this.action.parry = () => this.parry()
this.action.faceLeft = () => this.face('left')
this.action.faceRight = () => this.face('right')
this.controller.A.addEventListener(ControllerButtonEvent.Pressed, this.action.attack)
// this.controller.B.addEventListener(ControllerButtonEvent.Pressed, this.action.parry)
this.controller.left.addEventListener(ControllerButtonEvent.Pressed, this.action.faceLeft)
this.controller.right.addEventListener(ControllerButtonEvent.Pressed, this.action.faceRight)
}
public place(x: number, y: number) {
// Starting location or teleport
this.sprite.x = x
this.sprite.y = y
}
public destroy(andDestorySprites: boolean = true) {
if (andDestorySprites) {
this.sprite.destroy()
if (this._healthSprites.length) {
this._healthSprites.forEach((sprite) => sprite.destroy())
}
}
// Remove all event listeners
this.controller.A.removeEventListener(ControllerButtonEvent.Pressed, this.action.attack)
this.controller.B.removeEventListener(ControllerButtonEvent.Pressed, this.action.parry)
this.controller.left.removeEventListener(ControllerButtonEvent.Pressed, this.action.faceLeft)
this.controller.right.removeEventListener(ControllerButtonEvent.Pressed, this.action.faceRight)
}
public render() {
if (!this.isAttacking && !this.isGettingHurt && this.health > 0) {
this.sprite.x += this.controller.dx(50)
this.sprite.y += this.controller.dy(50)
}
this.sprite.z = this.sprite.y
// Brute force boundaries
if (this.sprite.y < this._boundaries[1]) {
this.sprite.y = this._boundaries[1]
}
if (this.sprite.y > this._boundaries[3]) {
this.sprite.y = this._boundaries[3]
}
}
public hit(enemy: Enemy, damage: number) {
if (this.isParrying) {
music.play(Pirate.parrySound, music.PlaybackMode.InBackground)
return
}
// Can't get hit if you are currently getting hit (invicible during your animation sorta)
if (this.isGettingHurt) return
// Can't get hit if you're dead
if (this.health <= 0) return
this.health -= damage
this.isGettingHurt = true
scene.cameraShake(2, 500)
// Setting to a character animation state that we don't have a rule for
// to pause any animation changes while we do this animation
// Setting state removes automatic state updates (aka how we prevent this)
characterAnimations.setCharacterState(this.sprite, 1024)
if (this.health > 0) {
animation.runImageAnimation(
this.sprite,
this.facing === 'right' ? this.hurtRightAnimation : this.hurtLeftAnimation,
200,
false
)
this._updateStats()
setTimeout(() => {
this.isGettingHurt = false
// Re-enable the character animations
characterAnimations.clearCharacterState(this.sprite)
}, this.hurtLeftAnimation.length * 200)
} else {
// Reduce pirate lives count
PirateLives.updatePirateCount(-1)
// You dead!
animation.runImageAnimation(
this.sprite,
this.facing === 'right' ? this.deathRightAnimation : this.deathLeftAnimation,
100,
false
)
this._updateStats()
this.die()
}
}
private die() {
console.log('Yarrg, ye be swimmin\' with d\'fishes! 💀🐟')
this.isGettingHurt = false
this.isAttacking = undefined
this.destroy(false)
this._onDieCallback({ pirate: this })
}
private parry() {
console.log('parry ' + this.controller.playerIndex)
}
private attack(attackCallback: (T: AttackCallbackParams) => void) {
// You can't attack if you are getting hurt
if (this.isGettingHurt) return
// Can't attack more frequently than attackDelay
if (control.millis() - this._lastAttackTick < Pirate._attackDelay) return
// music.play(Pirate.attackSound, music.PlaybackMode.InBackground)
const oldPos = { x: this.sprite.x, y: this.sprite.y }
if (this.facing === 'right') {
this._lastAttackTick = control.millis()
this.isAttacking = 'right'
attackCallback({ pirate: this })
// if (didHit) {
// music.play(Pirate.hitSound, music.PlaybackMode.InBackground)
// }
animation.runImageAnimation(
this.sprite,
this.attackRightAnimation,
50,
false
)
// And reset the sprite so it can no longer hit something
setTimeout(() => {
this.isAttacking = undefined
}, this.attackRightAnimation.length * 50)
} else {
this._lastAttackTick = control.millis()
this.isAttacking = 'left'
attackCallback({ pirate: this })
animation.runImageAnimation(
this.sprite,
this.attackLeftAnimation,
50,
false
)
// And reset the sprite so it can no longer hit something
setTimeout(() => {
this.isAttacking = undefined
}, this.attackLeftAnimation.length * 50)
}
}
private face(direction: 'left' | 'right') {
if (direction === 'left' && this.facing === 'right') {
this.facing = 'left'
} else if (direction === 'right' && this.facing === 'left') {
this.facing = 'right'
}
}
private _updateStats() {
if (!this._statLocation) return
if (this._healthSprites.length) {
this._healthSprites.forEach((sprite) => sprite.destroy())
}
this._healthSprites = Utils.getArrayOfLength(this.health).map((index) => {
const s = sprites.create(Pirate.heartIcon)
// 8 pixel image, 2 pixel margin
s.x = this._statLocation[0] + (index * s.width + 2)
s.y = this._statLocation[1]
s.z = 120
return s
})
}
private _setupAnimationColors(toColor: number = 14) {
Utils.swapAnimationColors(this.idleLeftAnimation, 14, toColor)
Utils.swapAnimationColors(this.idleRightAnimation, 14, toColor)
Utils.swapAnimationColors(this.attackLeftAnimation, 14, toColor)
Utils.swapAnimationColors(this.attackRightAnimation, 14, toColor)
Utils.swapAnimationColors(this.walkRightAnimation, 14, toColor)
Utils.swapAnimationColors(this.walkLeftAnimation, 14, toColor)
Utils.swapAnimationColors(this.hurtRightAnimation, 14, toColor)
Utils.swapAnimationColors(this.hurtLeftAnimation, 14, toColor)
Utils.swapAnimationColors(this.deathRightAnimation, 14, toColor)
Utils.swapAnimationColors(this.deathLeftAnimation, 14, toColor)
}
private _setupAnimations() {
// Setup animations
// These numbers are coming from the source code: https://github.com/microsoft/arcade-character-animations/blob/main/main.ts
characterAnimations.loopFrames(
this.sprite,
this.walkRightAnimation,
150,
// Moving right (and facing right):
8 + 128
)
characterAnimations.loopFrames(
this.sprite,
this.walkLeftAnimation,
150,
// Moving left (and facing left):
32 + 512
)
characterAnimations.loopFrames(
this.sprite,
this.idleLeftAnimation,
150,
// Facing left:
32
)
characterAnimations.loopFrames(
this.sprite,
this.idleRightAnimation,
150,
// Facing right:
8
)
characterAnimations.loopFrames(
this.sprite,
this.walkLeftAnimation,
150,
// Moving up (facing left):
32 + 64
)
characterAnimations.loopFrames(
this.sprite,
this.walkRightAnimation,
150,
// Moving up (facing right):
8 + 64
)
characterAnimations.loopFrames(
this.sprite,
this.walkLeftAnimation,
150,
// Moving down (facing left):
32 + 256
)
characterAnimations.loopFrames(
this.sprite,
this.walkRightAnimation,
150,
// Moving down (facing right):
8 + 256
)
}
}