BlueGit

projects-timfalken-com meteors

Files

meteors / test / drag-cluster.test.js

drag-cluster.test.js

drag-cluster.test.js 5.3 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, GameCore } = require('./helpers.js');
6
7function flyingGrey (game, col, y)
8{
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: 4,
13 decel: 2,
14 phase: 'up'
15 };
16 game.clusters[cluster.id] = cluster;
17 const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying');
18 b.clusterId = cluster.id;
19 b.vy = cluster.vy;
20 b.flame = true;
21 return { cluster, block: b };
22}
23
24function landedGroup (game, col, specs)
25{
26 // specs: [{ color, y }]
27 const cluster = {
28 id: game.nextClusterId++,
29 jumpHeight: 3,
30 vy: 0,
31 decel: 0,
32 phase: 'landed'
33 };
34 game.clusters[cluster.id] = cluster;
35 const members = [];
36 for (const s of specs)
37 {
38 const state = 'settling';
39 const b = game.createBlock(col, s.y, s.color, state);
40 b.clusterId = cluster.id;
41 b.vy = 0;
42 b.flame = false;
43 b.settleTime = s.settleTime || 0;
44 members.push(b);
45 }
46 return { cluster, members };
47}
48
49describe('Blocks under a flying cluster are draggable (t1107)', () =>
50{
51 it('a still stack under a flyer in the same column can be grabbed and swapped', () =>
52 {
53 const g = makeGame({ noSpawn: true });
54 const bottom = g.place(3, 0, 0);
55 const mid = g.place(3, 1, 1);
56 const { cluster, block: flyer } = flyingGrey(g, 3, 7);
57 assert.equal(g.isColumnStill(3), false, 'isColumnStill stays strict');
58 assert.equal(g.columnAllowsDrag(3), true);
59 assert.ok(g.isDraggableBlock(bottom));
60 assert.equal(g.isDraggableBlock(flyer), false, 'the flyer itself is not draggable');
61 assert.ok(g.beginDrag(3, 0.5));
62 assert.equal(g.drag.blockId, bottom.id);
63 g.moveDrag(1.5);
64 assert.equal(bottom.y, 1);
65 assert.equal(mid.y, 0);
66 g.endDrag();
67 assert.equal(flyer.state, 'flying');
68 assert.equal(flyer.clusterId, cluster.id);
69 assert.equal(flyer.y, 7, 'flyer was left alone');
70 });
71
72 it('pressing on a flying cluster does not grab it or a block below', () =>
73 {
74 const g = makeGame({ noSpawn: true });
75 g.place(1, 0, 2);
76 const { block: flyer } = flyingGrey(g, 1, 5);
77 assert.equal(g.beginDrag(1, 5.5), false);
78 assert.equal(g.drag, null);
79 assert.equal(flyer.state, 'flying');
80 });
81});
82
83describe('Dragging within, into and out of a landed cluster (t1107)', () =>
84{
85 it('dragging within a landed cluster swaps members and keeps them in the group', () =>
86 {
87 const g = makeGame({ noSpawn: true });
88 const { cluster, members } = landedGroup(g, 4, [
89 { color: GameCore.COLOR_GRAY, y: 0 },
90 { color: 1, y: 1 },
91 { color: 2, y: 2 }
92 ]);
93 const [grey, cargo1, cargo2] = members;
94 assert.ok(g.isDraggableBlock(grey));
95 assert.ok(g.isDraggableBlock(cargo1));
96 assert.ok(g.beginDrag(4, 0.4));
97 assert.equal(g.drag.blockId, grey.id);
98 g.moveDrag(2.5);
99 g.endDrag();
100 assert.equal(grey.y, 2);
101 assert.equal(cargo1.y, 0);
102 assert.equal(cargo2.y, 1);
103 assert.equal(grey.clusterId, cluster.id);
104 assert.equal(cargo1.clusterId, cluster.id);
105 assert.equal(cargo2.clusterId, cluster.id);
106 assert.equal(g.clusters[cluster.id].phase, 'landed');
107 });
108
109 it('swapping a still block into a landed cluster makes it a member', () =>
110 {
111 const g = makeGame({ noSpawn: true });
112 const still = g.place(5, 0, 3);
113 const { cluster, members } = landedGroup(g, 5, [
114 { color: GameCore.COLOR_GRAY, y: 1 },
115 { color: 4, y: 2 }
116 ]);
117 assert.ok(g.beginDrag(5, 0.5));
118 assert.equal(g.drag.blockId, still.id);
119 // Stop on the top cell, short of LAUNCH_PULL, so endDrag does not throw.
120 g.moveDrag(2.2);
121 g.endDrag();
122 assert.equal(still.y, 2);
123 assert.equal(still.clusterId, cluster.id);
124 assert.equal(still.state, 'settling');
125 assert.equal(still.color, 3);
126 // The grey was swapped into the outside slot and left the cluster.
127 assert.equal(members[0].y, 0);
128 assert.equal(members[0].clusterId, null);
129 assert.equal(members[0].state, 'settling');
130 assert.equal(members[0].color, GameCore.COLOR_GRAY);
131 // Cargo only moved to another cluster slot, so it stays a member.
132 assert.equal(members[1].y, 1);
133 assert.equal(members[1].clusterId, cluster.id);
134 assert.equal(members[1].color, 4);
135 });
136
137 it('swapping a landed member out leaves the cluster; a grey keeps its settle timer', () =>
138 {
139 const g = makeGame({ noSpawn: true });
140 g.place(6, 0, 0);
141 const { cluster, members } = landedGroup(g, 6, [
142 { color: GameCore.COLOR_GRAY, y: 1, settleTime: 1.25 },
143 { color: 2, y: 2 }
144 ]);
145 const grey = members[0];
146 assert.ok(g.beginDrag(6, 1.4));
147 assert.equal(g.drag.blockId, grey.id);
148 g.moveDrag(0.4);
149 g.endDrag();
150 assert.equal(grey.y, 0);
151 assert.equal(grey.clusterId, null);
152 assert.equal(grey.state, 'settling', 'grey keeps settling on its own timer');
153 assert.equal(grey.settleTime, 1.25);
154 assert.equal(grey.color, GameCore.COLOR_GRAY);
155 // The still block that moved in joined the cluster.
156 const joined = g.blocks.find((b) => b.color === 0);
157 assert.equal(joined.clusterId, cluster.id);
158 assert.equal(joined.state, 'settling');
159 assert.equal(joined.y, 1);
160 });
161
162 it('a gap in the column stops the swap; hanging members are part of the stack', () =>
163 {
164 const g = makeGame({ noSpawn: true });
165 const bottom = g.place(2, 0, 1);
166 const { members } = landedGroup(g, 2, [
167 { color: 3, y: 2 }
168 ]);
169 // Gap at y=1: swapToward must stop, the hanging member stays at 2.
170 assert.ok(g.beginDrag(2, 0.5));
171 g.moveDrag(2.5);
172 g.endDrag();
173 assert.equal(bottom.y, 0);
174 assert.equal(members[0].y, 2);
175 });
176});