chore: rebuild game.js with drag, match and Power display fixes
@@ -438,16 +438,30 @@ function power (planet)
438
438
return (gravity + air + mass) / 3;
439
439
}
440
440
441
/** Maximum miss chance, used by the gameplay formula and the displayed Power. */
442
var MAX_MISS_CHANCE = 0.60;
443
var MIN_MISS_CHANCE = 0.05;
444
441
445
/** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */
442
446
function missChance (planet)
443
447
{
444
return 0.60 - 0.55 * power(planet);
448
return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet);
449
}
450
451
/**
452
* Displayed Power, as a 0–100 percentage. This is the complement of the
453
* miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss
454
* → 0% Power. The gameplay power() and missChance() formulas are unchanged.
455
*/
456
function displayPower (planet)
457
{
458
return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100);
445
459
}
446
460
447
461
/**
448
462
* Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),
449
* launch force (N), and power as a percentage. The numbers do not have to
450
* explain themselves.
463
* launch force (N), and displayed Power as a percentage. The numbers do
464
* not have to explain themselves.
451
465
*/
452
466
function displayValues (planet)
453
467
{
@@ -458,7 +472,7 @@ function displayValues (planet)
458
472
gravity: gravity,
459
473
density: density,
460
474
newtons: newtons,
461
power: Math.round(power(planet) * 100),
475
power: displayPower(planet),
462
476
missChance: missChance(planet)
463
477
};
464
478
}
@@ -482,8 +496,11 @@ var Planet = {
482
496
jumpHeight: jumpHeight,
483
497
launchVelocity: launchVelocity,
484
498
launchDeceleration: launchDeceleration,
499
MAX_MISS_CHANCE: MAX_MISS_CHANCE,
500
MIN_MISS_CHANCE: MIN_MISS_CHANCE,
485
501
power: power,
486
502
missChance: missChance,
503
displayPower: displayPower,
487
504
displayValues: displayValues
488
505
};
489
506
@@ -1120,6 +1137,7 @@ GameCore.prototype.tick = function (dt)
1120
1137
{
1121
1138
var step = Math.min(SUBSTEP, left);
1122
1139
this.integrateFalling(step);
1140
this.resolveFlyingMatches();
1123
1141
this.integrateClusters(step);
1124
1142
this.mergeFlyingClusters();
1125
1143
this.integrateManual(step);
@@ -1332,8 +1350,9 @@ GameCore.prototype.integrateClusters = function (dt)
1332
1350
}
1333
1351
1334
1352
this.catchFallingOntoCluster(cluster, members);
1353
this.checkChainRelaunch(cluster);
1335
1354
1336
if (cluster.phase === 'down')
1355
if (cluster.phase === 'down' && this.clusters[cluster.id])
1337
1356
{ this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }
1338
1357
}
1339
1358
};
@@ -1442,7 +1461,8 @@ GameCore.prototype.mergeClusterGroup = function (clusters)
1442
1461
{ delete this.clusters[clusters[c].id]; }
1443
1462
}
1444
1463
this.packClusterOverlaps(keep);
1445
if (keep.phase === 'down')
1464
this.checkChainRelaunch(keep);
1465
if (keep.phase === 'down' && this.clusters[keep.id])
1446
1466
{ this.tryLandCluster(keep, this.clusterMembers(keep.id)); }
1447
1467
};
1448
1468
@@ -1702,6 +1722,24 @@ GameCore.prototype.pushClusterColumn = function (clusterId, col)
1702
1722
return true;
1703
1723
};
1704
1724
1725
/**
1726
* After a cargo attach or a cluster merge (or any other mid-air state
1727
* change), turn spontaneous lines of 3+ into rockets and give the whole
1728
* already-flying cluster an extra launch from its current position —
1729
* the same re-launch as a player push that completes a row.
1730
*/
1731
GameCore.prototype.resolveFlyingMatches = function ()
1732
{
1733
var ids = Object.keys(this.clusters);
1734
for (var n = 0; n < ids.length; n++)
1735
{
1736
var cluster = this.clusters[ids[n]];
1737
if (!cluster || cluster.phase === 'landed')
1738
{ continue; }
1739
this.checkChainRelaunch(cluster);
1740
}
1741
};
1742
1705
1743
GameCore.prototype.checkChainRelaunch = function (cluster)
1706
1744
{
1707
1745
if (!this.clusters[cluster.id])
@@ -2320,38 +2358,25 @@ GameCore.prototype.moveDrag = function (y)
2320
2358
this.drag = null;
2321
2359
return;
2322
2360
}
2323
var dy = y - this.drag.pointerY;
2324
2361
this.drag.pointerY = y;
2325
if (this.drag.accum == null)
2326
{ this.drag.accum = 0; }
2327
this.drag.accum += dy;
2328
// One swap per tile of travel, triggered after a minimal SWAP_DISTANCE.
2329
// A single large move (fast swipe) walks every tile; none are skipped.
2330
var guard = 0;
2331
while (guard++ < this.height + 2
2332
&& (this.drag.accum >= SWAP_DISTANCE || this.drag.accum <= -SWAP_DISTANCE))
2333
{
2334
var dir = this.drag.accum > 0 ? 1 : -1;
2335
if (!this.swapOne(block, dir))
2336
{
2337
if (dir > 0)
2338
{ this.drag.accum = Math.min(this.drag.accum, SWAP_DISTANCE - 1e-9); }
2339
else
2340
{ this.drag.accum = Math.max(this.drag.accum, -(SWAP_DISTANCE - 1e-9)); }
2341
break;
2342
}
2343
this.drag.accum -= dir;
2344
// A short move (SWAP_DISTANCE < 1) leaves a negative leftover;
2345
// do not let that immediately swap back the other way.
2346
if ((dir > 0 && this.drag.accum < 0) || (dir < 0 && this.drag.accum > 0))
2347
{ this.drag.accum = 0; }
2348
if (!this.isColumnStill(block.col))
2349
{
2350
this.drag = null;
2351
return;
2352
}
2353
}
2362
// 1:1 tracking: the target row is the one under the pointer, clamped
2363
// to the still stack. Swap step by step with each neighbour, and stop
2364
// exactly there. When the pointer stops, nothing else moves.
2365
// Accumulated deltas are not used: a SWAP_DISTANCE (0.25) threshold
2366
// used to jump a full cell on a short pull, and one large event
2367
// could walk the entire column (the "jumps all the way" bug).
2354
2368
var top = this.columnStackTopRow(block.col);
2369
var bottom = this.columnStackBottomRow(block.col);
2370
var target = Math.floor(y + 1e-12);
2371
if (bottom < 0)
2372
{ bottom = 0; }
2373
if (target < bottom)
2374
{ target = bottom; }
2375
if (target > top)
2376
{ target = top; }
2377
this.swapToward(block, target);
2378
2379
top = this.columnStackTopRow(block.col);
2355
2380
var isTop = Math.abs(block.y - top) < ALIGN_EPS;
2356
2381
if (this.drag.startedAsTop)
2357
2382
{
@@ -2414,6 +2439,18 @@ GameCore.prototype.columnStackTopRow = function (col)
2414
2439
return top;
2415
2440
};
2416
2441
2442
GameCore.prototype.columnStackBottomRow = function (col)
2443
{
2444
var bottom = Infinity;
2445
var list = this.blocksInColumn(col);
2446
for (var i = 0; i < list.length; i++)
2447
{
2448
if (this.isGrounded(list[i]) && list[i].y < bottom)
2449
{ bottom = list[i].y; }
2450
}
2451
return bottom === Infinity ? 0 : bottom;
2452
};
2453
2417
2454
GameCore.prototype.swapToward = function (block, targetY)
2418
2455
{
2419
2456
var guard = 0;
@@ -3640,11 +3677,14 @@ function fieldFromEvent (e)
3640
3677
var rect = canvas.getBoundingClientRect();
3641
3678
if (rect.width <= 0 || rect.height <= 0)
3642
3679
{ return null; }
3643
var x = (e.clientX - rect.left) * (canvas.width / rect.width);
3644
var y = (e.clientY - rect.top) * (canvas.height / rect.height);
3645
// Fractional column so beginDrag can snap across a nearby edge.
3646
var col = x / app.tile;
3647
var fieldY = (canvas.height - y) / app.tile;
3680
// Map through the CSS box only. Using canvas.width / app.tile would
3681
// scale the pointer by the backing-store ratio whenever that differs
3682
// from the layout size (high-DPR devices), so a short finger move
3683
// would look like a long swipe.
3684
var width = Planet.FIELD_WIDTH;
3685
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;
3648
3688
return { col: col, y: fieldY };
3649
3689
}
3650
3690
@@ -438,16 +438,30 @@ function power (planet)438
return (gravity + air + mass) / 3;438
return (gravity + air + mass) / 3;439
}439
}440
440
441
/** Maximum miss chance, used by the gameplay formula and the displayed Power. */442
var MAX_MISS_CHANCE = 0.60;443
var MIN_MISS_CHANCE = 0.05;444
441
/** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */445
/** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */442
function missChance (planet)446
function missChance (planet)443
{447
{444
return 0.60 - 0.55 * power(planet);448
return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet);449
}450
451
/**452
* Displayed Power, as a 0–100 percentage. This is the complement of the453
* miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss454
* → 0% Power. The gameplay power() and missChance() formulas are unchanged.455
*/456
function displayPower (planet)457
{458
return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100);445
}459
}446
460
447
/**461
/**448
* Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),462
* Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),449
* launch force (N), and power as a percentage. The numbers do not have to463
* launch force (N), and displayed Power as a percentage. The numbers do450
* explain themselves.464
* not have to explain themselves.451
*/465
*/452
function displayValues (planet)466
function displayValues (planet)453
{467
{@@ -458,7 +472,7 @@ function displayValues (planet)458
gravity: gravity,472
gravity: gravity,459
density: density,473
density: density,460
newtons: newtons,474
newtons: newtons,461
power: Math.round(power(planet) * 100),475
power: displayPower(planet),462
missChance: missChance(planet)476
missChance: missChance(planet)463
};477
};464
}478
}@@ -482,8 +496,11 @@ var Planet = {482
jumpHeight: jumpHeight,496
jumpHeight: jumpHeight,483
launchVelocity: launchVelocity,497
launchVelocity: launchVelocity,484
launchDeceleration: launchDeceleration,498
launchDeceleration: launchDeceleration,499
MAX_MISS_CHANCE: MAX_MISS_CHANCE,500
MIN_MISS_CHANCE: MIN_MISS_CHANCE,485
power: power,501
power: power,486
missChance: missChance,502
missChance: missChance,503
displayPower: displayPower,487
displayValues: displayValues504
displayValues: displayValues488
};505
};489
506
@@ -1120,6 +1137,7 @@ GameCore.prototype.tick = function (dt)1120
{1137
{1121
var step = Math.min(SUBSTEP, left);1138
var step = Math.min(SUBSTEP, left);1122
this.integrateFalling(step);1139
this.integrateFalling(step);1140
this.resolveFlyingMatches();1123
this.integrateClusters(step);1141
this.integrateClusters(step);1124
this.mergeFlyingClusters();1142
this.mergeFlyingClusters();1125
this.integrateManual(step);1143
this.integrateManual(step);@@ -1332,8 +1350,9 @@ GameCore.prototype.integrateClusters = function (dt)1332
}1350
}1333
1351
1334
this.catchFallingOntoCluster(cluster, members);1352
this.catchFallingOntoCluster(cluster, members);1353
this.checkChainRelaunch(cluster);1335
1354
1336
if (cluster.phase === 'down')1355
if (cluster.phase === 'down' && this.clusters[cluster.id])1337
{ this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }1356
{ this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }1338
}1357
}1339
};1358
};@@ -1442,7 +1461,8 @@ GameCore.prototype.mergeClusterGroup = function (clusters)1442
{ delete this.clusters[clusters[c].id]; }1461
{ delete this.clusters[clusters[c].id]; }1443
}1462
}1444
this.packClusterOverlaps(keep);1463
this.packClusterOverlaps(keep);1445
if (keep.phase === 'down')1464
this.checkChainRelaunch(keep);1465
if (keep.phase === 'down' && this.clusters[keep.id])1446
{ this.tryLandCluster(keep, this.clusterMembers(keep.id)); }1466
{ this.tryLandCluster(keep, this.clusterMembers(keep.id)); }1447
};1467
};1448
1468
@@ -1702,6 +1722,24 @@ GameCore.prototype.pushClusterColumn = function (clusterId, col)1702
return true;1722
return true;1703
};1723
};1704
1724
1725
/**1726
* After a cargo attach or a cluster merge (or any other mid-air state1727
* change), turn spontaneous lines of 3+ into rockets and give the whole1728
* already-flying cluster an extra launch from its current position —1729
* the same re-launch as a player push that completes a row.1730
*/1731
GameCore.prototype.resolveFlyingMatches = function ()1732
{1733
var ids = Object.keys(this.clusters);1734
for (var n = 0; n < ids.length; n++)1735
{1736
var cluster = this.clusters[ids[n]];1737
if (!cluster || cluster.phase === 'landed')1738
{ continue; }1739
this.checkChainRelaunch(cluster);1740
}1741
};1742
1705
GameCore.prototype.checkChainRelaunch = function (cluster)1743
GameCore.prototype.checkChainRelaunch = function (cluster)1706
{1744
{1707
if (!this.clusters[cluster.id])1745
if (!this.clusters[cluster.id])@@ -2320,38 +2358,25 @@ GameCore.prototype.moveDrag = function (y)2320
this.drag = null;2358
this.drag = null;2321
return;2359
return;2322
}2360
}2323
var dy = y - this.drag.pointerY;2324
this.drag.pointerY = y;2361
this.drag.pointerY = y;2325
if (this.drag.accum == null)2362
// 1:1 tracking: the target row is the one under the pointer, clamped2326
{ this.drag.accum = 0; }2363
// to the still stack. Swap step by step with each neighbour, and stop2327
this.drag.accum += dy;2364
// exactly there. When the pointer stops, nothing else moves.2328
// One swap per tile of travel, triggered after a minimal SWAP_DISTANCE.2365
// Accumulated deltas are not used: a SWAP_DISTANCE (0.25) threshold2329
// A single large move (fast swipe) walks every tile; none are skipped.2366
// used to jump a full cell on a short pull, and one large event2330
var guard = 0;2367
// could walk the entire column (the "jumps all the way" bug).2331
while (guard++ < this.height + 22332
&& (this.drag.accum >= SWAP_DISTANCE || this.drag.accum <= -SWAP_DISTANCE))2333
{2334
var dir = this.drag.accum > 0 ? 1 : -1;2335
if (!this.swapOne(block, dir))2336
{2337
if (dir > 0)2338
{ this.drag.accum = Math.min(this.drag.accum, SWAP_DISTANCE - 1e-9); }2339
else2340
{ this.drag.accum = Math.max(this.drag.accum, -(SWAP_DISTANCE - 1e-9)); }2341
break;2342
}2343
this.drag.accum -= dir;2344
// A short move (SWAP_DISTANCE < 1) leaves a negative leftover;2345
// do not let that immediately swap back the other way.2346
if ((dir > 0 && this.drag.accum < 0) || (dir < 0 && this.drag.accum > 0))2347
{ this.drag.accum = 0; }2348
if (!this.isColumnStill(block.col))2349
{2350
this.drag = null;2351
return;2352
}2353
}2354
var top = this.columnStackTopRow(block.col);2368
var top = this.columnStackTopRow(block.col);2369
var bottom = this.columnStackBottomRow(block.col);2370
var target = Math.floor(y + 1e-12);2371
if (bottom < 0)2372
{ bottom = 0; }2373
if (target < bottom)2374
{ target = bottom; }2375
if (target > top)2376
{ target = top; }2377
this.swapToward(block, target);2378
2379
top = this.columnStackTopRow(block.col);2355
var isTop = Math.abs(block.y - top) < ALIGN_EPS;2380
var isTop = Math.abs(block.y - top) < ALIGN_EPS;2356
if (this.drag.startedAsTop)2381
if (this.drag.startedAsTop)2357
{2382
{@@ -2414,6 +2439,18 @@ GameCore.prototype.columnStackTopRow = function (col)2414
return top;2439
return top;2415
};2440
};2416
2441
2442
GameCore.prototype.columnStackBottomRow = function (col)2443
{2444
var bottom = Infinity;2445
var list = this.blocksInColumn(col);2446
for (var i = 0; i < list.length; i++)2447
{2448
if (this.isGrounded(list[i]) && list[i].y < bottom)2449
{ bottom = list[i].y; }2450
}2451
return bottom === Infinity ? 0 : bottom;2452
};2453
2417
GameCore.prototype.swapToward = function (block, targetY)2454
GameCore.prototype.swapToward = function (block, targetY)2418
{2455
{2419
var guard = 0;2456
var guard = 0;@@ -3640,11 +3677,14 @@ function fieldFromEvent (e)3640
var rect = canvas.getBoundingClientRect();3677
var rect = canvas.getBoundingClientRect();3641
if (rect.width <= 0 || rect.height <= 0)3678
if (rect.width <= 0 || rect.height <= 0)3642
{ return null; }3679
{ return null; }3643
var x = (e.clientX - rect.left) * (canvas.width / rect.width);3680
// Map through the CSS box only. Using canvas.width / app.tile would3644
var y = (e.clientY - rect.top) * (canvas.height / rect.height);3681
// scale the pointer by the backing-store ratio whenever that differs3645
// Fractional column so beginDrag can snap across a nearby edge.3682
// from the layout size (high-DPR devices), so a short finger move3646
var col = x / app.tile;3683
// would look like a long swipe.3647
var fieldY = (canvas.height - y) / app.tile;3684
var width = Planet.FIELD_WIDTH;3685
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;3648
return { col: col, y: fieldY };3688
return { col: col, y: fieldY };3649
}3689
}3650
3690