Navigation and map
How to define, organize, and navigate rooms, locations, and maps in Pixi’VN, including custom classes and usage examples.
Nomenclature
Usually in video games, navigation elements used to identify the position of characters are called room, location (e.g. restaurant, house, hospital, etc.), and map. For this reason, names that follow this logic are used.
This does not prevent you from using these elements to manage navigation with the same logic but with different terms. For example, in a game set in space where the player moves between planets, rooms can be the planets, locations can be the solar systems, and maps can be the galaxy.
The navigation system is composed of the following elements:
| Element | Description |
|---|---|
rooms | The core elements of navigation, from which the position of the mc and npc is deduced. |
locations | A container of rooms. |
maps | A container of locations. |
The player can move between rooms. The location and the map are also determined based on the room in which the player is located.
Initialize
To initialize a room/location/map, create a new instance of the RoomBaseModel / LocationBaseModel / MapBaseModel class (or your custom class) and add it to the game room/location/map dictionary when the game is initialized.
Custom classes
It is recommended to use custom classes to adapt rooms/locations/maps to your game as best as possible. The default templates already use custom classes.
RegisteredRooms.add / RegisteredLocations.add / RegisteredMaps.add is required to save the rooms/locations/maps in the game.
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"],
});
export const aliceRoom = new RoomBaseModel("alice_room", mcHome, {
name: "Alice room",
image: "location_aliceroom",
routine: ["alice_sleep"],
});
export const bathroom = new RoomBaseModel("bathroom", mcHome, {
name: "Bathroom",
image: "location_bathroom",
});
export const terrace = new RoomBaseModel("terrace", mcHome, {
name: "Terrace",
image: "location_terrace",
routine: ["alice_smokes"],
});
RegisteredRooms.add(mcRoom, aliceRoom, bathroom, terrace);It is also recommended to set the current room during the start of the game.
import { navigator } from "@drincs/nqtr";
import { newLabel } from "@drincs/pixi-vn";
import { mcRoom } from "@/content/rooms";
export const startLabel = newLabel("start", [
async () => {
navigator.currentRoom = mcRoom;
// ... other initialization logic
},
]);Get
To get a room/location/map by its id, use the RegisteredRooms.get / RegisteredLocations.get / RegisteredMaps.get function.
import { RegisteredRooms } from "@drincs/nqtr";
const mcRoom = RegisteredRooms.get("mc_room");Get all
To get all rooms/locations/maps, use the RegisteredRooms.values / RegisteredLocations.values / RegisteredMaps.values function.
import { RegisteredRooms } from "@drincs/nqtr";
const rooms = RegisteredRooms.values();Navigate
As explained above, the player can navigate between rooms, and the current room determines the current location and map.
To navigate to a room, set the navigator.currentRoom property to the desired room instance. This will automatically update the current location and map based on the room's location and map.
import { navigator } from "@drincs/nqtr";
import { mcRoom } from "@/content/rooms";
navigator.currentRoom = mcRoom;Other features
Templates
In all templates, the Room / Location / Map class is already defined in the file models/Room.ts / models/Location.ts / models/Map.ts. You can use it directly or modify it to suit your needs.
It is recommended to create your own class Room / Location / Map that extends RoomStoredClass / LocationStoredClass / MapStoredClass and "override" the interface RoomInterface / LocationInterface / MapInterface to add, edit, or remove properties or methods.
For example, if you want to create a class Room / Location / Map, you must "override" the interface RoomInterface / LocationInterface / MapInterface to use your properties or methods. (See the file nqtr.d.ts)
Now you can create a class Room / Location / Map that extends RoomStoredClass / LocationStoredClass / MapStoredClass and implements the RoomInterface / LocationInterface / MapInterface. (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 Room.ts / Location.ts / Map.ts)
import {
type ActivityIdType,
type ActivityInterface,
type CommitmentIdType,
type CommitmentInterface,
type LocationInterface,
type OnRunProps,
type RoomInterface,
RoomStoredClass,
} from "@drincs/nqtr";
import type { PixiUIParam, PixiUIProp } from "@/models/ui-elements";
export default class Room extends RoomStoredClass implements RoomInterface {
constructor(
id: string,
location: LocationInterface,
props: {
name: string;
disabled?: boolean | (() => boolean);
hidden?: boolean | (() => boolean);
background: PixiUIParam<Room>;
activities?: (ActivityInterface | ActivityIdType)[];
routine?: (CommitmentInterface | CommitmentIdType)[];
isEntrance?: boolean;
},
) {
super(id, location, {
activities: props.activities || [],
routine: props.routine || [],
});
this.name = props.name;
this._defaultDisabled = props.disabled || false;
this._defaultHidden = props.hidden || false;
this._background = props.background;
this.isEntrance = props.isEntrance || false;
}
readonly name: string;
private readonly _background: PixiUIParam<Room>;
get background(): PixiUIProp {
const background = this._background;
if (typeof background === "function") {
return (runProps: OnRunProps) => background(this, runProps);
}
return background;
}
readonly isEntrance: boolean;
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);
}
}The developer can designate an "index" room for locations.
To set a default room for a location, you can add in your custom class a property isEntrance to the Room class, which will be set to true for the default room of the location, and then use the entrance property of the Location class to get the default room.
For example:
import { navigator, RegisteredRooms } from "@drincs/nqtr";
import { ImageSprite } from "@drincs/pixi-vn";
import { NAVIGATION_ROUTE } from "@/constans";
import Location from "@/models/Location";
import Room from "@/models/Room";
import { mainMap } from "@/content/maps";
export const mcHome = new Location("mc_home", mainMap, {
name: "MC Home",
icon: (location, { navigate }) => {
const icon = new ImageSprite(
{
xAlign: 0.3,
yAlign: 0.2,
height: 120,
width: 120,
eventMode: "static",
cursor: "pointer",
},
"icon_location_home",
);
icon.on("pointerdown", () => {
const entrance = location.entrance;
if (entrance) {
navigator.currentRoom = entrance;
navigate(NAVIGATION_ROUTE);
}
});
icon.load();
return icon;
},
});
export const mcRoom = new Room("mc_room", mcHome, {
name: "MC room",
image: "location_myroom",
isEntrance: true, // This room is the entrance of the location
});
RegisteredRooms.add(mcRoom);