diff --git a/src/update.ts b/src/update.ts index 27a96ff..8f0ba6a 100644 --- a/src/update.ts +++ b/src/update.ts @@ -32,7 +32,7 @@ export default function update(timestamp: number) { } } - // Make all buildings produce and consume. + // Make all buildings and units produce and consume. const productionPerMinute = getProduction(V); const storage = getStorage(V); @@ -41,6 +41,11 @@ export default function update(timestamp: number) { const outputPerMinute = productionPerMinute[resource]; const outputPerMilisecond = outputPerMinute / 60.0 / 1000.0; V.resources[resource] += outputPerMilisecond * delta; + }); + + // Make sure resources do not overflow. + Object.keys(productionPerMinute).forEach((key) => { + const resource = key as keyof ProductionType; if (V.resources[resource] > storage[resource]) { V.resources[resource] = storage[resource]; diff --git a/src/utils.ts b/src/utils.ts index 03ec5ae..2603802 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -24,14 +24,27 @@ export function getEmptyResources(): ProductionType { export function getProduction(villageState: VillageState): ProductionType { - return villageState.buildings + let production = getEmptyResources(); + + // Add buildings production and intake. + production = villageState.buildings .filter(b => b.behavior.production && b.level > 0) .map(b => { if (b.behavior.production) { return b.behavior.production(villageState, b); } }) - .reduce(_reduceResources, getEmptyResources()); + .reduce(_reduceResources, production); + + // Add units intake. + ['philosopher'].forEach(type => { + const unit = getUnitSource(type); + const unitCount = villageState.units[type] || 0; + const intakePerMinute = unit.behavior.foodIntakePerMinute * unitCount; + production.food -= intakePerMinute; + }); + + return production; }