Skip to content

Commit

Permalink
Attacks work!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 11, 2024
1 parent 7ac42a5 commit 8e06f1d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
49 changes: 37 additions & 12 deletions island.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
namespace Island {
let player1: Pirate
let player2: Pirate
let currentEnemies: Array<Sprite>
let currentEnemies: Array<Militia> = []

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)

_onLeaveIsland()
}

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)
Expand All @@ -45,5 +68,7 @@ namespace Island {
export function render() {
player1.render()
player2.render()

currentEnemies.forEach(enemy => enemy.render())
}
}
32 changes: 32 additions & 0 deletions militia.ts
Original file line number Diff line number Diff line change
@@ -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() {}
}
8 changes: 8 additions & 0 deletions pirate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"images.g.ts",
"utils.ts",
"island.ts",
"pirate.ts"
"pirate.ts",
"militia.ts"
],
"testFiles": [
"test.ts"
Expand Down

0 comments on commit 8e06f1d

Please sign in to comment.