BlueGit

projects-timfalken-com meteors

Files

meteors / test / match.test.js

match.test.js

match.test.js 6.0 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, placeRow, fillColumn, Planet, GameCore } = require('./helpers.js');
6
7describe('Match detection and launch', () =>
8{
9 it('does not match two adjacent blocks of the same color', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 g.place(0, 0, 1);
13 g.place(1, 0, 1);
14 g.place(2, 0, 2);
15 assert.equal(g.findMatchLines().length, 0);
16 g.resolveMatches();
17 assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 0);
18 });
19
20 it('does not match a diagonal of three', () =>
21 {
22 const g = makeGame({ noSpawn: true });
23 g.place(0, 0, 1);
24 g.place(1, 1, 1);
25 g.place(2, 2, 1);
26 g.place(1, 0, 2);
27 g.place(2, 0, 2);
28 g.place(2, 1, 2);
29 assert.equal(g.findMatchLines().length, 0);
30 });
31
32 it('a horizontal row of 3 turns gray, links, and launches together', () =>
33 {
34 const g = makeGame({ noSpawn: true });
35 const row = placeRow(g, 0, [0, 1, 2], 0);
36 const launched = g.resolveMatches();
37 assert.equal(launched.length, 1);
38 assert.equal(launched[0].blocks.length, 3);
39 const expected = Planet.jumpHeight(g.planet, 3, false);
40 assert.equal(launched[0].jumpHeight, expected);
41 for (const b of row)
42 {
43 assert.equal(b.color, GameCore.COLOR_GRAY);
44 assert.equal(b.state, 'flying');
45 assert.equal(b.clusterId, row[0].clusterId);
46 assert.equal(b.flame, true);
47 assert.ok(b.vy > 0);
48 }
49 assert.ok(row[0].clusterId != null);
50 assert.ok(g.clusters[row[0].clusterId]);
51 });
52
53 it('a vertical column of 3 launches with 120% jump height', () =>
54 {
55 const g = makeGame({ noSpawn: true });
56 const a = g.place(3, 0, 2);
57 const b = g.place(3, 1, 2);
58 const c = g.place(3, 2, 2);
59 const launched = g.resolveMatches();
60 assert.equal(launched.length, 1);
61 const expected = Planet.jumpHeight(g.planet, 3, true);
62 assert.equal(launched[0].jumpHeight, expected);
63 assert.ok(Math.abs(expected - Planet.jumpHeight(g.planet, 3, false) * 1.2) < 1e-12);
64 assert.equal(a.color, GameCore.COLOR_GRAY);
65 assert.equal(b.clusterId, a.clusterId);
66 assert.equal(c.clusterId, a.clusterId);
67 });
68
69 it('a horizontal row of 5 gets a 10% jump-height bonus (2 extra blocks)', () =>
70 {
71 const g = makeGame({ noSpawn: true });
72 placeRow(g, 0, [0, 1, 2, 3, 4], 3);
73 const launched = g.resolveMatches();
74 assert.equal(launched[0].jumpHeight, Planet.jumpHeight(g.planet, 5, false));
75 assert.ok(Math.abs(launched[0].jumpHeight - g.planet.baseJumpHeight * 1.10) < 1e-12);
76 });
77
78 it('everything sitting on top of a match flies with it, keeping its color', () =>
79 {
80 const g = makeGame({ noSpawn: true });
81 placeRow(g, 0, [0, 1, 2], 0);
82 const carried = g.place(1, 1, 4);
83 g.resolveMatches();
84 assert.equal(carried.state, 'flying');
85 assert.equal(carried.color, 4);
86 assert.equal(carried.flame, false);
87 assert.equal(carried.clusterId, g.blockAt(0, 0).clusterId);
88 });
89
90 it('blocks below a vertical match stay on the ground', () =>
91 {
92 const g = makeGame({ noSpawn: true });
93 const below = g.place(2, 0, 1);
94 g.place(2, 1, 3);
95 g.place(2, 2, 3);
96 g.place(2, 3, 3);
97 g.resolveMatches();
98 assert.equal(below.state, 'still');
99 assert.equal(below.color, 1);
100 assert.equal(below.y, 0);
101 assert.equal(g.blockAt(2, 1).state, 'flying');
102 });
103
104 it('two disjoint horizontal matches become two clusters', () =>
105 {
106 const g = makeGame({ noSpawn: true });
107 placeRow(g, 0, [0, 1, 2], 0);
108 placeRow(g, 0, [7, 8, 9], 1);
109 const launched = g.resolveMatches();
110 assert.equal(launched.length, 2);
111 const idA = g.blockAt(0, 0).clusterId;
112 const idB = g.blockAt(7, 0).clusterId;
113 assert.notEqual(idA, idB);
114 });
115
116 it('two matches in the same columns fly as one linked cluster', () =>
117 {
118 const g = makeGame({ noSpawn: true });
119 placeRow(g, 0, [0, 1, 2], 0);
120 placeRow(g, 1, [0, 1, 2], 1);
121 const launched = g.resolveMatches();
122 assert.equal(launched.length, 1);
123 const id = g.blockAt(0, 0).clusterId;
124 assert.equal(g.blockAt(2, 1).clusterId, id);
125 assert.equal(g.clusterMembers(id).length, 6);
126 });
127
128 it('tick itself finds and launches a match after a swap', () =>
129 {
130 const g = makeGame({ noSpawn: true });
131 g.place(0, 0, 1);
132 g.place(0, 1, 2);
133 g.place(1, 0, 1);
134 g.place(2, 0, 2);
135 g.place(2, 1, 1);
136 // After swapping col 2, the 1 at (2,1) goes to (2,0): horizontal 1s at row 0.
137 g.swapTo(2, 1, 0);
138 assert.equal(g.blockAt(2, 0).color, 1);
139 g.tick(0);
140 g.resolveMatches();
141 assert.equal(g.blockAt(0, 0).state, 'flying');
142 assert.equal(g.blockAt(1, 0).state, 'flying');
143 assert.equal(g.blockAt(2, 0).state, 'flying');
144 });
145});
146
147describe('Launch physics', () =>
148{
149 it('reaches the jump height in riseTime, then eases into a constant descent', () =>
150 {
151 const g = makeGame({ noSpawn: true });
152 placeRow(g, 0, [0, 1, 2], 0);
153 const launched = g.resolveMatches();
154 const H = launched[0].jumpHeight;
155 const T = g.planet.riseTime;
156 const startY = 0;
157 g.step(T, 1 / 240);
158 const apex = g.blocks[0].y;
159 assert.ok(Math.abs(apex - (startY + H)) < 0.08, 'apex y=' + apex + ' expected ' + (startY + H));
160 const cluster = g.clusters[g.blocks[0].clusterId];
161 assert.equal(cluster.phase, 'down');
162 // Just after the apex, speed is still below the (halved) cluster float-down.
163 const ease = g.planet.apexEaseTime;
164 const clusterFall = g.planet.clusterFallSpeed;
165 assert.ok(ease > 0.1, 'apexEaseTime=' + ease);
166 g.step(ease * 0.4, 1 / 240);
167 const midVy = Math.abs(g.blocks[0].vy);
168 assert.ok(midVy > 0.2, 'should have started descending, vy=' + g.blocks[0].vy);
169 assert.ok(midVy < clusterFall - 0.15, 'should still be easing, vy=' + g.blocks[0].vy + ' clusterFall=' + clusterFall);
170 // After the ease, descent is at constant clusterFallSpeed (t1124).
171 g.step(ease * 0.6 + 0.05, 1 / 240);
172 assert.ok(Math.abs(Math.abs(g.blocks[0].vy) - clusterFall) < 0.08, 'terminal vy=' + g.blocks[0].vy);
173 assert.ok(clusterFall < g.planet.fallSpeed - 0.5, 'cluster float-down is slower than spawn fall');
174 const y1 = g.blocks[0].y;
175 g.step(0.2, 1 / 120);
176 const y2 = g.blocks[0].y;
177 const speed = (y1 - y2) / 0.2;
178 assert.ok(Math.abs(speed - clusterFall) < 0.15, 'fall speed ' + speed);
179 assert.ok(y2 < y1, 'must be going down');
180 });
181});