From 7d6bb18ee54185c512cc7ae31f00b3173f3e7719 Mon Sep 17 00:00:00 2001 From: Chris Weed Date: Wed, 17 Jul 2024 16:34:41 -0500 Subject: [PATCH] Boat battle now has treasure --- boatBattle.ts | 56 +++++++++++++++++++++++++++++++++------------------ main.ts | 4 ++-- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/boatBattle.ts b/boatBattle.ts index 6b53ae4..7334477 100644 --- a/boatBattle.ts +++ b/boatBattle.ts @@ -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; @@ -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() } } @@ -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 }) { @@ -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) @@ -114,7 +130,7 @@ namespace BoatBattle { PirateLives.updatePirateCount(-2) destroy() - _onAllDeadCallback() + _onLooseCallback() } if (!anyAlive) { diff --git a/main.ts b/main.ts index a4196ad..9f176f3 100644 --- a/main.ts +++ b/main.ts @@ -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) { @@ -179,4 +179,4 @@ function startGame(initialState?: States) { switchState(initialState ? initialState : States.Menu) } -startGame() \ No newline at end of file +startGame(States.BoatBattle) \ No newline at end of file