/** * 👻 enemy.ghost — fantasma de la horda nocturna. Vuela atravesándolo todo y se acerca en ondas. * Mecánica: es tímido. Si el héroe lo MIRA de frente (de cara a él y cerca) se vuelve semitransparente, * se tapa y huye; cuando le das la espalda vuelve a por ti. Mientras está tímido no hace daño. * Débil al fuego (el fuego lo deshace casi de un golpe). Con luz de día se desvanece (salvo props.anytime). * Props: { facing?, range? (detección, 14), aggro? (por defecto true), anytime?: boolean, speed? } */ import * as THREE from 'three'; import { createChibi } from '../../characters/chibi'; import type { Hit, Props } from '../../core/types'; import { Enemy, approach } from './base'; export const GHOST = { hp: 2, hw: 0.4, hh: 0.45, size: 1, speed: 2.7, accel: 6, waveAmp: 1.7, waveFreq: 2.3, range: 14, shyDist: 8, shyFlee: 3.6, shyOpacity: 0.22, shyCalm: 0.45, opacity: 0.88, fireBonus: 2, color: 0xe4ddff, }; export class Ghost extends Enemy { private alpha = GHOST.opacity; private calmT = 0; /** Empapado: durante un rato no se tapa ni huye. */ private soakedT = 0; private mats: Array = []; private phase = Math.random() * Math.PI * 2; constructor(props: Props) { super(props); this.hw = GHOST.hw; this.hh = GHOST.hh; this.maxHp = GHOST.hp; this.flying = true; this.ghostly = true; this.freezable = false; this.burnable = true; this.stompable = true; this.baseColor = GHOST.color; this.shadowRadius = 0.35; this.state = 'haunt'; } protected build(): void { const rig = createChibi({ color: GHOST.color, size: GHOST.size, shape: 'ghost', expression: 'evil', opacity: GHOST.opacity, emissive: 0xb9a8ff, emissiveIntensity: 0.07, }); this.rig = rig; rig.play('float', { loop: true }); rig.root.traverse((o) => { const m = (o as THREE.Mesh).material as (THREE.Material & { opacity: number }) | undefined; if (m && !Array.isArray(m) && m.transparent && !m.userData.shared && !this.mats.includes(m)) this.mats.push(m); }); } protected restoreExpression(): void { this.rig?.setExpression(this.state === 'shy' ? 'surprised' : 'evil'); } takeHit(hit: Hit): boolean { if (hit.element === 'fire') hit = { ...hit, damage: hit.damage + GHOST.fireBonus }; return super.takeHit(hit); } protected onWater(_dir: number): void { this.soakedT = 3; if (this.state === 'shy') { this.setState('haunt'); this.rig?.setExpression('evil'); } } protected harmful(): boolean { return this.active && this.state !== 'shy'; } /** ¿El héroe lo está mirando de frente? */ private watched(dx: number, dist: number): boolean { const h = this.hero; if (!h) return false; // dx = héroe - fantasma; el héroe mira hacia el fantasma si facing tiene el signo de -dx return dist < GHOST.shyDist && h.facing === (dx < 0 ? 1 : -1); } protected think(dt: number): void { const b = this.body; const P = this.props; const g = this.game; if (!P.anytime && g.gfx.env.phase === 'day') { // la luz del día lo disuelve g.vfx.burst('sparkle', this.x, this.y, { count: 10, color: 0xdfe8ff }); g.vfx.burst('poof', this.x, this.y, { scale: 0.8 }); this.defeated = true; this.kill(); return; } const d = this.heroDelta(); const speed = P.speed ?? GHOST.speed; const range = P.range ?? GHOST.range; if (!d || (d.dist > range && P.aggro === false)) { b.vx = approach(b.vx, 0, GHOST.accel * dt); b.vy = Math.cos(this.t * GHOST.waveFreq + this.phase) * 0.6; return; } this.soakedT = Math.max(0, this.soakedT - dt); const seen = this.soakedT <= 0 && this.watched(d.dx, d.dist); if (d.dx !== 0) this.facing = (d.dx > 0 ? 1 : -1); if (this.state === 'haunt') { if (seen) { this.setState('shy'); this.calmT = 0; this.rig?.setExpression('surprised'); this.rig?.squash(-0.3); g.audio.sfx('whoosh', { pitch: 1.7, volume: 0.45, x: this.x }); g.vfx.burst('emoji', this.x, this.y + this.hh + 0.4, { emoji: '😳', count: 1 }); return; } const inv = 1 / Math.max(0.001, d.dist); const wave = Math.cos(this.t * GHOST.waveFreq + this.phase) * GHOST.waveAmp; b.vx = approach(b.vx, d.dx * inv * speed, GHOST.accel * dt); b.vy = approach(b.vy, d.dy * inv * speed + wave, GHOST.accel * 1.5 * dt); } else { // tímido: se aleja y sube un poco const away = d.dx > 0 ? -1 : 1; b.vx = approach(b.vx, away * GHOST.shyFlee, GHOST.accel * 1.5 * dt); b.vy = approach(b.vy, 0.6 + Math.cos(this.t * 3) * 0.6, GHOST.accel * dt); if (!seen) { this.calmT += dt; if (this.calmT > GHOST.shyCalm) { this.setState('haunt'); this.rig?.setExpression('evil'); g.vfx.burst('emoji', this.x, this.y + this.hh + 0.4, { emoji: '😈', count: 1 }); } } else this.calmT = 0; } } protected animate(dt: number): void { const b = this.body; this.rig?.update(dt, { vx: b.vx, vy: b.vy, onGround: true, facing: this.facing }); const target = this.state === 'shy' ? GHOST.shyOpacity : GHOST.opacity; this.alpha += (target - this.alpha) * Math.min(1, dt * 8); for (const m of this.mats) m.opacity = this.alpha; // flota: balanceo y estiramiento según la onda this.visual.rotation.z = -b.vx * 0.05 + Math.sin(this.t * 2.1) * 0.06; this.visual.position.y = Math.sin(this.t * 3.1 + this.phase) * 0.08; } }