叙事使用 JS/TS 进行叙事
选择菜单
解释如何在 Pixi’VN 中实现和自定义交互式选择菜单,让玩家做出影响故事走向的决定。
UI 界面
你可以在界面示例部分找到选择菜单 UI 界面的示例。
ink
你可以在 ink 语法中使用此方法。点击此处了解更多。
在视觉小说中,选择菜单允许玩家做出影响故事走向的决定。
在 Pixi’VN 中,你可以提示玩家做出选择。每个选择可以启动一个叙事节点(label)或关闭选择菜单。
要求玩家做出选择
要求玩家做出选择,请将 narration.choices 设置为 StoredChoiceInterface 的数组。要创建 StoredChoiceInterface 对象,请使用:
import {
newChoiceOption,
newCloseChoiceOption,
narration,
newLabel,
} from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async () => {
narration.dialogue = "Choose a fruit:";
narration.choices = [
newChoiceOption("Orange", orangeLabel, {}), // by default, the label will be called with "call"
newChoiceOption("Banana", bananaLabel, {}, { type: "jump" }),
newChoiceOption(
"Apple",
appleLabel,
{ quantity: 5 },
{ type: "call" },
),
newCloseChoiceOption("Cancel"),
];
},
() => {
narration.dialogue = "Restart";
},
async (props) => await narration.jump("start", props),
]);获取
要获取当前选择菜单,请使用 narration.choices.list。它返回 StoredChoiceInterface 的数组。
const menuOptions: StoredChoiceInterface[] = narration.choices.list;请求
要选择一个选项,请使用 narration.choices.select。
const item = narration.choices.list![0]; // get the first item
narration.choices
.select(item, {
// Add StepLabelProps here
navigate: navigate, // example
// And the props to pass to the label
...item.props,
})
.then(() => {
// ...
})
.catch((e) => {
// ...
});移除
要清除选择选项,请设置 narration.choices = undefined。
narration.choices = undefined;其他功能
要获取当前叙事步骤(step)中已做出的选择,请使用 narration.queries.alreadyCurrentStepMadeChoices。
import { INTERFACE_DATA_USE_QUERY_KEY } from "@/constants";
import { narration } from "@drincs/pixi-vn";
import { useQuery } from "@tanstack/react-query";
const CHOICE_MENU_OPTIONS_USE_QUERY_KEY = "choice_menu_options_use_query_key";
export function useQueryChoiceMenuOptions() {
return useQuery({
queryKey: [
INTERFACE_DATA_USE_QUERY_KEY,
CHOICE_MENU_OPTIONS_USE_QUERY_KEY,
],
queryFn: async () =>
narration.choices.list?.map((option) => ({
...option,
text:
typeof option.text === "string"
? option.text
: option.text.map((text) => text).join(" "),
alreadyChosen:
narration.queries.alreadyCurrentStepMadeChoices?.find(
(index) => index === option.choiceIndex,
) !== undefined,
})) || [],
});
}你可以通过向 ChoiceInterface 接口添加属性来自定义选择菜单选项。例如,添加 icon 属性以显示图标。
在你的 .d.ts 文件中覆盖 ChoiceInterface 接口:
declare module "@drincs/pixi-vn" {
interface ChoiceInterface {
icon?: string;
}
}