refactor: global state for scenes
This commit is contained in:
@@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user