Remove the queue of building construction and instead make buildings upgrade in parallel.

This commit is contained in:
Adrian 2024-11-05 12:42:39 +01:00
parent 4768e22a39
commit 3587196bd9
10 changed files with 39 additions and 96 deletions

View File

@ -7,8 +7,8 @@
export let building: BuildingType; export let building: BuildingType;
$: isUpgrading = $village.queue.find(q => q.id === building.id); $: isUpgrading = building.state.upgrade.isUpgrading;
$: canUpgrade = canPayBuildingCost($village, building); $: canUpgrade = !isUpgrading && canPayBuildingCost($village, building);
function upgradeBuilding() { function upgradeBuilding() {
@ -27,6 +27,11 @@
on:click|stopPropagation={ upgradeBuilding } on:click|stopPropagation={ upgradeBuilding }
> >
{ building.level } { building.level }
{ #if building.state.upgrade.isUpgrading }
<span>
({ Math.ceil(building.state.upgrade.remainingTime / 1000) })
</span>
{ /if }
</button> </button>
</p> </p>
</div> </div>
@ -43,6 +48,10 @@
font-size: 1.2em; font-size: 1.2em;
} }
.level span {
font-size: 0.9em;
}
.level.can-upgrade { .level.can-upgrade {
border-color: hsl(90, 99%, 36%); border-color: hsl(90, 99%, 36%);
} }
@ -50,8 +59,4 @@
.level.is-upgrading { .level.is-upgrading {
border-color: hsl(56, 99%, 43%); border-color: hsl(56, 99%, 43%);
} }
.level:hover.can-upgrade {
border-color: hsl(90, 99%, 36%);
}
</style> </style>

View File

@ -25,6 +25,11 @@ export function createBuilding(buildingType: string): BuildingType {
id: uid++, id: uid++,
level: 1, level: 1,
tile: new Hex(0, 0), tile: new Hex(0, 0),
state: {}, state: {
upgrade: {
isUpgrading: false,
remainingTime: 0,
}
},
}; };
} }

View File

@ -9,7 +9,6 @@
import BuildingCreator from "./BuildingCreator.svelte"; import BuildingCreator from "./BuildingCreator.svelte";
import BuildingPanel from "./BuildingPanel.svelte"; import BuildingPanel from "./BuildingPanel.svelte";
import Navigation from "./Navigation.svelte"; import Navigation from "./Navigation.svelte";
import Queue from "./Queue.svelte";
import Resources from "./Resources.svelte"; import Resources from "./Resources.svelte";
import Units from "./Units.svelte"; import Units from "./Units.svelte";
import Victory from "./Victory.svelte"; import Victory from "./Victory.svelte";
@ -47,9 +46,6 @@
<Outside /> <Outside />
{ /if } { /if }
</div> </div>
<div class="queue">
<Queue />
</div>
</section> </section>
<section class="overlay"> <section class="overlay">
<BuildingCreator /> <BuildingCreator />

View File

@ -1,50 +0,0 @@
<script lang="ts">
import { getBuilding } from "../utils";
import village from "../village";
$: queue = $village.queue.map(q => {
return {
...q,
building: getBuilding($village, q.id),
};
});
</script>
<section class="queue">
{ #each queue as item }
<div>
<p>{ item.building.name }</p>
<p class="time">{ Math.ceil(item.remainingTime / 1000) }</p>
</div>
{ /each }
</section>
<style>
.queue {
display: flex;
gap: 1em;
}
.queue div {
aspect-ratio: 1;
border: 0.4em solid grey;
border-radius: 100%;
height: 4em;
max-width: 4em;
position: relative;
}
.queue div p {
overflow-wrap: break-word;
}
.queue div .time {
background: hsl(0, 0%, 20%);
border-radius: 100%;
bottom: 0;
left: 50%;
padding: 0.2em 0.4em;
position: absolute;
translate: -50% 150%;
}
</style>

View File

@ -1,7 +1,5 @@
import buildings from "../data/buildings";
import { createBuilding, getBuildingSource } from "../create"; import { createBuilding, getBuildingSource } from "../create";
import type { Hex } from "../hexgrid"; import type { Hex } from "../hexgrid";
import { enqueueBuilding } from "../utils";
import { DEFAULT_TILE, type VillageState } from "../village"; 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); const newBuilding = createBuilding(buildingType);
newBuilding.tile = tile; newBuilding.tile = tile;
newBuilding.level = 0; newBuilding.level = 0;
newBuilding.state.upgrade.isUpgrading = true;
newBuilding.state.upgrade.remainingTime = 1000 * (newBuilding.level + 1);
V.buildings.push(newBuilding); V.buildings.push(newBuilding);
V.villageTiles[tile.y][tile.x] = newBuilding.id; V.villageTiles[tile.y][tile.x] = newBuilding.id;
enqueueBuilding(V, newBuilding);
return true; return true;
} }

