Files
boardgame-phaser/packages/sts-like-viewer/src/state/gameState.ts
T

35 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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,
},
};
}