ナラティブノードによる叙述フローの管理
Pixi'VN で `call`、`jump`、`closeLabel` などを使用してナラティブノード(`label`)で叙述フローを制御する方法について説明します。
叙述フローは call、jump、continue、back、closeLabel などの関数によって管理されており、これらはすべて narration オブジェクトから使用できます。
ナラティブノードの実行
ink
このメソッドは ink 構文で使用できます。詳細は こちら をご覧ください。
ナラティブノード(label)を呼び出すには、narration.call 関数を使用します。
import { narration } from "@drincs/pixi-vn";
narration.call(startLabel, {});ナラティブノード(label)を呼び出すと、そのナラティブノード(label)の最初のナラティブステップ(step)が実行されます。呼び出し前に別のナラティブノード(label)が実行中だった場合、呼び出されたナラティブノード(label)が終了した後、前のナラティブノード(label)の残りのナラティブステップ(steps)が再開されます。
例えば、ゲームがナラティブノード(label)A のナラティブステップ(step)5 を実行中に、ナラティブノード(label)B を 呼び出した 場合、ナラティブノード(label)B のすべてのナラティブステップ(steps)が実行された後、ゲームはナラティブノード(label)A のナラティブステップ(step)6 から続行されます。
narration.call は、呼び出されたナラティブノード(label)の最初のナラティブステップ(step)の 結果 を返します。
ナラティブステップ(step)内で narration.call を実行する場合、narration.call の結果を返し、await を使用する必要があります。これにより、履歴が正しく管理されます。
import { narration, newLabel } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
return await narration.call(TestLabel, props);
},
async (props) => await narration.call(TestLabel, props),
]);ナラティブノード(label)へジャンプするには、narration.jump 関数を使用します。
import { narration } from "@drincs/pixi-vn";
narration.jump(startLabel, {});ナラティブノード(label)へジャンプすると、現在のナラティブノード(label)のナラティブステップ(steps)が停止し、新しいナラティブノード(label)のナラティブステップ(steps)が開始されます。
例えば、ゲームがナラティブノード(label)A のナラティブステップ(step)5 を実行中に、ナラティブノード(label)B を 呼び出し、その後ナラティブノード(label)C へ ジャンプ した場合、ナラティブノード(label)C が終了した後、ゲームはナラティブノード(label)A のナラティブステップ(step)6 から続行されます。ナラティブノード(label)C へジャンプすると、ナラティブノード(label)B は閉じられます。
narration.jump は、呼び出されたナラティブノード(label)の 最初のナラティブステップ(step)の結果 を返します。
ナラティブステップ(step)内で narration.jump を実行する場合、narration.jump の結果を返し、await を使用する必要があります。これにより、新しいナラティブノード(label)の最初のナラティブステップ(step)が確実に待機されます。
import { narration } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
return await narration.jump(TestLabel, props);
},
async (props) => await narration.jump(TestLabel, props),
]);ゲームを開始するには、Game.start 関数を使用します。この関数はナラティブノード(label)を呼び出すことと同等ですが、よりクリーンなスタートアップのために使用することを推奨します。
import { Game } from "@drincs/pixi-vn";
Game.start(startLabel, gameProps);ナラティブステップの進行と戻る
進む
UI 画面
「画面をクリックして続行する」の例は、インターフェース例セクションにあります。
次のナラティブステップ(step)を実行するには、narration.continue 関数を使用します。
import { narration } from "@drincs/pixi-vn";
await narration.continue({});narration.continue は非同期なので、.then を使用して、例えばナラティブステップ(step)が完了するまで「次へ」ボタンを無効にするといったことができます。
import { narration } from "@drincs/pixi-vn";
// disable next button
narration.continue({}).then((result) => {
// enable next button
});ナラティブステップ(step)内で narration.continue を呼び出すと、"continue" リクエストはキューに追加され、キューが空になったときに実行されます。
import { narration, newLabel } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
narration.continue(props);
},
]);キューの動作を説明する例を示します:
import { narration, newLabel } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
await narration.call(label2, props);
console.log(1);
},
() => {
console.log(3);
},
]);
const label2 = newLabel("label_02", [
async (props) => {
await narration.continue(props);
console.log(2);
},
]);この例では、出力は 2、1、3 になります。手順は以下のとおりです:
await narration.call(label2, props)が label2 を呼び出し、その最初のナラティブステップ(step)を待機します。(キューに 1 件)- label2 の最初のナラティブステップ(
step)がawait narration.continue(props)を実行しますが、キューが空でないため continue リクエストはキューに追加されます。(キューに 2 件) console.log(2)が実行され、label2 の最初のナラティブステップ(step)が完了します。(キューに 1 件)console.log(1)が実行され、startLabel の最初のナラティブステップ(step)が完了します。(キューに 0 件)- continue リクエストが実行され、startLabel の 2 番目のナラティブステップ(
step)が実行されます。(キューに 1 件) console.log(3)が実行され、startLabel の 2 番目のナラティブステップ(step)が完了します。(キューに 0 件)
narration.canContinue プロパティを使用して、次のナラティブステップ(step)を実行できるか確認できます。
narration.canContinue が false になるのは以下の場合です:
import { narration } from "@drincs/pixi-vn";
function NextButton() {
return (
<button
disabled={!narration.canContinue}
onClick={() => {
narration.continue({});
}}
>
Next
</button>
);
}戻る
UI 画面
戻るボタンの例は インターフェース例 セクションで確認できます。
すべてのナラティブステップ(step)で、システムはゲームの現在の状態を保存します。前のナラティブステップ(step)に戻るには、stepHistory.back 関数を使用します。
stepHistory.canGoBack プロパティを使用して、戻れるかどうかを確認できます。
履歴に復元できるナラティブステップ(steps)がない場合、stepHistory.canGoBack は false になります。
import { stepHistory } from "@drincs/pixi-vn";
if (stepHistory.canGoBack) {
stepHistory.back();
}stepHistory.blockGoBack を呼び出すことで「戻る」を無効にできます。
import { stepHistory } from "@drincs/pixi-vn";
stepHistory.blockGoBack();ナラティブノードを閉じる
現在のナラティブノード(label)を閉じるには、narration.labels.closeCurrent 関数を使用します。
import { narration } from "@drincs/pixi-vn";
narration.labels.closeCurrent();すべてのナラティブノード(labels)を閉じるには、narration.labels.closeAll 関数を使用します。この関数を呼び出した後に何もナラティブノード(label)を呼び出さない場合、ゲームは終了します
import { narration } from "@drincs/pixi-vn";
narration.labels.closeAll();その他の機能
テンプレート
すべてのテンプレートには、この実装がすでに含まれています。
複数の stepHistory.back と narration.continue リクエストを同期的に実行すると、システムは内部でそれらをキューに入れ、必要な処理のみを実行するようにデルタを計算します。
そのため、stepHistory.back と narration.continue 関数を使用してロールバックおよびロールフォワード機能を実装できます。
import { HTML_CANVAS_LAYER_NAME, HTML_UI_LAYER_NAME } from "@/constans";
import { useGameProps } from "@/lib/hooks/props-hooks";
import { GameStatus } from "@/lib/stores/game-status-store";
import { hasScrollableParent } from "@/lib/utils/scroll-utils";
import { type StepLabelProps, stepHistory } from "@drincs/pixi-vn";
import { narration } from "@drincs/pixi-vn/narration";
import { useThrottler } from "@tanstack/react-pacer";
import { useEffect, useRef } from "react";
function isInsideRoot(target: EventTarget | null, selector: string): boolean {
if (!(target instanceof HTMLElement)) return false;
return target.closest(`#${selector}`) !== null;
}
export function useWheelActions({
throttleMs = 300,
minDelta = 20,
}: {
throttleMs?: number;
minDelta?: number;
} = {}) {
const pendingAsync = useRef(0);
const gameProps = useGameProps();
const runAsync = async (
fn: (props: StepLabelProps) => Promise<unknown>,
) => {
try {
pendingAsync.current += 1;
GameStatus.setLoading(pendingAsync.current > 0);
await fn(gameProps);
} finally {
pendingAsync.current -= 1;
GameStatus.setLoading(pendingAsync.current > 0);
if (pendingAsync.current === 0) {
gameProps.invalidateInterfaceData();
}
}
};
const handleWheel = useThrottler(
async (event: WheelEvent) => {
if (
!(
isInsideRoot(event.target, HTML_UI_LAYER_NAME) ||
isInsideRoot(event.target, HTML_CANVAS_LAYER_NAME)
)
)
return;
if (hasScrollableParent(event.target)) return;
// blocca lo scroll nativo
event.preventDefault();
const { deltaY } = event;
// ignora micro-movimenti del trackpad
if (Math.abs(deltaY) < minDelta) return;
if (deltaY < 0) {
// ⬆️ Scroll up
await runAsync(narration.continue.bind(narration));
}
if (deltaY > 0) {
// ⬇️ Scroll down
await runAsync(stepHistory.back.bind(stepHistory));
}
},
{ wait: throttleMs },
);
useEffect(() => {
window.addEventListener("wheel", handleWheel.maybeExecute, {
passive: false,
});
return () => {
window.removeEventListener("wheel", handleWheel.maybeExecute);
};
}, [handleWheel.maybeExecute]);
return null;
}