106 lines
3.8 KiB
TypeScript
106 lines
3.8 KiB
TypeScript
import type { Orbit, Snapshot } from "../api";
|
|
|
|
export type Vec2 = { x: number; y: number };
|
|
type OrbitSlot = { radius: number; angle: number; speed: number };
|
|
|
|
export const SCENE_WIDTH = 660;
|
|
export const SCENE_HEIGHT = 480;
|
|
export const ORBIT_TILT = 0.68;
|
|
|
|
const shells = [
|
|
{ radius: 128, count: 10, step: 3, speed: 0.047 },
|
|
{ radius: 190, count: 18, step: 7, speed: 0.033 },
|
|
{ radius: 260, count: 28, step: 11, speed: 0.023 },
|
|
];
|
|
|
|
const slots = Array.from({ length: 28 }, (_, index) =>
|
|
shells.flatMap(({ radius, count, step, speed }, shell) =>
|
|
index < count
|
|
? [{ radius, speed, angle: ((index * step) / count) * 2 * Math.PI - 1.2 + shell * 0.9 }]
|
|
: [],
|
|
),
|
|
).flat();
|
|
|
|
export const orbitRadii = shells.map(({ radius }) => radius);
|
|
export const orbitCapacity = slots.length;
|
|
|
|
export function assignOrbitSlots(
|
|
ids: string[],
|
|
previous = new Map<string, OrbitSlot>(),
|
|
selected?: string,
|
|
): Map<string, OrbitSlot> {
|
|
const currentIds = new Set(ids);
|
|
const next = new Map([...previous].filter(([id]) => currentIds.has(id)));
|
|
const occupied = new Set(next.values());
|
|
const free = slots.filter((slot) => !occupied.has(slot));
|
|
|
|
// shortcut: overview has a fixed number of slots, add level of detail if dense fleets need spatial navigation.
|
|
// Reserve the final slot for inspection when the overview is full.
|
|
const selectedSlot = slots[slots.length - 1];
|
|
for (const [id, slot] of next) {
|
|
if (slot === selectedSlot && id !== selected) next.delete(id);
|
|
}
|
|
const available = free.filter((slot) => slot !== selectedSlot);
|
|
for (const id of ids) {
|
|
if (next.has(id)) continue;
|
|
const slot = available.shift();
|
|
if (!slot) break;
|
|
next.set(id, slot);
|
|
}
|
|
if (selected && currentIds.has(selected) && !next.has(selected)) {
|
|
next.set(selected, selectedSlot);
|
|
}
|
|
return next;
|
|
}
|
|
|
|
export function orbitPose(slot: OrbitSlot, time: number): Vec2 {
|
|
const angle = slot.angle + time * slot.speed;
|
|
return {
|
|
x: Math.cos(angle) * slot.radius,
|
|
y: Math.sin(angle) * slot.radius * ORBIT_TILT,
|
|
};
|
|
}
|
|
|
|
export function moonPoses(star: Vec2, count: number, distance: number, obstacles: Vec2[] = []): Vec2[] {
|
|
const direction = Math.atan2(star.y, star.x);
|
|
const margin = distance / 2;
|
|
for (const offset of [0, Math.PI / 4, -Math.PI / 4, Math.PI / 2, -Math.PI / 2, Math.PI]) {
|
|
const positions = Array.from({ length: count }, (_, index) => {
|
|
const angle = direction + offset + (index - (count - 1) / 2) * Math.PI / 3;
|
|
return { x: star.x + Math.cos(angle) * distance, y: star.y + Math.sin(angle) * distance };
|
|
});
|
|
if (positions.every(({ x, y }) =>
|
|
Math.abs(x) < SCENE_WIDTH / 2 - margin && Math.abs(y) < SCENE_HEIGHT / 2 - margin &&
|
|
obstacles.every((point) => Math.hypot(point.x - x, point.y - y) >= 14),
|
|
)) return positions;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
export function groupSnapshots(orbits: Orbit[], snapshots: Snapshot[]): Map<string, Snapshot[]> {
|
|
const ids = new Set(orbits.map(({ id }) => id));
|
|
const names = new Map<string, string | null>();
|
|
for (const { id, name } of orbits) {
|
|
names.set(name, names.has(name) ? null : id);
|
|
}
|
|
const groups = new Map<string, Snapshot[]>();
|
|
for (const snapshot of snapshots) {
|
|
const id = snapshot.orbit_id ?? names.get(snapshot.orbit_name);
|
|
if (!id || !ids.has(id)) continue;
|
|
const group = groups.get(id) ?? [];
|
|
group.push(snapshot);
|
|
groups.set(id, group);
|
|
}
|
|
for (const group of groups.values()) {
|
|
group.sort((a, b) => Date.parse(b.taken_at) - Date.parse(a.taken_at));
|
|
}
|
|
return groups;
|
|
}
|
|
|
|
export function spiralPoint(source: Vec2, progress: number): Vec2 {
|
|
const u = Math.min(1, Math.max(0, progress));
|
|
const angle = Math.atan2(source.y, source.x) + u * Math.PI * 1.15;
|
|
const radius = Math.hypot(source.x, source.y) * (1 - u ** 1.4);
|
|
return { x: Math.cos(angle) * radius, y: Math.sin(angle) * radius };
|
|
}
|