chore: rebuild game.js with touch, cluster, merge, grab and flame fixes
@@ -668,8 +668,8 @@ var LAUNCH_PULL = 0.35;
668
668
// Minimal accumulated vertical pointer travel (tiles) to swap one step.
669
669
// No hold delay: the first swap fires as soon as this distance is reached.
670
670
var SWAP_DISTANCE = 0.25;
671
// Grab a still block if the pointer is at most this far (tiles) from it.
672
var SNAP_Y = 0.65;
671
// Fallback snap (tiles) when the press cell is empty: nearest block in the column.
672
var SNAP_Y = 0.5;
673
673
// Horizontal slop (tiles) for snapping to a neighbouring column on press.
674
674
var HORIZONTAL_SLOP = 0.35;
675
675
@@ -1073,12 +1073,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)
1073
1073
if (other.y + 1 > roof)
1074
1074
{ roof = other.y + 1; }
1075
1075
}
1076
var oldN = this.clusterMembers(cluster.id).length;
1077
var fallingVy = block.vy;
1076
1078
block.y = roof;
1077
1079
block.state = STATE_FLYING;
1078
1080
block.clusterId = cluster.id;
1079
block.vy = cluster.vy;
1080
1081
block.flame = false;
1081
1082
block.settleTime = 0;
1083
// A cluster born from a thrown block averages in falling cargo
1084
// the same way two flying clusters merge (t1108). Other rockets
1085
// keep their velocity and just pick the cargo up.
1086
if (cluster.fromManual)
1087
{
1088
var newN = oldN + 1;
1089
cluster.vy = (cluster.vy * oldN + fallingVy) / newN;
1090
cluster.decel = ((cluster.decel || 0) * oldN) / newN;
1091
cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;
1092
cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down';
1093
var members = this.clusterMembers(cluster.id);
1094
for (var m = 0; m < members.length; m++)
1095
{ members[m].vy = cluster.vy; }
1096
}
1097
else
1098
{ block.vy = cluster.vy; }
1082
1099
return true;
1083
1100
};
1084
1101
@@ -1141,6 +1158,7 @@ GameCore.prototype.tick = function (dt)
1141
1158
this.integrateClusters(step);
1142
1159
this.mergeFlyingClusters();
1143
1160
this.integrateManual(step);
1161
this.mergeAnyManualFallingOverlaps();
1144
1162
this.cullOffTop();
1145
1163
left -= step;
1146
1164
}
@@ -1222,6 +1240,12 @@ GameCore.prototype.integrateFalling = function (dt)
1222
1240
{ continue; }
1223
1241
this.accelerateFall(block, dt);
1224
1242
var nextY = block.y + block.vy * dt;
1243
var manual = this.manualInColumnHit(block, block.y, nextY);
1244
if (manual)
1245
{
1246
this.mergeManualWithFalling(manual, this.contiguousFallingInColumn(manual.col, manual, [block]));
1247
continue;
1248
}
1225
1249
var flyer = this.highestFlyingBelow(block);
1226
1250
if (flyer && nextY <= flyer.y + 1 + 1e-9)
1227
1251
{
@@ -1622,6 +1646,8 @@ GameCore.prototype.integrateManual = function (dt)
1622
1646
block.vy = proxy.vy;
1623
1647
block.manualPhase = proxy.phase;
1624
1648
block.y = Math.min(nextY, block.manualTarget);
1649
if (this.tryMergeManualWithFalling(block, oldY, block.y))
1650
{ continue; }
1625
1651
if (block.y >= block.manualTarget - 1e-12)
1626
1652
{
1627
1653
block.y = block.manualTarget;
@@ -1655,11 +1681,207 @@ GameCore.prototype.integrateManual = function (dt)
1655
1681
else
1656
1682
{
1657
1683
block.y = Math.min(nextY, this.height - 1);
1684
if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))
1685
{ continue; }
1658
1686
}
1659
1687
}
1660
1688
}
1661
1689
};
1662
1690
1691
/**
1692
* If the manual throw (or its path this step) hits falling blocks, merge
1693
* them into a flying cluster with mass-weighted velocity, the same rule
1694
* as two flying clusters colliding (t1108). Returns true if merged.
1695
*/
1696
GameCore.prototype.tryMergeManualWithFalling = function (manual, oldY, newY)
1697
{
1698
if (!manual || manual.state !== STATE_MANUAL)
1699
{ return false; }
1700
var hits = this.fallingHitsOnManualPath(manual, oldY, newY);
1701
if (!hits.length)
1702
{ return false; }
1703
this.mergeManualWithFalling(manual, hits);
1704
return true;
1705
};
1706
1707
GameCore.prototype.manualInColumnHit = function (falling, oldY, newY)
1708
{
1709
var lo = Math.min(oldY, newY);
1710
var hi = Math.max(oldY, newY) + 1;
1711
for (var i = 0; i < this.blocks.length; i++)
1712
{
1713
var other = this.blocks[i];
1714
if (other === falling || other.col !== falling.col)
1715
{ continue; }
1716
if (other.state !== STATE_MANUAL)
1717
{ continue; }
1718
if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9)
1719
{ return other; }
1720
}
1721
return null;
1722
};
1723
1724
GameCore.prototype.fallingHitsOnManualPath = function (manual, oldY, newY)
1725
{
1726
var lo = Math.min(oldY, newY);
1727
var hi = Math.max(oldY, newY) + 1;
1728
var hits = [];
1729
for (var i = 0; i < this.blocks.length; i++)
1730
{
1731
var other = this.blocks[i];
1732
if (other === manual || other.col !== manual.col)
1733
{ continue; }
1734
if (other.state !== STATE_FALLING)
1735
{ continue; }
1736
if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9)
1737
{ hits.push(other); }
1738
}
1739
if (!hits.length)
1740
{ return hits; }
1741
return this.contiguousFallingInColumn(manual.col, manual, hits);
1742
};
1743
1744
/**
1745
* Every falling block in `col` that overlaps `anchor` or a hit already
1746
* in `seed`, plus a contiguous falling stack sitting on them.
1747
*/
1748
GameCore.prototype.contiguousFallingInColumn = function (col, anchor, seed)
1749
{
1750
var falling = [];
1751
for (var i = 0; i < this.blocks.length; i++)
1752
{
1753
if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING)
1754
{ falling.push(this.blocks[i]); }
1755
}
1756
falling.sort(function (a, b) { return a.y - b.y; });
1757
var include = Object.create(null);
1758
var queue = [];
1759
function enqueue (b)
1760
{
1761
if (!b || include[b.id])
1762
{ return; }
1763
include[b.id] = true;
1764
queue.push(b);
1765
}
1766
if (seed)
1767
{
1768
for (var s = 0; s < seed.length; s++)
1769
{ enqueue(seed[s]); }
1770
}
1771
// Also include falling that already overlap the anchor.
1772
if (anchor)
1773
{
1774
for (var i = 0; i < falling.length; i++)
1775
{
1776
if (falling[i].y < anchor.y + 1 - 1e-9 && anchor.y < falling[i].y + 1 - 1e-9)
1777
{ enqueue(falling[i]); }
1778
}
1779
}
1780
for (var q = 0; q < queue.length; q++)
1781
{
1782
var a = queue[q];
1783
for (var i = 0; i < falling.length; i++)
1784
{
1785
var b = falling[i];
1786
if (include[b.id])
1787
{ continue; }
1788
if (a.y < b.y + 1 + 1e-9 && b.y < a.y + 1 + 1e-9)
1789
{ enqueue(b); }
1790
}
1791
}
1792
var out = [];
1793
for (var i = 0; i < falling.length; i++)
1794
{
1795
if (include[falling[i].id])
1796
{ out.push(falling[i]); }
1797
}
1798
return out;
1799
};
1800
1801
/**
1802
* Merge a thrown (manual) block with falling blocks into one flying
1803
* cluster. Velocity, deceleration and jumpHeight are the mass-weighted
1804
* averages by block count, identical to mergeClusterGroup. The thrown
1805
* block stops being manual and the group follows normal cluster physics.
1806
* fromManual marks the cluster so later falling cargo also averages in.
1807
*/
1808
GameCore.prototype.mergeManualWithFalling = function (manual, fallingList)
1809
{
1810
if (!manual || manual.state !== STATE_MANUAL)
1811
{ return null; }
1812
var blocks = [manual];
1813
for (var i = 0; i < fallingList.length; i++)
1814
{
1815
if (fallingList[i] && fallingList[i].state === STATE_FALLING && blocks.indexOf(fallingList[i]) < 0)
1816
{ blocks.push(fallingList[i]); }
1817
}
1818
if (blocks.length < 2)
1819
{ return null; }
1820
var n = blocks.length;
1821
var vySum = 0;
1822
var decelSum = 0;
1823
var hSum = 0;
1824
for (var i = 0; i < n; i++)
1825
{
1826
var b = blocks[i];
1827
vySum += b.vy;
1828
if (b === manual)
1829
{
1830
decelSum += manual.manualDecel || 0;
1831
hSum += manual.manualJumpHeight != null ? manual.manualJumpHeight
1832
: Math.max(0, (manual.manualTarget || this.height - 1) - manual.y);
1833
}
1834
}
1835
var cluster = {
1836
id: this.nextClusterId++,
1837
jumpHeight: hSum / n,
1838
vy: vySum / n,
1839
decel: decelSum / n,
1840
phase: (vySum / n) > 1e-10 ? 'up' : 'down',
1841
fromManual: true
1842
};
1843
this.clusters[cluster.id] = cluster;
1844
for (var i = 0; i < n; i++)
1845
{
1846
var b = blocks[i];
1847
b.clusterId = cluster.id;
1848
b.state = STATE_FLYING;
1849
b.vy = cluster.vy;
1850
b.flame = b.color === COLOR_GRAY;
1851
b.manualPhase = null;
1852
b.manualTarget = null;
1853
b.manualDecel = 0;
1854
b.manualJumpHeight = 0;
1855
b.pushedClusters = null;
1856
b.settleTime = 0;
1857
}
1858
if (this.drag && this.drag.blockId === manual.id)
1859
{ this.drag = null; }
1860
this.packClusterOverlaps(cluster);
1861
this.checkChainRelaunch(cluster);
1862
return cluster;
1863
};
1864
1865
/** Safety net: any remaining manual/falling overlaps after the substep. */
1866
GameCore.prototype.mergeAnyManualFallingOverlaps = function ()
1867
{
1868
var manuals = [];
1869
for (var i = 0; i < this.blocks.length; i++)
1870
{
1871
if (this.blocks[i].state === STATE_MANUAL)
1872
{ manuals.push(this.blocks[i]); }
1873
}
1874
for (var m = 0; m < manuals.length; m++)
1875
{
1876
var manual = manuals[m];
1877
if (manual.state !== STATE_MANUAL)
1878
{ continue; }
1879
var hits = this.contiguousFallingInColumn(manual.col, manual);
1880
if (hits.length)
1881
{ this.mergeManualWithFalling(manual, hits); }
1882
}
1883
};
1884
1663
1885
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)
1664
1886
{
1665
1887
if (!manual.pushedClusters)
@@ -1791,10 +2013,12 @@ GameCore.prototype.startManualLaunch = function (block)
1791
2013
{
1792
2014
var target = this.height - 1;
1793
2015
var height = Math.max(0.05, target - block.y);
2016
this.leaveCluster(block);
1794
2017
block.state = STATE_MANUAL;
1795
2018
block.pushedClusters = Object.create(null);
1796
2019
block.manualTarget = target;
1797
2020
block.manualPhase = 'up';
2021
block.manualJumpHeight = height;
1798
2022
block.vy = PlanetMod.launchVelocity(this.planet, height);
1799
2023
block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);
1800
2024
block.flame = false;
@@ -1968,7 +2192,11 @@ GameCore.prototype.findMatchLines = function ()
1968
2192
for (var i = 0; i < this.blocks.length; i++)
1969
2193
{
1970
2194
var block = this.blocks[i];
1971
if (block.state === STATE_STILL && block.color >= 0)
2195
// Coloured cargo in a just-landed cluster (SETTLING) can match
2196
// with already-resting blocks immediately (t1110). Greys have no
2197
// colour and wait until they recolor; after release they are STILL.
2198
if ((block.state === STATE_STILL || block.state === STATE_SETTLING)
2199
&& block.color >= 0)
1972
2200
{ candidates.push(block); }
1973
2201
}
1974
2202
return this.collectLines(candidates, true);
@@ -2278,48 +2506,103 @@ GameCore.prototype.beginDrag = function (col, y)
2278
2506
};
2279
2507
2280
2508
/**
2281
* Instant grab: the nearest still, coloured block to (col, y), allowing
2282
* a small vertical miss and a horizontal slop into a neighbouring
2283
* column. `col` may be fractional (pointer x in tiles). No hold delay.
2509
* Instant grab, cell-based (t1109). The press is mapped to the grid cell
2510
* floor(col), floor(y); any press inside that cell — corners, padding,
2511
* gaps between sprites, a visually tweened or bouncing sprite — grabs
2512
* the block that occupies it. This uses simulation cells, not the drawn
2513
* sprite bounds. If no block sits in that cell (mid-animation, a block
2514
* whose y is off the integer), fall back to the nearest grabbable block
2515
* in the same column within SNAP_Y (about half a tile). A small
2516
* horizontal slop still consults a neighbouring column when the press
2517
* is near the edge and the primary cell is empty.
2284
2518
*/
2285
2519
GameCore.prototype.snapDragBlock = function (col, y)
2286
2520
{
2287
2521
var raw = col;
2288
2522
var c0 = Math.floor(raw + 1e-9);
2523
var row = Math.floor(y + 1e-9);
2289
2524
var frac = raw - c0;
2290
var candidates = [];
2291
if (c0 >= 0 && c0 < this.width)
2292
{ candidates.push(c0); }
2293
// Only consult a neighbour when the pointer is actually near the edge
2294
// (a whole-number col, as in unit tests, stays in that column).
2525
2526
if (c0 >= 0 && c0 < this.width && this.columnAllowsDrag(c0))
2527
{
2528
var exact = this.draggableInCell(c0, row);
2529
if (exact)
2530
{ return exact; }
2531
var near = this.nearestDraggableInColumn(c0, y, SNAP_Y);
2532
if (near)
2533
{ return near; }
2534
}
2535
2536
// Primary cell empty: allow a small horizontal slop into a neighbour.
2537
var neighbours = [];
2295
2538
if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0)
2296
{ candidates.push(c0 - 1); }
2539
{ neighbours.push(c0 - 1); }
2297
2540
if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width)
2298
{ candidates.push(c0 + 1); }
2541
{ neighbours.push(c0 + 1); }
2542
var best = null;
2543
var bestDist = Infinity;
2544
for (var n = 0; n < neighbours.length; n++)
2545
{
2546
var cc = neighbours[n];
2547
if (!this.columnAllowsDrag(cc))
2548
{ continue; }
2549
var hit = this.draggableInCell(cc, row) || this.nearestDraggableInColumn(cc, y, SNAP_Y);
2550
if (!hit)
2551
{ continue; }
2552
var dist = this.pointerDistToBlock(hit, raw, y);
2553
if (dist < bestDist)
2554
{
2555
bestDist = dist;
2556
best = hit;
2557
}
2558
}
2559
return best;
2560
};
2299
2561
2562
/** The draggable block that occupies grid cell (col, row), if any. */
2563
GameCore.prototype.draggableInCell = function (col, row)
2564
{
2565
var best = null;
2566
var bestOverlap = 0;
2567
var list = this.blocksInColumn(col);
2568
for (var i = 0; i < list.length; i++)
2569
{
2570
var block = list[i];
2571
if (!this.isDraggableBlock(block))
2572
{ continue; }
2573
var lo = Math.max(block.y, row);
2574
var hi = Math.min(block.y + 1, row + 1);
2575
var overlap = hi - lo;
2576
if (overlap > bestOverlap + 1e-12)
2577
{
2578
bestOverlap = overlap;
2579
best = block;
2580
}
2581
}
2582
if (best && bestOverlap > 1e-9)
2583
{ return best; }
2584
return null;
2585
};
2586
2587
/** Nearest draggable block in `col` whose footprint is within `maxDist` of `y`. */
2588
GameCore.prototype.nearestDraggableInColumn = function (col, y, maxDist)
2589
{
2300
2590
var best = null;
2301
2591
var bestDist = Infinity;
2302
for (var c = 0; c < candidates.length; c++)
2592
var list = this.blocksInColumn(col);
2593
for (var i = 0; i < list.length; i++)
2303
2594
{
2304
var cc = candidates[c];
2305
if (!this.isColumnStill(cc))
2595
var block = list[i];
2596
if (!this.isDraggableBlock(block))
2306
2597
{ continue; }
2307
var list = this.blocksInColumn(cc);
2308
for (var i = 0; i < list.length; i++)
2598
var dist = this.pointerDistToBlock(block, col + 0.5, y);
2599
if (dist < bestDist)
2309
2600
{
2310
var block = list[i];
2311
if (block.state !== STATE_STILL || block.color < 0)
2312
{ continue; }
2313
var dist = this.pointerDistToBlock(block, raw, y);
2314
// Prefer the column the pointer is actually in on a tie.
2315
if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0))
2316
{
2317
bestDist = dist;
2318
best = block;
2319
}
2601
bestDist = dist;
2602
best = block;
2320
2603
}
2321
2604
}
2322
if (!best || bestDist > SNAP_Y)
2605
if (!best || bestDist > maxDist)
2323
2606
{ return null; }
2324
2607
return best;
2325
2608
};
@@ -2348,12 +2631,12 @@ GameCore.prototype.moveDrag = function (y)
2348
2631
if (!this.drag)
2349
2632
{ return; }
2350
2633
var block = this.blockById(this.drag.blockId);
2351
if (!block || block.state !== STATE_STILL)
2634
if (!block || !this.isDraggableBlock(block))
2352
2635
{
2353
2636
this.drag = null;
2354
2637
return;
2355
2638
}
2356
if (!this.isColumnStill(block.col))
2639
if (!this.columnAllowsDrag(block.col))
2357
2640
{
2358
2641
this.drag = null;
2359
2642
return;
@@ -2483,15 +2766,101 @@ GameCore.prototype.swapBlocks = function (a, b)
2483
2766
var y = a.y;
2484
2767
a.y = b.y;
2485
2768
b.y = y;
2769
this.transferLandedMembership(a, b);
2770
};
2771
2772
/**
2773
* A landed cluster (grey rockets or linked cargo resting on the ground)
2774
* is a swappable part of the contiguous grounded stack. Flying, falling
2775
* and manual blocks are not. A locked column is not.
2776
*/
2777
GameCore.prototype.isDraggableBlock = function (block)
2778
{
2779
if (!block)
2780
{ return false; }
2781
if (block.state === STATE_LOCKED || block.state === STATE_FLYING
2782
|| block.state === STATE_FALLING || block.state === STATE_MANUAL)
2783
{ return false; }
2784
if (block.clusterId != null)
2785
{
2786
var cluster = this.clusters[block.clusterId];
2787
// Flying (rising / descending) clusters stay non-draggable.
2788
if (cluster && cluster.phase !== 'landed')
2789
{ return false; }
2790
}
2791
return block.state === STATE_STILL || block.state === STATE_SETTLING;
2792
};
2793
2794
/**
2795
* The grounded contiguous stack in this column can be dragged, even if a
2796
* flying cluster or a falling spawn occupies the air above it. Only a
2797
* locked column is blocked as a whole.
2798
*/
2799
GameCore.prototype.columnAllowsDrag = function (col)
2800
{
2801
return !this.isColumnLocked(col);
2802
};
2803
2804
GameCore.prototype.landedClusterId = function (block)
2805
{
2806
if (!block || block.clusterId == null)
2807
{ return null; }
2808
var cluster = this.clusters[block.clusterId];
2809
if (!cluster || cluster.phase !== 'landed')
2810
{ return null; }
2811
return block.clusterId;
2812
};
2813
2814
/**
2815
* After a swap: leaving a landed cluster clears membership; joining one
2816
* makes the block part of that rigid group. Same-cluster swaps only
2817
* exchange positions. See docs/ASSUMPTIONS.md (t1107).
2818
*/
2819
GameCore.prototype.transferLandedMembership = function (a, b)
2820
{
2821
var ca = this.landedClusterId(a);
2822
var cb = this.landedClusterId(b);
2823
if (ca === cb)
2824
{ return; }
2825
// Join first so leaveCluster does not delete a cluster that still
2826
// has the incoming member.
2827
this.applyLandedMembership(b, ca);
2828
this.applyLandedMembership(a, cb);
2829
};
2830
2831
GameCore.prototype.applyLandedMembership = function (block, clusterId)
2832
{
2833
if (clusterId == null)
2834
{
2835
var wasGrey = block.color === COLOR_GRAY;
2836
var wasSettling = block.state === STATE_SETTLING;
2837
this.leaveCluster(block);
2838
if (wasGrey && wasSettling)
2839
{
2840
// Keep settling and recolor on its own, same remaining timer.
2841
block.state = STATE_SETTLING;
2842
}
2843
else if (wasSettling)
2844
{
2845
block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;
2846
}
2847
return;
2848
}
2849
if (!this.clusters[clusterId])
2850
{ return; }
2851
block.clusterId = clusterId;
2852
block.flame = false;
2853
if (block.state === STATE_STILL || block.state === STATE_SETTLING)
2854
{ block.state = STATE_SETTLING; }
2486
2855
};
2487
2856
2488
2857
/**
2489
2858
* Test helper: stepwise-swap the block at (col, fromRow) to toRow.
2490
* Returns the moved block, or null if the column is not still.
2859
* Returns the moved block, or null if the column does not allow a drag.
2491
2860
*/
2492
2861
GameCore.prototype.swapTo = function (col, fromRow, toRow)
2493
2862
{
2494
if (!this.isColumnStill(col))
2863
if (!this.columnAllowsDrag(col))
2495
2864
{ return null; }
2496
2865
var block = this.blockAt(col, fromRow);
2497
2866
if (!block)
@@ -3196,6 +3565,9 @@ var app = {
3196
3565
flames: Object.create(null),
3197
3566
cells: [],
3198
3567
pointerId: null,
3568
pointerType: null,
3569
activeTouchId: null,
3570
debugDrag: false,
3199
3571
gameOverShown: false,
3200
3572
selectTween: null,
3201
3573
online: false,
@@ -3208,6 +3580,7 @@ var app = {
3208
3580
3209
3581
window.onload = function ()
3210
3582
{
3583
app.debugDrag = isDragDebugEnabled();
3211
3584
var play = document.getElementById('btn-play');
3212
3585
play.addEventListener('click', onPlay);
3213
3586
play.addEventListener('pointerup', function (e)
@@ -3360,10 +3733,25 @@ function placeTile (obj, col, y)
3360
3733
obj.transform.size = { x: t, y: t };
3361
3734
}
3362
3735
3736
function layoutIfNeeded ()
3737
{
3738
if (app.core && app.core.drag)
3739
{ return; }
3740
var vw = window.innerWidth || 320;
3741
var vh = window.innerHeight || 480;
3742
if (app._layoutVw === vw && app._layoutVh === vh && app.scene && app.tile)
3743
{ return; }
3744
layout();
3745
}
3746
3363
3747
function layout ()
3364
3748
{
3749
if (app.core && app.core.drag)
3750
{ return; }
3365
3751
var vw = window.innerWidth || 320;
3366
3752
var vh = window.innerHeight || 480;
3753
app._layoutVw = vw;
3754
app._layoutVh = vh;
3367
3755
var safeTop = 0;
3368
3756
try
3369
3757
{
@@ -3447,9 +3835,16 @@ function syncSprites ()
3447
3835
}
3448
3836
flame.renderer.visible = true;
3449
3837
var t = app.tile;
3838
// 3x as large (length and width, t1110). Keep the top of the
3839
// flame at the same overlap into the block so the extra length
3840
// extends downward under the rocket.
3841
var flameW = t * 0.85 * 3;
3842
var flameH = t * 0.85 * 3;
3843
var origCenter = app.fieldH - block.y * t + t * 0.15;
3844
var origTop = origCenter - (t * 0.85) / 2;
3450
3845
flame.transform.position.x = block.col * t + t / 2;
3451
flame.transform.position.y = app.fieldH - block.y * t + t * 0.15;
3452
flame.transform.size = { x: t * 0.85, y: t * 0.85 };
3846
flame.transform.position.y = origTop + flameH / 2;
3847
flame.transform.size = { x: flameW, y: flameH };
3453
3848
}
3454
3849
else if (app.flames[block.id])
3455
3850
{ app.flames[block.id].renderer.visible = false; }
@@ -3589,7 +3984,11 @@ function tickView (dt)
3589
3984
if (app.mode !== 'playing' || !app.core)
3590
3985
{ return; }
3591
3986
3592
layout();
3987
// Never resize the canvas mid-drag: a URL-bar hide would move the
3988
// field under the finger and jump the target row. Also skip work
3989
// when the viewport has not actually changed.
3990
if (!(app.core && app.core.drag))
3991
{ layoutIfNeeded(); }
3593
3992
for (var i = 0; i < app.cells.length; i++)
3594
3993
{
3595
3994
var col = i % 10;
@@ -3619,15 +4018,58 @@ function tickView (dt)
3619
4018
3620
4019
/* ---------- pointer input (mouse + touch) --------------------------- */
3621
4020
/*
3622
* Both mouse and touch use Pointer Events. After pointerdown we listen
3623
* on window, so a drag that leaves the canvas (needed to launch a
3624
* block at the top of a full column) still reaches moveDrag. We do
3625
* not treat lostpointercapture as the end of a drag: on some touch
3626
* browsers that fires mid-gesture and would cancel a launch.
4021
* Mouse uses Pointer Events. Touch uses Touch Events as the authority:
4022
* on a real phone, pointer clientX/Y can disagree with the visual
4023
* finger (zoomed visual viewport, gesture takeover, capture cancel)
4024
* while changedTouches stays correct. When a touch is active, pointer
4025
* move/up/cancel are ignored so a poisoned pointer coordinate cannot
4026
* walk the block to the top or bottom of the stack (t1106).
4027
*
4028
* After a down we also listen on the document, so a drag that leaves
4029
* the canvas (needed to launch a block at the top of a full column)
4030
* still reaches moveDrag. We do not call setPointerCapture: on mobile
4031
* that is often cancelled when the browser takes the gesture, which
4032
* would fire pointercancel mid-drag.
4033
*
4034
* A non-passive document touchstart/touchmove preventDefault during
4035
* play stops scrolling, rubber-banding and the pointercancel that
4036
* follows. pointercancel / touchcancel end the drag without launch.
4037
*
4038
* ?debug=drag (or localStorage meteors-debug-drag=1) shows an overlay.
3627
4039
*/
3628
4040
3629
4041
var boundCanvas = null;
3630
4042
var windowListening = false;
4043
var touchListening = false;
4044
var lastDragDebug = null;
4045
4046
function isDragDebugEnabled ()
4047
{
4048
try
4049
{
4050
var q = window.location && window.location.search;
4051
if (q && /(?:\?|&)debug=drag(?:&|$)/.test(q))
4052
{ return true; }
4053
if (window.localStorage && window.localStorage.getItem('meteors-debug-drag') === '1')
4054
{ return true; }
4055
}
4056
catch (err) { /* ignore */ }
4057
return false;
4058
}
4059
4060
function eventClient (e)
4061
{
4062
var t = null;
4063
if (e.changedTouches && e.changedTouches.length)
4064
{ t = e.changedTouches[0]; }
4065
else if (e.touches && e.touches.length)
4066
{ t = e.touches[0]; }
4067
if (t)
4068
{ return { x: t.clientX, y: t.clientY, id: t.identifier, src: 'touch' }; }
4069
if (e.clientX != null && e.clientY != null)
4070
{ return { x: e.clientX, y: e.clientY, id: e.pointerId, src: 'pointer' }; }
4071
return null;
4072
}
3631
4073
3632
4074
function addWindowPointer ()
3633
4075
{
@@ -3635,7 +4077,7 @@ function addWindowPointer ()
3635
4077
{ return; }
3636
4078
window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false });
3637
4079
window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false });
3638
window.addEventListener('pointercancel', onPointerUp, { capture: true, passive: false });
4080
window.addEventListener('pointercancel', onPointerCancel, { capture: true, passive: false });
3639
4081
windowListening = true;
3640
4082
}
3641
4083
@@ -3645,28 +4087,59 @@ function removeWindowPointer ()
3645
4087
{ return; }
3646
4088
window.removeEventListener('pointermove', onPointerMove, { capture: true });
3647
4089
window.removeEventListener('pointerup', onPointerUp, { capture: true });
3648
window.removeEventListener('pointercancel', onPointerUp, { capture: true });
4090
window.removeEventListener('pointercancel', onPointerCancel, { capture: true });
3649
4091
windowListening = false;
3650
4092
}
3651
4093
4094
function addTouchListeners ()
4095
{
4096
if (touchListening)
4097
{ return; }
4098
var opts = { capture: true, passive: false };
4099
document.addEventListener('touchstart', onTouchStart, opts);
4100
document.addEventListener('touchmove', onTouchMove, opts);
4101
document.addEventListener('touchend', onTouchEnd, opts);
4102
document.addEventListener('touchcancel', onTouchCancel, opts);
4103
touchListening = true;
4104
}
4105
4106
function removeTouchListeners ()
4107
{
4108
if (!touchListening)
4109
{ return; }
4110
var opts = { capture: true };
4111
document.removeEventListener('touchstart', onTouchStart, opts);
4112
document.removeEventListener('touchmove', onTouchMove, opts);
4113
document.removeEventListener('touchend', onTouchEnd, opts);
4114
document.removeEventListener('touchcancel', onTouchCancel, opts);
4115
touchListening = false;
4116
}
4117
3652
4118
function bindPointer (canvas)
3653
4119
{
3654
4120
unbindPointer();
3655
4121
boundCanvas = canvas;
3656
4122
canvas.addEventListener('pointerdown', onPointerDown, { passive: false });
3657
4123
canvas.style.touchAction = 'none';
4124
canvas.style.position = 'fixed';
3658
4125
canvas.style.cursor = 'pointer';
3659
4126
canvas.style.webkitUserSelect = 'none';
3660
4127
canvas.style.userSelect = 'none';
4128
addTouchListeners();
4129
if (app.debugDrag)
4130
{ ensureDragDebugOverlay(); }
3661
4131
}
3662
4132
3663
4133
function unbindPointer ()
3664
4134
{
3665
4135
removeWindowPointer();
4136
removeTouchListeners();
3666
4137
if (boundCanvas)
3667
4138
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }
3668
4139
boundCanvas = null;
3669
4140
app.pointerId = null;
4141
app.pointerType = null;
4142
app.activeTouchId = null;
3670
4143
}
3671
4144
3672
4145
function fieldFromEvent (e)
@@ -3677,34 +4150,94 @@ function fieldFromEvent (e)
3677
4150
var rect = canvas.getBoundingClientRect();
3678
4151
if (rect.width <= 0 || rect.height <= 0)
3679
4152
{ return null; }
4153
var pt = eventClient(e);
4154
if (!pt)
4155
{ return null; }
3680
4156
// Map through the CSS box only. Using canvas.width / app.tile would
3681
4157
// scale the pointer by the backing-store ratio whenever that differs
3682
4158
// from the layout size (high-DPR devices), so a short finger move
3683
4159
// would look like a long swipe.
3684
4160
var width = Planet.FIELD_WIDTH;
3685
4161
var height = Planet.FIELD_HEIGHT;
3686
var col = ((e.clientX - rect.left) / rect.width) * width;
3687
var fieldY = (1 - (e.clientY - rect.top) / rect.height) * height;
3688
return { col: col, y: fieldY };
4162
var col = ((pt.x - rect.left) / rect.width) * width;
4163
var fieldY = (1 - (pt.y - rect.top) / rect.height) * height;
4164
return { col: col, y: fieldY, src: pt.src, clientX: pt.x, clientY: pt.y, rect: rect };
4165
}
4166
4167
function playingForDrag ()
4168
{
4169
return app.core && app.core.state === 'playing';
4170
}
4171
4172
function startDragFromEvent (e)
4173
{
4174
var pos = fieldFromEvent(e);
4175
if (!pos)
4176
{ return; }
4177
if (app.core.drag)
4178
{
4179
app.core.moveDrag(pos.y);
4180
logDragDebug(e, pos, 'move-already');
4181
return;
4182
}
4183
if (app.core.beginDrag(pos.col, pos.y) && app.core.drag)
4184
{ startPressTween(app.core.drag.blockId); }
4185
logDragDebug(e, pos, 'down');
4186
}
4187
4188
function moveDragFromEvent (e)
4189
{
4190
if (!app.core || !app.core.drag)
4191
{ return; }
4192
var pos = fieldFromEvent(e);
4193
if (pos)
4194
{ app.core.moveDrag(pos.y); }
4195
logDragDebug(e, pos, 'move');
4196
}
4197
4198
function finishDrag (opts)
4199
{
4200
opts = opts || {};
4201
app.pointerId = null;
4202
app.pointerType = null;
4203
app.activeTouchId = null;
4204
removeWindowPointer();
4205
if (boundCanvas)
4206
{
4207
try
4208
{
4209
if (opts.pointerId != null && boundCanvas.releasePointerCapture)
4210
{ boundCanvas.releasePointerCapture(opts.pointerId); }
4211
}
4212
catch (err) { /* ignore */ }
4213
}
4214
startReleaseTween();
4215
if (app.core && app.core.drag)
4216
{
4217
if (opts.noLaunch)
4218
{ app.core.drag.launchIntent = false; }
4219
app.core.endDrag();
4220
}
4221
else if (app.core)
4222
{ app.core.endDrag(); }
4223
logDragDebug(null, null, opts.noLaunch ? 'cancel' : 'up');
3689
4224
}
3690
4225
3691
4226
function onPointerDown (e)
3692
4227
{
3693
if (!app.core || app.core.state !== 'playing')
4228
if (!playingForDrag())
3694
4229
{ return; }
3695
if (app.pointerId != null)
4230
if (app.pointerId != null && e.pointerId !== app.pointerId)
3696
4231
{ return; }
3697
e.preventDefault();
4232
if (e.preventDefault)
4233
{ e.preventDefault(); }
3698
4234
app.pointerId = e.pointerId;
3699
try
3700
{ boundCanvas.setPointerCapture(e.pointerId); }
3701
catch (err) { /* ignore */ }
4235
app.pointerType = e.pointerType || 'mouse';
3702
4236
addWindowPointer();
3703
var pos = fieldFromEvent(e);
3704
// Instant grab, no long-press or hold delay: the block is taken on
3705
// pointerdown and the click tween starts in the same moment.
3706
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)
3707
{ startPressTween(app.core.drag.blockId); }
4237
// If a touch is already in charge, do not reset the grab from pointerdown.
4238
if (app.activeTouchId != null)
4239
{ return; }
4240
startDragFromEvent(e);
3708
4241
}
3709
4242
3710
4243
function onPointerMove (e)
@@ -3713,10 +4246,20 @@ function onPointerMove (e)
3713
4246
{ return; }
3714
4247
if (!app.core || !app.core.drag)
3715
4248
{ return; }
3716
e.preventDefault();
3717
var pos = fieldFromEvent(e);
3718
if (pos)
3719
{ app.core.moveDrag(pos.y); }
4249
// Touch coordinates are the authority; ignore pointer moves for a
4250
// live touch so a wrong clientY cannot walk the whole column.
4251
// Only skip when touchstart has actually arrived — some environments
4252
// deliver touch-pointer events without a TouchEvent stream.
4253
if (app.activeTouchId != null)
4254
{
4255
if (e.preventDefault)
4256
{ e.preventDefault(); }
4257
logDragDebug(e, fieldFromEvent(e), 'pointer-ignored');
4258
return;
4259
}
4260
if (e.preventDefault)
4261
{ e.preventDefault(); }
4262
moveDragFromEvent(e);
3720
4263
}
3721
4264
3722
4265
function onPointerUp (e)
@@ -3725,21 +4268,155 @@ function onPointerUp (e)
3725
4268
{ return; }
3726
4269
if (e && e.pointerId != null && e.pointerId !== app.pointerId)
3727
4270
{ return; }
3728
if (e)
4271
if (app.activeTouchId != null)
4272
{
4273
// touchend will finish the drag.
4274
if (e && e.preventDefault)
4275
{ e.preventDefault(); }
4276
logDragDebug(e, fieldFromEvent(e), 'pointer-up-ignored');
4277
return;
4278
}
4279
if (e && e.preventDefault)
3729
4280
{ e.preventDefault(); }
3730
app.pointerId = null;
3731
removeWindowPointer();
3732
if (boundCanvas)
4281
finishDrag({ pointerId: e && e.pointerId });
4282
}
4283
4284
function onPointerCancel (e)
4285
{
4286
if (app.pointerId == null)
4287
{ return; }
4288
if (e && e.pointerId != null && e.pointerId !== app.pointerId)
4289
{ return; }
4290
if (app.activeTouchId != null)
3733
4291
{
3734
try
3735
{ boundCanvas.releasePointerCapture(e && e.pointerId); }
3736
catch (err) { /* ignore */ }
4292
if (e && e.preventDefault)
4293
{ e.preventDefault(); }
4294
logDragDebug(e, fieldFromEvent(e), 'pointer-cancel-ignored');
4295
return;
3737
4296
}
3738
startReleaseTween();
3739
if (app.core)
3740
{ app.core.endDrag(); }
4297
if (e && e.preventDefault)
4298
{ e.preventDefault(); }
4299
// A cancelled gesture is not a launch pull.
4300
finishDrag({ pointerId: e && e.pointerId, noLaunch: true });
4301
}
4302
4303
function touchById (list, id)
4304
{
4305
if (!list)
4306
{ return null; }
4307
for (var i = 0; i < list.length; i++)
4308
{
4309
if (list[i].identifier === id)
4310
{ return list[i]; }
4311
}
4312
return null;
3741
4313
}
3742
4314
4315
function onTouchStart (e)
4316
{
4317
if (!playingForDrag())
4318
{ return; }
4319
if (app.activeTouchId != null)
4320
{
4321
if (e.preventDefault)
4322
{ e.preventDefault(); }
4323
return;
4324
}
4325
var t = e.changedTouches && e.changedTouches[0];
4326
if (!t)
4327
{ return; }
4328
if (e.preventDefault)
4329
{ e.preventDefault(); }
4330
app.activeTouchId = t.identifier;
4331
addWindowPointer();
4332
startDragFromEvent(e);
4333
}
4334
4335
function onTouchMove (e)
4336
{
4337
if (app.activeTouchId == null)
4338
{ return; }
4339
if (!touchById(e.changedTouches, app.activeTouchId)
4340
&& !touchById(e.touches, app.activeTouchId))
4341
{ return; }
4342
if (e.preventDefault)
4343
{ e.preventDefault(); }
4344
moveDragFromEvent(e);
4345
}
4346
4347
function onTouchEnd (e)
4348
{
4349
if (app.activeTouchId == null)
4350
{ return; }
4351
if (!touchById(e.changedTouches, app.activeTouchId))
4352
{ return; }
4353
if (e.preventDefault)
4354
{ e.preventDefault(); }
4355
finishDrag({});
4356
}
4357
4358
function onTouchCancel (e)
4359
{
4360
if (app.activeTouchId == null)
4361
{ return; }
4362
if (!touchById(e.changedTouches, app.activeTouchId))
4363
{ return; }
4364
if (e.preventDefault)
4365
{ e.preventDefault(); }
4366
finishDrag({ noLaunch: true });
4367
}
4368
4369
function ensureDragDebugOverlay ()
4370
{
4371
if (document.getElementById('drag-debug'))
4372
{ return; }
4373
var el = document.createElement('div');
4374
el.id = 'drag-debug';
4375
el.setAttribute('aria-live', 'polite');
4376
document.body.appendChild(el);
4377
}
4378
4379
function logDragDebug (e, pos, phase)
4380
{
4381
if (!app.debugDrag)
4382
{ return; }
4383
ensureDragDebugOverlay();
4384
var el = document.getElementById('drag-debug');
4385
if (!el)
4386
{ return; }
4387
var block = null;
4388
if (app.core && app.core.drag)
4389
{ block = app.core.blockById(app.core.drag.blockId); }
4390
var pt = e ? eventClient(e) : null;
4391
var evType = e ? e.type : phase;
4392
var info = {
4393
phase: phase,
4394
ev: evType,
4395
src: pos && pos.src,
4396
fieldY: pos && Math.round(pos.y * 100) / 100,
4397
col: pos && Math.round(pos.col * 100) / 100,
4398
cy: pt && Math.round(pt.y * 10) / 10,
4399
blockY: block && Math.round(block.y * 100) / 100,
4400
target: block && Math.floor((pos && pos.y) + 1e-12),
4401
launch: app.core && app.core.drag && app.core.drag.launchIntent,
4402
touchId: app.activeTouchId,
4403
pid: app.pointerId,
4404
ptype: app.pointerType
4405
};
4406
lastDragDebug = info;
4407
el.textContent = 'drag ' + phase
4408
+ ' ev=' + evType
4409
+ ' src=' + (info.src || '-')
4410
+ ' y=' + (info.fieldY != null ? info.fieldY : '-')
4411
+ ' → ' + (info.target != null ? info.target : '-')
4412
+ ' block=' + (info.blockY != null ? info.blockY : '-')
4413
+ (info.launch ? ' LAUNCH' : '')
4414
+ ' tId=' + info.touchId
4415
+ ' pId=' + info.pid;
4416
el.hidden = false;
4417
}
4418
4419
3743
4420
function hideEl (id)
3744
4421
{
3745
4422
document.getElementById(id).hidden = true;
@@ -668,8 +668,8 @@ var LAUNCH_PULL = 0.35;668
// Minimal accumulated vertical pointer travel (tiles) to swap one step.668
// Minimal accumulated vertical pointer travel (tiles) to swap one step.669
// No hold delay: the first swap fires as soon as this distance is reached.669
// No hold delay: the first swap fires as soon as this distance is reached.670
var SWAP_DISTANCE = 0.25;670
var SWAP_DISTANCE = 0.25;671
// Grab a still block if the pointer is at most this far (tiles) from it.671
// Fallback snap (tiles) when the press cell is empty: nearest block in the column.672
var SNAP_Y = 0.65;672
var SNAP_Y = 0.5;673
// Horizontal slop (tiles) for snapping to a neighbouring column on press.673
// Horizontal slop (tiles) for snapping to a neighbouring column on press.674
var HORIZONTAL_SLOP = 0.35;674
var HORIZONTAL_SLOP = 0.35;675
675
@@ -1073,12 +1073,29 @@ GameCore.prototype.attachToFlyingCluster = function (block, flyer)1073
if (other.y + 1 > roof)1073
if (other.y + 1 > roof)1074
{ roof = other.y + 1; }1074
{ roof = other.y + 1; }1075
}1075
}1076
var oldN = this.clusterMembers(cluster.id).length;1077
var fallingVy = block.vy;1076
block.y = roof;1078
block.y = roof;1077
block.state = STATE_FLYING;1079
block.state = STATE_FLYING;1078
block.clusterId = cluster.id;1080
block.clusterId = cluster.id;1079
block.vy = cluster.vy;1080
block.flame = false;1081
block.flame = false;1081
block.settleTime = 0;1082
block.settleTime = 0;1083
// A cluster born from a thrown block averages in falling cargo1084
// the same way two flying clusters merge (t1108). Other rockets1085
// keep their velocity and just pick the cargo up.1086
if (cluster.fromManual)1087
{1088
var newN = oldN + 1;1089
cluster.vy = (cluster.vy * oldN + fallingVy) / newN;1090
cluster.decel = ((cluster.decel || 0) * oldN) / newN;1091
cluster.jumpHeight = ((cluster.jumpHeight || 0) * oldN) / newN;1092
cluster.phase = cluster.vy > 1e-10 ? 'up' : 'down';1093
var members = this.clusterMembers(cluster.id);1094
for (var m = 0; m < members.length; m++)1095
{ members[m].vy = cluster.vy; }1096
}1097
else1098
{ block.vy = cluster.vy; }1082
return true;1099
return true;1083
};1100
};1084
1101
@@ -1141,6 +1158,7 @@ GameCore.prototype.tick = function (dt)1141
this.integrateClusters(step);1158
this.integrateClusters(step);1142
this.mergeFlyingClusters();1159
this.mergeFlyingClusters();1143
this.integrateManual(step);1160
this.integrateManual(step);1161
this.mergeAnyManualFallingOverlaps();1144
this.cullOffTop();1162
this.cullOffTop();1145
left -= step;1163
left -= step;1146
}1164
}@@ -1222,6 +1240,12 @@ GameCore.prototype.integrateFalling = function (dt)1222
{ continue; }1240
{ continue; }1223
this.accelerateFall(block, dt);1241
this.accelerateFall(block, dt);1224
var nextY = block.y + block.vy * dt;1242
var nextY = block.y + block.vy * dt;1243
var manual = this.manualInColumnHit(block, block.y, nextY);1244
if (manual)1245
{1246
this.mergeManualWithFalling(manual, this.contiguousFallingInColumn(manual.col, manual, [block]));1247
continue;1248
}1225
var flyer = this.highestFlyingBelow(block);1249
var flyer = this.highestFlyingBelow(block);1226
if (flyer && nextY <= flyer.y + 1 + 1e-9)1250
if (flyer && nextY <= flyer.y + 1 + 1e-9)1227
{1251
{@@ -1622,6 +1646,8 @@ GameCore.prototype.integrateManual = function (dt)1622
block.vy = proxy.vy;1646
block.vy = proxy.vy;1623
block.manualPhase = proxy.phase;1647
block.manualPhase = proxy.phase;1624
block.y = Math.min(nextY, block.manualTarget);1648
block.y = Math.min(nextY, block.manualTarget);1649
if (this.tryMergeManualWithFalling(block, oldY, block.y))1650
{ continue; }1625
if (block.y >= block.manualTarget - 1e-12)1651
if (block.y >= block.manualTarget - 1e-12)1626
{1652
{1627
block.y = block.manualTarget;1653
block.y = block.manualTarget;@@ -1655,11 +1681,207 @@ GameCore.prototype.integrateManual = function (dt)1655
else1681
else1656
{1682
{1657
block.y = Math.min(nextY, this.height - 1);1683
block.y = Math.min(nextY, this.height - 1);1684
if (this.tryMergeManualWithFalling(block, block.y - dy, block.y))1685
{ continue; }1658
}1686
}1659
}1687
}1660
}1688
}1661
};1689
};1662
1690
1691
/**1692
* If the manual throw (or its path this step) hits falling blocks, merge1693
* them into a flying cluster with mass-weighted velocity, the same rule1694
* as two flying clusters colliding (t1108). Returns true if merged.1695
*/1696
GameCore.prototype.tryMergeManualWithFalling = function (manual, oldY, newY)1697
{1698
if (!manual || manual.state !== STATE_MANUAL)1699
{ return false; }1700
var hits = this.fallingHitsOnManualPath(manual, oldY, newY);1701
if (!hits.length)1702
{ return false; }1703
this.mergeManualWithFalling(manual, hits);1704
return true;1705
};1706
1707
GameCore.prototype.manualInColumnHit = function (falling, oldY, newY)1708
{1709
var lo = Math.min(oldY, newY);1710
var hi = Math.max(oldY, newY) + 1;1711
for (var i = 0; i < this.blocks.length; i++)1712
{1713
var other = this.blocks[i];1714
if (other === falling || other.col !== falling.col)1715
{ continue; }1716
if (other.state !== STATE_MANUAL)1717
{ continue; }1718
if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9)1719
{ return other; }1720
}1721
return null;1722
};1723
1724
GameCore.prototype.fallingHitsOnManualPath = function (manual, oldY, newY)1725
{1726
var lo = Math.min(oldY, newY);1727
var hi = Math.max(oldY, newY) + 1;1728
var hits = [];1729
for (var i = 0; i < this.blocks.length; i++)1730
{1731
var other = this.blocks[i];1732
if (other === manual || other.col !== manual.col)1733
{ continue; }1734
if (other.state !== STATE_FALLING)1735
{ continue; }1736
if (other.y < hi - 1e-9 && other.y + 1 > lo + 1e-9)1737
{ hits.push(other); }1738
}1739
if (!hits.length)1740
{ return hits; }1741
return this.contiguousFallingInColumn(manual.col, manual, hits);1742
};1743
1744
/**1745
* Every falling block in `col` that overlaps `anchor` or a hit already1746
* in `seed`, plus a contiguous falling stack sitting on them.1747
*/1748
GameCore.prototype.contiguousFallingInColumn = function (col, anchor, seed)1749
{1750
var falling = [];1751
for (var i = 0; i < this.blocks.length; i++)1752
{1753
if (this.blocks[i].col === col && this.blocks[i].state === STATE_FALLING)1754
{ falling.push(this.blocks[i]); }1755
}1756
falling.sort(function (a, b) { return a.y - b.y; });1757
var include = Object.create(null);1758
var queue = [];1759
function enqueue (b)1760
{1761
if (!b || include[b.id])1762
{ return; }1763
include[b.id] = true;1764
queue.push(b);1765
}1766
if (seed)1767
{1768
for (var s = 0; s < seed.length; s++)1769
{ enqueue(seed[s]); }1770
}1771
// Also include falling that already overlap the anchor.1772
if (anchor)1773
{1774
for (var i = 0; i < falling.length; i++)1775
{1776
if (falling[i].y < anchor.y + 1 - 1e-9 && anchor.y < falling[i].y + 1 - 1e-9)1777
{ enqueue(falling[i]); }1778
}1779
}1780
for (var q = 0; q < queue.length; q++)1781
{1782
var a = queue[q];1783
for (var i = 0; i < falling.length; i++)1784
{1785
var b = falling[i];1786
if (include[b.id])1787
{ continue; }1788
if (a.y < b.y + 1 + 1e-9 && b.y < a.y + 1 + 1e-9)1789
{ enqueue(b); }1790
}1791
}1792
var out = [];1793
for (var i = 0; i < falling.length; i++)1794
{1795
if (include[falling[i].id])1796
{ out.push(falling[i]); }1797
}1798
return out;1799
};1800
1801
/**1802
* Merge a thrown (manual) block with falling blocks into one flying1803
* cluster. Velocity, deceleration and jumpHeight are the mass-weighted1804
* averages by block count, identical to mergeClusterGroup. The thrown1805
* block stops being manual and the group follows normal cluster physics.1806
* fromManual marks the cluster so later falling cargo also averages in.1807
*/1808
GameCore.prototype.mergeManualWithFalling = function (manual, fallingList)1809
{1810
if (!manual || manual.state !== STATE_MANUAL)1811
{ return null; }1812
var blocks = [manual];1813
for (var i = 0; i < fallingList.length; i++)1814
{1815
if (fallingList[i] && fallingList[i].state === STATE_FALLING && blocks.indexOf(fallingList[i]) < 0)1816
{ blocks.push(fallingList[i]); }1817
}1818
if (blocks.length < 2)1819
{ return null; }1820
var n = blocks.length;1821
var vySum = 0;1822
var decelSum = 0;1823
var hSum = 0;1824
for (var i = 0; i < n; i++)1825
{1826
var b = blocks[i];1827
vySum += b.vy;1828
if (b === manual)1829
{1830
decelSum += manual.manualDecel || 0;1831
hSum += manual.manualJumpHeight != null ? manual.manualJumpHeight1832
: Math.max(0, (manual.manualTarget || this.height - 1) - manual.y);1833
}1834
}1835
var cluster = {1836
id: this.nextClusterId++,1837
jumpHeight: hSum / n,1838
vy: vySum / n,1839
decel: decelSum / n,1840
phase: (vySum / n) > 1e-10 ? 'up' : 'down',1841
fromManual: true1842
};1843
this.clusters[cluster.id] = cluster;1844
for (var i = 0; i < n; i++)1845
{1846
var b = blocks[i];1847
b.clusterId = cluster.id;1848
b.state = STATE_FLYING;1849
b.vy = cluster.vy;1850
b.flame = b.color === COLOR_GRAY;1851
b.manualPhase = null;1852
b.manualTarget = null;1853
b.manualDecel = 0;1854
b.manualJumpHeight = 0;1855
b.pushedClusters = null;1856
b.settleTime = 0;1857
}1858
if (this.drag && this.drag.blockId === manual.id)1859
{ this.drag = null; }1860
this.packClusterOverlaps(cluster);1861
this.checkChainRelaunch(cluster);1862
return cluster;1863
};1864
1865
/** Safety net: any remaining manual/falling overlaps after the substep. */1866
GameCore.prototype.mergeAnyManualFallingOverlaps = function ()1867
{1868
var manuals = [];1869
for (var i = 0; i < this.blocks.length; i++)1870
{1871
if (this.blocks[i].state === STATE_MANUAL)1872
{ manuals.push(this.blocks[i]); }1873
}1874
for (var m = 0; m < manuals.length; m++)1875
{1876
var manual = manuals[m];1877
if (manual.state !== STATE_MANUAL)1878
{ continue; }1879
var hits = this.contiguousFallingInColumn(manual.col, manual);1880
if (hits.length)1881
{ this.mergeManualWithFalling(manual, hits); }1882
}1883
};1884
1663
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)1885
GameCore.prototype.checkManualPush = function (manual, oldY, nextY)1664
{1886
{1665
if (!manual.pushedClusters)1887
if (!manual.pushedClusters)@@ -1791,10 +2013,12 @@ GameCore.prototype.startManualLaunch = function (block)1791
{2013
{1792
var target = this.height - 1;2014
var target = this.height - 1;1793
var height = Math.max(0.05, target - block.y);2015
var height = Math.max(0.05, target - block.y);2016
this.leaveCluster(block);1794
block.state = STATE_MANUAL;2017
block.state = STATE_MANUAL;1795
block.pushedClusters = Object.create(null);2018
block.pushedClusters = Object.create(null);1796
block.manualTarget = target;2019
block.manualTarget = target;1797
block.manualPhase = 'up';2020
block.manualPhase = 'up';2021
block.manualJumpHeight = height;1798
block.vy = PlanetMod.launchVelocity(this.planet, height);2022
block.vy = PlanetMod.launchVelocity(this.planet, height);1799
block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);2023
block.manualDecel = PlanetMod.launchDeceleration(this.planet, height);1800
block.flame = false;2024
block.flame = false;@@ -1968,7 +2192,11 @@ GameCore.prototype.findMatchLines = function ()1968
for (var i = 0; i < this.blocks.length; i++)2192
for (var i = 0; i < this.blocks.length; i++)1969
{2193
{1970
var block = this.blocks[i];2194
var block = this.blocks[i];1971
if (block.state === STATE_STILL && block.color >= 0)2195
// Coloured cargo in a just-landed cluster (SETTLING) can match2196
// with already-resting blocks immediately (t1110). Greys have no2197
// colour and wait until they recolor; after release they are STILL.2198
if ((block.state === STATE_STILL || block.state === STATE_SETTLING)2199
&& block.color >= 0)1972
{ candidates.push(block); }2200
{ candidates.push(block); }1973
}2201
}1974
return this.collectLines(candidates, true);2202
return this.collectLines(candidates, true);@@ -2278,48 +2506,103 @@ GameCore.prototype.beginDrag = function (col, y)2278
};2506
};2279
2507
2280
/**2508
/**2281
* Instant grab: the nearest still, coloured block to (col, y), allowing2509
* Instant grab, cell-based (t1109). The press is mapped to the grid cell2282
* a small vertical miss and a horizontal slop into a neighbouring2510
* floor(col), floor(y); any press inside that cell — corners, padding,2283
* column. `col` may be fractional (pointer x in tiles). No hold delay.2511
* gaps between sprites, a visually tweened or bouncing sprite — grabs2512
* the block that occupies it. This uses simulation cells, not the drawn2513
* sprite bounds. If no block sits in that cell (mid-animation, a block2514
* whose y is off the integer), fall back to the nearest grabbable block2515
* in the same column within SNAP_Y (about half a tile). A small2516
* horizontal slop still consults a neighbouring column when the press2517
* is near the edge and the primary cell is empty.2284
*/2518
*/2285
GameCore.prototype.snapDragBlock = function (col, y)2519
GameCore.prototype.snapDragBlock = function (col, y)2286
{2520
{2287
var raw = col;2521
var raw = col;2288
var c0 = Math.floor(raw + 1e-9);2522
var c0 = Math.floor(raw + 1e-9);2523
var row = Math.floor(y + 1e-9);2289
var frac = raw - c0;2524
var frac = raw - c0;2290
var candidates = [];2525
2291
if (c0 >= 0 && c0 < this.width)2526
if (c0 >= 0 && c0 < this.width && this.columnAllowsDrag(c0))2292
{ candidates.push(c0); }2527
{2293
// Only consult a neighbour when the pointer is actually near the edge2528
var exact = this.draggableInCell(c0, row);2294
// (a whole-number col, as in unit tests, stays in that column).2529
if (exact)2530
{ return exact; }2531
var near = this.nearestDraggableInColumn(c0, y, SNAP_Y);2532
if (near)2533
{ return near; }2534
}2535
2536
// Primary cell empty: allow a small horizontal slop into a neighbour.2537
var neighbours = [];2295
if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0)2538
if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0)2296
{ candidates.push(c0 - 1); }2539
{ neighbours.push(c0 - 1); }2297
if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width)2540
if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width)2298
{ candidates.push(c0 + 1); }2541
{ neighbours.push(c0 + 1); }2542
var best = null;2543
var bestDist = Infinity;2544
for (var n = 0; n < neighbours.length; n++)2545
{2546
var cc = neighbours[n];2547
if (!this.columnAllowsDrag(cc))2548
{ continue; }2549
var hit = this.draggableInCell(cc, row) || this.nearestDraggableInColumn(cc, y, SNAP_Y);2550
if (!hit)2551
{ continue; }2552
var dist = this.pointerDistToBlock(hit, raw, y);2553
if (dist < bestDist)2554
{2555
bestDist = dist;2556
best = hit;2557
}2558
}2559
return best;2560
};2299
2561
2562
/** The draggable block that occupies grid cell (col, row), if any. */2563
GameCore.prototype.draggableInCell = function (col, row)2564
{2565
var best = null;2566
var bestOverlap = 0;2567
var list = this.blocksInColumn(col);2568
for (var i = 0; i < list.length; i++)2569
{2570
var block = list[i];2571
if (!this.isDraggableBlock(block))2572
{ continue; }2573
var lo = Math.max(block.y, row);2574
var hi = Math.min(block.y + 1, row + 1);2575
var overlap = hi - lo;2576
if (overlap > bestOverlap + 1e-12)2577
{2578
bestOverlap = overlap;2579
best = block;2580
}2581
}2582
if (best && bestOverlap > 1e-9)2583
{ return best; }2584
return null;2585
};2586
2587
/** Nearest draggable block in `col` whose footprint is within `maxDist` of `y`. */2588
GameCore.prototype.nearestDraggableInColumn = function (col, y, maxDist)2589
{2300
var best = null;2590
var best = null;2301
var bestDist = Infinity;2591
var bestDist = Infinity;2302
for (var c = 0; c < candidates.length; c++)2592
var list = this.blocksInColumn(col);2593
for (var i = 0; i < list.length; i++)2303
{2594
{2304
var cc = candidates[c];2595
var block = list[i];2305
if (!this.isColumnStill(cc))2596
if (!this.isDraggableBlock(block))2306
{ continue; }2597
{ continue; }2307
var list = this.blocksInColumn(cc);2598
var dist = this.pointerDistToBlock(block, col + 0.5, y);2308
for (var i = 0; i < list.length; i++)2599
if (dist < bestDist)2309
{2600
{2310
var block = list[i];2601
bestDist = dist;2311
if (block.state !== STATE_STILL || block.color < 0)2602
best = block;2312
{ continue; }2313
var dist = this.pointerDistToBlock(block, raw, y);2314
// Prefer the column the pointer is actually in on a tie.2315
if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0))2316
{2317
bestDist = dist;2318
best = block;2319
}2320
}2603
}2321
}2604
}2322
if (!best || bestDist > SNAP_Y)2605
if (!best || bestDist > maxDist)2323
{ return null; }2606
{ return null; }2324
return best;2607
return best;2325
};2608
};@@ -2348,12 +2631,12 @@ GameCore.prototype.moveDrag = function (y)2348
if (!this.drag)2631
if (!this.drag)2349
{ return; }2632
{ return; }2350
var block = this.blockById(this.drag.blockId);2633
var block = this.blockById(this.drag.blockId);2351
if (!block || block.state !== STATE_STILL)2634
if (!block || !this.isDraggableBlock(block))2352
{2635
{2353
this.drag = null;2636
this.drag = null;2354
return;2637
return;2355
}2638
}2356
if (!this.isColumnStill(block.col))2639
if (!this.columnAllowsDrag(block.col))2357
{2640
{2358
this.drag = null;2641
this.drag = null;2359
return;2642
return;@@ -2483,15 +2766,101 @@ GameCore.prototype.swapBlocks = function (a, b)2483
var y = a.y;2766
var y = a.y;2484
a.y = b.y;2767
a.y = b.y;2485
b.y = y;2768
b.y = y;2769
this.transferLandedMembership(a, b);2770
};2771
2772
/**2773
* A landed cluster (grey rockets or linked cargo resting on the ground)2774
* is a swappable part of the contiguous grounded stack. Flying, falling2775
* and manual blocks are not. A locked column is not.2776
*/2777
GameCore.prototype.isDraggableBlock = function (block)2778
{2779
if (!block)2780
{ return false; }2781
if (block.state === STATE_LOCKED || block.state === STATE_FLYING2782
|| block.state === STATE_FALLING || block.state === STATE_MANUAL)2783
{ return false; }2784
if (block.clusterId != null)2785
{2786
var cluster = this.clusters[block.clusterId];2787
// Flying (rising / descending) clusters stay non-draggable.2788
if (cluster && cluster.phase !== 'landed')2789
{ return false; }2790
}2791
return block.state === STATE_STILL || block.state === STATE_SETTLING;2792
};2793
2794
/**2795
* The grounded contiguous stack in this column can be dragged, even if a2796
* flying cluster or a falling spawn occupies the air above it. Only a2797
* locked column is blocked as a whole.2798
*/2799
GameCore.prototype.columnAllowsDrag = function (col)2800
{2801
return !this.isColumnLocked(col);2802
};2803
2804
GameCore.prototype.landedClusterId = function (block)2805
{2806
if (!block || block.clusterId == null)2807
{ return null; }2808
var cluster = this.clusters[block.clusterId];2809
if (!cluster || cluster.phase !== 'landed')2810
{ return null; }2811
return block.clusterId;2812
};2813
2814
/**2815
* After a swap: leaving a landed cluster clears membership; joining one2816
* makes the block part of that rigid group. Same-cluster swaps only2817
* exchange positions. See docs/ASSUMPTIONS.md (t1107).2818
*/2819
GameCore.prototype.transferLandedMembership = function (a, b)2820
{2821
var ca = this.landedClusterId(a);2822
var cb = this.landedClusterId(b);2823
if (ca === cb)2824
{ return; }2825
// Join first so leaveCluster does not delete a cluster that still2826
// has the incoming member.2827
this.applyLandedMembership(b, ca);2828
this.applyLandedMembership(a, cb);2829
};2830
2831
GameCore.prototype.applyLandedMembership = function (block, clusterId)2832
{2833
if (clusterId == null)2834
{2835
var wasGrey = block.color === COLOR_GRAY;2836
var wasSettling = block.state === STATE_SETTLING;2837
this.leaveCluster(block);2838
if (wasGrey && wasSettling)2839
{2840
// Keep settling and recolor on its own, same remaining timer.2841
block.state = STATE_SETTLING;2842
}2843
else if (wasSettling)2844
{2845
block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;2846
}2847
return;2848
}2849
if (!this.clusters[clusterId])2850
{ return; }2851
block.clusterId = clusterId;2852
block.flame = false;2853
if (block.state === STATE_STILL || block.state === STATE_SETTLING)2854
{ block.state = STATE_SETTLING; }2486
};2855
};2487
2856
2488
/**2857
/**2489
* Test helper: stepwise-swap the block at (col, fromRow) to toRow.2858
* Test helper: stepwise-swap the block at (col, fromRow) to toRow.2490
* Returns the moved block, or null if the column is not still.2859
* Returns the moved block, or null if the column does not allow a drag.2491
*/2860
*/2492
GameCore.prototype.swapTo = function (col, fromRow, toRow)2861
GameCore.prototype.swapTo = function (col, fromRow, toRow)2493
{2862
{2494
if (!this.isColumnStill(col))2863
if (!this.columnAllowsDrag(col))2495
{ return null; }2864
{ return null; }2496
var block = this.blockAt(col, fromRow);2865
var block = this.blockAt(col, fromRow);2497
if (!block)2866
if (!block)@@ -3196,6 +3565,9 @@ var app = {3196
flames: Object.create(null),3565
flames: Object.create(null),3197
cells: [],3566
cells: [],3198
pointerId: null,3567
pointerId: null,3568
pointerType: null,3569
activeTouchId: null,3570
debugDrag: false,3199
gameOverShown: false,3571
gameOverShown: false,3200
selectTween: null,3572
selectTween: null,3201
online: false,3573
online: false,@@ -3208,6 +3580,7 @@ var app = {3208
3580
3209
window.onload = function ()3581
window.onload = function ()3210
{3582
{3583
app.debugDrag = isDragDebugEnabled();3211
var play = document.getElementById('btn-play');3584
var play = document.getElementById('btn-play');3212
play.addEventListener('click', onPlay);3585
play.addEventListener('click', onPlay);3213
play.addEventListener('pointerup', function (e)3586
play.addEventListener('pointerup', function (e)@@ -3360,10 +3733,25 @@ function placeTile (obj, col, y)3360
obj.transform.size = { x: t, y: t };3733
obj.transform.size = { x: t, y: t };3361
}3734
}3362
3735
3736
function layoutIfNeeded ()3737
{3738
if (app.core && app.core.drag)3739
{ return; }3740
var vw = window.innerWidth || 320;3741
var vh = window.innerHeight || 480;3742
if (app._layoutVw === vw && app._layoutVh === vh && app.scene && app.tile)3743
{ return; }3744
layout();3745
}3746
3363
function layout ()3747
function layout ()3364
{3748
{3749
if (app.core && app.core.drag)3750
{ return; }3365
var vw = window.innerWidth || 320;3751
var vw = window.innerWidth || 320;3366
var vh = window.innerHeight || 480;3752
var vh = window.innerHeight || 480;3753
app._layoutVw = vw;3754
app._layoutVh = vh;3367
var safeTop = 0;3755
var safeTop = 0;3368
try3756
try3369
{3757
{@@ -3447,9 +3835,16 @@ function syncSprites ()3447
}3835
}3448
flame.renderer.visible = true;3836
flame.renderer.visible = true;3449
var t = app.tile;3837
var t = app.tile;3838
// 3x as large (length and width, t1110). Keep the top of the3839
// flame at the same overlap into the block so the extra length3840
// extends downward under the rocket.3841
var flameW = t * 0.85 * 3;3842
var flameH = t * 0.85 * 3;3843
var origCenter = app.fieldH - block.y * t + t * 0.15;3844
var origTop = origCenter - (t * 0.85) / 2;3450
flame.transform.position.x = block.col * t + t / 2;3845
flame.transform.position.x = block.col * t + t / 2;3451
flame.transform.position.y = app.fieldH - block.y * t + t * 0.15;3846
flame.transform.position.y = origTop + flameH / 2;3452
flame.transform.size = { x: t * 0.85, y: t * 0.85 };3847
flame.transform.size = { x: flameW, y: flameH };3453
}3848
}3454
else if (app.flames[block.id])3849
else if (app.flames[block.id])3455
{ app.flames[block.id].renderer.visible = false; }3850
{ app.flames[block.id].renderer.visible = false; }@@ -3589,7 +3984,11 @@ function tickView (dt)3589
if (app.mode !== 'playing' || !app.core)3984
if (app.mode !== 'playing' || !app.core)3590
{ return; }3985
{ return; }3591
3986
3592
layout();3987
// Never resize the canvas mid-drag: a URL-bar hide would move the3988
// field under the finger and jump the target row. Also skip work3989
// when the viewport has not actually changed.3990
if (!(app.core && app.core.drag))3991
{ layoutIfNeeded(); }3593
for (var i = 0; i < app.cells.length; i++)3992
for (var i = 0; i < app.cells.length; i++)3594
{3993
{3595
var col = i % 10;3994
var col = i % 10;@@ -3619,15 +4018,58 @@ function tickView (dt)3619
4018
3620
/* ---------- pointer input (mouse + touch) --------------------------- */4019
/* ---------- pointer input (mouse + touch) --------------------------- */3621
/*4020
/*3622
* Both mouse and touch use Pointer Events. After pointerdown we listen4021
* Mouse uses Pointer Events. Touch uses Touch Events as the authority:3623
* on window, so a drag that leaves the canvas (needed to launch a4022
* on a real phone, pointer clientX/Y can disagree with the visual3624
* block at the top of a full column) still reaches moveDrag. We do4023
* finger (zoomed visual viewport, gesture takeover, capture cancel)3625
* not treat lostpointercapture as the end of a drag: on some touch4024
* while changedTouches stays correct. When a touch is active, pointer3626
* browsers that fires mid-gesture and would cancel a launch.4025
* move/up/cancel are ignored so a poisoned pointer coordinate cannot4026
* walk the block to the top or bottom of the stack (t1106).4027
*4028
* After a down we also listen on the document, so a drag that leaves4029
* the canvas (needed to launch a block at the top of a full column)4030
* still reaches moveDrag. We do not call setPointerCapture: on mobile4031
* that is often cancelled when the browser takes the gesture, which4032
* would fire pointercancel mid-drag.4033
*4034
* A non-passive document touchstart/touchmove preventDefault during4035
* play stops scrolling, rubber-banding and the pointercancel that4036
* follows. pointercancel / touchcancel end the drag without launch.4037
*4038
* ?debug=drag (or localStorage meteors-debug-drag=1) shows an overlay.3627
*/4039
*/3628
4040
3629
var boundCanvas = null;4041
var boundCanvas = null;3630
var windowListening = false;4042
var windowListening = false;4043
var touchListening = false;4044
var lastDragDebug = null;4045
4046
function isDragDebugEnabled ()4047
{4048
try4049
{4050
var q = window.location && window.location.search;4051
if (q && /(?:\?|&)debug=drag(?:&|$)/.test(q))4052
{ return true; }4053
if (window.localStorage && window.localStorage.getItem('meteors-debug-drag') === '1')4054
{ return true; }4055
}4056
catch (err) { /* ignore */ }4057
return false;4058
}4059
4060
function eventClient (e)4061
{4062
var t = null;4063
if (e.changedTouches && e.changedTouches.length)4064
{ t = e.changedTouches[0]; }4065
else if (e.touches && e.touches.length)4066
{ t = e.touches[0]; }4067
if (t)4068
{ return { x: t.clientX, y: t.clientY, id: t.identifier, src: 'touch' }; }4069
if (e.clientX != null && e.clientY != null)4070
{ return { x: e.clientX, y: e.clientY, id: e.pointerId, src: 'pointer' }; }4071
return null;4072
}3631
4073
3632
function addWindowPointer ()4074
function addWindowPointer ()3633
{4075
{@@ -3635,7 +4077,7 @@ function addWindowPointer ()3635
{ return; }4077
{ return; }3636
window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false });4078
window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false });3637
window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false });4079
window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false });3638
window.addEventListener('pointercancel', onPointerUp, { capture: true, passive: false });4080
window.addEventListener('pointercancel', onPointerCancel, { capture: true, passive: false });3639
windowListening = true;4081
windowListening = true;3640
}4082
}3641
4083
@@ -3645,28 +4087,59 @@ function removeWindowPointer ()3645
{ return; }4087
{ return; }3646
window.removeEventListener('pointermove', onPointerMove, { capture: true });4088
window.removeEventListener('pointermove', onPointerMove, { capture: true });3647
window.removeEventListener('pointerup', onPointerUp, { capture: true });4089
window.removeEventListener('pointerup', onPointerUp, { capture: true });3648
window.removeEventListener('pointercancel', onPointerUp, { capture: true });4090
window.removeEventListener('pointercancel', onPointerCancel, { capture: true });3649
windowListening = false;4091
windowListening = false;3650
}4092
}3651
4093
4094
function addTouchListeners ()4095
{4096
if (touchListening)4097
{ return; }4098
var opts = { capture: true, passive: false };4099
document.addEventListener('touchstart', onTouchStart, opts);4100
document.addEventListener('touchmove', onTouchMove, opts);4101
document.addEventListener('touchend', onTouchEnd, opts);4102
document.addEventListener('touchcancel', onTouchCancel, opts);4103
touchListening = true;4104
}4105
4106
function removeTouchListeners ()4107
{4108
if (!touchListening)4109
{ return; }4110
var opts = { capture: true };4111
document.removeEventListener('touchstart', onTouchStart, opts);4112
document.removeEventListener('touchmove', onTouchMove, opts);4113
document.removeEventListener('touchend', onTouchEnd, opts);4114
document.removeEventListener('touchcancel', onTouchCancel, opts);4115
touchListening = false;4116
}4117
3652
function bindPointer (canvas)4118
function bindPointer (canvas)3653
{4119
{3654
unbindPointer();4120
unbindPointer();3655
boundCanvas = canvas;4121
boundCanvas = canvas;3656
canvas.addEventListener('pointerdown', onPointerDown, { passive: false });4122
canvas.addEventListener('pointerdown', onPointerDown, { passive: false });3657
canvas.style.touchAction = 'none';4123
canvas.style.touchAction = 'none';4124
canvas.style.position = 'fixed';3658
canvas.style.cursor = 'pointer';4125
canvas.style.cursor = 'pointer';3659
canvas.style.webkitUserSelect = 'none';4126
canvas.style.webkitUserSelect = 'none';3660
canvas.style.userSelect = 'none';4127
canvas.style.userSelect = 'none';4128
addTouchListeners();4129
if (app.debugDrag)4130
{ ensureDragDebugOverlay(); }3661
}4131
}3662
4132
3663
function unbindPointer ()4133
function unbindPointer ()3664
{4134
{3665
removeWindowPointer();4135
removeWindowPointer();4136
removeTouchListeners();3666
if (boundCanvas)4137
if (boundCanvas)3667
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }4138
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }3668
boundCanvas = null;4139
boundCanvas = null;3669
app.pointerId = null;4140
app.pointerId = null;4141
app.pointerType = null;4142
app.activeTouchId = null;3670
}4143
}3671
4144
3672
function fieldFromEvent (e)4145
function fieldFromEvent (e)@@ -3677,34 +4150,94 @@ function fieldFromEvent (e)3677
var rect = canvas.getBoundingClientRect();4150
var rect = canvas.getBoundingClientRect();3678
if (rect.width <= 0 || rect.height <= 0)4151
if (rect.width <= 0 || rect.height <= 0)3679
{ return null; }4152
{ return null; }4153
var pt = eventClient(e);4154
if (!pt)4155
{ return null; }3680
// Map through the CSS box only. Using canvas.width / app.tile would4156
// Map through the CSS box only. Using canvas.width / app.tile would3681
// scale the pointer by the backing-store ratio whenever that differs4157
// scale the pointer by the backing-store ratio whenever that differs3682
// from the layout size (high-DPR devices), so a short finger move4158
// from the layout size (high-DPR devices), so a short finger move3683
// would look like a long swipe.4159
// would look like a long swipe.3684
var width = Planet.FIELD_WIDTH;4160
var width = Planet.FIELD_WIDTH;3685
var height = Planet.FIELD_HEIGHT;4161
var height = Planet.FIELD_HEIGHT;3686
var col = ((e.clientX - rect.left) / rect.width) * width;4162
var col = ((pt.x - rect.left) / rect.width) * width;3687
var fieldY = (1 - (e.clientY - rect.top) / rect.height) * height;4163
var fieldY = (1 - (pt.y - rect.top) / rect.height) * height;3688
return { col: col, y: fieldY };4164
return { col: col, y: fieldY, src: pt.src, clientX: pt.x, clientY: pt.y, rect: rect };4165
}4166
4167
function playingForDrag ()4168
{4169
return app.core && app.core.state === 'playing';4170
}4171
4172
function startDragFromEvent (e)4173
{4174
var pos = fieldFromEvent(e);4175
if (!pos)4176
{ return; }4177
if (app.core.drag)4178
{4179
app.core.moveDrag(pos.y);4180
logDragDebug(e, pos, 'move-already');4181
return;4182
}4183
if (app.core.beginDrag(pos.col, pos.y) && app.core.drag)4184
{ startPressTween(app.core.drag.blockId); }4185
logDragDebug(e, pos, 'down');4186
}4187
4188
function moveDragFromEvent (e)4189
{4190
if (!app.core || !app.core.drag)4191
{ return; }4192
var pos = fieldFromEvent(e);4193
if (pos)4194
{ app.core.moveDrag(pos.y); }4195
logDragDebug(e, pos, 'move');4196
}4197
4198
function finishDrag (opts)4199
{4200
opts = opts || {};4201
app.pointerId = null;4202
app.pointerType = null;4203
app.activeTouchId = null;4204
removeWindowPointer();4205
if (boundCanvas)4206
{4207
try4208
{4209
if (opts.pointerId != null && boundCanvas.releasePointerCapture)4210
{ boundCanvas.releasePointerCapture(opts.pointerId); }4211
}4212
catch (err) { /* ignore */ }4213
}4214
startReleaseTween();4215
if (app.core && app.core.drag)4216
{4217
if (opts.noLaunch)4218
{ app.core.drag.launchIntent = false; }4219
app.core.endDrag();4220
}4221
else if (app.core)4222
{ app.core.endDrag(); }4223
logDragDebug(null, null, opts.noLaunch ? 'cancel' : 'up');3689
}4224
}3690
4225
3691
function onPointerDown (e)4226
function onPointerDown (e)3692
{4227
{3693
if (!app.core || app.core.state !== 'playing')4228
if (!playingForDrag())3694
{ return; }4229
{ return; }3695
if (app.pointerId != null)4230
if (app.pointerId != null && e.pointerId !== app.pointerId)3696
{ return; }4231
{ return; }3697
e.preventDefault();4232
if (e.preventDefault)4233
{ e.preventDefault(); }3698
app.pointerId = e.pointerId;4234
app.pointerId = e.pointerId;3699
try4235
app.pointerType = e.pointerType || 'mouse';3700
{ boundCanvas.setPointerCapture(e.pointerId); }3701
catch (err) { /* ignore */ }3702
addWindowPointer();4236
addWindowPointer();3703
var pos = fieldFromEvent(e);4237
// If a touch is already in charge, do not reset the grab from pointerdown.3704
// Instant grab, no long-press or hold delay: the block is taken on4238
if (app.activeTouchId != null)3705
// pointerdown and the click tween starts in the same moment.4239
{ return; }3706
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)4240
startDragFromEvent(e);3707
{ startPressTween(app.core.drag.blockId); }3708
}4241
}3709
4242
3710
function onPointerMove (e)4243
function onPointerMove (e)@@ -3713,10 +4246,20 @@ function onPointerMove (e)3713
{ return; }4246
{ return; }3714
if (!app.core || !app.core.drag)4247
if (!app.core || !app.core.drag)3715
{ return; }4248
{ return; }3716
e.preventDefault();4249
// Touch coordinates are the authority; ignore pointer moves for a3717
var pos = fieldFromEvent(e);4250
// live touch so a wrong clientY cannot walk the whole column.3718
if (pos)4251
// Only skip when touchstart has actually arrived — some environments3719
{ app.core.moveDrag(pos.y); }4252
// deliver touch-pointer events without a TouchEvent stream.4253
if (app.activeTouchId != null)4254
{4255
if (e.preventDefault)4256
{ e.preventDefault(); }4257
logDragDebug(e, fieldFromEvent(e), 'pointer-ignored');4258
return;4259
}4260
if (e.preventDefault)4261
{ e.preventDefault(); }4262
moveDragFromEvent(e);3720
}4263
}3721
4264
3722
function onPointerUp (e)4265
function onPointerUp (e)@@ -3725,21 +4268,155 @@ function onPointerUp (e)3725
{ return; }4268
{ return; }3726
if (e && e.pointerId != null && e.pointerId !== app.pointerId)4269
if (e && e.pointerId != null && e.pointerId !== app.pointerId)3727
{ return; }4270
{ return; }3728
if (e)4271
if (app.activeTouchId != null)4272
{4273
// touchend will finish the drag.4274
if (e && e.preventDefault)4275
{ e.preventDefault(); }4276
logDragDebug(e, fieldFromEvent(e), 'pointer-up-ignored');4277
return;4278
}4279
if (e && e.preventDefault)3729
{ e.preventDefault(); }4280
{ e.preventDefault(); }3730
app.pointerId = null;4281
finishDrag({ pointerId: e && e.pointerId });3731
removeWindowPointer();4282
}3732
if (boundCanvas)4283
4284
function onPointerCancel (e)4285
{4286
if (app.pointerId == null)4287
{ return; }4288
if (e && e.pointerId != null && e.pointerId !== app.pointerId)4289
{ return; }4290
if (app.activeTouchId != null)3733
{4291
{3734
try4292
if (e && e.preventDefault)3735
{ boundCanvas.releasePointerCapture(e && e.pointerId); }4293
{ e.preventDefault(); }3736
catch (err) { /* ignore */ }4294
logDragDebug(e, fieldFromEvent(e), 'pointer-cancel-ignored');4295
return;3737
}4296
}3738
startReleaseTween();4297
if (e && e.preventDefault)3739
if (app.core)4298
{ e.preventDefault(); }3740
{ app.core.endDrag(); }4299
// A cancelled gesture is not a launch pull.4300
finishDrag({ pointerId: e && e.pointerId, noLaunch: true });4301
}4302
4303
function touchById (list, id)4304
{4305
if (!list)4306
{ return null; }4307
for (var i = 0; i < list.length; i++)4308
{4309
if (list[i].identifier === id)4310
{ return list[i]; }4311
}4312
return null;3741
}4313
}3742
4314
4315
function onTouchStart (e)4316
{4317
if (!playingForDrag())4318
{ return; }4319
if (app.activeTouchId != null)4320
{4321
if (e.preventDefault)4322
{ e.preventDefault(); }4323
return;4324
}4325
var t = e.changedTouches && e.changedTouches[0];4326
if (!t)4327
{ return; }4328
if (e.preventDefault)4329
{ e.preventDefault(); }4330
app.activeTouchId = t.identifier;4331
addWindowPointer();4332
startDragFromEvent(e);4333
}4334
4335
function onTouchMove (e)4336
{4337
if (app.activeTouchId == null)4338
{ return; }4339
if (!touchById(e.changedTouches, app.activeTouchId)4340
&& !touchById(e.touches, app.activeTouchId))4341
{ return; }4342
if (e.preventDefault)4343
{ e.preventDefault(); }4344
moveDragFromEvent(e);4345
}4346
4347
function onTouchEnd (e)4348
{4349
if (app.activeTouchId == null)4350
{ return; }4351
if (!touchById(e.changedTouches, app.activeTouchId))4352
{ return; }4353
if (e.preventDefault)4354
{ e.preventDefault(); }4355
finishDrag({});4356
}4357
4358
function onTouchCancel (e)4359
{4360
if (app.activeTouchId == null)4361
{ return; }4362
if (!touchById(e.changedTouches, app.activeTouchId))4363
{ return; }4364
if (e.preventDefault)4365
{ e.preventDefault(); }4366
finishDrag({ noLaunch: true });4367
}4368
4369
function ensureDragDebugOverlay ()4370
{4371
if (document.getElementById('drag-debug'))4372
{ return; }4373
var el = document.createElement('div');4374
el.id = 'drag-debug';4375
el.setAttribute('aria-live', 'polite');4376
document.body.appendChild(el);4377
}4378
4379
function logDragDebug (e, pos, phase)4380
{4381
if (!app.debugDrag)4382
{ return; }4383
ensureDragDebugOverlay();4384
var el = document.getElementById('drag-debug');4385
if (!el)4386
{ return; }4387
var block = null;4388
if (app.core && app.core.drag)4389
{ block = app.core.blockById(app.core.drag.blockId); }4390
var pt = e ? eventClient(e) : null;4391
var evType = e ? e.type : phase;4392
var info = {4393
phase: phase,4394
ev: evType,4395
src: pos && pos.src,4396
fieldY: pos && Math.round(pos.y * 100) / 100,4397
col: pos && Math.round(pos.col * 100) / 100,4398
cy: pt && Math.round(pt.y * 10) / 10,4399
blockY: block && Math.round(block.y * 100) / 100,4400
target: block && Math.floor((pos && pos.y) + 1e-12),4401
launch: app.core && app.core.drag && app.core.drag.launchIntent,4402
touchId: app.activeTouchId,4403
pid: app.pointerId,4404
ptype: app.pointerType4405
};4406
lastDragDebug = info;4407
el.textContent = 'drag ' + phase4408
+ ' ev=' + evType4409
+ ' src=' + (info.src || '-')4410
+ ' y=' + (info.fieldY != null ? info.fieldY : '-')4411
+ ' → ' + (info.target != null ? info.target : '-')4412
+ ' block=' + (info.blockY != null ? info.blockY : '-')4413
+ (info.launch ? ' LAUNCH' : '')4414
+ ' tId=' + info.touchId4415
+ ' pId=' + info.pid;4416
el.hidden = false;4417
}4418
4419
3743
function hideEl (id)4420
function hideEl (id)3744
{4421
{3745
document.getElementById(id).hidden = true;4422
document.getElementById(id).hidden = true;