From 910334368f9b7059e9c2486bb358135c7e006692 Mon Sep 17 00:00:00 2001 From: Chris Weed Date: Sun, 14 Jul 2024 12:23:19 -0500 Subject: [PATCH] Overall death is MVP --- allDead.ts | 17 +++++++++++++++++ island.ts | 44 ++++++++++++++++++++++++++++++++++---------- main.ts | 22 +++++++++++++++++++--- map.ts | 19 ++++++------------- pirate.ts | 2 +- pxt.json | 3 ++- treasureStats.ts | 19 ++++++++++++++++--- 7 files changed, 95 insertions(+), 31 deletions(-) create mode 100644 allDead.ts diff --git a/allDead.ts b/allDead.ts new file mode 100644 index 0000000..ab73532 --- /dev/null +++ b/allDead.ts @@ -0,0 +1,17 @@ +namespace AllDead { + let _onRevive: () => void + + export function init() { + console.log("All dead!") + scene.setBackgroundImage(assets.image`empty`) + scene.setBackgroundColor(0) + + game.showLongText('Yarrgh, ye be swimmin\' with thee fishes! Thar militia have raided ye ship!', DialogLayout.Center) + console.log('Dismissed') + _onRevive() + } + + export function onRevive(callback: () => void) { + _onRevive = callback + } +} diff --git a/island.ts b/island.ts index e29e813..4b24a31 100644 --- a/island.ts +++ b/island.ts @@ -20,6 +20,9 @@ namespace Island { let _island: Map.Island let _onUpdateTreasure: (T: TreasureStats.OnUpdateTreasureProps) => void = () => undefined let _onLeaveIsland: () => void + let _onAllDead: () => void + // So we can't trigger "all dead" more than once (happens if multiple shots hit a dead guy) + let _allDead: boolean = false let _dirtSpeckles: Sprite[] = [] let _treasureSprite: Sprite let _treasureOpened: boolean = false @@ -60,7 +63,6 @@ namespace Island { } function onPirateDeath({ pirate }: { pirate: Pirate}) { - console.log('pirate died! ' + player2.health) // If there's still a living pirate, re-target all enemies to the other pirate if (player1.health > 0) { retargetEnemies(player1) @@ -68,6 +70,7 @@ namespace Island { retargetEnemies(player2) } else { // Everyone is dead! + whenAllDead() } } @@ -182,7 +185,7 @@ namespace Island { ) setTimeout(() => { // Add the islands riches to the boat! - _onUpdateTreasure({ onBoat: _island.riches, pulledFromIsland: _island.id }) + TreasureStats.updateTreasure({ onBoat: _island.riches, pulledFromIsland: _island.id }) }, openTreasureAnimation.length * 100) setTimeout(() => { @@ -226,9 +229,9 @@ namespace Island { }) } - function leaveIsland() { + function destroy() { _island = undefined - + player1.destroy() player2.destroy() @@ -247,15 +250,34 @@ namespace Island { music.stopAllSounds() - // Remove all listeners and clear the screen - controller.player1.B.removeEventListener(ControllerButtonEvent.Pressed, leaveIsland) + TreasureStats.hide() + } + + function whenAllDead() { + console.log('All dead! ' + _allDead) + if (!_allDead) { + _allDead = true + // You lose all your inPocket AND boat coin! + TreasureStats.updateTreasure({ inPocket: 0, onBoat: 0 }) + + // Take a breather + pause(2000) + + console.log("And show dead screen") + destroy() + _onAllDead() + } + } + + function leaveIsland() { + destroy() _onLeaveIsland() } - export function init({ island, onTreasureUpdate }: { island: Map.Island, onTreasureUpdate: (T: TreasureStats.TreasureStat) => void }) { + export function init({ island }: { island: Map.Island }) { _island = island - _onUpdateTreasure = onTreasureUpdate + _allDead = false isSegmentComplete = false currentSegment = 0 @@ -273,14 +295,16 @@ namespace Island { player1.place(10, 90) player2.place(10, 100) - - controller.player1.B.addEventListener(ControllerButtonEvent.Pressed, leaveIsland) } export function onLeaveIsland(callback: () => void) { _onLeaveIsland = callback } + export function onAllDead(callback: () => void) { + _onAllDead = callback + } + export function render() { player1.render() player2.render() diff --git a/main.ts b/main.ts index 4dbcd52..f4a03cf 100644 --- a/main.ts +++ b/main.ts @@ -18,7 +18,10 @@ enum States { Menu, Overview, - Island + Island, + AllDead, + GameOver, + Win } enum SpriteKind { PlayerAttackLeft, @@ -45,6 +48,7 @@ const islands: Array = [ risk: 0, image: assets.image`island`, segments: 2, + ownedBy: null }, { id: 1, @@ -54,7 +58,8 @@ const islands: Array = [ riches: 100, risk: 0, image: assets.image`island`, - segments: 6 + segments: 6, + ownedBy: null } ] @@ -89,7 +94,10 @@ function switchState(state: States) { Map.init(islands) break; case States.Island: - Island.init({ island: currentIsland, onTreasureUpdate: TreasureStats.updateTreasure }) + Island.init({ island: currentIsland }) + break; + case States.AllDead: + AllDead.init() break; default: Menu.init() @@ -106,11 +114,19 @@ function startGame() { currentIsland = undefined switchState(States.Overview) }) + Island.onAllDead(() => { + // Keep the current island + switchState(States.AllDead) + }) Menu.onStartGame(() => { switchState(States.Overview) }) + AllDead.onRevive(() => { + switchState(States.Overview) + }) + switchState(States.Menu) } diff --git a/map.ts b/map.ts index 8288317..0415f94 100644 --- a/map.ts +++ b/map.ts @@ -13,6 +13,8 @@ namespace Map { // The number of screens in the level // Development time limits = random scenes :) segments: number + // Island is owned by us them or no one + ownedBy?: 'players' | 'scallywags' } // const weBeSailinSong: Buffer = assets.song`We be Sailin` @@ -40,13 +42,12 @@ namespace Map { } }) waves.forEach(wave => wave.destroy()) - - if (cursor) { - cursor.destroy() - } + cursor.destroy() music.stopAllSounds() + TreasureStats.hide() + _onSelectIsland(_islands[currentSelectedIslandIndex]) } @@ -89,21 +90,13 @@ namespace Map { return sprite }) - // Rendering the islands - islands.forEach(island => { - // const sprite = sprites.create(island.image) - // sprite.x = island.x - // sprite.y = island.y - // island.sprite = sprite - }) - // Keyboard inputs controller.player1.left.addEventListener(ControllerButtonEvent.Pressed, moveCursorLeft) controller.player1.right.addEventListener(ControllerButtonEvent.Pressed, moveCursorRight) controller.player1.A.addEventListener(ControllerButtonEvent.Pressed, selectIsland) // Initial render of the cursor - renderCursor(islands[0], cursor) + renderCursor(currentIsland ? currentIsland : islands[0], cursor) } export function onSelectIsland(callback: (island: Island) => void) { diff --git a/pirate.ts b/pirate.ts index a8fbd3b..4ae4e49 100644 --- a/pirate.ts +++ b/pirate.ts @@ -58,7 +58,7 @@ class Pirate { topBoundary: number, statLocation: number[] }) { - this.health = 3 + this.health = 1 this.facing = 'right' this._topBoundary = topBoundary this._statLocation = statLocation diff --git a/pxt.json b/pxt.json index 5fea8a9..dcb53db 100644 --- a/pxt.json +++ b/pxt.json @@ -20,7 +20,8 @@ "pirate.ts", "militia.ts", "menu.ts", - "treasureStats.ts" + "treasureStats.ts", + "allDead.ts" ], "testFiles": [ "test.ts" diff --git a/treasureStats.ts b/treasureStats.ts index 6424050..12ec6ab 100644 --- a/treasureStats.ts +++ b/treasureStats.ts @@ -34,9 +34,22 @@ namespace TreasureStats { island.riches = 0 } } else { - // If not from an island we assume from the boat - currentTreasure.onIsland += currentTreasure.onBoat - currentTreasure.onBoat = 0 + // If any of the values are actually 0, then we set it to 0 + // otherwise we increment (kind of strange? but I'm lazy and tired) + if (onBoat === 0) { + currentTreasure.onBoat = 0 + } + if (onIsland === 0) { + currentTreasure.onIsland = 0 + } + if (inPocket === 0) { + currentTreasure.inPocket = 0 + } + + // Now increment + currentTreasure.onBoat = currentTreasure.onBoat + (onBoat ? onBoat : 0) + currentTreasure.onIsland = currentTreasure.onIsland + (onIsland ? onIsland : 0) + currentTreasure.inPocket = currentTreasure.inPocket + (inPocket ? inPocket : 0) } show()