Seasons based on a calendar

This commit is contained in:
2026-10-09 22:09:21 -03:00
parent 4d5700f0bf
commit 7fe9c15b67
12 changed files with 88 additions and 33 deletions
+7 -4
View File
@@ -12,8 +12,6 @@ export const TWILIGHT_TICKS = 60 * TICKS_PER_SECOND;
// in game hours are longer during the day than at night, since both halves are 12 hours
const DAY_TICKS_PER_HOUR = DAY_TICKS / 12;
const NIGHT_TICKS_PER_HOUR = NIGHT_TICKS / 12;
// from midnight to 06:00, to count days from midnight
const TICKS_BEFORE_SIX = 6 * NIGHT_TICKS_PER_HOUR;
// named times of day, as ticks into the cycle
export const TIMES_OF_DAY: Record<string, number> = {
@@ -33,13 +31,18 @@ export function is_day(time: number): boolean {
return time_of_day(time) < DAY_TICKS;
}
// the day number, starting at 1 and going up at midnight, and the time on a 24 hour clock
// days since the world started, going up at 06:00: 0 on the first day. crops grow and seasons change on it
export function farming_day(time: number): number {
return Math.floor(time / CYCLE_TICKS);
}
// the day number, the farming day counted from 1 so it also goes up at 06:00, and the time on a 24 hour clock
export function clock(time: number): { day: number; hour: number; minute: number } {
const t = time_of_day(time);
const hours = t < DAY_TICKS ? 6 + t / DAY_TICKS_PER_HOUR : 18 + (t - DAY_TICKS) / NIGHT_TICKS_PER_HOUR;
const total_minutes = Math.floor(hours * 60) % (24 * 60);
return {
day: Math.floor((time + TICKS_BEFORE_SIX) / CYCLE_TICKS) + 1,
day: farming_day(time) + 1,
hour: Math.floor(total_minutes / 60),
minute: total_minutes % 60,
};