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

34 lines
1.2 KiB
TypeScript

import type { SimContent } from "./board";
import { type RealTime, realToGame } 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: a straight line from the origin's position at
* departure to the destination's position at arrival.
*/
export function dispatchPosition(
record: DispatchRecord,
now: RealTime,
content: SimContent,
): Position {
const { config } = content;
const start = locationPosition(
findLocation(record.originId, content),
realToGame(record.departureAt, config),
);
const end = locationPosition(
findLocation(record.destinationId, content),
realToGame(record.arrivalAt, config),
);
const t = dispatchProgress(record, now);
return { x: start.x + (end.x - start.x) * t, y: start.y + (end.y - start.y) * t };
}