View File

@ -1,4 +1,4 @@
import { canPayBuildingCost, enqueueBuilding, getBuildingUpgradeCost } from "../utils"; import { canPayBuildingCost, getBuildingUpgradeCost } from "../utils";
import type { VillageState } from "../village"; import type { VillageState } from "../village";
@ -8,6 +8,10 @@ export default function upgradeBuilding(V: VillageState, buildingId: number) {
return false; return false;
} }
if (building.state.upgrade.isUpgrading) {
return false;
}
if (!canPayBuildingCost(V, building)) { if (!canPayBuildingCost(V, building)) {
return false; return false;
} }
@ -18,7 +22,9 @@ export default function upgradeBuilding(V: VillageState, buildingId: number) {
V.resources.stone -= cost.stone; V.resources.stone -= cost.stone;
V.resources.iron -= cost.iron; V.resources.iron -= cost.iron;
V.resources.food -= cost.food; V.resources.food -= cost.food;
enqueueBuilding(V, building);
building.state.upgrade.isUpgrading = true;
building.state.upgrade.remainingTime = 1000 * (building.level + 1);
return true; return true;
} }

View File

@ -41,6 +41,10 @@ export interface BuildingType extends BuildingSource {
level: number; level: number;
tile: Hex; tile: Hex;
state: { state: {
upgrade: {
isUpgrading: boolean,
remainingTime: number;
}
recruitment?: { recruitment?: {
count: number; count: number;
elapsedTime: number; elapsedTime: number;

View File

@ -23,14 +23,13 @@ export default function update(timestamp: number) {
return produce(state, (V: VillageState) => { return produce(state, (V: VillageState) => {
// Advance building construction. // Advance building construction.
if (V.queue.length) { V.buildings.filter(b => b.state.upgrade.isUpgrading).forEach(b => {
V.queue[0].remainingTime -= delta; b.state.upgrade.remainingTime -= delta;
if (V.queue[0].remainingTime <= 0) { if (b.state.upgrade.remainingTime <= 0) {
const building = getBuilding(V, V.queue[0].id); b.level++;
building.level++; b.state.upgrade.isUpgrading = false;
V.queue.shift();
} }
} });
// Make all buildings and units produce and consume. // Make all buildings and units produce and consume.
const productionPerMinute = getProduction(V); const productionPerMinute = getProduction(V);

View File

@ -122,21 +122,8 @@ export function shuffle<T>(array: Array<T>): Array<T> {
} }
export function enqueueBuilding(V: VillageState, building: BuildingType) { export function getBuildingUpgradeCost(_V: VillageState, building: BuildingType): CostType {
const ongoingUpgrades = V.queue.filter(q => q.id === building.id); const level = building.level + 1;
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;
return building.cost(level); return building.cost(level);
} }

View File

@ -13,12 +13,6 @@ type Board = {
} }
interface QueuedBuilding {
id: number;
remainingTime: number;
}
export interface VillageState { export interface VillageState {
buildings: BuildingType[]; buildings: BuildingType[];
units: { units: {
@ -27,7 +21,6 @@ export interface VillageState {
resources: ResourcesType; resources: ResourcesType;
villageTiles: Board; villageTiles: Board;
outsideTiles: Board; outsideTiles: Board;
queue: QueuedBuilding[];
victory: boolean; victory: boolean;
} }
@ -83,7 +76,6 @@ function getInitialState() {
}, },
villageTiles: getInitialVillageBoard(), villageTiles: getInitialVillageBoard(),
outsideTiles: getInitialOutsideBoard(), outsideTiles: getInitialOutsideBoard(),
queue: [],
victory: false, victory: false,
}; };