Skip to content

Commit

Permalink
Floaty boaty battles
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 17, 2024
1 parent 693668b commit 6092930
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
46 changes: 38 additions & 8 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum States {
Overview,
Island,
AllDead,
TreasureIsland,
Travel,
BoatBattle,
GameOver,
Expand All @@ -38,7 +39,7 @@ const version: string = 'v0.7'
const debugMode: boolean = false

let currentState: States
let currentIsland: Map.Island
let currentIsland: Map.Island = Map.islands[2]
let treasureSprite: Sprite

console.log('Yarrrgh! Beware of ye monsters in thee code!')
Expand Down Expand Up @@ -72,7 +73,7 @@ function switchState(state: States) {
currentState = state
switch (currentState) {
case States.Overview:
Map.init(Map.islands)
Map.init({ islands: Map.islands, currentIsland })
break;
case States.Island:
Island.init({ island: currentIsland })
Expand All @@ -90,7 +91,10 @@ function switchState(state: States) {
Win.init()
break;
case States.Travel:
Travel.init()
Travel.init({ targetIsland: currentIsland })
break;
case States.TreasureIsland:
TreasureIsland.init()
break;
case States.Menu:
default:
Expand All @@ -100,15 +104,26 @@ function switchState(state: States) {

function startGame(initialState?: States) {
Map.onSelectIsland((island: Map.Island) => {
currentIsland = island
switchState(States.Travel)
console.log('Current ' + currentIsland.id + ":" + island.id)
if (currentIsland && island.id !== currentIsland.id) {
currentIsland = island
switchState(States.Travel)
console.log('And now ' + currentIsland.id + ':' + island.id)
} else if (island.id === 0) {
currentIsland = island
// Re-entering your own island does nothing.
switchState(States.Overview)
} else {
currentIsland = island
// If you re-select the same island you don't floatyboaty
switchState(States.Island)
}
})
Map.onWin(() => {
switchState(States.Win)
})

Island.onLeaveIsland(() => {
currentIsland = undefined
switchState(States.Overview)
})
Island.onAllDead(() => {
Expand All @@ -120,11 +135,17 @@ function startGame(initialState?: States) {
})

BoatBattle.onWin(() => {
switchState(States.Overview)
if (currentIsland.id === 0) {
switchState(States.TreasureIsland)
} else {
switchState(States.Island)
}
})
BoatBattle.onAllDead(() => {
if (PirateLives.currentPirateCount <= 0) {
switchState(States.GameOver)
} else if (currentIsland.id === 0) {
switchState(States.TreasureIsland)
} else {
switchState(States.Overview)
}
Expand All @@ -135,7 +156,16 @@ function startGame(initialState?: States) {
})
Travel.onLandOnIsland(() => {
currentIsland = currentIsland ? currentIsland : Map.islands[0]
switchState(States.Island)
if (currentIsland.id === 0) {
// Our own Island
switchState(States.TreasureIsland)
} else {
switchState(States.Island)
}
})

TreasureIsland.onComplete(() => {
switchState(States.Overview)
})

Menu.onStartGame(() => {
Expand Down
15 changes: 2 additions & 13 deletions map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ namespace Map {
// We can't select the island too quickly after seeing this scene
if (control.millis() - _leftIslandTick < _selectIslandDelay) return

// Do special if we select our own island
if (_islands[currentSelectedIslandIndex].id === 0) {
TreasureStats.currentTreasure = {
onBoat: 0,
onIsland: TreasureStats.getTotal(),
inPocket: 0
}
TreasureStats.show({})
return
}

destroy()

_onSelectIsland(_islands[currentSelectedIslandIndex])
Expand All @@ -87,7 +76,7 @@ namespace Map {
renderIslandStats(islands[currentSelectedIslandIndex])
}

export function init(islands: Array<Island>) {
export function init({ islands, currentIsland }: { islands: Array<Island>, currentIsland: Island }) {
// Give us a second before allowing us to select an island
_leftIslandTick = control.millis()
_islands = islands
Expand Down Expand Up @@ -121,7 +110,7 @@ namespace Map {
controller.player1.A.addEventListener(ControllerButtonEvent.Pressed, selectIsland)

// Initial render of the cursor
renderCursor(islands[currentSelectedIslandIndex], cursor)
renderCursor(currentIsland ? currentIsland : islands[currentSelectedIslandIndex], cursor)
// And selected island
renderIslandStats(islands[currentSelectedIslandIndex])
// And the owner flags
Expand Down
3 changes: 2 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"boatBattle.ts",
"enemyPirate.ts",
"enemy.ts",
"travel.ts"
"travel.ts",
"treasureIsland.ts"
],
"testFiles": [
"test.ts"
Expand Down
14 changes: 11 additions & 3 deletions travel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ namespace Travel {
let _pirateAnimation: Image[]
let _islandAnimation: Image[]

export function init() {
export function init({ targetIsland }: { targetIsland: Map.Island }) {
// Probably should have done this better... oh well
const oddsOfBoatBattle = 80
const result = Math.min(TreasureStats.currentTreasure.onBoat / 300, oddsOfBoatBattle / 100) * 100
let result = Math.min(TreasureStats.currentTreasure.onBoat / 300, oddsOfBoatBattle / 100) * 100

console.log('Target island ' + targetIsland.id + ': ' + TreasureStats.currentTreasure.onBoat)

// You will ALWAYS get a pirate battle if you travel to your island with any amount of cash
if (TreasureStats.currentTreasure.onBoat > 150 && targetIsland.id === 0) {
result = 100
}

scene.setBackgroundColor(0)
scene.setBackgroundImage(assets.image`empty`)
Expand All @@ -32,7 +39,8 @@ namespace Travel {
} else {
pause(3000)
_textSprite.destroy()
_textSprite = textsprite.create('Arrgh, steal thee loot!')
const text = targetIsland.id === 0 ? 'Ye hid yer booty!' : 'Arrgh, steal thee loot!'
_textSprite = textsprite.create(text)
_textSprite.x = 80
_textSprite.y = 60
pause(2000)
Expand Down
22 changes: 22 additions & 0 deletions treasureIsland.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace TreasureIsland {
let _onComplete: () => void

export function init() {
// Do special if we select our own island
TreasureStats.currentTreasure = {
onBoat: 0,
onIsland: TreasureStats.getTotal(),
inPocket: 0
}
TreasureStats.show({})

destroy()
_onComplete()
}

export function onComplete(callback: () => void) {
_onComplete = callback
}

function destroy() {}
}
2 changes: 1 addition & 1 deletion treasureStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace TreasureStats {
let currentPosition: 'left' | 'center' = 'left'

export let currentTreasure: TreasureStat = {
onBoat: 0,
onBoat: 151,
onIsland: 0,
inPocket: 0
}
Expand Down

0 comments on commit 6092930

Please sign in to comment.