deadhead-space-freight-tycoon/packages/sim/src/clock.ts

27 lines
1.0 KiB
TypeScript

import type { GameConfig } from "@deadhead/content";
/**
* Times are milliseconds since the Unix epoch. A real time is a wall-clock
* instant; a game time is the instant of the in-game (ephemeris) date, so
* `new Date(gameTime)` is the in-game date.
*/
export type RealTime = number;
export type GameTime = number;
export const MS_PER_MINUTE = 60_000;
export const MS_PER_HOUR = 3_600_000;
/** Converts a real instant to the in-game instant it maps to. */
export function realToGame(real: RealTime, config: GameConfig): GameTime {
const realStart = Date.parse(config.epoch.realStart);
const gameStart = Date.parse(config.epoch.ephemerisDate);
return gameStart + (real - realStart) * config.timeCompression;
}
/** Converts an in-game instant back to the real instant it maps to. */
export function gameToReal(game: GameTime, config: GameConfig): RealTime {
const realStart = Date.parse(config.epoch.realStart);
const gameStart = Date.parse(config.epoch.ephemerisDate);
return realStart + (game - gameStart) / config.timeCompression;
}