import type { BossMotionState, WorldPosition } from "./types";

export const ARENA_CENTER: WorldPosition = [0, -1];
/** Keeps simulation limits and the room renderer in lockstep. */
export const ARENA_SIZE_MULTIPLIER = 1.3;
export const ARENA_RADIUS = 8.35 * ARENA_SIZE_MULTIPLIER;
export const ARENA_WALL_RADIUS = 9.55 * ARENA_SIZE_MULTIPLIER;

export function clampToArena(position: WorldPosition, padding = 0): WorldPosition {
  const radius = Math.max(0, ARENA_RADIUS - padding);
  const dx = position[0] - ARENA_CENTER[0];
  const dz = position[1] - ARENA_CENTER[1];
  const distance = Math.hypot(dx, dz);
  if (distance <= radius || distance < 0.0001) return [position[0], position[1]];
  const scale = radius / distance;
  return [ARENA_CENTER[0] + dx * scale, ARENA_CENTER[1] + dz * scale];
}

export function constrainBossMotion(motion: BossMotionState): BossMotionState {
  return {
    ...motion,
    position: clampToArena(motion.position),
    chargeStart: clampToArena(motion.chargeStart),
    chargeEnd: clampToArena(motion.chargeEnd),
    pounceCenter: clampToArena(motion.pounceCenter),
  };
}

export function isInsideArena(position: WorldPosition, tolerance = 0.001) {
  return Math.hypot(position[0] - ARENA_CENTER[0], position[1] - ARENA_CENTER[1]) <= ARENA_RADIUS + tolerance;
}
