First commit, done on M6 from the PLAN.MD
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// 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 so a background tab doesn't fast-forward the game on return, 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) {
|
||||
clock.acc += Math.max(0, Math.min(dtMs, 1000)) * 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);
|
||||
Reference in New Issue
Block a user