import type { AABB, Area, Body, Collider, PhysicsWorld } from '../core/types'; const EPS = 1e-4; /** Desplazamiento máximo por sub-paso: evita atravesar plataformas finas. */ const MAX_STEP = 0.2; export function overlaps(a: AABB, b: AABB, margin = EPS): boolean { return Math.abs(a.x - b.x) < a.hw + b.hw - margin && Math.abs(a.y - b.y) < a.hh + b.hh - margin; } /** * Física cinemática de plataformas con cajas AABB. * Resolución por ejes separados (X, luego Y) con sub-pasos, plataformas * atravesables (oneWay) y arrastre por plataformas móviles. */ export class Physics implements PhysicsWorld { readonly colliders: Collider[] = []; readonly areas: Area[] = []; add(c: Collider): Collider { this.colliders.push(c); return c; } box(x: number, y: number, w: number, h: number, extra: Partial = {}): Collider { return this.add({ x, y, hw: w / 2, hh: h / 2, ...extra }); } remove(c: Collider): void { const i = this.colliders.indexOf(c); if (i >= 0) this.colliders.splice(i, 1); } addArea(a: Area): Area { this.areas.push(a); return a; } removeArea(a: Area): void { const i = this.areas.indexOf(a); if (i >= 0) this.areas.splice(i, 1); } clear(): void { this.colliders.length = 0; this.areas.length = 0; } body(x: number, y: number, hw: number, hh: number): Body { return { x, y, hw, hh, vx: 0, vy: 0, onGround: false, onCeiling: false, onWall: 0, ground: null, dropThrough: 0 }; } move(b: Body, dt: number): void { // Arrastre de la plataforma sobre la que estaba apoyado. const g = b.ground; if (g && g.enabled !== false) { if (g.vx) b.x += g.vx * dt; if (g.vy) b.y += g.vy * dt; } if (b.dropThrough > 0) b.dropThrough -= dt; b.onGround = false; b.onCeiling = false; b.onWall = 0; b.ground = null; const dx = b.vx * dt; const dy = b.vy * dt; if (b.noCollide) { b.x += dx; b.y += dy; return; } const n = Math.max(1, Math.ceil(Math.max(Math.abs(dx), Math.abs(dy)) / MAX_STEP)); const sx = dx / n; const sy = dy / n; for (let i = 0; i < n; i++) { this.stepX(b, sx); this.stepY(b, sy); } } private stepX(b: Body, dx: number): void { b.x += dx; const list = this.colliders; for (let i = 0; i < list.length; i++) { const c = list[i]; if (c.enabled === false || c.oneWay || !overlaps(b, c)) continue; let dir = Math.sign(dx); if (dir === 0) { // Sin movimiento propio (lo empujó algo): solo se corrige en X si es el eje de menor penetración. const penX = b.hw + c.hw - Math.abs(b.x - c.x); const penY = b.hh + c.hh - Math.abs(b.y - c.y); if (penX >= penY) continue; dir = b.x < c.x ? 1 : -1; } if (dir > 0) { b.x = c.x - c.hw - b.hw; b.onWall = 1; if (b.vx > 0) b.vx = 0; } else { b.x = c.x + c.hw + b.hw; b.onWall = -1; if (b.vx < 0) b.vx = 0; } } } private stepY(b: Body, dy: number): void { b.y += dy; const list = this.colliders; for (let i = 0; i < list.length; i++) { const c = list[i]; if (c.enabled === false || !overlaps(b, c)) continue; const top = c.y + c.hh; if (c.oneWay) { if (dy > 0 || b.dropThrough > 0) continue; const prevBottom = b.y - dy - b.hh; const tolerance = 0.12 + Math.abs(c.vy ?? 0) / 30; if (prevBottom < top - tolerance) continue; this.land(b, c, top); continue; } let down = dy < 0; if (dy === 0) down = b.y >= c.y; if (down) { this.land(b, c, top); } else { b.y = c.y - c.hh - b.hh; b.onCeiling = true; if (b.vy > 0) b.vy = 0; } } } private land(b: Body, c: Collider, top: number): void { b.y = top + b.hh; b.onGround = true; b.ground = c; if (b.vy < 0) b.vy = 0; } query(box: AABB, filter?: (c: Collider) => boolean): Collider[] { const out: Collider[] = []; for (const c of this.colliders) { if (c.enabled === false || !overlaps(box, c)) continue; if (!filter || filter(c)) out.push(c); } return out; } areasAt(box: AABB, kind?: string): Area[] { const out: Area[] = []; for (const a of this.areas) { if (a.enabled === false || (kind && a.kind !== kind) || !overlaps(box, a)) continue; out.push(a); } return out; } solidAt(x: number, y: number): boolean { for (const c of this.colliders) { if (c.enabled === false || c.oneWay) continue; if (Math.abs(x - c.x) < c.hw && Math.abs(y - c.y) < c.hh) return true; } return false; } groundBelow(x: number, y: number, maxDist = 50): number | null { let best: number | null = null; for (const c of this.colliders) { if (c.enabled === false || Math.abs(x - c.x) > c.hw) continue; const top = c.y + c.hh; if (top > y + 0.01 || top < y - maxDist) continue; if (best === null || top > best) best = top; } return best; } }