starfleet-ui
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#6 · main · 2 min 01 s
0Étoiles
0Forks
0Issues
| 1 | <script lang="ts"> |
| 2 | import { T, useTask, useThrelte } from '@threlte/core'; |
| 3 | import { HTML, Stars, interactivity, type IntersectionEvent } from '@threlte/extras'; |
| 4 | import * as THREE from 'three'; |
| 5 | import { federationColor } from '$lib/fedColors'; |
| 6 | import type { GalaxyEvent, Influence, SystemDetail, SystemSummary } from '$lib/types'; |
| 7 | import CameraRig from './CameraRig.svelte'; |
| 8 | import type { CameraFlight } from './camera.svelte'; |
| 9 | import { galaxyPos } from './coords'; |
| 10 | |
| 11 | interface Props { |
| 12 | flight: CameraFlight; |
| 13 | systems: SystemSummary[]; |
| 14 | details: SystemDetail[]; |
| 15 | events: GalaxyEvent[]; |
| 16 | influence: Record<string, Influence>; |
| 17 | overlay: 'events' | 'influence'; |
| 18 | onSelect: (symbol: string) => void; |
| 19 | selected: string | null; |
| 20 | /** Systems where the player has ships. */ |
| 21 | fleet?: ReadonlySet<string>; |
| 22 | /** Systems to emphasise (route stops, hint targets). */ |
| 23 | highlights?: ReadonlySet<string>; |
| 24 | } |
| 25 | |
| 26 | let { |
| 27 | flight, |
| 28 | systems, |
| 29 | details, |
| 30 | events, |
| 31 | influence, |
| 32 | overlay, |
| 33 | onSelect, |
| 34 | selected, |
| 35 | fleet = new Set(), |
| 36 | highlights = new Set() |
| 37 | }: Props = $props(); |
| 38 | |
| 39 | interactivity(); |
| 40 | // NB: Threlte's <HTML pointerEvents> only applies in `transform` mode, so every label |
| 41 | // below also carries class="pointer-events-none"; without it, labels swallow clicks. |
| 42 | const { camera, dom } = useThrelte(); |
| 43 | |
| 44 | let hovered = $state<string | null>(null); |
| 45 | $effect(() => { |
| 46 | dom.style.cursor = hovered ? 'pointer' : ''; |
| 47 | }); |
| 48 | |
| 49 | const activeEvents = $derived.by(() => { |
| 50 | const now = Date.now(); |
| 51 | return new Map( |
| 52 | events |
| 53 | .filter((e) => new Date(e.starts).getTime() <= now && now < new Date(e.ends).getTime()) |
| 54 | .map((e) => [e.system, e.kind]) |
| 55 | ); |
| 56 | }); |
| 57 | |
| 58 | // Lanes: one LineSegments for the whole network; the old geometry is freed when lanes change. |
| 59 | const laneGeometry = $derived.by(() => { |
| 60 | const bySymbol = new Map(systems.map((s) => [s.symbol, s])); |
| 61 | const seen = new Set<string>(); |
| 62 | const points: number[] = []; |
| 63 | for (const d of details) { |
| 64 | for (const lane of d.lanes) { |
| 65 | const key = [d.symbol, lane.system].sort().join('|'); |
| 66 | if (seen.has(key)) continue; |
| 67 | seen.add(key); |
| 68 | const other = bySymbol.get(lane.system); |
| 69 | if (!other) continue; |
| 70 | points.push(...galaxyPos(d), ...galaxyPos(other)); |
| 71 | } |
| 72 | } |
| 73 | const geo = new THREE.BufferGeometry(); |
| 74 | geo.setAttribute('position', new THREE.Float32BufferAttribute(points, 3)); |
| 75 | return geo; |
| 76 | }); |
| 77 | $effect(() => { |
| 78 | const geo = laneGeometry; |
| 79 | return () => geo.dispose(); |
| 80 | }); |
| 81 | |
| 82 | // Shared geometry for every system; selection scales the mesh instead of rebuilding it. |
| 83 | const sphere = new THREE.SphereGeometry(2.4, 16, 16); |
| 84 | const ring = new THREE.RingGeometry(4.2, 4.8, 48); |
| 85 | const outerRing = new THREE.RingGeometry(5.6, 6.0, 48); |
| 86 | const hitSphere = new THREE.SphereGeometry(8, 8, 8); |
| 87 | const hitMaterial = new THREE.MeshBasicMaterial({ transparent: true, opacity: 0, depthWrite: false }); |
| 88 | $effect(() => () => { |
| 89 | hitSphere.dispose(); |
| 90 | hitMaterial.dispose(); |
| 91 | sphere.dispose(); |
| 92 | ring.dispose(); |
| 93 | outerRing.dispose(); |
| 94 | }); |
| 95 | |
| 96 | // Labels are DOM nodes re-projected every frame, so only show the ones that matter: |
| 97 | // selected, hovered, active events, and charted systems close to the camera. |
| 98 | const LABEL_DISTANCE = 160; |
| 99 | let near = $state<ReadonlySet<string>>(new Set()); |
| 100 | let sinceCheck = 0; |
| 101 | const tmp = new THREE.Vector3(); |
| 102 | useTask((delta) => { |
| 103 | sinceCheck += delta; |
| 104 | if (sinceCheck < 0.25) return; |
| 105 | sinceCheck = 0; |
| 106 | const cam = camera.current.position; |
| 107 | const next = new Set<string>(); |
| 108 | for (const s of systems) { |
| 109 | if (!s.charted) continue; |
| 110 | tmp.set(...galaxyPos(s)); |
| 111 | if (tmp.distanceTo(cam) < LABEL_DISTANCE) next.add(s.symbol); |
| 112 | } |
| 113 | if (next.size !== near.size || [...next].some((x) => !near.has(x))) near = next; |
| 114 | }); |
| 115 | |
| 116 | function colorFor(s: SystemSummary): string { |
| 117 | if (overlay === 'influence') { |
| 118 | const inf = influence[s.symbol]; |
| 119 | if (inf?.controller) return federationColor(inf.controller); |
| 120 | if (inf && inf.standings.length > 0) return '#eab308'; // contested |
| 121 | return s.charted ? '#38bdf8' : '#475569'; |
| 122 | } |
| 123 | const ev = activeEvents.get(s.symbol); |
| 124 | if (ev === 'storm') return '#ef4444'; |
| 125 | if (ev === 'boom') return '#4ade80'; |
| 126 | if (ev === 'surge') return '#fb923c'; |
| 127 | return s.charted ? '#38bdf8' : '#475569'; |
| 128 | } |
| 129 | </script> |
| 130 | |
| 131 | <CameraRig {flight} minDistance={20} maxDistance={900} /> |
| 132 | |
| 133 | <T.AmbientLight intensity={0.7} /> |
| 134 | <T.PointLight position={[0, 200, 0]} intensity={1.2} /> |
| 135 | <Stars radius={900} depth={200} count={6000} factor={10} fade /> |
| 136 | |
| 137 | <T.LineSegments geometry={laneGeometry}> |
| 138 | <T.LineBasicMaterial color="#155e75" transparent opacity={0.55} /> |
| 139 | </T.LineSegments> |
| 140 | |
| 141 | {#each systems as s (s.symbol)} |
| 142 | {@const p = galaxyPos(s)} |
| 143 | {@const color = colorFor(s)} |
| 144 | {@const isSel = selected === s.symbol} |
| 145 | {@const isHover = hovered === s.symbol} |
| 146 | <T.Mesh position={p} geometry={sphere} scale={isSel ? 1.6 : isHover ? 1.3 : 1}> |
| 147 | <T.MeshStandardMaterial {color} emissive={color} emissiveIntensity={s.charted ? 0.9 : 0.35} /> |
| 148 | </T.Mesh> |
| 149 | <!-- Systems are a few pixels wide at galaxy zoom: a larger invisible sphere takes the clicks. --> |
| 150 | <T.Mesh |
| 151 | position={p} |
| 152 | geometry={hitSphere} |
| 153 | material={hitMaterial} |
| 154 | onclick={(e: IntersectionEvent<MouseEvent>) => { |
| 155 | e.stopPropagation(); |
| 156 | onSelect(s.symbol); |
| 157 | }} |
| 158 | onpointerenter={(e: IntersectionEvent<PointerEvent>) => { |
| 159 | e.stopPropagation(); |
| 160 | hovered = s.symbol; |
| 161 | }} |
| 162 | onpointerleave={() => { |
| 163 | if (hovered === s.symbol) hovered = null; |
| 164 | }} |
| 165 | /> |
| 166 | {#if isSel} |
| 167 | <T.Mesh position={p} rotation.x={-Math.PI / 2} geometry={ring}> |
| 168 | <T.MeshBasicMaterial color="#22d3ee" transparent opacity={0.9} side={THREE.DoubleSide} /> |
| 169 | </T.Mesh> |
| 170 | {/if} |
| 171 | {#if fleet.has(s.symbol)} |
| 172 | <T.Mesh position={p} rotation.x={-Math.PI / 2} geometry={outerRing}> |
| 173 | <T.MeshBasicMaterial color="#fbbf24" transparent opacity={0.85} side={THREE.DoubleSide} /> |
| 174 | </T.Mesh> |
| 175 | {/if} |
| 176 | {#if highlights.has(s.symbol)} |
| 177 | <T.Mesh position={[p[0], p[1] + 0.2, p[2]]} rotation.x={-Math.PI / 2} geometry={ring} scale={1.6}> |
| 178 | <T.MeshBasicMaterial color="#a78bfa" transparent opacity={0.9} side={THREE.DoubleSide} /> |
| 179 | </T.Mesh> |
| 180 | {/if} |
| 181 | {#if isSel || isHover || fleet.has(s.symbol) || highlights.has(s.symbol) || activeEvents.has(s.symbol) || near.has(s.symbol)} |
| 182 | <HTML position={[p[0], p[1] + 6, p[2]]} center pointerEvents="none" class="pointer-events-none"> |
| 183 | <div |
| 184 | class="rounded px-1.5 py-0.5 font-mono text-[10px] whitespace-nowrap {isSel |
| 185 | ? 'bg-cyan-950/90 text-cyan-200' |
| 186 | : isHover |
| 187 | ? 'bg-slate-900/90 text-slate-200' |
| 188 | : 'bg-slate-950/70 text-slate-500'}" |
| 189 | > |
| 190 | {s.symbol} |
| 191 | </div> |
| 192 | </HTML> |
| 193 | {/if} |
| 194 | {/each} |
| 195 |