Make all units consume food.
This commit is contained in:
parent
1885463132
commit
ef43be402f
@ -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];
|
||||
|
17
src/utils.ts
17
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;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user