Skip to content

Commit

Permalink
Boat battle now has treasure
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 17, 2024
1 parent 79781a1 commit 7d6bb18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
56 changes: 36 additions & 20 deletions boatBattle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,41 @@ namespace BoatBattle {
let enemies: EnemyPirate[] = []
let player1: Pirate
let player2: Pirate
let treasure: Sprite

const player1StatLocation: number[] = [12, 10]
const player2StatLocation: number[] = [130, 10]
let _onWinCallback: () => void
let _onAllDeadCallback: () => void
let _onLooseCallback: () => void

const _timeAllowed = 10
let _currentTime = _timeAllowed
let _lastTick = 0
let _isDone = false
const _boundingBox: number[] = [0, 10, 160, 100]
const _enemyBoatBox: number[] = [0, 10, 160, 60]
const _ourBoatBox: number[] = [0, 90, 160, 100]

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

_currentTime = _timeAllowed
_lastTick = control.millis()

// Spawn the players
player1 = new Pirate({ control: controller.player1, playerNumber: 0, onAttack: onPirateAttack, onDie: onPirateDeath, boundaries: _boundingBox, statLocation: player1StatLocation })
player2 = new Pirate({ control: controller.player2, playerNumber: 1, onAttack: onPirateAttack, onDie: onPirateDeath, boundaries: _boundingBox, statLocation: player2StatLocation })
player1.sprite.x = Math.randomRange(_ourBoatBox[0], _ourBoatBox[2])
player1.sprite.y = Math.randomRange(_ourBoatBox[1], _ourBoatBox[3])
player2.sprite.x = Math.randomRange(_ourBoatBox[0], _ourBoatBox[2])
player2.sprite.y = Math.randomRange(_ourBoatBox[1], _ourBoatBox[3])

// Spawn the treasure!
treasure = sprites.create(assets.image`Chest`)
treasure.x = Math.randomRange(_ourBoatBox[0], _ourBoatBox[2])
treasure.y = Math.randomRange(_ourBoatBox[1], _ourBoatBox[3])

// Spawn the enemies!
// Based on the amount of treasure you have, more enemies will appear!
const totalTreasure = TreasureStats.getTotal()
let numberOfEnemies = 1
if (totalTreasure > 1000) {
numberOfEnemies = 10
numberOfEnemies = 8
} else {
// A log curve to determine the number of enemies
numberOfEnemies = 4 + Math.log(TreasureStats.getTotal() + 1) / Math.log(1000) * 8;
Expand All @@ -53,14 +58,7 @@ namespace BoatBattle {

enemies.forEach(enemy => enemy.render())

// Show timer (I know there's an extension but I want to try and roll my own)
if (control.millis() - _lastTick > 1000 && !_isDone) {
_lastTick = control.millis()
_currentTime -= 1
console.log('Time! ' + _currentTime)
}

if (_currentTime <= 0 && !_isDone && enemies.some((enemy) => enemy.health > 0)) {
if (!_isDone) {
checkIfOver()
}
}
Expand All @@ -77,8 +75,8 @@ namespace BoatBattle {
_onWinCallback = callback
}

export function onAllDead(callback: () => void) {
_onAllDeadCallback = callback
export function onLoose(callback: () => void) {
_onLooseCallback = callback
}

function onPirateAttack({ pirate }: { pirate: Pirate }) {
Expand All @@ -99,8 +97,26 @@ namespace BoatBattle {

function checkIfOver() {
const anyAlive = enemies.some((enemy) => enemy.health > 0)
const anyGotTreasure = enemies.some((enemy) => Utils.getDistance(enemy.sprite, treasure) < 5)
const allPlayersDead = player1.health <= 0 && player2.health <= 0
if (allPlayersDead || _currentTime <= 0) {
// if any enemy pirates are on top of the treasure, BOOM LOOSE!
if (anyGotTreasure) {
_isDone = true
pause(1500)

game.showLongText('Thee pirates stole ye booty!', DialogLayout.Center)

TreasureStats.currentTreasure = {
onBoat: 0,
onIsland: TreasureStats.currentTreasure.onIsland,
inPocket: 0
}

destroy()
_onLooseCallback()
}

if (allPlayersDead) {
_isDone = true
pause(1500)

Expand All @@ -114,7 +130,7 @@ namespace BoatBattle {
PirateLives.updatePirateCount(-2)

destroy()
_onAllDeadCallback()
_onLooseCallback()
}

if (!anyAlive) {
Expand Down
4 changes: 2 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function startGame(initialState?: States) {
switchState(States.Island)
}
})
BoatBattle.onAllDead(() => {
BoatBattle.onLoose(() => {
if (PirateLives.currentPirateCount <= 0) {
switchState(States.GameOver)
} else if (currentIsland.id === 0) {
Expand Down Expand Up @@ -179,4 +179,4 @@ function startGame(initialState?: States) {
switchState(initialState ? initialState : States.Menu)
}

startGame()
startGame(States.BoatBattle)

0 comments on commit 7d6bb18

Please sign in to comment.