36 lines
831 B
TypeScript
36 lines
831 B
TypeScript
import buildings from "./data/buildings";
|
|
import { Hex } from "./hexgrid";
|
|
import type { BuildingType, BuildingSource } from "./types";
|
|
|
|
|
|
let uid = 0;
|
|
|
|
|
|
export function getBuildingSource(buildingType: string): BuildingSource {
|
|
const source: BuildingSource | undefined = buildings.find(b => b.type === buildingType);
|
|
|
|
if (source === undefined) {
|
|
throw new Error(`Unknown building type: "${buildingType}"`);
|
|
}
|
|
|
|
return source
|
|
}
|
|
|
|
|
|
export function createBuilding(buildingType: string): BuildingType {
|
|
const source: BuildingSource = getBuildingSource(buildingType);
|
|
|
|
return {
|
|
...source,
|
|
id: uid++,
|
|
level: 1,
|
|
tile: new Hex(0, 0),
|
|
state: {
|
|
upgrade: {
|
|
isUpgrading: false,
|
|
remainingTime: 0,
|
|
}
|
|
},
|
|
};
|
|
}
|