diff --git a/src/buildings.ts b/src/buildings.ts index 3e7645c..7e3f9ff 100644 --- a/src/buildings.ts +++ b/src/buildings.ts @@ -4,6 +4,27 @@ import type { VillageState } from "./village"; export default { + 'townhall': { + name: 'Town Hall', + cost: (level: number) => { + return { + wood: level * 10, + stone: level * 10, + iron: level * 10, + food: 0, + }; + }, + behavior: { + storage: (_V: VillageState, _self: Building) => { + return { + 'wood': 100, + 'stone': 100, + 'iron': 100, + 'food': 100, + } + } + } + }, 'woodcutter': { name: 'Woodcutter', cost: (level: number) => { diff --git a/src/create.ts b/src/create.ts new file mode 100644 index 0000000..c2c92e1 --- /dev/null +++ b/src/create.ts @@ -0,0 +1,13 @@ +import type { Building, BuildingSource } from "./types"; + + +let uid = 0; + + +export function createBuilding(building: BuildingSource): Building { + return { + ...building, + level: 1, + id: uid++, + }; +} diff --git a/src/hud/BuildingCreator.svelte b/src/hud/BuildingCreator.svelte index ba02efc..55cdf25 100644 --- a/src/hud/BuildingCreator.svelte +++ b/src/hud/BuildingCreator.svelte @@ -6,7 +6,7 @@ function build(type: string) { - if (moves.createBuilding(type)) { + if (moves.build(type)) { close(); } } diff --git a/src/moves/createBuilding.ts b/src/moves/build.ts similarity index 69% rename from src/moves/createBuilding.ts rename to src/moves/build.ts index c8d3598..d34d21e 100644 --- a/src/moves/createBuilding.ts +++ b/src/moves/build.ts @@ -1,11 +1,9 @@ import buildings from "../buildings"; +import { createBuilding } from "../create"; import type { VillageState } from "../village"; -let uid = 0; - - -export default function createBuilding(V: VillageState, buildingType: keyof typeof buildings) { +export default function build(V: VillageState, buildingType: keyof typeof buildings) { const building = buildings[buildingType]; const cost = building.cost(1); @@ -23,12 +21,7 @@ export default function createBuilding(V: VillageState, buildingType: keyof type V.resources.iron -= cost.iron; V.resources.food -= cost.food; - const newBuilding = { - ...building, - level: 1, - id: uid++, - }; - V.buildings.push(newBuilding); + V.buildings.push(createBuilding(building)); return true; } diff --git a/src/moves/index.ts b/src/moves/index.ts index ff7bc8a..7521b68 100644 --- a/src/moves/index.ts +++ b/src/moves/index.ts @@ -1,7 +1,7 @@ import { produce } from 'immer'; import village, { type VillageState } from '../village'; -import createBuilding from './createBuilding'; +import build from './build'; import upgradeBuilding from './upgradeBuilding'; @@ -28,6 +28,6 @@ export function makeMove(move: (...args: any[]) => boolean) { export default { - createBuilding: makeMove(createBuilding), + build: makeMove(build), upgradeBuilding: makeMove(upgradeBuilding), }; diff --git a/src/types.ts b/src/types.ts index fb9e2fd..969a032 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,7 +11,6 @@ export type Production = Cost; export interface BuildingSource { name: string; - level: number; cost: (level: number) => Cost; behavior: { production?: Function; @@ -22,4 +21,5 @@ export interface BuildingSource { export interface Building extends BuildingSource { id: number; + level: number; } diff --git a/src/village.ts b/src/village.ts index 727404d..e5e45ee 100644 --- a/src/village.ts +++ b/src/village.ts @@ -1,5 +1,6 @@ import { writable } from "svelte/store"; import buildings from "./buildings"; +import { createBuilding } from "./create"; import type { Building } from "./types"; @@ -14,24 +15,10 @@ export interface VillageState { }; } -let uid = 0; const village = writable({ buildings: [ - { ...buildings.woodcutter, level: 1, id: uid++ }, - { ...buildings.woodcutter, level: 1, id: uid++ }, - { ...buildings.woodcutter, level: 1, id: uid++ }, - { ...buildings.woodcutter, level: 1, id: uid++ }, - { ...buildings.mine, level: 1, id: uid++ }, - { ...buildings.mine, level: 1, id: uid++ }, - { ...buildings.mine, level: 1, id: uid++ }, - { ...buildings.mine, level: 1, id: uid++ }, - { ...buildings.pit, level: 1, id: uid++ }, - { ...buildings.pit, level: 1, id: uid++ }, - { ...buildings.pit, level: 1, id: uid++ }, - { ...buildings.pit, level: 1, id: uid++ }, - { ...buildings.warehouse, level: 1, id: uid++ }, - { ...buildings.granary, level: 1, id: uid++ }, + createBuilding(buildings.townhall), ], resources: { wood: 100,