diff --git a/src/data/buildings.ts b/src/data/buildings.ts index 45b333a..66d1140 100644 --- a/src/data/buildings.ts +++ b/src/data/buildings.ts @@ -47,6 +47,7 @@ export default [ 'food': 100, } }, + constructionTimeReductionPerLevel: 0.025, }, }, { diff --git a/src/logic.ts b/src/logic.ts new file mode 100644 index 0000000..d77f587 --- /dev/null +++ b/src/logic.ts @@ -0,0 +1,14 @@ +import type { BuildingSource } from "./types"; +import type { VillageState } from "./village"; + + +export function getTimeToBuild(V: VillageState, building: BuildingSource, level: number): number { + const townhall = V.buildings.find(b => b.type === 'townhall'); + if (!townhall) { + throw new Error("Unable to find the Town hall"); + } + + const reduction = townhall.level * (townhall.behavior.constructionTimeReductionPerLevel || 0); + const baseBuildingTime = building.timeToBuild(level) * 1000; + return baseBuildingTime - (baseBuildingTime * reduction); +} diff --git a/src/moves/build.ts b/src/moves/build.ts index d4a7083..16d3d67 100644 --- a/src/moves/build.ts +++ b/src/moves/build.ts @@ -1,5 +1,6 @@ import { createBuilding, getBuildingSource } from "../create"; import type { Hex } from "../hexgrid"; +import { getTimeToBuild } from "../logic"; import { DEFAULT_TILE, type VillageState } from "../village"; @@ -29,7 +30,7 @@ export default function build(V: VillageState, buildingType: string, tile: Hex) newBuilding.tile = tile; newBuilding.level = 0; newBuilding.state.upgrade.isUpgrading = true; - newBuilding.state.upgrade.remainingTime = 1000 * newBuilding.timeToBuild(newBuilding.level + 1); + newBuilding.state.upgrade.remainingTime = getTimeToBuild(V, building, 1); V.buildings.push(newBuilding); V.villageTiles[tile.y][tile.x] = newBuilding.id; diff --git a/src/moves/upgradeBuilding.ts b/src/moves/upgradeBuilding.ts index 225563e..6fd945b 100644 --- a/src/moves/upgradeBuilding.ts +++ b/src/moves/upgradeBuilding.ts @@ -1,3 +1,4 @@ +import { getTimeToBuild } from "../logic"; import { canPayBuildingCost, getBuildingUpgradeCost } from "../utils"; import type { VillageState } from "../village"; @@ -28,7 +29,7 @@ export default function upgradeBuilding(V: VillageState, buildingId: number) { V.resources.food -= cost.food; building.state.upgrade.isUpgrading = true; - building.state.upgrade.remainingTime = 1000 * building.timeToBuild(building.level + 1); + building.state.upgrade.remainingTime = getTimeToBuild(V, building, building.level + 1); return true; } diff --git a/src/types.ts b/src/types.ts index 3545510..5a862b9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -34,6 +34,7 @@ export interface BuildingSource { type: string; recruitmentTime: Function; }; + constructionTimeReductionPerLevel?: number; }; }