Activity
The activity are actions the player can take, and they can be linked to a navigation element. The developer will decide how the player interacts with them.
초기화
To initialize a activity, create a new instance of the ActivityBaseModel class (or your custom class) and add it to the game activity dictionary when the game is initialized.
RegisteredActivities.add is required to save the activities in the game.
import {
ActivityBaseModel,
RegisteredActivities,
timeTracker,
} from "@drincs/nqtr";
import { RegisteredActivities, timeTracker } from "@drincs/nqtr";
import { narration } from "@drincs/pixi-vn";
export const bed = new ActivityBaseModel(
"bed",
async (_, props) => {
await props.navigate({ to: "/game/narration" });
if (timeTracker.nowIsBetween(5, 22)) {
await narration.jump("nap", props);
} else {
await narration.jump("sleep", props);
}
},
{
name: "bed",
},
);
export const orderProduct = new ActivityBaseModel(
"order_product",
async (_, props) => {
await props.navigate({ to: "/game/narration" });
await narration.jump("order_product", props);
},
{
name: "order_product",
},
);
export const takeProduct = new ActivityBaseModel(
"take_product",
async (_, props) => {
await props.navigate({ to: "/game/narration" });
await narration.jump("take_product", props);
},
{
name: "take_product",
},
);
RegisteredActivities.add(bed, orderProduct, takeProduct);Link
For an activity to be available to the player, it must be linked to a navigation element. The relationship between an activity and a navigation element is many-to-many, meaning that an activity can be linked to multiple navigation elements and a navigation element can have multiple activities associated with it.
There are two ways to do this:
During game initialization
When the game starts, a list of related activities will be defined. This list will not be saved when the game is saved. Therefore, the developer can modify this list from one version of the game to another without having to worry about migrations for older versions.
import { RegisteredRooms, RoomBaseModel } from "@drincs/nqtr";
import { mcHome } from "@/content/locations";
export const mcRoom = new RoomBaseModel("mc_room", mcHome, {
name: "MC room",
image: "location_myroom",
activities: ["bed"],
});
RegisteredRooms.add(mcRoom);During the game
You can also link an activity to a navigation element during the game. This feature is useful if you want to temporarily add an activity. The list of activities added this way will be included in your game saves.
To link an activity to a navigation element during the game, you can use the room.addActivity method.
import { newLabel, showImageContainer } from "@drincs/pixi-vn";
import { mcRoom } from "@/content/rooms";
export const startLabel = newLabel("start", [
async () => {
mcRoom.addActivity("order_product");
},
]);Unlink
To unlink an activity from a navigation element, you can use the room.removeActivity method.
import { mcRoom } from "@/content/rooms";
mcRoom.removeActivity("order_product");가져오기
To get a activity by its id, use the RegisteredActivities.get function.
import { RegisteredActivities } from "@drincs/nqtr";
const orderProduct = RegisteredActivities.get("order_product");Get all
To get all activities, use the RegisteredActivities.values function.
import { RegisteredActivities } from "@drincs/nqtr";
const activities = RegisteredActivities.values();기타 기능
템플릿
In all templates, the Activity class is already defined in the file models/Activity.ts. 직접 사용하거나 필요에 맞게 수정할 수 있습니다.
It is recommended to create your own class Activity that extends ActivityStoredClass and "override" the interface ActivityInterface to add, edit, or remove properties or methods.
For example, if you want to create a class Activity, you must "override" the interface ActivityInterface to use your properties or methods. (See the file nqtr.d.ts)
Now you can create a class Activity that extends ActivityStoredClass and implements the ActivityInterface. (TypeScript에서 클래스를 만드는 방법에 대한 자세한 내용은 공식 문서를 참조하세요)
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 Activity.ts)
import {
type ActiveScheduling,
type ActivityInterface,
ActivityStoredClass,
type ActivityStoredClassProps,
type OnRunAsyncFunction,
type OnRunEvent,
type OnRunProps,
} from "@drincs/nqtr";
import type { PixiUIParam, PixiUIProp, ReactUIParam, ReactUIProp } from "@/models/ui-elements";
export default class Activity extends ActivityStoredClass implements ActivityInterface {
constructor(
id: string,
onRun: OnRunEvent<ActivityInterface>,
props: {
name: string;
sprite?: PixiUIParam<Activity>;
icon?: ReactUIParam<Activity>;
disabled?: boolean | (() => boolean);
hidden?: boolean | (() => boolean);
} & ActivityStoredClassProps,
) {
super(id, onRun, props);
this.name = props.name || "";
this._sprite = props.sprite;
this._icon = props.icon;
this._defaultDisabled = props.disabled || false;
this._defaultHidden = props.hidden || false;
}
readonly name: string;
private readonly _sprite?: PixiUIParam<Activity>;
get sprite(): PixiUIProp | undefined {
const sprite = this._sprite;
if (typeof sprite === "function") {
return (runProps: OnRunProps) => sprite(this, runProps);
}
return sprite;
}
private readonly _icon?: ReactUIParam<Activity>;
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 {
const 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 {
const 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;
};
}
}