Add a victory screen.

This commit is contained in:
Adrian 2024-11-04 18:43:03 +01:00
parent 04a37732d9
commit dd50d9b80d
4 changed files with 53 additions and 2 deletions

View File

@ -12,6 +12,7 @@
import Queue from "./Queue.svelte"; import Queue from "./Queue.svelte";
import Resources from "./Resources.svelte"; import Resources from "./Resources.svelte";
import Units from "./Units.svelte"; import Units from "./Units.svelte";
import Victory from "./Victory.svelte";
onMount(() => { onMount(() => {
@ -53,6 +54,7 @@
<section class="overlay"> <section class="overlay">
<BuildingCreator /> <BuildingCreator />
<BuildingPanel /> <BuildingPanel />
<Victory />
</section> </section>
<style> <style>

30
src/hud/Victory.svelte Normal file
View File

@ -0,0 +1,30 @@
<script lang="ts">
import village from "../village";
function restart() {
village.reset();
}
</script>
{ #if $village.victory }
<section>
<h1>Victory!</h1>
<div>
<button on:click={ restart }>Play again</button>
</div>
</section>
{ /if }
<style>
section {
background-color: hsl(0, 0%, 10%, 0.9);
display: grid;
place-items: center;
height: 100vh;
left: 0;
position: absolute;
top: 0;
width: 100vw;
}
</style>

View File

@ -17,6 +17,10 @@ export default function update(timestamp: number) {
const delta = timestamp - lastFrame; const delta = timestamp - lastFrame;
village.update(state => { village.update(state => {
if (state.victory) {
return state;
}
return produce(state, (V: VillageState) => { return produce(state, (V: VillageState) => {
// Advance building construction. // Advance building construction.
if (V.queue.length) { if (V.queue.length) {
@ -71,7 +75,12 @@ export default function update(timestamp: number) {
const philosopher = getUnitSource('philosopher'); const philosopher = getUnitSource('philosopher');
const outputPerMinute = philosopher.behavior.culturePerMinute; const outputPerMinute = philosopher.behavior.culturePerMinute;
const outputPerMilisecond = outputPerMinute / 60.0 / 1000.0; 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; return V;
}); });

View File

@ -28,6 +28,7 @@ export interface VillageState {
villageTiles: Board; villageTiles: Board;
outsideTiles: Board; outsideTiles: Board;
queue: QueuedBuilding[]; queue: QueuedBuilding[];
victory: boolean;
} }
@ -83,6 +84,7 @@ function getInitialState() {
villageTiles: getInitialVillageBoard(), villageTiles: getInitialVillageBoard(),
outsideTiles: getInitialOutsideBoard(), outsideTiles: getInitialOutsideBoard(),
queue: [], queue: [],
victory: false,
}; };
// Create the Town hall. // Create the Town hall.
@ -122,4 +124,12 @@ function getInitialState() {
const village = writable<VillageState>(getInitialState()); const village = writable<VillageState>(getInitialState());
export default village; function reset() {
village.set(getInitialState());
}
export default {
...village,
reset,
};