Skip to content

Commit

Permalink
Title menu and stats refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikketer committed Jul 14, 2024
1 parent 0a34442 commit 1b64122
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 298 deletions.
12 changes: 6 additions & 6 deletions images.g.jres

Large diffs are not rendered by default.

303 changes: 66 additions & 237 deletions images.g.ts

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Island {
// [topleftX, topLeftY, bottomLeftX, bottomLeftY]
let _boundingBox: number[] = [0, 55, 160, 120]
let _island: Map.Island
let _onUpdateTreasure: (T: OnUpdateTreasureProps) => void = () => undefined
let _onUpdateTreasure: (T: TreasureStats.OnUpdateTreasureProps) => void = () => undefined
let _onLeaveIsland: () => void
let _dirtSpeckles: Sprite[] = []
let _treasureSprite: Sprite
Expand Down Expand Up @@ -111,7 +111,7 @@ namespace Island {
}

function drawBackground() {
scene.setBackgroundImage(assets.image`myImage`)
scene.setBackgroundImage(assets.image`Background`)
// Clear any exisitng speckles
_dirtSpeckles.forEach((speckle: Sprite) => {
speckle.destroy()
Expand Down Expand Up @@ -221,13 +221,15 @@ namespace Island {
_onLeaveIsland()
}

export function init({ island, onTreasureUpdate }: { island: Map.Island, onTreasureUpdate: (T: TreasureStat) => void }) {
export function init({ island, onTreasureUpdate }: { island: Map.Island, onTreasureUpdate: (T: TreasureStats.TreasureStat) => void }) {
_island = island
_onUpdateTreasure = onTreasureUpdate
isSegmentComplete = false
currentSegment = 0

scene.setBackgroundColor(8)
TreasureStats.show(['pocket'])

player1 = new Pirate({ control: controller.player1, playerNumber: 0, onAttack: onPirateAttack, topBoundary: _boundingBox[1], statLocation: player1StatLocation })
player2 = new Pirate({ control: controller.player2, playerNumber: 1, onAttack: onPirateAttack, topBoundary: _boundingBox[1], statLocation: player2StatLocation })

Expand Down
56 changes: 7 additions & 49 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,13 @@ enum SpriteKind {
PlayerAttackLeft,
PlayerAttackRight,
}
type TreasureStat = {
onBoat?: number
onIsland?: number
}
type OnUpdateTreasureProps = TreasureStat & {
pulledFromIsland ?: Map.Island['id']
}

const playerState = {
currentIsland: ''
}

let currentState: States
let currentIsland: Map.Island
let treasure: TreasureStat = {
onBoat: 0,
onIsland: 0
}
let treasureSprite: Sprite

const islands: Array<Map.Island> = [
Expand Down Expand Up @@ -82,56 +71,21 @@ game.onUpdate(() => {
}
})

function updateTreasure({ onBoat, onIsland, pulledFromIsland }: OnUpdateTreasureProps) {
console.log('Updating treasure ' + onBoat + ':' + pulledFromIsland)
if (pulledFromIsland != null) {
// Find the island
const island = islands.find(i => {
return i.id === pulledFromIsland
})

console.log("Found island " + JSON.stringify(island))
if (island) {
treasure.onBoat += island.riches
island.riches = 0
}
} else {
// If not from an island we assume from the boat
treasure.onIsland += treasure.onBoat
treasure.onBoat = 0
}

showTreasure()
}

function showTreasure() {
if (treasureSprite) {
treasureSprite.destroy()
}

treasureSprite = textsprite.create(treasure.onBoat + '', 1, 15)
treasureSprite.x = 80
treasureSprite.y = 8
treasureSprite.z = 100
}

function switchState(state: States) {
currentState = state
switch (currentState) {
case States.Overview:
Map.init(islands)
break;
case States.Island:
Island.init({ island: currentIsland, onTreasureUpdate: updateTreasure })
Island.init({ island: currentIsland, onTreasureUpdate: TreasureStats.updateTreasure })
break;
default:
console.log('Default State')
Menu.init()
}
}

function startGame() {
showTreasure()

Map.onSelectIsland((island: Map.Island) => {
currentIsland = island
switchState(States.Island)
Expand All @@ -142,7 +96,11 @@ function startGame() {
switchState(States.Overview)
})

switchState(States.Overview)
Menu.onStartGame(() => {
switchState(States.Overview)
})

switchState(States.Menu)
}

startGame()
5 changes: 3 additions & 2 deletions map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ namespace Map {
}

export function init(islands: Array<Island>) {
scene.setBackgroundColor(6)
// Give us a second before allowing us to select an island
_leftIslandTick = control.millis()
_islands = islands

scene.setBackgroundColor(6)
music.play(music.createSong(assets.song`We be Sailin`), music.PlaybackMode.LoopingInBackground)
TreasureStats.show(['island', 'boat'])

_islands = islands
// Cursor
cursor = sprites.create(assets.image`empty`)

Expand Down
25 changes: 25 additions & 0 deletions menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Menu {
let _callback: () => void
let _choiceSprite: Sprite

function startGame() {
scene.setBackgroundImage(assets.image`empty`)
controller.player1.A.removeEventListener(ControllerButtonEvent.Pressed, startGame)
_choiceSprite.destroy()

_callback()
}

export function init() {
scene.setBackgroundImage(assets.image`Splash Screen`)
_choiceSprite = textsprite.create('Arrrgh Be Greedy!', 0, 15)
_choiceSprite.x = 80
_choiceSprite.y = 100

controller.player1.A.addEventListener(ControllerButtonEvent.Pressed, startGame)
}

export function onStartGame(callback: () => void) {
_callback = callback
}
}
4 changes: 3 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"utils.ts",
"island.ts",
"pirate.ts",
"militia.ts"
"militia.ts",
"menu.ts",
"treasureStats.ts"
],
"testFiles": [
"test.ts"
Expand Down
58 changes: 58 additions & 0 deletions treasureStats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace TreasureStats {
export type TreasureStat = {
onBoat?: number
onIsland?: number
inPocket?: number
}
export type OnUpdateTreasureProps = TreasureStat & {
pulledFromIsland?: Map.Island['id']
}

let treasureSprite: Sprite
let currentDisplayCombo: Array<'island' | 'boat' | 'pocket'> = ['island']

const currentTreasure: TreasureStat = {
onBoat: 0,
onIsland: 0,
inPocket: 0
}

export function updateTreasure({ onBoat, onIsland, inPocket, pulledFromIsland }: OnUpdateTreasureProps) {
if (pulledFromIsland != null) {
// Find the island
const island = islands.find(i => {
return i.id === pulledFromIsland
})

if (island) {
currentTreasure.onBoat += island.riches
island.riches = 0
}
} else {
// If not from an island we assume from the boat
currentTreasure.onIsland += currentTreasure.onBoat
currentTreasure.onBoat = 0
}

show()
}

export function show(combination?: Array<'island' | 'boat' | 'pocket'>) {
currentDisplayCombo = combination ? combination : currentDisplayCombo

if (treasureSprite) {
treasureSprite.destroy()
}

treasureSprite = textsprite.create(currentTreasure.onBoat + '', 1, 15)
treasureSprite.x = 80
treasureSprite.y = 8
treasureSprite.z = 100
}

export function hide() {
if (treasureSprite) {
treasureSprite.destroy()
}
}
}

0 comments on commit 1b64122

Please sign in to comment.