Kill units when food is negative.

This commit is contained in:
Adrian 2024-11-05 12:06:44 +01:00
parent 33eb732162
commit 4768e22a39
2 changed files with 10 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import { produce } from 'immer'; import { produce } from 'immer';
import { getBuilding, getProduction, getStorage, getUnitSource } from './utils'; import { getBuilding, getProduction, getStorage, getUnitSource, shuffle } from './utils';
import village, { type VillageState } from "./village"; import village, { type VillageState } from "./village";
import type { ProductionType } from './types'; import type { ProductionType } from './types';
@ -43,6 +43,14 @@ export default function update(timestamp: number) {
V.resources[resource] += outputPerMilisecond * delta; V.resources[resource] += outputPerMilisecond * delta;
}); });
// Kill units if food is negative.
if (V.resources.food < 0) {
// Choose a unit type at random.
const type = shuffle(Object.keys(V.units).filter(t => V.units[t] > 0))[0];
// Kill one unit of that type.
V.units[type]--;
}
// Make sure resources do not overflow. // Make sure resources do not overflow.
Object.keys(productionPerMinute).forEach((key) => { Object.keys(productionPerMinute).forEach((key) => {
const resource = key as keyof ProductionType; const resource = key as keyof ProductionType;

View File

@ -111,7 +111,7 @@ function getInitialState() {
} }
const newBuilding = createBuilding(type); const newBuilding = createBuilding(type);
newBuilding.tile = new Hex(x, y); newBuilding.tile = new Hex(x, y);
newBuilding.level = 10; // DEBUG newBuilding.level = newBuilding.type === 'field' ? 1 : 10;
state.outsideTiles[y][x] = newBuilding.id; state.outsideTiles[y][x] = newBuilding.id;
state.buildings.push(newBuilding); state.buildings.push(newBuilding);
}); });