From 3587196bd9729328f509ab0c69258ef506a3520f Mon Sep 17 00:00:00 2001
From: Adrian Gaudebert
Date: Tue, 5 Nov 2024 12:42:39 +0100
Subject: [PATCH] Remove the queue of building construction and instead make
buildings upgrade in parallel.
---
src/board/BuildingTile.svelte | 17 +++++++-----
src/create.ts | 7 ++++-
src/hud/Game.svelte | 4 ---
src/hud/Queue.svelte | 50 -----------------------------------
src/moves/build.ts | 5 ++--
src/moves/upgradeBuilding.ts | 10 +++++--
src/types.ts | 4 +++
src/update.ts | 13 +++++----
src/utils.ts | 17 ++----------
src/village.ts | 8 ------
10 files changed, 39 insertions(+), 96 deletions(-)
delete mode 100644 src/hud/Queue.svelte
diff --git a/src/board/BuildingTile.svelte b/src/board/BuildingTile.svelte
index 3c400f1..42144ed 100644
--- a/src/board/BuildingTile.svelte
+++ b/src/board/BuildingTile.svelte
@@ -7,8 +7,8 @@
export let building: BuildingType;
- $: isUpgrading = $village.queue.find(q => q.id === building.id);
- $: canUpgrade = canPayBuildingCost($village, building);
+ $: isUpgrading = building.state.upgrade.isUpgrading;
+ $: canUpgrade = !isUpgrading && canPayBuildingCost($village, building);
function upgradeBuilding() {
@@ -27,6 +27,11 @@
on:click|stopPropagation={ upgradeBuilding }
>
{ building.level }
+ { #if building.state.upgrade.isUpgrading }
+
+ ({ Math.ceil(building.state.upgrade.remainingTime / 1000) }’)
+
+ { /if }
@@ -43,6 +48,10 @@
font-size: 1.2em;
}
+ .level span {
+ font-size: 0.9em;
+ }
+
.level.can-upgrade {
border-color: hsl(90, 99%, 36%);
}
@@ -50,8 +59,4 @@
.level.is-upgrading {
border-color: hsl(56, 99%, 43%);
}
-
- .level:hover.can-upgrade {
- border-color: hsl(90, 99%, 36%);
- }
diff --git a/src/create.ts b/src/create.ts
index 7ba2388..a129721 100644
--- a/src/create.ts
+++ b/src/create.ts
@@ -25,6 +25,11 @@ export function createBuilding(buildingType: string): BuildingType {
id: uid++,
level: 1,
tile: new Hex(0, 0),
- state: {},
+ state: {
+ upgrade: {
+ isUpgrading: false,
+ remainingTime: 0,
+ }
+ },
};
}
diff --git a/src/hud/Game.svelte b/src/hud/Game.svelte
index 7e0f73e..bd9f493 100644
--- a/src/hud/Game.svelte
+++ b/src/hud/Game.svelte
@@ -9,7 +9,6 @@
import BuildingCreator from "./BuildingCreator.svelte";
import BuildingPanel from "./BuildingPanel.svelte";
import Navigation from "./Navigation.svelte";
- import Queue from "./Queue.svelte";
import Resources from "./Resources.svelte";
import Units from "./Units.svelte";
import Victory from "./Victory.svelte";
@@ -47,9 +46,6 @@
{ /if }
-
-
-
diff --git a/src/hud/Queue.svelte b/src/hud/Queue.svelte
deleted file mode 100644
index 35e3871..0000000
--- a/src/hud/Queue.svelte
+++ /dev/null
@@ -1,50 +0,0 @@
-
-
-
- { #each queue as item }
-
-
{ item.building.name }
-
{ Math.ceil(item.remainingTime / 1000) }’
-
- { /each }
-
-
-
diff --git a/src/moves/build.ts b/src/moves/build.ts
index 8818359..bef574f 100644
--- a/src/moves/build.ts
+++ b/src/moves/build.ts
@@ -1,7 +1,5 @@
-import buildings from "../data/buildings";
import { createBuilding, getBuildingSource } from "../create";
import type { Hex } from "../hexgrid";
-import { enqueueBuilding } from "../utils";
import { DEFAULT_TILE, type VillageState } from "../village";
@@ -30,10 +28,11 @@ export default function build(V: VillageState, buildingType: string, tile: Hex)
const newBuilding = createBuilding(buildingType);
newBuilding.tile = tile;
newBuilding.level = 0;
+ newBuilding.state.upgrade.isUpgrading = true;
+ newBuilding.state.upgrade.remainingTime = 1000 * (newBuilding.level + 1);
V.buildings.push(newBuilding);
V.villageTiles[tile.y][tile.x] = newBuilding.id;
- enqueueBuilding(V, newBuilding);
return true;
}
diff --git a/src/moves/upgradeBuilding.ts b/src/moves/upgradeBuilding.ts
index 691384a..2264d1a 100644
--- a/src/moves/upgradeBuilding.ts
+++ b/src/moves/upgradeBuilding.ts
@@ -1,4 +1,4 @@
-import { canPayBuildingCost, enqueueBuilding, getBuildingUpgradeCost } from "../utils";
+import { canPayBuildingCost, getBuildingUpgradeCost } from "../utils";
import type { VillageState } from "../village";
@@ -8,6 +8,10 @@ export default function upgradeBuilding(V: VillageState, buildingId: number) {
return false;
}
+ if (building.state.upgrade.isUpgrading) {
+ return false;
+ }
+
if (!canPayBuildingCost(V, building)) {
return false;
}
@@ -18,7 +22,9 @@ export default function upgradeBuilding(V: VillageState, buildingId: number) {
V.resources.stone -= cost.stone;
V.resources.iron -= cost.iron;
V.resources.food -= cost.food;
- enqueueBuilding(V, building);
+
+ building.state.upgrade.isUpgrading = true;
+ building.state.upgrade.remainingTime = 1000 * (building.level + 1);
return true;
}
diff --git a/src/types.ts b/src/types.ts
index 8330cef..30da5f4 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -41,6 +41,10 @@ export interface BuildingType extends BuildingSource {
level: number;
tile: Hex;
state: {
+ upgrade: {
+ isUpgrading: boolean,
+ remainingTime: number;
+ }
recruitment?: {
count: number;
elapsedTime: number;
diff --git a/src/update.ts b/src/update.ts
index e350430..137f6a7 100644
--- a/src/update.ts
+++ b/src/update.ts
@@ -23,14 +23,13 @@ export default function update(timestamp: number) {
return produce(state, (V: VillageState) => {
// Advance building construction.
- if (V.queue.length) {
- V.queue[0].remainingTime -= delta;
- if (V.queue[0].remainingTime <= 0) {
- const building = getBuilding(V, V.queue[0].id);
- building.level++;
- V.queue.shift();
+ V.buildings.filter(b => b.state.upgrade.isUpgrading).forEach(b => {
+ b.state.upgrade.remainingTime -= delta;
+ if (b.state.upgrade.remainingTime <= 0) {
+ b.level++;
+ b.state.upgrade.isUpgrading = false;
}
- }
+ });
// Make all buildings and units produce and consume.
const productionPerMinute = getProduction(V);
diff --git a/src/utils.ts b/src/utils.ts
index af511d0..e25d368 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -122,21 +122,8 @@ export function shuffle(array: Array): Array {
}
-export function enqueueBuilding(V: VillageState, building: BuildingType) {
- const ongoingUpgrades = V.queue.filter(q => q.id === building.id);
- const level = building.level + 1 + ongoingUpgrades.length;
- const remainingTime = 1000 * level;
-
- V.queue.push({
- id: building.id,
- remainingTime,
- });
-}
-
-
-export function getBuildingUpgradeCost(V: VillageState, building: BuildingType): CostType {
- const ongoingUpgrades = V.queue.filter(q => q.id === building.id);
- const level = building.level + ongoingUpgrades.length + 1;
+export function getBuildingUpgradeCost(_V: VillageState, building: BuildingType): CostType {
+ const level = building.level + 1;
return building.cost(level);
}
diff --git a/src/village.ts b/src/village.ts
index da9bc8b..269eb2d 100644
--- a/src/village.ts
+++ b/src/village.ts
@@ -13,12 +13,6 @@ type Board = {
}
-interface QueuedBuilding {
- id: number;
- remainingTime: number;
-}
-
-
export interface VillageState {
buildings: BuildingType[];
units: {
@@ -27,7 +21,6 @@ export interface VillageState {
resources: ResourcesType;
villageTiles: Board;
outsideTiles: Board;
- queue: QueuedBuilding[];
victory: boolean;
}
@@ -83,7 +76,6 @@ function getInitialState() {
},
villageTiles: getInitialVillageBoard(),
outsideTiles: getInitialOutsideBoard(),
- queue: [],
victory: false,
};