/** * Diseño de los efectos: ráfagas (burst), estelas (trail), compuestos y ambientales. * Todo se construye a base de capas de partículas sobre ParticleEngine. */ import type { Game } from '../core/game'; import type { VfxOpts } from '../core/types'; import { AC, F, MODE, ParticleEngine, SC, Spec } from './particles'; import { Sp } from './sprites'; /** Opciones ampliadas (superconjunto de VfxOpts). */ export interface FxOpts extends VfxOpts { /** true: el efecto sigue animándose con el juego en pausa (efectos de UI). */ ui?: boolean; } export type AmbientKind = 'rain' | 'snow' | 'fireflies' | 'leaves' | 'embers' | 'dust' | 'bubbles'; export interface AmbientOpts { /** Multiplicador de densidad (1 por defecto). */ density?: number; color?: number; /** Viento horizontal en u/s. */ windX?: number; } /** Plano por defecto: un poco por delante del plano de juego. */ export const DEFAULT_Z = 0.6; const Y_ = 0xffc928, R_ = 0xf2452e, B_ = 0x2fa8f5, G_ = 0x5bd23c, P_ = 0xa455f0, W_ = 0xf4f1ff; const PALETTE = [Y_, R_, B_, G_, P_, W_, 0xff7ac8]; const TAU = Math.PI * 2; const rnd = (a: number, b: number): number => a + Math.random() * (b - a); const pm = (v: number): number => (Math.random() * 2 - 1) * v; type BurstFn = (x: number, y: number, z: number, S: number, o: FxOpts) => void; interface TrailState { kind: string; x: number; y: number; t: number; dist: number; time: number } interface TrailCfg { spacing: number; rate: number } const TRAILS: Record = { dust: { spacing: 0.42, rate: 0 }, fire: { spacing: 0.22, rate: 26 }, ice: { spacing: 0.26, rate: 12 }, star: { spacing: 0.32, rate: 0 }, sparkle: { spacing: 0.3, rate: 14 }, bubble: { spacing: 0.4, rate: 8 }, speed: { spacing: 0.34, rate: 0 }, smoke: { spacing: 0.3, rate: 12 }, }; export class Effects { private s = new Spec(); private ui = false; private bursts: Record; private trails: TrailState[] = []; /** Reloj propio (no avanza en pausa). */ clock = 0; private ambKind: AmbientKind | null = null; private ambDensity = 1; private ambColor: number | undefined; private ambWind = 0; private ambAcc = 0; constructor(private game: Game, private eng: ParticleEngine) { for (let i = 0; i < 16; i++) this.trails.push({ kind: '', x: 0, y: 0, t: -1, dist: 0, time: 0 }); const b = (fn: BurstFn): BurstFn => fn.bind(this); this.bursts = { star: b(this.star), dust: b(this.dust), hit: b(this.hit), poof: b(this.poof), splash: b(this.splash), fire: b(this.fire), ice: b(this.ice), spark: b(this.spark), heart: b(this.heart), bubble: b(this.bubble), confetti: b(this.confetti), smoke: b(this.smoke), emoji: b(this.emoji), shatter: b(this.shatter), land: b(this.land), sparkle: b(this.sparkle), explosion: b(this.explosion), powerup: b(this.powerup), transform: b(this.transform), collect: b(this.collect), freeze: b(this.freeze), levelup: b(this.levelup), }; } burstKinds(): string[] { return Object.keys(this.bursts); } burst(kind: string, x: number, y: number, o: FxOpts = {}): void { const fn = this.bursts[kind] ?? this.bursts.poof; const S = o.scale ?? 1; this.ui = !!o.ui; fn(x, y, o.z ?? DEFAULT_Z, S, o); this.ui = false; } ring(x: number, y: number, color = 0xffffff, scale = 1, z = DEFAULT_Z): void { this.ringAt(x, y, z, 0.5 * scale, 3.4 * scale, color, 0.4, 0.42, 0.02, 1, 0, 1.7); this.ringAt(x, y, z, 0.3 * scale, 2.5 * scale, 0xffffff, 0.34, 0.2, 0.01, 1, 0.06, 1.2); } reset(): void { for (const t of this.trails) t.t = -1; this.ambKind = null; this.ambAcc = 0; } // ───────────────────────── Primitivas ───────────────────────── private p(x: number, y: number, z: number): Spec { const s = this.s.reset(); s.x = x; s.y = y; s.z = z; return s; } private e(): void { if (this.ui) this.s.flags |= F.UI; this.eng.emit(this.s); } /** Ángulo de emisión: alrededor de (dirX, dirY) si se dan, o en todas direcciones. */ private dir(o: FxOpts, spread: number, i = 0, n = 1): number { if (o.dirX || o.dirY) return Math.atan2(o.dirY ?? 0, o.dirX ?? 0) + pm(spread); return ((i + Math.random() * 0.8) / n) * TAU; } /** Resplandor suave aditivo que se expande y se apaga. */ private glow(x: number, y: number, z: number, size: number, hex: number, life = 0.22, boost = 1.6, delay = 0): void { const s = this.p(x, y, z); s.cell = Sp.Glow; s.add = hex === 0xffffff ? 0.8 : 0.3; s.col(hex, hex === 0xffffff ? boost : Math.min(boost, 1.25)); s.s0 = size * 0.45; s.s1 = size; s.sc = SC.OUT; s.ac = AC.QUAD; s.life = life; s.delay = delay; this.e(); } /** Destello de 4 puntas que crece y desaparece. */ private glint(x: number, y: number, z: number, size: number, hex: number, life = 0.3, delay = 0, boost = 2, rot = 0): void { const s = this.p(x, y, z); s.cell = Sp.Sparkle; s.add = 1; s.col(hex, boost); s.s0 = size; s.sc = SC.SIN; s.life = life; s.delay = delay; s.rot = rot; s.rv = 1.5; this.e(); } /** Anillo procedural: diámetro d0→d1, grosor relativo th0→th1. */ private ringAt( x: number, y: number, z: number, d0: number, d1: number, hex: number, life = 0.35, th0 = 0.4, th1 = 0.02, asp = 1, delay = 0, boost = 1.6, ): void { const s = this.p(x, y, z); s.cell = Sp.Glow; s.add = hex === 0xffffff ? 0.6 : 0.12; s.col(hex, hex === 0xffffff ? boost : Math.min(boost, 1.3)); s.s0 = d0 / asp; s.s1 = d1 / asp; s.sc = SC.OUT; s.asp = asp; s.th0 = th0; s.th1 = th1; s.ac = AC.LATE; s.life = life; s.delay = delay; this.e(); } /** Estrellita maciza que sale disparada, gira, cae y encoge. */ private starBit(x: number, y: number, z: number, ang: number, speed: number, size: number, hex: number, life: number, delay = 0): void { const s = this.p(x, y, z); s.cell = Sp.Star; s.add = 0; s.col(hex, 1.12); s.vx = Math.cos(ang) * speed; s.vy = Math.sin(ang) * speed; s.drag = 3; s.grav = -8; s.s0 = s.s1 = size * 1.35; s.flags = F.POP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(9); s.life = life; s.delay = delay; this.e(); } /** Línea/chispa alineada con su velocidad. */ private streak( x: number, y: number, z: number, ang: number, speed: number, len: number, hex: number, life: number, boost = 2, drag = 6, grav = 0, start = 0, ): void { const s = this.p(x + Math.cos(ang) * start, y + Math.sin(ang) * start, z); s.cell = Sp.Streak; s.add = hex === 0xffffff ? 0.9 : 0.35; s.col(hex, hex === 0xffffff ? boost : Math.min(boost, 1.4)); s.vx = Math.cos(ang) * speed; s.vy = Math.sin(ang) * speed; s.drag = drag; s.grav = grav; s.s0 = len * 0.55; s.s1 = len * 0.12; s.asp = 2.2; s.rot = ang; s.flags = F.ALIGN; s.ac = AC.LATE; s.life = life; this.e(); } /** Bolita/nube que se expande y encoge. */ private puff( x: number, y: number, z: number, vx: number, vy: number, size: number, hex: number, life: number, cell: number = Sp.Disc, alpha = 1, delay = 0, ): void { const s = this.p(x, y, z); s.cell = cell; s.col(hex); s.a = alpha; s.vx = vx; s.vy = vy; s.drag = 4.5; s.s0 = size * 0.55; s.s1 = size; s.sc = SC.OUT; s.flags = F.POP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(2); s.life = life; s.delay = delay; this.e(); } /** Humo: sube, crece y se desvanece. */ private smokePuff(x: number, y: number, z: number, size: number, hex: number, life: number, delay = 0, alpha = 0.55, vy = 1.4): void { const s = this.p(x, y, z); s.cell = Sp.Smoke; s.col(hex); s.a = alpha; s.vx = pm(0.7); s.vy = vy * rnd(0.7, 1.3); s.drag = 1.2; s.s0 = size * 0.5; s.s1 = size * 1.5; s.sc = SC.OUT; s.rot = rnd(0, TAU); s.rv = pm(1.2); s.ac = AC.INOUT; s.life = life; s.delay = delay; this.e(); } private floorAt(x: number, y: number): number { const g = this.game.physics.groundBelow(x, y + 0.2, 8); return g === null ? y - 1.2 : g + 0.08; } // ───────────────────────── Ráfagas del catálogo ───────────────────────── private star(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? Y_; const n = o.count ?? 7; this.glow(x, y, z, 1.9 * S, c, 0.25, 1.4); this.glint(x, y, z, 1.5 * S, 0xffffff, 0.24); for (let i = 0; i < n; i++) { this.starBit(x, y, z, this.dir(o, 0.9, i, n), rnd(4.5, 7.5) * S, rnd(0.3, 0.46) * S, c, rnd(0.5, 0.75)); } for (let i = 0; i < 4; i++) this.glint(x + pm(0.8) * S, y + pm(0.8) * S, z, rnd(0.3, 0.5) * S, 0xffffff, 0.3, rnd(0.08, 0.3)); } private dust(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xe9dcc0; const n = o.count ?? 5; const dx = o.dirX ?? 0; for (let i = 0; i < n; i++) { const side = dx !== 0 ? Math.sign(dx) : i % 2 === 0 ? 1 : -1; this.puff(x + pm(0.15) * S, y + rnd(0, 0.12) * S, z, side * rnd(1.2, 3) * S, rnd(0.4, 1.5) * S + (o.dirY ?? 0), rnd(0.26, 0.42) * S, c, rnd(0.35, 0.55), Sp.Smoke, 0.85); } } private land(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xefe4cc; const n = o.count ?? 8; const dx = o.dirX ?? 0; for (let i = 0; i < n; i++) { let side = i % 2 === 0 ? 1 : -1; if (dx !== 0 && Math.random() < 0.75) side = Math.sign(dx); const sp = rnd(2.2, 5.2) * S; this.puff(x + side * rnd(0.1, 0.3) * S, y + rnd(0.02, 0.14) * S, z, side * sp, rnd(0.3, 1.3) * S, rnd(0.3, 0.5) * S, c, rnd(0.38, 0.58), Sp.Smoke, 0.9); } // Piedritas que saltan for (let i = 0; i < 4; i++) { const s = this.p(x + pm(0.3) * S, y + 0.05, z); s.cell = Sp.Dot; s.col(c, 0.85); s.vx = pm(3.5) * S; s.vy = rnd(2.5, 4.5) * S; s.grav = -20; s.drag = 1; s.s0 = s.s1 = rnd(0.07, 0.11) * S; s.flags = F.SHRINK; s.life = rnd(0.3, 0.45); this.e(); } this.ringAt(x, y + 0.04 * S, z, 0.4 * S, 2.6 * S, 0xffffff, 0.3, 0.3, 0.02, 4.2, 0, 0.9); } private hit(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? Y_; const n = o.count ?? 6; this.glow(x, y, z, 1.9 * S, c, 0.2, 1.3); this.glint(x, y, z, 2.4 * S, 0xffffff, 0.18, 0, 1.8); this.glint(x, y, z, 1.9 * S, c, 0.26, 0, 2, Math.PI / 4); this.ringAt(x, y, z, 0.5 * S, 2.7 * S, c, 0.26, 0.24, 0.02, 1, 0, 1.4); const lines = n + 2; for (let i = 0; i < lines; i++) { this.streak(x, y, z, this.dir(o, 1.0, i, lines), rnd(9, 14) * S, rnd(0.9, 1.3) * S, 0xffffff, rnd(0.18, 0.26), 2.2, 7, 0, 0.45 * S); } for (let i = 0; i < n; i++) { this.starBit(x, y, z, this.dir(o, 1.1, i, n), rnd(5, 8.5) * S, rnd(0.3, 0.44) * S, c, rnd(0.45, 0.65)); } } private poof(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? W_; const n = o.count ?? 9; this.puff(x, y, z, 0, 0, 1.0 * S, c, 0.32); for (let i = 0; i < n; i++) { const a = this.dir(o, 1.2, i, n); const sp = rnd(2.8, 4.8) * S; this.puff(x, y, z, Math.cos(a) * sp, Math.sin(a) * sp + 0.6, rnd(0.45, 0.75) * S, c, rnd(0.42, 0.62)); } for (let i = 0; i < 4; i++) { this.starBit(x, y, z, rnd(0, TAU), rnd(5, 7) * S, rnd(0.2, 0.3) * S, Y_, rnd(0.45, 0.6), 0.04); } this.ringAt(x, y, z, 0.4 * S, 2.4 * S, 0xffffff, 0.3, 0.22, 0.02, 1, 0, 1.1); } private splash(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0x7fd6ff; const n = o.count ?? 11; const dx = o.dirX ?? 0; this.ringAt(x, y, z, 0.4 * S, 3.0 * S, 0xffffff, 0.45, 0.3, 0.02, 3.6, 0, 1.2); this.ringAt(x, y, z, 0.3 * S, 1.9 * S, c, 0.4, 0.3, 0.02, 3.6, 0.08, 1.3); this.glow(x, y + 0.2 * S, z, 1.7 * S, 0xffffff, 0.2, 1.1); for (let i = 0; i < n; i++) { const a = Math.PI * (0.2 + 0.6 * ((i + Math.random()) / n)) - dx * 0.35; const sp = rnd(5.5, 9) * S; const s = this.p(x + Math.cos(a) * 0.25 * S, y + 0.05, z); s.cell = Sp.Drop; s.add = 0.35; s.col(i % 3 === 0 ? 0xffffff : c, 1.25); s.vx = Math.cos(a) * sp * 0.75; s.vy = Math.sin(a) * sp + (o.dirY ?? 0); s.grav = -24; s.drag = 0.6; s.s0 = rnd(0.26, 0.4) * S; s.s1 = s.s0 * 0.6; s.asp = 1.25; s.flags = F.ALIGN | F.SHRINK; s.rot = a; s.life = rnd(0.5, 0.75); this.e(); } for (let i = 0; i < 5; i++) { const s = this.p(x + pm(0.5) * S, y + 0.05, z); s.cell = Sp.Dot; s.add = 0.4; s.col(0xffffff, 1.2); s.vx = pm(2.2) * S; s.vy = rnd(2.5, 5.5) * S; s.grav = -18; s.s0 = s.s1 = rnd(0.08, 0.14) * S; s.flags = F.SHRINK; s.life = rnd(0.35, 0.6); this.e(); } } private fire(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xff8a1e; const n = o.count ?? 9; const dx = (o.dirX ?? 0) * 2.5; const dy = (o.dirY ?? 0) * 2.5; this.glow(x, y, z, 2.0 * S, c, 0.3, 1.2); for (let i = 0; i < n; i++) { const s = this.p(x + pm(0.38) * S, y + pm(0.2) * S, z); s.cell = Sp.Flame; s.add = 0.3; s.col(0xffd23a, 1.3).col2(0xff5a1e, 1.05); if (o.color !== undefined) s.col(c, 1.4).col2(c, 0.95); s.vx = pm(1.3) * S + dx; s.vy = rnd(1.8, 4.2) * S + dy; s.grav = 3; s.drag = 1.5; s.s0 = rnd(0.7, 1.05) * S; s.s1 = s.s0 * 0.4; s.flags = F.POP | F.SHRINK; s.rot = pm(0.35); s.rv = pm(1.5); s.life = rnd(0.4, 0.7); s.delay = rnd(0, 0.1); this.e(); } for (let i = 0; i < Math.ceil(n * 0.8); i++) { const s = this.p(x + pm(0.4) * S, y, z); s.cell = Sp.Glow; s.add = 1; s.col(0xfff0a0, 2.4).col2(0xff5a1e, 1.5); s.vx = pm(2.6) * S + dx; s.vy = rnd(2.5, 6.5) * S + dy; s.grav = 1.5; s.drag = 1.6; s.swA = 2; s.swF = 9; s.s0 = rnd(0.12, 0.2) * S; s.s1 = 0.02; s.life = rnd(0.5, 0.95); this.e(); } for (let i = 0; i < 3; i++) this.smokePuff(x + pm(0.3) * S, y + 0.5 * S, z - 0.02, rnd(0.6, 0.8) * S, 0x4a4252, rnd(0.7, 0.95), 0.18 + i * 0.07, 0.42, 2); } private ice(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0x9fe8ff; const n = o.count ?? 6; this.glow(x, y, z, 2.2 * S, c, 0.28, 1.3); this.glint(x, y, z, 2.0 * S, 0xffffff, 0.24, 0, 2); this.ringAt(x, y, z, 0.4 * S, 2.6 * S, c, 0.32, 0.26, 0.02, 1, 0, 1.5); for (let i = 0; i < 3; i++) this.smokePuff(x + pm(0.4) * S, y + pm(0.2) * S, z - 0.02, rnd(0.7, 1) * S, 0xdff6ff, rnd(0.55, 0.8), i * 0.04, 0.4, 0.5); for (let i = 0; i < n + 1; i++) { const a = this.dir(o, 1.1, i, n + 1); const sp = rnd(4.5, 8) * S; const s = this.p(x, y, z); s.cell = Sp.Shard; s.add = 0.45; s.col(i % 2 ? 0xffffff : c, 1.3); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp + 1.5; s.grav = -15; s.drag = 2; s.s0 = s.s1 = rnd(0.2, 0.34) * S; s.flags = F.POP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(12); s.life = rnd(0.45, 0.7); this.e(); } for (let i = 0; i < n; i++) { const a = rnd(0, TAU); const sp = rnd(1.5, 3.6) * S; const s = this.p(x + Math.cos(a) * 0.2, y + Math.sin(a) * 0.2, z); s.cell = Sp.Flake; s.add = 0.7; s.col(0xeafaff, 1.5); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp + 0.8; s.drag = 3; s.grav = -1.6; s.swA = 0.8; s.swF = 5; s.s0 = s.s1 = rnd(0.28, 0.46) * S; s.flags = F.POP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(3); s.life = rnd(0.7, 1.05); this.e(); } } private spark(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xffe25a; const n = o.count ?? 10; this.glow(x, y, z, 1.4 * S, c, 0.16, 1.8); this.glint(x, y, z, 1.2 * S, 0xffffff, 0.16, 0, 2.4); for (let i = 0; i < n; i++) { this.streak(x, y, z, this.dir(o, 0.8, i, n), rnd(6, 13) * S, rnd(0.45, 0.8) * S, i % 3 ? c : 0xffffff, rnd(0.25, 0.45), 2.4, 3.5, -14); } } private heart(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xff5fa2; const n = o.count ?? 5; this.glow(x, y, z, 1.6 * S, c, 0.3, 1.1); for (let i = 0; i < n; i++) { const s = this.p(x + pm(0.5) * S, y + pm(0.25) * S, z); s.cell = Sp.Heart; s.add = 0.15; s.col(c, 1.2); s.vx = pm(1.4) * S + (o.dirX ?? 0); s.vy = rnd(1.6, 3) * S + (o.dirY ?? 0); s.drag = 1.2; s.swA = 1.6; s.swF = 5; s.s0 = s.s1 = rnd(0.46, 0.66) * S; s.flags = F.POP | F.SHRINK; s.rot = pm(0.3); s.life = rnd(0.75, 1.1); s.delay = i * 0.06; this.e(); } for (let i = 0; i < 4; i++) this.glint(x + pm(0.7) * S, y + rnd(0, 0.9) * S, z, rnd(0.25, 0.4) * S, 0xffffff, 0.3, rnd(0.1, 0.5)); } private bubble(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xbfefff; const n = o.count ?? 7; for (let i = 0; i < n; i++) { const a = this.dir(o, 1.2, i, n); const sp = rnd(1.5, 3.5) * S; const s = this.p(x + pm(0.2) * S, y + pm(0.2) * S, z); s.cell = Sp.Bubble; s.add = 0.5; s.col(c, 1.35); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp * 0.6 + rnd(0.8, 1.8); s.drag = 2.4; s.grav = 2.4; s.swA = 1.2; s.swF = 6; s.s0 = rnd(0.2, 0.3) * S; s.s1 = rnd(0.3, 0.5) * S; s.sc = SC.OUT; s.flags = F.POP; s.ac = AC.LATE; s.life = rnd(0.6, 1.0); this.e(); } this.ringAt(x, y, z, 0.3 * S, 1.6 * S, 0xffffff, 0.25, 0.2, 0.02, 1, 0, 1.1); } private confetti(x: number, y: number, z: number, S: number, o: FxOpts): void { const n = o.count ?? 26; for (let i = 0; i < n; i++) { const a = o.dirX || o.dirY ? this.dir(o, 0.7) : Math.PI / 2 + pm(1.25); const sp = rnd(5, 11) * S; const s = this.p(x + pm(0.2) * S, y + pm(0.2) * S, z + pm(0.2)); s.cell = Sp.Confetti; s.col(o.color !== undefined && i % 3 === 0 ? o.color : PALETTE[i % PALETTE.length], 1.15); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp; s.grav = -8; s.drag = 2.4; s.swA = 2.2; s.swF = rnd(4, 8); s.s0 = s.s1 = rnd(0.16, 0.26) * S; s.asp = 1.5; s.flags = F.FLIP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(7); s.life = rnd(1.1, 1.8); this.e(); } this.glint(x, y, z, 1.6 * S, 0xffffff, 0.2); } private smoke(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0x8a8798; const n = o.count ?? 6; for (let i = 0; i < n; i++) { const s = this.p(x + pm(0.35) * S, y + pm(0.25) * S, z); s.cell = Sp.Smoke; s.col(c); s.a = 0.7; s.vx = pm(1.4) * S + (o.dirX ?? 0) * 1.5; s.vy = rnd(0.8, 2.2) * S + (o.dirY ?? 0) * 1.5; s.drag = 1.4; s.s0 = rnd(0.4, 0.6) * S; s.s1 = rnd(1.0, 1.4) * S; s.sc = SC.OUT; s.rot = rnd(0, TAU); s.rv = pm(1.4); s.ac = AC.INOUT; s.life = rnd(0.7, 1.1); s.delay = i * 0.03; this.e(); } } private emoji(x: number, y: number, z: number, S: number, o: FxOpts): void { const cell = this.eng.atlas.emoji(o.emoji ?? '⭐'); const n = o.count ?? 5; for (let i = 0; i < n; i++) { const a = o.dirX || o.dirY ? this.dir(o, 0.6) : Math.PI / 2 + pm(1.1); const sp = rnd(4, 7.5) * S; const s = this.p(x + pm(0.15) * S, y + pm(0.15) * S, z + i * 0.01); s.cell = cell; s.col(0xffffff, 1.05); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp; s.grav = -13; s.drag = 1.2; s.s0 = s.s1 = rnd(0.6, 0.8) * S; s.flags = F.POP | F.SHRINK; s.rot = pm(0.4); s.rv = pm(4); s.life = rnd(0.7, 1.0); this.e(); } this.ringAt(x, y, z, 0.3 * S, 1.8 * S, 0xffffff, 0.25, 0.2, 0.02, 1, 0, 1.1); } private shatter(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xbfefff; const n = o.count ?? 12; const floor = this.floorAt(x, y); this.glow(x, y, z, 2.2 * S, c, 0.2, 1.4); this.glint(x, y, z, 2.2 * S, 0xffffff, 0.2, 0, 2.2); this.ringAt(x, y, z, 0.5 * S, 2.6 * S, 0xffffff, 0.28, 0.25, 0.02); for (let i = 0; i < n; i++) { const a = this.dir(o, 1.2, i, n); const sp = rnd(3.5, 8) * S; const s = this.p(x + Math.cos(a) * 0.2 * S, y + Math.sin(a) * 0.2 * S, z + pm(0.15)); s.cell = Sp.Shard; s.add = 0.2; s.col(i % 3 === 0 ? 0xffffff : c, 1.2); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp + 3 * S; s.grav = -26; s.drag = 0.5; s.s0 = s.s1 = rnd(0.2, 0.42) * S; s.flags = F.BOUNCE | F.SHRINK; s.floor = floor; s.rot = rnd(0, TAU); s.rv = pm(16); s.life = rnd(0.8, 1.2); this.e(); } for (let i = 0; i < 5; i++) this.glint(x + pm(0.9) * S, y + pm(0.7) * S, z, rnd(0.3, 0.5) * S, 0xffffff, 0.28, rnd(0.05, 0.35)); } private sparkle(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xfff2a8; const n = o.count ?? 6; this.glint(x, y, z, 1.0 * S, 0xffffff, 0.3, 0, 2.2); this.glow(x, y, z, 1.3 * S, c, 0.3, 0.9); for (let i = 0; i < n; i++) { const a = rnd(0, TAU); const r = rnd(0.3, 0.85) * S; this.glint(x + Math.cos(a) * r + (o.dirX ?? 0) * 0.3, y + Math.sin(a) * r + (o.dirY ?? 0) * 0.3, z, rnd(0.35, 0.7) * S, i % 2 ? c : 0xffffff, rnd(0.28, 0.42), 0.04 + i * 0.05, 2); } } // ───────────────────────── Compuestos ───────────────────────── /** Explosión: destello + anillo + bolas de fuego + humo + chispas + pequeño temblor de cámara. */ private explosion(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0xff8a1e; const n = o.count ?? 10; this.glow(x, y, z, 3.2 * S, 0xffffff, 0.14, 1.8); this.glow(x, y, z, 4.4 * S, c, 0.4, 1.3); this.glint(x, y, z, 4.2 * S, 0xffffff, 0.2, 0, 2); this.ringAt(x, y, z, 0.8 * S, 5.4 * S, c, 0.42, 0.4, 0.02, 1, 0, 1.4); for (let i = 0; i < n; i++) { const a = (i / n) * TAU + pm(0.3); const sp = rnd(3, 6.5) * S; const s = this.p(x, y, z); s.cell = Sp.Disc; s.add = 0.25; s.col(0xffd23a, 1.25).col2(0xf2452e, 0.95); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp; s.drag = 4.5; s.s0 = rnd(0.6, 0.8) * S; s.s1 = rnd(1.0, 1.4) * S; s.sc = SC.OUT; s.flags = F.POP | F.SHRINK; s.life = rnd(0.4, 0.6); this.e(); } for (let i = 0; i < 6; i++) { const a = (i / 6) * TAU + pm(0.4); this.smokePuff(x + Math.cos(a) * 0.7 * S, y + Math.sin(a) * 0.7 * S, z - 0.02, rnd(1.0, 1.5) * S, 0x4a4252, rnd(0.8, 1.2), rnd(0.14, 0.26), 0.55, 1.2); } for (let i = 0; i < n + 6; i++) { this.streak(x, y, z, rnd(0, TAU), rnd(9, 17) * S, rnd(0.6, 1.1) * S, i % 2 ? 0xffe45a : 0xffffff, rnd(0.35, 0.6), 2.4, 3, -12, 0.3 * S); } for (let i = 0; i < 5; i++) this.starBit(x, y, z, rnd(0.3, Math.PI - 0.3), rnd(7, 10) * S, rnd(0.3, 0.42) * S, Y_, rnd(0.6, 0.8)); this.game.gfx.cam.shake(0.25 * Math.min(2, S), 0.3); } /** Columna de luz y partículas de brillo compartidas por powerup/levelup. */ private column(x: number, y: number, z: number, S: number, c: number, life: number): void { for (let i = 0; i < 2; i++) { const s = this.p(x, y + 2.6 * S, z - 0.05); s.cell = Sp.Glow; s.add = i ? 1 : 0.4; s.col(i ? 0xffffff : c, i ? 1.2 : 1.2); s.s0 = 7.5 * S; s.s1 = 8.5 * S; s.asp = i ? 0.12 : 0.26; s.ac = AC.INOUT; s.a = 0.85; s.life = life; this.e(); } for (let i = 0; i < 10; i++) { this.streak(x + pm(0.55) * S, y + rnd(0, 1.5) * S, z, Math.PI / 2, rnd(5, 10) * S, rnd(0.7, 1.3) * S, i % 2 ? c : 0xffffff, rnd(0.4, 0.7), 1.8, 1.5); this.s.delay = 0; // (la última spec ya se emitió; nada que hacer) } } /** Espiral de estrellas ascendentes alrededor de (x, y). */ private spiral(x: number, y: number, z: number, S: number, c: number, n: number, rad0: number, rad1: number, rise: number, life: number, cell: number = Sp.Star): void { for (let i = 0; i < n; i++) { const s = this.p(x, y, z); s.mode = MODE.ORBIT; s.cell = cell; s.add = cell === Sp.Star ? 0.3 : 1; s.col(i % 3 === 0 ? 0xffffff : c, cell === Sp.Star ? 1.3 : 1.9); s.ang = (i / n) * TAU * 2; s.angV = 7.5; s.rad0 = rad0 * S; s.rad1 = rad1 * S; s.vy = rise * S; s.s0 = s.s1 = rnd(0.24, 0.38) * S; s.flags = F.POP | F.SHRINK; s.rv = 5; s.life = life; s.delay = (i / n) * life * 0.6; this.e(); } } /** Power-up: columna de luz + espiral de estrellas ascendentes + anillos. */ private powerup(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? Y_; this.column(x, y, z, S, c, 0.9); this.glow(x, y + 0.4 * S, z, 3 * S, c, 0.5, 1.4); this.spiral(x, y, z, S, c, o.count ?? 14, 1.1, 0.5, 3.4, 0.8); this.ringAt(x, y + 0.05 * S, z, 0.5 * S, 4.2 * S, c, 0.5, 0.3, 0.02, 3.4, 0, 1.6); this.ringAt(x, y + 0.05 * S, z, 0.5 * S, 3.4 * S, 0xffffff, 0.5, 0.25, 0.02, 3.4, 0.18, 1.3); for (let i = 0; i < 6; i++) this.glint(x + pm(1.1) * S, y + rnd(0.2, 2.6) * S, z, rnd(0.3, 0.55) * S, 0xffffff, 0.32, rnd(0.2, 0.8)); } /** Transformación: remolino del color del poder que converge + destello. */ private transform(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? P_; const n = o.count ?? 16; for (let i = 0; i < n; i++) { const s = this.p(x, y, z); s.mode = MODE.ORBIT; s.cell = i % 4 === 0 ? Sp.Sparkle : Sp.Glow; s.add = i % 3 === 0 ? 1 : 0.3; s.col(i % 3 === 0 ? 0xffffff : c, 1.3); s.ang = (i / n) * TAU; s.angV = 13; s.rad0 = rnd(1.5, 1.9) * S; s.rad1 = 0.1 * S; s.s0 = rnd(0.3, 0.5) * S; s.s1 = 0.12 * S; s.life = 0.36; s.delay = (i % 4) * 0.02; this.e(); } // La órbita se aplasta en Y (falso 3D): añadimos un remolino vertical cruzado con líneas. for (let i = 0; i < 8; i++) { const a = (i / 8) * TAU; const s = this.p(x + Math.cos(a) * 1.7 * S, y + Math.sin(a) * 1.7 * S, z); s.mode = MODE.HOME; s.tx = x; s.ty = y; s.vx = -Math.sin(a) * 3 * S; s.vy = Math.cos(a) * 3 * S; s.cell = Sp.Glow; s.add = 0.3; s.col(c, 1.3); s.s0 = 0.4 * S; s.s1 = 0.1 * S; s.life = 0.34; this.e(); } const d = 0.32; this.glow(x, y, z, 3.6 * S, c, 0.32, 1.8, d); this.glint(x, y, z, 3.4 * S, 0xffffff, 0.26, d, 2.6); this.ringAt(x, y, z, 0.5 * S, 3.6 * S, c, 0.36, 0.38, 0.02, 1, d, 1.8); for (let i = 0; i < 7; i++) this.starBit(x, y, z, (i / 7) * TAU + pm(0.3), rnd(5, 8) * S, rnd(0.26, 0.38) * S, i % 2 ? c : Y_, rnd(0.45, 0.65), d); } /** Recogida: chispitas que convergen al punto y un destellito final. */ private collect(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? Y_; const n = o.count ?? 8; for (let i = 0; i < n; i++) { const a = (i / n) * TAU + pm(0.3); const r = rnd(0.9, 1.4) * S; const s = this.p(x + Math.cos(a) * r, y + Math.sin(a) * r, z); s.mode = MODE.HOME; s.tx = x + (o.dirX ?? 0); s.ty = y + (o.dirY ?? 0); s.vx = -Math.sin(a) * 1.6 * S; s.vy = Math.cos(a) * 1.6 * S; s.cell = Sp.Sparkle; s.add = 1; s.col(i % 2 ? c : 0xffffff, 2); s.s0 = rnd(0.4, 0.6) * S; s.s1 = 0.15 * S; s.flags = F.POP; s.rv = 4; s.life = rnd(0.26, 0.34); this.e(); } const tx = x + (o.dirX ?? 0); const ty = y + (o.dirY ?? 0); this.glint(tx, ty, z, 1.5 * S, 0xffffff, 0.22, 0.26, 2.4); this.glow(tx, ty, z, 1.6 * S, c, 0.25, 1.5, 0.26); this.ringAt(tx, ty, z, 0.3 * S, 1.8 * S, c, 0.26, 0.25, 0.02, 1, 0.26, 1.5); } /** Congelación: onda helada + cristales que brotan + copos que quedan flotando + vaho. */ private freeze(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? 0x8fe3ff; const n = o.count ?? 8; this.glow(x, y, z, 1.8 * S, 0xffffff, 0.18, 1.4); this.glow(x, y, z, 3 * S, c, 0.5, 1.1); this.glint(x, y, z, 2.6 * S, 0xffffff, 0.3, 0, 1.8); this.ringAt(x, y, z, 0.5 * S, 3.6 * S, c, 0.4, 0.4, 0.02, 1, 0, 1.7); this.ringAt(x, y, z, 0.4 * S, 2.6 * S, 0xffffff, 0.4, 0.2, 0.02, 1, 0.1, 1.3); for (let i = 0; i < n; i++) { // Cristales: esquirlas alargadas que brotan hacia fuera y se quedan un instante. const a = (i / n) * TAU + pm(0.15); const s = this.p(x + Math.cos(a) * 0.55 * S, y + Math.sin(a) * 0.55 * S, z); s.cell = Sp.Streak; s.add = 0.8; s.col(i % 2 ? 0xffffff : c, 1.7); s.vx = Math.cos(a) * 3.2 * S; s.vy = Math.sin(a) * 3.2 * S; s.drag = 9; s.s0 = s.s1 = rnd(0.9, 1.3) * S; s.asp = 1; s.rot = a; s.flags = F.POP | F.SHRINK | F.ALIGN; s.life = rnd(0.5, 0.7); this.e(); } for (let i = 0; i < n + 2; i++) { const a = rnd(0, TAU); const sp = rnd(3, 6) * S; const s = this.p(x, y, z); s.cell = Sp.Flake; s.add = 0.7; s.col(0xeafaff, 1.5); s.vx = Math.cos(a) * sp; s.vy = Math.sin(a) * sp; s.drag = 5; s.grav = -1.2; s.swA = 0.7; s.swF = 4; s.s0 = s.s1 = rnd(0.3, 0.5) * S; s.flags = F.POP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(3); s.life = rnd(0.9, 1.3); this.e(); } for (let i = 0; i < 4; i++) this.smokePuff(x + pm(0.7) * S, y + pm(0.4) * S, z - 0.02, rnd(0.9, 1.3) * S, 0xdff6ff, rnd(0.7, 1), i * 0.05, 0.4, 0.3); } /** Subida de nivel: columna + doble espiral + anillos + fuente de estrellas + confeti. */ private levelup(x: number, y: number, z: number, S: number, o: FxOpts): void { const c = o.color ?? Y_; this.column(x, y, z, S * 1.15, c, 1.2); this.glow(x, y + 0.5 * S, z, 4 * S, c, 0.6, 1.5); this.glint(x, y + 0.5 * S, z, 3.2 * S, 0xffffff, 0.35, 0, 2.4); this.spiral(x, y, z, S, c, 12, 1.2, 0.7, 3.2, 1.0); this.spiral(x, y + 0.2 * S, z, S, 0xffffff, 10, 0.8, 1.5, 2.4, 1.0, Sp.Sparkle); for (let i = 0; i < 3; i++) this.ringAt(x, y + 0.05 * S, z, 0.5 * S, (4.8 - i * 0.6) * S, i === 1 ? 0xffffff : c, 0.55, 0.3, 0.02, 3.4, i * 0.16, 1.6); for (let i = 0; i < 9; i++) this.starBit(x, y + 0.6 * S, z, Math.PI / 2 + pm(0.9), rnd(7, 11) * S, rnd(0.3, 0.46) * S, c, rnd(0.7, 1.0), 0.15 + i * 0.03); const keep = this.ui; this.confetti(x, y + 1.2 * S, z, S, { count: o.count ?? 22 }); this.ui = keep; } // ───────────────────────── Estelas ───────────────────────── /** * Estela continua: se llama cada frame. Emite según la distancia recorrida y/o el tiempo, * así que quieto no ensucia (dust/star/speed) o emite a ritmo constante (fire/smoke…). */ trail(kindIn: string, x: number, y: number, o: FxOpts = {}): void { const kind = TRAILS[kindIn] ? kindIn : 'dust'; const cfg = TRAILS[kind]; const now = this.clock; // Empareja con el emisor del mismo tipo más cercano y reciente. let st: TrailState | null = null; let stale: TrailState = this.trails[0]; let best = 2.5 * 2.5; for (const t of this.trails) { if (t.t < stale.t) stale = t; if (t.kind !== kind || t.t < 0 || now - t.t > 0.3) continue; const d2 = (t.x - x) * (t.x - x) + (t.y - y) * (t.y - y); if (d2 < best) { best = d2; st = t; } } if (!st) { st = stale; st.kind = kind; st.x = x; st.y = y; st.t = now; st.dist = Math.random() * cfg.spacing; st.time = 0; if (cfg.rate <= 0) return; } const mx = x - st.x; const my = y - st.y; const moved = Math.hypot(mx, my); const dt = Math.min(0.1, now - st.t); const mult = o.count ?? 1; st.dist += moved; st.time += dt * cfg.rate * mult; st.x = x; st.y = y; st.t = now; let n = 0; const spacing = cfg.spacing / mult; if (st.dist >= spacing) { n += Math.floor(st.dist / spacing); st.dist %= spacing; } if (st.time >= 1) { n += Math.floor(st.time); st.time %= 1; } if (n <= 0) return; if (n > 5) n = 5; // Dirección de avance (normalizada): la del movimiento o la indicada. let dx = o.dirX ?? 0; let dy = o.dirY ?? 0; if (dx === 0 && dy === 0 && moved > 1e-4) { dx = mx / moved; dy = my / moved; } const S = o.scale ?? 1; const z = o.z ?? DEFAULT_Z - 0.1; this.ui = !!o.ui; for (let i = 0; i < n; i++) { // Reparte las emisiones a lo largo del tramo recorrido. const k = n > 1 ? i / n : 0; this.trailBit(kind, x - mx * k, y - my * k, z, S, dx, dy, o.color); } this.ui = false; } private trailBit(kind: string, x: number, y: number, z: number, S: number, dx: number, dy: number, color?: number): void { switch (kind) { case 'dust': this.puff(x + pm(0.1) * S, y + rnd(0, 0.1) * S, z, -dx * rnd(0.6, 1.6), rnd(0.5, 1.3), rnd(0.24, 0.38) * S, color ?? 0xe9dcc0, rnd(0.3, 0.45), Sp.Smoke, 0.8); break; case 'fire': { const s = this.p(x + pm(0.22) * S, y + pm(0.22) * S, z); s.cell = Sp.Flame; s.add = 0.3; s.col(0xffd23a, 1.3).col2(color ?? 0xff5a1e, 1.05); s.vx = -dx * rnd(0.5, 1.5) + pm(0.5); s.vy = rnd(1, 2.4) - dy * 0.8; s.grav = 2.5; s.drag = 1.5; s.s0 = rnd(0.65, 0.95) * S; s.s1 = s.s0 * 0.55; s.flags = F.POP | F.SHRINK; s.rot = pm(0.4); s.rv = pm(2); s.life = rnd(0.3, 0.5); this.e(); if (Math.random() < 0.35) { const k = this.p(x + pm(0.2) * S, y, z); k.cell = Sp.Glow; k.add = 1; k.col(0xfff0a0, 2.4).col2(0xff5a1e, 1.5); k.vx = pm(1.5) - dx; k.vy = rnd(1.5, 4); k.swA = 1.5; k.swF = 9; k.s0 = rnd(0.1, 0.16) * S; k.s1 = 0.02; k.life = rnd(0.4, 0.7); this.e(); } break; } case 'ice': { const s = this.p(x + pm(0.3) * S, y + pm(0.3) * S, z); const flake = Math.random() < 0.6; s.cell = flake ? Sp.Flake : Sp.Sparkle; s.add = 0.8; s.col(flake ? 0xeafaff : color ?? 0x9fe8ff, 1.6); s.vx = -dx * 0.5 + pm(0.4); s.vy = -rnd(0.3, 1); s.swA = 0.8; s.swF = 5; s.s0 = s.s1 = rnd(0.3, 0.46) * S; s.flags = F.POP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(3); s.life = rnd(0.45, 0.75); this.e(); if (Math.random() < 0.3) this.smokePuff(x, y, z - 0.02, 0.5 * S, 0xdff6ff, 0.5, 0, 0.3, 0.3); break; } case 'star': { const s = this.p(x + pm(0.2) * S, y + pm(0.2) * S, z); s.cell = Sp.Star; s.add = 0.25; s.col(color ?? Y_, 1.25); s.vx = -dx * rnd(0.3, 1.2) + pm(0.6); s.vy = rnd(-0.2, 1); s.grav = -3; s.s0 = s.s1 = rnd(0.34, 0.5) * S; s.flags = F.POP | F.SHRINK; s.rot = rnd(0, TAU); s.rv = pm(6); s.life = rnd(0.4, 0.65); this.e(); if (Math.random() < 0.5) this.glint(x + pm(0.35) * S, y + pm(0.35) * S, z, rnd(0.2, 0.35) * S, 0xffffff, 0.28, rnd(0, 0.1)); break; } case 'sparkle': this.glint(x + pm(0.4) * S, y + pm(0.4) * S, z, rnd(0.25, 0.5) * S, Math.random() < 0.5 ? color ?? 0xfff2a8 : 0xffffff, rnd(0.3, 0.45), 0, 2); break; case 'bubble': { const s = this.p(x + pm(0.25) * S, y + pm(0.25) * S, z); s.cell = Sp.Bubble; s.add = 0.5; s.col(color ?? 0xbfefff, 1.35); s.vx = -dx * 0.5 + pm(0.4); s.vy = rnd(0.8, 1.8); s.swA = 1.2; s.swF = 6; s.s0 = rnd(0.2, 0.28) * S; s.s1 = rnd(0.34, 0.5) * S; s.sc = SC.OUT; s.flags = F.POP; s.ac = AC.LATE; s.life = rnd(0.5, 0.9); this.e(); break; } case 'speed': { // Líneas de velocidad: detrás del móvil, paralelas al avance. const off = pm(0.42) * S; const back = rnd(0.2, 0.7) * S; const s = this.p(x - dx * back - dy * off, y - dy * back + dx * off, z); s.cell = Sp.Streak; s.add = 0.8; s.col(color ?? 0xffffff, 1.5); s.a = 0.85; s.vx = -dx * 1.5; s.vy = -dy * 1.5; s.s0 = rnd(0.9, 1.4) * S; s.s1 = s.s0 * 0.6; s.asp = 2.6; s.rot = Math.atan2(dy, dx); s.ac = AC.LIN; s.life = rnd(0.14, 0.22); this.e(); break; } default: { const s = this.p(x + pm(0.15) * S, y + pm(0.15) * S, z); s.cell = Sp.Smoke; s.col(color ?? 0x8a8798); s.a = 0.55; s.vx = -dx * rnd(0.3, 1) + pm(0.3); s.vy = rnd(0.5, 1.4); s.drag = 1.2; s.s0 = rnd(0.25, 0.38) * S; s.s1 = rnd(0.6, 0.85) * S; s.sc = SC.OUT; s.rot = rnd(0, TAU); s.rv = pm(1.4); s.ac = AC.INOUT; s.life = rnd(0.5, 0.8); this.e(); } } } // ───────────────────────── Ambientales ───────────────────────── get ambientKind(): AmbientKind | null { return this.ambKind; } ambient(kind: AmbientKind | null, opts: AmbientOpts = {}): void { const changed = kind !== this.ambKind; this.ambKind = kind; this.ambDensity = opts.density ?? 1; this.ambColor = opts.color; this.ambWind = opts.windX ?? 0; this.ambAcc = 0; if (kind && changed) { // Precalienta: llena la pantalla de golpe para que no "empiece a llover" desde arriba. const n = Math.round(AMB[kind].rate * AMB[kind].life * this.ambDensity * 0.8); for (let i = 0; i < n; i++) this.ambientBit(kind, true); } } /** Emisión continua del ambiente activo (llamar con el dt de simulación visual). */ updateAmbient(dt: number): void { const kind = this.ambKind; if (!kind) return; this.ambAcc += dt * AMB[kind].rate * this.ambDensity; let n = Math.floor(this.ambAcc); this.ambAcc -= n; if (n > 12) n = 12; for (let i = 0; i < n; i++) this.ambientBit(kind, false); } private ambientBit(kind: AmbientKind, anywhere: boolean): void { const cam = this.game.gfx.camera; const rig = this.game.gfx.cam; const halfH = Math.tan((cam.fov * Math.PI) / 360) * Math.max(4, cam.position.z) * 1.12; const halfW = halfH * cam.aspect; const cx = rig.x; const cy = rig.y; const wind = this.ambWind; const c = this.ambColor; const rx = cx + pm(halfW + 2); const ry = cy + pm(halfH); const top = cy + halfH + 0.5; const z = rnd(-1.5, 2.5); const s = this.p(rx, ry, z); switch (kind) { case 'rain': { const vy = -rnd(24, 30); const vx = wind - 3; s.y = anywhere ? ry : top + rnd(0, 3); s.x = rx - (vx / vy) * (s.y - cy); s.cell = Sp.Streak; s.add = 0.5; s.col(c ?? 0xbfe4ff, 1.2); s.a = 0.6; s.vx = vx; s.vy = vy; s.rot = Math.atan2(vy, vx); s.s0 = s.s1 = rnd(0.5, 0.8); s.asp = 1.8; s.life = ((s.y - (cy - halfH)) / -vy) * rnd(0.8, 1.05); s.ac = AC.HOLD; break; } case 'snow': s.y = anywhere ? ry : top; s.cell = Math.random() < 0.3 ? Sp.Flake : Sp.Glow; s.add = 0.3; s.col(c ?? 0xffffff, 1.15); s.a = 0.9; s.vx = wind + pm(0.3); s.vy = -rnd(1.2, 2.2); s.swA = 0.9; s.swF = rnd(1, 2.4); s.s0 = s.s1 = s.cell === Sp.Flake ? rnd(0.22, 0.34) : rnd(0.14, 0.24); s.rv = pm(1.5); s.life = (halfH * 2 + 1) / -s.vy; s.ac = AC.INOUT; if (anywhere) s.life *= (s.y - (cy - halfH)) / (halfH * 2) + 0.05; break; case 'fireflies': s.cell = Sp.Glow; s.add = 1; s.col(c ?? 0xd8ff5a, 2.2); s.vx = wind + pm(0.5); s.vy = pm(0.4); s.swA = 0.9; s.swF = rnd(1.5, 3); s.s0 = rnd(0.16, 0.26); s.s1 = s.s0; s.sc = SC.LIN; s.life = rnd(2.5, 4.5); s.ac = AC.SIN; break; case 'leaves': s.y = anywhere ? ry : top; s.cell = Sp.Leaf; s.col(c ?? [G_, 0x9fd84a, 0xffb347, 0xe8862a][(Math.random() * 4) | 0], 1.05); s.vx = wind - rnd(0.6, 1.6); s.vy = -rnd(1.1, 2); s.swA = 1.6; s.swF = rnd(1.5, 3); s.s0 = s.s1 = rnd(0.24, 0.38); s.rot = rnd(0, TAU); s.rv = pm(3); s.flags = F.FLIP; s.life = (halfH * 2 + 1) / -s.vy; s.ac = AC.INOUT; if (anywhere) s.life *= (s.y - (cy - halfH)) / (halfH * 2) + 0.05; break; case 'embers': s.y = anywhere ? ry : cy - halfH + rnd(0, halfH); s.cell = Sp.Glow; s.add = 1; s.col(c ?? 0xffb347, 2.4).col2(0xff4a1e, 1.6); s.vx = wind + pm(0.5); s.vy = rnd(1.2, 2.8); s.swA = 1.3; s.swF = rnd(2, 4); s.s0 = rnd(0.1, 0.2); s.s1 = 0.04; s.life = rnd(2.5, 4); s.ac = AC.SIN; break; case 'dust': s.cell = Sp.Glow; s.add = 0.6; s.col(c ?? 0xfff6dc, 1.1); s.a = 0.5; s.vx = wind + pm(0.25); s.vy = pm(0.2) + 0.08; s.swA = 0.3; s.swF = rnd(0.8, 1.6); s.s0 = s.s1 = rnd(0.08, 0.18); s.life = rnd(3.5, 6); s.ac = AC.SIN; break; default: // bubbles s.y = anywhere ? ry : cy - halfH + rnd(-0.5, halfH * 0.8); s.cell = Sp.Bubble; s.add = 0.5; s.col(c ?? 0xbfefff, 1.3); s.a = 0.85; s.vx = wind + pm(0.2); s.vy = rnd(0.9, 1.9); s.swA = 0.8; s.swF = rnd(1.5, 3); s.s0 = s.s1 = rnd(0.16, 0.42); s.life = rnd(4, 6); s.ac = AC.INOUT; } this.eng.emit(s); } } /** Ritmo (partículas/s) y vida típica (s) por ambiente; rate × life ≈ partículas vivas. */ const AMB: Record = { rain: { rate: 240, life: 0.6 }, snow: { rate: 16, life: 8 }, fireflies: { rate: 8, life: 3.5 }, leaves: { rate: 3.5, life: 9 }, embers: { rate: 14, life: 3.2 }, dust: { rate: 10, life: 4.7 }, bubbles: { rate: 6, life: 5 }, };