import type { World } from '../engine/world' /** * 统一时轮调度:所有系统以 phase 注册,由时钟按固定顺序驱动。 * - 月度 phase:production / aging / cultivation / missions / events / diplomacy / epilogue * - 年首钩子:onYearStart(婚配 → 声望岁贡 → 岁末族簿) * 新增系统 = 注册一行,不再修改 advanceMonth 本体。 */ export type PhaseId = | 'production' | 'aging' | 'cultivation' | 'missions' | 'events' | 'diplomacy' | 'epilogue' export type SystemHook = (w: World) => void export interface PhaseStat { phase: PhaseId ms: number count: number } export const PHASE_ORDER: PhaseId[] = [ 'production', 'aging', 'cultivation', 'missions', 'events', 'diplomacy', 'epilogue' ] export class GameClock { private monthly = new Map() private yearly: SystemHook[] = [] register(phase: PhaseId, fn: SystemHook): void { const list = this.monthly.get(phase) ?? [] list.push(fn) this.monthly.set(phase, list) } onYearStart(fn: SystemHook): void { this.yearly.push(fn) } fireYearStart(w: World): void { for (const fn of this.yearly) fn(w) } stepMonthly(w: World): PhaseStat[] { const report: PhaseStat[] = [] for (const phase of PHASE_ORDER) { const t0 = performance.now() const fns = this.monthly.get(phase) ?? [] for (const fn of fns) fn(w) const ms = performance.now() - t0 report.push({ phase, ms, count: fns.length }) } return report } subscriptionCount(): number { let n = 0 for (const list of this.monthly.values()) n += list.length return n + this.yearly.length } }