Compare commits

..

No commits in common. "f427f4e2b6d360c07d25242719a892827cc45aef" and "fbe48f925d7a53557c2e715403f1bfc2085661a6" have entirely different histories.

19 changed files with 132 additions and 169 deletions

17
package-lock.json generated
View File

@ -10,9 +10,7 @@
"devDependencies": { "devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.1.2", "@sveltejs/vite-plugin-svelte": "^3.1.2",
"@tsconfig/svelte": "^5.0.4", "@tsconfig/svelte": "^5.0.4",
"@types/luxon": "^3.4.2",
"immer": "^10.1.1", "immer": "^10.1.1",
"luxon": "^3.5.0",
"svelte": "^4.2.19", "svelte": "^4.2.19",
"svelte-check": "^4.0.4", "svelte-check": "^4.0.4",
"tslib": "^2.7.0", "tslib": "^2.7.0",
@ -708,12 +706,6 @@
"integrity": "sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==", "integrity": "sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==",
"dev": true "dev": true
}, },
"node_modules/@types/luxon": {
"version": "3.4.2",
"resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.4.2.tgz",
"integrity": "sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==",
"dev": true
},
"node_modules/acorn": { "node_modules/acorn": {
"version": "8.13.0", "version": "8.13.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.13.0.tgz", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.13.0.tgz",
@ -920,15 +912,6 @@
"integrity": "sha512-SW13ws7BjaeJ6p7Q6CO2nchbYEc3X3J6WrmTTDto7yMPqVSZTUyY5Tjbid+Ab8gLnATtygYtiDIJGQRRn2ZOiA==", "integrity": "sha512-SW13ws7BjaeJ6p7Q6CO2nchbYEc3X3J6WrmTTDto7yMPqVSZTUyY5Tjbid+Ab8gLnATtygYtiDIJGQRRn2ZOiA==",
"dev": true "dev": true
}, },
"node_modules/luxon": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/luxon/-/luxon-3.5.0.tgz",
"integrity": "sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==",
"dev": true,
"engines": {
"node": ">=12"
}
},
"node_modules/magic-string": { "node_modules/magic-string": {
"version": "0.30.12", "version": "0.30.12",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.12.tgz", "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.12.tgz",

View File

@ -12,9 +12,7 @@
"devDependencies": { "devDependencies": {
"@sveltejs/vite-plugin-svelte": "^3.1.2", "@sveltejs/vite-plugin-svelte": "^3.1.2",
"@tsconfig/svelte": "^5.0.4", "@tsconfig/svelte": "^5.0.4",
"@types/luxon": "^3.4.2",
"immer": "^10.1.1", "immer": "^10.1.1",
"luxon": "^3.5.0",
"svelte": "^4.2.19", "svelte": "^4.2.19",
"svelte-check": "^4.0.4", "svelte-check": "^4.0.4",
"tslib": "^2.7.0", "tslib": "^2.7.0",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -7,8 +7,8 @@
export let building: BuildingType; export let building: BuildingType;
$: isUpgrading = building.state.upgrade.isUpgrading; $: isUpgrading = $village.queue.find(q => q.id === building.id);
$: canUpgrade = !isUpgrading && canPayBuildingCost($village, building); $: canUpgrade = canPayBuildingCost($village, building);
function upgradeBuilding() { function upgradeBuilding() {
@ -27,11 +27,6 @@
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>
@ -48,10 +43,6 @@
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%);
} }
@ -59,4 +50,8 @@
.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

@ -1 +0,0 @@
export const CULTURE_TO_WIN = 20000;

View File

@ -25,11 +25,6 @@ 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

@ -27,7 +27,7 @@
<section> <section>
<div class="building-panel"> <div class="building-panel">
<header> <header>
<h1>{ building.name } ({ building.level })</h1> <h1>Building</h1>
<span class="close"> <span class="close">
<button on:click={ close }>X</button> <button on:click={ close }>X</button>
</span> </span>
@ -41,6 +41,7 @@
</div> </div>
<div class="upgrade"> <div class="upgrade">
<div> <div>
<p>{ building.name } ({ building.level })</p>
<button on:click={ () => upgrade() }>Upgrade</button> <button on:click={ () => upgrade() }>Upgrade</button>
</div> </div>
</div> </div>

View File

@ -1,13 +1,7 @@
<script lang="ts"> <script lang="ts">
import { Duration } from 'luxon';
import type { CostType } from "../types"; import type { CostType } from "../types";
export let cost: CostType; export let cost: CostType;
export let duration: number | null = null;
$: prettyDuration = (duration !== null) ? Duration.fromMillis(duration * 1000).toFormat(`mm:ss:SS`) : '';
</script> </script>
<div class="cost"> <div class="cost">
@ -27,12 +21,6 @@
<img src="/img/icons/food.png" alt="Food" /> <img src="/img/icons/food.png" alt="Food" />
{ cost.food } { cost.food }
</div> </div>
{ #if duration !== null }
<div>
<img src="/img/icons/time.png" alt="Duration">
{ prettyDuration }
</div>
{ /if }
</div> </div>
<style> <style>

View File

@ -9,6 +9,7 @@
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";
@ -46,6 +47,9 @@
<Outside /> <Outside />
{ /if } { /if }
</div> </div>
<div class="queue">
<Queue />
</div>
</section> </section>
<section class="overlay"> <section class="overlay">
<BuildingCreator /> <BuildingCreator />

50
src/hud/Queue.svelte Normal file
View 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>

View File

@ -1,20 +0,0 @@
<script lang="ts">
import type { BuildingType } from "../types";
import village from "../village";
export let building: BuildingType;
$: unitsCount = building.state.recruitment?.count || 0;
$: recruitmentTime = building.behavior.units?.recruitmentTime($village, building) || 0;
$: currentRecruitTime = building.state.recruitment?.elapsedTime || 0;
$: timeToNextRecruit = recruitmentTime * 1000 - currentRecruitTime;
$: prettyDuration = Math.ceil(timeToNextRecruit / 1000);
</script>
{ #if unitsCount > 0 }
<div>
{ unitsCount }
({ prettyDuration })
</div>
{ /if }

View File

@ -31,7 +31,6 @@
<div> <div>
<img src="/img/icons/culture.png" alt="Culture" /> <img src="/img/icons/culture.png" alt="Culture" />
{ Math.floor($village.resources.culture) } { Math.floor($village.resources.culture) }
({ production.culture >= 0 ? '+' : '' }{ production.culture })
</div> </div>
</div> </div>

View File

@ -5,7 +5,6 @@
import type { VillageState } from "../village"; import type { VillageState } from "../village";
import village from "../village"; import village from "../village";
import Cost from "./Cost.svelte"; import Cost from "./Cost.svelte";
import RecruitmentQueue from "./RecruitmentQueue.svelte";
export let building: BuildingType; export let building: BuildingType;
@ -23,7 +22,7 @@
iron: unit.cost.iron * numberOfUnits, iron: unit.cost.iron * numberOfUnits,
food: unit.cost.food * numberOfUnits, food: unit.cost.food * numberOfUnits,
}; };
$: timeToRecruit = building.behavior.units?.recruitmentTime($village, building) * numberOfUnits;
function recruit() { function recruit() {
moves.recruitUnits(building.id, unitType, numberOfUnits); moves.recruitUnits(building.id, unitType, numberOfUnits);
@ -43,19 +42,12 @@
return res; return res;
} }
function setMaxUnits() {
numberOfUnits = maximumUnits;
}
</script> </script>
<div class="university"> <div class="university">
<p>Create Philosophers</p> <p>Create Philosophers</p>
<div class="cost"> <div class="cost"><Cost { cost } /></div>
<Cost { cost } duration={ timeToRecruit } />
</div>
<div>
<input <input
type="range" type="range"
name="units" name="units"
@ -70,10 +62,7 @@
max={ maximumUnits } max={ maximumUnits }
bind:value={ numberOfUnits } bind:value={ numberOfUnits }
/> />
<button on:click={ setMaxUnits }>↑</button>
<button on:click={ recruit } disabled={ numberOfUnits > maximumUnits }>Recruit</button> <button on:click={ recruit } disabled={ numberOfUnits > maximumUnits }>Recruit</button>
</div>
<RecruitmentQueue { building } />
</div> </div>
<style> <style>

View File

@ -1,5 +1,7 @@
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";
@ -28,11 +30,10 @@ 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, getBuildingUpgradeCost } from "../utils"; import { canPayBuildingCost, enqueueBuilding, getBuildingUpgradeCost } from "../utils";
import type { VillageState } from "../village"; import type { VillageState } from "../village";
@ -8,10 +8,6 @@ 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;
} }
@ -22,9 +18,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.state.upgrade.isUpgrading = true;
building.state.upgrade.remainingTime = 1000 * (building.level + 1);
return true; return true;
} }

