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.

131 lignes · 4561 octetsBrut · Historique
1/**
2 * Shared HUD state: what the map shows and what is selected on it.
3 *
4 * Owned by the `(hud)` layout and handed to pages through context (never a module
5 * global, so nothing leaks between sessions). Pages *declare* what the map should show
6 * (`showSystem` / `showGalaxy`) and read selections back; the World scene renders it.
7 */
8import { getContext, setContext } from 'svelte';
9import { CameraFlight, type V3 } from '$lib/scenes/camera.svelte';
10import { frameGalaxy, galaxyPos, systemPos } from '$lib/scenes/coords';
11import type { MarketSnapshot } from '$lib/game/trade';
12import type { SystemDetail, SystemSummary, Waypoint } from '$lib/types';
13
14const SYSTEM_HOME: V3 = [0, 160, 200];
15const KEY = Symbol('hud');
16
17export type MapMode = 'galaxy' | 'system';
18
19export class Hud {
20 mode = $state<MapMode>('galaxy');
21 /** Galaxy colouring: live events, or federation control. */
22 overlay = $state<'events' | 'influence'>('events');
23 /** System shown in system mode. Raw: replaced wholesale on every data refresh. */
24 system = $state.raw<SystemDetail | null>(null);
25 selectedSystem = $state<string | null>(null);
26 selectedWaypoint = $state<string | null>(null);
27 /** Ship the camera/rail focus on (the ship console's ship). */
28 focusShip = $state<string | null>(null);
29 /** Waypoints or systems to emphasise on the map (hint targets, route stops). */
30 highlights = $state.raw<ReadonlySet<string>>(new Set());
31
32 /** Known market snapshots for the trade helper (fetched on demand, see loadMarkets). */
33 markets = $state.raw<MarketSnapshot[]>([]);
34 marketsLoading = $state(false);
35 #marketsAt = 0;
36
37 readonly galaxyFlight: CameraFlight;
38 /** A fresh flight per system, so entering a system always starts from its overview. */
39 systemFlight = $state.raw(new CameraFlight(SYSTEM_HOME));
40
41 constructor(systems: readonly SystemSummary[]) {
42 const home = frameGalaxy(systems);
43 this.galaxyFlight = new CameraFlight(home.pos, home.look);
44 }
45
46 showGalaxy() {
47 this.mode = 'galaxy';
48 }
49
50 showSystem(detail: SystemDetail) {
51 if (this.system?.symbol !== detail.symbol) {
52 this.systemFlight = new CameraFlight(SYSTEM_HOME);
53 this.selectedWaypoint = null;
54 }
55 this.system = detail;
56 this.mode = 'system';
57 }
58
59 /** Select (or toggle off) a system on the galaxy map and fly to it. */
60 selectSystem(symbol: string | null, systems: readonly SystemSummary[], { toggle = true } = {}) {
61 if (symbol === null || (toggle && this.selectedSystem === symbol)) {
62 this.selectedSystem = null;
63 this.galaxyFlight.goHome();
64 return;
65 }
66 this.selectedSystem = symbol;
67 const s = systems.find((x) => x.symbol === symbol);
68 if (s) {
69 const [x, , z] = galaxyPos(s);
70 this.galaxyFlight.flyTo([x + 34, 44, z + 34], [x, 0, z]);
71 }
72 }
73
74 /** Select (or toggle off) a waypoint in the shown system and fly to it. */
75 selectWaypoint(symbol: string | null, { toggle = true } = {}) {
76 if (symbol === null || (toggle && this.selectedWaypoint === symbol)) {
77 this.selectedWaypoint = null;
78 this.systemFlight.goHome();
79 return;
80 }
81 this.selectedWaypoint = symbol;
82 const w = this.waypoint(symbol);
83 if (w) {
84 const [x, , z] = systemPos(w);
85 this.systemFlight.flyTo([x + 22, 26, z + 22], [x, 0, z]);
86 }
87 }
88
89 /** Point the system camera at a waypoint without selecting it. */
90 lookAt(symbol: string | null | undefined) {
91 const w = this.waypoint(symbol);
92 if (!w) return;
93 const [x, , z] = systemPos(w);
94 this.systemFlight.flyTo([x + 70, 95, z + 95], [x, 0, z]);
95 }
96
97 waypoint(symbol: string | null | undefined): Waypoint | null {
98 if (!symbol || !this.system) return null;
99 return this.system.waypoints.find((w) => w.data.symbol === symbol)?.data ?? null;
100 }
101
102 highlight(symbols: Iterable<string>) {
103 this.highlights = new Set(symbols);
104 }
105
106 /** Fetch market intel unless a fresh copy (< 60 s) is already here. */
107 async loadMarkets(force = false) {
108 if (this.marketsLoading || (!force && Date.now() - this.#marketsAt < 60_000)) return;
109 this.marketsLoading = true;
110 try {
111 const res = await fetch('/intel/markets');
112 if (res.ok) {
113 this.markets = (await res.json()) as MarketSnapshot[];
114 this.#marketsAt = Date.now();
115 }
116 } finally {
117 this.marketsLoading = false;
118 }
119 }
120}
121
122/** Create the HUD state for this layout; `systems` frame the galaxy camera's home view. */
123export function provideHud(systems: readonly SystemSummary[]): Hud {
124 return setContext(KEY, new Hud(systems));
125}
126
127export function useHud(): Hud {
128 const hud = getContext<Hud | undefined>(KEY);
129 if (!hud) throw new Error('useHud() outside the (hud) layout');
130 return hud;
131}