BlueGit

projects-timfalken-com meteors

Files

meteors / test / swap.test.js

swap.test.js

swap.test.js 11.2 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, fillColumn, GameCore } = require('./helpers.js');
6
7describe('Vertical swapping', () =>
8{
9 it('dragging a block through a column swaps it with the blocks it passes', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 const a = g.place(0, 0, 0);
13 const b = g.place(0, 1, 1);
14 const c = g.place(0, 2, 2);
15 assert.ok(g.beginDrag(0, 0));
16 g.moveDrag(2);
17 g.endDrag();
18 assert.equal(a.y, 2);
19 assert.equal(b.y, 0);
20 assert.equal(c.y, 1);
21 assert.equal(g.blockAt(0, 2).color, 0);
22 assert.equal(g.blockAt(0, 0).color, 1);
23 });
24
25 it('swapTo moves a block stepwise and exchanges neighbors', () =>
26 {
27 const g = makeGame({ noSpawn: true });
28 g.place(1, 0, 4);
29 g.place(1, 1, 3);
30 g.place(1, 2, 2);
31 g.place(1, 3, 1);
32 const moved = g.swapTo(1, 0, 3);
33 assert.equal(moved.color, 4);
34 assert.equal(moved.y, 3);
35 assert.equal(g.blockAt(1, 0).color, 3);
36 assert.equal(g.blockAt(1, 1).color, 2);
37 assert.equal(g.blockAt(1, 2).color, 1);
38 });
39
40 it('refuses to drag in a locked or flying column', () =>
41 {
42 const g = makeGame({ noSpawn: true });
43 fillColumn(g, 0, 13, [0]);
44 g.checkLocks();
45 assert.equal(g.isColumnLocked(0), true);
46 assert.equal(g.beginDrag(0, 0), false);
47
48 const g2 = makeGame({ noSpawn: true });
49 g2.place(2, 0, 1);
50 g2.place(3, 0, 1);
51 g2.place(4, 0, 1);
52 g2.place(2, 1, 2);
53 g2.resolveMatches();
54 assert.equal(g2.isColumnStill(2), false);
55 assert.equal(g2.beginDrag(2, 1), false);
56 // A quiet neighboring column is still draggable.
57 g2.place(7, 0, 3);
58 g2.place(7, 1, 4);
59 assert.equal(g2.isColumnStill(7), true);
60 assert.ok(g2.beginDrag(7, 0));
61 g2.endDrag();
62 });
63
64 it('pulling a top block further up starts a manual launch that cannot leave the field', () =>
65 {
66 const g = makeGame({ noSpawn: true });
67 g.place(0, 0, 1);
68 g.place(0, 1, 2);
69 g.place(0, 2, 3);
70 assert.ok(g.beginDrag(0, 2));
71 g.moveDrag(2 + 1.5);
72 assert.equal(g.drag.launchIntent, true);
73 g.endDrag();
74 const block = g.blocks.find((b) => b.color === 3);
75 assert.equal(block.state, 'manual');
76 assert.equal(block.manualPhase, 'up');
77 assert.ok(block.vy > 0);
78 g.step(g.planet.riseTime, 1 / 240);
79 assert.ok(Math.abs(block.y - (g.height - 1)) < 0.05, 'apex should be just under the top, y=' + block.y);
80 assert.ok(block.y <= g.height - 1 + 1e-9, 'y=' + block.y);
81 assert.ok(block.y < g.height, 'must not leave the field');
82 assert.ok(block.manualPhase === 'down' || block.state === 'still' || Math.abs(block.y - (g.height - 1)) < 0.05);
83 g.step(0.5, 1 / 120);
84 assert.ok(block.y < g.height - 1 - 0.01 || block.state === 'still', 'must come back down, y=' + block.y + ' state=' + block.state);
85 assert.ok(block.y < g.height);
86 });
87
88 it('a manual jump from mid-stack still caps at the top cell', () =>
89 {
90 const g = makeGame({ noSpawn: true });
91 g.place(5, 0, 0);
92 g.place(5, 1, 1);
93 const top = g.place(5, 2, 2);
94 g.startManualLaunch(top);
95 g.step(4, 1 / 120);
96 assert.ok(top.y <= g.height - 1 + 1e-9);
97 assert.ok(top.y < g.height);
98 // After coming back down it lands on the remaining stack (y=1, since it left).
99 assert.ok(top.state === 'still' || top.state === 'manual');
100 if (top.state === 'still')
101 { assert.equal(top.y, 2); }
102 });
103
104 it('a click in the lower half of a cell grabs that cell, not the one above', () =>
105 {
106 const g = makeGame({ noSpawn: true });
107 g.place(0, 0, 0);
108 g.place(0, 1, 1);
109 assert.ok(g.beginDrag(0, 0.4));
110 assert.equal(g.blockById(g.drag.blockId).color, 0);
111 g.endDrag();
112 assert.ok(g.beginDrag(0, 0.9));
113 assert.equal(g.blockById(g.drag.blockId).color, 0);
114 g.endDrag();
115 assert.ok(g.beginDrag(0, 1.1));
116 assert.equal(g.blockById(g.drag.blockId).color, 1);
117 g.endDrag();
118 });
119});
120
121describe('Manual launch of the already-topmost block', () =>
122{
123 it('dragging the top block upward past its own position starts a manual jump', () =>
124 {
125 const g = makeGame({ noSpawn: true });
126 // A single block is already the topmost (and only) block of its column.
127 const block = g.place(0, 0, 2);
128 // Grab at the visual centre (y=0.5), then pull just past its position —
129 // well short of the old "empty space above the cell" threshold (1.25).
130 assert.ok(g.beginDrag(0, 0.5));
131 assert.equal(g.drag.startedAsTop, true);
132 g.moveDrag(0.5 + 0.4);
133 assert.ok(0.9 < 0 + 1 + 0.25, 'this pull would have failed the old threshold');
134 assert.equal(g.drag.launchIntent, true, 'pull of 0.4 tiles past the press point must launch');
135 g.endDrag();
136 assert.equal(block.state, 'manual');
137 assert.equal(block.manualPhase, 'up');
138 assert.ok(block.vy > 0);
139 g.step(g.planet.riseTime, 1 / 240);
140 assert.ok(Math.abs(block.y - (g.height - 1)) < 0.08, 'jumps to just under the top, y=' + block.y);
141 assert.ok(block.y <= g.height - 1 + 1e-9);
142 });
143
144 it('a short upward twitch below LAUNCH_PULL does not launch', () =>
145 {
146 const g = makeGame({ noSpawn: true });
147 const block = g.place(3, 0, 1);
148 assert.ok(g.beginDrag(3, 0.5));
149 g.moveDrag(0.5 + 0.2);
150 assert.ok(0.2 < g.constructor.LAUNCH_PULL || 0.2 < 0.35);
151 assert.equal(g.drag.launchIntent, false);
152 g.endDrag();
153 assert.equal(block.state, 'still');
154 assert.equal(block.y, 0);
155 });
156
157 it('dragging a mid-stack block up to the top still swaps instead of launching', () =>
158 {
159 const g = makeGame({ noSpawn: true });
160 const bottom = g.place(1, 0, 0);
161 g.place(1, 1, 1);
162 g.place(1, 2, 2);
163 assert.ok(g.beginDrag(1, 0.3));
164 assert.equal(g.drag.startedAsTop, false);
165 // Pointer is past the top cell centre but not into the empty space.
166 g.moveDrag(2.4);
167 assert.equal(bottom.y, 2, 'swapped to the top');
168 assert.equal(g.drag.launchIntent, false, 'must not launch a mid-stack rearrange');
169 g.endDrag();
170 assert.equal(bottom.state, 'still');
171 assert.equal(bottom.y, 2);
172 });
173
174 it('after a mid-stack block becomes the top, a further pull into empty space still launches', () =>
175 {
176 const g = makeGame({ noSpawn: true });
177 const bottom = g.place(2, 0, 4);
178 g.place(2, 1, 3);
179 assert.ok(g.beginDrag(2, 0.2));
180 assert.equal(g.drag.startedAsTop, false);
181 g.moveDrag(1 + 1 + 0.4);
182 assert.equal(bottom.y, 1);
183 assert.equal(g.drag.launchIntent, true);
184 g.endDrag();
185 assert.equal(bottom.state, 'manual');
186 });
187});
188
189describe('Instant grab, snap and 1:1 pointer tracking (t1102/t1103/t1104)', () =>
190{
191 it('grabs instantly on beginDrag with no hold delay or elapsed time', () =>
192 {
193 const g = makeGame({ noSpawn: true });
194 const block = g.place(2, 1, 1);
195 g.place(2, 0, 0);
196 // No tick between "pointerdown" and the grab — there is no long press.
197 assert.ok(g.beginDrag(2, 1.4));
198 assert.ok(g.drag);
199 assert.equal(g.drag.blockId, block.id);
200 assert.equal(g.drag.accum, 0);
201 assert.equal(block.state, 'still');
202 });
203
204 it('snaps to the nearest still block when the press is slightly outside it', () =>
205 {
206 const g = makeGame({ noSpawn: true });
207 const bottom = g.place(1, 0, 0);
208 g.place(1, 1, 1);
209 const top = g.place(1, 2, 2);
210 // Just below the bottom block (occupies [0, 1)).
211 assert.ok(g.beginDrag(1, -0.2));
212 assert.equal(g.blockById(g.drag.blockId).id, bottom.id);
213 g.endDrag();
214 // Just above the top block (occupies [2, 3)).
215 assert.ok(g.beginDrag(1, 3.2));
216 assert.equal(g.blockById(g.drag.blockId).id, top.id);
217 g.endDrag();
218 // A miss further than SNAP_Y does not grab.
219 assert.equal(g.beginDrag(1, 3.2 + GameCore.SNAP_Y + 0.1), false);
220 });
221
222 it('horizontal slop grabs a neighbouring column when the press is near the edge', () =>
223 {
224 const g = makeGame({ noSpawn: true });
225 const block = g.place(2, 1, 3);
226 g.place(2, 0, 0);
227 // Pointer is in column 3, 0.2 tiles from the left edge — close to col 2.
228 assert.ok(g.beginDrag(3.2, 1.5));
229 assert.equal(g.blockById(g.drag.blockId).id, block.id);
230 assert.equal(g.blockById(g.drag.blockId).col, 2);
231 g.endDrag();
232 // An integer column 3 (no slop — used by existing tests) does not jump left.
233 assert.equal(g.beginDrag(3, 1.5), false);
234 });
235
236 it('a slightly diagonal drag stays in the starting column', () =>
237 {
238 const g = makeGame({ noSpawn: true });
239 const a = g.place(4, 0, 0);
240 g.place(4, 1, 1);
241 g.place(4, 2, 2);
242 g.place(5, 0, 3);
243 g.place(5, 1, 4);
244 g.place(5, 2, 0);
245 assert.ok(g.beginDrag(4.85, 0.4));
246 assert.equal(g.blockById(g.drag.blockId).col, 4);
247 // moveDrag only takes y — column is locked for the whole press.
248 g.moveDrag(2.2);
249 g.endDrag();
250 assert.equal(a.col, 4);
251 assert.equal(a.y, 2);
252 assert.equal(g.blockAt(5, 0).color, 3, 'neighbour column is untouched');
253 });
254
255 it('a short move that stays inside the same cell does not swap', () =>
256 {
257 const g = makeGame({ noSpawn: true });
258 const a = g.place(0, 0, 0);
259 g.place(0, 1, 1);
260 g.place(0, 2, 2);
261 assert.ok(g.beginDrag(0, 0.5));
262 // Previously SWAP_DISTANCE (0.25) triggered a full-tile jump.
263 g.moveDrag(0.5 + GameCore.SWAP_DISTANCE);
264 assert.equal(a.y, 0, 'still in the starting cell, y=' + a.y);
265 g.endDrag();
266 });
267
268 it('a 1-tile downward drag swaps exactly one step and stops when the pointer stops', () =>
269 {
270 const g = makeGame({ noSpawn: true });
271 const a = g.place(0, 0, 0);
272 const b = g.place(0, 1, 1);
273 const c = g.place(0, 2, 2);
274 assert.ok(g.beginDrag(0, 1.5));
275 g.moveDrag(0.5);
276 assert.equal(b.y, 0, 'grabbed block followed the pointer one tile down');
277 assert.equal(a.y, 1);
278 assert.equal(c.y, 2);
279 g.moveDrag(0.5);
280 assert.equal(b.y, 0, 'a stopped pointer moves nothing else');
281 assert.equal(a.y, 1);
282 assert.equal(c.y, 2);
283 g.endDrag();
284 });
285
286 it('a 1-tile upward drag swaps exactly one step and stops when the pointer stops', () =>
287 {
288 const g = makeGame({ noSpawn: true });
289 const a = g.place(0, 0, 0);
290 const b = g.place(0, 1, 1);
291 const c = g.place(0, 2, 2);
292 assert.ok(g.beginDrag(0, 0.5));
293 g.moveDrag(1.5);
294 assert.equal(a.y, 1, 'grabbed block followed the pointer one tile up');
295 assert.equal(b.y, 0);
296 assert.equal(c.y, 2);
297 g.moveDrag(1.5);
298 assert.equal(a.y, 1, 'a stopped pointer moves nothing else');
299 assert.equal(b.y, 0);
300 assert.equal(c.y, 2);
301 g.endDrag();
302 });
303
304 it('a 2-tile drag then stop leaves the block on the row under the pointer', () =>
305 {
306 const g = makeGame({ noSpawn: true });
307 const a = g.place(0, 0, 0);
308 const b = g.place(0, 1, 1);
309 const c = g.place(0, 2, 2);
310 const d = g.place(0, 3, 3);
311 assert.ok(g.beginDrag(0, 0.5));
312 g.moveDrag(2.5);
313 assert.equal(a.y, 2);
314 assert.equal(b.y, 0);
315 assert.equal(c.y, 1);
316 assert.equal(d.y, 3);
317 g.moveDrag(2.5);
318 assert.equal(a.y, 2, 'stopped pointer, no further movement');
319 assert.equal(b.y, 0);
320 assert.equal(c.y, 1);
321 assert.equal(d.y, 3);
322 // A twitch that stays in the same cell also leaves the stack alone.
323 g.moveDrag(2.7);
324 assert.equal(a.y, 2);
325 g.endDrag();
326 });
327
328 it('a fast swipe of several tiles swaps through every block and does not skip', () =>
329 {
330 const g = makeGame({ noSpawn: true });
331 const a = g.place(6, 0, 0);
332 const b = g.place(6, 1, 1);
333 const c = g.place(6, 2, 2);
334 const d = g.place(6, 3, 3);
335 const e = g.place(6, 4, 4);
336 assert.ok(g.beginDrag(6, 0.5));
337 // One large move event, as a fast swipe that only reports the end point.
338 g.moveDrag(0.5 + 4);
339 g.endDrag();
340 assert.equal(a.y, 4, 'bottom block walked all four steps, y=' + a.y);
341 assert.equal(b.y, 0, 'each neighbour was swapped, not skipped: b at ' + b.y);
342 assert.equal(c.y, 1);
343 assert.equal(d.y, 2);
344 assert.equal(e.y, 3);
345 assert.equal(g.blockAt(6, 4).id, a.id);
346 });
347});