Add buildings to a construction queue when building or upgrading them.
This commit is contained in:
parent
21952c2024
commit
fe34ffee8d
@ -26,7 +26,9 @@
|
|||||||
{ :else if $village.outsideTiles[y][x] === VILLAGE_TILE }
|
{ :else if $village.outsideTiles[y][x] === VILLAGE_TILE }
|
||||||
<Tile
|
<Tile
|
||||||
onTileClick={ () => gameTab.set('village') }
|
onTileClick={ () => gameTab.set('village') }
|
||||||
/>
|
>
|
||||||
|
<img src="/img/icons/village.svg" alt="">
|
||||||
|
</Tile>
|
||||||
{ /if }
|
{ /if }
|
||||||
{ /each }
|
{ /each }
|
||||||
</div>
|
</div>
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import type { Building, BuildingSource } from "./types";
|
import type { Building } from "./types";
|
||||||
import { getEmptyResources } from "./utils";
|
import { getEmptyResources } from "./utils";
|
||||||
import type { VillageState } from "./village";
|
import type { VillageState } from "./village";
|
||||||
|
|
||||||
|
|
||||||
const buildings = [
|
export default [
|
||||||
{
|
{
|
||||||
type: 'townhall',
|
type: 'townhall',
|
||||||
name: 'Town Hall',
|
name: 'Town Hall',
|
||||||
@ -172,4 +172,3 @@ const buildings = [
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
export default buildings;
|
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
|
|
||||||
|
import Outside from "../board/Outside.svelte";
|
||||||
import Village from "../board/Village.svelte";
|
import Village from "../board/Village.svelte";
|
||||||
|
import gameTab from "../stores/gameTab";
|
||||||
|
import type { GameTab } from "../types";
|
||||||
import update from "../update";
|
import update from "../update";
|
||||||
import BuildingCreator from "./BuildingCreator.svelte";
|
import BuildingCreator from "./BuildingCreator.svelte";
|
||||||
import Resources from "./Resources.svelte";
|
|
||||||
import BuildingPanel from "./BuildingPanel.svelte";
|
import BuildingPanel from "./BuildingPanel.svelte";
|
||||||
import Outside from "../board/Outside.svelte";
|
|
||||||
import Navigation from "./Navigation.svelte";
|
import Navigation from "./Navigation.svelte";
|
||||||
import type { GameTab } from "../types";
|
import Resources from "./Resources.svelte";
|
||||||
import gameTab from "../stores/gameTab";
|
import Queue from "./Queue.svelte";
|
||||||
|
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
@ -36,13 +37,16 @@
|
|||||||
<Resources />
|
<Resources />
|
||||||
<Navigation { setTab } />
|
<Navigation { setTab } />
|
||||||
</header>
|
</header>
|
||||||
<div class="">
|
<div class="board">
|
||||||
{ #if $gameTab === 'village' }
|
{ #if $gameTab === 'village' }
|
||||||
<Village />
|
<Village />
|
||||||
{ :else if $gameTab === 'resources' }
|
{ :else if $gameTab === 'resources' }
|
||||||
<Outside />
|
<Outside />
|
||||||
{ /if }
|
{ /if }
|
||||||
</div>
|
</div>
|
||||||
|
<div class="queue">
|
||||||
|
<Queue />
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<section class="overlay">
|
<section class="overlay">
|
||||||
<BuildingCreator />
|
<BuildingCreator />
|
||||||
@ -50,6 +54,11 @@
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-around;
|
||||||
|
}
|
||||||
|
|
||||||
.overlay {
|
.overlay {
|
||||||
left: 0;
|
left: 0;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
|
50
src/hud/Queue.svelte
Normal file
50
src/hud/Queue.svelte
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<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>
|
@ -34,6 +34,7 @@
|
|||||||
.resources {
|
.resources {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-around;
|
justify-content: space-around;
|
||||||
|
gap: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.resources div {
|
.resources div {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import buildings from "../buildings";
|
import buildings from "../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";
|
||||||
|
|
||||||
|
|
||||||
@ -28,9 +29,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;
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { enqueueBuilding } from "../utils";
|
||||||
import type { VillageState } from "../village";
|
import type { VillageState } from "../village";
|
||||||
|
|
||||||
|
|
||||||
@ -7,7 +8,10 @@ export default function upgradeBuilding(V: VillageState, buildingId: number) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cost = building.cost(building.level + 1);
|
const ongoingUpgrades = V.queue.filter(q => q.id === building.id);
|
||||||
|
const level = building.level + 1 + ongoingUpgrades.length;
|
||||||
|
|
||||||
|
const cost = building.cost(level);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
cost.wood > V.resources.wood
|
cost.wood > V.resources.wood
|
||||||
@ -22,8 +26,7 @@ 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.level++;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { produce } from 'immer';
|
import { produce } from 'immer';
|
||||||
|
|
||||||
import { getProduction, getStorage } from './utils';
|
import { getBuilding, getProduction, getStorage } from './utils';
|
||||||
import village, { type VillageState } from "./village";
|
import village, { type VillageState } from "./village";
|
||||||
import type { Production } from './types';
|
import type { Production } from './types';
|
||||||
|
|
||||||
@ -18,6 +18,17 @@ export default function update(timestamp: number) {
|
|||||||
|
|
||||||
village.update(state => {
|
village.update(state => {
|
||||||
return produce(state, (V: VillageState) => {
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make all buildings produce and consume.
|
||||||
const productionPerMinute = getProduction(V);
|
const productionPerMinute = getProduction(V);
|
||||||
const storage = getStorage(V);
|
const storage = getStorage(V);
|
||||||
|
|
||||||
|
16
src/utils.ts
16
src/utils.ts
@ -24,7 +24,7 @@ export function getEmptyResources(): Production {
|
|||||||
|
|
||||||
export function getProduction(villageState: VillageState): Production {
|
export function getProduction(villageState: VillageState): Production {
|
||||||
return villageState.buildings
|
return villageState.buildings
|
||||||
.filter(b => b.behavior.production)
|
.filter(b => b.behavior.production && b.level > 0)
|
||||||
.map(b => {
|
.map(b => {
|
||||||
if (b.behavior.production) {
|
if (b.behavior.production) {
|
||||||
return b.behavior.production(villageState, b);
|
return b.behavior.production(villageState, b);
|
||||||
@ -36,7 +36,7 @@ export function getProduction(villageState: VillageState): Production {
|
|||||||
|
|
||||||
export function getStorage(villageState: VillageState): Production {
|
export function getStorage(villageState: VillageState): Production {
|
||||||
return villageState.buildings
|
return villageState.buildings
|
||||||
.filter(b => b.behavior.storage)
|
.filter(b => b.behavior.storage && b.level > 0)
|
||||||
.map(b => {
|
.map(b => {
|
||||||
if (b.behavior.storage) {
|
if (b.behavior.storage) {
|
||||||
return b.behavior.storage(villageState, b);
|
return b.behavior.storage(villageState, b);
|
||||||
@ -87,3 +87,15 @@ export function shuffle<T>(array: Array<T>): Array<T> {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function enqueueBuilding(V: VillageState, building: Building) {
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -14,6 +14,12 @@ type Board = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface QueuedBuilding {
|
||||||
|
id: number;
|
||||||
|
remainingTime: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface VillageState {
|
export interface VillageState {
|
||||||
buildings: Building[];
|
buildings: Building[];
|
||||||
resources: {
|
resources: {
|
||||||
@ -25,6 +31,7 @@ export interface VillageState {
|
|||||||
};
|
};
|
||||||
villageTiles: Board;
|
villageTiles: Board;
|
||||||
outsideTiles: Board;
|
outsideTiles: Board;
|
||||||
|
queue: QueuedBuilding[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -78,6 +85,7 @@ function getInitialState() {
|
|||||||
},
|
},
|
||||||
villageTiles: getInitialVillageBoard(),
|
villageTiles: getInitialVillageBoard(),
|
||||||
outsideTiles: getInitialOutsideBoard(),
|
outsideTiles: getInitialOutsideBoard(),
|
||||||
|
queue: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the Town hall.
|
// Create the Town hall.
|
||||||
|
Loading…
Reference in New Issue
Block a user