BlueGit

projects-timfalken-com meteors

Files

meteors / test / falling-cluster.test.js

falling-cluster.test.js

falling-cluster.test.js 9.5 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js');
6
7function flyingGrayRow (game, y, cols)
8{
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: -game.planet.fallSpeed,
13 decel: 0,
14 phase: 'down'
15 };
16 game.clusters[cluster.id] = cluster;
17 const members = [];
18 for (const col of cols)
19 {
20 const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying');
21 b.clusterId = cluster.id;
22 b.vy = cluster.vy;
23 b.flame = true;
24 members.push(b);
25 }
26 return { cluster, members };
27}
28
29describe('Falling blocks land on flying clusters', () =>
30{
31 it('a falling spawn lands on a flying cluster and flies along as cargo', () =>
32 {
33 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
34 const { cluster, members } = flyingGrayRow(g, 6, [2, 3, 4]);
35 // Freeze ascent/descent so the falling block can meet them cleanly,
36 // then unfreeze so they move together.
37 cluster.phase = 'hold';
38 cluster.vy = 0;
39 const falling = g.createBlock(3, 10, 1, 'falling');
40 falling.vy = -g.planet.fallSpeed;
41 // Fall from 10 onto 6+1=7: distance 3, at fallSpeed 13/3 ≈ 4.33 → ~0.7s
42 g.step(1.0, 1 / 120);
43 assert.equal(falling.state, 'flying', 'should have attached, state=' + falling.state + ' y=' + falling.y);
44 assert.equal(falling.clusterId, cluster.id);
45 assert.equal(falling.flame, false, 'cargo has no flame');
46 assert.equal(falling.color, 1, 'keeps its color');
47 assert.ok(almost(falling.y, members[1].y + 1, 0.05), 'sits on top, y=' + falling.y + ' flyer=' + members[1].y);
48
49 // Now let the cluster descend: cargo must move with it, same dy.
50 cluster.phase = 'down';
51 cluster.vy = 0; // will ease into fallSpeed
52 const yCargo = falling.y;
53 const yFlyer = members[1].y;
54 g.step(0.4, 1 / 120);
55 assert.equal(falling.clusterId, cluster.id);
56 assert.equal(falling.state, 'flying');
57 assert.ok(almost(falling.y - members[1].y, yCargo - yFlyer, 0.05),
58 'relative offset kept: cargo=' + falling.y + ' flyer=' + members[1].y);
59 assert.ok(falling.y < yCargo, 'must have moved down with the cluster');
60 assert.ok(almost(falling.vy, members[1].vy, 1e-9), 'same vy');
61 });
62
63 it('a falling block does not pass through a descending or rising cluster', () =>
64 {
65 const g = makeGame({ noSpawn: true });
66 const { cluster } = flyingGrayRow(g, 5, [1]);
67 cluster.phase = 'down';
68 cluster.vy = -g.planet.fallSpeed;
69 const falling = g.createBlock(1, 8, 2, 'falling');
70 falling.vy = -g.planet.fallSpeed;
71 g.step(2.0, 1 / 120);
72 assert.notEqual(falling.state, 'falling');
73 assert.ok(falling.state === 'flying' || falling.state === 'settling' || falling.state === 'still',
74 'must not still be falling through, state=' + falling.state + ' y=' + falling.y);
75 // Never went below the flyer without joining (flyer may have landed).
76 const flyer = g.blocks.find((b) => b.color === GameCore.COLOR_GRAY);
77 if (falling.state === 'flying' && flyer)
78 { assert.ok(falling.y >= flyer.y - 1e-6, 'cargo not below its flyer'); }
79 });
80
81 it('a rising cluster that meets a falling block picks it up', () =>
82 {
83 const g = makeGame({ noSpawn: true });
84 const { cluster, members } = flyingGrayRow(g, 3, [5]);
85 cluster.phase = 'up';
86 cluster.jumpHeight = 6;
87 cluster.vy = Planet.launchVelocity(g.planet, 6);
88 cluster.decel = Planet.launchDeceleration(g.planet, 6);
89 members[0].vy = cluster.vy;
90 const falling = g.createBlock(5, 7, 3, 'falling');
91 falling.vy = -g.planet.fallSpeed;
92 g.step(0.8, 1 / 120);
93 assert.equal(falling.state, 'flying', 'picked up, state=' + falling.state + ' y=' + falling.y);
94 assert.equal(falling.clusterId, cluster.id);
95 assert.ok(falling.y >= members[0].y, 'sits at or above the flyer, cargo=' + falling.y + ' flyer=' + members[0].y);
96 });
97});
98
99describe('Rigid grey landing', () =>
100{
101 it('keeps relative positions, so part of the group can hang above a gap', () =>
102 {
103 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } });
104 // Horizontal match on a pedestal in col 1 only: cols 0 and 2 will hang.
105 g.place(1, 0, 4); // pedestal
106 placeRow(g, 1, [0, 1, 2], 0);
107 g.resolveMatches();
108 assert.ok(g.blocks.some((b) => b.state === 'flying'));
109 let landed = false;
110 for (let i = 0; i < 3000; i++)
111 {
112 g.tick(1 / 120);
113 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
114 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
115 {
116 landed = true;
117 break;
118 }
119 }
120 assert.ok(landed, 'should have landed rigid\n' + g.snapshot().picture);
121 const at = (col) => g.blocks.find((b) => b.col === col && b.color === GameCore.COLOR_GRAY);
122 const a = at(0);
123 const b = at(1);
124 const c = at(2);
125 assert.ok(a && b && c, 'all three greys present');
126 // The middle one rests on the pedestal (y=1). The sides hang at the same y.
127 assert.ok(almost(b.y, 1, 0.05), 'middle sits on pedestal, y=' + b.y);
128 assert.ok(almost(a.y, b.y, 0.05), 'left hangs at the same height, y=' + a.y);
129 assert.ok(almost(c.y, b.y, 0.05), 'right hangs at the same height, y=' + c.y);
130 assert.equal(g.blockAt(0, 0), null, 'gap under the left hang remains');
131 assert.equal(g.blockAt(2, 0), null, 'gap under the right hang remains');
132 // They stay there through the settle wait.
133 g.step(2.9, 1 / 60);
134 assert.ok(almost(a.y, 1, 0.05) && a.state === 'settling');
135 });
136
137 it('after conversion, hanging blocks fall individually and supported ones stay', () =>
138 {
139 const g = makeGame({
140 noSpawn: true,
141 planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 },
142 seed: 7
143 });
144 g.place(1, 0, 4);
145 placeRow(g, 1, [0, 1, 2], 0);
146 g.resolveMatches();
147 for (let i = 0; i < 3000; i++)
148 {
149 g.tick(1 / 120);
150 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
151 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
152 { break; }
153 }
154 const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY);
155 const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY);
156 const right = g.blocks.find((b) => b.col === 2 && b.color === GameCore.COLOR_GRAY);
157 const leftY = left.y;
158 g.step(3.0, 1 / 120);
159 // Conversion happens at 3s; take a short step so the fall is visible
160 // but not finished (from rest, Terra-like ease is not instant).
161 g.step(0.25, 1 / 120);
162 assert.ok(mid.color >= 0 && mid.state === 'still', 'supported middle stays, state=' + mid.state);
163 assert.ok(almost(mid.y, 1, 0.05), 'middle stays on the pedestal, y=' + mid.y);
164 assert.equal(mid.clusterId, null);
165 // Hanging sides fall from rest — they must not teleport to the floor.
166 assert.equal(left.state, 'falling', 'left should still be falling after 0.25s, state=' + left.state + ' y=' + left.y);
167 assert.ok(left.y < leftY - 0.05, 'left has started falling smoothly, y=' + left.y + ' from ' + leftY);
168 assert.ok(left.y > 0.25, 'must not have teleported to the floor, y=' + left.y);
169 assert.ok(Math.abs(left.vy) <= g.planet.fallSpeed + 1e-6);
170 assert.ok(right.state === 'falling' || (right.state === 'still' && almost(right.y, 0, 0.05)),
171 'right falls, state=' + right.state);
172 // Give them time to land individually.
173 g.step(g.planet.fallTime + 0.5, 1 / 60);
174 assert.equal(left.state, 'still');
175 assert.ok(almost(left.y, 0, 0.05), 'left lands on the floor, y=' + left.y);
176 assert.equal(right.state, 'still');
177 assert.ok(almost(right.y, 0, 0.05));
178 });
179});
180
181describe('Landing bounce planet parameter', () =>
182{
183 it('sets bouncing on a landed block, faster at low air resistance', () =>
184 {
185 const slow = Planet.createPlanet({ fallTime: 5, jumpsToClear: 2, riseTime: 1.5 });
186 const fast = Planet.createPlanet({ fallTime: 1, jumpsToClear: 2, riseTime: 1.5 });
187 assert.ok(fast.bounceDuration < slow.bounceDuration,
188 'low air (fallTime=1) must bounce faster: ' + fast.bounceDuration + ' vs ' + slow.bounceDuration);
189 assert.ok(almost(fast.bounceDuration, 0.12 * 1, 1e-9));
190 assert.ok(almost(slow.bounceDuration, 0.12 * 5, 1e-9));
191 assert.equal(fast.bounceSquash, 0.16);
192 assert.ok(fast.apexEaseTime < slow.apexEaseTime);
193 assert.ok(fast.fallAccel > slow.fallAccel, 'low air eases into fall faster');
194
195 const g = makeGame({ noSpawn: true, planetOpts: { fallTime: 3 } });
196 const block = g.createBlock(0, 5, 1, 'falling');
197 block.vy = -g.planet.fallSpeed;
198 let landed = false;
199 for (let i = 0; i < 2000; i++)
200 {
201 g.tick(1 / 120);
202 if (block.state === 'still')
203 {
204 landed = true;
205 break;
206 }
207 }
208 assert.ok(landed, 'should have landed');
209 assert.ok(almost(block.y, 0, 0.05));
210 assert.equal(block.bouncing, true);
211 assert.ok(block.bounceAge >= 0 && block.bounceAge < g.planet.bounceDuration,
212 'bounce just started, age=' + block.bounceAge);
213 g.step(g.planet.bounceDuration + 0.05, 1 / 60);
214 assert.equal(block.bouncing, false);
215 });
216
217 it('a rigid cluster bounce only on members that actually rest on something', () =>
218 {
219 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } });
220 g.place(1, 0, 4);
221 placeRow(g, 1, [0, 1, 2], 0);
222 g.resolveMatches();
223 for (let i = 0; i < 3000; i++)
224 {
225 g.tick(1 / 120);
226 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
227 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
228 { break; }
229 }
230 const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY);
231 const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY);
232 assert.equal(mid.bouncing, true, 'the member on the pedestal bounces');
233 assert.equal(left.bouncing, false, 'the hanging member does not bounce');
234 });
235});