BlueGit

projects-timfalken-com meteors

Files

meteors / test / points.test.js

points.test.js

points.test.js 4.3 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
7describe('Points, landing and recolor', () =>
8{
9 it('blocks that leave the top of the field become one point each and are gone', () =>
10 {
11 const g = makeGame({
12 noSpawn: true,
13 planetOpts: { jumpsToClear: 2, fallTime: 5, riseTime: 0.5 }
14 });
15 // t1199: minimum gravity is 2 (9.75-tile jump), so the match sits on
16 // a non-matching support stack and one jump still clears the field.
17 for (let y = 0; y < 4; y++)
18 for (const c of [0, 1, 2]) g.place(c, y, 1 + (c + y) % 3);
19 placeRow(g, 4, [0, 1, 2], 0);
20 const carried = g.place(1, 5, 4);
21 g.resolveMatches();
22 assert.equal(g.score, 0);
23 g.step(2, 1 / 120);
24 assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 0);
25 assert.equal(g.score, 4, '3 rockets + 1 carried');
26 assert.equal(g.blockAt(1, 5), null);
27 assert.ok(!g.blocks.includes(carried) && carried.y >= g.height || g.score === 4);
28 });
29
30 it('a cluster that does not clear the field lands, sits for 3 seconds, then takes random colors', () =>
31 {
32 const g = makeGame({
33 noSpawn: true,
34 planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 },
35 seed: 99
36 });
37 placeRow(g, 0, [3, 4, 5], 2);
38 g.resolveMatches();
39 // Jump height is 13/4 = 3.25, they come back down. Step until they land.
40 let landed = false;
41 for (let i = 0; i < 2000; i++)
42 {
43 g.tick(1 / 120);
44 const grayNow = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
45 if (grayNow.length >= 3 && grayNow.every((b) => b.state === 'settling'))
46 {
47 landed = true;
48 break;
49 }
50 }
51 const gray = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
52 assert.ok(landed, 'should have landed as gray, got ' + gray.length + ' states=' + gray.map((b) => b.state + '@' + b.y).join(',') + '\n' + g.snapshot().picture);
53 for (const b of gray)
54 {
55 assert.equal(b.state, 'settling');
56 assert.ok(b.y < 6, 'landed low, y=' + b.y);
57 assert.ok(b.settleTime < 0.05, 'timer starts at landing, t=' + b.settleTime);
58 }
59 g.step(2.9, 1 / 60);
60 assert.ok(gray.every((b) => b.state === 'settling'), 'still settling just before 3s: ' + gray.map((b) => b.state + '/' + b.settleTime).join(','));
61 g.step(0.2, 1 / 60);
62 for (const b of gray)
63 {
64 assert.equal(b.state, 'still');
65 assert.ok(b.color >= 0 && b.color < 5, 'recolored, color=' + b.color);
66 assert.equal(b.clusterId, null);
67 }
68 });
69
70 it('carried colored blocks keep their color and stay in the rigid group until greys convert', () =>
71 {
72 const g = makeGame({
73 noSpawn: true,
74 planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 }
75 });
76 placeRow(g, 0, [0, 1, 2], 0);
77 const carried = g.place(1, 1, 4);
78 g.resolveMatches();
79 // Land (jump is short) then sit mid-settle.
80 let landed = false;
81 for (let i = 0; i < 2000; i++)
82 {
83 g.tick(1 / 120);
84 if (carried.state === 'settling' && g.clusters[carried.clusterId]
85 && g.clusters[carried.clusterId].phase === 'landed')
86 {
87 landed = true;
88 break;
89 }
90 }
91 assert.ok(landed, 'cargo should land with the rigid group, state=' + carried.state);
92 assert.equal(carried.color, 4, 'cargo does not recolor');
93 assert.ok(carried.clusterId != null, 'cargo stays in the group');
94 g.step(2.9, 1 / 60);
95 assert.equal(carried.color, 4);
96 assert.equal(carried.state, 'settling');
97 g.step(0.2, 1 / 60);
98 assert.equal(carried.color, 4);
99 assert.equal(carried.state, 'still');
100 assert.equal(carried.clusterId, null);
101 assert.ok(almost(carried.y, 1, 1e-6), 'y=' + carried.y);
102 });
103
104 it('spawn drops a random color into a random open column every second', () =>
105 {
106 const g = makeGame({ seed: 3 });
107 g.playTime = 6; // after the opening burst / ramp (t1189)
108 assert.equal(g.blocks.length, 0);
109 g.step(0.99);
110 assert.equal(g.blocks.length, 0);
111 g.step(0.02);
112 assert.equal(g.blocks.length, 1);
113 assert.equal(g.blocks[0].state, 'falling');
114 assert.ok(g.blocks[0].color >= 0 && g.blocks[0].color < 5);
115 assert.ok(g.blocks[0].col >= 0 && g.blocks[0].col < 10);
116 g.step(1);
117 assert.equal(g.blocks.length, 2);
118 // Let the first one land.
119 g.step(g.planet.fallTime + 0.2);
120 const landed = g.blocks.filter((b) => b.state === 'still');
121 assert.ok(landed.length >= 1);
122 for (const b of landed)
123 { assert.equal(b.y, 0); }
124 });
125});