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.

67 lignes · 2081 octetsBrut · Historique
1<script lang="ts">
2 import { T, useTask } from '@threlte/core';
3 import { HTML } from '@threlte/extras';
4 import * as THREE from 'three';
5 import { shipModel } from '$lib/assets/manifest';
6 import { isDisabled, type ActiveShip, type Envelope, type Waypoint } from '$lib/types';
7 import Model from './Model.svelte';
8 import { damp } from './motion';
9 import { shipPose } from './shipPath';
10
11 interface Props {
12 ship: Envelope<ActiveShip>;
13 waypoints: Envelope<Waypoint>[];
14 }
15
16 let { ship, waypoints }: Props = $props();
17
18 const s = $derived(ship.data);
19 const src = $derived(shipModel(s.symbol));
20 const wps = $derived(waypoints.map((w) => w.data));
21 const disabled = $derived(isDisabled(s));
22
23 let group = $state<THREE.Group>();
24 let engine = $state<THREE.PointLight>();
25 const target = new THREE.Vector3();
26 const look = new THREE.Vector3();
27 let placed = false;
28
29 useTask((delta) => {
30 if (!group) return;
31 const now = Date.now();
32 const pose = shipPose(s, wps, now);
33 if (!pose) return;
34
35 target.set(...pose.position);
36 if (!pose.moving && !disabled) target.y += Math.sin(now / 900) * 0.6; // idle bob
37
38 if (!placed) {
39 // first frame: appear where the ship is, don't fly in from the origin
40 group.position.copy(target);
41 placed = true;
42 } else {
43 group.position.lerp(target, damp(8, delta));
44 }
45 if (pose.heading) group.lookAt(look.set(...pose.heading));
46 if (engine) engine.intensity = disabled ? 0 : pose.moving ? 14 : 4;
47 });
48</script>
49
50<T.Group bind:ref={group}>
51 {#key src}
52 <Model {src} scale={3.2} />
53 {/key}
54 <T.PointLight bind:ref={engine} color="#f59e0b" intensity={4} distance={14} position={[0, 0, 2]} />
55 {#if disabled}
56 <T.PointLight color="#ef4444" intensity={10} distance={12} position={[0, 2, 0]} />
57 {/if}
58 <HTML position={[0, 4, 0]} center pointerEvents="none" class="pointer-events-none">
59 <div
60 class="rounded px-1.5 py-0.5 font-mono text-[10px] whitespace-nowrap {disabled
61 ? 'bg-red-950/80 text-red-200'
62 : 'bg-amber-950/80 text-amber-200'}"
63 >
64 {s.symbol}{disabled ? ' · stranded' : ''}
65 </div>
66 </HTML>
67</T.Group>