From 4768e22a39ff791552ae785f4884df21299d3fc3 Mon Sep 17 00:00:00 2001 From: Adrian Gaudebert Date: Tue, 5 Nov 2024 12:06:44 +0100 Subject: [PATCH] Kill units when food is negative. --- src/update.ts | 10 +++++++++- src/village.ts | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/update.ts b/src/update.ts index 2b975ac..e350430 100644 --- a/src/update.ts +++ b/src/update.ts @@ -1,6 +1,6 @@ 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 type { ProductionType } from './types'; @@ -43,6 +43,14 @@ export default function update(timestamp: number) { 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. Object.keys(productionPerMinute).forEach((key) => { const resource = key as keyof ProductionType; diff --git a/src/village.ts b/src/village.ts index d0a1fab..da9bc8b 100644 --- a/src/village.ts +++ b/src/village.ts @@ -111,7 +111,7 @@ function getInitialState() { } const newBuilding = createBuilding(type); newBuilding.tile = new Hex(x, y); - newBuilding.level = 10; // DEBUG + newBuilding.level = newBuilding.type === 'field' ? 1 : 10; state.outsideTiles[y][x] = newBuilding.id; state.buildings.push(newBuilding); });