Quests
The quest system is a way to create and manage quests in your game. Quests can have multiple stages, and players can complete them to earn rewards or progress in the story.
Initialize
In an existing Pixi’VN template you can add the quest view (quest menu, translations and NQTR setup) with the command npm create pixi-vn@latest add quest-system.
To initialize a quest, create a new instance of the QuestBaseModel class (or your custom class) and add it to the game quest dictionary when the game is initialized.
RegisteredQuests.add is required to save the quests in the game.
import { mcRoom, terrace } from "@/content/rooms";
import { RegisteredQuests } from "@drincs/nqtr";
export const aliceQuest = new QuestBaseModel(
"aliceQuest",
[
// stages
new StageBaseModel("talk_alice1", {
onStart: () => {
terrace.addCommitment("alice_quest_talk");
},
name: "Talk to Alice",
description: "Talk to Alice on the terrace",
}),
new StageBaseModel("order_products", {
onStart: () => {
mcRoom.addActivity("order_product");
},
name: "Order products",
description: "Order the products with your PC",
}),
new StageBaseModel("take_products", {
onStart: (_, { toast }) => {
terrace.addActivity("take_product");
toast("You can take the products on the Terrace");
},
name: "Take products",
description: "Take products on the Terrace",
requestDescriptionToStart: "Wait for the products you ordered to arrive (2 day)",
deltaDateRequired: 2,
}),
new StageBaseModel("talk_alice2", {
name: "Talk to Alice",
description: "Talk to Alice on the terrace",
}),
],
{
// props
name: "Help Alice",
description:
'To learn more about how the repo works, Talk to Alice. \nGoing when she is there will automatically start an "Event" (see aliceQuest.tsx to learn more). \nAfter that an action will be added to open the pc, in MC room. \n\n(during the quest you can talk to Alice and you will see her talking during the quests of the same Quest)',
image: "alice_terrace0A",
onStart: (quest, { toast, uiTransition }) => {
toast(uiTransition("notify_quest_is_started", { quest: quest.name }));
},
onContinue: (stage, { toast, uiTransition }) => {
toast(uiTransition("notify_quest_is_updated", { quest: stage.name }));
},
},
);
RegisteredQuests.add(aliceQuest);Start
To start a quest, you can use the quest.start method of the Quest class. This method will set the current stage to the first stage of the quest and trigger the onStart action defined in the quest.
import { newLabel } from "@drincs/pixi-vn";
import { aliceQuest } from "@/content/quests/alice.quest";
export const startLabel = newLabel("start", [
async (props) => {
await aliceQuest.start(props);
},
]);Continue
To go to the next stage of a quest, you can use the quest.continue method of the Quest class. This method will set the current stage to the next stage of the quest and trigger the onContinue action defined in the quest.
import { newLabel } from "@drincs/pixi-vn";
import { aliceQuest } from "@/content/quests/alice.quest";
export const startLabel = newLabel("start", [
async (props) => {
await aliceQuest.continue(props);
},
]);To go to the next stage of a quest only if the current stage is completed, you can use the quest.advanceIfCompleted method of the Quest class. This method will check if the current stage is completed and then set the current stage to the next stage of the quest and trigger the onContinue action defined in the quest.
import { newLabel } from "@drincs/pixi-vn";
import { aliceQuest } from "@/content/quests/alice.quest";
export const startLabel = newLabel("start", [
async (props) => {
await aliceQuest.advanceIfCompleted(props);
},
]);Get
To get a quest by its id, use the RegisteredQuests.get function.
import { RegisteredQuests } from "@drincs/nqtr";
const aliceQuest = RegisteredQuests.get("aliceQuest");Get all
To get all quests, use the RegisteredQuests.values function.
import { RegisteredQuests } from "@drincs/nqtr";
const quests = RegisteredQuests.values();Other features
Templates
In all templates, the Quest class is already defined in the file models/Quest.ts. You can use it directly or modify it to suit your needs.
It is recommended to create your own class Quest that extends QuestStoredClass and "override" the interface QuestInterface to add, edit, or remove properties or methods.
For example, if you want to create a class Quest, you must "override" the interface QuestInterface to use your properties or methods. (See the file nqtr.d.ts)
Now you can create a class Quest that extends QuestStoredClass and implements the QuestInterface. (For more information on how to create a class in TypeScript, read the official documentation)
To create a property that stores its value in the game storage, you can create Getters/Setters and use the this.getStorageProperty() / this.setStorageProperty() methods. (See the file Quest.ts)
import {
QuestInterface,
QuestStoredClass,
QuestStoredClassProps,
StageInterface,
} from "@drincs/nqtr";
export default class Quest extends QuestStoredClass implements QuestInterface {
constructor(
id: string,
_stages: StageInterface[],
props: {
name?: string;
description?: string;
image?: string;
inDevelopment?: boolean;
} & QuestStoredClassProps,
) {
super(id, _stages, props);
this.name = props.name || "";
this.description = props.description || "";
this.image = props.image;
this.inDevelopment = props.inDevelopment || false;
}
readonly name: string;
readonly description: string;
readonly image?: string;
readonly inDevelopment: boolean;
}To fail a quest, you can use the failed property of the QuestBaseModel class. This property is a boolean that indicates whether the quest has been failed or not. You can set it to true to fail the quest.
import { newLabel } from "@drincs/pixi-vn";
import { aliceQuest } from "@/content/quests/alice.quest";
export const startLabel = newLabel("start", [
async (props) => {
aliceQuest.failed = true;
},
]);To query the state of a quest, you can use the following properties and methods:
quest.currentStageIndex: the index of the current stage of the quest. It starts at 0 and goes up to stages.length - 1. If the quest is completed, it will be equal to stages.length.
started: a boolean that indicates whether the quest has been started or not.completed: a boolean that indicates whether the quest has been completed or not.failed: a boolean that indicates whether the quest has been failed or not.
import { newLabel } from "@drincs/pixi-vn";
import { aliceQuest } from "@/content/quests/alice.quest";
export const startLabel = newLabel("start", [
async () => {
await showImage(BACKGROUND_ID, "alice_terrace0At");
narration.dialogue = {
character: alice,
text: "Hi, what do you want to talk about?",
};
let optionsMenu: StoredChoiceInterface[] = [];
if (aliceQuest.started) {
optionsMenu.push(newChoiceOption("Quest started", startLabel, {}));
}
if (aliceQuest.completed) {
optionsMenu.push(
newChoiceOption("Quest completed", startLabel, {}),
);
}
if (aliceQuest.failed) {
optionsMenu.push(newChoiceOption("Quest failed", startLabel, {}));
}
if (aliceQuest.currentStageIndex === 3) {
optionsMenu.push(
newChoiceOption("Quest not started", startLabel, {}),
);
}
narration.choices = [...optionsMenu, newCloseChoiceOption("Cancel")];
},
]);