Compare commits

...

9 Commits

19 changed files with 169 additions and 132 deletions

17
package-lock.json generated
View File

@ -10,7 +10,9 @@
"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",
@ -706,6 +708,12 @@
"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",
@ -912,6 +920,15 @@
"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,7 +12,9 @@
"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",

BIN
public/img/icons/time.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

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>

1
src/constants.ts Normal file
View File

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

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

@ -27,7 +27,7 @@
<section> <section>
<div class="building-panel"> <div class="building-panel">
<header> <header>
<h1>Building</h1> <h1>{ building.name } ({ building.level })</h1>
<span class="close"> <span class="close">
<button on:click={ close }>X</button> <button on:click={ close }>X</button>
</span> </span>
@ -41,7 +41,6 @@
</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,7 +1,13 @@
<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">
@ -21,6 +27,12 @@
<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,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

@ -0,0 +1,20 @@
<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,6 +31,7 @@
<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,6 +5,7 @@
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;
@ -22,7 +23,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);
@ -42,12 +43,19 @@
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"><Cost { cost } /></div> <div class="cost">
<Cost { cost } duration={ timeToRecruit } />
</div>
<div>
<input <input
type="range" type="range"
name="units" name="units"
@ -62,8 +70,11 @@
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> </div>
<RecruitmentQueue { building } />
</div>
<style> <style>
.university .cost { .university .cost {

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

@ -1,8 +1,9 @@
import { produce } from 'immer'; import { produce } from 'immer';
import { getBuilding, getProduction, getStorage, getUnitSource } from './utils'; import { CULTURE_TO_WIN } from './constants';
import village, { type VillageState } from "./village";
import type { ProductionType } from './types'; import type { ProductionType } from './types';
import { getProduction, getStorage, shuffle } from './utils';
import village, { type VillageState } from "./village";
let lastFrame: number; let lastFrame: number;
@ -23,16 +24,15 @@ 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 produce and consume. // Make all buildings and units produce and consume.
const productionPerMinute = getProduction(V); const productionPerMinute = getProduction(V);
const storage = getStorage(V); const storage = getStorage(V);
@ -41,6 +41,19 @@ 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];
@ -71,14 +84,8 @@ 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 >= 2) { if (V.resources.culture >= CULTURE_TO_WIN) {
V.victory = true; V.victory = true;
} }

View File

@ -1,37 +1,60 @@
import units from "./data/units"; import units from "./data/units";
import type { BuildingType, CostType, ProductionType } from "./types"; import type { BuildingType, CostType, ProductionType, ResourcesType } from "./types";
import type { VillageState } from "./village"; import type { VillageState } from "./village";
function _reduceResources(acc: ProductionType, item: ProductionType): ProductionType { function _reduceResources(acc: ResourcesType, item: ResourcesType): ResourcesType {
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(): ProductionType { export function getEmptyResources(): ResourcesType {
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): ProductionType { export function getProduction(villageState: VillageState): ResourcesType {
return villageState.buildings let production = getEmptyResources();
// 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, getEmptyResources()); .reduce(_reduceResources, production);
// 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;
} }
@ -99,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,
}; };
@ -111,7 +103,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 = 10; // DEBUG newBuilding.level = newBuilding.type === 'field' ? 1 : 10;
state.outsideTiles[y][x] = newBuilding.id; state.outsideTiles[y][x] = newBuilding.id;
state.buildings.push(newBuilding); state.buildings.push(newBuilding);
}); });