From b6f5dd9fe979c750b732800227c001db2cce1de4 Mon Sep 17 00:00:00 2001 From: Chris Weed Date: Tue, 16 Jul 2024 08:00:00 -0500 Subject: [PATCH] Refactored attack to simply use the pirate --- boatBattle.ts | 15 +++++++++++++++ island.ts | 8 ++++---- pirate.ts | 12 +++++++++--- pxt.json | 3 ++- utils.ts | 2 ++ 5 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 boatBattle.ts diff --git a/boatBattle.ts b/boatBattle.ts new file mode 100644 index 0000000..e364a30 --- /dev/null +++ b/boatBattle.ts @@ -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() {} +} diff --git a/island.ts b/island.ts index dd96f6e..3602747 100644 --- a/island.ts +++ b/island.ts @@ -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 @@ -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)) { @@ -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)) { diff --git a/pirate.ts b/pirate.ts index 1194891..6301336 100644 --- a/pirate.ts +++ b/pirate.ts @@ -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 @@ -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, @@ -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, @@ -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, diff --git a/pxt.json b/pxt.json index 4ca5c97..aea7478 100644 --- a/pxt.json +++ b/pxt.json @@ -25,7 +25,8 @@ "islands.ts", "pirateLives.ts", "gameOver.ts", - "win.ts" + "win.ts", + "boatBattle.ts" ], "testFiles": [ "test.ts" diff --git a/utils.ts b/utils.ts index b6b443e..5b7894d 100644 --- a/utils.ts +++ b/utils.ts @@ -26,4 +26,6 @@ namespace Utils { anim.map(frame => frame.replace(fromColor, toColor)) return anim } + + export function checkAttack({ pirate, enemies }: { pirate: Pirate, enemies: Sprite[] }) {} }