Merge branch 'feature/game-core'
@@ -0,0 +1,1204 @@
1
'use strict';
2
3
/**
4
* Deterministic Meteors game core. No DOM, no rendering, no input devices.
5
* The view layer reads this state and translates pointer events into
6
* beginDrag / moveDrag / endDrag.
7
*/
8
9
var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined')
10
? require('./planet.js')
11
: (typeof Planet !== 'undefined' ? Planet : globalThis.Planet);
12
13
var COLOR_GRAY = -1;
14
15
var STATE_STILL = 'still';
16
var STATE_FALLING = 'falling';
17
var STATE_FLYING = 'flying';
18
var STATE_SETTLING = 'settling';
19
var STATE_LOCKED = 'locked';
20
var STATE_MANUAL = 'manual';
21
22
var ALIGN_EPS = 0.15;
23
var SUBSTEP = 1 / 120;
24
var DRAG_LAUNCH_SLACK = 0.65;
25
26
function UnionFind ()
27
{
28
this.parent = Object.create(null);
29
}
30
31
UnionFind.prototype.add = function (id)
32
{
33
if (this.parent[id] == null)
34
{ this.parent[id] = id; }
35
};
36
37
UnionFind.prototype.find = function (id)
38
{
39
if (this.parent[id] !== id)
40
{ this.parent[id] = this.find(this.parent[id]); }
41
return this.parent[id];
42
};
43
44
UnionFind.prototype.union = function (a, b)
45
{
46
this.add(a);
47
this.add(b);
48
var ra = this.find(a);
49
var rb = this.find(b);
50
if (ra !== rb)
51
{ this.parent[rb] = ra; }
52
};
53
54
function GameCore (planet, rng)
55
{
56
this.planet = planet || PlanetMod.defaultPlanet();
57
this.rng = rng || Math.random;
58
this.width = PlanetMod.FIELD_WIDTH;
59
this.height = PlanetMod.FIELD_HEIGHT;
60
this.blocks = [];
61
this.clusters = Object.create(null);
62
this.nextBlockId = 1;
63
this.nextClusterId = 1;
64
this.spawnTimer = 0;
65
this.spawnInterval = PlanetMod.SPAWN_INTERVAL;
66
this.settleTime = PlanetMod.SETTLE_TIME;
67
this.score = 0;
68
this.state = 'playing';
69
this.drag = null;
70
this.gameOverTime = 0;
71
}
72
73
GameCore.COLOR_GRAY = COLOR_GRAY;
74
GameCore.STATE_STILL = STATE_STILL;
75
GameCore.STATE_FALLING = STATE_FALLING;
76
GameCore.STATE_FLYING = STATE_FLYING;
77
GameCore.STATE_SETTLING = STATE_SETTLING;
78
GameCore.STATE_LOCKED = STATE_LOCKED;
79
GameCore.STATE_MANUAL = STATE_MANUAL;
80
81
GameCore.prototype.randomColor = function ()
82
{
83
return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length;
84
};
85
86
GameCore.prototype.createBlock = function (col, y, color, state)
87
{
88
var block = {
89
id: this.nextBlockId++,
90
col: col,
91
y: y,
92
color: color,
93
state: state || STATE_STILL,
94
vy: 0,
95
clusterId: null,
96
settleTime: 0,
97
flame: false,
98
pushedClusters: null,
99
manualTarget: 0,
100
manualDecel: 0,
101
manualPhase: null
102
};
103
this.blocks.push(block);
104
return block;
105
};
106
107
GameCore.prototype.removeBlock = function (block)
108
{
109
if (block.clusterId != null)
110
{ this.leaveCluster(block); }
111
var index = this.blocks.indexOf(block);
112
if (index >= 0)
113
{ this.blocks.splice(index, 1); }
114
};
115
116
GameCore.prototype.leaveCluster = function (block)
117
{
118
var id = block.clusterId;
119
block.clusterId = null;
120
block.flame = false;
121
if (id == null || !this.clusters[id])
122
{ return; }
123
var still = 0;
124
for (var i = 0; i < this.blocks.length; i++)
125
{
126
if (this.blocks[i].clusterId === id)
127
{ still++; }
128
}
129
if (still === 0)
130
{ delete this.clusters[id]; }
131
};
132
133
GameCore.prototype.blocksInColumn = function (col)
134
{
135
var out = [];
136
for (var i = 0; i < this.blocks.length; i++)
137
{
138
if (this.blocks[i].col === col)
139
{ out.push(this.blocks[i]); }
140
}
141
out.sort(function (a, b) { return a.y - b.y; });
142
return out;
143
};
144
145
GameCore.prototype.isGrounded = function (block)
146
{
147
return block.state === STATE_STILL
148
|| block.state === STATE_SETTLING
149
|| block.state === STATE_LOCKED;
150
};
151
152
GameCore.prototype.stackTop = function (col, ignore)
153
{
154
var top = 0;
155
for (var i = 0; i < this.blocks.length; i++)
156
{
157
var block = this.blocks[i];
158
if (block.col !== col)
159
{ continue; }
160
if (ignore && ignore(block))
161
{ continue; }
162
if (this.isGrounded(block))
163
{
164
var surface = block.y + 1;
165
if (surface > top)
166
{ top = surface; }
167
}
168
}
169
return top;
170
};
171
172
GameCore.prototype.isColumnLocked = function (col)
173
{
174
for (var i = 0; i < this.blocks.length; i++)
175
{
176
var block = this.blocks[i];
177
if (block.col === col && block.state === STATE_LOCKED)
178
{ return true; }
179
}
180
return false;
181
};
182
183
GameCore.prototype.isColumnStill = function (col)
184
{
185
if (this.isColumnLocked(col))
186
{ return false; }
187
for (var i = 0; i < this.blocks.length; i++)
188
{
189
var block = this.blocks[i];
190
if (block.col !== col)
191
{ continue; }
192
if (block.state === STATE_FLYING
193
|| block.state === STATE_FALLING
194
|| block.state === STATE_MANUAL
195
|| block.state === STATE_SETTLING)
196
{ return false; }
197
}
198
return true;
199
};
200
201
GameCore.prototype.groundedCount = function (col)
202
{
203
var n = 0;
204
for (var i = 0; i < this.blocks.length; i++)
205
{
206
if (this.blocks[i].col === col && this.isGrounded(this.blocks[i]))
207
{ n++; }
208
}
209
return n;
210
};
211
212
GameCore.prototype.blockAt = function (col, row)
213
{
214
for (var i = 0; i < this.blocks.length; i++)
215
{
216
var block = this.blocks[i];
217
if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS)
218
{ return block; }
219
}
220
return null;
221
};
222
223
GameCore.prototype.place = function (col, row, color, state)
224
{
225
return this.createBlock(col, row, color, state || STATE_STILL);
226
};
227
228
GameCore.prototype.lockedColumnCount = function ()
229
{
230
var n = 0;
231
for (var c = 0; c < this.width; c++)
232
{
233
if (this.isColumnLocked(c))
234
{ n++; }
235
}
236
return n;
237
};
238
239
GameCore.prototype.tick = function (dt)
240
{
241
if (this.state === 'gameover')
242
{
243
this.gameOverTime += Math.max(0, dt);
244
return;
245
}
246
if (dt <= 0)
247
{ return; }
248
dt = Math.min(dt, 0.1);
249
250
this.spawnTimer += dt;
251
while (this.spawnTimer >= this.spawnInterval)
252
{
253
this.spawnTimer -= this.spawnInterval;
254
this.trySpawn();
255
}
256
257
var left = dt;
258
while (left > 1e-12)
259
{
260
var step = Math.min(SUBSTEP, left);
261
this.integrateFalling(step);
262
this.integrateClusters(step);
263
this.integrateManual(step);
264
this.cullOffTop();
265
left -= step;
266
}
267
268
this.advanceSettle(dt);
269
if (!this.drag)
270
{ this.resolveMatches(); }
271
this.checkLocks();
272
this.checkGameOver();
273
};
274
275
/** Step a stretch of time in fixed substeps. Useful for tests. */
276
GameCore.prototype.step = function (seconds, dt)
277
{
278
dt = dt || SUBSTEP;
279
var t = 0;
280
while (t < seconds - 1e-15)
281
{
282
var s = Math.min(dt, seconds - t);
283
this.tick(s);
284
t += s;
285
}
286
};
287
288
GameCore.prototype.trySpawn = function ()
289
{
290
if (this.state !== 'playing')
291
{ return null; }
292
var eligible = [];
293
for (var col = 0; col < this.width; col++)
294
{
295
if (this.isColumnLocked(col))
296
{ continue; }
297
if (this.groundedCount(col) >= this.height)
298
{ continue; }
299
var alreadyFalling = false;
300
var list = this.blocksInColumn(col);
301
for (var i = 0; i < list.length; i++)
302
{
303
if (list[i].state === STATE_FALLING)
304
{
305
alreadyFalling = true;
306
break;
307
}
308
}
309
if (!alreadyFalling)
310
{ eligible.push(col); }
311
}
312
if (!eligible.length)
313
{ return null; }
314
var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
315
var color = this.randomColor();
316
return this.createBlock(col, this.height, color, STATE_FALLING);
317
};
318
319
GameCore.prototype.integrateFalling = function (dt)
320
{
321
var speed = this.planet.fallSpeed;
322
for (var i = 0; i < this.blocks.length; i++)
323
{
324
var block = this.blocks[i];
325
if (block.state !== STATE_FALLING)
326
{ continue; }
327
var top = this.stackTop(block.col);
328
var nextY = block.y - speed * dt;
329
if (nextY <= top)
330
{
331
block.y = top;
332
block.state = STATE_STILL;
333
block.vy = 0;
334
}
335
else
336
{ block.y = nextY; }
337
}
338
};
339
340
GameCore.prototype.clusterMembers = function (clusterId)
341
{
342
var out = [];
343
for (var i = 0; i < this.blocks.length; i++)
344
{
345
if (this.blocks[i].clusterId === clusterId)
346
{ out.push(this.blocks[i]); }
347
}
348
return out;
349
};
350
351
352
/**
353
* Advance a jump (cluster or manual) by dt. Uses exact kinematics so a
354
* jump of height H peaks at exactly H after riseTime. Returns dy.
355
* Mutates body.vy and body.phase ('up' / 'down'). body.decel is the
356
* upward deceleration; falling uses planet.fallSpeed.
357
*/
358
GameCore.prototype.advanceJump = function (body, dt)
359
{
360
var dy = 0;
361
var left = dt;
362
if (body.phase === 'up')
363
{
364
var decel = body.decel;
365
var vy = body.vy;
366
var tApex = decel > 1e-12 ? vy / decel : Infinity;
367
if (tApex <= left + 1e-15)
368
{
369
dy += vy * tApex - 0.5 * decel * tApex * tApex;
370
body.vy = 0;
371
body.phase = 'down';
372
left -= tApex;
373
}
374
else
375
{
376
var vyNew = vy - decel * left;
377
dy += (vy + vyNew) * 0.5 * left;
378
body.vy = vyNew;
379
left = 0;
380
}
381
}
382
if (body.phase === 'up' && body.vy <= 1e-10)
383
{
384
body.vy = 0;
385
body.phase = 'down';
386
}
387
if (body.phase === 'down' && left > 0)
388
{
389
body.vy = -this.planet.fallSpeed;
390
dy += body.vy * left;
391
}
392
return dy;
393
};
394
395
GameCore.prototype.integrateClusters = function (dt)
396
{
397
var ids = Object.keys(this.clusters);
398
for (var n = 0; n < ids.length; n++)
399
{
400
var cluster = this.clusters[ids[n]];
401
if (!cluster)
402
{ continue; }
403
var members = this.clusterMembers(cluster.id);
404
if (!members.length)
405
{
406
delete this.clusters[cluster.id];
407
continue;
408
}
409
if (cluster.phase === 'landed' || cluster.phase === 'hold')
410
{ continue; }
411
412
var dy = this.advanceJump(cluster, dt);
413
for (var i = 0; i < members.length; i++)
414
{
415
members[i].y += dy;
416
members[i].vy = cluster.vy;
417
}
418
419
if (cluster.phase === 'down')
420
{ this.tryLandCluster(cluster, members); }
421
}
422
};
423
424
GameCore.prototype.tryLandCluster = function (cluster, members)
425
{
426
var self = this;
427
var hit = false;
428
for (var i = 0; i < members.length; i++)
429
{
430
var block = members[i];
431
var top = this.stackTop(block.col, function (other)
432
{
433
return other.clusterId === cluster.id;
434
});
435
if (block.y <= top + 1e-9)
436
{ hit = true; }
437
}
438
if (!hit)
439
{ return; }
440
this.landCluster(cluster, members);
441
};
442
443
GameCore.prototype.landCluster = function (cluster, members)
444
{
445
var byCol = Object.create(null);
446
for (var i = 0; i < members.length; i++)
447
{
448
var block = members[i];
449
if (!byCol[block.col])
450
{ byCol[block.col] = []; }
451
byCol[block.col].push(block);
452
}
453
var cols = Object.keys(byCol);
454
for (var c = 0; c < cols.length; c++)
455
{
456
var col = +cols[c];
457
var list = byCol[col];
458
list.sort(function (a, b) { return a.y - b.y; });
459
var self = this;
460
var top = this.stackTop(col, function (other)
461
{
462
return other.clusterId === cluster.id;
463
});
464
for (var i = 0; i < list.length; i++)
465
{
466
var block = list[i];
467
block.y = top;
468
block.vy = 0;
469
block.flame = false;
470
top = block.y + 1;
471
if (block.color === COLOR_GRAY)
472
{
473
block.state = STATE_SETTLING;
474
block.settleTime = 0;
475
}
476
else
477
{
478
block.state = STATE_STILL;
479
block.clusterId = null;
480
}
481
}
482
}
483
cluster.phase = 'landed';
484
cluster.vy = 0;
485
var remaining = 0;
486
for (var i = 0; i < this.blocks.length; i++)
487
{
488
if (this.blocks[i].clusterId === cluster.id)
489
{ remaining++; }
490
}
491
if (remaining === 0)
492
{ delete this.clusters[cluster.id]; }
493
};
494
495
GameCore.prototype.integrateManual = function (dt)
496
{
497
for (var i = 0; i < this.blocks.length; i++)
498
{
499
var block = this.blocks[i];
500
if (block.state !== STATE_MANUAL)
501
{ continue; }
502
503
if (block.manualPhase === 'up')
504
{
505
var oldY = block.y;
506
var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };
507
var dy = this.advanceJump(proxy, dt);
508
var nextY = oldY + dy;
509
this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget));
510
block.vy = proxy.vy;
511
block.manualPhase = proxy.phase;
512
block.y = Math.min(nextY, block.manualTarget);
513
if (block.y >= block.manualTarget - 1e-12)
514
{
515
block.y = block.manualTarget;
516
block.vy = 0;
517
block.manualPhase = 'down';
518
}
519
if (block.y > this.height - 1)
520
{
521
block.y = this.height - 1;
522
block.vy = 0;
523
block.manualPhase = 'down';
524
}
525
}
526
527
if (block.manualPhase === 'down')
528
{
529
var top = this.stackTop(block.col);
530
var nextY = block.y - this.planet.fallSpeed * dt;
531
if (nextY <= top)
532
{
533
block.y = top;
534
block.state = STATE_STILL;
535
block.vy = 0;
536
block.manualPhase = null;
537
block.flame = false;
538
}
539
else
540
{
541
block.y = Math.min(nextY, this.height - 1);
542
block.vy = -this.planet.fallSpeed;
543
}
544
}
545
}
546
};
547
548
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
549
{
550
if (!manual.pushedClusters)
551
{ manual.pushedClusters = Object.create(null); }
552
553
var manualTopOld = oldY + 1;
554
var manualTopNew = nextY + 1;
555
var low = Math.min(oldY, nextY);
556
var high = Math.max(oldY, nextY) + 1;
557
558
for (var i = 0; i < this.blocks.length; i++)
559
{
560
var other = this.blocks[i];
561
if (other === manual)
562
{ continue; }
563
if (other.col !== manual.col)
564
{ continue; }
565
if (other.state !== STATE_FLYING || other.clusterId == null)
566
{ continue; }
567
if (manual.pushedClusters[other.clusterId])
568
{ continue; }
569
var otherBot = other.y;
570
var otherTop = other.y + 1;
571
var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9;
572
var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9;
573
if (overlapping || crossing)
574
{
575
if (this.pushClusterColumn(other.clusterId, manual.col))
576
{
577
manual.pushedClusters[other.clusterId] = true;
578
return;
579
}
580
}
581
}
582
};
583
584
585
/**
586
* Push every flying block of the cluster that sits in `col` one tile up.
587
* Other columns stay put (they remain linked). Then look for a new
588
* same-color line of 3+ in the flying cluster; if there is one, those
589
* blocks turn gray, join the link, and the whole cluster re-launches
590
* with the same jump height from its current position.
591
*/
592
GameCore.prototype.pushClusterColumn = function (clusterId, col)
593
{
594
var cluster = this.clusters[clusterId];
595
if (!cluster)
596
{ return false; }
597
var members = this.clusterMembers(clusterId);
598
for (var i = 0; i < members.length; i++)
599
{
600
if (members[i].col === col)
601
{ members[i].y += 1; }
602
}
603
this.cullOffTop();
604
if (!this.clusters[clusterId])
605
{ return true; }
606
this.checkChainRelaunch(cluster);
607
return true;
608
};
609
610
GameCore.prototype.checkChainRelaunch = function (cluster)
611
{
612
if (!this.clusters[cluster.id])
613
{ return; }
614
var lines = this.findFlyingMatchLines(cluster.id);
615
if (!lines.length)
616
{ return; }
617
var newly = [];
618
var seen = Object.create(null);
619
for (var i = 0; i < lines.length; i++)
620
{
621
for (var j = 0; j < lines[i].blocks.length; j++)
622
{
623
var block = lines[i].blocks[j];
624
if (seen[block.id])
625
{ continue; }
626
seen[block.id] = true;
627
newly.push(block);
628
}
629
}
630
for (var i = 0; i < newly.length; i++)
631
{
632
var block = newly[i];
633
block.color = COLOR_GRAY;
634
block.flame = true;
635
block.clusterId = cluster.id;
636
block.state = STATE_FLYING;
637
}
638
this.relaunchCluster(cluster, cluster.jumpHeight);
639
};
640
641
GameCore.prototype.relaunchCluster = function (cluster, height)
642
{
643
cluster.jumpHeight = height;
644
cluster.vy = PlanetMod.launchVelocity(this.planet, height);
645
cluster.decel = PlanetMod.launchDeceleration(this.planet, height);
646
cluster.phase = 'up';
647
var members = this.clusterMembers(cluster.id);
648
for (var i = 0; i < members.length; i++)
649
{
650
members[i].vy = cluster.vy;
651
members[i].state = STATE_FLYING;
652
if (members[i].color === COLOR_GRAY)
653
{ members[i].flame = true; }
654
}
655
};
656
657
GameCore.prototype.startManualLaunch = function (block)
658
{
659
var target = this.height - 1;
660
var height = Math.max(0.05, target - block.y);
661
block.state = STATE_MANUAL;
662
block.pushedClusters = Object.create(null);
663
block.manualTarget = target;
664
block.manualPhase = 'up';
665
block.vy = PlanetMod.launchVelocity(this.planet, height);
666
block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);
667
block.flame = false;
668
if (this.drag && this.drag.blockId === block.id)
669
{ this.drag = null; }
670
};
671
672
GameCore.prototype.cullOffTop = function ()
673
{
674
var gone = [];
675
for (var i = 0; i < this.blocks.length; i++)
676
{
677
var block = this.blocks[i];
678
if (block.state === STATE_MANUAL)
679
{
680
if (block.y > this.height - 1)
681
{ block.y = this.height - 1; }
682
continue;
683
}
684
if (block.y >= this.height - 1e-9)
685
{ gone.push(block); }
686
}
687
for (var i = 0; i < gone.length; i++)
688
{
689
this.score += 1;
690
this.removeBlock(gone[i]);
691
}
692
};
693
694
GameCore.prototype.advanceSettle = function (dt)
695
{
696
for (var i = 0; i < this.blocks.length; i++)
697
{
698
var block = this.blocks[i];
699
if (block.state !== STATE_SETTLING)
700
{ continue; }
701
block.settleTime += dt;
702
if (block.settleTime >= this.settleTime)
703
{
704
block.color = this.randomColor();
705
block.state = STATE_STILL;
706
block.settleTime = 0;
707
block.flame = false;
708
this.leaveCluster(block);
709
}
710
}
711
};
712
713
GameCore.prototype.findMatchLines = function ()
714
{
715
var candidates = [];
716
for (var i = 0; i < this.blocks.length; i++)
717
{
718
var block = this.blocks[i];
719
if (block.state === STATE_STILL && block.color >= 0)
720
{ candidates.push(block); }
721
}
722
return this.collectLines(candidates, true);
723
};
724
725
GameCore.prototype.findFlyingMatchLines = function (clusterId)
726
{
727
var candidates = [];
728
for (var i = 0; i < this.blocks.length; i++)
729
{
730
var block = this.blocks[i];
731
if (block.clusterId === clusterId && block.color >= 0
732
&& (block.state === STATE_FLYING))
733
{ candidates.push(block); }
734
}
735
return this.collectLines(candidates, false);
736
};
737
738
GameCore.prototype.collectLines = function (candidates, snapInteger)
739
{
740
var lines = [];
741
if (!candidates.length)
742
{ return lines; }
743
744
var byRow = Object.create(null);
745
for (var i = 0; i < candidates.length; i++)
746
{
747
var block = candidates[i];
748
var row = Math.round(block.y);
749
if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5))
750
{ continue; }
751
if (!byRow[row])
752
{ byRow[row] = []; }
753
byRow[row].push(block);
754
}
755
var rows = Object.keys(byRow);
756
for (var r = 0; r < rows.length; r++)
757
{
758
var list = byRow[rows[r]];
759
list.sort(function (a, b) { return a.col - b.col; });
760
this.pushRuns(lines, list, false, function (a, b)
761
{
762
return a.color === b.color && a.col === b.col - 1 && a.color >= 0;
763
});
764
}
765
766
var byCol = Object.create(null);
767
for (var i = 0; i < candidates.length; i++)
768
{
769
var block = candidates[i];
770
if (!byCol[block.col])
771
{ byCol[block.col] = []; }
772
byCol[block.col].push(block);
773
}
774
var cols = Object.keys(byCol);
775
for (var c = 0; c < cols.length; c++)
776
{
777
var list = byCol[cols[c]];
778
list.sort(function (a, b) { return a.y - b.y; });
779
this.pushRuns(lines, list, true, function (a, b)
780
{
781
return a.color === b.color && a.color >= 0
782
&& Math.abs((b.y - a.y) - 1) < ALIGN_EPS;
783
});
784
}
785
return lines;
786
};
787
788
GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent)
789
{
790
var start = 0;
791
for (var i = 1; i <= list.length; i++)
792
{
793
var same = i < list.length && adjacent(list[i - 1], list[i]);
794
if (!same)
795
{
796
var length = i - start;
797
if (length >= 3)
798
{
799
lines.push({
800
blocks: list.slice(start, i),
801
length: length,
802
vertical: vertical
803
});
804
}
805
start = i;
806
}
807
}
808
};
809
810
GameCore.prototype.resolveMatches = function ()
811
{
812
if (this.state !== 'playing' || this.drag)
813
{ return []; }
814
var lines = this.findMatchLines();
815
if (!lines.length)
816
{ return []; }
817
818
var uf = new UnionFind();
819
var blockById = Object.create(null);
820
for (var i = 0; i < lines.length; i++)
821
{
822
var line = lines[i];
823
for (var j = 0; j < line.blocks.length; j++)
824
{
825
uf.add(line.blocks[j].id);
826
blockById[line.blocks[j].id] = line.blocks[j];
827
if (j > 0)
828
{ uf.union(line.blocks[0].id, line.blocks[j].id); }
829
}
830
}
831
var matched = [];
832
var seen = Object.create(null);
833
for (var id in blockById)
834
{
835
if (!seen[id])
836
{
837
seen[id] = true;
838
matched.push(blockById[id]);
839
}
840
}
841
for (var i = 0; i < matched.length; i++)
842
{
843
for (var j = i + 1; j < matched.length; j++)
844
{
845
if (matched[i].col === matched[j].col)
846
{ uf.union(matched[i].id, matched[j].id); }
847
}
848
}
849
850
var groups = Object.create(null);
851
for (var i = 0; i < matched.length; i++)
852
{
853
var root = uf.find(matched[i].id);
854
if (!groups[root])
855
{ groups[root] = []; }
856
groups[root].push(matched[i]);
857
}
858
859
var launched = [];
860
for (var root in groups)
861
{
862
var group = groups[root];
863
var height = 0;
864
for (var i = 0; i < lines.length; i++)
865
{
866
var line = lines[i];
867
var overlaps = false;
868
for (var j = 0; j < line.blocks.length; j++)
869
{
870
for (var k = 0; k < group.length; k++)
871
{
872
if (line.blocks[j].id === group[k].id)
873
{ overlaps = true; }
874
}
875
}
876
if (overlaps)
877
{
878
var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical);
879
if (h > height)
880
{ height = h; }
881
}
882
}
883
this.launchCluster(group, height);
884
launched.push({ blocks: group, jumpHeight: height });
885
}
886
return launched;
887
};
888
889
/**
890
* Gray the matched blocks, take everything sitting on top of them in
891
* those columns, link them, and launch with jump height `height`.
892
*/
893
GameCore.prototype.launchCluster = function (matchedBlocks, height)
894
{
895
var lowestInCol = Object.create(null);
896
var matchedIds = Object.create(null);
897
for (var i = 0; i < matchedBlocks.length; i++)
898
{
899
var block = matchedBlocks[i];
900
matchedIds[block.id] = true;
901
if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col])
902
{ lowestInCol[block.col] = block.y; }
903
}
904
905
var clusterId = this.nextClusterId++;
906
var cluster = {
907
id: clusterId,
908
jumpHeight: height,
909
vy: PlanetMod.launchVelocity(this.planet, height),
910
decel: PlanetMod.launchDeceleration(this.planet, height),
911
phase: 'up'
912
};
913
this.clusters[clusterId] = cluster;
914
915
var riders = [];
916
var cols = Object.keys(lowestInCol);
917
for (var c = 0; c < cols.length; c++)
918
{
919
var col = +cols[c];
920
var lowest = lowestInCol[col];
921
var list = this.blocksInColumn(col);
922
for (var i = 0; i < list.length; i++)
923
{
924
var block = list[i];
925
if (!this.isGrounded(block) && block.state !== STATE_STILL)
926
{
927
if (block.state !== STATE_SETTLING)
928
{ continue; }
929
}
930
if (block.y + ALIGN_EPS >= lowest)
931
{ riders.push(block); }
932
}
933
}
934
935
for (var i = 0; i < riders.length; i++)
936
{
937
var block = riders[i];
938
var isMatch = !!matchedIds[block.id];
939
if (isMatch || block.color === COLOR_GRAY)
940
{
941
block.color = COLOR_GRAY;
942
block.flame = true;
943
}
944
else
945
{ block.flame = false; }
946
block.state = STATE_FLYING;
947
block.clusterId = clusterId;
948
block.vy = cluster.vy;
949
block.settleTime = 0;
950
}
951
return cluster;
952
};
953
954
GameCore.prototype.checkLocks = function ()
955
{
956
for (var col = 0; col < this.width; col++)
957
{
958
if (this.isColumnLocked(col))
959
{ continue; }
960
if (this.groundedCount(col) < this.height)
961
{ continue; }
962
// Must fill the column from floor to top: 13 grounded blocks at y 0..12.
963
var filled = true;
964
for (var row = 0; row < this.height; row++)
965
{
966
var block = this.blockAt(col, row);
967
if (!block || !this.isGrounded(block))
968
{
969
filled = false;
970
break;
971
}
972
}
973
if (!filled)
974
{ continue; }
975
var list = this.blocksInColumn(col);
976
for (var i = 0; i < list.length; i++)
977
{
978
var block = list[i];
979
if (!this.isGrounded(block))
980
{ continue; }
981
block.state = STATE_LOCKED;
982
block.color = COLOR_GRAY;
983
block.flame = false;
984
block.vy = 0;
985
block.settleTime = 0;
986
if (block.clusterId != null)
987
{ this.leaveCluster(block); }
988
}
989
}
990
};
991
992
GameCore.prototype.checkGameOver = function ()
993
{
994
if (this.state !== 'playing')
995
{ return; }
996
for (var col = 0; col < this.width; col++)
997
{
998
if (!this.isColumnLocked(col))
999
{ return; }
1000
}
1001
this.state = 'gameover';
1002
this.gameOverTime = 0;
1003
this.drag = null;
1004
};
1005
1006
/* ---------- dragging -------------------------------------------------- */
1007
1008
GameCore.prototype.beginDrag = function (col, y)
1009
{
1010
if (this.state !== 'playing')
1011
{ return false; }
1012
col = col | 0;
1013
if (col < 0 || col >= this.width)
1014
{ return false; }
1015
if (!this.isColumnStill(col))
1016
{ return false; }
1017
var row = Math.floor(y + 1e-6);
1018
if (row < 0)
1019
{ return false; }
1020
var block = this.blockAt(col, row);
1021
if (!block || block.state !== STATE_STILL || block.color < 0)
1022
{ return false; }
1023
this.drag = { blockId: block.id, pointerY: y, launchIntent: false };
1024
return true;
1025
};
1026
1027
GameCore.prototype.moveDrag = function (y)
1028
{
1029
if (!this.drag)
1030
{ return; }
1031
var block = this.blockById(this.drag.blockId);
1032
if (!block || block.state !== STATE_STILL)
1033
{
1034
this.drag = null;
1035
return;
1036
}
1037
if (!this.isColumnStill(block.col))
1038
{
1039
this.drag = null;
1040
return;
1041
}
1042
this.drag.pointerY = y;
1043
this.swapToward(block, y);
1044
var top = this.columnStackTopRow(block.col);
1045
// The top block occupies [top, top+1). Launch only when the pointer
1046
// is pulled into the empty space above the stack.
1047
this.drag.launchIntent = (Math.abs(block.y - top) < ALIGN_EPS && y > top + 1 + 0.25);
1048
};
1049
1050
GameCore.prototype.endDrag = function ()
1051
{
1052
if (!this.drag)
1053
{ return; }
1054
var block = this.blockById(this.drag.blockId);
1055
var launch = this.drag.launchIntent;
1056
this.drag = null;
1057
if (launch && block && block.state === STATE_STILL)
1058
{ this.startManualLaunch(block); }
1059
else
1060
{ this.resolveMatches(); }
1061
};
1062
1063
GameCore.prototype.blockById = function (id)
1064
{
1065
for (var i = 0; i < this.blocks.length; i++)
1066
{
1067
if (this.blocks[i].id === id)
1068
{ return this.blocks[i]; }
1069
}
1070
return null;
1071
};
1072
1073
GameCore.prototype.columnStackTopRow = function (col)
1074
{
1075
var top = -1;
1076
var list = this.blocksInColumn(col);
1077
for (var i = 0; i < list.length; i++)
1078
{
1079
if (this.isGrounded(list[i]) && list[i].y > top)
1080
{ top = list[i].y; }
1081
}
1082
return top;
1083
};
1084
1085
GameCore.prototype.swapToward = function (block, targetY)
1086
{
1087
var guard = 0;
1088
while (guard++ < this.height + 2)
1089
{
1090
var current = block.y;
1091
if (targetY > current + 0.5)
1092
{
1093
var above = this.blockAt(block.col, current + 1);
1094
if (above && this.isGrounded(above) && above !== block)
1095
{ this.swapBlocks(block, above); }
1096
else
1097
{ break; }
1098
}
1099
else if (targetY < current - 0.5)
1100
{
1101
var below = this.blockAt(block.col, current - 1);
1102
if (below && this.isGrounded(below) && below !== block)
1103
{ this.swapBlocks(block, below); }
1104
else
1105
{ break; }
1106
}
1107
else
1108
{ break; }
1109
}
1110
};
1111
1112
GameCore.prototype.swapBlocks = function (a, b)
1113
{
1114
var y = a.y;
1115
a.y = b.y;
1116
b.y = y;
1117
};
1118
1119
/**
1120
* Test helper: stepwise-swap the block at (col, fromRow) to toRow.
1121
* Returns the moved block, or null if the column is not still.
1122
*/
1123
GameCore.prototype.swapTo = function (col, fromRow, toRow)
1124
{
1125
if (!this.isColumnStill(col))
1126
{ return null; }
1127
var block = this.blockAt(col, fromRow);
1128
if (!block)
1129
{ return null; }
1130
this.swapToward(block, toRow);
1131
return block;
1132
};
1133
1134
/**
1135
* Test helper: a compact picture of grounded/falling/flying blocks.
1136
* Bottom row is index 0. Characters: 0-4 color, g gray flying/settling,
1137
* L locked, f falling, m manual, . empty. Flying colored blocks use
1138
* their color with a marker in `flying`.
1139
*/
1140
GameCore.prototype.snapshot = function ()
1141
{
1142
var grid = [];
1143
for (var r = 0; r < this.height; r++)
1144
{
1145
var row = [];
1146
for (var c = 0; c < this.width; c++)
1147
{ row.push('.'); }
1148
grid.push(row);
1149
}
1150
var airborne = [];
1151
for (var i = 0; i < this.blocks.length; i++)
1152
{
1153
var block = this.blocks[i];
1154
var ch;
1155
if (block.state === STATE_LOCKED)
1156
{ ch = 'L'; }
1157
else if (block.color === COLOR_GRAY)
1158
{ ch = 'g'; }
1159
else
1160
{ ch = String(block.color); }
1161
if (this.isGrounded(block) || block.state === STATE_FALLING)
1162
{
1163
var row = Math.round(block.y);
1164
if (row >= 0 && row < this.height)
1165
{ grid[row][block.col] = ch; }
1166
}
1167
else
1168
{
1169
airborne.push({
1170
id: block.id,
1171
col: block.col,
1172
y: block.y,
1173
color: block.color,
1174
state: block.state,
1175
clusterId: block.clusterId,
1176
flame: block.flame,
1177
vy: block.vy
1178
});
1179
}
1180
}
1181
var lines = [];
1182
for (var r = this.height - 1; r >= 0; r--)
1183
{ lines.push(grid[r].join('')); }
1184
return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state };
1185
};
1186
1187
var GameCoreAPI = {
1188
GameCore: GameCore,
1189
COLOR_GRAY: COLOR_GRAY,
1190
STATE_STILL: STATE_STILL,
1191
STATE_FALLING: STATE_FALLING,
1192
STATE_FLYING: STATE_FLYING,
1193
STATE_SETTLING: STATE_SETTLING,
1194
STATE_LOCKED: STATE_LOCKED,
1195
STATE_MANUAL: STATE_MANUAL
1196
};
1197
1198
if (typeof module !== 'undefined' && module.exports)
1199
{ module.exports = GameCoreAPI; }
1200
else if (typeof globalThis !== 'undefined')
1201
{
1202
globalThis.GameCore = GameCore;
1203
globalThis.GameCoreAPI = GameCoreAPI;
1204
}
@@ -0,0 +1,1204 @@1
'use strict';2
3
/**4
* Deterministic Meteors game core. No DOM, no rendering, no input devices.5
* The view layer reads this state and translates pointer events into6
* beginDrag / moveDrag / endDrag.7
*/8
9
var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined')10
? require('./planet.js')11
: (typeof Planet !== 'undefined' ? Planet : globalThis.Planet);12
13
var COLOR_GRAY = -1;14
15
var STATE_STILL = 'still';16
var STATE_FALLING = 'falling';17
var STATE_FLYING = 'flying';18
var STATE_SETTLING = 'settling';19
var STATE_LOCKED = 'locked';20
var STATE_MANUAL = 'manual';21
22
var ALIGN_EPS = 0.15;23
var SUBSTEP = 1 / 120;24
var DRAG_LAUNCH_SLACK = 0.65;25
26
function UnionFind ()27
{28
this.parent = Object.create(null);29
}30
31
UnionFind.prototype.add = function (id)32
{33
if (this.parent[id] == null)34
{ this.parent[id] = id; }35
};36
37
UnionFind.prototype.find = function (id)38
{39
if (this.parent[id] !== id)40
{ this.parent[id] = this.find(this.parent[id]); }41
return this.parent[id];42
};43
44
UnionFind.prototype.union = function (a, b)45
{46
this.add(a);47
this.add(b);48
var ra = this.find(a);49
var rb = this.find(b);50
if (ra !== rb)51
{ this.parent[rb] = ra; }52
};53
54
function GameCore (planet, rng)55
{56
this.planet = planet || PlanetMod.defaultPlanet();57
this.rng = rng || Math.random;58
this.width = PlanetMod.FIELD_WIDTH;59
this.height = PlanetMod.FIELD_HEIGHT;60
this.blocks = [];61
this.clusters = Object.create(null);62
this.nextBlockId = 1;63
this.nextClusterId = 1;64
this.spawnTimer = 0;65
this.spawnInterval = PlanetMod.SPAWN_INTERVAL;66
this.settleTime = PlanetMod.SETTLE_TIME;67
this.score = 0;68
this.state = 'playing';69
this.drag = null;70
this.gameOverTime = 0;71
}72
73
GameCore.COLOR_GRAY = COLOR_GRAY;74
GameCore.STATE_STILL = STATE_STILL;75
GameCore.STATE_FALLING = STATE_FALLING;76
GameCore.STATE_FLYING = STATE_FLYING;77
GameCore.STATE_SETTLING = STATE_SETTLING;78
GameCore.STATE_LOCKED = STATE_LOCKED;79
GameCore.STATE_MANUAL = STATE_MANUAL;80
81
GameCore.prototype.randomColor = function ()82
{83
return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length;84
};85
86
GameCore.prototype.createBlock = function (col, y, color, state)87
{88
var block = {89
id: this.nextBlockId++,90
col: col,91
y: y,92
color: color,93
state: state || STATE_STILL,94
vy: 0,95
clusterId: null,96
settleTime: 0,97
flame: false,98
pushedClusters: null,99
manualTarget: 0,100
manualDecel: 0,101
manualPhase: null102
};103
this.blocks.push(block);104
return block;105
};106
107
GameCore.prototype.removeBlock = function (block)108
{109
if (block.clusterId != null)110
{ this.leaveCluster(block); }111
var index = this.blocks.indexOf(block);112
if (index >= 0)113
{ this.blocks.splice(index, 1); }114
};115
116
GameCore.prototype.leaveCluster = function (block)117
{118
var id = block.clusterId;119
block.clusterId = null;120
block.flame = false;121
if (id == null || !this.clusters[id])122
{ return; }123
var still = 0;124
for (var i = 0; i < this.blocks.length; i++)125
{126
if (this.blocks[i].clusterId === id)127
{ still++; }128
}129
if (still === 0)130
{ delete this.clusters[id]; }131
};132
133
GameCore.prototype.blocksInColumn = function (col)134
{135
var out = [];136
for (var i = 0; i < this.blocks.length; i++)137
{138
if (this.blocks[i].col === col)139
{ out.push(this.blocks[i]); }140
}141
out.sort(function (a, b) { return a.y - b.y; });142
return out;143
};144
145
GameCore.prototype.isGrounded = function (block)146
{147
return block.state === STATE_STILL148
|| block.state === STATE_SETTLING149
|| block.state === STATE_LOCKED;150
};151
152
GameCore.prototype.stackTop = function (col, ignore)153
{154
var top = 0;155
for (var i = 0; i < this.blocks.length; i++)156
{157
var block = this.blocks[i];158
if (block.col !== col)159
{ continue; }160
if (ignore && ignore(block))161
{ continue; }162
if (this.isGrounded(block))163
{164
var surface = block.y + 1;165
if (surface > top)166
{ top = surface; }167
}168
}169
return top;170
};171
172
GameCore.prototype.isColumnLocked = function (col)173
{174
for (var i = 0; i < this.blocks.length; i++)175
{176
var block = this.blocks[i];177
if (block.col === col && block.state === STATE_LOCKED)178
{ return true; }179
}180
return false;181
};182
183
GameCore.prototype.isColumnStill = function (col)184
{185
if (this.isColumnLocked(col))186
{ return false; }187
for (var i = 0; i < this.blocks.length; i++)188
{189
var block = this.blocks[i];190
if (block.col !== col)191
{ continue; }192
if (block.state === STATE_FLYING193
|| block.state === STATE_FALLING194
|| block.state === STATE_MANUAL195
|| block.state === STATE_SETTLING)196
{ return false; }197
}198
return true;199
};200
201
GameCore.prototype.groundedCount = function (col)202
{203
var n = 0;204
for (var i = 0; i < this.blocks.length; i++)205
{206
if (this.blocks[i].col === col && this.isGrounded(this.blocks[i]))207
{ n++; }208
}209
return n;210
};211
212
GameCore.prototype.blockAt = function (col, row)213
{214
for (var i = 0; i < this.blocks.length; i++)215
{216
var block = this.blocks[i];217
if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS)218
{ return block; }219
}220
return null;221
};222
223
GameCore.prototype.place = function (col, row, color, state)224
{225
return this.createBlock(col, row, color, state || STATE_STILL);226
};227
228
GameCore.prototype.lockedColumnCount = function ()229
{230
var n = 0;231
for (var c = 0; c < this.width; c++)232
{233
if (this.isColumnLocked(c))234
{ n++; }235
}236
return n;237
};238
239
GameCore.prototype.tick = function (dt)240
{241
if (this.state === 'gameover')242
{243
this.gameOverTime += Math.max(0, dt);244
return;245
}246
if (dt <= 0)247
{ return; }248
dt = Math.min(dt, 0.1);249
250
this.spawnTimer += dt;251
while (this.spawnTimer >= this.spawnInterval)252
{253
this.spawnTimer -= this.spawnInterval;254
this.trySpawn();255
}256
257
var left = dt;258
while (left > 1e-12)259
{260
var step = Math.min(SUBSTEP, left);261
this.integrateFalling(step);262
this.integrateClusters(step);263
this.integrateManual(step);264
this.cullOffTop();265
left -= step;266
}267
268
this.advanceSettle(dt);269
if (!this.drag)270
{ this.resolveMatches(); }271
this.checkLocks();272
this.checkGameOver();273
};274
275
/** Step a stretch of time in fixed substeps. Useful for tests. */276
GameCore.prototype.step = function (seconds, dt)277
{278
dt = dt || SUBSTEP;279
var t = 0;280
while (t < seconds - 1e-15)281
{282
var s = Math.min(dt, seconds - t);283
this.tick(s);284
t += s;285
}286
};287
288
GameCore.prototype.trySpawn = function ()289
{290
if (this.state !== 'playing')291
{ return null; }292
var eligible = [];293
for (var col = 0; col < this.width; col++)294
{295
if (this.isColumnLocked(col))296
{ continue; }297
if (this.groundedCount(col) >= this.height)298
{ continue; }299
var alreadyFalling = false;300
var list = this.blocksInColumn(col);301
for (var i = 0; i < list.length; i++)302
{303
if (list[i].state === STATE_FALLING)304
{305
alreadyFalling = true;306
break;307
}308
}309
if (!alreadyFalling)310
{ eligible.push(col); }311
}312
if (!eligible.length)313
{ return null; }314
var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];315
var color = this.randomColor();316
return this.createBlock(col, this.height, color, STATE_FALLING);317
};318
319
GameCore.prototype.integrateFalling = function (dt)320
{321
var speed = this.planet.fallSpeed;322
for (var i = 0; i < this.blocks.length; i++)323
{324
var block = this.blocks[i];325
if (block.state !== STATE_FALLING)326
{ continue; }327
var top = this.stackTop(block.col);328
var nextY = block.y - speed * dt;329
if (nextY <= top)330
{331
block.y = top;332
block.state = STATE_STILL;333
block.vy = 0;334
}335
else336
{ block.y = nextY; }337
}338
};339
340
GameCore.prototype.clusterMembers = function (clusterId)341
{342
var out = [];343
for (var i = 0; i < this.blocks.length; i++)344
{345
if (this.blocks[i].clusterId === clusterId)346
{ out.push(this.blocks[i]); }347
}348
return out;349
};350
351
352
/**353
* Advance a jump (cluster or manual) by dt. Uses exact kinematics so a354
* jump of height H peaks at exactly H after riseTime. Returns dy.355
* Mutates body.vy and body.phase ('up' / 'down'). body.decel is the356
* upward deceleration; falling uses planet.fallSpeed.357
*/358
GameCore.prototype.advanceJump = function (body, dt)359
{360
var dy = 0;361
var left = dt;362
if (body.phase === 'up')363
{364
var decel = body.decel;365
var vy = body.vy;366
var tApex = decel > 1e-12 ? vy / decel : Infinity;367
if (tApex <= left + 1e-15)368
{369
dy += vy * tApex - 0.5 * decel * tApex * tApex;370
body.vy = 0;371
body.phase = 'down';372
left -= tApex;373
}374
else375
{376
var vyNew = vy - decel * left;377
dy += (vy + vyNew) * 0.5 * left;378
body.vy = vyNew;379
left = 0;380
}381
}382
if (body.phase === 'up' && body.vy <= 1e-10)383
{384
body.vy = 0;385
body.phase = 'down';386
}387
if (body.phase === 'down' && left > 0)388
{389
body.vy = -this.planet.fallSpeed;390
dy += body.vy * left;391
}392
return dy;393
};394
395
GameCore.prototype.integrateClusters = function (dt)396
{397
var ids = Object.keys(this.clusters);398
for (var n = 0; n < ids.length; n++)399
{400
var cluster = this.clusters[ids[n]];401
if (!cluster)402
{ continue; }403
var members = this.clusterMembers(cluster.id);404
if (!members.length)405
{406
delete this.clusters[cluster.id];407
continue;408
}409
if (cluster.phase === 'landed' || cluster.phase === 'hold')410
{ continue; }411
412
var dy = this.advanceJump(cluster, dt);413
for (var i = 0; i < members.length; i++)414
{415
members[i].y += dy;416
members[i].vy = cluster.vy;417
}418
419
if (cluster.phase === 'down')420
{ this.tryLandCluster(cluster, members); }421
}422
};423
424
GameCore.prototype.tryLandCluster = function (cluster, members)425
{426
var self = this;427
var hit = false;428
for (var i = 0; i < members.length; i++)429
{430
var block = members[i];431
var top = this.stackTop(block.col, function (other)432
{433
return other.clusterId === cluster.id;434
});435
if (block.y <= top + 1e-9)436
{ hit = true; }437
}438
if (!hit)439
{ return; }440
this.landCluster(cluster, members);441
};442
443
GameCore.prototype.landCluster = function (cluster, members)444
{445
var byCol = Object.create(null);446
for (var i = 0; i < members.length; i++)447
{448
var block = members[i];449
if (!byCol[block.col])450
{ byCol[block.col] = []; }451
byCol[block.col].push(block);452
}453
var cols = Object.keys(byCol);454
for (var c = 0; c < cols.length; c++)455
{456
var col = +cols[c];457
var list = byCol[col];458
list.sort(function (a, b) { return a.y - b.y; });459
var self = this;460
var top = this.stackTop(col, function (other)461
{462
return other.clusterId === cluster.id;463
});464
for (var i = 0; i < list.length; i++)465
{466
var block = list[i];467
block.y = top;468
block.vy = 0;469
block.flame = false;470
top = block.y + 1;471
if (block.color === COLOR_GRAY)472
{473
block.state = STATE_SETTLING;474
block.settleTime = 0;475
}476
else477
{478
block.state = STATE_STILL;479
block.clusterId = null;480
}481
}482
}483
cluster.phase = 'landed';484
cluster.vy = 0;485
var remaining = 0;486
for (var i = 0; i < this.blocks.length; i++)487
{488
if (this.blocks[i].clusterId === cluster.id)489
{ remaining++; }490
}491
if (remaining === 0)492
{ delete this.clusters[cluster.id]; }493
};494
495
GameCore.prototype.integrateManual = function (dt)496
{497
for (var i = 0; i < this.blocks.length; i++)498
{499
var block = this.blocks[i];500
if (block.state !== STATE_MANUAL)501
{ continue; }502
503
if (block.manualPhase === 'up')504
{505
var oldY = block.y;506
var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel };507
var dy = this.advanceJump(proxy, dt);508
var nextY = oldY + dy;509
this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget));510
block.vy = proxy.vy;511
block.manualPhase = proxy.phase;512
block.y = Math.min(nextY, block.manualTarget);513
if (block.y >= block.manualTarget - 1e-12)514
{515
block.y = block.manualTarget;516
block.vy = 0;517
block.manualPhase = 'down';518
}519
if (block.y > this.height - 1)520
{521
block.y = this.height - 1;522
block.vy = 0;523
block.manualPhase = 'down';524
}525
}526
527
if (block.manualPhase === 'down')528
{529
var top = this.stackTop(block.col);530
var nextY = block.y - this.planet.fallSpeed * dt;531
if (nextY <= top)532
{533
block.y = top;534
block.state = STATE_STILL;535
block.vy = 0;536
block.manualPhase = null;537
block.flame = false;538
}539
else540
{541
block.y = Math.min(nextY, this.height - 1);542
block.vy = -this.planet.fallSpeed;543
}544
}545
}546
};547
548
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)549
{550
if (!manual.pushedClusters)551
{ manual.pushedClusters = Object.create(null); }552
553
var manualTopOld = oldY + 1;554
var manualTopNew = nextY + 1;555
var low = Math.min(oldY, nextY);556
var high = Math.max(oldY, nextY) + 1;557
558
for (var i = 0; i < this.blocks.length; i++)559
{560
var other = this.blocks[i];561
if (other === manual)562
{ continue; }563
if (other.col !== manual.col)564
{ continue; }565
if (other.state !== STATE_FLYING || other.clusterId == null)566
{ continue; }567
if (manual.pushedClusters[other.clusterId])568
{ continue; }569
var otherBot = other.y;570
var otherTop = other.y + 1;571
var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9;572
var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9;573
if (overlapping || crossing)574
{575
if (this.pushClusterColumn(other.clusterId, manual.col))576
{577
manual.pushedClusters[other.clusterId] = true;578
return;579
}580
}581
}582
};583
584
585
/**586
* Push every flying block of the cluster that sits in `col` one tile up.587
* Other columns stay put (they remain linked). Then look for a new588
* same-color line of 3+ in the flying cluster; if there is one, those589
* blocks turn gray, join the link, and the whole cluster re-launches590
* with the same jump height from its current position.591
*/592
GameCore.prototype.pushClusterColumn = function (clusterId, col)593
{594
var cluster = this.clusters[clusterId];595
if (!cluster)596
{ return false; }597
var members = this.clusterMembers(clusterId);598
for (var i = 0; i < members.length; i++)599
{600
if (members[i].col === col)601
{ members[i].y += 1; }602
}603
this.cullOffTop();604
if (!this.clusters[clusterId])605
{ return true; }606
this.checkChainRelaunch(cluster);607
return true;608
};609
610
GameCore.prototype.checkChainRelaunch = function (cluster)611
{612
if (!this.clusters[cluster.id])613
{ return; }614
var lines = this.findFlyingMatchLines(cluster.id);615
if (!lines.length)616
{ return; }617
var newly = [];618
var seen = Object.create(null);619
for (var i = 0; i < lines.length; i++)620
{621
for (var j = 0; j < lines[i].blocks.length; j++)622
{623
var block = lines[i].blocks[j];624
if (seen[block.id])625
{ continue; }626
seen[block.id] = true;627
newly.push(block);628
}629
}630
for (var i = 0; i < newly.length; i++)631
{632
var block = newly[i];633
block.color = COLOR_GRAY;634
block.flame = true;635
block.clusterId = cluster.id;636
block.state = STATE_FLYING;637
}638
this.relaunchCluster(cluster, cluster.jumpHeight);639
};640
641
GameCore.prototype.relaunchCluster = function (cluster, height)642
{643
cluster.jumpHeight = height;644
cluster.vy = PlanetMod.launchVelocity(this.planet, height);645
cluster.decel = PlanetMod.launchDeceleration(this.planet, height);646
cluster.phase = 'up';647
var members = this.clusterMembers(cluster.id);648
for (var i = 0; i < members.length; i++)649
{650
members[i].vy = cluster.vy;651
members[i].state = STATE_FLYING;652
if (members[i].color === COLOR_GRAY)653
{ members[i].flame = true; }654
}655
};656
657
GameCore.prototype.startManualLaunch = function (block)658
{659
var target = this.height - 1;660
var height = Math.max(0.05, target - block.y);661
block.state = STATE_MANUAL;662
block.pushedClusters = Object.create(null);663
block.manualTarget = target;664
block.manualPhase = 'up';665
block.vy = PlanetMod.launchVelocity(this.planet, height);666
block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);667
block.flame = false;668
if (this.drag && this.drag.blockId === block.id)669
{ this.drag = null; }670
};671
672
GameCore.prototype.cullOffTop = function ()673
{674
var gone = [];675
for (var i = 0; i < this.blocks.length; i++)676
{677
var block = this.blocks[i];678
if (block.state === STATE_MANUAL)679
{680
if (block.y > this.height - 1)681
{ block.y = this.height - 1; }682
continue;683
}684
if (block.y >= this.height - 1e-9)685
{ gone.push(block); }686
}687
for (var i = 0; i < gone.length; i++)688
{689
this.score += 1;690
this.removeBlock(gone[i]);691
}692
};693
694
GameCore.prototype.advanceSettle = function (dt)695
{696
for (var i = 0; i < this.blocks.length; i++)697
{698
var block = this.blocks[i];699
if (block.state !== STATE_SETTLING)700
{ continue; }701
block.settleTime += dt;702
if (block.settleTime >= this.settleTime)703
{704
block.color = this.randomColor();705
block.state = STATE_STILL;706
block.settleTime = 0;707
block.flame = false;708
this.leaveCluster(block);709
}710
}711
};712
713
GameCore.prototype.findMatchLines = function ()714
{715
var candidates = [];716
for (var i = 0; i < this.blocks.length; i++)717
{718
var block = this.blocks[i];719
if (block.state === STATE_STILL && block.color >= 0)720
{ candidates.push(block); }721
}722
return this.collectLines(candidates, true);723
};724
725
GameCore.prototype.findFlyingMatchLines = function (clusterId)726
{727
var candidates = [];728
for (var i = 0; i < this.blocks.length; i++)729
{730
var block = this.blocks[i];731
if (block.clusterId === clusterId && block.color >= 0732
&& (block.state === STATE_FLYING))733
{ candidates.push(block); }734
}735
return this.collectLines(candidates, false);736
};737
738
GameCore.prototype.collectLines = function (candidates, snapInteger)739
{740
var lines = [];741
if (!candidates.length)742
{ return lines; }743
744
var byRow = Object.create(null);745
for (var i = 0; i < candidates.length; i++)746
{747
var block = candidates[i];748
var row = Math.round(block.y);749
if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5))750
{ continue; }751
if (!byRow[row])752
{ byRow[row] = []; }753
byRow[row].push(block);754
}755
var rows = Object.keys(byRow);756
for (var r = 0; r < rows.length; r++)757
{758
var list = byRow[rows[r]];759
list.sort(function (a, b) { return a.col - b.col; });760
this.pushRuns(lines, list, false, function (a, b)761
{762
return a.color === b.color && a.col === b.col - 1 && a.color >= 0;763
});764
}765
766
var byCol = Object.create(null);767
for (var i = 0; i < candidates.length; i++)768
{769
var block = candidates[i];770
if (!byCol[block.col])771
{ byCol[block.col] = []; }772
byCol[block.col].push(block);773
}774
var cols = Object.keys(byCol);775
for (var c = 0; c < cols.length; c++)776
{777
var list = byCol[cols[c]];778
list.sort(function (a, b) { return a.y - b.y; });779
this.pushRuns(lines, list, true, function (a, b)780
{781
return a.color === b.color && a.color >= 0782
&& Math.abs((b.y - a.y) - 1) < ALIGN_EPS;783
});784
}785
return lines;786
};787
788
GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent)789
{790
var start = 0;791
for (var i = 1; i <= list.length; i++)792
{793
var same = i < list.length && adjacent(list[i - 1], list[i]);794
if (!same)795
{796
var length = i - start;797
if (length >= 3)798
{799
lines.push({800
blocks: list.slice(start, i),801
length: length,802
vertical: vertical803
});804
}805
start = i;806
}807
}808
};809
810
GameCore.prototype.resolveMatches = function ()811
{812
if (this.state !== 'playing' || this.drag)813
{ return []; }814
var lines = this.findMatchLines();815
if (!lines.length)816
{ return []; }817
818
var uf = new UnionFind();819
var blockById = Object.create(null);820
for (var i = 0; i < lines.length; i++)821
{822
var line = lines[i];823
for (var j = 0; j < line.blocks.length; j++)824
{825
uf.add(line.blocks[j].id);826
blockById[line.blocks[j].id] = line.blocks[j];827
if (j > 0)828
{ uf.union(line.blocks[0].id, line.blocks[j].id); }829
}830
}831
var matched = [];832
var seen = Object.create(null);833
for (var id in blockById)834
{835
if (!seen[id])836
{837
seen[id] = true;838
matched.push(blockById[id]);839
}840
}841
for (var i = 0; i < matched.length; i++)842
{843
for (var j = i + 1; j < matched.length; j++)844
{845
if (matched[i].col === matched[j].col)846
{ uf.union(matched[i].id, matched[j].id); }847
}848
}849
850
var groups = Object.create(null);851
for (var i = 0; i < matched.length; i++)852
{853
var root = uf.find(matched[i].id);854
if (!groups[root])855
{ groups[root] = []; }856
groups[root].push(matched[i]);857
}858
859
var launched = [];860
for (var root in groups)861
{862
var group = groups[root];863
var height = 0;864
for (var i = 0; i < lines.length; i++)865
{866
var line = lines[i];867
var overlaps = false;868
for (var j = 0; j < line.blocks.length; j++)869
{870
for (var k = 0; k < group.length; k++)871
{872
if (line.blocks[j].id === group[k].id)873
{ overlaps = true; }874
}875
}876
if (overlaps)877
{878
var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical);879
if (h > height)880
{ height = h; }881
}882
}883
this.launchCluster(group, height);884
launched.push({ blocks: group, jumpHeight: height });885
}886
return launched;887
};888
889
/**890
* Gray the matched blocks, take everything sitting on top of them in891
* those columns, link them, and launch with jump height `height`.892
*/893
GameCore.prototype.launchCluster = function (matchedBlocks, height)894
{895
var lowestInCol = Object.create(null);896
var matchedIds = Object.create(null);897
for (var i = 0; i < matchedBlocks.length; i++)898
{899
var block = matchedBlocks[i];900
matchedIds[block.id] = true;901
if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col])902
{ lowestInCol[block.col] = block.y; }903
}904
905
var clusterId = this.nextClusterId++;906
var cluster = {907
id: clusterId,908
jumpHeight: height,909
vy: PlanetMod.launchVelocity(this.planet, height),910
decel: PlanetMod.launchDeceleration(this.planet, height),911
phase: 'up'912
};913
this.clusters[clusterId] = cluster;914
915
var riders = [];916
var cols = Object.keys(lowestInCol);917
for (var c = 0; c < cols.length; c++)918
{919
var col = +cols[c];920
var lowest = lowestInCol[col];921
var list = this.blocksInColumn(col);922
for (var i = 0; i < list.length; i++)923
{924
var block = list[i];925
if (!this.isGrounded(block) && block.state !== STATE_STILL)926
{927
if (block.state !== STATE_SETTLING)928
{ continue; }929
}930
if (block.y + ALIGN_EPS >= lowest)931
{ riders.push(block); }932
}933
}934
935
for (var i = 0; i < riders.length; i++)936
{937
var block = riders[i];938
var isMatch = !!matchedIds[block.id];939
if (isMatch || block.color === COLOR_GRAY)940
{941
block.color = COLOR_GRAY;942
block.flame = true;943
}944
else945
{ block.flame = false; }946
block.state = STATE_FLYING;947
block.clusterId = clusterId;948
block.vy = cluster.vy;949
block.settleTime = 0;950
}951
return cluster;952
};953
954
GameCore.prototype.checkLocks = function ()955
{956
for (var col = 0; col < this.width; col++)957
{958
if (this.isColumnLocked(col))959
{ continue; }960
if (this.groundedCount(col) < this.height)961
{ continue; }962
// Must fill the column from floor to top: 13 grounded blocks at y 0..12.963
var filled = true;964
for (var row = 0; row < this.height; row++)965
{966
var block = this.blockAt(col, row);967
if (!block || !this.isGrounded(block))968
{969
filled = false;970
break;971
}972
}973
if (!filled)974
{ continue; }975
var list = this.blocksInColumn(col);976
for (var i = 0; i < list.length; i++)977
{978
var block = list[i];979
if (!this.isGrounded(block))980
{ continue; }981
block.state = STATE_LOCKED;982
block.color = COLOR_GRAY;983
block.flame = false;984
block.vy = 0;985
block.settleTime = 0;986
if (block.clusterId != null)987
{ this.leaveCluster(block); }988
}989
}990
};991
992
GameCore.prototype.checkGameOver = function ()993
{994
if (this.state !== 'playing')995
{ return; }996
for (var col = 0; col < this.width; col++)997
{998
if (!this.isColumnLocked(col))999
{ return; }1000
}1001
this.state = 'gameover';1002
this.gameOverTime = 0;1003
this.drag = null;1004
};1005
1006
/* ---------- dragging -------------------------------------------------- */1007
1008
GameCore.prototype.beginDrag = function (col, y)1009
{1010
if (this.state !== 'playing')1011
{ return false; }1012
col = col | 0;1013
if (col < 0 || col >= this.width)1014
{ return false; }1015
if (!this.isColumnStill(col))1016
{ return false; }1017
var row = Math.floor(y + 1e-6);1018
if (row < 0)1019
{ return false; }1020
var block = this.blockAt(col, row);1021
if (!block || block.state !== STATE_STILL || block.color < 0)1022
{ return false; }1023
this.drag = { blockId: block.id, pointerY: y, launchIntent: false };1024
return true;1025
};1026
1027
GameCore.prototype.moveDrag = function (y)1028
{1029
if (!this.drag)1030
{ return; }1031
var block = this.blockById(this.drag.blockId);1032
if (!block || block.state !== STATE_STILL)1033
{1034
this.drag = null;1035
return;1036
}1037
if (!this.isColumnStill(block.col))1038
{1039
this.drag = null;1040
return;1041
}1042
this.drag.pointerY = y;1043
this.swapToward(block, y);1044
var top = this.columnStackTopRow(block.col);1045
// The top block occupies [top, top+1). Launch only when the pointer1046
// is pulled into the empty space above the stack.1047
this.drag.launchIntent = (Math.abs(block.y - top) < ALIGN_EPS && y > top + 1 + 0.25);1048
};1049
1050
GameCore.prototype.endDrag = function ()1051
{1052
if (!this.drag)1053
{ return; }1054
var block = this.blockById(this.drag.blockId);1055
var launch = this.drag.launchIntent;1056
this.drag = null;1057
if (launch && block && block.state === STATE_STILL)1058
{ this.startManualLaunch(block); }1059
else1060
{ this.resolveMatches(); }1061
};1062
1063
GameCore.prototype.blockById = function (id)1064
{1065
for (var i = 0; i < this.blocks.length; i++)1066
{1067
if (this.blocks[i].id === id)1068
{ return this.blocks[i]; }1069
}1070
return null;1071
};1072
1073
GameCore.prototype.columnStackTopRow = function (col)1074
{1075
var top = -1;1076
var list = this.blocksInColumn(col);1077
for (var i = 0; i < list.length; i++)1078
{1079
if (this.isGrounded(list[i]) && list[i].y > top)1080
{ top = list[i].y; }1081
}1082
return top;1083
};1084
1085
GameCore.prototype.swapToward = function (block, targetY)1086
{1087
var guard = 0;1088
while (guard++ < this.height + 2)1089
{1090
var current = block.y;1091
if (targetY > current + 0.5)1092
{1093
var above = this.blockAt(block.col, current + 1);1094
if (above && this.isGrounded(above) && above !== block)1095
{ this.swapBlocks(block, above); }1096
else1097
{ break; }1098
}1099
else if (targetY < current - 0.5)1100
{1101
var below = this.blockAt(block.col, current - 1);1102
if (below && this.isGrounded(below) && below !== block)1103
{ this.swapBlocks(block, below); }1104
else1105
{ break; }1106
}1107
else1108
{ break; }1109
}1110
};1111
1112
GameCore.prototype.swapBlocks = function (a, b)1113
{1114
var y = a.y;1115
a.y = b.y;1116
b.y = y;1117
};1118
1119
/**1120
* Test helper: stepwise-swap the block at (col, fromRow) to toRow.1121
* Returns the moved block, or null if the column is not still.1122
*/1123
GameCore.prototype.swapTo = function (col, fromRow, toRow)1124
{1125
if (!this.isColumnStill(col))1126
{ return null; }1127
var block = this.blockAt(col, fromRow);1128
if (!block)1129
{ return null; }1130
this.swapToward(block, toRow);1131
return block;1132
};1133
1134
/**1135
* Test helper: a compact picture of grounded/falling/flying blocks.1136
* Bottom row is index 0. Characters: 0-4 color, g gray flying/settling,1137
* L locked, f falling, m manual, . empty. Flying colored blocks use1138
* their color with a marker in `flying`.1139
*/1140
GameCore.prototype.snapshot = function ()1141
{1142
var grid = [];1143
for (var r = 0; r < this.height; r++)1144
{1145
var row = [];1146
for (var c = 0; c < this.width; c++)1147
{ row.push('.'); }1148
grid.push(row);1149
}1150
var airborne = [];1151
for (var i = 0; i < this.blocks.length; i++)1152
{1153
var block = this.blocks[i];1154
var ch;1155
if (block.state === STATE_LOCKED)1156
{ ch = 'L'; }1157
else if (block.color === COLOR_GRAY)1158
{ ch = 'g'; }1159
else1160
{ ch = String(block.color); }1161
if (this.isGrounded(block) || block.state === STATE_FALLING)1162
{1163
var row = Math.round(block.y);1164
if (row >= 0 && row < this.height)1165
{ grid[row][block.col] = ch; }1166
}1167
else1168
{1169
airborne.push({1170
id: block.id,1171
col: block.col,1172
y: block.y,1173
color: block.color,1174
state: block.state,1175
clusterId: block.clusterId,1176
flame: block.flame,1177
vy: block.vy1178
});1179
}1180
}1181
var lines = [];1182
for (var r = this.height - 1; r >= 0; r--)1183
{ lines.push(grid[r].join('')); }1184
return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state };1185
};1186
1187
var GameCoreAPI = {1188
GameCore: GameCore,1189
COLOR_GRAY: COLOR_GRAY,1190
STATE_STILL: STATE_STILL,1191
STATE_FALLING: STATE_FALLING,1192
STATE_FLYING: STATE_FLYING,1193
STATE_SETTLING: STATE_SETTLING,1194
STATE_LOCKED: STATE_LOCKED,1195
STATE_MANUAL: STATE_MANUAL1196
};1197
1198
if (typeof module !== 'undefined' && module.exports)1199
{ module.exports = GameCoreAPI; }1200
else if (typeof globalThis !== 'undefined')1201
{1202
globalThis.GameCore = GameCore;1203
globalThis.GameCoreAPI = GameCoreAPI;1204
}@@ -0,0 +1,60 @@
1
'use strict';
2
3
const Planet = require('../src/planet.js');
4
const Rng = require('../src/rng.js');
5
const { GameCore } = require('../src/gameCore.js');
6
7
function makeGame (opts)
8
{
9
opts = opts || {};
10
const planet = opts.planet || Planet.createPlanet(opts.planetOpts || {
11
jumpsToClear: 2,
12
fallTime: 3,
13
riseTime: 1.5
14
});
15
const rng = typeof opts.seed === 'number' ? Rng.create(opts.seed) : (opts.rng || Rng.create(1));
16
const game = new GameCore(planet, rng);
17
if (opts.noSpawn)
18
{ game.spawnInterval = 1e9; }
19
return game;
20
}
21
22
/** Fill a column with `count` still blocks of rotating colors starting at 0. */
23
function fillColumn (game, col, count, colors)
24
{
25
colors = colors || [0, 1, 2, 3, 4];
26
const out = [];
27
for (let r = 0; r < count; r++)
28
{ out.push(game.place(col, r, colors[r % colors.length])); }
29
return out;
30
}
31
32
function placeRow (game, row, cols, color)
33
{
34
const out = [];
35
for (const c of cols)
36
{ out.push(game.place(c, row, color)); }
37
return out;
38
}
39
40
function grayFlying (game)
41
{
42
return game.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY);
43
}
44
45
function almost (a, b, eps)
46
{
47
eps = eps == null ? 1e-6 : eps;
48
return Math.abs(a - b) <= eps;
49
}
50
51
module.exports = {
52
makeGame,
53
fillColumn,
54
placeRow,
55
grayFlying,
56
almost,
57
Planet,
58
Rng,
59
GameCore
60
};
@@ -0,0 +1,60 @@1
'use strict';2
3
const Planet = require('../src/planet.js');4
const Rng = require('../src/rng.js');5
const { GameCore } = require('../src/gameCore.js');6
7
function makeGame (opts)8
{9
opts = opts || {};10
const planet = opts.planet || Planet.createPlanet(opts.planetOpts || {11
jumpsToClear: 2,12
fallTime: 3,13
riseTime: 1.514
});15
const rng = typeof opts.seed === 'number' ? Rng.create(opts.seed) : (opts.rng || Rng.create(1));16
const game = new GameCore(planet, rng);17
if (opts.noSpawn)18
{ game.spawnInterval = 1e9; }19
return game;20
}21
22
/** Fill a column with `count` still blocks of rotating colors starting at 0. */23
function fillColumn (game, col, count, colors)24
{25
colors = colors || [0, 1, 2, 3, 4];26
const out = [];27
for (let r = 0; r < count; r++)28
{ out.push(game.place(col, r, colors[r % colors.length])); }29
return out;30
}31
32
function placeRow (game, row, cols, color)33
{34
const out = [];35
for (const c of cols)36
{ out.push(game.place(c, row, color)); }37
return out;38
}39
40
function grayFlying (game)41
{42
return game.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY);43
}44
45
function almost (a, b, eps)46
{47
eps = eps == null ? 1e-6 : eps;48
return Math.abs(a - b) <= eps;49
}50
51
module.exports = {52
makeGame,53
fillColumn,54
placeRow,55
grayFlying,56
almost,57
Planet,58
Rng,59
GameCore60
};@@ -0,0 +1,76 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');
6
7
describe('Column lock and game over', () =>
8
{
9
it('a column filled from floor to top becomes dark gray and locked', () =>
10
{
11
const g = makeGame({ noSpawn: true });
12
fillColumn(g, 3, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]);
13
g.checkLocks();
14
assert.equal(g.isColumnLocked(3), true);
15
assert.equal(g.isColumnStill(3), false);
16
for (let r = 0; r < 13; r++)
17
{
18
const b = g.blockAt(3, r);
19
assert.ok(b);
20
assert.equal(b.state, 'locked');
21
assert.equal(b.color, GameCore.COLOR_GRAY);
22
}
23
assert.equal(g.beginDrag(3, 5), false);
24
});
25
26
it('a new spawn never lands in a locked column', () =>
27
{
28
const g = makeGame({ seed: 11, noSpawn: false });
29
g.spawnInterval = 1e9;
30
fillColumn(g, 0, 13, [0, 1]);
31
g.checkLocks();
32
assert.equal(g.isColumnLocked(0), true);
33
for (let i = 0; i < 40; i++)
34
{
35
const spawned = g.trySpawn();
36
assert.ok(spawned);
37
assert.notEqual(spawned.col, 0);
38
g.removeBlock(spawned);
39
}
40
});
41
42
it('a nearly full column (12 blocks) is not locked', () =>
43
{
44
const g = makeGame({ noSpawn: true });
45
fillColumn(g, 2, 12, [0, 1, 2]);
46
g.checkLocks();
47
assert.equal(g.isColumnLocked(2), false);
48
assert.equal(g.isColumnStill(2), true);
49
assert.ok(g.beginDrag(2, 0));
50
g.endDrag();
51
});
52
53
it('when every column is locked the player loses', () =>
54
{
55
const g = makeGame({ noSpawn: true });
56
for (let c = 0; c < 10; c++)
57
{ fillColumn(g, c, 13, [c % 5, (c + 1) % 5, (c + 2) % 5]); }
58
g.tick(0.01);
59
assert.equal(g.state, 'gameover');
60
assert.equal(g.lockedColumnCount(), 10);
61
assert.equal(g.trySpawn(), null);
62
assert.equal(g.beginDrag(0, 0), false);
63
});
64
65
it('game over does not fire while at least one column is open', () =>
66
{
67
const g = makeGame({ noSpawn: true });
68
for (let c = 0; c < 9; c++)
69
{ fillColumn(g, c, 13, [0, 1, 2]); }
70
fillColumn(g, 9, 4, [3, 4]);
71
g.checkLocks();
72
g.checkGameOver();
73
assert.equal(g.state, 'playing');
74
assert.equal(g.lockedColumnCount(), 9);
75
});
76
});
@@ -0,0 +1,76 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');6
7
describe('Column lock and game over', () =>8
{9
it('a column filled from floor to top becomes dark gray and locked', () =>10
{11
const g = makeGame({ noSpawn: true });12
fillColumn(g, 3, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]);13
g.checkLocks();14
assert.equal(g.isColumnLocked(3), true);15
assert.equal(g.isColumnStill(3), false);16
for (let r = 0; r < 13; r++)17
{18
const b = g.blockAt(3, r);19
assert.ok(b);20
assert.equal(b.state, 'locked');21
assert.equal(b.color, GameCore.COLOR_GRAY);22
}23
assert.equal(g.beginDrag(3, 5), false);24
});25
26
it('a new spawn never lands in a locked column', () =>27
{28
const g = makeGame({ seed: 11, noSpawn: false });29
g.spawnInterval = 1e9;30
fillColumn(g, 0, 13, [0, 1]);31
g.checkLocks();32
assert.equal(g.isColumnLocked(0), true);33
for (let i = 0; i < 40; i++)34
{35
const spawned = g.trySpawn();36
assert.ok(spawned);37
assert.notEqual(spawned.col, 0);38
g.removeBlock(spawned);39
}40
});41
42
it('a nearly full column (12 blocks) is not locked', () =>43
{44
const g = makeGame({ noSpawn: true });45
fillColumn(g, 2, 12, [0, 1, 2]);46
g.checkLocks();47
assert.equal(g.isColumnLocked(2), false);48
assert.equal(g.isColumnStill(2), true);49
assert.ok(g.beginDrag(2, 0));50
g.endDrag();51
});52
53
it('when every column is locked the player loses', () =>54
{55
const g = makeGame({ noSpawn: true });56
for (let c = 0; c < 10; c++)57
{ fillColumn(g, c, 13, [c % 5, (c + 1) % 5, (c + 2) % 5]); }58
g.tick(0.01);59
assert.equal(g.state, 'gameover');60
assert.equal(g.lockedColumnCount(), 10);61
assert.equal(g.trySpawn(), null);62
assert.equal(g.beginDrag(0, 0), false);63
});64
65
it('game over does not fire while at least one column is open', () =>66
{67
const g = makeGame({ noSpawn: true });68
for (let c = 0; c < 9; c++)69
{ fillColumn(g, c, 13, [0, 1, 2]); }70
fillColumn(g, 9, 4, [3, 4]);71
g.checkLocks();72
g.checkGameOver();73
assert.equal(g.state, 'playing');74
assert.equal(g.lockedColumnCount(), 9);75
});76
});@@ -0,0 +1,168 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, placeRow, fillColumn, Planet, GameCore } = require('./helpers.js');
6
7
describe('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
147
describe('Launch physics', () =>
148
{
149
it('reaches the jump height in riseTime, then floats down at constant speed', () =>
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
assert.equal(g.clusters[g.blocks[0].clusterId].phase, 'down');
161
const y1 = g.blocks[0].y;
162
g.step(0.2, 1 / 120);
163
const y2 = g.blocks[0].y;
164
const speed = (y1 - y2) / 0.2;
165
assert.ok(Math.abs(speed - g.planet.fallSpeed) < 0.15, 'fall speed ' + speed);
166
assert.ok(y2 < y1, 'must be going down');
167
});
168
});
@@ -0,0 +1,168 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, placeRow, fillColumn, Planet, GameCore } = require('./helpers.js');6
7
describe('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
147
describe('Launch physics', () =>148
{149
it('reaches the jump height in riseTime, then floats down at constant speed', () =>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
assert.equal(g.clusters[g.blocks[0].clusterId].phase, 'down');161
const y1 = g.blocks[0].y;162
g.step(0.2, 1 / 120);163
const y2 = g.blocks[0].y;164
const speed = (y1 - y2) / 0.2;165
assert.ok(Math.abs(speed - g.planet.fallSpeed) < 0.15, 'fall speed ' + speed);166
assert.ok(y2 < y1, 'must be going down');167
});168
});@@ -0,0 +1,101 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, placeRow, GameCore, Planet } = require('./helpers.js');
6
7
describe('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 land as still blocks and do not wait 3 seconds', () =>
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
g.step(4, 1 / 120);
76
assert.equal(carried.color, 4);
77
assert.equal(carried.state, 'still');
78
assert.equal(carried.clusterId, null);
79
});
80
81
it('spawn drops a random color into a random open column every second', () =>
82
{
83
const g = makeGame({ seed: 3 });
84
assert.equal(g.blocks.length, 0);
85
g.step(0.99);
86
assert.equal(g.blocks.length, 0);
87
g.step(0.02);
88
assert.equal(g.blocks.length, 1);
89
assert.equal(g.blocks[0].state, 'falling');
90
assert.ok(g.blocks[0].color >= 0 && g.blocks[0].color < 5);
91
assert.ok(g.blocks[0].col >= 0 && g.blocks[0].col < 10);
92
g.step(1);
93
assert.equal(g.blocks.length, 2);
94
// Let the first one land.
95
g.step(g.planet.fallTime + 0.2);
96
const landed = g.blocks.filter((b) => b.state === 'still');
97
assert.ok(landed.length >= 1);
98
for (const b of landed)
99
{ assert.equal(b.y, 0); }
100
});
101
});
@@ -0,0 +1,101 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, placeRow, GameCore, Planet } = require('./helpers.js');6
7
describe('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: 9932
});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 land as still blocks and do not wait 3 seconds', () =>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
g.step(4, 1 / 120);76
assert.equal(carried.color, 4);77
assert.equal(carried.state, 'still');78
assert.equal(carried.clusterId, null);79
});80
81
it('spawn drops a random color into a random open column every second', () =>82
{83
const g = makeGame({ seed: 3 });84
assert.equal(g.blocks.length, 0);85
g.step(0.99);86
assert.equal(g.blocks.length, 0);87
g.step(0.02);88
assert.equal(g.blocks.length, 1);89
assert.equal(g.blocks[0].state, 'falling');90
assert.ok(g.blocks[0].color >= 0 && g.blocks[0].color < 5);91
assert.ok(g.blocks[0].col >= 0 && g.blocks[0].col < 10);92
g.step(1);93
assert.equal(g.blocks.length, 2);94
// Let the first one land.95
g.step(g.planet.fallTime + 0.2);96
const landed = g.blocks.filter((b) => b.state === 'still');97
assert.ok(landed.length >= 1);98
for (const b of landed)99
{ assert.equal(b.y, 0); }100
});101
});@@ -0,0 +1,157 @@
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
});
@@ -0,0 +1,157 @@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_GRAY80
});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: 1127
});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
});@@ -0,0 +1,119 @@
1
'use strict';
2
3
const { describe, it } = require('node:test');
4
const assert = require('node:assert/strict');
5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');
6
7
describe('Vertical swapping', () =>
8
{
9
it('dragging a block through a column swaps it with the blocks it passes', () =>
10
{
11
const g = makeGame({ noSpawn: true });
12
const a = g.place(0, 0, 0);
13
const b = g.place(0, 1, 1);
14
const c = g.place(0, 2, 2);
15
assert.ok(g.beginDrag(0, 0));
16
g.moveDrag(2);
17
g.endDrag();
18
assert.equal(a.y, 2);
19
assert.equal(b.y, 0);
20
assert.equal(c.y, 1);
21
assert.equal(g.blockAt(0, 2).color, 0);
22
assert.equal(g.blockAt(0, 0).color, 1);
23
});
24
25
it('swapTo moves a block stepwise and exchanges neighbors', () =>
26
{
27
const g = makeGame({ noSpawn: true });
28
g.place(1, 0, 4);
29
g.place(1, 1, 3);
30
g.place(1, 2, 2);
31
g.place(1, 3, 1);
32
const moved = g.swapTo(1, 0, 3);
33
assert.equal(moved.color, 4);
34
assert.equal(moved.y, 3);
35
assert.equal(g.blockAt(1, 0).color, 3);
36
assert.equal(g.blockAt(1, 1).color, 2);
37
assert.equal(g.blockAt(1, 2).color, 1);
38
});
39
40
it('refuses to drag in a locked or flying column', () =>
41
{
42
const g = makeGame({ noSpawn: true });
43
fillColumn(g, 0, 13, [0]);
44
g.checkLocks();
45
assert.equal(g.isColumnLocked(0), true);
46
assert.equal(g.beginDrag(0, 0), false);
47
48
const g2 = makeGame({ noSpawn: true });
49
g2.place(2, 0, 1);
50
g2.place(3, 0, 1);
51
g2.place(4, 0, 1);
52
g2.place(2, 1, 2);
53
g2.resolveMatches();
54
assert.equal(g2.isColumnStill(2), false);
55
assert.equal(g2.beginDrag(2, 1), false);
56
// A quiet neighboring column is still draggable.
57
g2.place(7, 0, 3);
58
g2.place(7, 1, 4);
59
assert.equal(g2.isColumnStill(7), true);
60
assert.ok(g2.beginDrag(7, 0));
61
g2.endDrag();
62
});
63
64
it('pulling a top block further up starts a manual launch that cannot leave the field', () =>
65
{
66
const g = makeGame({ noSpawn: true });
67
g.place(0, 0, 1);
68
g.place(0, 1, 2);
69
g.place(0, 2, 3);
70
assert.ok(g.beginDrag(0, 2));
71
g.moveDrag(2 + 1.5);
72
assert.equal(g.drag.launchIntent, true);
73
g.endDrag();
74
const block = g.blocks.find((b) => b.color === 3);
75
assert.equal(block.state, 'manual');
76
assert.equal(block.manualPhase, 'up');
77
assert.ok(block.vy > 0);
78
g.step(g.planet.riseTime, 1 / 240);
79
assert.ok(Math.abs(block.y - (g.height - 1)) < 0.05, 'apex should be just under the top, y=' + block.y);
80
assert.ok(block.y <= g.height - 1 + 1e-9, 'y=' + block.y);
81
assert.ok(block.y < g.height, 'must not leave the field');
82
assert.ok(block.manualPhase === 'down' || block.state === 'still' || Math.abs(block.y - (g.height - 1)) < 0.05);
83
g.step(0.5, 1 / 120);
84
assert.ok(block.y < g.height - 1 - 0.01 || block.state === 'still', 'must come back down, y=' + block.y + ' state=' + block.state);
85
assert.ok(block.y < g.height);
86
});
87
88
it('a manual jump from mid-stack still caps at the top cell', () =>
89
{
90
const g = makeGame({ noSpawn: true });
91
g.place(5, 0, 0);
92
g.place(5, 1, 1);
93
const top = g.place(5, 2, 2);
94
g.startManualLaunch(top);
95
g.step(4, 1 / 120);
96
assert.ok(top.y <= g.height - 1 + 1e-9);
97
assert.ok(top.y < g.height);
98
// After coming back down it lands on the remaining stack (y=1, since it left).
99
assert.ok(top.state === 'still' || top.state === 'manual');
100
if (top.state === 'still')
101
{ assert.equal(top.y, 2); }
102
});
103
104
it('a click in the lower half of a cell grabs that cell, not the one above', () =>
105
{
106
const g = makeGame({ noSpawn: true });
107
g.place(0, 0, 0);
108
g.place(0, 1, 1);
109
assert.ok(g.beginDrag(0, 0.4));
110
assert.equal(g.blockById(g.drag.blockId).color, 0);
111
g.endDrag();
112
assert.ok(g.beginDrag(0, 0.9));
113
assert.equal(g.blockById(g.drag.blockId).color, 0);
114
g.endDrag();
115
assert.ok(g.beginDrag(0, 1.1));
116
assert.equal(g.blockById(g.drag.blockId).color, 1);
117
g.endDrag();
118
});
119
});
@@ -0,0 +1,119 @@1
'use strict';2
3
const { describe, it } = require('node:test');4
const assert = require('node:assert/strict');5
const { makeGame, fillColumn, GameCore } = require('./helpers.js');6
7
describe('Vertical swapping', () =>8
{9
it('dragging a block through a column swaps it with the blocks it passes', () =>10
{11
const g = makeGame({ noSpawn: true });12
const a = g.place(0, 0, 0);13
const b = g.place(0, 1, 1);14
const c = g.place(0, 2, 2);15
assert.ok(g.beginDrag(0, 0));16
g.moveDrag(2);17
g.endDrag();18
assert.equal(a.y, 2);19
assert.equal(b.y, 0);20
assert.equal(c.y, 1);21
assert.equal(g.blockAt(0, 2).color, 0);22
assert.equal(g.blockAt(0, 0).color, 1);23
});24
25
it('swapTo moves a block stepwise and exchanges neighbors', () =>26
{27
const g = makeGame({ noSpawn: true });28
g.place(1, 0, 4);29
g.place(1, 1, 3);30
g.place(1, 2, 2);31
g.place(1, 3, 1);32
const moved = g.swapTo(1, 0, 3);33
assert.equal(moved.color, 4);34
assert.equal(moved.y, 3);35
assert.equal(g.blockAt(1, 0).color, 3);36
assert.equal(g.blockAt(1, 1).color, 2);37
assert.equal(g.blockAt(1, 2).color, 1);38
});39
40
it('refuses to drag in a locked or flying column', () =>41
{42
const g = makeGame({ noSpawn: true });43
fillColumn(g, 0, 13, [0]);44
g.checkLocks();45
assert.equal(g.isColumnLocked(0), true);46
assert.equal(g.beginDrag(0, 0), false);47
48
const g2 = makeGame({ noSpawn: true });49
g2.place(2, 0, 1);50
g2.place(3, 0, 1);51
g2.place(4, 0, 1);52
g2.place(2, 1, 2);53
g2.resolveMatches();54
assert.equal(g2.isColumnStill(2), false);55
assert.equal(g2.beginDrag(2, 1), false);56
// A quiet neighboring column is still draggable.57
g2.place(7, 0, 3);58
g2.place(7, 1, 4);59
assert.equal(g2.isColumnStill(7), true);60
assert.ok(g2.beginDrag(7, 0));61
g2.endDrag();62
});63
64
it('pulling a top block further up starts a manual launch that cannot leave the field', () =>65
{66
const g = makeGame({ noSpawn: true });67
g.place(0, 0, 1);68
g.place(0, 1, 2);69
g.place(0, 2, 3);70
assert.ok(g.beginDrag(0, 2));71
g.moveDrag(2 + 1.5);72
assert.equal(g.drag.launchIntent, true);73
g.endDrag();74
const block = g.blocks.find((b) => b.color === 3);75
assert.equal(block.state, 'manual');76
assert.equal(block.manualPhase, 'up');77
assert.ok(block.vy > 0);78
g.step(g.planet.riseTime, 1 / 240);79
assert.ok(Math.abs(block.y - (g.height - 1)) < 0.05, 'apex should be just under the top, y=' + block.y);80
assert.ok(block.y <= g.height - 1 + 1e-9, 'y=' + block.y);81
assert.ok(block.y < g.height, 'must not leave the field');82
assert.ok(block.manualPhase === 'down' || block.state === 'still' || Math.abs(block.y - (g.height - 1)) < 0.05);83
g.step(0.5, 1 / 120);84
assert.ok(block.y < g.height - 1 - 0.01 || block.state === 'still', 'must come back down, y=' + block.y + ' state=' + block.state);85
assert.ok(block.y < g.height);86
});87
88
it('a manual jump from mid-stack still caps at the top cell', () =>89
{90
const g = makeGame({ noSpawn: true });91
g.place(5, 0, 0);92
g.place(5, 1, 1);93
const top = g.place(5, 2, 2);94
g.startManualLaunch(top);95
g.step(4, 1 / 120);96
assert.ok(top.y <= g.height - 1 + 1e-9);97
assert.ok(top.y < g.height);98
// After coming back down it lands on the remaining stack (y=1, since it left).99
assert.ok(top.state === 'still' || top.state === 'manual');100
if (top.state === 'still')101
{ assert.equal(top.y, 2); }102
});103
104
it('a click in the lower half of a cell grabs that cell, not the one above', () =>105
{106
const g = makeGame({ noSpawn: true });107
g.place(0, 0, 0);108
g.place(0, 1, 1);109
assert.ok(g.beginDrag(0, 0.4));110
assert.equal(g.blockById(g.drag.blockId).color, 0);111
g.endDrag();112
assert.ok(g.beginDrag(0, 0.9));113
assert.equal(g.blockById(g.drag.blockId).color, 0);114
g.endDrag();115
assert.ok(g.beginDrag(0, 1.1));116
assert.equal(g.blockById(g.drag.blockId).color, 1);117
g.endDrag();118
});119
});