View File

@ -41,10 +41,6 @@ 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

@ -1,9 +1,8 @@
import { produce } from 'immer'; import { produce } from 'immer';
import { CULTURE_TO_WIN } from './constants'; import { getBuilding, getProduction, getStorage, getUnitSource } from './utils';
import type { ProductionType } from './types';
import { getProduction, getStorage, shuffle } from './utils';
import village, { type VillageState } from "./village"; import village, { type VillageState } from "./village";
import type { ProductionType } from './types';
let lastFrame: number; let lastFrame: number;
@ -24,15 +23,16 @@ export default function update(timestamp: number) {
return produce(state, (V: VillageState) => { return produce(state, (V: VillageState) => {
// Advance building construction. // Advance building construction.
V.buildings.filter(b => b.state.upgrade.isUpgrading).forEach(b => { if (V.queue.length) {
b.state.upgrade.remainingTime -= delta; V.queue[0].remainingTime -= delta;
if (b.state.upgrade.remainingTime <= 0) { if (V.queue[0].remainingTime <= 0) {
b.level++; const building = getBuilding(V, V.queue[0].id);
b.state.upgrade.isUpgrading = false; building.level++;
V.queue.shift();
}
} }
});
// Make all buildings and units produce and consume. // Make all buildings produce and consume.
const productionPerMinute = getProduction(V); const productionPerMinute = getProduction(V);
const storage = getStorage(V); const storage = getStorage(V);
@ -41,19 +41,6 @@ export default function update(timestamp: number) {
const outputPerMinute = productionPerMinute[resource]; const outputPerMinute = productionPerMinute[resource];
const outputPerMilisecond = outputPerMinute / 60.0 / 1000.0; const outputPerMilisecond = outputPerMinute / 60.0 / 1000.0;
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.
Object.keys(productionPerMinute).forEach((key) => {
const resource = key as keyof ProductionType;
if (V.resources[resource] > storage[resource]) { if (V.resources[resource] > storage[resource]) {
V.resources[resource] = storage[resource]; V.resources[resource] = storage[resource];
@ -84,8 +71,14 @@ export default function update(timestamp: number) {
} }
}); });
// Make philosophers produce culture.
const philosopher = getUnitSource('philosopher');
const outputPerMinute = philosopher.behavior.culturePerMinute;
const outputPerMilisecond = outputPerMinute / 60.0 / 1000.0;
V.resources.culture += outputPerMilisecond * delta * (V.units.philosopher || 0);
// Check if the game is won. // Check if the game is won.
if (V.resources.culture >= CULTURE_TO_WIN) { if (V.resources.culture >= 2) {
V.victory = true; V.victory = true;
} }

View File

@ -1,60 +1,37 @@
import units from "./data/units"; import units from "./data/units";
import type { BuildingType, CostType, ProductionType, ResourcesType } from "./types"; import type { BuildingType, CostType, ProductionType } from "./types";
import type { VillageState } from "./village"; import type { VillageState } from "./village";
function _reduceResources(acc: ResourcesType, item: ResourcesType): ResourcesType { function _reduceResources(acc: ProductionType, item: ProductionType): ProductionType {
return { return {
wood: acc.wood + item.wood, wood: acc.wood + item.wood,
stone: acc.stone + item.stone, stone: acc.stone + item.stone,
iron: acc.iron + item.iron, iron: acc.iron + item.iron,
food: acc.food + item.food, food: acc.food + item.food,
culture: acc.culture + item.culture,
}; };
} }
export function getEmptyResources(): ResourcesType { export function getEmptyResources(): ProductionType {
return { return {
wood: 0, wood: 0,
stone: 0, stone: 0,
iron: 0, iron: 0,
food: 0, food: 0,
culture: 0,
}; };
} }
export function getProduction(villageState: VillageState): ResourcesType { export function getProduction(villageState: VillageState): ProductionType {
let production = getEmptyResources(); return villageState.buildings
// Add buildings production and intake.
production = villageState.buildings
.filter(b => b.behavior.production && b.level > 0) .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);
} }
}) })
.reduce(_reduceResources, production); .reduce(_reduceResources, getEmptyResources());
// Add units production and intake.
['philosopher'].forEach(type => {
const unit = getUnitSource(type);
const unitCount = villageState.units[type] || 0;
// Add food intake.
const intakePerMinute = unit.behavior.foodIntakePerMinute * unitCount;
production.food -= intakePerMinute;
// Add culture production for Philosophers.
if (type === 'philosopher') {
const outputPerMinute = unit.behavior.culturePerMinute;
production.culture += outputPerMinute * unitCount;
}
});
return production;
} }
@ -122,8 +99,21 @@ export function shuffle<T>(array: Array<T>): Array<T> {
} }
export function getBuildingUpgradeCost(_V: VillageState, building: BuildingType): CostType { export function enqueueBuilding(V: VillageState, building: BuildingType) {
const level = building.level + 1; 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;
return building.cost(level); return building.cost(level);
} }

View File

@ -13,6 +13,12 @@ type Board = {
} }
interface QueuedBuilding {
id: number;
remainingTime: number;
}
export interface VillageState { export interface VillageState {
buildings: BuildingType[]; buildings: BuildingType[];
units: { units: {
@ -21,6 +27,7 @@ export interface VillageState {
resources: ResourcesType; resources: ResourcesType;
villageTiles: Board; villageTiles: Board;
outsideTiles: Board; outsideTiles: Board;
queue: QueuedBuilding[];
victory: boolean; victory: boolean;
} }
@ -76,6 +83,7 @@ function getInitialState() {
}, },
villageTiles: getInitialVillageBoard(), villageTiles: getInitialVillageBoard(),
outsideTiles: getInitialOutsideBoard(), outsideTiles: getInitialOutsideBoard(),
queue: [],
victory: false, victory: false,
}; };
@ -103,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 = newBuilding.type === 'field' ? 1 : 10; newBuilding.level = 10; // DEBUG
state.outsideTiles[y][x] = newBuilding.id; state.outsideTiles[y][x] = newBuilding.id;
state.buildings.push(newBuilding); state.buildings.push(newBuilding);
}); });