From f7f86db5100e7dd0d47ab82f23c6ca478ee3debe Mon Sep 17 00:00:00 2001 From: Adrian Gaudebert Date: Sat, 8 Mar 2025 18:41:32 +0100 Subject: [PATCH] Change quests to only offer one resource, but there is always one quest per type of resource. --- src/constants.ts | 5 +++++ src/create.ts | 25 +++++++++-------------- src/hud/Quests.svelte | 44 ++++++++++++++++++++++++++++++++--------- src/moves/startQuest.ts | 1 + src/types.ts | 6 +++++- src/update.ts | 10 +++------- src/village.ts | 12 +++++------ 7 files changed, 63 insertions(+), 40 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index adb6ef5..16054ab 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,7 +1,12 @@ +import type { Resource } from "./types"; + + export const CULTURE_TO_WIN = 20000; export const WORLD_MAP_HEIGHT = 9; export const WORLD_MAP_WIDTH = 9; +export const RESOURCES: Resource[] = ['wood', 'stone', 'iron', 'food', 'culture']; + // Debug values. export const TOWN_HALL_START_LEVEL = 1; export const RESOURCE_BUILDINGS_START_LEVEL = 1; diff --git a/src/create.ts b/src/create.ts index 5064948..7540afb 100644 --- a/src/create.ts +++ b/src/create.ts @@ -2,7 +2,7 @@ import buildings from "./data/buildings"; import { NAMES } from "./data/heroes"; import { Hex } from "./hexgrid"; import type { BuildingSource, BuildingType, HeroType, QuestType, ResourcesType } from "./types"; -import { getEmptyResources, random, shuffle } from "./utils"; +import { random, shuffle } from "./utils"; let uid = 0; @@ -37,25 +37,18 @@ export function createBuilding(buildingType: string): BuildingType { } -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); - } - }); +export function createQuest(resource: keyof ResourcesType, level: number): QuestType { + const adjustedLevel = Math.ceil((level * level + 3) / 3); + const duration = Math.max(1, random(adjustedLevel - 2, adjustedLevel + 2)); + const reward = Math.max(10, random( + Math.round((duration * 5 - level * 10) * (1 + level / 3)), + Math.round((duration * 5 + level * 10) * (1 + level / 3)), + )); return { id: uid++, duration, + resource, reward, level, started: false, diff --git a/src/hud/Quests.svelte b/src/hud/Quests.svelte index 5fe748b..2cf2dad 100644 --- a/src/hud/Quests.svelte +++ b/src/hud/Quests.svelte @@ -3,7 +3,8 @@ import { fly } from "svelte/transition"; import moves from "../moves"; - import { getPrettyTime } from "../utils"; + import type { Resource, ResourcesType } from "../types"; + import { getEmptyResources, getPrettyTime } from "../utils"; import village from "../village"; import Reward from "./Reward.svelte"; @@ -13,6 +14,12 @@ function startQuest(id: number, heroId: number) { moves.startQuest(id, heroId); } + + function getRewardAsResources(resource: Resource, reward: number): ResourcesType { + const output = getEmptyResources(); + output[resource] = reward; + return output; + }
@@ -23,15 +30,19 @@ out:fly={{ duration: 200, x: 500 }} animate:flip={{ duration: 100 }} > -

- +

+ + Duration + { #if quest.started } + { getPrettyTime(quest.remainingTime || 0) } + { :else } + { quest.duration } + { /if } + +

+ { #if !quest.started }

- Duration - { #if quest.started } - { getPrettyTime(quest.remainingTime || 0) } - { :else } - { quest.duration } { #each availableHeroes as hero }

diff --git a/src/moves/startQuest.ts b/src/moves/startQuest.ts index ef9eb60..1bbd475 100644 --- a/src/moves/startQuest.ts +++ b/src/moves/startQuest.ts @@ -1,5 +1,6 @@ import type { VillageState } from "../village"; + export default function startQuest(V: VillageState, questId: number, heroId: number) { const quest = V.quests.find(q => q.id === questId); if (!quest) { diff --git a/src/types.ts b/src/types.ts index dfae412..301e87a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,6 +26,9 @@ export interface ResourcesType extends CostType { } +export type Resource = keyof ResourcesType; + + export interface BuildingSource { name: string; type: string; @@ -139,7 +142,8 @@ export interface HeroType { export interface QuestType { id: number; duration: number; - reward: ResourcesType; + resource: keyof ResourcesType + reward: number; level: number; started: boolean; hero?: number; diff --git a/src/update.ts b/src/update.ts index 5f9eb4c..6691677 100644 --- a/src/update.ts +++ b/src/update.ts @@ -56,15 +56,11 @@ export default function update(timestamp: number) { quest.remainingTime -= delta; if (quest.remainingTime <= 0) { - V.resources.wood += quest.reward.wood; - V.resources.stone += quest.reward.stone; - V.resources.iron += quest.reward.iron; - V.resources.food += quest.reward.food; - V.resources.culture += quest.reward.culture; + V.resources[quest.resource] += quest.reward; // Replace the finished quest with a new one. - V.quests = V.quests.filter(q => q.id !== quest.id); - V.quests.push(createQuest(quest.level + 1)); + const index = V.quests.findIndex(q => q.id === quest.id); + V.quests[index] = createQuest(quest.resource, quest.level + 1); } }); diff --git a/src/village.ts b/src/village.ts index fa23ef7..2137a2f 100644 --- a/src/village.ts +++ b/src/village.ts @@ -1,11 +1,11 @@ import { writable } from "svelte/store"; +import { RESOURCE_BUILDINGS_START_LEVEL, RESOURCES, TOWN_HALL_START_LEVEL, WORLD_MAP_HEIGHT, WORLD_MAP_WIDTH } from "./constants"; import { createBuilding, createHero, createQuest } from "./create"; import worldmap from "./data/worldmap"; import { getTilesAtDistance, Hex } from "./hexgrid"; import { WORLDMAP_TYPES, type BuildingType, type CostType, type HeroType, type QuestType, type RegionType, type ResourcesType } from "./types"; -import { distanceBetweenCells, getAdjacentWorldmapCells, getKeysAsNumbers, indexToPoint, shuffle } from "./utils"; -import { RESOURCE_BUILDINGS_START_LEVEL, TOWN_HALL_START_LEVEL, WORLD_MAP_HEIGHT, WORLD_MAP_WIDTH } from "./constants"; +import { distanceBetweenCells, getKeysAsNumbers, indexToPoint, shuffle } from "./utils"; type Board = { @@ -106,11 +106,9 @@ function getInitialWorldmap(): RegionType[] { function getInitialQuests(): QuestType[] { - return [ - createQuest(1), - createQuest(1), - createQuest(1), - ]; + return RESOURCES.map(r => { + return createQuest(r, 1); + }); }