Skip to content

Commit

Permalink
Overall death is MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 14, 2024
1 parent 68aa923 commit 9103343
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 31 deletions.
17 changes: 17 additions & 0 deletions allDead.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
44 changes: 34 additions & 10 deletions island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -60,14 +63,14 @@ 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)
} else if (player2.health > 0) {
retargetEnemies(player2)
} else {
// Everyone is dead!
whenAllDead()
}
}

Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -226,9 +229,9 @@ namespace Island {
})
}

function leaveIsland() {
function destroy() {
_island = undefined

player1.destroy()
player2.destroy()

Expand All @@ -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

Expand All @@ -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()
Expand Down
22 changes: 19 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
enum States {
Menu,
Overview,
Island
Island,
AllDead,
GameOver,
Win
}
enum SpriteKind {
PlayerAttackLeft,
Expand All @@ -45,6 +48,7 @@ const islands: Array<Map.Island> = [
risk: 0,
image: assets.image`island`,
segments: 2,
ownedBy: null
},
{
id: 1,
Expand All @@ -54,7 +58,8 @@ const islands: Array<Map.Island> = [
riches: 100,
risk: 0,
image: assets.image`island`,
segments: 6
segments: 6,
ownedBy: null
}
]

Expand Down Expand Up @@ -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()
Expand All @@ -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)
}

Expand Down
19 changes: 6 additions & 13 deletions map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -40,13 +42,12 @@ namespace Map {
}
})
waves.forEach(wave => wave.destroy())

if (cursor) {
cursor.destroy()
}
cursor.destroy()

music.stopAllSounds()

TreasureStats.hide()

_onSelectIsland(_islands[currentSelectedIslandIndex])
}

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pirate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"pirate.ts",
"militia.ts",
"menu.ts",
"treasureStats.ts"
"treasureStats.ts",
"allDead.ts"
],
"testFiles": [
"test.ts"
Expand Down
19 changes: 16 additions & 3 deletions treasureStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9103343

Please sign in to comment.