Story
作为游戏入口的 `Story` 类,涵盖场景入口、持久化与服务注册,以及存档兼容哈希
Story 是创建剧本的主入口
import { Story } from 'narraleaf-react';
const story = new Story("故事名称");
const scene1 = new Scene("场景 1");
story.entry(scene1);公共方法
constructor
name: string- 剧本名称config: IStoryConfig- 参见 IStoryConfig
entry
scene: Scene- 入口场景- 返回
this
registerPersistent
注册持久化对象,使其在剧本中可用:
const story = new Story("故事名称");
const persis = new Persistent</* ... */>("persis", /* ... */);
story.registerPersistent(persis);
const scene1 = new Scene("场景 1");
scene1.action([
persis.set(/* ... */, /* ... */)
]);persistent: Persistent<any>- 要注册的 Persistent- 返回
this
registerService
getService<T extends Service>
通过名称获取服务,未找到时抛出错误:
const game = useGame();
const liveGame = game.getLiveGame();
const gallery = liveGame.story?.getService<Gallery>("gallery");name: string- 服务名称- 返回
T
createPersistent<T extends PersistentContent>
创建并注册 Persistent:
const persistent = story.createPersistent("playerData", {
name: "persistent",
});
// 等价于
const persistent = new Persistent("playerData", {
name: "persistent",
});
story.registerPersistent(persistent);namespace: string- 持久化命名空间defaultContent: T- 默认内容- 返回
Persistent<T>
hash
返回剧本的 64 位哈希值
哈希值基于剧本的逻辑结构和操作顺序计算,而非文本内容:
- 操作顺序改变(动画、跳转等)会导致不同的哈希值
- 文本内容改变(对话、描述等)不会影响哈希值
strict为true时,哈希值还会包含字符串化的 lambda 函数
主要用于检查存档之间的剧本兼容性
const story = new Story("故事名称");
const hash = story.hash(); // 基本哈希值
const strictHash = story.hash(true); // 严格模式strict: boolean- 是否使用严格模式(默认:false)- 返回
string- 64 位哈希值
stringify
将剧本转换为字符串表示,主要用于内部哈希计算:
const story = new Story("故事名称");
const str = story.stringify(); // 基本字符串表示
const strictStr = story.stringify(true); // 严格模式strict: boolean- 是否使用严格模式(默认:false)- 返回
string- 字符串表示