From 80867d6ccf171d7a8044bc3079c327eb6c8f6781 Mon Sep 17 00:00:00 2001 From: Adrian Gaudebert Date: Tue, 22 Oct 2024 18:27:11 +0200 Subject: [PATCH] Add a move and button to create a new building. --- src/App.svelte | 9 +++++ src/hud/BuildingCreator.svelte | 69 ++++++++++++++++++++++++++++++++++ src/moves/createBuilding.ts | 34 +++++++++++++++++ src/moves/index.ts | 6 ++- 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/hud/BuildingCreator.svelte create mode 100644 src/moves/createBuilding.ts diff --git a/src/App.svelte b/src/App.svelte index 4802de8..1dce74a 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -5,6 +5,7 @@ import moves from "./moves"; import update from "./update"; import village from "./village"; + import BuildingCreator from "./hud/BuildingCreator.svelte"; onMount(() => { @@ -25,12 +26,20 @@ function upgradeBuilding(id: number) { moves.upgradeBuilding(id); } + + let showBuildingCreator = false; +{ #if showBuildingCreator } + { showBuildingCreator = false } } /> +{ /if }
+
+ +
{ #each $village.buildings as building }
diff --git a/src/hud/BuildingCreator.svelte b/src/hud/BuildingCreator.svelte new file mode 100644 index 0000000..ba02efc --- /dev/null +++ b/src/hud/BuildingCreator.svelte @@ -0,0 +1,69 @@ + + +
+
+
+

Build

+ + + +
+
+ { #each Object.entries(buildings) as [type, building] } +
+

{ building.name }

+ +
+ { /each } +
+
+
+ + diff --git a/src/moves/createBuilding.ts b/src/moves/createBuilding.ts new file mode 100644 index 0000000..c8d3598 --- /dev/null +++ b/src/moves/createBuilding.ts @@ -0,0 +1,34 @@ +import buildings from "../buildings"; +import type { VillageState } from "../village"; + + +let uid = 0; + + +export default function createBuilding(V: VillageState, buildingType: keyof typeof buildings) { + const building = buildings[buildingType]; + const cost = building.cost(1); + + if ( + cost.wood > V.resources.wood + || cost.stone > V.resources.stone + || cost.iron > V.resources.iron + || cost.food > V.resources.food + ) { + return false; + } + + V.resources.wood -= cost.wood; + V.resources.stone -= cost.stone; + V.resources.iron -= cost.iron; + V.resources.food -= cost.food; + + const newBuilding = { + ...building, + level: 1, + id: uid++, + }; + V.buildings.push(newBuilding); + + return true; +} diff --git a/src/moves/index.ts b/src/moves/index.ts index f5f3061..ff7bc8a 100644 --- a/src/moves/index.ts +++ b/src/moves/index.ts @@ -1,6 +1,7 @@ import { produce } from 'immer'; import village, { type VillageState } from '../village'; +import createBuilding from './createBuilding'; import upgradeBuilding from './upgradeBuilding'; @@ -16,8 +17,8 @@ export function makeMove(move: (...args: any[]) => boolean) { // That updates the game state. village.update(state => { // With an immutable state. - return produce(state, (G: VillageState) => { - result = move(G, ...args); + return produce(state, (V: VillageState) => { + result = move(V, ...args); }); }); @@ -27,5 +28,6 @@ export function makeMove(move: (...args: any[]) => boolean) { export default { + createBuilding: makeMove(createBuilding), upgradeBuilding: makeMove(upgradeBuilding), };