import type { VisualPosition } from './index.js'; export const TICK_MS = 100; export const MOVE_MIN_PX = 18; export const STEP_MOVE_MIN_PX = 4; export const STILL_STEP_MAX_PX = 3; export interface Vec { x: number; y: number; } export function vector(from: Vec, to: Vec): Vec { return { x: to.x - from.x, y: to.y - from.y }; } export function length(value: Vec): number { return Math.hypot(value.x, value.y); } export function distance(a: Vec, b: Vec): number { return Math.hypot(a.x - b.x, a.y - b.y); } export function dot(a: Vec, b: Vec): number { return a.x * b.x + a.y * b.y; } export function cross(a: Vec, b: Vec): number { return a.x * b.y - a.y * b.x; } export function unit(value: Vec): Vec { const magnitude = length(value); return magnitude <= 0 ? { x: 0, y: 0 } : { x: value.x / magnitude, y: value.y / magnitude }; } export function cosine(a: Vec, b: Vec): number { const denominator = length(a) * length(b); return denominator <= 0 ? 0 : dot(a, b) / denominator; } export function angleDegrees(a: Vec, b: Vec): number { return Math.acos(Math.max(-1, Math.min(1, cosine(a, b)))) * (180 / Math.PI); } export type Direction = '左' | '右' | '上' | '下' | '左上' | '左下' | '右上' | '右下' | '原地'; export function direction(from: Vec, to: Vec, minimum = MOVE_MIN_PX): Direction { const dx = to.x - from.x; const dy = to.y - from.y; if (Math.hypot(dx, dy) < minimum) return '原地'; const horizontal = Math.abs(dx) >= minimum / 2 ? (dx > 0 ? '右' : '左') : ''; const vertical = Math.abs(dy) >= minimum / 2 ? (dy > 0 ? '下' : '上') : ''; return `${horizontal}${vertical}` as Direction || '原地'; } /** * Quantizes a displacement by its heading rather than by absolute component * sizes. Unlike direction(), this label does not change merely because the * same straight trajectory is measured over a shorter or longer window. */ export function headingDirection(from: Vec, to: Vec, minimum = MOVE_MIN_PX): Direction { const dx = to.x - from.x; const dy = to.y - from.y; if (Math.hypot(dx, dy) < minimum) return '原地'; const octant = Math.round(Math.atan2(dy, dx) / (Math.PI / 4)); const directions: readonly Direction[] = [ '右', '右下', '下', '左下', '左', '左上', '上', '右上', ]; return directions[(octant + directions.length) % directions.length]; } export function actorLabel(actor: { observer: boolean; seat?: number; name?: string; id: string }): string { if (actor.observer) return '你'; if (actor.seat !== undefined) return `${actor.seat}号`; return actor.name ?? actor.id; } export function actorValue(actor: { observer: boolean; seat?: number; id: string }): number | string { return actor.seat ?? actor.id; } export function timeRange(fromTick: number, toTick: number): string { const format = (tick: number): string => { const total = Math.max(0, Math.floor((tick * TICK_MS) / 1000)); return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}`; }; const from = format(fromTick); const to = format(toTick); return from === to ? from : `${from}-${to}`; } export function round1(value: number): number { return Math.round(value * 10) / 10; } export function positionCopy(position: VisualPosition): VisualPosition { return { x: position.x, y: position.y, room: position.room }; }