/** * Cámara de plataformas 2.5D. * - Seguimiento suave con adelanto según `facing` (y algo de la velocidad si el objetivo tiene `body`). * - Zona muerta vertical: no sigue cada saltito; sube cuando el objetivo aterriza más alto o sale de la * ventana por arriba; baja rápido si cae por debajo. * - Límites de zona, `focus` (punto + zoom), zoom suave, shake por "trauma" con ruido suave decreciente. */ import * as THREE from 'three'; import { clamp, damp } from '../core/math'; import type { CameraRig, Vec2, ZoneBounds } from '../core/types'; /** Distancia de la cámara al plano de juego con zoom 1. */ export const CAM_DIST = 26; export const CAM_FOV = 32; /** Altura de la mirada sobre los pies del objetivo. */ const LOOK_UP = 3.0; /** Elevación de la cámara sobre el punto mirado (ligero picado). */ const CAM_RISE = 1.8; /** Ventana vertical (u) sobre la línea base antes de subir la cámara en el aire. */ const WIN_UP = 3.2; /** Margen por debajo de la línea base antes de bajar (caídas). */ const WIN_DOWN = 0.6; /** Ancho mínimo visible (u) en pantallas verticales: en el celular en vertical la cámara se aleja hasta mostrarlo. */ const MIN_VIEW_W = 9; /** En vertical, fracción de la media altura que se baja la mirada para dejar al héroe sobre los controles táctiles. */ const PORTRAIT_LIFT = 0.3; /** * En vertical, cuánto se deja bajar la cámara (u) para subir el terreno en pantalla. * La barra de EmojiPowers vive sobre los controles táctiles y, con el encuadre anterior, * tapaba al héroe y la línea de suelo (medido: suelo a 544 px y barra a 493–538 px en * 390×720 táctil). Solo relaja el límite INFERIOR de la cámara; el superior (techos y * plataformas altas) no cambia. El borde inferior visible queda en minY − 3, dentro de * la losa de suelo (que baja hasta minY − 6), así que no se ve el vacío. */ const PORTRAIT_DROP = 3.0; type Target = Vec2 & { facing?: number; body?: { onGround?: boolean; vx?: number; vy?: number } }; /** Ruido 1D suave (suma de senos desfasados) en -1..1. */ function wobble(t: number, seed: number): number { return (Math.sin(t * 1.0 + seed) * 0.5 + Math.sin(t * 2.3 + seed * 1.7) * 0.3 + Math.sin(t * 5.1 + seed * 3.1) * 0.2); } export class Camera implements CameraRig { x = 0; y = 0; zoomTarget = 1; /** Zoom actual (interpolado). */ zoom = 1; /** Multiplicador global del shake (accesibilidad). */ shakeScale = 1; /** Desplazamiento extra del encuadre (cinemáticas). */ readonly offset = new THREE.Vector2(); private target: Target | null = null; private bounds: ZoneBounds | null = null; private focusPoint: Vec2 | null = null; private focusZoom = 1; private look = 0; private baseY = 0; private trauma = 0; private traumaDecay = 1; private shakeAmp = 0; private t = 0; constructor(private cam: THREE.PerspectiveCamera) {} follow(t: Target | null): void { this.target = t; if (t) this.baseY = t.y; } setBounds(b: ZoneBounds | null): void { this.bounds = b; } focus(p: Vec2 | null, zoom = 1): void { this.focusPoint = p; this.focusZoom = zoom; if (!p && this.target) this.baseY = this.target.y; } /** Corta al objetivo (o a x,y = pies del objetivo) sin suavizado. */ snap(x?: number, y?: number): void { const t = this.focusPoint ?? this.target; const tx = x ?? t?.x ?? this.x; const ty = y ?? t?.y ?? this.y - LOOK_UP; this.baseY = ty; this.look = 0; this.x = tx; this.y = ty + LOOK_UP; this.zoom = this.focusPoint ? this.focusZoom : this.zoomTarget; this.apply(0); } /** * Sacudida: `intensity` ≈ desplazamiento máximo en unidades (0.1 leve, 0.3 golpe, 0.6 jefe), * decae a cero en `seconds`. Las sacudidas se acumulan sin pasar del máximo. */ shake(intensity: number, seconds: number): void { if (intensity <= 0 || seconds <= 0) return; const amp = Math.max(this.shakeAmp * this.trauma, intensity); this.shakeAmp = amp; this.trauma = 1; this.traumaDecay = 1 / seconds; } update(dt: number): void { this.t += dt; const t = this.focusPoint ?? this.target; if (t) { const isFocus = !!this.focusPoint; const tgt = this.target as Target | null; const facing = isFocus ? 0 : tgt?.facing ?? 0; const vx = isFocus ? 0 : tgt?.body?.vx ?? 0; const lead = clamp(facing * 2.0 + vx * 0.12, -3.2, 3.2); this.look = damp(this.look, lead, 1.8, dt); let ty: number; if (isFocus) { ty = t.y; this.baseY = t.y; } else { const grounded = tgt?.body ? !!tgt.body.onGround : true; if (grounded) this.baseY = damp(this.baseY, t.y, 10, dt); else if (t.y > this.baseY + WIN_UP) this.baseY = t.y - WIN_UP; else if (t.y < this.baseY - WIN_DOWN) this.baseY = t.y + WIN_DOWN; ty = this.baseY; } const falling = !isFocus && t.y < this.baseY; this.x = damp(this.x, t.x + this.look, isFocus ? 3 : 5.5, dt); this.y = damp(this.y, ty + LOOK_UP, isFocus ? 3 : falling ? 9 : 4, dt); } this.zoom = damp(this.zoom, this.focusPoint ? this.focusZoom : this.zoomTarget, 3, dt); this.trauma = Math.max(0, this.trauma - this.traumaDecay * dt); this.apply(dt); } /** Semi-extensiones visibles en el plano z=0 con el zoom actual. */ /** Distancia de la cámara: la base o la necesaria para no bajar de MIN_VIEW_W de ancho (vertical). */ private distance(): number { const tan = Math.tan(THREE.MathUtils.degToRad(this.cam.fov / 2)); const needed = MIN_VIEW_W / 2 / (tan * Math.max(0.2, this.cam.aspect)); return Math.max(CAM_DIST, needed) / this.zoom; } halfExtents(): { hw: number; hh: number } { const dist = this.distance(); const hh = Math.tan(THREE.MathUtils.degToRad(this.cam.fov / 2)) * dist; return { hw: hh * this.cam.aspect, hh }; } private apply(_dt: number): void { const dist = this.distance(); const { hw, hh } = this.halfExtents(); // En vertical se mira algo más abajo: el héroe queda en la mitad superior, libre de los controles. const lift = this.cam.aspect < 1 ? hh * PORTRAIT_LIFT : 0; const drop = this.cam.aspect < 1 ? PORTRAIT_DROP : 0; const b = this.bounds; if (b) { this.x = b.maxX - b.minX <= hw * 2 ? (b.minX + b.maxX) / 2 : clamp(this.x, b.minX + hw, b.maxX - hw); this.y = b.maxY - b.minY <= hh * 2 ? (b.minY + b.maxY) / 2 + lift : clamp(this.y, b.minY + hh + lift - drop, b.maxY - hh + lift); } let sx = 0, sy = 0, roll = 0; if (this.trauma > 0) { const k = this.shakeAmp * this.trauma * this.trauma * this.shakeScale; const f = 22; sx = wobble(this.t * f, 1.3) * k; sy = wobble(this.t * f, 7.9) * k; roll = wobble(this.t * f * 0.7, 4.2) * k * 0.03; } const lx = this.x + this.offset.x + sx; const ly = this.y + this.offset.y + sy - lift; this.cam.position.set(lx, ly + CAM_RISE, dist); this.cam.lookAt(lx, ly, 0); if (roll) this.cam.rotateZ(roll); } }