32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
// Real-time clock accumulator and in-game calendar helpers.
|
|
|
|
export function createClock() {
|
|
return { acc: 0 };
|
|
}
|
|
|
|
// Feed it the real elapsed ms; it returns how many in-game days are due.
|
|
// dt is capped (capMs) so a frozen tab doesn't fast-forward the game without limit when it wakes
|
|
// up, and floored at 0: the first animation frame can carry a timestamp slightly before the one the
|
|
// caller recorded, and a negative step would pre-load a whole day.
|
|
export function consumeClock(clock, dtMs, msPerDay, speed, capMs = 1000) {
|
|
clock.acc += Math.max(0, Math.min(dtMs, capMs)) * speed;
|
|
const days = Math.floor(clock.acc / msPerDay);
|
|
clock.acc -= days * msPerDay;
|
|
return days;
|
|
}
|
|
|
|
export function dateOfDay(startDate, day) {
|
|
const [y, m, d] = startDate.split("-").map(Number);
|
|
const date = new Date(Date.UTC(y, m - 1, d + day));
|
|
return {
|
|
year: date.getUTCFullYear(),
|
|
month: date.getUTCMonth() + 1,
|
|
day: date.getUTCDate(),
|
|
weekday: date.getUTCDay(), // 0 = Sunday
|
|
iso: date.toISOString().slice(0, 10),
|
|
};
|
|
}
|
|
|
|
export const weekOfDay = (day, bal) => Math.floor(day / bal.time.daysPerWeek);
|
|
export const quarterOfDay = (day, bal) => Math.floor(day / bal.time.daysPerQuarter);
|