Skip to content
Make the player visible with a procedural 3D humanoid132be7c
JavaScript69 lines3.2 KB
export const lerp = (a, b, t) => a + (b - a) * t;export const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));export function interpolate(prev, curr, alpha) {  const t = clamp(alpha, 0, 1);  return { ...curr, x: lerp(prev.x, curr.x, t), y: lerp(prev.y, curr.y, t) };}export function hudLayout(width, height) {  const margin = Math.round(width * 0.03);  const barW = Math.round(width * 0.4);  const barH = Math.max(12, Math.round(height * 0.035));  return {    p1: { x: margin, y: margin, w: barW, h: barH },    p2: { x: width - margin - barW, y: margin, w: barW, h: barH },    timer: { x: width / 2, y: margin + barH },  };}export function healthFraction(hp, max = 100) {  return clamp(max > 0 ? hp / max : 0, 0, 1);}export function generateRoomCode(rand = Math.random, len = 5) {  const chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";  let s = "";  for (let i = 0; i < len; i++) s += chars[Math.floor(rand() * chars.length) % chars.length];  return s;} // ---- Visibility helpers (pure, tested in visibility.test.mjs) ----// Keep a fighter x (world units) inside the arena so it never leaves the stage/camera.export function clampToArena(x, arenaW, margin = 0.4) {  return arenaW > 2 * margin ? clamp(x, margin, arenaW - margin) : arenaW / 2;}// Camera distance and centre that keep every fighter (plus a body margin) in the horizontal frame.export function frameCamera(xs, aspect = 16 / 9, fov = Math.PI / 4, arenaW = 9.6, margin = 1.2) {  const a = Math.max(aspect, 0.3);  const pts = xs.length ? xs.map((x) => clampToArena(x, arenaW)) : [arenaW / 2];  const lo = Math.min(...pts), hi = Math.max(...pts);  const cx = (lo + hi) / 2;  const halfW = (hi - lo) / 2 + margin;  const distance = halfW / (Math.tan(fov / 2) * a);  return { cx, distance, halfW };}// Is world x visible horizontally from a camera at cx/distance?export function inFrame(x, cx, distance, aspect = 16 / 9, fov = Math.PI / 4) {  return Math.abs(x - cx) <= distance * Math.tan(fov / 2) * aspect;}// Draw order: background layers first, then opponents, then the player (index 0) last.export function drawOrder(layerCount, fighterCount, playerIndex = 0) {  const out = [];  for (let i = 0; i < layerCount; i++) out.push({ kind: 'layer', index: i });  for (let i = 0; i < fighterCount; i++) if (i !== playerIndex) out.push({ kind: 'fighter', index: i });  if (playerIndex >= 0 && playerIndex < fighterCount) out.push({ kind: 'fighter', index: playerIndex });  return out;}const luminance = (c) => 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];// Rim/outline colour that contrasts with the background colour.export function rimColor(bg, isPlayer = true) {  const dark = luminance(bg) > 0.5;  if (isPlayer) return dark ? [0.05, 0.05, 0.1] : [1.0, 0.95, 0.35];  return dark ? [0.25, 0.25, 0.3] : [0.85, 0.9, 1.0];}// Push a body colour away from the background luminance so the fighter stands out.export function contrastColor(color, bg, minDelta = 0.35) {  const d = luminance(color) - luminance(bg);  if (Math.abs(d) >= minDelta) return color.slice();  const up = luminance(bg) < 0.5;  const k = minDelta - Math.abs(d);  return color.map((c) => clamp(up ? c + k * 1.4 : c - k * 1.4, 0, 1));}