Skip to content

Commit

Permalink
Refactored attack to simply use the pirate
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 16, 2024
1 parent 3ab7c77 commit b6f5dd9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
15 changes: 15 additions & 0 deletions boatBattle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace BoatBattle {
let enemies: Sprite[]
let player1: Pirate
let player2: Pirate

export function init() {
scene.setBackgroundColor(6)
scene.setBackgroundImage(assets.image`empty`)

// Spawn the players
// player1 = new Pirate({ control: controller.player1 })
}

export function destory() {}
}
8 changes: 4 additions & 4 deletions island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace Island {
let _treasureSprite: Sprite
let _treasureOpened: boolean = false

function onPirateAttack({ pirate, direction }: { pirate: Pirate, direction: 'left' | 'right' }) {
const dirPix = direction === 'left' ? -1 : 1
function onPirateAttack({ pirate }: { pirate: Pirate }) {
const dirPix = pirate.direction === 'left' ? -1 : 1
// The hit zone is the pirate "sword" box: [center, right|left] and [top, bottom]
const hitXZone = [pirate.sprite.x, pirate.sprite.x + (13 * dirPix)]
// The sword is only near the top of the sprite, we don't kill with feet
Expand All @@ -46,7 +46,7 @@ namespace Island {
currentEnemies.forEach((enemy) => {
// Don't hurt the dead, that's just mean
if (enemy.health <= 0 && enemy.riches <= 0) return
if (direction === 'right'
if (pirate.direction === 'right'
&& enemy.sprite.x >= hitXZone[0] && enemy.sprite.x <= hitXZone[1]
// Bottom of pirate is overlapping the top of the enemy (and opposite)
&& hitYZone[1] >= enemy.sprite.y - (enemy.sprite.height / 2) && hitYZone[0] <= enemy.sprite.y + (enemy.sprite.height / 2)) {
Expand All @@ -55,7 +55,7 @@ namespace Island {
} else {
enemy.hit(1)
}
} else if (direction === 'left'
} else if (pirate.direction === 'left'
&& enemy.sprite.x <= hitXZone[0] && enemy.sprite.x >= hitXZone[1]
// Same vertical check as the right side
&& hitYZone[1] >= enemy.sprite.y - (enemy.sprite.height / 2) && hitYZone[0] <= enemy.sprite.y + (enemy.sprite.height / 2)) {
Expand Down
12 changes: 9 additions & 3 deletions pirate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type ActionObject = {
faceRight: () => void
}

type AttackCallbackParams = { pirate: Pirate, direction: 'left' | 'right' }
type AttackCallbackParams = { pirate: Pirate }

class Pirate {
static _attackDelay: number = 400
Expand Down Expand Up @@ -51,6 +51,12 @@ class Pirate {
}
public health: number

// Makes "facing" aka "direction" read only
// pirate.direction
public get direction() {
return this.facing
}

constructor({ control, playerNumber, onAttack, onDie, topBoundary, statLocation }: {
control: controller.Controller,
playerNumber: 0 | 1,
Expand Down Expand Up @@ -195,7 +201,7 @@ class Pirate {
if (this.facing === 'right') {
this._lastAttackTick = control.millis()
this.isAttacking = 'right'
attackCallback({ pirate: this, direction: 'right' })
attackCallback({ pirate: this })
animation.runImageAnimation(
this.sprite,
this.attackRightAnimation,
Expand All @@ -209,7 +215,7 @@ class Pirate {
} else {
this._lastAttackTick = control.millis()
this.isAttacking = 'left'
attackCallback({ pirate: this, direction: 'left' })
attackCallback({ pirate: this })
animation.runImageAnimation(
this.sprite,
this.attackLeftAnimation,
Expand Down
3 changes: 2 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"islands.ts",
"pirateLives.ts",
"gameOver.ts",
"win.ts"
"win.ts",
"boatBattle.ts"
],
"testFiles": [
"test.ts"
Expand Down
2 changes: 2 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ namespace Utils {
anim.map(frame => frame.replace(fromColor, toColor))
return anim
}

export function checkAttack({ pirate, enemies }: { pirate: Pirate, enemies: Sprite[] }) {}
}

0 comments on commit b6f5dd9

Please sign in to comment.