From 620bfb75814d0b8052ea58ad01b4c42615a7fed1 Mon Sep 17 00:00:00 2001 From: Adrian Gaudebert Date: Tue, 12 Nov 2024 16:12:17 +0100 Subject: [PATCH] Make the town hall reduce the construction time of buildings. --- src/data/buildings.ts | 1 + src/logic.ts | 14 ++++++++++++++ src/moves/build.ts | 3 ++- src/moves/upgradeBuilding.ts | 3 ++- src/types.ts | 1 + 5 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/logic.ts 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; }; }