refactor(0.1.14-P2): Kernel 三合一 + GameEngine 引擎门面
- Kernel(engine/kernel/Kernel.ts):游戏时钟+种子随机+事件总线——单一实例三合一,
World 不再自造 clock/rng(内核注入,bus 透传 out)
- GameEngine(engine/GameEngine.ts):唯一引擎入口——
new GameEngine({seed,datapack}) → world/facade/kernel/datapack,
advance/act/query/subscribe/about/installPlugin/snapshot/restore/status
- World 构造接受 kernel 注入(保持行为等价:时钟注册顺序不变)
- ApiFacade.subscribe 简化纯实现
- 验证:967 测试全绿、金钟罩三档零漂移、typecheck 0 error
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { GameClock } from './clock'
|
||||
import { Rng, seedToRng } from './rng'
|
||||
import type { WorldEventBus } from '../runtime/World'
|
||||
|
||||
export interface KernelBus {
|
||||
onLog: WorldEventBus['onLog']
|
||||
onChronicle: WorldEventBus['onChronicle']
|
||||
onBattle: WorldEventBus['onBattle']
|
||||
onPendingEvent: WorldEventBus['onPendingEvent']
|
||||
onGameOver: WorldEventBus['onGameOver']
|
||||
onYearPaper?: WorldEventBus['onYearPaper']
|
||||
onSystemChange?: WorldEventBus['onSystemChange']
|
||||
onPluginChange?: WorldEventBus['onPluginChange']
|
||||
}
|
||||
|
||||
/** 三合一内核:游戏时钟 + 种子随机 + 事件总线(单一实例,由 GameEngine 持有) */
|
||||
export class Kernel {
|
||||
readonly clock: GameClock
|
||||
readonly rng: Rng
|
||||
readonly bus: KernelBus
|
||||
private busOut: WorldEventBus[] = []
|
||||
|
||||
constructor(seed: string) {
|
||||
this.clock = new GameClock()
|
||||
this.rng = new Rng(seedToRng(seed))
|
||||
this.bus = {
|
||||
onLog: (kind, text) => this.busOut.forEach((o) => o.onLog(kind, text)),
|
||||
onChronicle: (entry, important) => this.busOut.forEach((o) => o.onChronicle(entry, important)),
|
||||
onBattle: (log) => this.busOut.forEach((o) => o.onBattle(log)),
|
||||
onPendingEvent: (id) => this.busOut.forEach((o) => o.onPendingEvent(id)),
|
||||
onGameOver: (reason, year) => this.busOut.forEach((o) => o.onGameOver(reason, year)),
|
||||
onYearPaper: (r) => this.busOut.forEach((o) => o.onYearPaper?.(r)),
|
||||
onSystemChange: (id, enabled) => this.busOut.forEach((o) => o.onSystemChange?.(id, enabled)),
|
||||
onPluginChange: (id, action) => this.busOut.forEach((o) => o.onPluginChange?.(id, action))
|
||||
}
|
||||
}
|
||||
|
||||
/** 注册外部订阅(每引擎一个 World 共享 bus) */
|
||||
attach(out: WorldEventBus): void {
|
||||
this.busOut.push(out)
|
||||
}
|
||||
|
||||
detachAll(): void {
|
||||
this.busOut = []
|
||||
}
|
||||
|
||||
/** 随机快照回写(存档时) */
|
||||
syncRngTo(state: { rng: ReturnType<Rng['getState']> }): void {
|
||||
state.rng = this.rng.getState()
|
||||
}
|
||||
}
|
||||
|
||||
/** 引擎启动统一入口(含默认种子) */
|
||||
export function makeKernel(seed?: string): Kernel {
|
||||
return new Kernel(seed ?? `cotyc-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`)
|
||||
}
|
||||
Reference in New Issue
Block a user