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

24 lines
1017 B
TypeScript

import type { SimContent } from "./board";
import type { RealTime } from "./clock";
import { type DispatchRecord, findLocation } from "./dispatch";
import { locationPosition, type Position } from "./positions";
/** Fraction of the trip completed at a real time, clamped to [0, 1]. */
export function dispatchProgress(record: DispatchRecord, now: RealTime): number {
const duration = record.arrivalAt - record.departureAt;
if (duration <= 0) return 1;
return Math.min(1, Math.max(0, (now - record.departureAt) / duration));
}
/** Ship position at a real time: along the straight line from origin to destination. */
export function dispatchPosition(
record: DispatchRecord,
now: RealTime,
content: SimContent,
): Position {
const start = locationPosition(findLocation(record.originId, content));
const end = locationPosition(findLocation(record.destinationId, content));
const t = dispatchProgress(record, now);
return { x: start.x + (end.x - start.x) * t, y: start.y + (end.y - start.y) * t };
}