/** * Shared HUD state: what the map shows and what is selected on it. * * Owned by the `(hud)` layout and handed to pages through context (never a module * global, so nothing leaks between sessions). Pages *declare* what the map should show * (`showSystem` / `showGalaxy`) and read selections back; the World scene renders it. */ import { getContext, setContext } from 'svelte'; import { CameraFlight, type V3 } from '$lib/scenes/camera.svelte'; import { frameGalaxy, galaxyPos, systemPos } from '$lib/scenes/coords'; import type { MarketSnapshot } from '$lib/game/trade'; import type { SystemDetail, SystemSummary, Waypoint } from '$lib/types'; const SYSTEM_HOME: V3 = [0, 160, 200]; const KEY = Symbol('hud'); export type MapMode = 'galaxy' | 'system'; export class Hud { mode = $state('galaxy'); /** Galaxy colouring: live events, or federation control. */ overlay = $state<'events' | 'influence'>('events'); /** System shown in system mode. Raw: replaced wholesale on every data refresh. */ system = $state.raw(null); selectedSystem = $state(null); selectedWaypoint = $state(null); /** Ship the camera/rail focus on (the ship console's ship). */ focusShip = $state(null); /** Waypoints or systems to emphasise on the map (hint targets, route stops). */ highlights = $state.raw>(new Set()); /** Known market snapshots for the trade helper (fetched on demand, see loadMarkets). */ markets = $state.raw([]); marketsLoading = $state(false); #marketsAt = 0; readonly galaxyFlight: CameraFlight; /** A fresh flight per system, so entering a system always starts from its overview. */ systemFlight = $state.raw(new CameraFlight(SYSTEM_HOME)); constructor(systems: readonly SystemSummary[]) { const home = frameGalaxy(systems); this.galaxyFlight = new CameraFlight(home.pos, home.look); } showGalaxy() { this.mode = 'galaxy'; } showSystem(detail: SystemDetail) { if (this.system?.symbol !== detail.symbol) { this.systemFlight = new CameraFlight(SYSTEM_HOME); this.selectedWaypoint = null; } this.system = detail; this.mode = 'system'; } /** Select (or toggle off) a system on the galaxy map and fly to it. */ selectSystem(symbol: string | null, systems: readonly SystemSummary[], { toggle = true } = {}) { if (symbol === null || (toggle && this.selectedSystem === symbol)) { this.selectedSystem = null; this.galaxyFlight.goHome(); return; } this.selectedSystem = symbol; const s = systems.find((x) => x.symbol === symbol); if (s) { const [x, , z] = galaxyPos(s); this.galaxyFlight.flyTo([x + 34, 44, z + 34], [x, 0, z]); } } /** Select (or toggle off) a waypoint in the shown system and fly to it. */ selectWaypoint(symbol: string | null, { toggle = true } = {}) { if (symbol === null || (toggle && this.selectedWaypoint === symbol)) { this.selectedWaypoint = null; this.systemFlight.goHome(); return; } this.selectedWaypoint = symbol; const w = this.waypoint(symbol); if (w) { const [x, , z] = systemPos(w); this.systemFlight.flyTo([x + 22, 26, z + 22], [x, 0, z]); } } /** Point the system camera at a waypoint without selecting it. */ lookAt(symbol: string | null | undefined) { const w = this.waypoint(symbol); if (!w) return; const [x, , z] = systemPos(w); this.systemFlight.flyTo([x + 70, 95, z + 95], [x, 0, z]); } waypoint(symbol: string | null | undefined): Waypoint | null { if (!symbol || !this.system) return null; return this.system.waypoints.find((w) => w.data.symbol === symbol)?.data ?? null; } highlight(symbols: Iterable) { this.highlights = new Set(symbols); } /** Fetch market intel unless a fresh copy (< 60 s) is already here. */ async loadMarkets(force = false) { if (this.marketsLoading || (!force && Date.now() - this.#marketsAt < 60_000)) return; this.marketsLoading = true; try { const res = await fetch('/intel/markets'); if (res.ok) { this.markets = (await res.json()) as MarketSnapshot[]; this.#marketsAt = Date.now(); } } finally { this.marketsLoading = false; } } } /** Create the HUD state for this layout; `systems` frame the galaxy camera's home view. */ export function provideHud(systems: readonly SystemSummary[]): Hud { return setContext(KEY, new Hud(systems)); } export function useHud(): Hud { const hud = getContext(KEY); if (!hud) throw new Error('useHud() outside the (hud) layout'); return hud; }