From 8e06f1db0b64451d9f27c9db0195292fafe269a1 Mon Sep 17 00:00:00 2001 From: Chris Weed Date: Wed, 10 Jul 2024 22:55:48 -0500 Subject: [PATCH] Attacks work! --- island.ts | 49 +++++++++++++++++++++++++++++++++++++------------ militia.ts | 32 ++++++++++++++++++++++++++++++++ pirate.ts | 8 ++++++++ pxt.json | 3 ++- 4 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 militia.ts diff --git a/island.ts b/island.ts index 0ba3dc1..3380e0e 100644 --- a/island.ts +++ b/island.ts @@ -1,18 +1,47 @@ namespace Island { let player1: Pirate let player2: Pirate - let currentEnemies: Array + let currentEnemies: Array = [] let _onLeaveIsland: () => void - function onAttack({ pirate, direction }: { pirate: Pirate, direction: 'left' | 'right' }) { - console.log('Attacking! ' + pirate.controller.playerIndex + ':' + direction) + function onPirateAttack({ pirate, direction }: { pirate: Pirate, direction: 'left' | 'right' }) { + const dirPix = direction === 'left' ? -1 : 1 + // The hit zone is the pirate "sword" box: [center, right|left] and [top, bottom] + const hitXZone = [pirate.currentSprite.x, pirate.currentSprite.x + (13 * dirPix)] + // The sword is only near the top of the sprite, we don't kill with feet + const hitYZone = [pirate.currentSprite.y - 4, pirate.currentSprite.y + 2] + + // manually check each enemy to see if they overlap, also check for parry + currentEnemies.forEach((enemy) => { + if (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)) { + enemy.hit(1) + } else if (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)) { + enemy.hit(1) + } + }) + // clean any enemies off the array if they are dead + currentEnemies = currentEnemies.reduce((acc, enemy) => { + if (enemy.health > 0) { + acc.push(enemy) + } + return acc + }, []) } function leaveIsland() { player1.destroy() player2.destroy() + currentEnemies.map(enemy => enemy.destory()) + currentEnemies = [] + // Remove all listeners and clear the screen controller.player1.B.removeEventListener(ControllerButtonEvent.Pressed, leaveIsland) @@ -20,17 +49,11 @@ namespace Island { } export function init(island: Map.Island) { - player1 = new Pirate({ control: controller.player1, playerNumber: 0, onAttack }) - player2 = new Pirate({ control: controller.player2, playerNumber: 1, onAttack }) + player1 = new Pirate({ control: controller.player1, playerNumber: 0, onAttack: onPirateAttack }) + player2 = new Pirate({ control: controller.player2, playerNumber: 1, onAttack: onPirateAttack }) // Baddies - const enemy = sprites.create(assets.image`Enemy`, SpriteKind.Enemy) - enemy.x = 90 - enemy.y = 70 - - // sprites.onOverlap(SpriteKind.PlayerAttackRight, SpriteKind.Enemy, (pirate, enemy) => { - // console.log('overlap of stuff! ' + enemy.x + ':' + pirate.x) - // }) + currentEnemies.push(new Militia({ x: 50, y: 50 })) player1.place(10, 90) player2.place(10, 100) @@ -45,5 +68,7 @@ namespace Island { export function render() { player1.render() player2.render() + + currentEnemies.forEach(enemy => enemy.render()) } } diff --git a/militia.ts b/militia.ts new file mode 100644 index 0000000..943a6f7 --- /dev/null +++ b/militia.ts @@ -0,0 +1,32 @@ +class Militia { + sprite: Sprite + currentTarget: Pirate + public isParrying: boolean = false + public health: number = 1 + + constructor({ x, y }: { x: number, y: number }) { + this.sprite = sprites.create(assets.image`Enemy`) + this.place(x, y) + } + + public place(x: number, y: number) { + this.sprite.x = x + this.sprite.y = y + } + + public hit(damage: number) { + if (this.isParrying) return + this.health -= damage + + if (this.health <= 0) { + console.log('You killed me') + this.destory() + } + } + + public destory() { + this.sprite.destroy() + } + + public render() {} +} diff --git a/pirate.ts b/pirate.ts index 8aa2eea..4a09dc8 100644 --- a/pirate.ts +++ b/pirate.ts @@ -23,6 +23,7 @@ class Pirate { facing: 'left' | 'right' controller: controller.Controller isAttacking?: 'left' | 'right' + isParrying?: 'left' | 'right' _lastAttackTick: number = 0 _isAttackingTimeout: number // This action object is for registering event listeners @@ -90,6 +91,13 @@ class Pirate { this.currentSprite.z = this.currentSprite.y } + public hit(damage: number) { + if (this.isParrying) return + this.health -= damage + + // Add death! + } + parry() { console.log('parry ' + this.controller.playerIndex) } diff --git a/pxt.json b/pxt.json index 6694e7a..fb35f8b 100644 --- a/pxt.json +++ b/pxt.json @@ -16,7 +16,8 @@ "images.g.ts", "utils.ts", "island.ts", - "pirate.ts" + "pirate.ts", + "militia.ts" ], "testFiles": [ "test.ts"