createScene()
创建游戏场景,场景是游戏流程的基本单位。
函数签名
createScene(sceneId: string, exec: () => Promise<Scene | void>): Scene基础用法
const scene1 = createScene("scene1", async () => {
Game.narrate`欢迎来到场景1`;
figure.speak`你好!`;
});场景跳转
通过返回 Scene 实例实现跳转:
const entry = createScene("entry", async () => {
Game.narrate`游戏开始`;
return scene2; // 跳转到 scene2
});
const scene2 = createScene("scene2", async () => {
Game.narrate`这是场景2`;
// 不返回任何场景,游戏结束
});条件分支
const entry = createScene("entry", async () => {
const choice = await Game.use<string>(
"target",
<InputDialog
title="去哪里?"
type="select-single"
options={[
{ label: "森林", value: "forest" },
{ label: "城镇", value: "town" },
]}
/>
);
if (choice === "forest") {
return forestScene;
}
return townScene;
});循环场景
const loopScene = createScene("loop", async () => {
Game.narrate`你被困在这里了!`;
return loopScene; // 返回自身,无限循环
});入口场景配置
import { Game } from "@core/game";
Game.configure({
defaultEntryScene: entry
});注意事项
- 场景 ID 必须唯一
- 场景会自动注册到全局场景表
- 不返回场景时游戏结束
- 场景执行函数推荐是 async 函数,这样可以方便使用await关键词
Last updated on