/** * 🦇 enemy.bat — cuelga boca abajo dormido; al acercarse el héroe despierta, se lanza en picado en arco * (pasa por donde estaba el héroe y sube al otro lado) y vuelve a su percha. Congelable: congelado en el aire * sirve de plataforma. Con COOL cae al suelo mareado. * ¡OJO! y del spawn = punto del techo del que cuelga (parte de arriba del murciélago). * Props: { facing?, range? (despertar, 5), aggro? (despierta sin esperar), patrol?: [x0, x1] (limita el arco) } */ import * as THREE from 'three'; import { createChibi } from '../../characters/chibi'; import type { Props } from '../../core/types'; import { Enemy } from './base'; import { meshOf, rigBody, sharedGeo, sharedMat } from './shared'; export const BAT = { hp: 1, hw: 0.36, hh: 0.34, size: 0.72, range: 5, depth: 9, wake: 0.5, swoop: 1.35, ret: 1.1, rest: 1.4, maxSwoopDx: 7, color: 0x6b4aa8, }; function wingGeo(): THREE.BufferGeometry { return sharedGeo('bat.wing', () => { const s = new THREE.Shape(); s.moveTo(0, 0.1); s.quadraticCurveTo(0.45, 0.42, 0.95, 0.3); // borde festoneado s.quadraticCurveTo(0.82, 0.08, 0.86, -0.12); s.quadraticCurveTo(0.66, -0.02, 0.58, -0.2); s.quadraticCurveTo(0.42, -0.06, 0.3, -0.2); s.quadraticCurveTo(0.2, -0.05, 0, -0.08); s.closePath(); const g = new THREE.ExtrudeGeometry(s, { depth: 0.04, bevelEnabled: true, bevelThickness: 0.02, bevelSize: 0.02, bevelSegments: 2, curveSegments: 6 }); g.translate(0, 0, -0.02); return g; }); } export class Bat extends Enemy { private home = { x: 0, y: 0 }; private p0 = { x: 0, y: 0 }; private c = { x: 0, y: 0 }; private p2 = { x: 0, y: 0 }; private restT = 0; private wings: THREE.Group[] = []; private flip = 1; // 1 = boca abajo constructor(props: Props) { super(props); this.hw = BAT.hw; this.hh = BAT.hh; this.maxHp = BAT.hp; this.flying = true; this.ghostly = true; this.baseColor = BAT.color; this.shadowRadius = 0.3; this.dizzyHeight = 0.15; this.state = 'hang'; } protected spawnCenterY(y: number): number { return y - this.hh; } protected afterAdded(): void { this.home = { x: this.body.x, y: this.body.y }; } protected build(): void { const rig = createChibi({ color: BAT.color, size: BAT.size, expression: 'sleepy', hands: false, footColor: 0x3b2a63 }); this.rig = rig; rig.play('sleep', { loop: true }); const body = rigBody(rig); const wingMat = sharedMat('bat.wing', { color: 0x4a2f80, gloss: 0.5, rim: 0.7, side: THREE.DoubleSide }); for (const side of [-1, 1]) { const pivot = new THREE.Group(); pivot.position.set(side * 0.4, 0.05, -0.08); const w = meshOf(wingGeo(), wingMat); w.scale.set(side, 1, 1); pivot.add(w); body.add(pivot); this.wings.push(pivot); } // orejas puntiagudas y colmillos const earMat = sharedMat('bat.ear', { color: BAT.color, gloss: 0.7 }); const earGeo = sharedGeo('bat.earGeo', () => new THREE.ConeGeometry(0.13, 0.32, 12)); for (const side of [-1, 1]) { const e = meshOf(earGeo, earMat); e.position.set(side * 0.24, 0.5, 0); e.rotation.z = -side * 0.35; body.add(e); } const fangMat = sharedMat('bat.fang', { color: 0xffffff, gloss: 0.8 }); const fangGeo = sharedGeo('bat.fangGeo', () => new THREE.ConeGeometry(0.03, 0.08, 8)); for (const side of [-1, 1]) { const f = meshOf(fangGeo, fangMat, false); f.rotation.z = Math.PI; f.position.set(side * 0.05, -0.13, 0.48); body.add(f); } } protected restoreExpression(): void { this.rig?.setExpression(this.state === 'hang' ? 'sleepy' : 'evil'); } protected stunFalls(): boolean { return true; } private interrupted(): void { if (this.state !== 'hang') this.state = 'return*'; } protected onHurt(dir: number): void { super.onHurt(dir); this.body.noCollide = false; this.interrupted(); } protected onDaze(): void { this.body.noCollide = false; this.state = 'return*'; } protected onThaw(): void { this.interrupted(); } private bez(u: number, out: { x: number; y: number }): void { const a = (1 - u) * (1 - u), b = 2 * u * (1 - u), c = u * u; out.x = a * this.p0.x + b * this.c.x + c * this.p2.x; out.y = a * this.p0.y + b * this.c.y + c * this.p2.y; } private readonly tmp = { x: 0, y: 0 }; /** Mueve el cuerpo hacia el punto u de la curva actual vía velocidad (para que la física interpole). */ private steer(u: number, dt: number): void { this.bez(Math.min(1, u), this.tmp); const b = this.body; b.vx = (this.tmp.x - b.x) / dt; b.vy = (this.tmp.y - b.y) / dt; if (Math.abs(b.vx) > 0.5) this.facing = b.vx > 0 ? 1 : -1; } protected think(dt: number): void { const b = this.body; const P = this.props; const g = this.game; b.noCollide = true; if (this.state === 'return*') { // regreso tras un golpe/mareo: arco hacia la percha this.p0 = { x: b.x, y: b.y }; this.p2 = { ...this.home }; this.c = { x: (b.x + this.home.x) / 2, y: Math.max(b.y, this.home.y) + 1.5 }; this.setState('return'); this.rig?.setExpression('evil'); this.rig?.play('float', { loop: true }); } switch (this.state) { case 'hang': { b.vx = (this.home.x - b.x) * 8; b.vy = (this.home.y - b.y) * 8; this.restT -= dt; const d = this.heroDelta(); const range = P.range ?? BAT.range; if (d && this.restT <= 0 && (P.aggro || (Math.abs(d.dx) < range && d.dy < 0.5 && d.dy > -BAT.depth))) { this.setState('wake'); this.facing = d.dx > 0 ? 1 : -1; this.rig?.setExpression('surprised'); this.rig?.play('float', { loop: true }); this.rig?.squash(0.3); g.audio.sfx('whoosh', { pitch: 1.8, volume: 0.5, x: this.x }); g.vfx.burst('emoji', this.x, this.y - 0.6, { emoji: '❗', count: 1 }); } break; } case 'wake': { b.vx = 0; b.vy = -0.6; if (this.stateT > BAT.wake * 0.5) this.rig?.setExpression('evil'); if (this.stateT >= BAT.wake) { const h = this.hero; const tx = h ? h.x : b.x + this.facing * 3; const ty = h ? h.y : b.y - 3; let dx = Math.max(-BAT.maxSwoopDx, Math.min(BAT.maxSwoopDx, tx - b.x)); if (Math.abs(dx) < 1.5) dx = (dx < 0 ? -1 : 1) * 1.5; this.p0 = { x: b.x, y: b.y }; let x2 = b.x + dx * 2; if (P.patrol) x2 = Math.max(Math.min(P.patrol[0], P.patrol[1]), Math.min(Math.max(P.patrol[0], P.patrol[1]), x2)); this.p2 = { x: x2, y: b.y }; const mx = b.x + dx; // B(0.5) = objetivo ⇒ C = 2·T − (P0+P2)/2 this.c = { x: 2 * mx - (this.p0.x + this.p2.x) / 2, y: 2 * ty - (this.p0.y + this.p2.y) / 2 }; this.setState('swoop'); g.audio.sfx('whoosh', { pitch: 1.1, x: this.x }); } break; } case 'swoop': { const u = this.stateT / BAT.swoop; // acelera al bajar, frena al subir const e = u < 0.5 ? 2 * u * u : 1 - 2 * (1 - u) * (1 - u); this.steer(e, dt); if (u >= 1) { this.p0 = { x: b.x, y: b.y }; this.p2 = { ...this.home }; this.c = { x: (b.x + this.home.x) / 2, y: Math.max(b.y, this.home.y) + 1.2 }; this.setState('return'); } break; } case 'return': { const u = this.stateT / BAT.ret; this.steer(u * u * (3 - 2 * u), dt); if (u >= 1) { this.setState('hang'); this.restT = BAT.rest; this.rig?.setExpression('sleepy'); this.rig?.play('sleep', { loop: true }); } break; } default: this.state = 'return*'; } } protected animate(dt: number): void { const b = this.body; const hanging = this.state === 'hang' && !this.frozen && this.stunT <= 0; this.rig?.update(dt, { vx: b.vx, vy: b.vy, onGround: true, facing: this.facing }); const target = hanging ? 1 : 0; this.flip += (target - this.flip) * Math.min(1, dt * 10); this.visual.rotation.z = this.flip * Math.PI + (hanging ? Math.sin(this.t * 1.5) * 0.08 : -b.vx * 0.03); // alas: plegadas colgando, aleteo rápido volando, quietas congelado const frozen = this.frozen; for (let i = 0; i < this.wings.length; i++) { const side = i === 0 ? -1 : 1; const w = this.wings[i]; let a: number; if (frozen) a = 0.2; else if (hanging) a = 1.1 + Math.sin(this.t * 2) * 0.05; else if (this.stunT > 0) a = 0.9; else a = Math.sin(this.t * (this.state === 'swoop' ? 14 : 22)) * 0.75; w.rotation.y = -side * a; w.rotation.z = side * (hanging ? -0.4 : 0.1); } } }