tobogganSe connecter

starfleet-ui

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

59 lignes · 2179 octetsBrut · Historique
1import type { ActiveShip, Waypoint } from '../types';
2import { systemPos, type V3 } from './coords';
3
4/** Height ships hover above the system plane. */
5export const SHIP_ALTITUDE = 6;
6/** How far above the gate a ship appears/vanishes when jumping in or out. */
7export const JUMP_HEIGHT = 60;
8
9export interface ShipPose {
10 position: V3;
11 /** Point the bow faces, or null to keep the current heading. */
12 heading: V3 | null;
13 moving: boolean;
14}
15
16/**
17 * Where a ship should be drawn in a system scene at time `now`.
18 * - In transit inside the system: interpolated along the straight route.
19 * - Jumping in (origin not in this system): descends onto the destination gate.
20 * - Jumping out (destination not in this system): rises off the origin gate.
21 * - Otherwise: hovering at its waypoint (the idle bob is applied by the caller).
22 * Returns null when the ship cannot be placed (unknown waypoint).
23 */
24export function shipPose(ship: ActiveShip, waypoints: readonly Waypoint[], now: number): ShipPose | null {
25 const find = (symbol: string | null | undefined) =>
26 symbol ? (waypoints.find((w) => w.symbol === symbol) ?? null) : null;
27
28 if (ship.status === 'in_transit' && ship.route) {
29 const from = find(ship.route.from);
30 const to = find(ship.route.to);
31 const start = Date.parse(ship.route.departed);
32 const end = Date.parse(ship.route.arrives);
33 const k = end > start ? Math.min(1, Math.max(0, (now - start) / (end - start))) : 1;
34
35 if (from && to) {
36 const [fx, , fz] = systemPos(from);
37 const [tx, , tz] = systemPos(to);
38 return {
39 position: [fx + (tx - fx) * k, SHIP_ALTITUDE, fz + (tz - fz) * k],
40 heading: [tx, SHIP_ALTITUDE, tz],
41 moving: k < 1
42 };
43 }
44 if (to) {
45 const [tx, , tz] = systemPos(to);
46 return { position: [tx, SHIP_ALTITUDE + (1 - k) * JUMP_HEIGHT, tz], heading: null, moving: k < 1 };
47 }
48 if (from) {
49 const [fx, , fz] = systemPos(from);
50 return { position: [fx, SHIP_ALTITUDE + k * JUMP_HEIGHT, fz], heading: null, moving: k < 1 };
51 }
52 return null;
53 }
54
55 const at = find(ship.waypoint);
56 if (!at) return null;
57 const [x, , z] = systemPos(at);
58 return { position: [x, SHIP_ALTITUDE, z], heading: null, moving: false };
59}