BlueGit

projects-timfalken-com meteors

Files

meteors / src / flameFx.js

flameFx.js

flameFx.js 4.2 KiB Download Raw
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; }