BlueGit

projects-timfalken-com meteors

Files

meteors / test / points.test.js

points.test.js

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