/** * t1183 — rocket flame jitter. * * Every animation tick (FPS ticks per second) the flame picks a random * sprite frame (different from the current one), flips horizontally with * 50% chance and gets a small random scale / width / height / rotation. * All of it pivots on the top-centre of the flame, where it attaches to * the block, so that point never moves. * * Purely visual. Pure functions so it can be unit-tested. */ var FlameFx = {}; /** Ticks per second (was 12.5 fps = 0.08 s per frame; doubled). */ FlameFx.FPS = 25; /** Reduced motion: old frame rate, no flip/scale/rotation jitter. */ FlameFx.REDUCED_FPS = 12.5; /** Overall scale variation (±). */ FlameFx.SCALE_VAR = 0.06; /** Extra independent width variation (±). */ FlameFx.WIDTH_VAR = 0.07; /** Extra independent height variation (±). */ FlameFx.HEIGHT_VAR = 0.07; /** Rotation variation in degrees (±). */ FlameFx.ROT_VAR_DEG = 2.5; FlameFx.reducedMotion = function () { try { var w = typeof window !== 'undefined' ? window : null; return !!(w && w.matchMedia && w.matchMedia('(prefers-reduced-motion: reduce)').matches); } catch (e) { return false; } }; /** New per-flame state; the phase is random so flames don't tick in sync. */ FlameFx.create = function (frames, rand, reduced) { rand = rand || Math.random; var st = { frame: 0, timer: 0, flip: false, scale: 1, w: 1, h: 1, rot: 0, frames: frames || 1 }; st.timer = rand() / FlameFx.FPS; FlameFx.roll(st, rand, !!reduced); return st; }; /** Random frame index in [0, frames), different from `current` when possible. */ FlameFx.pickFrame = function (current, frames, rand) { rand = rand || Math.random; if (frames <= 1) { return 0; } // Choose among the other frames-1 indices uniformly. var k = Math.floor(rand() * (frames - 1)); if (k >= frames - 1) { k = frames - 2; } return k >= current ? k + 1 : k; }; function flameVar (rand, amount) { return 1 + (rand() * 2 - 1) * amount; } /** One animation tick: new frame, flip, scales and rotation. */ FlameFx.roll = function (st, rand, reduced) { rand = rand || Math.random; st.frame = FlameFx.pickFrame(st.frame, st.frames, rand); if (reduced) { st.flip = false; st.scale = 1; st.w = 1; st.h = 1; st.rot = 0; return st; } st.flip = rand() < 0.5; st.scale = flameVar(rand, FlameFx.SCALE_VAR); st.w = flameVar(rand, FlameFx.WIDTH_VAR); st.h = flameVar(rand, FlameFx.HEIGHT_VAR); st.rot = (rand() * 2 - 1) * FlameFx.ROT_VAR_DEG; return st; }; /** Advance time (seconds). Returns the number of ticks that happened. */ FlameFx.advance = function (st, dt, rand, reduced) { var period = 1 / (reduced ? FlameFx.REDUCED_FPS : FlameFx.FPS); st.timer += Math.max(0, dt || 0); var ticks = 0; while (st.timer >= period - 1e-9) { st.timer = Math.max(0, st.timer - period); ticks++; } // Long stall: one roll is enough, don't loop the RNG for nothing. if (ticks > 0) { FlameFx.roll(st, rand, reduced); } return ticks; }; /** * Draw pose for a flame whose top-centre anchor is (ax, ay) and whose * nominal size is baseW × baseH. The framework draws sprites centred on * (x, y), rotated by `rotation` degrees (clockwise, canvas y-down) around * that centre, and mirrorX flips around the vertical centre line. The * centre is placed so the rotated, scaled top-centre lands exactly on * the anchor. */ FlameFx.pose = function (st, ax, ay, baseW, baseH) { var w = baseW * st.scale * st.w; var h = baseH * st.scale * st.h; var rad = st.rot * Math.PI / 180; return { x: ax - Math.sin(rad) * h / 2, y: ay + Math.cos(rad) * h / 2, w: w, h: h, rotation: st.rot, mirrorX: !!st.flip, frame: st.frame }; }; /** * Where the top-centre of the drawn quad ends up for a pose — same * transform as the framework's sprite matrix (model point (0, -0.5)). * Used by tests to prove the anchor never moves. */ FlameFx.topCentreOf = function (pose) { var rad = pose.rotation * Math.PI / 180; var c = Math.cos(rad); var s = Math.sin(rad); var sy = pose.h; // x' = c*sx*u - s*sy*v + x ; y' = s*sx*u + c*sy*v + y with u = 0, v = -0.5 return { x: pose.x + s * sy * 0.5, y: pose.y - c * sy * 0.5 }; }; if (typeof globalThis !== 'undefined') { globalThis.FlameFx = FlameFx; } if (typeof module !== 'undefined' && module.exports) { module.exports = FlameFx; }