레이블로 내러티브 흐름 관리하기
Pixi'VN에서 내러티브 노드 (`label`)를 사용해 내러티브 흐름을 제어하는 방법을 설명합니다. call, jump, 내러티브 노드 닫기 함수를 포함합니다.
내러티브 흐름은 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의 두 번째 내러티브 단계 (
step)가 실행됩니다. (대기열에 1개) console.log(3)이 실행되어 startLabel의 두 번째 내러티브 단계 (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;
}