import type { AxialCoord, HexCell } from "./hex.ts"; import type { GridCellAnimationState } from "./gridAnimations.ts"; import { hexDistance } from "./hex.ts"; import { getActivationEnvelope, smootherstep } from "./motion.ts"; export type NavigationTransitionPhase = "out" | "in"; export type NavigationTransitionDirection = "forward" | "backward"; export interface NavigationTransitionBaseOptions { cells: HexCell[]; frame: number; phase: NavigationTransitionPhase; direction?: NavigationTransitionDirection; duration?: number; intensity?: number; reducedMotion?: boolean; } export interface NavigationConvergeFrameOptions extends NavigationTransitionBaseOptions { origin?: AxialCoord; maxRadius?: number; trail?: number; } export interface NavigationExpandFrameOptions extends NavigationConvergeFrameOptions {} export interface NavigationOriginArrivalFrameOptions extends NavigationTransitionBaseOptions { trail?: number; axis?: "q" | "r" | "q+r"; } export interface NavigationNodeFlickerNode { id: string; active?: boolean; } export interface NavigationToneLevels { dark: number; medium: number; bright: number; } export interface NavigationNodeFlickerFrameOptions { nodes: NavigationNodeFlickerNode[]; frame: number; phase: NavigationTransitionPhase; seed?: string | number; duration?: number; intensity?: number; tones?: Partial; reducedMotion?: boolean; } const clamp = (value: number, min = 0, max = 1) => Math.min(max, Math.max(min, value)); const smooth = (value: number) => smootherstep(0, 1, clamp(value)); function hashString(input: string) { let hash = 2166136261; for (let index = 0; index < input.length; index += 1) { hash ^= input.charCodeAt(index); hash = Math.imul(hash, 16777619); } return hash >>> 0; } function seeded(seed: string | number | undefined, value: string) { return hashString(String(seed ?? "navigation") + ":" + value) / 4294967295; } function state(id: string, stateName: GridCellAnimationState["state"], intensity: number, type: string) { return { id, state: stateName, intensity: clamp(intensity), type } satisfies GridCellAnimationState; } function axialProjection(cell: AxialCoord, axis: "q" | "r" | "q+r") { return axis === "q" ? cell.q : axis === "r" ? cell.r : cell.q + cell.r; } function quiet(cells: Array<{ id: string }>, type: string, intensity = 0.1) { return cells.map((cell) => state(cell.id, "idle", intensity, type)); } function directionalFrame(options: NavigationTransitionBaseOptions & { type: string; trail?: number; partial?: number; axis?: "q" | "r" | "q+r" }) { const { cells, frame, phase, direction = "forward", duration = phase === "out" ? 240 : 280, intensity = 0.3, reducedMotion = false, type, trail = 2, partial = 1, axis = "q+r" } = options; if (!cells.length) return []; if (reducedMotion || frame >= duration) return quiet(cells, type); const sign = direction === "backward" ? -1 : 1; const projections = cells.map((cell) => axialProjection(cell, axis) * sign); const minimum = Math.min(...projections); const maximum = Math.max(...projections); const span = Math.max(1, maximum - minimum); const progress = clamp(frame / duration); const travel = span * partial; const position = phase === "out" ? minimum - trail + progress * (travel + trail * 2) : maximum + trail - progress * (travel + trail * 2); return cells.map((cell, index) => { const offset = phase === "out" ? position - projections[index] : projections[index] - position; const activity = getActivationEnvelope(offset, { attack: 0.7, hold: 0.12, release: trail + 0.6 }); if (activity <= 0.015) return state(cell.id, "idle", 0.1, type); const active = offset >= -0.7 && offset <= 0.35; return state(cell.id, active ? "active" : "trail", intensity * activity * (active ? 1 : 0.7), type); }); } export function getNavigationFlowFrame(options: NavigationTransitionBaseOptions & { trail?: number; axis?: "q" | "r" | "q+r" }) { return directionalFrame({ ...options, type: "navigation-flow", trail: options.trail ?? 2 }); } function radialFrame(options: NavigationConvergeFrameOptions, mode: "converge" | "expand") { const { cells, frame, phase, origin = { q: 0, r: 0 }, maxRadius, duration = phase === "out" ? 300 : 320, intensity = 0.32, trail = 1.8, reducedMotion = false } = options; const type = "navigation-" + mode; if (!cells.length) return []; const radius = maxRadius ?? Math.max(1, ...cells.map((cell) => hexDistance(origin, cell))); if (reducedMotion || frame >= duration) return quiet(cells, type); const progress = smooth(frame / duration); const expands = mode === "expand" ? phase === "out" : phase === "in"; const target = expands ? progress * radius : (1 - progress) * radius; return cells.map((cell) => { const distance = hexDistance(origin, cell); const offset = target - distance; const activity = getActivationEnvelope(offset, { attack: 0.8, hold: 0.16, release: trail }); if (activity <= 0.015) return state(cell.id, "idle", 0.1, type); return state(cell.id, offset >= -0.8 && offset <= 0.35 ? "active" : "trail", intensity * activity, type); }); } export function getNavigationConvergeFrame(options: NavigationConvergeFrameOptions) { return radialFrame(options, "converge"); } export function getNavigationExpandFrame(options: NavigationExpandFrameOptions) { return radialFrame(options, "expand"); } export function getNavigationOriginArrivalFrame(options: NavigationOriginArrivalFrameOptions) { const duration = options.duration ?? (options.phase === "in" ? 260 : 220); return directionalFrame({ ...options, duration, intensity: (options.intensity ?? 0.3) * 0.82, partial: 0.58, trail: options.trail ?? 1.6, type: "navigation-origin-arrival", axis: options.axis ?? "q+r" }); } export function deriveNavigationToneLevels(gridColor?: string): NavigationToneLevels { if (!gridColor) return { dark: 0.76, medium: 0.98, bright: 1.2 }; const match = gridColor.trim().match(/^#([0-9a-f]{6})$/i); if (!match) return { dark: 0.76, medium: 0.98, bright: 1.2 }; const channels = [0, 2, 4].map((offset) => Number.parseInt(match[1].slice(offset, offset + 2), 16) / 255); const luminance = channels[0] * 0.2126 + channels[1] * 0.7152 + channels[2] * 0.0722; return { dark: luminance < 0.16 ? 0.84 : 0.76, medium: 0.98, bright: luminance > 0.82 ? 1.08 : 1.2 }; } export function getNavigationNodeFlickerFrame(options: NavigationNodeFlickerFrameOptions): GridCellAnimationState[] { const { nodes, frame, phase, seed = "navigation-node-flicker", duration = 420, intensity = 0.26, reducedMotion = false } = options; const tones = { dark: 0.76, medium: 0.98, bright: 1.2, ...options.tones }; if (!nodes.length) return []; if (reducedMotion || frame >= duration) return nodes.map((node) => state(node.id, "idle", 0.12, "navigation-node-flicker-settled")); const progress = clamp(frame / duration); const envelope = phase === "out" ? smooth(progress) : smooth(1 - progress); const toneNames = ["dark", "medium", "bright"] as const; return nodes.map((node) => { if (node.active === false) return state(node.id, "idle", 0.12, "navigation-node-flicker-settled"); const offset = seeded(seed, node.id) * duration * 0.28; const rate = 68 + Math.floor(seeded(seed, node.id + ":rate") * 45); const local = frame + (phase === "out" ? offset : -offset); const flicker = Math.floor(Math.max(0, local) / rate); const phasePoint = (Math.max(0, local) % rate) / rate; const active = envelope > 0.08 && phasePoint < 0.72 && local >= offset * 0.35; if (!active) return state(node.id, "idle", 0.14 + intensity * envelope * 0.18, "navigation-node-flicker-settled"); const tone = toneNames[(flicker + Math.floor(seeded(seed, node.id + ":tone") * 3)) % toneNames.length]; return state(node.id, "active", intensity * envelope * tones[tone], "navigation-node-flicker-" + tone); }); }