boardgame-core/docs/api-reference.md

4.2 KiB

API 参考

核心

导出 说明
IGameContext 游戏上下文基础接口
createGameContext(registry, initialState?) 创建游戏上下文实例
createGameContextFromModule(module) 从 GameModule 创建游戏上下文
createGameCommandRegistry<TState>() 创建命令注册表
createGameModule(module) 辅助函数,标记 GameModule
GameHost<TState> 游戏生命周期管理类
createGameHost(module, options?) 从 GameModule 创建 GameHost
GameHostStatus 类型: 'created' | 'running' | 'disposed'
GameModule 游戏模块类型

棋子 (Parts)

导出 说明
Part<TMeta> 游戏棋子类型
PartTemplate<TMeta> 创建棋子的模板类型
PartPool<TMeta> 棋子池类型
createPart(template, id) 创建单个棋子
createParts(template, count, idPrefix) 批量创建相同棋子
createPartsFromTable(template, table, idField?) 从表格数据创建棋子
createPartPool(template, count, idPrefix) 创建棋子池
mergePartPools(...pools) 合并多个棋子池
findPartById(parts, id) 按 ID 查找棋子
isCellOccupied(parts, regionId, position) 检查格子是否被占用
getPartAtPosition(parts, regionId, position) 获取格子上的棋子
isCellOccupiedByRegion(region, position) O(1) 检查格子占用
getPartAtPositionInRegion(region, parts, position) O(1) 获取棋子
flip(part) / flipTo(part, side) / roll(part, rng) 翻面/随机面

区域 (Regions)

导出 说明
Region / RegionAxis 区域类型
createRegion(id, axes) 创建区域
applyAlign(region, parts) 紧凑排列
shuffle(region, parts, rng) 打乱位置
moveToRegion(part, sourceRegion?, targetRegion, position?) 移动棋子到区域

命令 (Commands)

导出 说明
Command / CommandSchema / CommandResult 命令相关类型
CommandParamSchema / CommandOptionSchema / CommandFlagSchema 命令参数类型
parseCommand(input) 解析命令字符串
parseCommandSchema(schema) 解析 Schema 字符串
validateCommand(cmd, schema) 验证命令
parseCommandWithSchema(input, schema) 解析并应用 Schema
applyCommandSchema(cmd, schema) 应用 Schema 到命令

命令运行器

导出 说明
CommandRunner<TContext, TResult> 命令运行器类型
CommandRunnerHandler 命令处理器
CommandRunnerContext / CommandRunnerContextExport 命令运行器上下文
CommandRegistry 命令注册表类型
PromptEvent / CommandRunnerEvents 提示事件类型
PromptValidator<T> 提示验证器类型
createCommandRegistry() 创建命令注册表
registerCommand(registry, name, handler) 注册命令
unregisterCommand(registry, name) 注销命令
hasCommand(registry, name) 检查命令是否存在
getCommand(registry, name) 获取命令
runCommand(ctx, input) 运行命令
runCommandParsed(ctx, cmd) 运行已解析命令
createCommandRunnerContext(registry, ctx) 创建命令运行器上下文

MutableSignal

导出 说明
MutableSignal<T> 支持突变和动画中断的响应式信号
mutableSignal(initial?, options?) 创建 MutableSignal
EntityCollection<T> / createEntityCollection() 实体集合辅助函数

MutableSignal 成员

成员 说明
value: T 获取当前值
produce(fn: (draft: T) => void): void 同步不可变更新
addInterruption(promise: Promise<void>): void 添加中断
clearInterruptions(): void 清除所有中断
produceAsync(fn: (draft: T) => void): Promise<void> 等待中断完成后更新

GameHost 中断代理

GameHost 直接代理 addInterruptionclearInterruptions,供 UI 层使用。 详见 动画与状态更新同步

工具

导出 说明
RNG 随机数生成器接口
createRNG(seed?) 创建种子 RNG
Mulberry32RNG Mulberry32 算法实现