/** * Módulo VFX: partículas, ondas, destellos, textos flotantes, estelas y ambientes. * * game.vfx.burst(kind, x, y, opts) kinds del catálogo + compuestos: explosion, powerup, transform, * collect, freeze, levelup (desconocidos → poof) * game.vfx.trail(kind, x, y, opts) dust, fire, ice, star, sparkle, bubble, speed, smoke * vfxAmbient(game, kind, opts) rain, snow, fireflies, leaves, embers, dust, bubbles | null * * Todo se dibuja en 1 draw call (InstancedBufferGeometry + atlas por canvas). Los textos flotantes * y el destello de pantalla son DOM sobre el canvas. */ import '@fontsource/fredoka/700.css'; import * as THREE from 'three'; import type { Game } from '../core/game'; import type { VfxService } from '../core/types'; import { ParticleEngine } from './particles'; import { DEFAULT_Z, Effects, type AmbientKind, type AmbientOpts, type FxOpts } from './effects'; const ALLOW_SCREEN_FLASH = false; export type { AmbientKind, AmbientOpts, FxOpts } from './effects'; const TEXT_POOL = 24; interface FloatItem { el: HTMLDivElement; x: number; y: number; age: number; life: number; on: boolean } export class Vfx implements VfxService { readonly engine = new ParticleEngine(); readonly fx: Effects; private layer: HTMLDivElement; private flashEl: HTMLDivElement; private flashT = 0; private flashDur = 0; private texts: FloatItem[] = []; private textCursor = 0; private v = new THREE.Vector3(); constructor(private game: Game) { this.fx = new Effects(game, this.engine); game.gfx.scene.add(this.engine.mesh); this.layer = document.createElement('div'); this.layer.className = 'vfx-layer'; Object.assign(this.layer.style, { position: 'fixed', inset: '0', pointerEvents: 'none', overflow: 'hidden', zIndex: '5', } as Partial); this.flashEl = document.createElement('div'); Object.assign(this.flashEl.style, { position: 'absolute', inset: '0', opacity: '0', mixBlendMode: 'screen', background: '#fff', } as Partial); this.layer.appendChild(this.flashEl); for (let i = 0; i < TEXT_POOL; i++) { const el = document.createElement('div'); Object.assign(el.style, { position: 'absolute', left: '0', top: '0', whiteSpace: 'nowrap', display: 'none', fontFamily: '"Fredoka", "Noto Color Emoji", system-ui, sans-serif', fontWeight: '700', fontSize: 'clamp(20px, 3.6vmin, 40px)', letterSpacing: '0.02em', WebkitTextStroke: '0.16em #2a1f7a', paintOrder: 'stroke fill', textShadow: '0 0.12em 0 #2a1f7a, 0 0.2em 0.3em rgba(0,0,0,0.35)', willChange: 'transform, opacity', } as Partial); this.layer.appendChild(el); this.texts.push({ el, x: 0, y: 0, age: 0, life: 1, on: false }); } document.body.appendChild(this.layer); } burst(kind: string, x: number, y: number, opts?: FxOpts): void { try { this.fx.burst(kind, x, y, opts); } catch (err) { console.warn('[vfx] burst', kind, err); } } ring(x: number, y: number, color = 0xffffff, scale = 1): void { this.fx.ring(x, y, color, scale, DEFAULT_Z); } trail(kind: string, x: number, y: number, opts?: FxOpts): void { this.fx.trail(kind, x, y, opts); } flash(color: number, seconds = 0.25): void { // Destellos de pantalla completa desactivados: molestaban la vista (como flashes de cámara). if (!ALLOW_SCREEN_FLASH) return; this.flashEl.style.background = `#${(color & 0xffffff).toString(16).padStart(6, '0')}`; this.flashDur = Math.max(0.05, seconds); this.flashT = this.flashDur; } floatText(text: string, x: number, y: number, color = '#FFC928'): void { const it = this.texts[this.textCursor]; this.textCursor = (this.textCursor + 1) % TEXT_POOL; it.el.textContent = text; it.el.style.color = color; it.el.style.display = 'block'; it.x = x; it.y = y; it.age = 0; it.life = 1.0; it.on = true; } /** Efecto ambiental continuo que sigue a la cámara (null lo apaga). Se apaga solo al cambiar de zona. */ ambient(kind: AmbientKind | null, opts?: AmbientOpts): void { this.fx.ambient(kind, opts); } /** Borra todo lo vivo (partículas, textos, estelas, ambiente). */ clear(): void { this.engine.clear(); this.fx.reset(); for (const t of this.texts) { t.on = false; t.el.style.display = 'none'; } this.flashT = 0; this.flashEl.style.opacity = '0'; } render(dt: number): void { const frozen = this.game.frozen; if (!frozen) { this.fx.clock += dt; this.fx.updateAmbient(dt); } this.engine.update(dt, frozen); // Destello (siempre corre: es UI). if (this.flashT > 0) { this.flashT = Math.max(0, this.flashT - dt); const k = this.flashT / this.flashDur; this.flashEl.style.opacity = (0.85 * k * k).toFixed(3); } // Textos flotantes: proyectados con la cámara. const cam = this.game.gfx.camera; const w = window.innerWidth; const h = window.innerHeight; for (const t of this.texts) { if (!t.on) continue; if (!frozen) t.age += dt; if (t.age >= t.life) { t.on = false; t.el.style.display = 'none'; continue; } const u = t.age / t.life; const rise = 1.6 * (1 - (1 - u) * (1 - u)); this.v.set(t.x, t.y + rise, DEFAULT_Z + 0.4).project(cam); const sx = (this.v.x * 0.5 + 0.5) * w; const sy = (-this.v.y * 0.5 + 0.5) * h; // Rebote al aparecer (outBack) y desvanecido al final. const p = Math.min(1, t.age / 0.22); const c = 2.6; const pop = 1 + (c + 1) * Math.pow(p - 1, 3) + c * Math.pow(p - 1, 2); const scale = Math.max(0, pop) * (u > 0.75 ? 1 - (u - 0.75) * 1.2 : 1); const alpha = u > 0.6 ? 1 - (u - 0.6) / 0.4 : 1; t.el.style.transform = `translate(${sx.toFixed(1)}px, ${sy.toFixed(1)}px) translate(-50%, -50%) scale(${scale.toFixed(3)})`; t.el.style.opacity = alpha.toFixed(3); } } } /** Activa un ambiente sin depender de la interfaz VfxService (no hace nada si el VFX real no está instalado). */ export function vfxAmbient(game: Game, kind: AmbientKind | null, opts?: AmbientOpts): void { const v = game.vfx as Partial; if (typeof v.ambient === 'function') v.ambient.call(v, kind, opts); } /** Lista de kinds aceptados por burst (útil para depurar). */ export function vfxBurstKinds(game: Game): string[] { const v = game.vfx as Partial; return v.fx ? v.fx.burstKinds() : []; } export function install(game: Game): void { const vfx = new Vfx(game); game.vfx = vfx; game.addSystem({ render: (_alpha, dt) => vfx.render(dt) }); game.events.on('zone:unload', () => vfx.clear()); }