Routine
Initialisation
To initialize a commitment, create a new instance of the CommitmentBaseModel class (or your custom class) and add it to the game commitment dictionary when the game is initialized.
RegisteredCommitments.add is required to save the commitments in the game.
import { RegisteredCommitments, CommitmentBaseModel } from "@drincs/nqtr";
import { narration } from "@drincs/pixi-vn";
export const aliceSleep = new CommitmentBaseModel("alice_sleep", "alice", {
priority: 1,
timeSlot: {
from: 20,
to: 10,
},
onRun: async (_, props) => {
await props.navigate({ to: "/game/narration" });
await narration.jump("talk_alice_sleep", props);
},
});
export const aliceGoSchool = new CommitmentBaseModel("alice_go_school", "alice", {
timeSlot: {
from: 8,
to: 14,
},
priority: 2,
});
export const aliceSmokes = new CommitmentBaseModel("alice_smokes", "alice", {
timeSlot: {
from: 10,
to: 20,
},
onRun: async (_, props) => {
await props.navigate({ to: "/game/narration" });
await narration.jump("alice_talk_menu", props);
},
});
RegisteredCommitments.add(aliceSleep, aliceGoSchool, aliceSmokes);Link
For an commitment to be linked to a room, the room must be included in the Commitment instance. The relationship between a commitment and a room is one-to-many, meaning that a rooms can be linked to multiple commitments, but a commitment can only be linked to one room (a character cannot be in two places at the same time).
There are two ways to do this:
During game initialization
For a commitment to be available to the player, it must be linked to a room.
import { RegisteredRooms, RoomBaseModel } from "@drincs/nqtr";
import { mcHome } from "@/content/locations";
export const aliceRoom = new RoomBaseModel("alice_room", mcHome, {
name: "Alice room",
image: "location_aliceroom",
routine: ["alice_sleep"],
});
RegisteredRooms.add(aliceRoom);During the game
You can also link a commitment to a room during the game. This feature is useful if you want to temporarily add a commitment. The list of routine added this way will be included in your game saves.
To link a commitment to a room during the game, you can use the room.addCommitment method.
import { newLabel, showImageContainer } from "@drincs/pixi-vn";
import { terrace } from "@/content/rooms";
export const startLabel = newLabel("start", [
async () => {
terrace.addCommitment("alice_quest_talk");
},
]);Unlink
To detach a commitment from a room, you must first consider how it was linked.
If it was linked during the game initialization, it cannot be removed. However, you can add another commitment with the same character at the same time slot, so the character will be occupied with the new commitment and therefore will no longer be present in the previous room. By default, priority is given to the commitment added during gameplay over the one added during initialization, but this behavior can be modified using the priority property.
If it was linked during gameplay, it can be removed using the room.removeCommitment method.
import { routine } from "@drincs/nqtr";
routine.remove("alice_smokes");Obtenir
To get a commitment by its id, use the RegisteredCommitments.get function.
import { RegisteredCommitments } from "@drincs/nqtr";
const aliceWorks = RegisteredCommitments.get("alice_works");Get all
To get all commitments, use the RegisteredCommitments.values function.
import { RegisteredCommitments } from "@drincs/nqtr";
const commitments = RegisteredCommitments.values();Autres fonctionnalités
Modèles
In all templates, the Commitment class is already defined in the file models/Commitment.ts. Vous pouvez l'utiliser directement ou la modifier selon vos besoins.
It is recommended to create your own class Commitment that extends CommitmentStoredClass and "override" the interface CommitmentInterface to add, edit, or remove properties or methods.
For example, if you want to create a class Commitment, you must "override" the interface CommitmentInterface to use your properties or methods. (See the file nqtr.d.ts)
Now you can create a class Commitment that extends CommitmentStoredClass and implements the CommitmentInterface. (Pour plus d'informations sur la création d'une classe en TypeScript, lisez la documentation officielle)
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 Commitment.ts)
import {
ActiveScheduling,
CommitmentInterface,
CommitmentStoredClass,
CommitmentStoredClassProps,
OnRunAsyncFunction,
OnRunEvent,
OnRunProps,
} from "@drincs/nqtr";
import { CharacterInterface } from "@drincs/pixi-vn";
import {
PixiUIParam,
PixiUIProp,
ReactUIParam,
ReactUIProp,
} from "@/models/ui-elements";
export default class Commitment
extends CommitmentStoredClass
implements CommitmentInterface
{
constructor(
id: string,
characters: CharacterInterface | CharacterInterface[],
props: {
name?: string;
image?: PixiUIParam<Commitment>;
background?: PixiUIParam<Commitment>;
icon?: ReactUIParam<Commitment>;
onRun?: OnRunEvent<CommitmentInterface>;
disabled?: boolean | (() => boolean);
hidden?: boolean | (() => boolean);
} & CommitmentStoredClassProps,
) {
characters = Array.isArray(characters) ? characters : [characters];
super(id, characters, props.onRun, props);
this.name = props.name || "";
this._image = props.image;
this._background = props.background;
this._icon = props.icon;
this._defaultDisabled = props.disabled || false;
this._defaultHidden = props.hidden || false;
}
readonly name: string;
private readonly _image?: PixiUIParam<Commitment>;
get sprite(): PixiUIProp | undefined {
const image = this._image;
if (typeof image === "function") {
return (runProps: OnRunProps) => image(this, runProps);
}
return image;
}
private readonly _background?: PixiUIParam<Commitment>;
get background(): PixiUIProp | undefined {
const background = this._background;
if (typeof background === "function") {
return (runProps: OnRunProps) => background(this, runProps);
}
return background;
}
private readonly _icon?: ReactUIParam<Commitment>;
get icon(): ReactUIProp | undefined {
const icon = this._icon;
if (typeof icon === "function") {
return (runProps: OnRunProps) => icon(this, runProps);
}
return icon;
}
private _defaultDisabled: boolean | (() => boolean) = false;
get disabled(): boolean {
let value =
this.getStorageProperty<boolean>("disabled") ||
this._defaultDisabled;
if (typeof value === "function") {
return value();
}
return value;
}
set disabled(value: boolean) {
this.setStorageProperty("disabled", value);
}
private _defaultHidden: boolean | (() => boolean) = false;
get hidden(): boolean {
let value =
this.getStorageProperty<boolean>("hidden") || this._defaultHidden;
if (typeof value === "function") {
return value();
}
return value;
}
set hidden(value: boolean) {
this.setStorageProperty("hidden", value);
}
override isActive(options?: ActiveScheduling): boolean {
if (this.hidden) {
return false;
}
return super.isActive(options);
}
override get run(): OnRunAsyncFunction {
return async (runProps: OnRunProps) => {
const res = await super.run(runProps);
await runProps.invalidateInterfaceData();
return res;
};
}
}