BlueGit

projects-timfalken-com meteors

t1183: rocket flame at 25 fps with random frame, flip, scale and rotation around the top anchor

timfa Grok Bot · pushed by timfa

Authored 2026-11-14 · 3 days ago

Hash
bb3f80d06732c203e4e1ee3393291fa2ad58b425
Parent
fafff2d3
4 files changed +297 −5
src/build.sh +1 −0
@@ -19,6 +19,7 @@ FILES=(
19 19 "$SRC_DIR/blocks.js"
20 20 "$SRC_DIR/easing.js"
21 21 "$SRC_DIR/gameCore.js"
22 "$SRC_DIR/flameFx.js"
22 23 "$SRC_DIR/miniLayout.js"
23 24 "$SRC_DIR/miniPredict.js"
24 25 "$SRC_DIR/online.js"
@@ -19,6 +19,7 @@ FILES=(
19 "$SRC_DIR/blocks.js"
19 "$SRC_DIR/blocks.js"
20 "$SRC_DIR/easing.js"
20 "$SRC_DIR/easing.js"
21 "$SRC_DIR/gameCore.js"
21 "$SRC_DIR/gameCore.js"
22 "$SRC_DIR/flameFx.js"
22 "$SRC_DIR/miniLayout.js"
23 "$SRC_DIR/miniLayout.js"
23 "$SRC_DIR/miniPredict.js"
24 "$SRC_DIR/miniPredict.js"
24 "$SRC_DIR/online.js"
25 "$SRC_DIR/online.js"
src/flameFx.js added +147 −0
@@ -0,0 +1,147 @@
1 /**
2 * t1183 — rocket flame jitter.
3 *
4 * Every animation tick (FPS ticks per second) the flame picks a random
5 * sprite frame (different from the current one), flips horizontally with
6 * 50% chance and gets a small random scale / width / height / rotation.
7 * All of it pivots on the top-centre of the flame, where it attaches to
8 * the block, so that point never moves.
9 *
10 * Purely visual. Pure functions so it can be unit-tested.
11 */
12 var FlameFx = {};
13
14 /** Ticks per second (was 12.5 fps = 0.08 s per frame; doubled). */
15 FlameFx.FPS = 25;
16 /** Reduced motion: old frame rate, no flip/scale/rotation jitter. */
17 FlameFx.REDUCED_FPS = 12.5;
18 /** Overall scale variation (±). */
19 FlameFx.SCALE_VAR = 0.06;
20 /** Extra independent width variation (±). */
21 FlameFx.WIDTH_VAR = 0.07;
22 /** Extra independent height variation (±). */
23 FlameFx.HEIGHT_VAR = 0.07;
24 /** Rotation variation in degrees (±). */
25 FlameFx.ROT_VAR_DEG = 2.5;
26
27 FlameFx.reducedMotion = function ()
28 {
29 try
30 {
31 var w = typeof window !== 'undefined' ? window : null;
32 return !!(w && w.matchMedia && w.matchMedia('(prefers-reduced-motion: reduce)').matches);
33 }
34 catch (e)
35 { return false; }
36 };
37
38 /** New per-flame state; the phase is random so flames don't tick in sync. */
39 FlameFx.create = function (frames, rand, reduced)
40 {
41 rand = rand || Math.random;
42 var st = { frame: 0, timer: 0, flip: false, scale: 1, w: 1, h: 1, rot: 0, frames: frames || 1 };
43 st.timer = rand() / FlameFx.FPS;
44 FlameFx.roll(st, rand, !!reduced);
45 return st;
46 };
47
48 /** Random frame index in [0, frames), different from `current` when possible. */
49 FlameFx.pickFrame = function (current, frames, rand)
50 {
51 rand = rand || Math.random;
52 if (frames <= 1)
53 { return 0; }
54 // Choose among the other frames-1 indices uniformly.
55 var k = Math.floor(rand() * (frames - 1));
56 if (k >= frames - 1)
57 { k = frames - 2; }
58 return k >= current ? k + 1 : k;
59 };
60
61 function flameVar (rand, amount)
62 {
63 return 1 + (rand() * 2 - 1) * amount;
64 }
65
66 /** One animation tick: new frame, flip, scales and rotation. */
67 FlameFx.roll = function (st, rand, reduced)
68 {
69 rand = rand || Math.random;
70 st.frame = FlameFx.pickFrame(st.frame, st.frames, rand);
71 if (reduced)
72 {
73 st.flip = false;
74 st.scale = 1;
75 st.w = 1;
76 st.h = 1;
77 st.rot = 0;
78 return st;
79 }
80 st.flip = rand() < 0.5;
81 st.scale = flameVar(rand, FlameFx.SCALE_VAR);
82 st.w = flameVar(rand, FlameFx.WIDTH_VAR);
83 st.h = flameVar(rand, FlameFx.HEIGHT_VAR);
84 st.rot = (rand() * 2 - 1) * FlameFx.ROT_VAR_DEG;
85 return st;
86 };
87
88 /** Advance time (seconds). Returns the number of ticks that happened. */
89 FlameFx.advance = function (st, dt, rand, reduced)
90 {
91 var period = 1 / (reduced ? FlameFx.REDUCED_FPS : FlameFx.FPS);
92 st.timer += Math.max(0, dt || 0);
93 var ticks = 0;
94 while (st.timer >= period - 1e-9)
95 {
96 st.timer = Math.max(0, st.timer - period);
97 ticks++;
98 }
99 // Long stall: one roll is enough, don't loop the RNG for nothing.
100 if (ticks > 0)
101 { FlameFx.roll(st, rand, reduced); }
102 return ticks;
103 };
104
105 /**
106 * Draw pose for a flame whose top-centre anchor is (ax, ay) and whose
107 * nominal size is baseW × baseH. The framework draws sprites centred on
108 * (x, y), rotated by `rotation` degrees (clockwise, canvas y-down) around
109 * that centre, and mirrorX flips around the vertical centre line. The
110 * centre is placed so the rotated, scaled top-centre lands exactly on
111 * the anchor.
112 */
113 FlameFx.pose = function (st, ax, ay, baseW, baseH)
114 {
115 var w = baseW * st.scale * st.w;
116 var h = baseH * st.scale * st.h;
117 var rad = st.rot * Math.PI / 180;
118 return {
119 x: ax - Math.sin(rad) * h / 2,
120 y: ay + Math.cos(rad) * h / 2,
121 w: w,
122 h: h,
123 rotation: st.rot,
124 mirrorX: !!st.flip,
125 frame: st.frame
126 };
127 };
128
129 /**
130 * Where the top-centre of the drawn quad ends up for a pose — same
131 * transform as the framework's sprite matrix (model point (0, -0.5)).
132 * Used by tests to prove the anchor never moves.
133 */
134 FlameFx.topCentreOf = function (pose)
135 {
136 var rad = pose.rotation * Math.PI / 180;
137 var c = Math.cos(rad);
138 var s = Math.sin(rad);
139 var sy = pose.h;
140 // x' = c*sx*u - s*sy*v + x ; y' = s*sx*u + c*sy*v + y with u = 0, v = -0.5
141 return { x: pose.x + s * sy * 0.5, y: pose.y - c * sy * 0.5 };
142 };
143
144 if (typeof globalThis !== 'undefined')
145 { globalThis.FlameFx = FlameFx; }
146 if (typeof module !== 'undefined' && module.exports)
147 { module.exports = FlameFx; }
@@ -0,0 +1,147 @@
1/**
2 * t1183 — rocket flame jitter.
3 *
4 * Every animation tick (FPS ticks per second) the flame picks a random
5 * sprite frame (different from the current one), flips horizontally with
6 * 50% chance and gets a small random scale / width / height / rotation.
7 * All of it pivots on the top-centre of the flame, where it attaches to
8 * the block, so that point never moves.
9 *
10 * Purely visual. Pure functions so it can be unit-tested.
11 */
12var FlameFx = {};
13
14/** Ticks per second (was 12.5 fps = 0.08 s per frame; doubled). */
15FlameFx.FPS = 25;
16/** Reduced motion: old frame rate, no flip/scale/rotation jitter. */
17FlameFx.REDUCED_FPS = 12.5;
18/** Overall scale variation (±). */
19FlameFx.SCALE_VAR = 0.06;
20/** Extra independent width variation (±). */
21FlameFx.WIDTH_VAR = 0.07;
22/** Extra independent height variation (±). */
23FlameFx.HEIGHT_VAR = 0.07;
24/** Rotation variation in degrees (±). */
25FlameFx.ROT_VAR_DEG = 2.5;
26
27FlameFx.reducedMotion = function ()
28{
29 try
30 {
31 var w = typeof window !== 'undefined' ? window : null;
32 return !!(w && w.matchMedia && w.matchMedia('(prefers-reduced-motion: reduce)').matches);
33 }
34 catch (e)
35 { return false; }
36};
37
38/** New per-flame state; the phase is random so flames don't tick in sync. */
39FlameFx.create = function (frames, rand, reduced)
40{
41 rand = rand || Math.random;
42 var st = { frame: 0, timer: 0, flip: false, scale: 1, w: 1, h: 1, rot: 0, frames: frames || 1 };
43 st.timer = rand() / FlameFx.FPS;
44 FlameFx.roll(st, rand, !!reduced);
45 return st;
46};
47
48/** Random frame index in [0, frames), different from `current` when possible. */
49FlameFx.pickFrame = function (current, frames, rand)
50{
51 rand = rand || Math.random;
52 if (frames <= 1)
53 { return 0; }
54 // Choose among the other frames-1 indices uniformly.
55 var k = Math.floor(rand() * (frames - 1));
56 if (k >= frames - 1)
57 { k = frames - 2; }
58 return k >= current ? k + 1 : k;
59};
60
61function flameVar (rand, amount)
62{
63 return 1 + (rand() * 2 - 1) * amount;
64}
65
66/** One animation tick: new frame, flip, scales and rotation. */
67FlameFx.roll = function (st, rand, reduced)
68{
69 rand = rand || Math.random;
70 st.frame = FlameFx.pickFrame(st.frame, st.frames, rand);
71 if (reduced)
72 {
73 st.flip = false;
74 st.scale = 1;
75 st.w = 1;
76 st.h = 1;
77 st.rot = 0;
78 return st;
79 }
80 st.flip = rand() < 0.5;
81 st.scale = flameVar(rand, FlameFx.SCALE_VAR);
82 st.w = flameVar(rand, FlameFx.WIDTH_VAR);
83 st.h = flameVar(rand, FlameFx.HEIGHT_VAR);
84 st.rot = (rand() * 2 - 1) * FlameFx.ROT_VAR_DEG;
85 return st;
86};
87
88/** Advance time (seconds). Returns the number of ticks that happened. */
89FlameFx.advance = function (st, dt, rand, reduced)
90{
91 var period = 1 / (reduced ? FlameFx.REDUCED_FPS : FlameFx.FPS);
92 st.timer += Math.max(0, dt || 0);
93 var ticks = 0;
94 while (st.timer >= period - 1e-9)
95 {
96 st.timer = Math.max(0, st.timer - period);
97 ticks++;
98 }
99 // Long stall: one roll is enough, don't loop the RNG for nothing.
100 if (ticks > 0)
101 { FlameFx.roll(st, rand, reduced); }
102 return ticks;
103};
104
105/**
106 * Draw pose for a flame whose top-centre anchor is (ax, ay) and whose
107 * nominal size is baseW × baseH. The framework draws sprites centred on
108 * (x, y), rotated by `rotation` degrees (clockwise, canvas y-down) around
109 * that centre, and mirrorX flips around the vertical centre line. The
110 * centre is placed so the rotated, scaled top-centre lands exactly on
111 * the anchor.
112 */
113FlameFx.pose = function (st, ax, ay, baseW, baseH)
114{
115 var w = baseW * st.scale * st.w;
116 var h = baseH * st.scale * st.h;
117 var rad = st.rot * Math.PI / 180;
118 return {
119 x: ax - Math.sin(rad) * h / 2,
120 y: ay + Math.cos(rad) * h / 2,
121 w: w,
122 h: h,
123 rotation: st.rot,
124 mirrorX: !!st.flip,
125 frame: st.frame
126 };
127};
128
129/**
130 * Where the top-centre of the drawn quad ends up for a pose — same
131 * transform as the framework's sprite matrix (model point (0, -0.5)).
132 * Used by tests to prove the anchor never moves.
133 */
134FlameFx.topCentreOf = function (pose)
135{
136 var rad = pose.rotation * Math.PI / 180;
137 var c = Math.cos(rad);
138 var s = Math.sin(rad);
139 var sy = pose.h;
140 // x' = c*sx*u - s*sy*v + x ; y' = s*sx*u + c*sy*v + y with u = 0, v = -0.5
141 return { x: pose.x + s * sy * 0.5, y: pose.y - c * sy * 0.5 };
142};
143
144if (typeof globalThis !== 'undefined')
145{ globalThis.FlameFx = FlameFx; }
146if (typeof module !== 'undefined' && module.exports)
147{ module.exports = FlameFx; }
src/main.js +20 −5
@@ -580,6 +580,13 @@ function spriteFor (block)
580 580 return game.images[key] || game.images.gray;
581 581 }
582 582
583 /** Frames in the flame sprite (from the loaded image, default 4). */
584 function flameFrameCount ()
585 {
586 var img = typeof game !== 'undefined' && game.images ? game.images.flame : null;
587 return img && img.subImages ? img.subImages : 4;
588 }
589
583 590 function syncSprites (dt)
584 591 {
585 592 if (!app.core || !app.scene)
@@ -617,22 +624,30 @@ function syncSprites (dt)
617 624 if (!flame)
618 625 {
619 626 flame = new GameObject(0, 0, game.images.flame, 1);
620 flame.renderer.setAnimation('burn', 0, 0.08);
627 // t1183: frames are chosen by FlameFx, not the framework cycle.
628 flame.fx = FlameFx.create(flameFrameCount(), Math.random, FlameFx.reducedMotion());
629 flame.renderer.setSubImage(flame.fx.frame);
621 630 app.scene.addObject(flame);
622 631 app.flames[block.id] = flame;
623 632 }
624 633 flame.renderer.visible = true;
634 FlameFx.advance(flame.fx, dt, Math.random, FlameFx.reducedMotion());
625 635 var t = app.tile;
626 636 // 3x as large (length and width, t1110). Keep the top of the
627 637 // flame at the same overlap into the block so the extra length
628 // extends downward under the rocket.
638 // extends downward under the rocket. t1183: random frame, flip,
639 // scale and rotation all pivot on that top-centre anchor.
629 640 var flameW = t * 0.85 * 3;
630 641 var flameH = t * 0.85 * 3;
631 642 var origCenter = app.fieldH - block.y * t + t * 0.15;
632 643 var origTop = origCenter - (t * 0.85) / 2;
633 flame.transform.position.x = block.col * t + t / 2;
634 flame.transform.position.y = origTop + flameH / 2;
635 flame.transform.size = { x: flameW, y: flameH };
644 var pose = FlameFx.pose(flame.fx, block.col * t + t / 2, origTop, flameW, flameH);
645 flame.renderer.subImage = pose.frame;
646 flame.renderer.mirrorX = pose.mirrorX;
647 flame.transform.rotation = pose.rotation;
648 flame.transform.position.x = pose.x;
649 flame.transform.position.y = pose.y;
650 flame.transform.size = { x: pose.w, y: pose.h };
636 651 }
637 652 else if (app.flames[block.id])
638 653 { app.flames[block.id].renderer.visible = false; }
@@ -580,6 +580,13 @@ function spriteFor (block)
580 return game.images[key] || game.images.gray;
580 return game.images[key] || game.images.gray;
581}
581}
582
582
583/** Frames in the flame sprite (from the loaded image, default 4). */
584function flameFrameCount ()
585{
586 var img = typeof game !== 'undefined' && game.images ? game.images.flame : null;
587 return img && img.subImages ? img.subImages : 4;
588}
589
583function syncSprites (dt)
590function syncSprites (dt)
584{
591{
585 if (!app.core || !app.scene)
592 if (!app.core || !app.scene)
@@ -617,22 +624,30 @@ function syncSprites (dt)
617 if (!flame)
624 if (!flame)
618 {
625 {
619 flame = new GameObject(0, 0, game.images.flame, 1);
626 flame = new GameObject(0, 0, game.images.flame, 1);
620 flame.renderer.setAnimation('burn', 0, 0.08);
627 // t1183: frames are chosen by FlameFx, not the framework cycle.
628 flame.fx = FlameFx.create(flameFrameCount(), Math.random, FlameFx.reducedMotion());
629 flame.renderer.setSubImage(flame.fx.frame);
621 app.scene.addObject(flame);
630 app.scene.addObject(flame);
622 app.flames[block.id] = flame;
631 app.flames[block.id] = flame;
623 }
632 }
624 flame.renderer.visible = true;
633 flame.renderer.visible = true;
634 FlameFx.advance(flame.fx, dt, Math.random, FlameFx.reducedMotion());
625 var t = app.tile;
635 var t = app.tile;
626 // 3x as large (length and width, t1110). Keep the top of the
636 // 3x as large (length and width, t1110). Keep the top of the
627 // flame at the same overlap into the block so the extra length
637 // flame at the same overlap into the block so the extra length
628 // extends downward under the rocket.
638 // extends downward under the rocket. t1183: random frame, flip,
639 // scale and rotation all pivot on that top-centre anchor.
629 var flameW = t * 0.85 * 3;
640 var flameW = t * 0.85 * 3;
630 var flameH = t * 0.85 * 3;
641 var flameH = t * 0.85 * 3;
631 var origCenter = app.fieldH - block.y * t + t * 0.15;
642 var origCenter = app.fieldH - block.y * t + t * 0.15;
632 var origTop = origCenter - (t * 0.85) / 2;
643 var origTop = origCenter - (t * 0.85) / 2;
633 flame.transform.position.x = block.col * t + t / 2;
644 var pose = FlameFx.pose(flame.fx, block.col * t + t / 2, origTop, flameW, flameH);
634 flame.transform.position.y = origTop + flameH / 2;
645 flame.renderer.subImage = pose.frame;
635 flame.transform.size = { x: flameW, y: flameH };
646 flame.renderer.mirrorX = pose.mirrorX;
647 flame.transform.rotation = pose.rotation;
648 flame.transform.position.x = pose.x;
649 flame.transform.position.y = pose.y;
650 flame.transform.size = { x: pose.w, y: pose.h };
636 }
651 }
637 else if (app.flames[block.id])
652 else if (app.flames[block.id])
638 { app.flames[block.id].renderer.visible = false; }
653 { app.flames[block.id].renderer.visible = false; }
test/flame-fx.test.js added +129 −0
@@ -0,0 +1,129 @@
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 });
@@ -0,0 +1,129 @@
1'use strict';
2
3// t1183: rocket flame jitter (random frame, flip, scale, rotation).
4
5const { describe, it } = require('node:test');
6const assert = require('node:assert/strict');
7const FX = require('../src/flameFx.js');
8
9function seeded (seed)
10{
11 let s = seed >>> 0;
12 return () => ((s = (s * 1664525 + 1013904223) >>> 0) / 4294967296);
13}
14
15describe('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
120describe('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});