Skip to content

Commit

Permalink
Enemy pirates always parry
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 18, 2024
1 parent 18caf8e commit e7db26e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion boatBattle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace BoatBattle {
hitEnemies.forEach((enemy) => {
// No looting of EnemyPirates
if (enemy.health > 0) {
enemy.hit(1)
enemy.hit({ attacker: pirate, damage: 1 })
}
})

Expand Down
2 changes: 1 addition & 1 deletion enemy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Enemy {
this.sprite.follow(this._currentTarget.sprite, this._speed)
}

protected hit(damage: number) {
protected hit({ attacker, damage }: { attacker: Pirate, damage: number }) {
this.health -= damage

if (this.health <= 0) {
Expand Down
44 changes: 36 additions & 8 deletions enemyPirate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ class EnemyPirate extends Enemy {
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 parrySound: music.SoundEffect = music.createSoundEffect(WaveShape.Noise, 5000, 5000, 255, 0, 100, SoundExpressionEffect.Vibrato, InterpolationCurve.Curve)
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 = 100

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 })
Expand All @@ -18,22 +23,45 @@ class EnemyPirate extends Enemy {
this.walk('left')
}

public hit(damage: number) {
super.hit(damage)

if (this.health <= 0) {
public hit({ attacker, damage }: {attacker: Pirate, damage: number }) {
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.deathRightAnimation : EnemyPirate.deathLeftAnimation,
100,
this._facing === 'right' ? EnemyPirate.parryRightAnimation : EnemyPirate.parryLeftAnimation,
500,
false
)
setTimeout(() => {
this._isParrying = false
this.walk()
}, EnemyPirate.parryRightAnimation.length * 500 + 1000)
} else {
super.hit({ attacker, damage })

if (this.health <= 0) {
animation.runImageAnimation(
this.sprite,
this._facing === 'right' ? EnemyPirate.deathRightAnimation : EnemyPirate.deathLeftAnimation,
100,
false
)
}
}
}

public render() {
// No Undead walking!
if (this.health <= 0 || this._isAttacking) return
if (this.health <= 0 || this._isAttacking || this._isParrying) return

super.render()

Expand Down
2 changes: 1 addition & 1 deletion main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,4 @@ function startGame(initialState?: States) {
switchState(initialState ? initialState : States.Menu)
}

startGame()
startGame(States.BoatBattle)
4 changes: 2 additions & 2 deletions militia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Militia extends Enemy {
this.walk('left')
}

public hit(damage: number) {
super.hit(damage)
public hit({ attacker, damage }: { attacker: Pirate, damage: number }) {
super.hit({ attacker, damage })

if (this.health <= 0) {
animation.runImageAnimation(
Expand Down

0 comments on commit e7db26e

Please sign in to comment.