35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { MutableSignal, mutableSignal, createRNG } from 'boardgame-core';
|
||
import { createRunState, generatePointCrawlMap, data, type RunState, type ItemData } from 'boardgame-core/samples/slay-the-spire-like';
|
||
|
||
/**
|
||
* 全局游戏运行状态 Signal
|
||
*
|
||
* 在 App.tsx 中创建为单例,所有场景共享。
|
||
* 遭遇场景通过读取此 signal 的当前遭遇状态来构建 UI。
|
||
*/
|
||
export function createGameState(seed?: number): MutableSignal<RunState> {
|
||
const actualSeed = seed ?? Date.now();
|
||
const rng = createRNG(actualSeed);
|
||
const encounters = data.desert.encounters;
|
||
const map = generatePointCrawlMap(rng, encounters);
|
||
const starterItems: ItemData[] = data.desert.items.slice(0, 3);
|
||
return mutableSignal<RunState>(createRunState(map, starterItems));
|
||
}
|
||
|
||
/** 获取当前遭遇数据(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,
|
||
},
|
||
};
|
||
}
|