2024-10-21 15:35:32 +00:00
|
|
|
import { writable } from "svelte/store";
|
2024-10-23 08:36:03 +00:00
|
|
|
|
2024-10-21 15:35:32 +00:00
|
|
|
import buildings from "./buildings";
|
2024-10-23 08:14:19 +00:00
|
|
|
import { createBuilding } from "./create";
|
2024-10-21 15:35:32 +00:00
|
|
|
import type { Building } from "./types";
|
2024-10-24 09:32:31 +00:00
|
|
|
import { getTilesAtDistance } from "./hexgrid";
|
|
|
|
|
|
|
|
|
|
|
|
type Board = {
|
|
|
|
[key: number]: {
|
|
|
|
[key: number]: number;
|
|
|
|
}
|
|
|
|
}
|
2024-10-21 15:35:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
export interface VillageState {
|
|
|
|
buildings: Building[];
|
|
|
|
resources: {
|
|
|
|
wood: number;
|
|
|
|
stone: number;
|
|
|
|
iron: number;
|
|
|
|
food: number;
|
|
|
|
culture: number;
|
2024-10-22 15:15:35 +00:00
|
|
|
};
|
2024-10-24 09:32:31 +00:00
|
|
|
villageTiles: Board;
|
|
|
|
outsideTiles: Board;
|
2024-10-21 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-24 09:32:31 +00:00
|
|
|
export const DEFAULT_TILE = -1;
|
|
|
|
export const VILLAGE_TILE = -2;
|
|
|
|
|
|
|
|
|
|
|
|
function getInitialVillageBoard() {
|
|
|
|
const board: Board = {
|
|
|
|
0: { 0: DEFAULT_TILE },
|
|
|
|
};
|
|
|
|
for (let i = 1; i <= 2; i++) {
|
|
|
|
getTilesAtDistance(i).forEach(tile => {
|
|
|
|
if (board[tile.y] === undefined) {
|
|
|
|
board[tile.y] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
board[tile.y][tile.x] = DEFAULT_TILE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return board;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getInitialOutsideBoard() {
|
|
|
|
const board: Board = {
|
|
|
|
0: { 0: VILLAGE_TILE },
|
|
|
|
};
|
|
|
|
for (let i = 1; i <= 2; i++) {
|
|
|
|
getTilesAtDistance(i).forEach(tile => {
|
|
|
|
if (board[tile.y] === undefined) {
|
|
|
|
board[tile.y] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
board[tile.y][tile.x] = DEFAULT_TILE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return board;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getInitialState() {
|
|
|
|
const townhall = createBuilding(buildings.townhall);
|
|
|
|
const state = {
|
|
|
|
buildings: [
|
|
|
|
townhall,
|
|
|
|
],
|
|
|
|
resources: {
|
|
|
|
wood: 60,
|
|
|
|
stone: 60,
|
|
|
|
iron: 60,
|
|
|
|
food: 50,
|
|
|
|
culture: 0,
|
|
|
|
},
|
|
|
|
villageTiles: getInitialVillageBoard(),
|
|
|
|
outsideTiles: getInitialOutsideBoard(),
|
|
|
|
};
|
|
|
|
|
|
|
|
state.villageTiles[0][0] = townhall.id;
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const village = writable<VillageState>(getInitialState());
|
2024-10-21 15:35:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
export default village;
|