refactor: global state for scenes

This commit is contained in:
2026-04-14 14:21:51 +08:00
parent 7fb9edcbf0
commit 3ca2a16e29
6 changed files with 122 additions and 91 deletions
@@ -0,0 +1,29 @@
import { MutableSignal, mutableSignal } from 'boardgame-core';
import { createRunState, type RunState } from 'boardgame-core/samples/slay-the-spire-like';
/**
* 全局游戏运行状态 Signal
*
* 在 App.tsx 中创建为单例,所有场景共享。
* 遭遇场景通过读取此 signal 的当前遭遇状态来构建 UI。
*/
export function createGameState(seed?: number): MutableSignal<RunState> {
return mutableSignal<RunState>(createRunState(seed));
}
/** 获取当前遭遇数据(computed getter) */
export function currentEncounter(
gameState: MutableSignal<RunState>
): { nodeId: string; encounter: { name: string; description: string; type: string } } | null {
const state = gameState.value;
const node = state.map.nodes.get(state.currentNodeId);
if (!node || !node.encounter) return null;
return {
nodeId: node.id,
encounter: {
type: node.type,
name: node.encounter.name,
description: node.encounter.description,
},
};
}