| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const { makeGame, placeRow, almost, Planet, GameCore } = require('./helpers.js'); |
| 6 | |
| 7 | function flyingClusterAt (game, y, colorsByCol) |
| 8 | { |
| 9 | // Place a grounded match, launch, then teleport the cluster to y. |
| 10 | const cols = Object.keys(colorsByCol).map(Number).sort((a, b) => a - b); |
| 11 | // Use a dummy match to create a cluster, then rebuild positions/colors. |
| 12 | placeRow(game, 0, cols, 0); |
| 13 | game.resolveMatches(); |
| 14 | const clusterId = game.blocks.find((b) => b.clusterId != null).clusterId; |
| 15 | const cluster = game.clusters[clusterId]; |
| 16 | for (const b of game.clusterMembers(clusterId)) |
| 17 | { game.removeBlock(b); } |
| 18 | // removeBlock drops an empty cluster; put it back before adding new members. |
| 19 | game.clusters[clusterId] = cluster; |
| 20 | const members = []; |
| 21 | for (const col of cols) |
| 22 | { |
| 23 | const b = game.createBlock(col, y, colorsByCol[col], 'flying'); |
| 24 | b.clusterId = clusterId; |
| 25 | if (colorsByCol[col] === GameCore.COLOR_GRAY) |
| 26 | { |
| 27 | b.color = GameCore.COLOR_GRAY; |
| 28 | b.flame = true; |
| 29 | } |
| 30 | members.push(b); |
| 31 | } |
| 32 | cluster.phase = 'hold'; |
| 33 | cluster.vy = 0; |
| 34 | cluster.decel = 0; |
| 35 | return { cluster, members }; |
| 36 | } |
| 37 | |
| 38 | describe('Push-up and chain re-launch', () => |
| 39 | { |
| 40 | it('a manual jump hitting a flying cluster pushes that column one tile up, other columns stay', () => |
| 41 | { |
| 42 | const g = makeGame({ noSpawn: true }); |
| 43 | // Flying gray row at y=6, cols 1,2,3. |
| 44 | const { cluster, members } = flyingClusterAt(g, 6, { 1: GameCore.COLOR_GRAY, 2: GameCore.COLOR_GRAY, 3: GameCore.COLOR_GRAY }); |
| 45 | const pushed = members.find((b) => b.col === 2); |
| 46 | const left = members.find((b) => b.col === 1); |
| 47 | const right = members.find((b) => b.col === 3); |
| 48 | const manual = g.place(2, 2, 4); |
| 49 | g.startManualLaunch(manual); |
| 50 | // Fast-forward until the push has happened (or we time out). |
| 51 | let pushedHappened = false; |
| 52 | for (let i = 0; i < 400; i++) |
| 53 | { |
| 54 | g.tick(1 / 120); |
| 55 | if (pushed.y >= 6.99) |
| 56 | { |
| 57 | pushedHappened = true; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | assert.ok(pushedHappened, 'expected the hit column to move up, y=' + pushed.y); |
| 62 | assert.ok(pushed.y >= 6.99 && pushed.y < 8.5, 'hit column y=' + pushed.y); |
| 63 | assert.ok(Math.abs(left.y - 6) < 0.3, 'left should stay, y=' + left.y); |
| 64 | assert.ok(Math.abs(right.y - 6) < 0.3, 'right should stay, y=' + right.y); |
| 65 | assert.equal(left.clusterId, cluster.id); |
| 66 | assert.equal(pushed.clusterId, cluster.id); |
| 67 | assert.equal(right.clusterId, cluster.id); |
| 68 | }); |
| 69 | |
| 70 | it('a push that completes a new row of 3 turns those gray, links them, and re-launches the whole cluster the same height', () => |
| 71 | { |
| 72 | const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 4, riseTime: 1 } }); |
| 73 | // Flying: col 0 and 2 have color 1 at y=6, col 1 has color 1 at y=5 (one tile below). |
| 74 | // A gray rocket at col 4 y=6 is the original link (disjoint, so "not always touching"). |
| 75 | const { cluster } = flyingClusterAt(g, 6, { |
| 76 | 0: 1, |
| 77 | 1: 1, |
| 78 | 2: 1, |
| 79 | 4: GameCore.COLOR_GRAY |
| 80 | }); |
| 81 | // Move col 1 down one tile so they are not yet a horizontal 3 at the same y. |
| 82 | const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id); |
| 83 | const left = g.blocks.find((b) => b.col === 0 && b.clusterId === cluster.id); |
| 84 | const right = g.blocks.find((b) => b.col === 2 && b.clusterId === cluster.id); |
| 85 | const gray = g.blocks.find((b) => b.col === 4 && b.clusterId === cluster.id); |
| 86 | mid.y = 5; |
| 87 | left.y = 6; |
| 88 | right.y = 6; |
| 89 | gray.y = 6; |
| 90 | const originalHeight = cluster.jumpHeight; |
| 91 | // Manual in col 1, will push mid from 5 to 6, forming a row of three 1s. |
| 92 | const manual = g.place(1, 1, 3); |
| 93 | g.startManualLaunch(manual); |
| 94 | let relaunched = false; |
| 95 | const midStart = mid.y; |
| 96 | for (let i = 0; i < 500; i++) |
| 97 | { |
| 98 | g.tick(1 / 120); |
| 99 | if (mid.color === GameCore.COLOR_GRAY && mid.flame && cluster.phase === 'up' && cluster.vy > 1) |
| 100 | { |
| 101 | relaunched = true; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | assert.ok(relaunched, 'expected a re-launch after the new match, mid.color=' + mid.color + ' flame=' + mid.flame + ' phase=' + cluster.phase + ' vy=' + cluster.vy + ' mid.y=' + mid.y); |
| 106 | assert.equal(left.color, GameCore.COLOR_GRAY); |
| 107 | assert.equal(right.color, GameCore.COLOR_GRAY); |
| 108 | assert.equal(mid.color, GameCore.COLOR_GRAY); |
| 109 | assert.equal(mid.clusterId, cluster.id); |
| 110 | assert.equal(gray.clusterId, cluster.id); |
| 111 | assert.equal(cluster.jumpHeight, originalHeight); |
| 112 | // The whole cluster (including the distant gray) shares the new upward velocity. |
| 113 | assert.ok(cluster.vy > 0); |
| 114 | assert.equal(gray.state, 'flying'); |
| 115 | }); |
| 116 | |
| 117 | it('if a re-launch sends every block off the top, that is fine and they become points', () => |
| 118 | { |
| 119 | const g = makeGame({ |
| 120 | noSpawn: true, |
| 121 | planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.5 } |
| 122 | }); |
| 123 | const { cluster } = flyingClusterAt(g, 11, { |
| 124 | 0: 1, |
| 125 | 1: 1, |
| 126 | 2: 1 |
| 127 | }); |
| 128 | const mid = g.blocks.find((b) => b.col === 1 && b.clusterId === cluster.id); |
| 129 | mid.y = 10; |
| 130 | g.blocks.find((b) => b.col === 0).y = 11; |
| 131 | g.blocks.find((b) => b.col === 2).y = 11; |
| 132 | const manual = g.place(1, 6, 2); |
| 133 | g.startManualLaunch(manual); |
| 134 | g.step(3, 1 / 120); |
| 135 | const flying = g.blocks.filter((b) => b.clusterId === cluster.id); |
| 136 | assert.equal(flying.length, 0, 'cluster should have left the field'); |
| 137 | assert.ok(g.score >= 3, 'score=' + g.score); |
| 138 | assert.equal(g.state, 'playing'); |
| 139 | assert.ok(!g.clusters[cluster.id]); |
| 140 | }); |
| 141 | |
| 142 | it('gray blocks can also disappear off the top and score points', () => |
| 143 | { |
| 144 | const g = makeGame({ |
| 145 | noSpawn: true, |
| 146 | planetOpts: { jumpsToClear: 1, fallTime: 5, riseTime: 0.4 } |
| 147 | }); |
| 148 | placeRow(g, 11, [0, 1, 2], 2); |
| 149 | g.resolveMatches(); |
| 150 | const rockets = g.blocks.filter((b) => b.col <= 2); |
| 151 | assert.ok(rockets.length === 3 && rockets.every((b) => b.color === GameCore.COLOR_GRAY && b.state === 'flying')); |
| 152 | g.step(2, 1 / 120); |
| 153 | assert.equal(g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY).length, 0); |
| 154 | assert.equal(g.score, 3); |
| 155 | assert.equal(g.blocks.length, 0); |
| 156 | }); |
| 157 | }); |
projects-timfalken-com meteors