| 1 | 'use strict'; |
| 2 | |
| 3 | // t1183: rocket flame jitter (random frame, flip, scale, rotation). |
| 4 | |
| 5 | const { describe, it } = require('node:test'); |
| 6 | const assert = require('node:assert/strict'); |
| 7 | const FX = require('../src/flameFx.js'); |
| 8 | |
| 9 | function seeded (seed) |
| 10 | { |
| 11 | let s = seed >>> 0; |
| 12 | return () => ((s = (s * 1664525 + 1013904223) >>> 0) / 4294967296); |
| 13 | } |
| 14 | |
| 15 | describe('FlameFx (t1183)', () => |
| 16 | { |
| 17 | it('runs at double the old frame rate (0.08 s → 0.04 s per frame)', () => |
| 18 | { |
| 19 | assert.equal(FX.FPS, 25); |
| 20 | assert.equal(FX.FPS, 2 * (1 / 0.08)); |
| 21 | const st = FX.create(4, seeded(1)); |
| 22 | st.timer = 0; |
| 23 | assert.equal(FX.advance(st, 1, seeded(2), false), 25); |
| 24 | st.timer = 0; |
| 25 | assert.equal(FX.advance(st, 1, seeded(2), true), 12, 'reduced motion keeps the old rate'); |
| 26 | }); |
| 27 | |
| 28 | it('picks a random frame that differs from the current one, covering all frames', () => |
| 29 | { |
| 30 | const rand = seeded(7); |
| 31 | const seen = new Set(); |
| 32 | let cur = 0; |
| 33 | for (let i = 0; i < 400; i++) |
| 34 | { |
| 35 | const next = FX.pickFrame(cur, 4, rand); |
| 36 | assert.notEqual(next, cur); |
| 37 | assert.ok(next >= 0 && next < 4); |
| 38 | seen.add(next); |
| 39 | cur = next; |
| 40 | } |
| 41 | assert.equal(seen.size, 4); |
| 42 | assert.equal(FX.pickFrame(0, 1, rand), 0); |
| 43 | assert.equal(FX.pickFrame(2, 4, () => 0.999999999), 3); |
| 44 | }); |
| 45 | |
| 46 | it('flips about half the time and keeps scale/size/rotation within bounds, independently', () => |
| 47 | { |
| 48 | const rand = seeded(42); |
| 49 | const st = FX.create(4, rand); |
| 50 | let flips = 0; |
| 51 | const n = 4000; |
| 52 | const ws = []; |
| 53 | const hs = []; |
| 54 | for (let i = 0; i < n; i++) |
| 55 | { |
| 56 | FX.roll(st, rand, false); |
| 57 | if (st.flip) flips++; |
| 58 | assert.ok(Math.abs(st.scale - 1) <= FX.SCALE_VAR + 1e-12); |
| 59 | assert.ok(Math.abs(st.w - 1) <= FX.WIDTH_VAR + 1e-12); |
| 60 | assert.ok(Math.abs(st.h - 1) <= FX.HEIGHT_VAR + 1e-12); |
| 61 | assert.ok(Math.abs(st.rot) <= FX.ROT_VAR_DEG + 1e-12); |
| 62 | ws.push(st.w); hs.push(st.h); |
| 63 | } |
| 64 | assert.ok(flips / n > 0.45 && flips / n < 0.55, 'flip share ' + flips / n); |
| 65 | // width and height vary independently (low correlation). |
| 66 | const mean = (a) => a.reduce((x, y) => x + y, 0) / a.length; |
| 67 | const mw = mean(ws), mh = mean(hs); |
| 68 | let cov = 0, vw = 0, vh = 0; |
| 69 | for (let i = 0; i < n; i++) |
| 70 | { cov += (ws[i] - mw) * (hs[i] - mh); vw += (ws[i] - mw) ** 2; vh += (hs[i] - mh) ** 2; } |
| 71 | assert.ok(Math.abs(cov / Math.sqrt(vw * vh)) < 0.1); |
| 72 | }); |
| 73 | |
| 74 | it('the top-centre anchor never moves, whatever the flip, scale or rotation', () => |
| 75 | { |
| 76 | const rand = seeded(3); |
| 77 | const st = FX.create(4, rand); |
| 78 | for (let i = 0; i < 500; i++) |
| 79 | { |
| 80 | FX.roll(st, rand, false); |
| 81 | const pose = FX.pose(st, 123.5, 77.25, 90, 90); |
| 82 | const top = FX.topCentreOf(pose); |
| 83 | assert.ok(Math.abs(top.x - 123.5) < 1e-9 && Math.abs(top.y - 77.25) < 1e-9, JSON.stringify(top)); |
| 84 | assert.equal(pose.rotation, st.rot); |
| 85 | assert.equal(pose.mirrorX, st.flip); |
| 86 | } |
| 87 | // Unjittered pose = the old placement (centre half a flame below the top). |
| 88 | const plain = { frame: 0, flip: false, scale: 1, w: 1, h: 1, rot: 0 }; |
| 89 | assert.deepEqual(FX.pose(plain, 10, 20, 30, 40), { x: 10, y: 40, w: 30, h: 40, rotation: 0, mirrorX: false, frame: 0 }); |
| 90 | }); |
| 91 | |
| 92 | it('reduced motion: random frame only, no flip/scale/rotation', () => |
| 93 | { |
| 94 | const rand = seeded(9); |
| 95 | const st = FX.create(4, rand); |
| 96 | for (let i = 0; i < 50; i++) |
| 97 | { |
| 98 | const before = st.frame; |
| 99 | FX.roll(st, rand, true); |
| 100 | assert.notEqual(st.frame, before); |
| 101 | assert.equal(st.flip, false); |
| 102 | assert.equal(st.scale * st.w * st.h, 1); |
| 103 | assert.equal(st.rot, 0); |
| 104 | } |
| 105 | }); |
| 106 | |
| 107 | it('a long stall rolls once and keeps the remainder', () => |
| 108 | { |
| 109 | const st = FX.create(4, seeded(5)); |
| 110 | st.timer = 0; |
| 111 | let calls = 0; |
| 112 | const rand = () => { calls++; return 0.3; }; |
| 113 | assert.equal(FX.advance(st, 2.01, rand, false), 50); |
| 114 | assert.ok(calls <= 6, 'one roll, not 50: ' + calls); |
| 115 | assert.ok(st.timer >= 0 && st.timer < 0.04); |
| 116 | assert.equal(FX.advance(st, 0.001, rand, false) >= 0, true); |
| 117 | }); |
| 118 | }); |
| 119 | |
| 120 | describe('FlameFx create (t1183)', () => |
| 121 | { |
| 122 | it('a flame created under reduced motion starts unjittered', () => |
| 123 | { |
| 124 | const st = FX.create(4, () => 0.9, true); |
| 125 | assert.equal(st.flip, false); |
| 126 | assert.equal(st.rot, 0); |
| 127 | assert.equal(st.scale * st.w * st.h, 1); |
| 128 | }); |
| 129 | }); |
projects-timfalken-com meteors