feat: rebuild the orbital vault as an operational map
This commit is contained in:
@@ -1,68 +1,105 @@
|
||||
/** Face-on orrery: two even shells around the vault. */
|
||||
import type { Orbit, Snapshot } from "../api";
|
||||
|
||||
export type Vec2 = { x: number; y: number };
|
||||
type OrbitSlot = { radius: number; angle: number; speed: number };
|
||||
|
||||
export type ShellBody = {
|
||||
index: number;
|
||||
shell: "inner" | "outer";
|
||||
radius: number;
|
||||
baseAngle: number;
|
||||
size: number;
|
||||
warm: boolean;
|
||||
};
|
||||
export const SCENE_WIDTH = 660;
|
||||
export const SCENE_HEIGHT = 480;
|
||||
export const ORBIT_TILT = 0.68;
|
||||
|
||||
const INNER_R = 112;
|
||||
const OUTER_R = 178;
|
||||
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 },
|
||||
];
|
||||
|
||||
/** Inner shell is fewer, larger, warmer. Outer shell is the rest. */
|
||||
export function assignShells(count: number): ShellBody[] {
|
||||
if (count <= 0) return [];
|
||||
let innerCount = Math.max(1, Math.round(count * 0.38));
|
||||
if (count > 4) innerCount = Math.max(3, innerCount);
|
||||
innerCount = Math.min(innerCount, count);
|
||||
const outerCount = count - innerCount;
|
||||
const bodies: ShellBody[] = [];
|
||||
for (let i = 0; i < innerCount; i++) {
|
||||
bodies.push({
|
||||
index: i,
|
||||
shell: "inner",
|
||||
radius: INNER_R,
|
||||
baseAngle: -Math.PI / 2 + (i / innerCount) * Math.PI * 2,
|
||||
size: 11,
|
||||
warm: true,
|
||||
});
|
||||
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);
|
||||
}
|
||||
for (let i = 0; i < outerCount; i++) {
|
||||
bodies.push({
|
||||
index: innerCount + i,
|
||||
shell: "outer",
|
||||
radius: OUTER_R,
|
||||
baseAngle: -Math.PI / 2 + ((i + 0.5) / outerCount) * Math.PI * 2,
|
||||
size: 6.4,
|
||||
warm: false,
|
||||
});
|
||||
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);
|
||||
}
|
||||
return bodies;
|
||||
if (selected && currentIds.has(selected) && !next.has(selected)) {
|
||||
next.set(selected, selectedSlot);
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
export function shellPose(body: ShellBody, time: number): Vec2 & { angle: number } {
|
||||
const speed = body.shell === "inner" ? 0.18 : 0.11;
|
||||
const angle = body.baseAngle + time * speed;
|
||||
export function orbitPose(slot: OrbitSlot, time: number): Vec2 {
|
||||
const angle = slot.angle + time * slot.speed;
|
||||
return {
|
||||
angle,
|
||||
x: Math.cos(angle) * body.radius,
|
||||
y: Math.sin(angle) * body.radius,
|
||||
x: Math.cos(angle) * slot.radius,
|
||||
y: Math.sin(angle) * slot.radius * ORBIT_TILT,
|
||||
};
|
||||
}
|
||||
|
||||
/** Point on a smooth inward spiral. u = 0 is the source; u = 1 is the shadow. */
|
||||
export function spiralPoint(source: Vec2, u: number): Vec2 {
|
||||
const clamped = Math.min(1, Math.max(0, u));
|
||||
const a0 = Math.atan2(source.y, source.x);
|
||||
const r0 = Math.hypot(source.x, source.y);
|
||||
const fall = 1 - Math.pow(clamped, 0.92);
|
||||
const spin = clamped * Math.PI * 1.05;
|
||||
const r = r0 * fall;
|
||||
const a = a0 + spin;
|
||||
return { x: Math.cos(a) * r, y: Math.sin(a) * r };
|
||||
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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user