BlueGit

projects-timfalken-com meteors

feat: attach falling cargo, rigid grey land, apex ease, top launch

Falling blocks land on a flying cluster and ride with it. Grey clusters
keep relative positions when they land, hanging over gaps until they
recolor, then fall individually. Jump descent eases into fallSpeed.
The already-topmost block launches after a short upward pull. Landing
sets a visual bounce flag whose duration is a planet parameter.

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
6e9fb96e6b88c20db90e9b5a5126b9c856e0b5d0
Parent
73408215
5 files changed +763 −64
src/gameCore.js +426 −60
@@ -22,6 +22,8 @@ var STATE_MANUAL = 'manual';
22 22 var ALIGN_EPS = 0.15;
23 23 var SUBSTEP = 1 / 120;
24 24 var DRAG_LAUNCH_SLACK = 0.65;
25 // Upward pull (tiles past the press point) that launches an already-topmost block.
26 var LAUNCH_PULL = 0.35;
25 27
26 28 function UnionFind ()
27 29 {
@@ -71,6 +73,7 @@ function GameCore (planet, rng)
71 73 }
72 74
73 75 GameCore.COLOR_GRAY = COLOR_GRAY;
76 GameCore.LAUNCH_PULL = LAUNCH_PULL;
74 77 GameCore.STATE_STILL = STATE_STILL;
75 78 GameCore.STATE_FALLING = STATE_FALLING;
76 79 GameCore.STATE_FLYING = STATE_FLYING;
@@ -98,7 +101,9 @@ GameCore.prototype.createBlock = function (col, y, color, state)
98 101 pushedClusters: null,
99 102 manualTarget: 0,
100 103 manualDecel: 0,
101 manualPhase: null
104 manualPhase: null,
105 bouncing: false,
106 bounceAge: 0
102 107 };
103 108 this.blocks.push(block);
104 109 return block;
@@ -236,6 +241,199 @@ GameCore.prototype.lockedColumnCount = function ()
236 241 return n;
237 242 };
238 243
244 GameCore.prototype.startBounce = function (block)
245 {
246 if (!block)
247 { return; }
248 block.bouncing = true;
249 block.bounceAge = 0;
250 };
251
252 GameCore.prototype.advanceBounces = function (dt)
253 {
254 var duration = this.planet.bounceDuration;
255 for (var i = 0; i < this.blocks.length; i++)
256 {
257 var block = this.blocks[i];
258 if (!block.bouncing)
259 { continue; }
260 block.bounceAge += dt;
261 if (duration <= 0 || block.bounceAge >= duration)
262 {
263 block.bouncing = false;
264 block.bounceAge = duration;
265 }
266 }
267 };
268
269 /**
270 * True if `block` rests on the floor or on a solid (still / settling /
271 * locked) block immediately below it in the same column.
272 */
273 GameCore.prototype.hasSupport = function (block)
274 {
275 if (block.y <= 1e-9)
276 { return true; }
277 for (var i = 0; i < this.blocks.length; i++)
278 {
279 var other = this.blocks[i];
280 if (other === block || other.col !== block.col)
281 { continue; }
282 if (!this.isGrounded(other))
283 { continue; }
284 if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
285 { return true; }
286 }
287 return false;
288 };
289
290 /** Highest flying block in this column whose top is at or below `block.y + 1`. */
291 GameCore.prototype.highestFlyingBelow = function (block)
292 {
293 var best = null;
294 for (var i = 0; i < this.blocks.length; i++)
295 {
296 var other = this.blocks[i];
297 if (other === block || other.col !== block.col)
298 { continue; }
299 if (other.state !== STATE_FLYING || other.clusterId == null)
300 { continue; }
301 var cluster = this.clusters[other.clusterId];
302 if (!cluster || cluster.phase === 'landed')
303 { continue; }
304 // The falling block is at or above this flyer (or already overlapping).
305 if (block.y + 1e-9 < other.y)
306 { continue; }
307 if (!best || other.y > best.y)
308 { best = other; }
309 }
310 return best;
311 };
312
313 GameCore.prototype.highestFallingBelow = function (block)
314 {
315 var best = null;
316 for (var i = 0; i < this.blocks.length; i++)
317 {
318 var other = this.blocks[i];
319 if (other === block || other.col !== block.col)
320 { continue; }
321 if (other.state !== STATE_FALLING)
322 { continue; }
323 if (other.y > block.y - 1e-9)
324 { continue; }
325 if (!best || other.y > best.y)
326 { best = other; }
327 }
328 return best;
329 };
330
331 /**
332 * Make an unsupported still block start falling from rest, so it eases
333 * into fallSpeed under normal gravity. Skips the block being dragged.
334 */
335 GameCore.prototype.releaseUnsupported = function ()
336 {
337 for (var i = 0; i < this.blocks.length; i++)
338 {
339 var block = this.blocks[i];
340 if (block.state !== STATE_STILL)
341 { continue; }
342 if (this.drag && this.drag.blockId === block.id)
343 { continue; }
344 if (this.hasSupport(block))
345 { continue; }
346 block.state = STATE_FALLING;
347 block.vy = 0;
348 block.flame = false;
349 this.leaveCluster(block);
350 }
351 };
352
353 GameCore.prototype.accelerateFall = function (block, dt)
354 {
355 var target = -this.planet.fallSpeed;
356 if (block.vy > target)
357 { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); }
358 else
359 { block.vy = target; }
360 };
361
362 GameCore.prototype.landFallingBlock = function (block)
363 {
364 block.vy = 0;
365 block.flame = false;
366 this.startBounce(block);
367 if (block.color === COLOR_GRAY)
368 {
369 block.state = STATE_SETTLING;
370 block.settleTime = 0;
371 }
372 else
373 { block.state = STATE_STILL; }
374 };
375
376 /**
377 * Sit `block` on top of the flying cluster that `flyer` belongs to, and
378 * carry it along as cargo (no flame). Returns true if attached.
379 */
380 GameCore.prototype.attachToFlyingCluster = function (block, flyer)
381 {
382 var cluster = this.clusters[flyer.clusterId];
383 if (!cluster || cluster.phase === 'landed')
384 { return false; }
385 var roof = flyer.y + 1;
386 for (var i = 0; i < this.blocks.length; i++)
387 {
388 var other = this.blocks[i];
389 if (other === block || other.col !== block.col)
390 { continue; }
391 if (other.clusterId !== cluster.id)
392 { continue; }
393 if (other.y + 1 > roof)
394 { roof = other.y + 1; }
395 }
396 block.y = roof;
397 block.state = STATE_FLYING;
398 block.clusterId = cluster.id;
399 block.vy = cluster.vy;
400 block.flame = false;
401 block.settleTime = 0;
402 return true;
403 };
404
405 GameCore.prototype.catchFallingOntoCluster = function (cluster, members)
406 {
407 if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
408 { return; }
409 // Snapshot falling blocks; attaching mutates state.
410 var falling = [];
411 for (var i = 0; i < this.blocks.length; i++)
412 {
413 if (this.blocks[i].state === STATE_FALLING)
414 { falling.push(this.blocks[i]); }
415 }
416 for (var i = 0; i < falling.length; i++)
417 {
418 var block = falling[i];
419 var flyer = null;
420 for (var m = 0; m < members.length; m++)
421 {
422 var other = members[m];
423 if (other.col !== block.col)
424 { continue; }
425 // Overlap or rest on this member's top.
426 if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9)
427 {
428 if (!flyer || other.y > flyer.y)
429 { flyer = other; }
430 }
431 }
432 if (flyer)
433 { this.attachToFlyingCluster(block, flyer); }
434 }
435 };
436
239 437 GameCore.prototype.tick = function (dt)
240 438 {
241 439 if (this.state === 'gameover')
@@ -265,7 +463,9 @@ GameCore.prototype.tick = function (dt)
265 463 left -= step;
266 464 }
267 465
466 this.releaseUnsupported();
268 467 this.advanceSettle(dt);
468 this.advanceBounces(dt);
269 469 if (!this.drag)
270 470 { this.resolveMatches(); }
271 471 this.checkLocks();
@@ -313,24 +513,42 @@ GameCore.prototype.trySpawn = function ()
313 513 { return null; }
314 514 var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
315 515 var color = this.randomColor();
316 return this.createBlock(col, this.height, color, STATE_FALLING);
516 var block = this.createBlock(col, this.height, color, STATE_FALLING);
517 block.vy = -this.planet.fallSpeed;
518 return block;
317 519 };
318 520
319 521 GameCore.prototype.integrateFalling = function (dt)
320 522 {
321 var speed = this.planet.fallSpeed;
523 var falling = [];
322 524 for (var i = 0; i < this.blocks.length; i++)
323 525 {
324 var block = this.blocks[i];
526 if (this.blocks[i].state === STATE_FALLING)
527 { falling.push(this.blocks[i]); }
528 }
529 // Lowest first, so a stack of falling blocks rests on itself.
530 falling.sort(function (a, b) { return a.y - b.y; });
531 for (var i = 0; i < falling.length; i++)
532 {
533 var block = falling[i];
325 534 if (block.state !== STATE_FALLING)
326 535 { continue; }
536 this.accelerateFall(block, dt);
537 var nextY = block.y + block.vy * dt;
538 var flyer = this.highestFlyingBelow(block);
539 if (flyer && nextY <= flyer.y + 1 + 1e-9)
540 {
541 if (this.attachToFlyingCluster(block, flyer))
542 { continue; }
543 }
327 544 var top = this.stackTop(block.col);
328 var nextY = block.y - speed * dt;
545 var below = this.highestFallingBelow(block);
546 if (below && below.y + 1 > top)
547 { top = below.y + 1; }
329 548 if (nextY <= top)
330 549 {
331 550 block.y = top;
332 block.state = STATE_STILL;
333 block.vy = 0;
551 this.landFallingBlock(block);
334 552 }
335 553 else
336 554 { block.y = nextY; }
@@ -353,7 +571,8 @@ GameCore.prototype.clusterMembers = function (clusterId)
353 571 * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a
354 572 * jump of height H peaks at exactly H after riseTime. Returns dy.
355 573 * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the
356 * upward deceleration; falling uses planet.fallSpeed.
574 * upward deceleration. After the apex, vy eases from 0 to -fallSpeed
575 * at planet.fallAccel, then stays at terminal fallSpeed.
357 576 */
358 577 GameCore.prototype.advanceJump = function (body, dt)
359 578 {
@@ -386,8 +605,35 @@ GameCore.prototype.advanceJump = function (body, dt)
386 605 }
387 606 if (body.phase === 'down' && left > 0)
388 607 {
389 body.vy = -this.planet.fallSpeed;
390 dy += body.vy * left;
608 var target = -this.planet.fallSpeed;
609 var accel = this.planet.fallAccel;
610 if (accel < 1e-12)
611 {
612 body.vy = target;
613 dy += body.vy * left;
614 }
615 else if (body.vy > target + 1e-12)
616 {
617 var tReach = (body.vy - target) / accel;
618 if (tReach <= left + 1e-15)
619 {
620 dy += (body.vy + target) * 0.5 * tReach;
621 body.vy = target;
622 left -= tReach;
623 dy += body.vy * left;
624 }
625 else
626 {
627 var vyNew = body.vy - accel * left;
628 dy += (body.vy + vyNew) * 0.5 * left;
629 body.vy = vyNew;
630 }
631 }
632 else
633 {
634 body.vy = target;
635 dy += body.vy * left;
636 }
391 637 }
392 638 return dy;
393 639 };
@@ -416,14 +662,18 @@ GameCore.prototype.integrateClusters = function (dt)
416 662 members[i].vy = cluster.vy;
417 663 }
418 664
665 this.catchFallingOntoCluster(cluster, members);
666
419 667 if (cluster.phase === 'down')
420 { this.tryLandCluster(cluster, members); }
668 { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }
421 669 }
422 670 };
423 671
424 672 GameCore.prototype.tryLandCluster = function (cluster, members)
425 673 {
426 var self = this;
674 if (!members.length)
675 { return; }
676 var shift = 0;
427 677 var hit = false;
428 678 for (var i = 0; i < members.length; i++)
429 679 {
@@ -433,63 +683,109 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
433 683 return other.clusterId === cluster.id;
434 684 });
435 685 if (block.y <= top + 1e-9)
436 { hit = true; }
686 {
687 hit = true;
688 var need = top - block.y;
689 if (need > shift)
690 { shift = need; }
691 }
437 692 }
438 693 if (!hit)
439 694 { return; }
695 // Lift the whole rigid body so the deepest contact sits flush.
696 for (var i = 0; i < members.length; i++)
697 { members[i].y += shift; }
440 698 this.landCluster(cluster, members);
441 699 };
442 700
701 /**
702 * Land as a rigid group: relative positions are kept, so part of the
703 * cluster may hang above a gap. Grey members start the 3s settle timer;
704 * coloured cargo stays put with them until every grey has recolored.
705 * Members that actually rest on the floor or on another block bounce.
706 */
443 707 GameCore.prototype.landCluster = function (cluster, members)
444 708 {
445 var byCol = Object.create(null);
446 709 for (var i = 0; i < members.length; i++)
447 710 {
448 711 var block = members[i];
449 if (!byCol[block.col])
450 { byCol[block.col] = []; }
451 byCol[block.col].push(block);
712 block.vy = 0;
713 block.flame = false;
714 block.clusterId = cluster.id;
715 if (block.color === COLOR_GRAY)
716 {
717 block.state = STATE_SETTLING;
718 block.settleTime = 0;
719 }
720 else
721 {
722 // Cargo stays in the rigid group (not matchable) until release.
723 block.state = STATE_SETTLING;
724 block.settleTime = 0;
725 }
726 if (this.hasSupport(block))
727 { this.startBounce(block); }
452 728 }
453 var cols = Object.keys(byCol);
454 for (var c = 0; c < cols.length; c++)
729 cluster.phase = 'landed';
730 cluster.vy = 0;
731 };
732
733 /**
734 * After every grey in a landed cluster has recolored, let the group go.
735 * Unsupported blocks fall individually from rest; supported ones stay.
736 */
737 GameCore.prototype.releaseLandedCluster = function (cluster)
738 {
739 var members = this.clusterMembers(cluster.id);
740 var releasing = Object.create(null);
741 for (var i = 0; i < members.length; i++)
742 { releasing[members[i].id] = true; }
743
744 for (var i = 0; i < members.length; i++)
455 745 {
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)
746 var block = members[i];
747 if (block.color === COLOR_GRAY)
461 748 {
462 return other.clusterId === cluster.id;
463 });
464 for (var i = 0; i < list.length; i++)
749 block.color = this.randomColor();
750 block.settleTime = 0;
751 }
752 block.flame = false;
753 this.leaveCluster(block);
754 // Support from outside the group (floor or a foreign block). Fellow
755 // members that will also fall do not count, so a hanging stack
756 // drops together without the top one staying put.
757 var supported = this.hasSupportOutside(block, releasing);
758 if (supported)
465 759 {
466 var block = list[i];
467 block.y = top;
760 block.state = STATE_STILL;
761 block.vy = 0;
762 }
763 else
764 {
765 block.state = STATE_FALLING;
468 766 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 767 }
482 768 }
483 cluster.phase = 'landed';
484 cluster.vy = 0;
485 var remaining = 0;
769 delete this.clusters[cluster.id];
770 };
771
772 GameCore.prototype.hasSupportOutside = function (block, ignoreIds)
773 {
774 if (block.y <= 1e-9)
775 { return true; }
486 776 for (var i = 0; i < this.blocks.length; i++)
487 777 {
488 if (this.blocks[i].clusterId === cluster.id)
489 { remaining++; }
778 var other = this.blocks[i];
779 if (other === block || other.col !== block.col)
780 { continue; }
781 if (ignoreIds && ignoreIds[other.id])
782 { continue; }
783 if (!this.isGrounded(other))
784 { continue; }
785 if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
786 { return true; }
490 787 }
491 if (remaining === 0)
492 { delete this.clusters[cluster.id]; }
788 return false;
493 789 };
494 790
495 791 GameCore.prototype.integrateManual = function (dt)
@@ -526,8 +822,11 @@ GameCore.prototype.integrateManual = function (dt)
526 822
527 823 if (block.manualPhase === 'down')
528 824 {
825 var proxy = { phase: 'down', vy: block.vy, decel: 0 };
826 var dy = this.advanceJump(proxy, dt);
827 block.vy = proxy.vy;
828 var nextY = block.y + dy;
529 829 var top = this.stackTop(block.col);
530 var nextY = block.y - this.planet.fallSpeed * dt;
531 830 if (nextY <= top)
532 831 {
533 832 block.y = top;
@@ -535,11 +834,11 @@ GameCore.prototype.integrateManual = function (dt)
535 834 block.vy = 0;
536 835 block.manualPhase = null;
537 836 block.flame = false;
837 this.startBounce(block);
538 838 }
539 839 else
540 840 {
541 841 block.y = Math.min(nextY, this.height - 1);
542 block.vy = -this.planet.fallSpeed;
543 842 }
544 843 }
545 844 }
@@ -698,15 +997,64 @@ GameCore.prototype.advanceSettle = function (dt)
698 997 var block = this.blocks[i];
699 998 if (block.state !== STATE_SETTLING)
700 999 { continue; }
701 block.settleTime += dt;
702 if (block.settleTime >= this.settleTime)
1000 var inLanded = block.clusterId != null
1001 && this.clusters[block.clusterId]
1002 && this.clusters[block.clusterId].phase === 'landed';
1003 if (block.color === COLOR_GRAY)
703 1004 {
704 block.color = this.randomColor();
705 block.state = STATE_STILL;
706 block.settleTime = 0;
707 block.flame = false;
1005 block.settleTime += dt;
1006 if (block.settleTime >= this.settleTime)
1007 {
1008 block.color = this.randomColor();
1009 block.settleTime = 0;
1010 block.flame = false;
1011 if (inLanded)
1012 {
1013 // Stay in the rigid group; releaseLandedCluster runs below.
1014 block.state = STATE_SETTLING;
1015 }
1016 else
1017 {
1018 this.leaveCluster(block);
1019 if (this.hasSupport(block))
1020 {
1021 block.state = STATE_STILL;
1022 block.vy = 0;
1023 }
1024 else
1025 {
1026 block.state = STATE_FALLING;
1027 block.vy = 0;
1028 }
1029 }
1030 }
1031 }
1032 else if (!inLanded)
1033 {
1034 // Coloured settling that is not in a landed cluster (shouldn't
1035 // normally happen): just become still or fall.
708 1036 this.leaveCluster(block);
1037 block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;
1038 if (block.state === STATE_FALLING)
1039 { block.vy = 0; }
1040 }
1041 }
1042
1043 var ids = Object.keys(this.clusters);
1044 for (var n = 0; n < ids.length; n++)
1045 {
1046 var cluster = this.clusters[ids[n]];
1047 if (!cluster || cluster.phase !== 'landed')
1048 { continue; }
1049 var members = this.clusterMembers(cluster.id);
1050 var greySettling = false;
1051 for (var i = 0; i < members.length; i++)
1052 {
1053 if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING)
1054 { greySettling = true; }
709 1055 }
1056 if (!greySettling)
1057 { this.releaseLandedCluster(cluster); }
710 1058 }
711 1059 };
712 1060
@@ -1020,7 +1368,15 @@ GameCore.prototype.beginDrag = function (col, y)
1020 1368 var block = this.blockAt(col, row);
1021 1369 if (!block || block.state !== STATE_STILL || block.color < 0)
1022 1370 { return false; }
1023 this.drag = { blockId: block.id, pointerY: y, launchIntent: false };
1371 var top = this.columnStackTopRow(col);
1372 var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS;
1373 this.drag = {
1374 blockId: block.id,
1375 pointerY: y,
1376 startY: y,
1377 launchIntent: false,
1378 startedAsTop: startedAsTop
1379 };
1024 1380 return true;
1025 1381 };
1026 1382
@@ -1042,9 +1398,19 @@ GameCore.prototype.moveDrag = function (y)
1042 1398 this.drag.pointerY = y;
1043 1399 this.swapToward(block, y);
1044 1400 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);
1401 var isTop = Math.abs(block.y - top) < ALIGN_EPS;
1402 if (this.drag.startedAsTop)
1403 {
1404 // Already the topmost block: pulling upward past its own position
1405 // (LAUNCH_PULL tiles above the press point) starts a manual jump.
1406 this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL);
1407 }
1408 else
1409 {
1410 // Started mid-stack: swap first, and only launch once the pointer
1411 // is pulled into the empty space above the (new) top.
1412 this.drag.launchIntent = isTop && (y > top + 1 + 0.25);
1413 }
1048 1414 };
1049 1415
1050 1416 GameCore.prototype.endDrag = function ()
@@ -22,6 +22,8 @@ var STATE_MANUAL = 'manual';
22var ALIGN_EPS = 0.15;
22var ALIGN_EPS = 0.15;
23var SUBSTEP = 1 / 120;
23var SUBSTEP = 1 / 120;
24var DRAG_LAUNCH_SLACK = 0.65;
24var DRAG_LAUNCH_SLACK = 0.65;
25// Upward pull (tiles past the press point) that launches an already-topmost block.
26var LAUNCH_PULL = 0.35;
25
27
26function UnionFind ()
28function UnionFind ()
27{
29{
@@ -71,6 +73,7 @@ function GameCore (planet, rng)
71}
73}
72
74
73GameCore.COLOR_GRAY = COLOR_GRAY;
75GameCore.COLOR_GRAY = COLOR_GRAY;
76GameCore.LAUNCH_PULL = LAUNCH_PULL;
74GameCore.STATE_STILL = STATE_STILL;
77GameCore.STATE_STILL = STATE_STILL;
75GameCore.STATE_FALLING = STATE_FALLING;
78GameCore.STATE_FALLING = STATE_FALLING;
76GameCore.STATE_FLYING = STATE_FLYING;
79GameCore.STATE_FLYING = STATE_FLYING;
@@ -98,7 +101,9 @@ GameCore.prototype.createBlock = function (col, y, color, state)
98 pushedClusters: null,
101 pushedClusters: null,
99 manualTarget: 0,
102 manualTarget: 0,
100 manualDecel: 0,
103 manualDecel: 0,
101 manualPhase: null
104 manualPhase: null,
105 bouncing: false,
106 bounceAge: 0
102 };
107 };
103 this.blocks.push(block);
108 this.blocks.push(block);
104 return block;
109 return block;
@@ -236,6 +241,199 @@ GameCore.prototype.lockedColumnCount = function ()
236 return n;
241 return n;
237};
242};
238
243
244GameCore.prototype.startBounce = function (block)
245{
246 if (!block)
247 { return; }
248 block.bouncing = true;
249 block.bounceAge = 0;
250};
251
252GameCore.prototype.advanceBounces = function (dt)
253{
254 var duration = this.planet.bounceDuration;
255 for (var i = 0; i < this.blocks.length; i++)
256 {
257 var block = this.blocks[i];
258 if (!block.bouncing)
259 { continue; }
260 block.bounceAge += dt;
261 if (duration <= 0 || block.bounceAge >= duration)
262 {
263 block.bouncing = false;
264 block.bounceAge = duration;
265 }
266 }
267};
268
269/**
270 * True if `block` rests on the floor or on a solid (still / settling /
271 * locked) block immediately below it in the same column.
272 */
273GameCore.prototype.hasSupport = function (block)
274{
275 if (block.y <= 1e-9)
276 { return true; }
277 for (var i = 0; i < this.blocks.length; i++)
278 {
279 var other = this.blocks[i];
280 if (other === block || other.col !== block.col)
281 { continue; }
282 if (!this.isGrounded(other))
283 { continue; }
284 if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
285 { return true; }
286 }
287 return false;
288};
289
290/** Highest flying block in this column whose top is at or below `block.y + 1`. */
291GameCore.prototype.highestFlyingBelow = function (block)
292{
293 var best = null;
294 for (var i = 0; i < this.blocks.length; i++)
295 {
296 var other = this.blocks[i];
297 if (other === block || other.col !== block.col)
298 { continue; }
299 if (other.state !== STATE_FLYING || other.clusterId == null)
300 { continue; }
301 var cluster = this.clusters[other.clusterId];
302 if (!cluster || cluster.phase === 'landed')
303 { continue; }
304 // The falling block is at or above this flyer (or already overlapping).
305 if (block.y + 1e-9 < other.y)
306 { continue; }
307 if (!best || other.y > best.y)
308 { best = other; }
309 }
310 return best;
311};
312
313GameCore.prototype.highestFallingBelow = function (block)
314{
315 var best = null;
316 for (var i = 0; i < this.blocks.length; i++)
317 {
318 var other = this.blocks[i];
319 if (other === block || other.col !== block.col)
320 { continue; }
321 if (other.state !== STATE_FALLING)
322 { continue; }
323 if (other.y > block.y - 1e-9)
324 { continue; }
325 if (!best || other.y > best.y)
326 { best = other; }
327 }
328 return best;
329};
330
331/**
332 * Make an unsupported still block start falling from rest, so it eases
333 * into fallSpeed under normal gravity. Skips the block being dragged.
334 */
335GameCore.prototype.releaseUnsupported = function ()
336{
337 for (var i = 0; i < this.blocks.length; i++)
338 {
339 var block = this.blocks[i];
340 if (block.state !== STATE_STILL)
341 { continue; }
342 if (this.drag && this.drag.blockId === block.id)
343 { continue; }
344 if (this.hasSupport(block))
345 { continue; }
346 block.state = STATE_FALLING;
347 block.vy = 0;
348 block.flame = false;
349 this.leaveCluster(block);
350 }
351};
352
353GameCore.prototype.accelerateFall = function (block, dt)
354{
355 var target = -this.planet.fallSpeed;
356 if (block.vy > target)
357 { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); }
358 else
359 { block.vy = target; }
360};
361
362GameCore.prototype.landFallingBlock = function (block)
363{
364 block.vy = 0;
365 block.flame = false;
366 this.startBounce(block);
367 if (block.color === COLOR_GRAY)
368 {
369 block.state = STATE_SETTLING;
370 block.settleTime = 0;
371 }
372 else
373 { block.state = STATE_STILL; }
374};
375
376/**
377 * Sit `block` on top of the flying cluster that `flyer` belongs to, and
378 * carry it along as cargo (no flame). Returns true if attached.
379 */
380GameCore.prototype.attachToFlyingCluster = function (block, flyer)
381{
382 var cluster = this.clusters[flyer.clusterId];
383 if (!cluster || cluster.phase === 'landed')
384 { return false; }
385 var roof = flyer.y + 1;
386 for (var i = 0; i < this.blocks.length; i++)
387 {
388 var other = this.blocks[i];
389 if (other === block || other.col !== block.col)
390 { continue; }
391 if (other.clusterId !== cluster.id)
392 { continue; }
393 if (other.y + 1 > roof)
394 { roof = other.y + 1; }
395 }
396 block.y = roof;
397 block.state = STATE_FLYING;
398 block.clusterId = cluster.id;
399 block.vy = cluster.vy;
400 block.flame = false;
401 block.settleTime = 0;
402 return true;
403};
404
405GameCore.prototype.catchFallingOntoCluster = function (cluster, members)
406{
407 if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
408 { return; }
409 // Snapshot falling blocks; attaching mutates state.
410 var falling = [];
411 for (var i = 0; i < this.blocks.length; i++)
412 {
413 if (this.blocks[i].state === STATE_FALLING)
414 { falling.push(this.blocks[i]); }
415 }
416 for (var i = 0; i < falling.length; i++)
417 {
418 var block = falling[i];
419 var flyer = null;
420 for (var m = 0; m < members.length; m++)
421 {
422 var other = members[m];
423 if (other.col !== block.col)
424 { continue; }
425 // Overlap or rest on this member's top.
426 if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9)
427 {
428 if (!flyer || other.y > flyer.y)
429 { flyer = other; }
430 }
431 }
432 if (flyer)
433 { this.attachToFlyingCluster(block, flyer); }
434 }
435};
436
239GameCore.prototype.tick = function (dt)
437GameCore.prototype.tick = function (dt)
240{
438{
241 if (this.state === 'gameover')
439 if (this.state === 'gameover')
@@ -265,7 +463,9 @@ GameCore.prototype.tick = function (dt)
265 left -= step;
463 left -= step;
266 }
464 }
267
465
466 this.releaseUnsupported();
268 this.advanceSettle(dt);
467 this.advanceSettle(dt);
468 this.advanceBounces(dt);
269 if (!this.drag)
469 if (!this.drag)
270 { this.resolveMatches(); }
470 { this.resolveMatches(); }
271 this.checkLocks();
471 this.checkLocks();
@@ -313,24 +513,42 @@ GameCore.prototype.trySpawn = function ()
313 { return null; }
513 { return null; }
314 var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
514 var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
315 var color = this.randomColor();
515 var color = this.randomColor();
316 return this.createBlock(col, this.height, color, STATE_FALLING);
516 var block = this.createBlock(col, this.height, color, STATE_FALLING);
517 block.vy = -this.planet.fallSpeed;
518 return block;
317};
519};
318
520
319GameCore.prototype.integrateFalling = function (dt)
521GameCore.prototype.integrateFalling = function (dt)
320{
522{
321 var speed = this.planet.fallSpeed;
523 var falling = [];
322 for (var i = 0; i < this.blocks.length; i++)
524 for (var i = 0; i < this.blocks.length; i++)
323 {
525 {
324 var block = this.blocks[i];
526 if (this.blocks[i].state === STATE_FALLING)
527 { falling.push(this.blocks[i]); }
528 }
529 // Lowest first, so a stack of falling blocks rests on itself.
530 falling.sort(function (a, b) { return a.y - b.y; });
531 for (var i = 0; i < falling.length; i++)
532 {
533 var block = falling[i];
325 if (block.state !== STATE_FALLING)
534 if (block.state !== STATE_FALLING)
326 { continue; }
535 { continue; }
536 this.accelerateFall(block, dt);
537 var nextY = block.y + block.vy * dt;
538 var flyer = this.highestFlyingBelow(block);
539 if (flyer && nextY <= flyer.y + 1 + 1e-9)
540 {
541 if (this.attachToFlyingCluster(block, flyer))
542 { continue; }
543 }
327 var top = this.stackTop(block.col);
544 var top = this.stackTop(block.col);
328 var nextY = block.y - speed * dt;
545 var below = this.highestFallingBelow(block);
546 if (below && below.y + 1 > top)
547 { top = below.y + 1; }
329 if (nextY <= top)
548 if (nextY <= top)
330 {
549 {
331 block.y = top;
550 block.y = top;
332 block.state = STATE_STILL;
551 this.landFallingBlock(block);
333 block.vy = 0;
334 }
552 }
335 else
553 else
336 { block.y = nextY; }
554 { block.y = nextY; }
@@ -353,7 +571,8 @@ GameCore.prototype.clusterMembers = function (clusterId)
353 * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a
571 * 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.
572 * 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
573 * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the
356 * upward deceleration; falling uses planet.fallSpeed.
574 * upward deceleration. After the apex, vy eases from 0 to -fallSpeed
575 * at planet.fallAccel, then stays at terminal fallSpeed.
357 */
576 */
358GameCore.prototype.advanceJump = function (body, dt)
577GameCore.prototype.advanceJump = function (body, dt)
359{
578{
@@ -386,8 +605,35 @@ GameCore.prototype.advanceJump = function (body, dt)
386 }
605 }
387 if (body.phase === 'down' && left > 0)
606 if (body.phase === 'down' && left > 0)
388 {
607 {
389 body.vy = -this.planet.fallSpeed;
608 var target = -this.planet.fallSpeed;
390 dy += body.vy * left;
609 var accel = this.planet.fallAccel;
610 if (accel < 1e-12)
611 {
612 body.vy = target;
613 dy += body.vy * left;
614 }
615 else if (body.vy > target + 1e-12)
616 {
617 var tReach = (body.vy - target) / accel;
618 if (tReach <= left + 1e-15)
619 {
620 dy += (body.vy + target) * 0.5 * tReach;
621 body.vy = target;
622 left -= tReach;
623 dy += body.vy * left;
624 }
625 else
626 {
627 var vyNew = body.vy - accel * left;
628 dy += (body.vy + vyNew) * 0.5 * left;
629 body.vy = vyNew;
630 }
631 }
632 else
633 {
634 body.vy = target;
635 dy += body.vy * left;
636 }
391 }
637 }
392 return dy;
638 return dy;
393};
639};
@@ -416,14 +662,18 @@ GameCore.prototype.integrateClusters = function (dt)
416 members[i].vy = cluster.vy;
662 members[i].vy = cluster.vy;
417 }
663 }
418
664
665 this.catchFallingOntoCluster(cluster, members);
666
419 if (cluster.phase === 'down')
667 if (cluster.phase === 'down')
420 { this.tryLandCluster(cluster, members); }
668 { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }
421 }
669 }
422};
670};
423
671
424GameCore.prototype.tryLandCluster = function (cluster, members)
672GameCore.prototype.tryLandCluster = function (cluster, members)
425{
673{
426 var self = this;
674 if (!members.length)
675 { return; }
676 var shift = 0;
427 var hit = false;
677 var hit = false;
428 for (var i = 0; i < members.length; i++)
678 for (var i = 0; i < members.length; i++)
429 {
679 {
@@ -433,63 +683,109 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
433 return other.clusterId === cluster.id;
683 return other.clusterId === cluster.id;
434 });
684 });
435 if (block.y <= top + 1e-9)
685 if (block.y <= top + 1e-9)
436 { hit = true; }
686 {
687 hit = true;
688 var need = top - block.y;
689 if (need > shift)
690 { shift = need; }
691 }
437 }
692 }
438 if (!hit)
693 if (!hit)
439 { return; }
694 { return; }
695 // Lift the whole rigid body so the deepest contact sits flush.
696 for (var i = 0; i < members.length; i++)
697 { members[i].y += shift; }
440 this.landCluster(cluster, members);
698 this.landCluster(cluster, members);
441};
699};
442
700
701/**
702 * Land as a rigid group: relative positions are kept, so part of the
703 * cluster may hang above a gap. Grey members start the 3s settle timer;
704 * coloured cargo stays put with them until every grey has recolored.
705 * Members that actually rest on the floor or on another block bounce.
706 */
443GameCore.prototype.landCluster = function (cluster, members)
707GameCore.prototype.landCluster = function (cluster, members)
444{
708{
445 var byCol = Object.create(null);
446 for (var i = 0; i < members.length; i++)
709 for (var i = 0; i < members.length; i++)
447 {
710 {
448 var block = members[i];
711 var block = members[i];
449 if (!byCol[block.col])
712 block.vy = 0;
450 { byCol[block.col] = []; }
713 block.flame = false;
451 byCol[block.col].push(block);
714 block.clusterId = cluster.id;
715 if (block.color === COLOR_GRAY)
716 {
717 block.state = STATE_SETTLING;
718 block.settleTime = 0;
719 }
720 else
721 {
722 // Cargo stays in the rigid group (not matchable) until release.
723 block.state = STATE_SETTLING;
724 block.settleTime = 0;
725 }
726 if (this.hasSupport(block))
727 { this.startBounce(block); }
452 }
728 }
453 var cols = Object.keys(byCol);
729 cluster.phase = 'landed';
454 for (var c = 0; c < cols.length; c++)
730 cluster.vy = 0;
731};
732
733/**
734 * After every grey in a landed cluster has recolored, let the group go.
735 * Unsupported blocks fall individually from rest; supported ones stay.
736 */
737GameCore.prototype.releaseLandedCluster = function (cluster)
738{
739 var members = this.clusterMembers(cluster.id);
740 var releasing = Object.create(null);
741 for (var i = 0; i < members.length; i++)
742 { releasing[members[i].id] = true; }
743
744 for (var i = 0; i < members.length; i++)
455 {
745 {
456 var col = +cols[c];
746 var block = members[i];
457 var list = byCol[col];
747 if (block.color === COLOR_GRAY)
458 list.sort(function (a, b) { return a.y - b.y; });
459 var self = this;
460 var top = this.stackTop(col, function (other)
461 {
748 {
462 return other.clusterId === cluster.id;
749 block.color = this.randomColor();
463 });
750 block.settleTime = 0;
464 for (var i = 0; i < list.length; i++)
751 }
752 block.flame = false;
753 this.leaveCluster(block);
754 // Support from outside the group (floor or a foreign block). Fellow
755 // members that will also fall do not count, so a hanging stack
756 // drops together without the top one staying put.
757 var supported = this.hasSupportOutside(block, releasing);
758 if (supported)
465 {
759 {
466 var block = list[i];
760 block.state = STATE_STILL;
467 block.y = top;
761 block.vy = 0;
762 }
763 else
764 {
765 block.state = STATE_FALLING;
468 block.vy = 0;
766 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 }
767 }
482 }
768 }
483 cluster.phase = 'landed';
769 delete this.clusters[cluster.id];
484 cluster.vy = 0;
770};
485 var remaining = 0;
771
772GameCore.prototype.hasSupportOutside = function (block, ignoreIds)
773{
774 if (block.y <= 1e-9)
775 { return true; }
486 for (var i = 0; i < this.blocks.length; i++)
776 for (var i = 0; i < this.blocks.length; i++)
487 {
777 {
488 if (this.blocks[i].clusterId === cluster.id)
778 var other = this.blocks[i];
489 { remaining++; }
779 if (other === block || other.col !== block.col)
780 { continue; }
781 if (ignoreIds && ignoreIds[other.id])
782 { continue; }
783 if (!this.isGrounded(other))
784 { continue; }
785 if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
786 { return true; }
490 }
787 }
491 if (remaining === 0)
788 return false;
492 { delete this.clusters[cluster.id]; }
493};
789};
494
790
495GameCore.prototype.integrateManual = function (dt)
791GameCore.prototype.integrateManual = function (dt)
@@ -526,8 +822,11 @@ GameCore.prototype.integrateManual = function (dt)
526
822
527 if (block.manualPhase === 'down')
823 if (block.manualPhase === 'down')
528 {
824 {
825 var proxy = { phase: 'down', vy: block.vy, decel: 0 };
826 var dy = this.advanceJump(proxy, dt);
827 block.vy = proxy.vy;
828 var nextY = block.y + dy;
529 var top = this.stackTop(block.col);
829 var top = this.stackTop(block.col);
530 var nextY = block.y - this.planet.fallSpeed * dt;
531 if (nextY <= top)
830 if (nextY <= top)
532 {
831 {
533 block.y = top;
832 block.y = top;
@@ -535,11 +834,11 @@ GameCore.prototype.integrateManual = function (dt)
535 block.vy = 0;
834 block.vy = 0;
536 block.manualPhase = null;
835 block.manualPhase = null;
537 block.flame = false;
836 block.flame = false;
837 this.startBounce(block);
538 }
838 }
539 else
839 else
540 {
840 {
541 block.y = Math.min(nextY, this.height - 1);
841 block.y = Math.min(nextY, this.height - 1);
542 block.vy = -this.planet.fallSpeed;
543 }
842 }
544 }
843 }
545 }
844 }
@@ -698,15 +997,64 @@ GameCore.prototype.advanceSettle = function (dt)
698 var block = this.blocks[i];
997 var block = this.blocks[i];
699 if (block.state !== STATE_SETTLING)
998 if (block.state !== STATE_SETTLING)
700 { continue; }
999 { continue; }
701 block.settleTime += dt;
1000 var inLanded = block.clusterId != null
702 if (block.settleTime >= this.settleTime)
1001 && this.clusters[block.clusterId]
1002 && this.clusters[block.clusterId].phase === 'landed';
1003 if (block.color === COLOR_GRAY)
703 {
1004 {
704 block.color = this.randomColor();
1005 block.settleTime += dt;
705 block.state = STATE_STILL;
1006 if (block.settleTime >= this.settleTime)
706 block.settleTime = 0;
1007 {
707 block.flame = false;
1008 block.color = this.randomColor();
1009 block.settleTime = 0;
1010 block.flame = false;
1011 if (inLanded)
1012 {
1013 // Stay in the rigid group; releaseLandedCluster runs below.
1014 block.state = STATE_SETTLING;
1015 }
1016 else
1017 {
1018 this.leaveCluster(block);
1019 if (this.hasSupport(block))
1020 {
1021 block.state = STATE_STILL;
1022 block.vy = 0;
1023 }
1024 else
1025 {
1026 block.state = STATE_FALLING;
1027 block.vy = 0;
1028 }
1029 }
1030 }
1031 }
1032 else if (!inLanded)
1033 {
1034 // Coloured settling that is not in a landed cluster (shouldn't
1035 // normally happen): just become still or fall.
708 this.leaveCluster(block);
1036 this.leaveCluster(block);
1037 block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;
1038 if (block.state === STATE_FALLING)
1039 { block.vy = 0; }
1040 }
1041 }
1042
1043 var ids = Object.keys(this.clusters);
1044 for (var n = 0; n < ids.length; n++)
1045 {
1046 var cluster = this.clusters[ids[n]];
1047 if (!cluster || cluster.phase !== 'landed')
1048 { continue; }
1049 var members = this.clusterMembers(cluster.id);
1050 var greySettling = false;
1051 for (var i = 0; i < members.length; i++)
1052 {
1053 if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING)
1054 { greySettling = true; }
709 }
1055 }
1056 if (!greySettling)
1057 { this.releaseLandedCluster(cluster); }
710 }
1058 }
711};
1059};
712
1060
@@ -1020,7 +1368,15 @@ GameCore.prototype.beginDrag = function (col, y)
1020 var block = this.blockAt(col, row);
1368 var block = this.blockAt(col, row);
1021 if (!block || block.state !== STATE_STILL || block.color < 0)
1369 if (!block || block.state !== STATE_STILL || block.color < 0)
1022 { return false; }
1370 { return false; }
1023 this.drag = { blockId: block.id, pointerY: y, launchIntent: false };
1371 var top = this.columnStackTopRow(col);
1372 var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS;
1373 this.drag = {
1374 blockId: block.id,
1375 pointerY: y,
1376 startY: y,
1377 launchIntent: false,
1378 startedAsTop: startedAsTop
1379 };
1024 return true;
1380 return true;
1025};
1381};
1026
1382
@@ -1042,9 +1398,19 @@ GameCore.prototype.moveDrag = function (y)
1042 this.drag.pointerY = y;
1398 this.drag.pointerY = y;
1043 this.swapToward(block, y);
1399 this.swapToward(block, y);
1044 var top = this.columnStackTopRow(block.col);
1400 var top = this.columnStackTopRow(block.col);
1045 // The top block occupies [top, top+1). Launch only when the pointer
1401 var isTop = Math.abs(block.y - top) < ALIGN_EPS;
1046 // is pulled into the empty space above the stack.
1402 if (this.drag.startedAsTop)
1047 this.drag.launchIntent = (Math.abs(block.y - top) < ALIGN_EPS && y > top + 1 + 0.25);
1403 {
1404 // Already the topmost block: pulling upward past its own position
1405 // (LAUNCH_PULL tiles above the press point) starts a manual jump.
1406 this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL);
1407 }
1408 else
1409 {
1410 // Started mid-stack: swap first, and only launch once the pointer
1411 // is pulled into the empty space above the (new) top.
1412 this.drag.launchIntent = isTop && (y > top + 1 + 0.25);
1413 }
1048};
1414};
1049
1415
1050GameCore.prototype.endDrag = function ()
1416GameCore.prototype.endDrag = function ()
test/falling-cluster.test.js added +235 −0
@@ -0,0 +1,235 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js');
6
7 function flyingGrayRow (game, y, cols)
8 {
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: -game.planet.fallSpeed,
13 decel: 0,
14 phase: 'down'
15 };
16 game.clusters[cluster.id] = cluster;
17 const members = [];
18 for (const col of cols)
19 {
20 const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying');
21 b.clusterId = cluster.id;
22 b.vy = cluster.vy;
23 b.flame = true;
24 members.push(b);
25 }
26 return { cluster, members };
27 }
28
29 describe('Falling blocks land on flying clusters', () =>
30 {
31 it('a falling spawn lands on a flying cluster and flies along as cargo', () =>
32 {
33 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
34 const { cluster, members } = flyingGrayRow(g, 6, [2, 3, 4]);
35 // Freeze ascent/descent so the falling block can meet them cleanly,
36 // then unfreeze so they move together.
37 cluster.phase = 'hold';
38 cluster.vy = 0;
39 const falling = g.createBlock(3, 10, 1, 'falling');
40 falling.vy = -g.planet.fallSpeed;
41 // Fall from 10 onto 6+1=7: distance 3, at fallSpeed 13/3 ≈ 4.33 → ~0.7s
42 g.step(1.0, 1 / 120);
43 assert.equal(falling.state, 'flying', 'should have attached, state=' + falling.state + ' y=' + falling.y);
44 assert.equal(falling.clusterId, cluster.id);
45 assert.equal(falling.flame, false, 'cargo has no flame');
46 assert.equal(falling.color, 1, 'keeps its color');
47 assert.ok(almost(falling.y, members[1].y + 1, 0.05), 'sits on top, y=' + falling.y + ' flyer=' + members[1].y);
48
49 // Now let the cluster descend: cargo must move with it, same dy.
50 cluster.phase = 'down';
51 cluster.vy = 0; // will ease into fallSpeed
52 const yCargo = falling.y;
53 const yFlyer = members[1].y;
54 g.step(0.4, 1 / 120);
55 assert.equal(falling.clusterId, cluster.id);
56 assert.equal(falling.state, 'flying');
57 assert.ok(almost(falling.y - members[1].y, yCargo - yFlyer, 0.05),
58 'relative offset kept: cargo=' + falling.y + ' flyer=' + members[1].y);
59 assert.ok(falling.y < yCargo, 'must have moved down with the cluster');
60 assert.ok(almost(falling.vy, members[1].vy, 1e-9), 'same vy');
61 });
62
63 it('a falling block does not pass through a descending or rising cluster', () =>
64 {
65 const g = makeGame({ noSpawn: true });
66 const { cluster } = flyingGrayRow(g, 5, [1]);
67 cluster.phase = 'down';
68 cluster.vy = -g.planet.fallSpeed;
69 const falling = g.createBlock(1, 8, 2, 'falling');
70 falling.vy = -g.planet.fallSpeed;
71 g.step(2.0, 1 / 120);
72 assert.notEqual(falling.state, 'falling');
73 assert.ok(falling.state === 'flying' || falling.state === 'settling' || falling.state === 'still',
74 'must not still be falling through, state=' + falling.state + ' y=' + falling.y);
75 // Never went below the flyer without joining (flyer may have landed).
76 const flyer = g.blocks.find((b) => b.color === GameCore.COLOR_GRAY);
77 if (falling.state === 'flying' && flyer)
78 { assert.ok(falling.y >= flyer.y - 1e-6, 'cargo not below its flyer'); }
79 });
80
81 it('a rising cluster that meets a falling block picks it up', () =>
82 {
83 const g = makeGame({ noSpawn: true });
84 const { cluster, members } = flyingGrayRow(g, 3, [5]);
85 cluster.phase = 'up';
86 cluster.jumpHeight = 6;
87 cluster.vy = Planet.launchVelocity(g.planet, 6);
88 cluster.decel = Planet.launchDeceleration(g.planet, 6);
89 members[0].vy = cluster.vy;
90 const falling = g.createBlock(5, 7, 3, 'falling');
91 falling.vy = -g.planet.fallSpeed;
92 g.step(0.8, 1 / 120);
93 assert.equal(falling.state, 'flying', 'picked up, state=' + falling.state + ' y=' + falling.y);
94 assert.equal(falling.clusterId, cluster.id);
95 assert.ok(falling.y >= members[0].y, 'sits at or above the flyer, cargo=' + falling.y + ' flyer=' + members[0].y);
96 });
97 });
98
99 describe('Rigid grey landing', () =>
100 {
101 it('keeps relative positions, so part of the group can hang above a gap', () =>
102 {
103 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } });
104 // Horizontal match on a pedestal in col 1 only: cols 0 and 2 will hang.
105 g.place(1, 0, 4); // pedestal
106 placeRow(g, 1, [0, 1, 2], 0);
107 g.resolveMatches();
108 assert.ok(g.blocks.some((b) => b.state === 'flying'));
109 let landed = false;
110 for (let i = 0; i < 3000; i++)
111 {
112 g.tick(1 / 120);
113 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
114 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
115 {
116 landed = true;
117 break;
118 }
119 }
120 assert.ok(landed, 'should have landed rigid\n' + g.snapshot().picture);
121 const at = (col) => g.blocks.find((b) => b.col === col && b.color === GameCore.COLOR_GRAY);
122 const a = at(0);
123 const b = at(1);
124 const c = at(2);
125 assert.ok(a && b && c, 'all three greys present');
126 // The middle one rests on the pedestal (y=1). The sides hang at the same y.
127 assert.ok(almost(b.y, 1, 0.05), 'middle sits on pedestal, y=' + b.y);
128 assert.ok(almost(a.y, b.y, 0.05), 'left hangs at the same height, y=' + a.y);
129 assert.ok(almost(c.y, b.y, 0.05), 'right hangs at the same height, y=' + c.y);
130 assert.equal(g.blockAt(0, 0), null, 'gap under the left hang remains');
131 assert.equal(g.blockAt(2, 0), null, 'gap under the right hang remains');
132 // They stay there through the settle wait.
133 g.step(2.9, 1 / 60);
134 assert.ok(almost(a.y, 1, 0.05) && a.state === 'settling');
135 });
136
137 it('after conversion, hanging blocks fall individually and supported ones stay', () =>
138 {
139 const g = makeGame({
140 noSpawn: true,
141 planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 },
142 seed: 7
143 });
144 g.place(1, 0, 4);
145 placeRow(g, 1, [0, 1, 2], 0);
146 g.resolveMatches();
147 for (let i = 0; i < 3000; i++)
148 {
149 g.tick(1 / 120);
150 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
151 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
152 { break; }
153 }
154 const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY);
155 const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY);
156 const right = g.blocks.find((b) => b.col === 2 && b.color === GameCore.COLOR_GRAY);
157 const leftY = left.y;
158 g.step(3.0, 1 / 120);
159 // Conversion happens at 3s; take a short step so the fall is visible
160 // but not finished (from rest, Terra-like ease is not instant).
161 g.step(0.25, 1 / 120);
162 assert.ok(mid.color >= 0 && mid.state === 'still', 'supported middle stays, state=' + mid.state);
163 assert.ok(almost(mid.y, 1, 0.05), 'middle stays on the pedestal, y=' + mid.y);
164 assert.equal(mid.clusterId, null);
165 // Hanging sides fall from rest — they must not teleport to the floor.
166 assert.equal(left.state, 'falling', 'left should still be falling after 0.25s, state=' + left.state + ' y=' + left.y);
167 assert.ok(left.y < leftY - 0.05, 'left has started falling smoothly, y=' + left.y + ' from ' + leftY);
168 assert.ok(left.y > 0.25, 'must not have teleported to the floor, y=' + left.y);
169 assert.ok(Math.abs(left.vy) <= g.planet.fallSpeed + 1e-6);
170 assert.ok(right.state === 'falling' || (right.state === 'still' && almost(right.y, 0, 0.05)),
171 'right falls, state=' + right.state);
172 // Give them time to land individually.
173 g.step(g.planet.fallTime + 0.5, 1 / 60);
174 assert.equal(left.state, 'still');
175 assert.ok(almost(left.y, 0, 0.05), 'left lands on the floor, y=' + left.y);
176 assert.equal(right.state, 'still');
177 assert.ok(almost(right.y, 0, 0.05));
178 });
179 });
180
181 describe('Landing bounce planet parameter', () =>
182 {
183 it('sets bouncing on a landed block, faster at low air resistance', () =>
184 {
185 const slow = Planet.createPlanet({ fallTime: 5, jumpsToClear: 2, riseTime: 1.5 });
186 const fast = Planet.createPlanet({ fallTime: 1, jumpsToClear: 2, riseTime: 1.5 });
187 assert.ok(fast.bounceDuration < slow.bounceDuration,
188 'low air (fallTime=1) must bounce faster: ' + fast.bounceDuration + ' vs ' + slow.bounceDuration);
189 assert.ok(almost(fast.bounceDuration, 0.12 * 1, 1e-9));
190 assert.ok(almost(slow.bounceDuration, 0.12 * 5, 1e-9));
191 assert.equal(fast.bounceSquash, 0.16);
192 assert.ok(fast.apexEaseTime < slow.apexEaseTime);
193 assert.ok(fast.fallAccel > slow.fallAccel, 'low air eases into fall faster');
194
195 const g = makeGame({ noSpawn: true, planetOpts: { fallTime: 3 } });
196 const block = g.createBlock(0, 5, 1, 'falling');
197 block.vy = -g.planet.fallSpeed;
198 let landed = false;
199 for (let i = 0; i < 2000; i++)
200 {
201 g.tick(1 / 120);
202 if (block.state === 'still')
203 {
204 landed = true;
205 break;
206 }
207 }
208 assert.ok(landed, 'should have landed');
209 assert.ok(almost(block.y, 0, 0.05));
210 assert.equal(block.bouncing, true);
211 assert.ok(block.bounceAge >= 0 && block.bounceAge < g.planet.bounceDuration,
212 'bounce just started, age=' + block.bounceAge);
213 g.step(g.planet.bounceDuration + 0.05, 1 / 60);
214 assert.equal(block.bouncing, false);
215 });
216
217 it('a rigid cluster bounce only on members that actually rest on something', () =>
218 {
219 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } });
220 g.place(1, 0, 4);
221 placeRow(g, 1, [0, 1, 2], 0);
222 g.resolveMatches();
223 for (let i = 0; i < 3000; i++)
224 {
225 g.tick(1 / 120);
226 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
227 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
228 { break; }
229 }
230 const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY);
231 const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY);
232 assert.equal(mid.bouncing, true, 'the member on the pedestal bounces');
233 assert.equal(left.bouncing, false, 'the hanging member does not bounce');
234 });
235 });
@@ -0,0 +1,235 @@
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, placeRow, almost, GameCore, Planet } = require('./helpers.js');
6
7function flyingGrayRow (game, y, cols)
8{
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: -game.planet.fallSpeed,
13 decel: 0,
14 phase: 'down'
15 };
16 game.clusters[cluster.id] = cluster;
17 const members = [];
18 for (const col of cols)
19 {
20 const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying');
21 b.clusterId = cluster.id;
22 b.vy = cluster.vy;
23 b.flame = true;
24 members.push(b);
25 }
26 return { cluster, members };
27}
28
29describe('Falling blocks land on flying clusters', () =>
30{
31 it('a falling spawn lands on a flying cluster and flies along as cargo', () =>
32 {
33 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
34 const { cluster, members } = flyingGrayRow(g, 6, [2, 3, 4]);
35 // Freeze ascent/descent so the falling block can meet them cleanly,
36 // then unfreeze so they move together.
37 cluster.phase = 'hold';
38 cluster.vy = 0;
39 const falling = g.createBlock(3, 10, 1, 'falling');
40 falling.vy = -g.planet.fallSpeed;
41 // Fall from 10 onto 6+1=7: distance 3, at fallSpeed 13/3 ≈ 4.33 → ~0.7s
42 g.step(1.0, 1 / 120);
43 assert.equal(falling.state, 'flying', 'should have attached, state=' + falling.state + ' y=' + falling.y);
44 assert.equal(falling.clusterId, cluster.id);
45 assert.equal(falling.flame, false, 'cargo has no flame');
46 assert.equal(falling.color, 1, 'keeps its color');
47 assert.ok(almost(falling.y, members[1].y + 1, 0.05), 'sits on top, y=' + falling.y + ' flyer=' + members[1].y);
48
49 // Now let the cluster descend: cargo must move with it, same dy.
50 cluster.phase = 'down';
51 cluster.vy = 0; // will ease into fallSpeed
52 const yCargo = falling.y;
53 const yFlyer = members[1].y;
54 g.step(0.4, 1 / 120);
55 assert.equal(falling.clusterId, cluster.id);
56 assert.equal(falling.state, 'flying');
57 assert.ok(almost(falling.y - members[1].y, yCargo - yFlyer, 0.05),
58 'relative offset kept: cargo=' + falling.y + ' flyer=' + members[1].y);
59 assert.ok(falling.y < yCargo, 'must have moved down with the cluster');
60 assert.ok(almost(falling.vy, members[1].vy, 1e-9), 'same vy');
61 });
62
63 it('a falling block does not pass through a descending or rising cluster', () =>
64 {
65 const g = makeGame({ noSpawn: true });
66 const { cluster } = flyingGrayRow(g, 5, [1]);
67 cluster.phase = 'down';
68 cluster.vy = -g.planet.fallSpeed;
69 const falling = g.createBlock(1, 8, 2, 'falling');
70 falling.vy = -g.planet.fallSpeed;
71 g.step(2.0, 1 / 120);
72 assert.notEqual(falling.state, 'falling');
73 assert.ok(falling.state === 'flying' || falling.state === 'settling' || falling.state === 'still',
74 'must not still be falling through, state=' + falling.state + ' y=' + falling.y);
75 // Never went below the flyer without joining (flyer may have landed).
76 const flyer = g.blocks.find((b) => b.color === GameCore.COLOR_GRAY);
77 if (falling.state === 'flying' && flyer)
78 { assert.ok(falling.y >= flyer.y - 1e-6, 'cargo not below its flyer'); }
79 });
80
81 it('a rising cluster that meets a falling block picks it up', () =>
82 {
83 const g = makeGame({ noSpawn: true });
84 const { cluster, members } = flyingGrayRow(g, 3, [5]);
85 cluster.phase = 'up';
86 cluster.jumpHeight = 6;
87 cluster.vy = Planet.launchVelocity(g.planet, 6);
88 cluster.decel = Planet.launchDeceleration(g.planet, 6);
89 members[0].vy = cluster.vy;
90 const falling = g.createBlock(5, 7, 3, 'falling');
91 falling.vy = -g.planet.fallSpeed;
92 g.step(0.8, 1 / 120);
93 assert.equal(falling.state, 'flying', 'picked up, state=' + falling.state + ' y=' + falling.y);
94 assert.equal(falling.clusterId, cluster.id);
95 assert.ok(falling.y >= members[0].y, 'sits at or above the flyer, cargo=' + falling.y + ' flyer=' + members[0].y);
96 });
97});
98
99describe('Rigid grey landing', () =>
100{
101 it('keeps relative positions, so part of the group can hang above a gap', () =>
102 {
103 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } });
104 // Horizontal match on a pedestal in col 1 only: cols 0 and 2 will hang.
105 g.place(1, 0, 4); // pedestal
106 placeRow(g, 1, [0, 1, 2], 0);
107 g.resolveMatches();
108 assert.ok(g.blocks.some((b) => b.state === 'flying'));
109 let landed = false;
110 for (let i = 0; i < 3000; i++)
111 {
112 g.tick(1 / 120);
113 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
114 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
115 {
116 landed = true;
117 break;
118 }
119 }
120 assert.ok(landed, 'should have landed rigid\n' + g.snapshot().picture);
121 const at = (col) => g.blocks.find((b) => b.col === col && b.color === GameCore.COLOR_GRAY);
122 const a = at(0);
123 const b = at(1);
124 const c = at(2);
125 assert.ok(a && b && c, 'all three greys present');
126 // The middle one rests on the pedestal (y=1). The sides hang at the same y.
127 assert.ok(almost(b.y, 1, 0.05), 'middle sits on pedestal, y=' + b.y);
128 assert.ok(almost(a.y, b.y, 0.05), 'left hangs at the same height, y=' + a.y);
129 assert.ok(almost(c.y, b.y, 0.05), 'right hangs at the same height, y=' + c.y);
130 assert.equal(g.blockAt(0, 0), null, 'gap under the left hang remains');
131 assert.equal(g.blockAt(2, 0), null, 'gap under the right hang remains');
132 // They stay there through the settle wait.
133 g.step(2.9, 1 / 60);
134 assert.ok(almost(a.y, 1, 0.05) && a.state === 'settling');
135 });
136
137 it('after conversion, hanging blocks fall individually and supported ones stay', () =>
138 {
139 const g = makeGame({
140 noSpawn: true,
141 planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 },
142 seed: 7
143 });
144 g.place(1, 0, 4);
145 placeRow(g, 1, [0, 1, 2], 0);
146 g.resolveMatches();
147 for (let i = 0; i < 3000; i++)
148 {
149 g.tick(1 / 120);
150 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
151 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
152 { break; }
153 }
154 const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY);
155 const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY);
156 const right = g.blocks.find((b) => b.col === 2 && b.color === GameCore.COLOR_GRAY);
157 const leftY = left.y;
158 g.step(3.0, 1 / 120);
159 // Conversion happens at 3s; take a short step so the fall is visible
160 // but not finished (from rest, Terra-like ease is not instant).
161 g.step(0.25, 1 / 120);
162 assert.ok(mid.color >= 0 && mid.state === 'still', 'supported middle stays, state=' + mid.state);
163 assert.ok(almost(mid.y, 1, 0.05), 'middle stays on the pedestal, y=' + mid.y);
164 assert.equal(mid.clusterId, null);
165 // Hanging sides fall from rest — they must not teleport to the floor.
166 assert.equal(left.state, 'falling', 'left should still be falling after 0.25s, state=' + left.state + ' y=' + left.y);
167 assert.ok(left.y < leftY - 0.05, 'left has started falling smoothly, y=' + left.y + ' from ' + leftY);
168 assert.ok(left.y > 0.25, 'must not have teleported to the floor, y=' + left.y);
169 assert.ok(Math.abs(left.vy) <= g.planet.fallSpeed + 1e-6);
170 assert.ok(right.state === 'falling' || (right.state === 'still' && almost(right.y, 0, 0.05)),
171 'right falls, state=' + right.state);
172 // Give them time to land individually.
173 g.step(g.planet.fallTime + 0.5, 1 / 60);
174 assert.equal(left.state, 'still');
175 assert.ok(almost(left.y, 0, 0.05), 'left lands on the floor, y=' + left.y);
176 assert.equal(right.state, 'still');
177 assert.ok(almost(right.y, 0, 0.05));
178 });
179});
180
181describe('Landing bounce planet parameter', () =>
182{
183 it('sets bouncing on a landed block, faster at low air resistance', () =>
184 {
185 const slow = Planet.createPlanet({ fallTime: 5, jumpsToClear: 2, riseTime: 1.5 });
186 const fast = Planet.createPlanet({ fallTime: 1, jumpsToClear: 2, riseTime: 1.5 });
187 assert.ok(fast.bounceDuration < slow.bounceDuration,
188 'low air (fallTime=1) must bounce faster: ' + fast.bounceDuration + ' vs ' + slow.bounceDuration);
189 assert.ok(almost(fast.bounceDuration, 0.12 * 1, 1e-9));
190 assert.ok(almost(slow.bounceDuration, 0.12 * 5, 1e-9));
191 assert.equal(fast.bounceSquash, 0.16);
192 assert.ok(fast.apexEaseTime < slow.apexEaseTime);
193 assert.ok(fast.fallAccel > slow.fallAccel, 'low air eases into fall faster');
194
195 const g = makeGame({ noSpawn: true, planetOpts: { fallTime: 3 } });
196 const block = g.createBlock(0, 5, 1, 'falling');
197 block.vy = -g.planet.fallSpeed;
198 let landed = false;
199 for (let i = 0; i < 2000; i++)
200 {
201 g.tick(1 / 120);
202 if (block.state === 'still')
203 {
204 landed = true;
205 break;
206 }
207 }
208 assert.ok(landed, 'should have landed');
209 assert.ok(almost(block.y, 0, 0.05));
210 assert.equal(block.bouncing, true);
211 assert.ok(block.bounceAge >= 0 && block.bounceAge < g.planet.bounceDuration,
212 'bounce just started, age=' + block.bounceAge);
213 g.step(g.planet.bounceDuration + 0.05, 1 / 60);
214 assert.equal(block.bouncing, false);
215 });
216
217 it('a rigid cluster bounce only on members that actually rest on something', () =>
218 {
219 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 4, fallTime: 2, riseTime: 0.5 } });
220 g.place(1, 0, 4);
221 placeRow(g, 1, [0, 1, 2], 0);
222 g.resolveMatches();
223 for (let i = 0; i < 3000; i++)
224 {
225 g.tick(1 / 120);
226 const greys = g.blocks.filter((b) => b.color === GameCore.COLOR_GRAY);
227 if (greys.length >= 3 && greys.every((b) => b.state === 'settling'))
228 { break; }
229 }
230 const left = g.blocks.find((b) => b.col === 0 && b.color === GameCore.COLOR_GRAY);
231 const mid = g.blocks.find((b) => b.col === 1 && b.color === GameCore.COLOR_GRAY);
232 assert.equal(mid.bouncing, true, 'the member on the pedestal bounces');
233 assert.equal(left.bouncing, false, 'the hanging member does not bounce');
234 });
235});
test/match.test.js +13 −2
@@ -146,7 +146,7 @@ describe('Match detection and launch', () =>
146 146
147 147 describe('Launch physics', () =>
148 148 {
149 it('reaches the jump height in riseTime, then floats down at constant speed', () =>
149 it('reaches the jump height in riseTime, then eases into a constant descent', () =>
150 150 {
151 151 const g = makeGame({ noSpawn: true });
152 152 placeRow(g, 0, [0, 1, 2], 0);
@@ -157,7 +157,18 @@ describe('Launch physics', () =>
157 157 g.step(T, 1 / 240);
158 158 const apex = g.blocks[0].y;
159 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');
160 const cluster = g.clusters[g.blocks[0].clusterId];
161 assert.equal(cluster.phase, 'down');
162 // Just after the apex, speed is still below terminal fallSpeed.
163 const ease = g.planet.apexEaseTime;
164 assert.ok(ease > 0.1, 'apexEaseTime=' + ease);
165 g.step(ease * 0.4, 1 / 240);
166 const midVy = Math.abs(g.blocks[0].vy);
167 assert.ok(midVy > 0.2, 'should have started descending, vy=' + g.blocks[0].vy);
168 assert.ok(midVy < g.planet.fallSpeed - 0.3, 'should still be easing, vy=' + g.blocks[0].vy + ' fall=' + g.planet.fallSpeed);
169 // After the ease, descent is at constant fallSpeed.
170 g.step(ease * 0.6 + 0.05, 1 / 240);
171 assert.ok(Math.abs(Math.abs(g.blocks[0].vy) - g.planet.fallSpeed) < 0.08, 'terminal vy=' + g.blocks[0].vy);
161 172 const y1 = g.blocks[0].y;
162 173 g.step(0.2, 1 / 120);
163 174 const y2 = g.blocks[0].y;
@@ -146,7 +146,7 @@ describe('Match detection and launch', () =>
146
146
147describe('Launch physics', () =>
147describe('Launch physics', () =>
148{
148{
149 it('reaches the jump height in riseTime, then floats down at constant speed', () =>
149 it('reaches the jump height in riseTime, then eases into a constant descent', () =>
150 {
150 {
151 const g = makeGame({ noSpawn: true });
151 const g = makeGame({ noSpawn: true });
152 placeRow(g, 0, [0, 1, 2], 0);
152 placeRow(g, 0, [0, 1, 2], 0);
@@ -157,7 +157,18 @@ describe('Launch physics', () =>
157 g.step(T, 1 / 240);
157 g.step(T, 1 / 240);
158 const apex = g.blocks[0].y;
158 const apex = g.blocks[0].y;
159 assert.ok(Math.abs(apex - (startY + H)) < 0.08, 'apex y=' + apex + ' expected ' + (startY + H));
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');
160 const cluster = g.clusters[g.blocks[0].clusterId];
161 assert.equal(cluster.phase, 'down');
162 // Just after the apex, speed is still below terminal fallSpeed.
163 const ease = g.planet.apexEaseTime;
164 assert.ok(ease > 0.1, 'apexEaseTime=' + ease);
165 g.step(ease * 0.4, 1 / 240);
166 const midVy = Math.abs(g.blocks[0].vy);
167 assert.ok(midVy > 0.2, 'should have started descending, vy=' + g.blocks[0].vy);
168 assert.ok(midVy < g.planet.fallSpeed - 0.3, 'should still be easing, vy=' + g.blocks[0].vy + ' fall=' + g.planet.fallSpeed);
169 // After the ease, descent is at constant fallSpeed.
170 g.step(ease * 0.6 + 0.05, 1 / 240);
171 assert.ok(Math.abs(Math.abs(g.blocks[0].vy) - g.planet.fallSpeed) < 0.08, 'terminal vy=' + g.blocks[0].vy);
161 const y1 = g.blocks[0].y;
172 const y1 = g.blocks[0].y;
162 g.step(0.2, 1 / 120);
173 g.step(0.2, 1 / 120);
163 const y2 = g.blocks[0].y;
174 const y2 = g.blocks[0].y;
test/points.test.js +21 −2
@@ -63,7 +63,7 @@ describe('Points, landing and recolor', () =>
63 63 }
64 64 });
65 65
66 it('carried colored blocks land as still blocks and do not wait 3 seconds', () =>
66 it('carried colored blocks keep their color and stay in the rigid group until greys convert', () =>
67 67 {
68 68 const g = makeGame({
69 69 noSpawn: true,
@@ -72,10 +72,29 @@ describe('Points, landing and recolor', () =>
72 72 placeRow(g, 0, [0, 1, 2], 0);
73 73 const carried = g.place(1, 1, 4);
74 74 g.resolveMatches();
75 g.step(4, 1 / 120);
75 // Land (jump is short) then sit mid-settle.
76 let landed = false;
77 for (let i = 0; i < 2000; i++)
78 {
79 g.tick(1 / 120);
80 if (carried.state === 'settling' && g.clusters[carried.clusterId]
81 && g.clusters[carried.clusterId].phase === 'landed')
82 {
83 landed = true;
84 break;
85 }
86 }
87 assert.ok(landed, 'cargo should land with the rigid group, state=' + carried.state);
88 assert.equal(carried.color, 4, 'cargo does not recolor');
89 assert.ok(carried.clusterId != null, 'cargo stays in the group');
90 g.step(2.9, 1 / 60);
91 assert.equal(carried.color, 4);
92 assert.equal(carried.state, 'settling');
93 g.step(0.2, 1 / 60);
76 94 assert.equal(carried.color, 4);
77 95 assert.equal(carried.state, 'still');
78 96 assert.equal(carried.clusterId, null);
97 assert.equal(carried.y, 1);
79 98 });
80 99
81 100 it('spawn drops a random color into a random open column every second', () =>
@@ -63,7 +63,7 @@ describe('Points, landing and recolor', () =>
63 }
63 }
64 });
64 });
65
65
66 it('carried colored blocks land as still blocks and do not wait 3 seconds', () =>
66 it('carried colored blocks keep their color and stay in the rigid group until greys convert', () =>
67 {
67 {
68 const g = makeGame({
68 const g = makeGame({
69 noSpawn: true,
69 noSpawn: true,
@@ -72,10 +72,29 @@ describe('Points, landing and recolor', () =>
72 placeRow(g, 0, [0, 1, 2], 0);
72 placeRow(g, 0, [0, 1, 2], 0);
73 const carried = g.place(1, 1, 4);
73 const carried = g.place(1, 1, 4);
74 g.resolveMatches();
74 g.resolveMatches();
75 g.step(4, 1 / 120);
75 // Land (jump is short) then sit mid-settle.
76 let landed = false;
77 for (let i = 0; i < 2000; i++)
78 {
79 g.tick(1 / 120);
80 if (carried.state === 'settling' && g.clusters[carried.clusterId]
81 && g.clusters[carried.clusterId].phase === 'landed')
82 {
83 landed = true;
84 break;
85 }
86 }
87 assert.ok(landed, 'cargo should land with the rigid group, state=' + carried.state);
88 assert.equal(carried.color, 4, 'cargo does not recolor');
89 assert.ok(carried.clusterId != null, 'cargo stays in the group');
90 g.step(2.9, 1 / 60);
91 assert.equal(carried.color, 4);
92 assert.equal(carried.state, 'settling');
93 g.step(0.2, 1 / 60);
76 assert.equal(carried.color, 4);
94 assert.equal(carried.color, 4);
77 assert.equal(carried.state, 'still');
95 assert.equal(carried.state, 'still');
78 assert.equal(carried.clusterId, null);
96 assert.equal(carried.clusterId, null);
97 assert.equal(carried.y, 1);
79 });
98 });
80
99
81 it('spawn drops a random color into a random open column every second', () =>
100 it('spawn drops a random color into a random open column every second', () =>
test/swap.test.js +68 −0
@@ -117,3 +117,71 @@ describe('Vertical swapping', () =>
117 117 g.endDrag();
118 118 });
119 119 });
120
121 describe('Manual launch of the already-topmost block', () =>
122 {
123 it('dragging the top block upward past its own position starts a manual jump', () =>
124 {
125 const g = makeGame({ noSpawn: true });
126 // A single block is already the topmost (and only) block of its column.
127 const block = g.place(0, 0, 2);
128 // Grab at the visual centre (y=0.5), then pull just past its position —
129 // well short of the old "empty space above the cell" threshold (1.25).
130 assert.ok(g.beginDrag(0, 0.5));
131 assert.equal(g.drag.startedAsTop, true);
132 g.moveDrag(0.5 + 0.4);
133 assert.ok(0.9 < 0 + 1 + 0.25, 'this pull would have failed the old threshold');
134 assert.equal(g.drag.launchIntent, true, 'pull of 0.4 tiles past the press point must launch');
135 g.endDrag();
136 assert.equal(block.state, 'manual');
137 assert.equal(block.manualPhase, 'up');
138 assert.ok(block.vy > 0);
139 g.step(g.planet.riseTime, 1 / 240);
140 assert.ok(Math.abs(block.y - (g.height - 1)) < 0.08, 'jumps to just under the top, y=' + block.y);
141 assert.ok(block.y <= g.height - 1 + 1e-9);
142 });
143
144 it('a short upward twitch below LAUNCH_PULL does not launch', () =>
145 {
146 const g = makeGame({ noSpawn: true });
147 const block = g.place(3, 0, 1);
148 assert.ok(g.beginDrag(3, 0.5));
149 g.moveDrag(0.5 + 0.2);
150 assert.ok(0.2 < g.constructor.LAUNCH_PULL || 0.2 < 0.35);
151 assert.equal(g.drag.launchIntent, false);
152 g.endDrag();
153 assert.equal(block.state, 'still');
154 assert.equal(block.y, 0);
155 });
156
157 it('dragging a mid-stack block up to the top still swaps instead of launching', () =>
158 {
159 const g = makeGame({ noSpawn: true });
160 const bottom = g.place(1, 0, 0);
161 g.place(1, 1, 1);
162 g.place(1, 2, 2);
163 assert.ok(g.beginDrag(1, 0.3));
164 assert.equal(g.drag.startedAsTop, false);
165 // Pointer is past the top cell centre but not into the empty space.
166 g.moveDrag(2.4);
167 assert.equal(bottom.y, 2, 'swapped to the top');
168 assert.equal(g.drag.launchIntent, false, 'must not launch a mid-stack rearrange');
169 g.endDrag();
170 assert.equal(bottom.state, 'still');
171 assert.equal(bottom.y, 2);
172 });
173
174 it('after a mid-stack block becomes the top, a further pull into empty space still launches', () =>
175 {
176 const g = makeGame({ noSpawn: true });
177 const bottom = g.place(2, 0, 4);
178 g.place(2, 1, 3);
179 assert.ok(g.beginDrag(2, 0.2));
180 assert.equal(g.drag.startedAsTop, false);
181 g.moveDrag(1 + 1 + 0.4);
182 assert.equal(bottom.y, 1);
183 assert.equal(g.drag.launchIntent, true);
184 g.endDrag();
185 assert.equal(bottom.state, 'manual');
186 });
187 });
@@ -117,3 +117,71 @@ describe('Vertical swapping', () =>
117 g.endDrag();
117 g.endDrag();
118 });
118 });
119});
119});
120
121describe('Manual launch of the already-topmost block', () =>
122{
123 it('dragging the top block upward past its own position starts a manual jump', () =>
124 {
125 const g = makeGame({ noSpawn: true });
126 // A single block is already the topmost (and only) block of its column.
127 const block = g.place(0, 0, 2);
128 // Grab at the visual centre (y=0.5), then pull just past its position —
129 // well short of the old "empty space above the cell" threshold (1.25).
130 assert.ok(g.beginDrag(0, 0.5));
131 assert.equal(g.drag.startedAsTop, true);
132 g.moveDrag(0.5 + 0.4);
133 assert.ok(0.9 < 0 + 1 + 0.25, 'this pull would have failed the old threshold');
134 assert.equal(g.drag.launchIntent, true, 'pull of 0.4 tiles past the press point must launch');
135 g.endDrag();
136 assert.equal(block.state, 'manual');
137 assert.equal(block.manualPhase, 'up');
138 assert.ok(block.vy > 0);
139 g.step(g.planet.riseTime, 1 / 240);
140 assert.ok(Math.abs(block.y - (g.height - 1)) < 0.08, 'jumps to just under the top, y=' + block.y);
141 assert.ok(block.y <= g.height - 1 + 1e-9);
142 });
143
144 it('a short upward twitch below LAUNCH_PULL does not launch', () =>
145 {
146 const g = makeGame({ noSpawn: true });
147 const block = g.place(3, 0, 1);
148 assert.ok(g.beginDrag(3, 0.5));
149 g.moveDrag(0.5 + 0.2);
150 assert.ok(0.2 < g.constructor.LAUNCH_PULL || 0.2 < 0.35);
151 assert.equal(g.drag.launchIntent, false);
152 g.endDrag();
153 assert.equal(block.state, 'still');
154 assert.equal(block.y, 0);
155 });
156
157 it('dragging a mid-stack block up to the top still swaps instead of launching', () =>
158 {
159 const g = makeGame({ noSpawn: true });
160 const bottom = g.place(1, 0, 0);
161 g.place(1, 1, 1);
162 g.place(1, 2, 2);
163 assert.ok(g.beginDrag(1, 0.3));
164 assert.equal(g.drag.startedAsTop, false);
165 // Pointer is past the top cell centre but not into the empty space.
166 g.moveDrag(2.4);
167 assert.equal(bottom.y, 2, 'swapped to the top');
168 assert.equal(g.drag.launchIntent, false, 'must not launch a mid-stack rearrange');
169 g.endDrag();
170 assert.equal(bottom.state, 'still');
171 assert.equal(bottom.y, 2);
172 });
173
174 it('after a mid-stack block becomes the top, a further pull into empty space still launches', () =>
175 {
176 const g = makeGame({ noSpawn: true });
177 const bottom = g.place(2, 0, 4);
178 g.place(2, 1, 3);
179 assert.ok(g.beginDrag(2, 0.2));
180 assert.equal(g.drag.startedAsTop, false);
181 g.moveDrag(1 + 1 + 0.4);
182 assert.equal(bottom.y, 1);
183 assert.equal(g.drag.launchIntent, true);
184 g.endDrag();
185 assert.equal(bottom.state, 'manual');
186 });
187});