bourgade/src/moves/upgradeBuilding.ts

30 lines
683 B
TypeScript
Raw Normal View History

import type { VillageState } from "../village";
export default function upgradeBuilding(V: VillageState, buildingId: number) {
const building = V.buildings.find(b => b.id === buildingId);
if (!building) {
return false;
}
const cost = building.cost(building.level);
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;
building.level++;
return true;
}