import type { ActiveShip, Waypoint } from '../types'; import { systemPos, type V3 } from './coords'; /** Height ships hover above the system plane. */ export const SHIP_ALTITUDE = 6; /** How far above the gate a ship appears/vanishes when jumping in or out. */ export const JUMP_HEIGHT = 60; export interface ShipPose { position: V3; /** Point the bow faces, or null to keep the current heading. */ heading: V3 | null; moving: boolean; } /** * Where a ship should be drawn in a system scene at time `now`. * - In transit inside the system: interpolated along the straight route. * - Jumping in (origin not in this system): descends onto the destination gate. * - Jumping out (destination not in this system): rises off the origin gate. * - Otherwise: hovering at its waypoint (the idle bob is applied by the caller). * Returns null when the ship cannot be placed (unknown waypoint). */ export function shipPose(ship: ActiveShip, waypoints: readonly Waypoint[], now: number): ShipPose | null { const find = (symbol: string | null | undefined) => symbol ? (waypoints.find((w) => w.symbol === symbol) ?? null) : null; if (ship.status === 'in_transit' && ship.route) { const from = find(ship.route.from); const to = find(ship.route.to); const start = Date.parse(ship.route.departed); const end = Date.parse(ship.route.arrives); const k = end > start ? Math.min(1, Math.max(0, (now - start) / (end - start))) : 1; if (from && to) { const [fx, , fz] = systemPos(from); const [tx, , tz] = systemPos(to); return { position: [fx + (tx - fx) * k, SHIP_ALTITUDE, fz + (tz - fz) * k], heading: [tx, SHIP_ALTITUDE, tz], moving: k < 1 }; } if (to) { const [tx, , tz] = systemPos(to); return { position: [tx, SHIP_ALTITUDE + (1 - k) * JUMP_HEIGHT, tz], heading: null, moving: k < 1 }; } if (from) { const [fx, , fz] = systemPos(from); return { position: [fx, SHIP_ALTITUDE + k * JUMP_HEIGHT, fz], heading: null, moving: k < 1 }; } return null; } const at = find(ship.waypoint); if (!at) return null; const [x, , z] = systemPos(at); return { position: [x, SHIP_ALTITUDE, z], heading: null, moving: false }; }