Create all resource buildings by default.

This commit is contained in:
Adrian 2024-10-24 15:31:40 +02:00
parent 25f281028c
commit a47486973c
2 changed files with 60 additions and 6 deletions

View File

@ -58,3 +58,32 @@ export function getBuilding(V: VillageState, buildingId: number): Building {
export function getKeysAsNumbers(dict: Object): Array<number> { export function getKeysAsNumbers(dict: Object): Array<number> {
return Object.keys(dict).map(i => parseInt(i)).sort((a, b) => a - b); return Object.keys(dict).map(i => parseInt(i)).sort((a, b) => a - b);
} }
/**
* Creates an array of shuffled values, using a version of the
* [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
*
* @since 0.1.0
* @category Array
* @param {Array} array The array to shuffle.
* @returns {Array} Returns the new shuffled array.
* @example
*
* shuffle([1, 2, 3, 4])
* // => [4, 1, 3, 2]
*/
export function shuffle<T>(array: Array<T>): Array<T> {
const length = array.length;
if (!length) {
return [];
}
let index = -1;
const lastIndex = length - 1;
const result = [ ...array ];
while (++index < length) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1));
[ result[rand], result[index] ] = [ result[index], result[rand] ];
}
return result;
}

View File

@ -3,7 +3,8 @@ import { writable } from "svelte/store";
import buildings from "./buildings"; import buildings from "./buildings";
import { createBuilding } from "./create"; import { createBuilding } from "./create";
import type { Building } from "./types"; import type { Building } from "./types";
import { getTilesAtDistance } from "./hexgrid"; import { getTilesAtDistance, Hex } from "./hexgrid";
import { getKeysAsNumbers, shuffle } from "./utils";
type Board = { type Board = {
@ -66,11 +67,8 @@ function getInitialOutsideBoard() {
function getInitialState() { function getInitialState() {
const townhall = createBuilding(buildings.townhall); const state: VillageState = {
const state = { buildings: [],
buildings: [
townhall,
],
resources: { resources: {
wood: 60, wood: 60,
stone: 60, stone: 60,
@ -82,7 +80,34 @@ function getInitialState() {
outsideTiles: getInitialOutsideBoard(), outsideTiles: getInitialOutsideBoard(),
}; };
// Create the Town hall.
const townhall = createBuilding(buildings.townhall);
state.villageTiles[0][0] = townhall.id; state.villageTiles[0][0] = townhall.id;
state.buildings.push(townhall);
// Create all the resource buildings.
const resourceBuildingTypes: Array<keyof typeof buildings> = shuffle([
'woodcutter', 'woodcutter', 'woodcutter', 'woodcutter',
'mine', 'mine', 'mine', 'mine',
'pit', 'pit', 'pit', 'pit',
'field', 'field', 'field', 'field', 'field', 'field',
]);
getKeysAsNumbers(state.outsideTiles).forEach(y => {
getKeysAsNumbers(state.outsideTiles[y]).forEach(x => {
if (state.outsideTiles[y][x] !== DEFAULT_TILE) {
return;
}
const type = resourceBuildingTypes.pop();
if (type === undefined) {
throw new Error("Not enough building types for outside resource buildings");
}
const newBuilding = createBuilding(buildings[type]);
newBuilding.tile = new Hex(x, y);
state.outsideTiles[y][x] = newBuilding.id;
state.buildings.push(newBuilding);
});
});
return state; return state;
} }