2024-10-25 19:03:32 +02:00
|
|
|
import buildings from "./data/buildings";
|
2024-11-12 17:21:19 +01:00
|
|
|
import { NAMES } from "./data/heroes";
|
2024-10-24 11:32:31 +02:00
|
|
|
import { Hex } from "./hexgrid";
|
2024-11-12 17:21:19 +01:00
|
|
|
import type { BuildingSource, BuildingType, HeroType, QuestType, ResourcesType } from "./types";
|
|
|
|
import { getEmptyResources, random, shuffle } from "./utils";
|
2024-10-23 10:14:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
let uid = 0;
|
|
|
|
|
|
|
|
|
2024-10-24 16:24:39 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-25 19:03:32 +02:00
|
|
|
export function createBuilding(buildingType: string): BuildingType {
|
2024-10-24 16:24:39 +02:00
|
|
|
const source: BuildingSource = getBuildingSource(buildingType);
|
|
|
|
|
2024-10-23 10:14:19 +02:00
|
|
|
return {
|
2024-10-24 16:24:39 +02:00
|
|
|
...source,
|
2024-10-23 10:14:19 +02:00
|
|
|
id: uid++,
|
2024-10-24 11:32:31 +02:00
|
|
|
level: 1,
|
|
|
|
tile: new Hex(0, 0),
|
2024-11-05 12:42:39 +01:00
|
|
|
state: {
|
|
|
|
upgrade: {
|
|
|
|
isUpgrading: false,
|
|
|
|
remainingTime: 0,
|
|
|
|
}
|
|
|
|
},
|
2024-10-23 10:14:19 +02:00
|
|
|
};
|
|
|
|
}
|
2024-11-12 17:21:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
export function createQuest(level: number): QuestType {
|
|
|
|
const reward = getEmptyResources();
|
|
|
|
const adjustedLevel = level * level + 3;
|
|
|
|
const duration = random(adjustedLevel - 2, adjustedLevel + 2);
|
|
|
|
Object.keys(reward).forEach(r => {
|
|
|
|
const resource = r as keyof ResourcesType;
|
|
|
|
reward[resource] = random(
|
|
|
|
Math.round((duration * 5 - level * 10) * (1 + level / 3)),
|
|
|
|
Math.round((duration * 5 + level * 10) * (1 + level / 3)),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (resource === 'culture') {
|
|
|
|
reward[resource] = Math.round(reward[resource] / 20);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: uid++,
|
|
|
|
duration,
|
|
|
|
reward,
|
|
|
|
level,
|
|
|
|
started: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function createHero(): HeroType {
|
|
|
|
return {
|
|
|
|
id: uid++,
|
|
|
|
name: shuffle(NAMES)[0],
|
|
|
|
};
|
|
|
|
}
|