From dd50d9b80dc7202054d6d5fa954684bb0353be36 Mon Sep 17 00:00:00 2001 From: Adrian Gaudebert Date: Mon, 4 Nov 2024 18:43:03 +0100 Subject: [PATCH] Add a victory screen. --- src/hud/Game.svelte | 2 ++ src/hud/Victory.svelte | 30 ++++++++++++++++++++++++++++++ src/update.ts | 11 ++++++++++- src/village.ts | 12 +++++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/hud/Victory.svelte diff --git a/src/hud/Game.svelte b/src/hud/Game.svelte index 7370fcb..7e0f73e 100644 --- a/src/hud/Game.svelte +++ b/src/hud/Game.svelte @@ -12,6 +12,7 @@ import Queue from "./Queue.svelte"; import Resources from "./Resources.svelte"; import Units from "./Units.svelte"; + import Victory from "./Victory.svelte"; onMount(() => { @@ -53,6 +54,7 @@
+
diff --git a/src/update.ts b/src/update.ts index cf557ed..13c4fce 100644 --- a/src/update.ts +++ b/src/update.ts @@ -17,6 +17,10 @@ export default function update(timestamp: number) { const delta = timestamp - lastFrame; village.update(state => { + if (state.victory) { + return state; + } + return produce(state, (V: VillageState) => { // Advance building construction. if (V.queue.length) { @@ -71,7 +75,12 @@ export default function update(timestamp: number) { const philosopher = getUnitSource('philosopher'); const outputPerMinute = philosopher.behavior.culturePerMinute; const outputPerMilisecond = outputPerMinute / 60.0 / 1000.0; - V.resources.culture += outputPerMilisecond * delta * V.units.philosopher; + V.resources.culture += outputPerMilisecond * delta * (V.units.philosopher || 0); + + // Check if the game is won. + if (V.resources.culture >= 2) { + V.victory = true; + } return V; }); diff --git a/src/village.ts b/src/village.ts index dc46c50..d0a1fab 100644 --- a/src/village.ts +++ b/src/village.ts @@ -28,6 +28,7 @@ export interface VillageState { villageTiles: Board; outsideTiles: Board; queue: QueuedBuilding[]; + victory: boolean; } @@ -83,6 +84,7 @@ function getInitialState() { villageTiles: getInitialVillageBoard(), outsideTiles: getInitialOutsideBoard(), queue: [], + victory: false, }; // Create the Town hall. @@ -122,4 +124,12 @@ function getInitialState() { const village = writable(getInitialState()); -export default village; +function reset() { + village.set(getInitialState()); +} + + +export default { + ...village, + reset, +};