Skip to content

time management

Black Ram edited this page Jul 1, 2024 · 2 revisions

Time Management

Time management means a set of features that allows you to introduce the concept of time into the game and the possibility of managing it in a more or less complex way.

In particular, it was designed to allow the player to introduce 3 types of time management:

  • Realistic: by default the time division is realistic, i.e. 24 hours a day, 7 days a week, 4 weeks a month, 12 months a year.
  • Time slots: Time is divided into time slots, for example morning, afternoon, evening, night.
  • Custom: the time is divided in a personalized way, you can decide how to divide the time.

Usage

All functions are contained in the TimeManager static class.

It is very important to set the time settings before using the nqtr library, otherwise the default settings will be used.

Set time settings

To set the time settings you need to set TimeManager.settings.

For example, to set the time settings for a realistic time management:

import { TimeManager } from '@drincs/nqtr';

TimeManager.settings = {
    defaultTimeSpent: 1,
    maxDayHours: 24,
    minDayHours: 0,
    timeSlots: [
        { name: "Morning", startHour: 5 },
        { name: "Afternoon", startHour: 12 },
        { name: "Evening", startHour: 18 },
        { name: "Night", startHour: 22 }
    ],
    weekDaysNames: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
    weekendStartDay: 6, // Saturday
}

For example, to set the time settings for a time slot time management:

import { TimeManager } from '@drincs/nqtr';

TimeManager.settings = {
    defaultTimeSpent: 6,
    maxDayHours: 24,
    minDayHours: 0,
    timeSlots: [
        { name: "Morning", startHour: 0 },
        { name: "Afternoon", startHour: 6 },
        { name: "Evening", startHour: 18 },
        { name: "Night", startHour: 0 }
    ],
    weekDaysNames: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
    weekendStartDay: 6, // Saturday
}

For example, to set the time settings for a custom time management (for example, school time management):

import { TimeManager } from '@drincs/nqtr';

TimeManager.settings = {
    defaultTimeSpent: 1,
    maxDayHours: 14,
    minDayHours: 8,
    weekDaysNames: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
    weekendStartDay: 6, // since weekendStartDay > weekLength, the weekend doesn't exist
    weekLength: 5,
}

Current Time

  • For get the current hour you can use the TimeManager.currentHour property.
  • For get the current day you can use the TimeManager.currentDay property.
  • For get the current time slots you can use the TimeManager.currentTimeSlot property. If the time slot is not defined, it will return 0 and print a warning.
  • For get the current week day number you can use the TimeManager.currentWeekDayNumber property. For example, if weekLength is 7 and the current day is 8, the current week day number will be 2.
  • For get the current day name you can use the TimeManager.currentDayName property. For example, if weekDaysNames is ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] and the current day is 8, the current day name will be "Tuesday" (because )

Increase day

To move on to the next day you can use the TimeManager.increaseDay() function. This function has 2 parameters:

  • newDayHour: the hour at which the new day starts, if not specified it will be equal to 1.
  • days: the number of days to move forward, if not specified it will be equal to 1.

My suggestion is to create functions to manage the transition from one day to the next, in order to insert your own logic.

For example:

import { TimeManager } from '@drincs/nqtr';

function sleep(wakeUpTime: number) {
    TimeManager.increaseDay(wakeUpTime)
}

Example with more complex logic:

export function sleep(newDayHour: number, notify: (message: string, variant: VariantType) => void): boolean {
    if (getFlag(NOT_CAN_SPEND_TIME_FLAG_KEY)) {
        notify("You can't sleep now", "info")
        return false;
    }
    TimeManager.increaseDay(newDayHour)
    setFlag("weekend", TimeManager.isWeekend)
    setFlag("not_weekend", !TimeManager.isWeekend)
    return true
}

export function wait(timeSpent: number, notify: (message: string, variant: VariantType) => void): boolean {
    if (getFlag(NOT_CAN_SPEND_TIME_FLAG_KEY)) {
        notify("You can't sleep now", "info")
        return false;
    }
    if (TimeManager.currentHour + timeSpent >= 23 || TimeManager.currentHour < 5) {
        notify("You can't wait anymore, you should sleep now", "info")
        return false;
    }
    TimeManager.increaseHour(timeSpent)
    setFlag("weekend", TimeManager.isWeekend)
    setFlag("not_weekend", !TimeManager.isWeekend)
    return true
}

Increase hour

To move on to the next hour you can use the TimeManager.increaseHour() function. This function has 1 parameters:

  • hours: the number of hours to move forward, if not specified it will be equal to TimeManager.settings.defaultTimeSpent.

My suggestion is to create functions to manage the transition from one hour to the next, in order to insert your own logic.

For example:

import { TimeManager } from '@drincs/nqtr';

function wait() {
    if (TimeManager.currentHour >= 23) {
        // the player can't wait anymore
    } else {
        TimeManager.increaseHour()
    }
}

Now is between

To check if the current time is between two times you can use the TimeManager.nowIsBetween() function. This function has 2 parameters:

  • fromHour: the starting hour. If the current time is equal to this hour, the function will return true.
  • toHour: the ending hour.

For example:

import { TimeManager } from '@drincs/nqtr';

if (TimeManager.nowIsBetween(8, 12)) {
    // ...
}

Is Weekend

To check if the current day is a weekend you can use the TimeManager.isWeekend property.

Block the spending time

nqtr it doesn't manage the block of time spent, so you have to manage it yourself.

My suggestion is add a Pixi'VN flag to block the time spent.

For example:

import { TimeManager } from '@drincs/nqtr';
import { getFlag, setFlag } from '@drincs/pixi-vn'

function blockTimeSpent() {
    setFlag('not_can_spend_time', true)
}

function unblockTimeSpent() {
    setFlag('not_can_spend_time', false)
}

function increaseHour() {
    if (getFlag('not_can_spend_time')) {
        return
    }

    TimeManager.increaseHour()
}

Image that changes based on the time period

To change the image based on the time period you can use the TimeManager.currentTimeSlot property.

For example:

// react
import { TimeManager } from '@drincs/nqtr';

function changeBackground() {
    return (
        <img src={`background-${TimeManager.currentTimeSlot}.png`} />
    )
}

Screenshot 2023-10-21 120727

Screenshot 2023-10-21 120807

React Example

export default function Time() {
    const { t } = useTranslation(["translation"]);
    const [hour, setHour] = useState(TimeManager.currentHour);

    return (
        <Stack
            direction="column"
            justifyContent="center"
            alignItems="center"
            spacing={0}
        >
            <Stack
                direction="row"
                justifyContent="center"
                alignItems="center"
                spacing={0}
            >
                <Typography>
                    {hour > 9 ? `${hour}:00` : `0${hour}:00`}
                </Typography>
                <IconButton
                    variant="soft"
                    ariaLabel={t("wait")}
                    onClick={() => {
                        wait(1)
                        setHour(TimeManager.currentHour)
                    }}
                >
                    <AccessTimeIcon />
                </IconButton>
            </Stack>
            <Typography>
                {TimeManager.currentDayName ? t(TimeManager.currentDayName) : ""}
            </Typography>
        </Stack>
    );
}