feat: landing bounce squash and Click Tween press scale on selection
A held block scales with easeOutBack to 80% in 150 ms and eases back with easeOutElastic in 720 ms. Landings play a subtle squash/rebound whose speed follows the planet's air resistance. Pointer move/up is tracked on the window so mouse and touch can launch past the canvas.
@@ -121,7 +121,14 @@ function createPlanet (opts)
121
121
riseTime: riseTime,
122
122
colors: colors,
123
123
baseJumpHeight: FIELD_HEIGHT / jumpsToClear,
124
fallSpeed: FIELD_HEIGHT / fallTime
124
fallSpeed: FIELD_HEIGHT / fallTime,
125
// Time to ease from vy=0 at the apex to terminal fallSpeed.
126
// Shorter when air resistance is low (small fallTime).
127
apexEaseTime: 0.15 * fallTime,
128
fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime),
129
// Visual landing bounce: faster at low air resistance. Stage-3 params.
130
bounceDuration: 0.12 * fallTime,
131
bounceSquash: 0.16
125
132
};
126
133
return planet;
127
134
}
@@ -232,6 +239,127 @@ else if (typeof globalThis !== 'undefined')
232
239
* /workspace/meteors/src/planet.js
233
240
*/
234
241
242
/*
243
* Start File:
244
* /workspace/meteors/src/easing.js
245
*/
246
'use strict';
247
248
/**
249
* Easing curves matching Qt QEasingCurve defaults (the same values Tim's
250
* KWin Click Tween effect uses). Formulas and default parameters come from
251
* Qt 6's QEasingCurve and https://easings.net/.
252
*
253
* Source of the click/release defaults:
254
* /workspace/click-tween/settings.h (clickTime, releaseTime, easing names)
255
* /workspace/click-tween/clicktween.cpp (press scale 0.8, release scale 1.0)
256
* /workspace/click-tween/README.md
257
*/
258
259
/** Qt QEasingCurve default overshoot for InBack / OutBack / InOutBack. */
260
var BACK_OVERSHOOT = 1.70158;
261
262
/** Qt QEasingCurve default period and amplitude for Elastic curves. */
263
var ELASTIC_PERIOD = 0.3;
264
var ELASTIC_AMPLITUDE = 1.0;
265
266
function clamp01 (t)
267
{
268
if (t <= 0)
269
{ return 0; }
270
if (t >= 1)
271
{ return 1; }
272
return t;
273
}
274
275
/** easeOutBack: overshoots the end value, then settles. Qt / easings.net. */
276
function easeOutBack (t)
277
{
278
t = clamp01(t);
279
var c1 = BACK_OVERSHOOT;
280
var c3 = c1 + 1;
281
var u = t - 1;
282
return 1 + c3 * u * u * u + c1 * u * u;
283
}
284
285
/**
286
* easeOutElastic: decaying sine overshoot past the end value.
287
* Qt default amplitude 1, period 0.3 (equivalent to easings.net).
288
*/
289
function easeOutElastic (t)
290
{
291
t = clamp01(t);
292
if (t === 0 || t === 1)
293
{ return t; }
294
var p = ELASTIC_PERIOD;
295
var s = p / 4;
296
return Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p) + 1;
297
}
298
299
function tween (from, to, t, ease)
300
{
301
return from + (to - from) * ease(clamp01(t));
302
}
303
304
/**
305
* DEFAULT settings of Tim's KWin Click Tween
306
* (git.timfalken.com/timfa/click-tween).
307
*
308
* Press: shrink to 80% in 150 ms with easeOutBack, stay at 80% while held.
309
* Release: grow back to 100% in 720 ms with easeOutElastic.
310
* Mid-animation release starts from the current scale, like QVariantAnimation.
311
*/
312
var CLICK_TWEEN = {
313
pressScale: 0.8,
314
releaseScale: 1.0,
315
pressDuration: 0.150,
316
releaseDuration: 0.720,
317
pressEase: easeOutBack,
318
releaseEase: easeOutElastic,
319
backOvershoot: BACK_OVERSHOOT,
320
elasticPeriod: ELASTIC_PERIOD,
321
elasticAmplitude: ELASTIC_AMPLITUDE,
322
source: 'click-tween settings.h + clicktween.cpp (Qt QEasingCurve defaults)'
323
};
324
325
/**
326
* Visual landing bounce, u in 0..1. Subtle squash with a slight stretch
327
* on the other axis and a tiny upward rebound (in tiles, visual only).
328
*/
329
function bouncePose (u, squash)
330
{
331
if (u <= 0 || u >= 1)
332
{ return { sx: 1, sy: 1, dy: 0 }; }
333
var wave = Math.sin(Math.PI * u);
334
return {
335
sx: 1 + squash * 0.6 * wave,
336
sy: 1 - squash * wave,
337
dy: 0.07 * wave * (1 - u)
338
};
339
}
340
341
var Easing = {
342
BACK_OVERSHOOT: BACK_OVERSHOOT,
343
ELASTIC_PERIOD: ELASTIC_PERIOD,
344
ELASTIC_AMPLITUDE: ELASTIC_AMPLITUDE,
345
easeOutBack: easeOutBack,
346
easeOutElastic: easeOutElastic,
347
tween: tween,
348
CLICK_TWEEN: CLICK_TWEEN,
349
clamp01: clamp01,
350
bouncePose: bouncePose
351
};
352
353
if (typeof module !== 'undefined' && module.exports)
354
{ module.exports = Easing; }
355
else if (typeof globalThis !== 'undefined')
356
{ globalThis.Easing = Easing; }
357
358
/*
359
* End File:
360
* /workspace/meteors/src/easing.js
361
*/
362
235
363
/*
236
364
* Start File:
237
365
* /workspace/meteors/src/gameCore.js
@@ -260,6 +388,8 @@ var STATE_MANUAL = 'manual';
260
388
var ALIGN_EPS = 0.15;
261
389
var SUBSTEP = 1 / 120;
262
390
var DRAG_LAUNCH_SLACK = 0.65;
391
// Upward pull (tiles past the press point) that launches an already-topmost block.
392
var LAUNCH_PULL = 0.35;
263
393
264
394
function UnionFind ()
265
395
{
@@ -309,6 +439,7 @@ function GameCore (planet, rng)
309
439
}
310
440
311
441
GameCore.COLOR_GRAY = COLOR_GRAY;
442
GameCore.LAUNCH_PULL = LAUNCH_PULL;
312
443
GameCore.STATE_STILL = STATE_STILL;
313
444
GameCore.STATE_FALLING = STATE_FALLING;
314
445
GameCore.STATE_FLYING = STATE_FLYING;
@@ -336,7 +467,9 @@ GameCore.prototype.createBlock = function (col, y, color, state)
336
467
pushedClusters: null,
337
468
manualTarget: 0,
338
469
manualDecel: 0,
339
manualPhase: null
470
manualPhase: null,
471
bouncing: false,
472
bounceAge: 0
340
473
};
341
474
this.blocks.push(block);
342
475
return block;
@@ -474,6 +607,199 @@ GameCore.prototype.lockedColumnCount = function ()
474
607
return n;
475
608
};
476
609
610
GameCore.prototype.startBounce = function (block)
611
{
612
if (!block)
613
{ return; }
614
block.bouncing = true;
615
block.bounceAge = 0;
616
};
617
618
GameCore.prototype.advanceBounces = function (dt)
619
{
620
var duration = this.planet.bounceDuration;
621
for (var i = 0; i < this.blocks.length; i++)
622
{
623
var block = this.blocks[i];
624
if (!block.bouncing)
625
{ continue; }
626
block.bounceAge += dt;
627
if (duration <= 0 || block.bounceAge >= duration)
628
{
629
block.bouncing = false;
630
block.bounceAge = duration;
631
}
632
}
633
};
634
635
/**
636
* True if `block` rests on the floor or on a solid (still / settling /
637
* locked) block immediately below it in the same column.
638
*/
639
GameCore.prototype.hasSupport = function (block)
640
{
641
if (block.y <= 1e-9)
642
{ return true; }
643
for (var i = 0; i < this.blocks.length; i++)
644
{
645
var other = this.blocks[i];
646
if (other === block || other.col !== block.col)
647
{ continue; }
648
if (!this.isGrounded(other))
649
{ continue; }
650
if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
651
{ return true; }
652
}
653
return false;
654
};
655
656
/** Highest flying block in this column whose top is at or below `block.y + 1`. */
657
GameCore.prototype.highestFlyingBelow = function (block)
658
{
659
var best = null;
660
for (var i = 0; i < this.blocks.length; i++)
661
{
662
var other = this.blocks[i];
663
if (other === block || other.col !== block.col)
664
{ continue; }
665
if (other.state !== STATE_FLYING || other.clusterId == null)
666
{ continue; }
667
var cluster = this.clusters[other.clusterId];
668
if (!cluster || cluster.phase === 'landed')
669
{ continue; }
670
// The falling block is at or above this flyer (or already overlapping).
671
if (block.y + 1e-9 < other.y)
672
{ continue; }
673
if (!best || other.y > best.y)
674
{ best = other; }
675
}
676
return best;
677
};
678
679
GameCore.prototype.highestFallingBelow = function (block)
680
{
681
var best = null;
682
for (var i = 0; i < this.blocks.length; i++)
683
{
684
var other = this.blocks[i];
685
if (other === block || other.col !== block.col)
686
{ continue; }
687
if (other.state !== STATE_FALLING)
688
{ continue; }
689
if (other.y > block.y - 1e-9)
690
{ continue; }
691
if (!best || other.y > best.y)
692
{ best = other; }
693
}
694
return best;
695
};
696
697
/**
698
* Make an unsupported still block start falling from rest, so it eases
699
* into fallSpeed under normal gravity. Skips the block being dragged.
700
*/
701
GameCore.prototype.releaseUnsupported = function ()
702
{
703
for (var i = 0; i < this.blocks.length; i++)
704
{
705
var block = this.blocks[i];
706
if (block.state !== STATE_STILL)
707
{ continue; }
708
if (this.drag && this.drag.blockId === block.id)
709
{ continue; }
710
if (this.hasSupport(block))
711
{ continue; }
712
block.state = STATE_FALLING;
713
block.vy = 0;
714
block.flame = false;
715
this.leaveCluster(block);
716
}
717
};
718
719
GameCore.prototype.accelerateFall = function (block, dt)
720
{
721
var target = -this.planet.fallSpeed;
722
if (block.vy > target)
723
{ block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); }
724
else
725
{ block.vy = target; }
726
};
727
728
GameCore.prototype.landFallingBlock = function (block)
729
{
730
block.vy = 0;
731
block.flame = false;
732
this.startBounce(block);
733
if (block.color === COLOR_GRAY)
734
{
735
block.state = STATE_SETTLING;
736
block.settleTime = 0;
737
}
738
else
739
{ block.state = STATE_STILL; }
740
};
741
742
/**
743
* Sit `block` on top of the flying cluster that `flyer` belongs to, and
744
* carry it along as cargo (no flame). Returns true if attached.
745
*/
746
GameCore.prototype.attachToFlyingCluster = function (block, flyer)
747
{
748
var cluster = this.clusters[flyer.clusterId];
749
if (!cluster || cluster.phase === 'landed')
750
{ return false; }
751
var roof = flyer.y + 1;
752
for (var i = 0; i < this.blocks.length; i++)
753
{
754
var other = this.blocks[i];
755
if (other === block || other.col !== block.col)
756
{ continue; }
757
if (other.clusterId !== cluster.id)
758
{ continue; }
759
if (other.y + 1 > roof)
760
{ roof = other.y + 1; }
761
}
762
block.y = roof;
763
block.state = STATE_FLYING;
764
block.clusterId = cluster.id;
765
block.vy = cluster.vy;
766
block.flame = false;
767
block.settleTime = 0;
768
return true;
769
};
770
771
GameCore.prototype.catchFallingOntoCluster = function (cluster, members)
772
{
773
if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')
774
{ return; }
775
// Snapshot falling blocks; attaching mutates state.
776
var falling = [];
777
for (var i = 0; i < this.blocks.length; i++)
778
{
779
if (this.blocks[i].state === STATE_FALLING)
780
{ falling.push(this.blocks[i]); }
781
}
782
for (var i = 0; i < falling.length; i++)
783
{
784
var block = falling[i];
785
var flyer = null;
786
for (var m = 0; m < members.length; m++)
787
{
788
var other = members[m];
789
if (other.col !== block.col)
790
{ continue; }
791
// Overlap or rest on this member's top.
792
if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9)
793
{
794
if (!flyer || other.y > flyer.y)
795
{ flyer = other; }
796
}
797
}
798
if (flyer)
799
{ this.attachToFlyingCluster(block, flyer); }
800
}
801
};
802
477
803
GameCore.prototype.tick = function (dt)
478
804
{
479
805
if (this.state === 'gameover')
@@ -503,7 +829,9 @@ GameCore.prototype.tick = function (dt)
503
829
left -= step;
504
830
}
505
831
832
this.releaseUnsupported();
506
833
this.advanceSettle(dt);
834
this.advanceBounces(dt);
507
835
if (!this.drag)
508
836
{ this.resolveMatches(); }
509
837
this.checkLocks();
@@ -551,24 +879,42 @@ GameCore.prototype.trySpawn = function ()
551
879
{ return null; }
552
880
var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];
553
881
var color = this.randomColor();
554
return this.createBlock(col, this.height, color, STATE_FALLING);
882
var block = this.createBlock(col, this.height, color, STATE_FALLING);
883
block.vy = -this.planet.fallSpeed;
884
return block;
555
885
};
556
886
557
887
GameCore.prototype.integrateFalling = function (dt)
558
888
{
559
var speed = this.planet.fallSpeed;
889
var falling = [];
560
890
for (var i = 0; i < this.blocks.length; i++)
561
891
{
562
var block = this.blocks[i];
892
if (this.blocks[i].state === STATE_FALLING)
893
{ falling.push(this.blocks[i]); }
894
}
895
// Lowest first, so a stack of falling blocks rests on itself.
896
falling.sort(function (a, b) { return a.y - b.y; });
897
for (var i = 0; i < falling.length; i++)
898
{
899
var block = falling[i];
563
900
if (block.state !== STATE_FALLING)
564
901
{ continue; }
902
this.accelerateFall(block, dt);
903
var nextY = block.y + block.vy * dt;
904
var flyer = this.highestFlyingBelow(block);
905
if (flyer && nextY <= flyer.y + 1 + 1e-9)
906
{
907
if (this.attachToFlyingCluster(block, flyer))
908
{ continue; }
909
}
565
910
var top = this.stackTop(block.col);
566
var nextY = block.y - speed * dt;
911
var below = this.highestFallingBelow(block);
912
if (below && below.y + 1 > top)
913
{ top = below.y + 1; }
567
914
if (nextY <= top)
568
915
{
569
916
block.y = top;
570
block.state = STATE_STILL;
571
block.vy = 0;
917
this.landFallingBlock(block);
572
918
}
573
919
else
574
920
{ block.y = nextY; }
@@ -591,7 +937,8 @@ GameCore.prototype.clusterMembers = function (clusterId)
591
937
* Advance a jump (cluster or manual) by dt. Uses exact kinematics so a
592
938
* jump of height H peaks at exactly H after riseTime. Returns dy.
593
939
* Mutates body.vy and body.phase ('up' / 'down'). body.decel is the
594
* upward deceleration; falling uses planet.fallSpeed.
940
* upward deceleration. After the apex, vy eases from 0 to -fallSpeed
941
* at planet.fallAccel, then stays at terminal fallSpeed.
595
942
*/
596
943
GameCore.prototype.advanceJump = function (body, dt)
597
944
{
@@ -624,8 +971,35 @@ GameCore.prototype.advanceJump = function (body, dt)
624
971
}
625
972
if (body.phase === 'down' && left > 0)
626
973
{
627
body.vy = -this.planet.fallSpeed;
628
dy += body.vy * left;
974
var target = -this.planet.fallSpeed;
975
var accel = this.planet.fallAccel;
976
if (accel < 1e-12)
977
{
978
body.vy = target;
979
dy += body.vy * left;
980
}
981
else if (body.vy > target + 1e-12)
982
{
983
var tReach = (body.vy - target) / accel;
984
if (tReach <= left + 1e-15)
985
{
986
dy += (body.vy + target) * 0.5 * tReach;
987
body.vy = target;
988
left -= tReach;
989
dy += body.vy * left;
990
}
991
else
992
{
993
var vyNew = body.vy - accel * left;
994
dy += (body.vy + vyNew) * 0.5 * left;
995
body.vy = vyNew;
996
}
997
}
998
else
999
{
1000
body.vy = target;
1001
dy += body.vy * left;
1002
}
629
1003
}
630
1004
return dy;
631
1005
};
@@ -654,14 +1028,18 @@ GameCore.prototype.integrateClusters = function (dt)
654
1028
members[i].vy = cluster.vy;
655
1029
}
656
1030
1031
this.catchFallingOntoCluster(cluster, members);
1032
657
1033
if (cluster.phase === 'down')
658
{ this.tryLandCluster(cluster, members); }
1034
{ this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }
659
1035
}
660
1036
};
661
1037
662
1038
GameCore.prototype.tryLandCluster = function (cluster, members)
663
1039
{
664
var self = this;
1040
if (!members.length)
1041
{ return; }
1042
var shift = 0;
665
1043
var hit = false;
666
1044
for (var i = 0; i < members.length; i++)
667
1045
{
@@ -671,63 +1049,109 @@ GameCore.prototype.tryLandCluster = function (cluster, members)
671
1049
return other.clusterId === cluster.id;
672
1050
});
673
1051
if (block.y <= top + 1e-9)
674
{ hit = true; }
1052
{
1053
hit = true;
1054
var need = top - block.y;
1055
if (need > shift)
1056
{ shift = need; }
1057
}
675
1058
}
676
1059
if (!hit)
677
1060
{ return; }
1061
// Lift the whole rigid body so the deepest contact sits flush.
1062
for (var i = 0; i < members.length; i++)
1063
{ members[i].y += shift; }
678
1064
this.landCluster(cluster, members);
679
1065
};
680
1066
1067
/**
1068
* Land as a rigid group: relative positions are kept, so part of the
1069
* cluster may hang above a gap. Grey members start the 3s settle timer;
1070
* coloured cargo stays put with them until every grey has recolored.
1071
* Members that actually rest on the floor or on another block bounce.
1072
*/
681
1073
GameCore.prototype.landCluster = function (cluster, members)
682
1074
{
683
var byCol = Object.create(null);
684
1075
for (var i = 0; i < members.length; i++)
685
1076
{
686
1077
var block = members[i];
687
if (!byCol[block.col])
688
{ byCol[block.col] = []; }
689
byCol[block.col].push(block);
1078
block.vy = 0;
1079
block.flame = false;
1080
block.clusterId = cluster.id;
1081
if (block.color === COLOR_GRAY)
1082
{
1083
block.state = STATE_SETTLING;
1084
block.settleTime = 0;
1085
}
1086
else
1087
{
1088
// Cargo stays in the rigid group (not matchable) until release.
1089
block.state = STATE_SETTLING;
1090
block.settleTime = 0;
1091
}
1092
if (this.hasSupport(block))
1093
{ this.startBounce(block); }
690
1094
}
691
var cols = Object.keys(byCol);
692
for (var c = 0; c < cols.length; c++)
1095
cluster.phase = 'landed';
1096
cluster.vy = 0;
1097
};
1098
1099
/**
1100
* After every grey in a landed cluster has recolored, let the group go.
1101
* Unsupported blocks fall individually from rest; supported ones stay.
1102
*/
1103
GameCore.prototype.releaseLandedCluster = function (cluster)
1104
{
1105
var members = this.clusterMembers(cluster.id);
1106
var releasing = Object.create(null);
1107
for (var i = 0; i < members.length; i++)
1108
{ releasing[members[i].id] = true; }
1109
1110
for (var i = 0; i < members.length; i++)
693
1111
{
694
var col = +cols[c];
695
var list = byCol[col];
696
list.sort(function (a, b) { return a.y - b.y; });
697
var self = this;
698
var top = this.stackTop(col, function (other)
1112
var block = members[i];
1113
if (block.color === COLOR_GRAY)
699
1114
{
700
return other.clusterId === cluster.id;
701
});
702
for (var i = 0; i < list.length; i++)
1115
block.color = this.randomColor();
1116
block.settleTime = 0;
1117
}
1118
block.flame = false;
1119
this.leaveCluster(block);
1120
// Support from outside the group (floor or a foreign block). Fellow
1121
// members that will also fall do not count, so a hanging stack
1122
// drops together without the top one staying put.
1123
var supported = this.hasSupportOutside(block, releasing);
1124
if (supported)
703
1125
{
704
var block = list[i];
705
block.y = top;
1126
block.state = STATE_STILL;
1127
block.vy = 0;
1128
}
1129
else
1130
{
1131
block.state = STATE_FALLING;
706
1132
block.vy = 0;
707
block.flame = false;
708
top = block.y + 1;
709
if (block.color === COLOR_GRAY)
710
{
711
block.state = STATE_SETTLING;
712
block.settleTime = 0;
713
}
714
else
715
{
716
block.state = STATE_STILL;
717
block.clusterId = null;
718
}
719
1133
}
720
1134
}
721
cluster.phase = 'landed';
722
cluster.vy = 0;
723
var remaining = 0;
1135
delete this.clusters[cluster.id];
1136
};
1137
1138
GameCore.prototype.hasSupportOutside = function (block, ignoreIds)
1139
{
1140
if (block.y <= 1e-9)
1141
{ return true; }
724
1142
for (var i = 0; i < this.blocks.length; i++)
725
1143
{
726
if (this.blocks[i].clusterId === cluster.id)
727
{ remaining++; }
1144
var other = this.blocks[i];
1145
if (other === block || other.col !== block.col)
1146
{ continue; }
1147
if (ignoreIds && ignoreIds[other.id])
1148
{ continue; }
1149
if (!this.isGrounded(other))
1150
{ continue; }
1151
if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)
1152
{ return true; }
728
1153
}
729
if (remaining === 0)
730
{ delete this.clusters[cluster.id]; }
1154
return false;
731
1155
};
732
1156
733
1157
GameCore.prototype.integrateManual = function (dt)
@@ -764,8 +1188,11 @@ GameCore.prototype.integrateManual = function (dt)
764
1188
765
1189
if (block.manualPhase === 'down')
766
1190
{
1191
var proxy = { phase: 'down', vy: block.vy, decel: 0 };
1192
var dy = this.advanceJump(proxy, dt);
1193
block.vy = proxy.vy;
1194
var nextY = block.y + dy;
767
1195
var top = this.stackTop(block.col);
768
var nextY = block.y - this.planet.fallSpeed * dt;
769
1196
if (nextY <= top)
770
1197
{
771
1198
block.y = top;
@@ -773,11 +1200,11 @@ GameCore.prototype.integrateManual = function (dt)
773
1200
block.vy = 0;
774
1201
block.manualPhase = null;
775
1202
block.flame = false;
1203
this.startBounce(block);
776
1204
}
777
1205
else
778
1206
{
779
1207
block.y = Math.min(nextY, this.height - 1);
780
block.vy = -this.planet.fallSpeed;
781
1208
}
782
1209
}
783
1210
}
@@ -936,16 +1363,65 @@ GameCore.prototype.advanceSettle = function (dt)
936
1363
var block = this.blocks[i];
937
1364
if (block.state !== STATE_SETTLING)
938
1365
{ continue; }
939
block.settleTime += dt;
940
if (block.settleTime >= this.settleTime)
1366
var inLanded = block.clusterId != null
1367
&& this.clusters[block.clusterId]
1368
&& this.clusters[block.clusterId].phase === 'landed';
1369
if (block.color === COLOR_GRAY)
941
1370
{
942
block.color = this.randomColor();
943
block.state = STATE_STILL;
944
block.settleTime = 0;
945
block.flame = false;
1371
block.settleTime += dt;
1372
if (block.settleTime >= this.settleTime)
1373
{
1374
block.color = this.randomColor();
1375
block.settleTime = 0;
1376
block.flame = false;
1377
if (inLanded)
1378
{
1379
// Stay in the rigid group; releaseLandedCluster runs below.
1380
block.state = STATE_SETTLING;
1381
}
1382
else
1383
{
1384
this.leaveCluster(block);
1385
if (this.hasSupport(block))
1386
{
1387
block.state = STATE_STILL;
1388
block.vy = 0;
1389
}
1390
else
1391
{
1392
block.state = STATE_FALLING;
1393
block.vy = 0;
1394
}
1395
}
1396
}
1397
}
1398
else if (!inLanded)
1399
{
1400
// Coloured settling that is not in a landed cluster (shouldn't
1401
// normally happen): just become still or fall.
946
1402
this.leaveCluster(block);
1403
block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;
1404
if (block.state === STATE_FALLING)
1405
{ block.vy = 0; }
947
1406
}
948
1407
}
1408
1409
var ids = Object.keys(this.clusters);
1410
for (var n = 0; n < ids.length; n++)
1411
{
1412
var cluster = this.clusters[ids[n]];
1413
if (!cluster || cluster.phase !== 'landed')
1414
{ continue; }
1415
var members = this.clusterMembers(cluster.id);
1416
var greySettling = false;
1417
for (var i = 0; i < members.length; i++)
1418
{
1419
if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING)
1420
{ greySettling = true; }
1421
}
1422
if (!greySettling)
1423
{ this.releaseLandedCluster(cluster); }
1424
}
949
1425
};
950
1426
951
1427
GameCore.prototype.findMatchLines = function ()
@@ -1258,7 +1734,15 @@ GameCore.prototype.beginDrag = function (col, y)
1258
1734
var block = this.blockAt(col, row);
1259
1735
if (!block || block.state !== STATE_STILL || block.color < 0)
1260
1736
{ return false; }
1261
this.drag = { blockId: block.id, pointerY: y, launchIntent: false };
1737
var top = this.columnStackTopRow(col);
1738
var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS;
1739
this.drag = {
1740
blockId: block.id,
1741
pointerY: y,
1742
startY: y,
1743
launchIntent: false,
1744
startedAsTop: startedAsTop
1745
};
1262
1746
return true;
1263
1747
};
1264
1748
@@ -1280,9 +1764,19 @@ GameCore.prototype.moveDrag = function (y)
1280
1764
this.drag.pointerY = y;
1281
1765
this.swapToward(block, y);
1282
1766
var top = this.columnStackTopRow(block.col);
1283
// The top block occupies [top, top+1). Launch only when the pointer
1284
// is pulled into the empty space above the stack.
1285
this.drag.launchIntent = (Math.abs(block.y - top) < ALIGN_EPS && y > top + 1 + 0.25);
1767
var isTop = Math.abs(block.y - top) < ALIGN_EPS;
1768
if (this.drag.startedAsTop)
1769
{
1770
// Already the topmost block: pulling upward past its own position
1771
// (LAUNCH_PULL tiles above the press point) starts a manual jump.
1772
this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL);
1773
}
1774
else
1775
{
1776
// Started mid-stack: swap first, and only launch once the pointer
1777
// is pulled into the empty space above the (new) top.
1778
this.drag.launchIntent = isTop && (y > top + 1 + 0.25);
1779
}
1286
1780
};
1287
1781
1288
1782
GameCore.prototype.endDrag = function ()
@@ -1493,7 +1987,8 @@ var app = {
1493
1987
flames: Object.create(null),
1494
1988
cells: [],
1495
1989
pointerId: null,
1496
gameOverShown: false
1990
gameOverShown: false,
1991
selectTween: null
1497
1992
};
1498
1993
1499
1994
window.onload = function ()
@@ -1539,6 +2034,7 @@ function startPlay ()
1539
2034
app.flames = Object.create(null);
1540
2035
app.cells = [];
1541
2036
app.pointerId = null;
2037
app.selectTween = null;
1542
2038
1543
2039
layout();
1544
2040
app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH);
@@ -1566,6 +2062,7 @@ function returnToMenu ()
1566
2062
app.sprites = Object.create(null);
1567
2063
app.flames = Object.create(null);
1568
2064
app.cells = [];
2065
app.selectTween = null;
1569
2066
1570
2067
hideEl('hud');
1571
2068
hideEl('game-over');
@@ -1664,6 +2161,7 @@ function syncSprites ()
1664
2161
}
1665
2162
obj.renderer.sprite = spriteFor(block);
1666
2163
placeTile(obj, block.col, block.y);
2164
applyBlockVisual(obj, block);
1667
2165
obj.renderer.visible = true;
1668
2166
obj.renderer.alpha = 1;
1669
2167
@@ -1703,6 +2201,96 @@ function syncSprites ()
1703
2201
}
1704
2202
}
1705
2203
2204
function applyBlockVisual (obj, block)
2205
{
2206
var t = app.tile;
2207
var sx = 1;
2208
var sy = 1;
2209
var dy = 0;
2210
if (block.bouncing && app.planet && app.planet.bounceDuration > 0
2211
&& typeof Easing !== 'undefined' && Easing.bouncePose)
2212
{
2213
var u = block.bounceAge / app.planet.bounceDuration;
2214
var pose = Easing.bouncePose(u, app.planet.bounceSquash);
2215
sx = pose.sx;
2216
sy = pose.sy;
2217
dy = pose.dy;
2218
}
2219
var sel = 1;
2220
var tw = app.selectTween;
2221
if (tw && tw.blockId === block.id && tw.phase !== 'idle')
2222
{ sel = tw.scale; }
2223
obj.transform.size = { x: t * sx * sel, y: t * sy * sel };
2224
if (dy)
2225
{ obj.transform.position.y -= dy * t; }
2226
}
2227
2228
function clickTween ()
2229
{
2230
return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null;
2231
}
2232
2233
function startPressTween (blockId)
2234
{
2235
var c = clickTween();
2236
if (!c)
2237
{ return; }
2238
var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1;
2239
app.selectTween = {
2240
blockId: blockId,
2241
from: from,
2242
to: c.pressScale,
2243
t: 0,
2244
duration: c.pressDuration,
2245
ease: c.pressEase,
2246
scale: from,
2247
phase: 'press'
2248
};
2249
}
2250
2251
function startReleaseTween ()
2252
{
2253
var c = clickTween();
2254
var tw = app.selectTween;
2255
if (!c || !tw || tw.phase === 'idle')
2256
{ return; }
2257
app.selectTween = {
2258
blockId: tw.blockId,
2259
from: tw.scale,
2260
to: c.releaseScale,
2261
t: 0,
2262
duration: c.releaseDuration,
2263
ease: c.releaseEase,
2264
scale: tw.scale,
2265
phase: 'release'
2266
};
2267
}
2268
2269
function updateSelectTween (dt)
2270
{
2271
var tw = app.selectTween;
2272
if (!tw || tw.phase === 'idle' || tw.phase === 'hold')
2273
{ return; }
2274
tw.t += dt;
2275
var u = tw.duration > 0 ? tw.t / tw.duration : 1;
2276
if (u >= 1)
2277
{
2278
tw.scale = tw.to;
2279
tw.t = tw.duration;
2280
if (tw.phase === 'press')
2281
{ tw.phase = 'hold'; }
2282
else
2283
{
2284
tw.phase = 'idle';
2285
tw.scale = 1;
2286
tw.blockId = null;
2287
}
2288
return;
2289
}
2290
var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack);
2291
tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u;
2292
}
2293
1706
2294
function refreshHud ()
1707
2295
{
1708
2296
if (!app.core)
@@ -1743,6 +2331,7 @@ function tickView (dt)
1743
2331
}
1744
2332
1745
2333
app.core.tick(dt);
2334
updateSelectTween(dt);
1746
2335
syncSprites();
1747
2336
refreshHud();
1748
2337
@@ -1758,31 +2347,51 @@ function tickView (dt)
1758
2347
}
1759
2348
1760
2349
/* ---------- pointer input (mouse + touch) --------------------------- */
2350
/*
2351
* Both mouse and touch use Pointer Events. After pointerdown we listen
2352
* on window, so a drag that leaves the canvas (needed to launch a
2353
* block at the top of a full column) still reaches moveDrag. We do
2354
* not treat lostpointercapture as the end of a drag: on some touch
2355
* browsers that fires mid-gesture and would cancel a launch.
2356
*/
1761
2357
1762
2358
var boundCanvas = null;
2359
var windowListening = false;
2360
2361
function addWindowPointer ()
2362
{
2363
if (windowListening)
2364
{ return; }
2365
window.addEventListener('pointermove', onPointerMove, true);
2366
window.addEventListener('pointerup', onPointerUp, true);
2367
window.addEventListener('pointercancel', onPointerUp, true);
2368
windowListening = true;
2369
}
2370
2371
function removeWindowPointer ()
2372
{
2373
if (!windowListening)
2374
{ return; }
2375
window.removeEventListener('pointermove', onPointerMove, true);
2376
window.removeEventListener('pointerup', onPointerUp, true);
2377
window.removeEventListener('pointercancel', onPointerUp, true);
2378
windowListening = false;
2379
}
1763
2380
1764
2381
function bindPointer (canvas)
1765
2382
{
1766
2383
unbindPointer();
1767
2384
boundCanvas = canvas;
1768
2385
canvas.addEventListener('pointerdown', onPointerDown);
1769
canvas.addEventListener('pointermove', onPointerMove);
1770
canvas.addEventListener('pointerup', onPointerUp);
1771
canvas.addEventListener('pointercancel', onPointerUp);
1772
canvas.addEventListener('lostpointercapture', onPointerUp);
1773
2386
canvas.style.touchAction = 'none';
1774
2387
canvas.style.cursor = 'pointer';
1775
2388
}
1776
2389
1777
2390
function unbindPointer ()
1778
2391
{
1779
if (!boundCanvas)
1780
{ return; }
1781
boundCanvas.removeEventListener('pointerdown', onPointerDown);
1782
boundCanvas.removeEventListener('pointermove', onPointerMove);
1783
boundCanvas.removeEventListener('pointerup', onPointerUp);
1784
boundCanvas.removeEventListener('pointercancel', onPointerUp);
1785
boundCanvas.removeEventListener('lostpointercapture', onPointerUp);
2392
removeWindowPointer();
2393
if (boundCanvas)
2394
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }
1786
2395
boundCanvas = null;
1787
2396
app.pointerId = null;
1788
2397
}
@@ -1813,9 +2422,10 @@ function onPointerDown (e)
1813
2422
try
1814
2423
{ boundCanvas.setPointerCapture(e.pointerId); }
1815
2424
catch (err) { /* ignore */ }
2425
addWindowPointer();
1816
2426
var pos = fieldFromEvent(e);
1817
if (pos)
1818
{ app.core.beginDrag(pos.col, pos.y); }
2427
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)
2428
{ startPressTween(app.core.drag.blockId); }
1819
2429
}
1820
2430
1821
2431
function onPointerMove (e)
@@ -1839,6 +2449,14 @@ function onPointerUp (e)
1839
2449
if (e)
1840
2450
{ e.preventDefault(); }
1841
2451
app.pointerId = null;
2452
removeWindowPointer();
2453
if (boundCanvas)
2454
{
2455
try
2456
{ boundCanvas.releasePointerCapture(e && e.pointerId); }
2457
catch (err) { /* ignore */ }
2458
}
2459
startReleaseTween();
1842
2460
if (app.core)
1843
2461
{ app.core.endDrag(); }
1844
2462
}
@@ -121,7 +121,14 @@ function createPlanet (opts)121
riseTime: riseTime,121
riseTime: riseTime,122
colors: colors,122
colors: colors,123
baseJumpHeight: FIELD_HEIGHT / jumpsToClear,123
baseJumpHeight: FIELD_HEIGHT / jumpsToClear,124
fallSpeed: FIELD_HEIGHT / fallTime124
fallSpeed: FIELD_HEIGHT / fallTime,125
// Time to ease from vy=0 at the apex to terminal fallSpeed.126
// Shorter when air resistance is low (small fallTime).127
apexEaseTime: 0.15 * fallTime,128
fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime),129
// Visual landing bounce: faster at low air resistance. Stage-3 params.130
bounceDuration: 0.12 * fallTime,131
bounceSquash: 0.16125
};132
};126
return planet;133
return planet;127
}134
}@@ -232,6 +239,127 @@ else if (typeof globalThis !== 'undefined')232
* /workspace/meteors/src/planet.js239
* /workspace/meteors/src/planet.js233
*/ 240
*/ 234
241
242
/*243
* Start File:244
* /workspace/meteors/src/easing.js245
*/ 246
'use strict';247
248
/**249
* Easing curves matching Qt QEasingCurve defaults (the same values Tim's250
* KWin Click Tween effect uses). Formulas and default parameters come from251
* Qt 6's QEasingCurve and https://easings.net/.252
*253
* Source of the click/release defaults:254
* /workspace/click-tween/settings.h (clickTime, releaseTime, easing names)255
* /workspace/click-tween/clicktween.cpp (press scale 0.8, release scale 1.0)256
* /workspace/click-tween/README.md257
*/258
259
/** Qt QEasingCurve default overshoot for InBack / OutBack / InOutBack. */260
var BACK_OVERSHOOT = 1.70158;261
262
/** Qt QEasingCurve default period and amplitude for Elastic curves. */263
var ELASTIC_PERIOD = 0.3;264
var ELASTIC_AMPLITUDE = 1.0;265
266
function clamp01 (t)267
{268
if (t <= 0)269
{ return 0; }270
if (t >= 1)271
{ return 1; }272
return t;273
}274
275
/** easeOutBack: overshoots the end value, then settles. Qt / easings.net. */276
function easeOutBack (t)277
{278
t = clamp01(t);279
var c1 = BACK_OVERSHOOT;280
var c3 = c1 + 1;281
var u = t - 1;282
return 1 + c3 * u * u * u + c1 * u * u;283
}284
285
/**286
* easeOutElastic: decaying sine overshoot past the end value.287
* Qt default amplitude 1, period 0.3 (equivalent to easings.net).288
*/289
function easeOutElastic (t)290
{291
t = clamp01(t);292
if (t === 0 || t === 1)293
{ return t; }294
var p = ELASTIC_PERIOD;295
var s = p / 4;296
return Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p) + 1;297
}298
299
function tween (from, to, t, ease)300
{301
return from + (to - from) * ease(clamp01(t));302
}303
304
/**305
* DEFAULT settings of Tim's KWin Click Tween306
* (git.timfalken.com/timfa/click-tween).307
*308
* Press: shrink to 80% in 150 ms with easeOutBack, stay at 80% while held.309
* Release: grow back to 100% in 720 ms with easeOutElastic.310
* Mid-animation release starts from the current scale, like QVariantAnimation.311
*/312
var CLICK_TWEEN = {313
pressScale: 0.8,314
releaseScale: 1.0,315
pressDuration: 0.150,316
releaseDuration: 0.720,317
pressEase: easeOutBack,318
releaseEase: easeOutElastic,319
backOvershoot: BACK_OVERSHOOT,320
elasticPeriod: ELASTIC_PERIOD,321
elasticAmplitude: ELASTIC_AMPLITUDE,322
source: 'click-tween settings.h + clicktween.cpp (Qt QEasingCurve defaults)'323
};324
325
/**326
* Visual landing bounce, u in 0..1. Subtle squash with a slight stretch327
* on the other axis and a tiny upward rebound (in tiles, visual only).328
*/329
function bouncePose (u, squash)330
{331
if (u <= 0 || u >= 1)332
{ return { sx: 1, sy: 1, dy: 0 }; }333
var wave = Math.sin(Math.PI * u);334
return {335
sx: 1 + squash * 0.6 * wave,336
sy: 1 - squash * wave,337
dy: 0.07 * wave * (1 - u)338
};339
}340
341
var Easing = {342
BACK_OVERSHOOT: BACK_OVERSHOOT,343
ELASTIC_PERIOD: ELASTIC_PERIOD,344
ELASTIC_AMPLITUDE: ELASTIC_AMPLITUDE,345
easeOutBack: easeOutBack,346
easeOutElastic: easeOutElastic,347
tween: tween,348
CLICK_TWEEN: CLICK_TWEEN,349
clamp01: clamp01,350
bouncePose: bouncePose351
};352
353
if (typeof module !== 'undefined' && module.exports)354
{ module.exports = Easing; }355
else if (typeof globalThis !== 'undefined')356
{ globalThis.Easing = Easing; }357
358
/*359
* End File:360
* /workspace/meteors/src/easing.js361
*/ 362
235
/*363
/*236
* Start File:364
* Start File:237
* /workspace/meteors/src/gameCore.js365
* /workspace/meteors/src/gameCore.js@@ -260,6 +388,8 @@ var STATE_MANUAL = 'manual';260
var ALIGN_EPS = 0.15;388
var ALIGN_EPS = 0.15;261
var SUBSTEP = 1 / 120;389
var SUBSTEP = 1 / 120;262
var DRAG_LAUNCH_SLACK = 0.65;390
var DRAG_LAUNCH_SLACK = 0.65;391
// Upward pull (tiles past the press point) that launches an already-topmost block.392
var LAUNCH_PULL = 0.35;263
393
264
function UnionFind ()394
function UnionFind ()265
{395
{@@ -309,6 +439,7 @@ function GameCore (planet, rng)309
}439
}310
440
311
GameCore.COLOR_GRAY = COLOR_GRAY;441
GameCore.COLOR_GRAY = COLOR_GRAY;442
GameCore.LAUNCH_PULL = LAUNCH_PULL;312
GameCore.STATE_STILL = STATE_STILL;443
GameCore.STATE_STILL = STATE_STILL;313
GameCore.STATE_FALLING = STATE_FALLING;444
GameCore.STATE_FALLING = STATE_FALLING;314
GameCore.STATE_FLYING = STATE_FLYING;445
GameCore.STATE_FLYING = STATE_FLYING;@@ -336,7 +467,9 @@ GameCore.prototype.createBlock = function (col, y, color, state)336
pushedClusters: null,467
pushedClusters: null,337
manualTarget: 0,468
manualTarget: 0,338
manualDecel: 0,469
manualDecel: 0,339
manualPhase: null470
manualPhase: null,471
bouncing: false,472
bounceAge: 0340
};473
};341
this.blocks.push(block);474
this.blocks.push(block);342
return block;475
return block;@@ -474,6 +607,199 @@ GameCore.prototype.lockedColumnCount = function ()474
return n;607
return n;475
};608
};476
609
610
GameCore.prototype.startBounce = function (block)611
{612
if (!block)613
{ return; }614
block.bouncing = true;615
block.bounceAge = 0;616
};617
618
GameCore.prototype.advanceBounces = function (dt)619
{620
var duration = this.planet.bounceDuration;621
for (var i = 0; i < this.blocks.length; i++)622
{623
var block = this.blocks[i];624
if (!block.bouncing)625
{ continue; }626
block.bounceAge += dt;627
if (duration <= 0 || block.bounceAge >= duration)628
{629
block.bouncing = false;630
block.bounceAge = duration;631
}632
}633
};634
635
/**636
* True if `block` rests on the floor or on a solid (still / settling /637
* locked) block immediately below it in the same column.638
*/639
GameCore.prototype.hasSupport = function (block)640
{641
if (block.y <= 1e-9)642
{ return true; }643
for (var i = 0; i < this.blocks.length; i++)644
{645
var other = this.blocks[i];646
if (other === block || other.col !== block.col)647
{ continue; }648
if (!this.isGrounded(other))649
{ continue; }650
if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)651
{ return true; }652
}653
return false;654
};655
656
/** Highest flying block in this column whose top is at or below `block.y + 1`. */657
GameCore.prototype.highestFlyingBelow = function (block)658
{659
var best = null;660
for (var i = 0; i < this.blocks.length; i++)661
{662
var other = this.blocks[i];663
if (other === block || other.col !== block.col)664
{ continue; }665
if (other.state !== STATE_FLYING || other.clusterId == null)666
{ continue; }667
var cluster = this.clusters[other.clusterId];668
if (!cluster || cluster.phase === 'landed')669
{ continue; }670
// The falling block is at or above this flyer (or already overlapping).671
if (block.y + 1e-9 < other.y)672
{ continue; }673
if (!best || other.y > best.y)674
{ best = other; }675
}676
return best;677
};678
679
GameCore.prototype.highestFallingBelow = function (block)680
{681
var best = null;682
for (var i = 0; i < this.blocks.length; i++)683
{684
var other = this.blocks[i];685
if (other === block || other.col !== block.col)686
{ continue; }687
if (other.state !== STATE_FALLING)688
{ continue; }689
if (other.y > block.y - 1e-9)690
{ continue; }691
if (!best || other.y > best.y)692
{ best = other; }693
}694
return best;695
};696
697
/**698
* Make an unsupported still block start falling from rest, so it eases699
* into fallSpeed under normal gravity. Skips the block being dragged.700
*/701
GameCore.prototype.releaseUnsupported = function ()702
{703
for (var i = 0; i < this.blocks.length; i++)704
{705
var block = this.blocks[i];706
if (block.state !== STATE_STILL)707
{ continue; }708
if (this.drag && this.drag.blockId === block.id)709
{ continue; }710
if (this.hasSupport(block))711
{ continue; }712
block.state = STATE_FALLING;713
block.vy = 0;714
block.flame = false;715
this.leaveCluster(block);716
}717
};718
719
GameCore.prototype.accelerateFall = function (block, dt)720
{721
var target = -this.planet.fallSpeed;722
if (block.vy > target)723
{ block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); }724
else725
{ block.vy = target; }726
};727
728
GameCore.prototype.landFallingBlock = function (block)729
{730
block.vy = 0;731
block.flame = false;732
this.startBounce(block);733
if (block.color === COLOR_GRAY)734
{735
block.state = STATE_SETTLING;736
block.settleTime = 0;737
}738
else739
{ block.state = STATE_STILL; }740
};741
742
/**743
* Sit `block` on top of the flying cluster that `flyer` belongs to, and744
* carry it along as cargo (no flame). Returns true if attached.745
*/746
GameCore.prototype.attachToFlyingCluster = function (block, flyer)747
{748
var cluster = this.clusters[flyer.clusterId];749
if (!cluster || cluster.phase === 'landed')750
{ return false; }751
var roof = flyer.y + 1;752
for (var i = 0; i < this.blocks.length; i++)753
{754
var other = this.blocks[i];755
if (other === block || other.col !== block.col)756
{ continue; }757
if (other.clusterId !== cluster.id)758
{ continue; }759
if (other.y + 1 > roof)760
{ roof = other.y + 1; }761
}762
block.y = roof;763
block.state = STATE_FLYING;764
block.clusterId = cluster.id;765
block.vy = cluster.vy;766
block.flame = false;767
block.settleTime = 0;768
return true;769
};770
771
GameCore.prototype.catchFallingOntoCluster = function (cluster, members)772
{773
if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold')774
{ return; }775
// Snapshot falling blocks; attaching mutates state.776
var falling = [];777
for (var i = 0; i < this.blocks.length; i++)778
{779
if (this.blocks[i].state === STATE_FALLING)780
{ falling.push(this.blocks[i]); }781
}782
for (var i = 0; i < falling.length; i++)783
{784
var block = falling[i];785
var flyer = null;786
for (var m = 0; m < members.length; m++)787
{788
var other = members[m];789
if (other.col !== block.col)790
{ continue; }791
// Overlap or rest on this member's top.792
if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9)793
{794
if (!flyer || other.y > flyer.y)795
{ flyer = other; }796
}797
}798
if (flyer)799
{ this.attachToFlyingCluster(block, flyer); }800
}801
};802
477
GameCore.prototype.tick = function (dt)803
GameCore.prototype.tick = function (dt)478
{804
{479
if (this.state === 'gameover')805
if (this.state === 'gameover')@@ -503,7 +829,9 @@ GameCore.prototype.tick = function (dt)503
left -= step;829
left -= step;504
}830
}505
831
832
this.releaseUnsupported();506
this.advanceSettle(dt);833
this.advanceSettle(dt);834
this.advanceBounces(dt);507
if (!this.drag)835
if (!this.drag)508
{ this.resolveMatches(); }836
{ this.resolveMatches(); }509
this.checkLocks();837
this.checkLocks();@@ -551,24 +879,42 @@ GameCore.prototype.trySpawn = function ()551
{ return null; }879
{ return null; }552
var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];880
var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length];553
var color = this.randomColor();881
var color = this.randomColor();554
return this.createBlock(col, this.height, color, STATE_FALLING);882
var block = this.createBlock(col, this.height, color, STATE_FALLING);883
block.vy = -this.planet.fallSpeed;884
return block;555
};885
};556
886
557
GameCore.prototype.integrateFalling = function (dt)887
GameCore.prototype.integrateFalling = function (dt)558
{888
{559
var speed = this.planet.fallSpeed;889
var falling = [];560
for (var i = 0; i < this.blocks.length; i++)890
for (var i = 0; i < this.blocks.length; i++)561
{891
{562
var block = this.blocks[i];892
if (this.blocks[i].state === STATE_FALLING)893
{ falling.push(this.blocks[i]); }894
}895
// Lowest first, so a stack of falling blocks rests on itself.896
falling.sort(function (a, b) { return a.y - b.y; });897
for (var i = 0; i < falling.length; i++)898
{899
var block = falling[i];563
if (block.state !== STATE_FALLING)900
if (block.state !== STATE_FALLING)564
{ continue; }901
{ continue; }902
this.accelerateFall(block, dt);903
var nextY = block.y + block.vy * dt;904
var flyer = this.highestFlyingBelow(block);905
if (flyer && nextY <= flyer.y + 1 + 1e-9)906
{907
if (this.attachToFlyingCluster(block, flyer))908
{ continue; }909
}565
var top = this.stackTop(block.col);910
var top = this.stackTop(block.col);566
var nextY = block.y - speed * dt;911
var below = this.highestFallingBelow(block);912
if (below && below.y + 1 > top)913
{ top = below.y + 1; }567
if (nextY <= top)914
if (nextY <= top)568
{915
{569
block.y = top;916
block.y = top;570
block.state = STATE_STILL;917
this.landFallingBlock(block);571
block.vy = 0;572
}918
}573
else919
else574
{ block.y = nextY; }920
{ block.y = nextY; }@@ -591,7 +937,8 @@ GameCore.prototype.clusterMembers = function (clusterId)591
* Advance a jump (cluster or manual) by dt. Uses exact kinematics so a937
* Advance a jump (cluster or manual) by dt. Uses exact kinematics so a592
* jump of height H peaks at exactly H after riseTime. Returns dy.938
* jump of height H peaks at exactly H after riseTime. Returns dy.593
* Mutates body.vy and body.phase ('up' / 'down'). body.decel is the939
* Mutates body.vy and body.phase ('up' / 'down'). body.decel is the594
* upward deceleration; falling uses planet.fallSpeed.940
* upward deceleration. After the apex, vy eases from 0 to -fallSpeed941
* at planet.fallAccel, then stays at terminal fallSpeed.595
*/942
*/596
GameCore.prototype.advanceJump = function (body, dt)943
GameCore.prototype.advanceJump = function (body, dt)597
{944
{@@ -624,8 +971,35 @@ GameCore.prototype.advanceJump = function (body, dt)624
}971
}625
if (body.phase === 'down' && left > 0)972
if (body.phase === 'down' && left > 0)626
{973
{627
body.vy = -this.planet.fallSpeed;974
var target = -this.planet.fallSpeed;628
dy += body.vy * left;975
var accel = this.planet.fallAccel;976
if (accel < 1e-12)977
{978
body.vy = target;979
dy += body.vy * left;980
}981
else if (body.vy > target + 1e-12)982
{983
var tReach = (body.vy - target) / accel;984
if (tReach <= left + 1e-15)985
{986
dy += (body.vy + target) * 0.5 * tReach;987
body.vy = target;988
left -= tReach;989
dy += body.vy * left;990
}991
else992
{993
var vyNew = body.vy - accel * left;994
dy += (body.vy + vyNew) * 0.5 * left;995
body.vy = vyNew;996
}997
}998
else999
{1000
body.vy = target;1001
dy += body.vy * left;1002
}629
}1003
}630
return dy;1004
return dy;631
};1005
};@@ -654,14 +1028,18 @@ GameCore.prototype.integrateClusters = function (dt)654
members[i].vy = cluster.vy;1028
members[i].vy = cluster.vy;655
}1029
}656
1030
1031
this.catchFallingOntoCluster(cluster, members);1032
657
if (cluster.phase === 'down')1033
if (cluster.phase === 'down')658
{ this.tryLandCluster(cluster, members); }1034
{ this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); }659
}1035
}660
};1036
};661
1037
662
GameCore.prototype.tryLandCluster = function (cluster, members)1038
GameCore.prototype.tryLandCluster = function (cluster, members)663
{1039
{664
var self = this;1040
if (!members.length)1041
{ return; }1042
var shift = 0;665
var hit = false;1043
var hit = false;666
for (var i = 0; i < members.length; i++)1044
for (var i = 0; i < members.length; i++)667
{1045
{@@ -671,63 +1049,109 @@ GameCore.prototype.tryLandCluster = function (cluster, members)671
return other.clusterId === cluster.id;1049
return other.clusterId === cluster.id;672
});1050
});673
if (block.y <= top + 1e-9)1051
if (block.y <= top + 1e-9)674
{ hit = true; }1052
{1053
hit = true;1054
var need = top - block.y;1055
if (need > shift)1056
{ shift = need; }1057
}675
}1058
}676
if (!hit)1059
if (!hit)677
{ return; }1060
{ return; }1061
// Lift the whole rigid body so the deepest contact sits flush.1062
for (var i = 0; i < members.length; i++)1063
{ members[i].y += shift; }678
this.landCluster(cluster, members);1064
this.landCluster(cluster, members);679
};1065
};680
1066
1067
/**1068
* Land as a rigid group: relative positions are kept, so part of the1069
* cluster may hang above a gap. Grey members start the 3s settle timer;1070
* coloured cargo stays put with them until every grey has recolored.1071
* Members that actually rest on the floor or on another block bounce.1072
*/681
GameCore.prototype.landCluster = function (cluster, members)1073
GameCore.prototype.landCluster = function (cluster, members)682
{1074
{683
var byCol = Object.create(null);684
for (var i = 0; i < members.length; i++)1075
for (var i = 0; i < members.length; i++)685
{1076
{686
var block = members[i];1077
var block = members[i];687
if (!byCol[block.col])1078
block.vy = 0;688
{ byCol[block.col] = []; }1079
block.flame = false;689
byCol[block.col].push(block);1080
block.clusterId = cluster.id;1081
if (block.color === COLOR_GRAY)1082
{1083
block.state = STATE_SETTLING;1084
block.settleTime = 0;1085
}1086
else1087
{1088
// Cargo stays in the rigid group (not matchable) until release.1089
block.state = STATE_SETTLING;1090
block.settleTime = 0;1091
}1092
if (this.hasSupport(block))1093
{ this.startBounce(block); }690
}1094
}691
var cols = Object.keys(byCol);1095
cluster.phase = 'landed';692
for (var c = 0; c < cols.length; c++)1096
cluster.vy = 0;1097
};1098
1099
/**1100
* After every grey in a landed cluster has recolored, let the group go.1101
* Unsupported blocks fall individually from rest; supported ones stay.1102
*/1103
GameCore.prototype.releaseLandedCluster = function (cluster)1104
{1105
var members = this.clusterMembers(cluster.id);1106
var releasing = Object.create(null);1107
for (var i = 0; i < members.length; i++)1108
{ releasing[members[i].id] = true; }1109
1110
for (var i = 0; i < members.length; i++)693
{1111
{694
var col = +cols[c];1112
var block = members[i];695
var list = byCol[col];1113
if (block.color === COLOR_GRAY)696
list.sort(function (a, b) { return a.y - b.y; });697
var self = this;698
var top = this.stackTop(col, function (other)699
{1114
{700
return other.clusterId === cluster.id;1115
block.color = this.randomColor();701
});1116
block.settleTime = 0;702
for (var i = 0; i < list.length; i++)1117
}1118
block.flame = false;1119
this.leaveCluster(block);1120
// Support from outside the group (floor or a foreign block). Fellow1121
// members that will also fall do not count, so a hanging stack1122
// drops together without the top one staying put.1123
var supported = this.hasSupportOutside(block, releasing);1124
if (supported)703
{1125
{704
var block = list[i];1126
block.state = STATE_STILL;705
block.y = top;1127
block.vy = 0;1128
}1129
else1130
{1131
block.state = STATE_FALLING;706
block.vy = 0;1132
block.vy = 0;707
block.flame = false;708
top = block.y + 1;709
if (block.color === COLOR_GRAY)710
{711
block.state = STATE_SETTLING;712
block.settleTime = 0;713
}714
else715
{716
block.state = STATE_STILL;717
block.clusterId = null;718
}719
}1133
}720
}1134
}721
cluster.phase = 'landed';1135
delete this.clusters[cluster.id];722
cluster.vy = 0;1136
};723
var remaining = 0;1137
1138
GameCore.prototype.hasSupportOutside = function (block, ignoreIds)1139
{1140
if (block.y <= 1e-9)1141
{ return true; }724
for (var i = 0; i < this.blocks.length; i++)1142
for (var i = 0; i < this.blocks.length; i++)725
{1143
{726
if (this.blocks[i].clusterId === cluster.id)1144
var other = this.blocks[i];727
{ remaining++; }1145
if (other === block || other.col !== block.col)1146
{ continue; }1147
if (ignoreIds && ignoreIds[other.id])1148
{ continue; }1149
if (!this.isGrounded(other))1150
{ continue; }1151
if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS)1152
{ return true; }728
}1153
}729
if (remaining === 0)1154
return false;730
{ delete this.clusters[cluster.id]; }731
};1155
};732
1156
733
GameCore.prototype.integrateManual = function (dt)1157
GameCore.prototype.integrateManual = function (dt)@@ -764,8 +1188,11 @@ GameCore.prototype.integrateManual = function (dt)764
1188
765
if (block.manualPhase === 'down')1189
if (block.manualPhase === 'down')766
{1190
{1191
var proxy = { phase: 'down', vy: block.vy, decel: 0 };1192
var dy = this.advanceJump(proxy, dt);1193
block.vy = proxy.vy;1194
var nextY = block.y + dy;767
var top = this.stackTop(block.col);1195
var top = this.stackTop(block.col);768
var nextY = block.y - this.planet.fallSpeed * dt;769
if (nextY <= top)1196
if (nextY <= top)770
{1197
{771
block.y = top;1198
block.y = top;@@ -773,11 +1200,11 @@ GameCore.prototype.integrateManual = function (dt)773
block.vy = 0;1200
block.vy = 0;774
block.manualPhase = null;1201
block.manualPhase = null;775
block.flame = false;1202
block.flame = false;1203
this.startBounce(block);776
}1204
}777
else1205
else778
{1206
{779
block.y = Math.min(nextY, this.height - 1);1207
block.y = Math.min(nextY, this.height - 1);780
block.vy = -this.planet.fallSpeed;781
}1208
}782
}1209
}783
}1210
}@@ -936,16 +1363,65 @@ GameCore.prototype.advanceSettle = function (dt)936
var block = this.blocks[i];1363
var block = this.blocks[i];937
if (block.state !== STATE_SETTLING)1364
if (block.state !== STATE_SETTLING)938
{ continue; }1365
{ continue; }939
block.settleTime += dt;1366
var inLanded = block.clusterId != null940
if (block.settleTime >= this.settleTime)1367
&& this.clusters[block.clusterId]1368
&& this.clusters[block.clusterId].phase === 'landed';1369
if (block.color === COLOR_GRAY)941
{1370
{942
block.color = this.randomColor();1371
block.settleTime += dt;943
block.state = STATE_STILL;1372
if (block.settleTime >= this.settleTime)944
block.settleTime = 0;1373
{945
block.flame = false;1374
block.color = this.randomColor();1375
block.settleTime = 0;1376
block.flame = false;1377
if (inLanded)1378
{1379
// Stay in the rigid group; releaseLandedCluster runs below.1380
block.state = STATE_SETTLING;1381
}1382
else1383
{1384
this.leaveCluster(block);1385
if (this.hasSupport(block))1386
{1387
block.state = STATE_STILL;1388
block.vy = 0;1389
}1390
else1391
{1392
block.state = STATE_FALLING;1393
block.vy = 0;1394
}1395
}1396
}1397
}1398
else if (!inLanded)1399
{1400
// Coloured settling that is not in a landed cluster (shouldn't1401
// normally happen): just become still or fall.946
this.leaveCluster(block);1402
this.leaveCluster(block);1403
block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING;1404
if (block.state === STATE_FALLING)1405
{ block.vy = 0; }947
}1406
}948
}1407
}1408
1409
var ids = Object.keys(this.clusters);1410
for (var n = 0; n < ids.length; n++)1411
{1412
var cluster = this.clusters[ids[n]];1413
if (!cluster || cluster.phase !== 'landed')1414
{ continue; }1415
var members = this.clusterMembers(cluster.id);1416
var greySettling = false;1417
for (var i = 0; i < members.length; i++)1418
{1419
if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING)1420
{ greySettling = true; }1421
}1422
if (!greySettling)1423
{ this.releaseLandedCluster(cluster); }1424
}949
};1425
};950
1426
951
GameCore.prototype.findMatchLines = function ()1427
GameCore.prototype.findMatchLines = function ()@@ -1258,7 +1734,15 @@ GameCore.prototype.beginDrag = function (col, y)1258
var block = this.blockAt(col, row);1734
var block = this.blockAt(col, row);1259
if (!block || block.state !== STATE_STILL || block.color < 0)1735
if (!block || block.state !== STATE_STILL || block.color < 0)1260
{ return false; }1736
{ return false; }1261
this.drag = { blockId: block.id, pointerY: y, launchIntent: false };1737
var top = this.columnStackTopRow(col);1738
var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS;1739
this.drag = {1740
blockId: block.id,1741
pointerY: y,1742
startY: y,1743
launchIntent: false,1744
startedAsTop: startedAsTop1745
};1262
return true;1746
return true;1263
};1747
};1264
1748
@@ -1280,9 +1764,19 @@ GameCore.prototype.moveDrag = function (y)1280
this.drag.pointerY = y;1764
this.drag.pointerY = y;1281
this.swapToward(block, y);1765
this.swapToward(block, y);1282
var top = this.columnStackTopRow(block.col);1766
var top = this.columnStackTopRow(block.col);1283
// The top block occupies [top, top+1). Launch only when the pointer1767
var isTop = Math.abs(block.y - top) < ALIGN_EPS;1284
// is pulled into the empty space above the stack.1768
if (this.drag.startedAsTop)1285
this.drag.launchIntent = (Math.abs(block.y - top) < ALIGN_EPS && y > top + 1 + 0.25);1769
{1770
// Already the topmost block: pulling upward past its own position1771
// (LAUNCH_PULL tiles above the press point) starts a manual jump.1772
this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL);1773
}1774
else1775
{1776
// Started mid-stack: swap first, and only launch once the pointer1777
// is pulled into the empty space above the (new) top.1778
this.drag.launchIntent = isTop && (y > top + 1 + 0.25);1779
}1286
};1780
};1287
1781
1288
GameCore.prototype.endDrag = function ()1782
GameCore.prototype.endDrag = function ()@@ -1493,7 +1987,8 @@ var app = {1493
flames: Object.create(null),1987
flames: Object.create(null),1494
cells: [],1988
cells: [],1495
pointerId: null,1989
pointerId: null,1496
gameOverShown: false1990
gameOverShown: false,1991
selectTween: null1497
};1992
};1498
1993
1499
window.onload = function ()1994
window.onload = function ()@@ -1539,6 +2034,7 @@ function startPlay ()1539
app.flames = Object.create(null);2034
app.flames = Object.create(null);1540
app.cells = [];2035
app.cells = [];1541
app.pointerId = null;2036
app.pointerId = null;2037
app.selectTween = null;1542
2038
1543
layout();2039
layout();1544
app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH);2040
app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH);@@ -1566,6 +2062,7 @@ function returnToMenu ()1566
app.sprites = Object.create(null);2062
app.sprites = Object.create(null);1567
app.flames = Object.create(null);2063
app.flames = Object.create(null);1568
app.cells = [];2064
app.cells = [];2065
app.selectTween = null;1569
2066
1570
hideEl('hud');2067
hideEl('hud');1571
hideEl('game-over');2068
hideEl('game-over');@@ -1664,6 +2161,7 @@ function syncSprites ()1664
}2161
}1665
obj.renderer.sprite = spriteFor(block);2162
obj.renderer.sprite = spriteFor(block);1666
placeTile(obj, block.col, block.y);2163
placeTile(obj, block.col, block.y);2164
applyBlockVisual(obj, block);1667
obj.renderer.visible = true;2165
obj.renderer.visible = true;1668
obj.renderer.alpha = 1;2166
obj.renderer.alpha = 1;1669
2167
@@ -1703,6 +2201,96 @@ function syncSprites ()1703
}2201
}1704
}2202
}1705
2203
2204
function applyBlockVisual (obj, block)2205
{2206
var t = app.tile;2207
var sx = 1;2208
var sy = 1;2209
var dy = 0;2210
if (block.bouncing && app.planet && app.planet.bounceDuration > 02211
&& typeof Easing !== 'undefined' && Easing.bouncePose)2212
{2213
var u = block.bounceAge / app.planet.bounceDuration;2214
var pose = Easing.bouncePose(u, app.planet.bounceSquash);2215
sx = pose.sx;2216
sy = pose.sy;2217
dy = pose.dy;2218
}2219
var sel = 1;2220
var tw = app.selectTween;2221
if (tw && tw.blockId === block.id && tw.phase !== 'idle')2222
{ sel = tw.scale; }2223
obj.transform.size = { x: t * sx * sel, y: t * sy * sel };2224
if (dy)2225
{ obj.transform.position.y -= dy * t; }2226
}2227
2228
function clickTween ()2229
{2230
return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null;2231
}2232
2233
function startPressTween (blockId)2234
{2235
var c = clickTween();2236
if (!c)2237
{ return; }2238
var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1;2239
app.selectTween = {2240
blockId: blockId,2241
from: from,2242
to: c.pressScale,2243
t: 0,2244
duration: c.pressDuration,2245
ease: c.pressEase,2246
scale: from,2247
phase: 'press'2248
};2249
}2250
2251
function startReleaseTween ()2252
{2253
var c = clickTween();2254
var tw = app.selectTween;2255
if (!c || !tw || tw.phase === 'idle')2256
{ return; }2257
app.selectTween = {2258
blockId: tw.blockId,2259
from: tw.scale,2260
to: c.releaseScale,2261
t: 0,2262
duration: c.releaseDuration,2263
ease: c.releaseEase,2264
scale: tw.scale,2265
phase: 'release'2266
};2267
}2268
2269
function updateSelectTween (dt)2270
{2271
var tw = app.selectTween;2272
if (!tw || tw.phase === 'idle' || tw.phase === 'hold')2273
{ return; }2274
tw.t += dt;2275
var u = tw.duration > 0 ? tw.t / tw.duration : 1;2276
if (u >= 1)2277
{2278
tw.scale = tw.to;2279
tw.t = tw.duration;2280
if (tw.phase === 'press')2281
{ tw.phase = 'hold'; }2282
else2283
{2284
tw.phase = 'idle';2285
tw.scale = 1;2286
tw.blockId = null;2287
}2288
return;2289
}2290
var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack);2291
tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u;2292
}2293
1706
function refreshHud ()2294
function refreshHud ()1707
{2295
{1708
if (!app.core)2296
if (!app.core)@@ -1743,6 +2331,7 @@ function tickView (dt)1743
}2331
}1744
2332
1745
app.core.tick(dt);2333
app.core.tick(dt);2334
updateSelectTween(dt);1746
syncSprites();2335
syncSprites();1747
refreshHud();2336
refreshHud();1748
2337
@@ -1758,31 +2347,51 @@ function tickView (dt)1758
}2347
}1759
2348
1760
/* ---------- pointer input (mouse + touch) --------------------------- */2349
/* ---------- pointer input (mouse + touch) --------------------------- */2350
/*2351
* Both mouse and touch use Pointer Events. After pointerdown we listen2352
* on window, so a drag that leaves the canvas (needed to launch a2353
* block at the top of a full column) still reaches moveDrag. We do2354
* not treat lostpointercapture as the end of a drag: on some touch2355
* browsers that fires mid-gesture and would cancel a launch.2356
*/1761
2357
1762
var boundCanvas = null;2358
var boundCanvas = null;2359
var windowListening = false;2360
2361
function addWindowPointer ()2362
{2363
if (windowListening)2364
{ return; }2365
window.addEventListener('pointermove', onPointerMove, true);2366
window.addEventListener('pointerup', onPointerUp, true);2367
window.addEventListener('pointercancel', onPointerUp, true);2368
windowListening = true;2369
}2370
2371
function removeWindowPointer ()2372
{2373
if (!windowListening)2374
{ return; }2375
window.removeEventListener('pointermove', onPointerMove, true);2376
window.removeEventListener('pointerup', onPointerUp, true);2377
window.removeEventListener('pointercancel', onPointerUp, true);2378
windowListening = false;2379
}1763
2380
1764
function bindPointer (canvas)2381
function bindPointer (canvas)1765
{2382
{1766
unbindPointer();2383
unbindPointer();1767
boundCanvas = canvas;2384
boundCanvas = canvas;1768
canvas.addEventListener('pointerdown', onPointerDown);2385
canvas.addEventListener('pointerdown', onPointerDown);1769
canvas.addEventListener('pointermove', onPointerMove);1770
canvas.addEventListener('pointerup', onPointerUp);1771
canvas.addEventListener('pointercancel', onPointerUp);1772
canvas.addEventListener('lostpointercapture', onPointerUp);1773
canvas.style.touchAction = 'none';2386
canvas.style.touchAction = 'none';1774
canvas.style.cursor = 'pointer';2387
canvas.style.cursor = 'pointer';1775
}2388
}1776
2389
1777
function unbindPointer ()2390
function unbindPointer ()1778
{2391
{1779
if (!boundCanvas)2392
removeWindowPointer();1780
{ return; }2393
if (boundCanvas)1781
boundCanvas.removeEventListener('pointerdown', onPointerDown);2394
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }1782
boundCanvas.removeEventListener('pointermove', onPointerMove);1783
boundCanvas.removeEventListener('pointerup', onPointerUp);1784
boundCanvas.removeEventListener('pointercancel', onPointerUp);1785
boundCanvas.removeEventListener('lostpointercapture', onPointerUp);1786
boundCanvas = null;2395
boundCanvas = null;1787
app.pointerId = null;2396
app.pointerId = null;1788
}2397
}@@ -1813,9 +2422,10 @@ function onPointerDown (e)1813
try2422
try1814
{ boundCanvas.setPointerCapture(e.pointerId); }2423
{ boundCanvas.setPointerCapture(e.pointerId); }1815
catch (err) { /* ignore */ }2424
catch (err) { /* ignore */ }2425
addWindowPointer();1816
var pos = fieldFromEvent(e);2426
var pos = fieldFromEvent(e);1817
if (pos)2427
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)1818
{ app.core.beginDrag(pos.col, pos.y); }2428
{ startPressTween(app.core.drag.blockId); }1819
}2429
}1820
2430
1821
function onPointerMove (e)2431
function onPointerMove (e)@@ -1839,6 +2449,14 @@ function onPointerUp (e)1839
if (e)2449
if (e)1840
{ e.preventDefault(); }2450
{ e.preventDefault(); }1841
app.pointerId = null;2451
app.pointerId = null;2452
removeWindowPointer();2453
if (boundCanvas)2454
{2455
try2456
{ boundCanvas.releasePointerCapture(e && e.pointerId); }2457
catch (err) { /* ignore */ }2458
}2459
startReleaseTween();1842
if (app.core)2460
if (app.core)1843
{ app.core.endDrag(); }2461
{ app.core.endDrag(); }1844
}2462
}@@ -41,7 +41,8 @@ var app = {
41
41
flames: Object.create(null),
42
42
cells: [],
43
43
pointerId: null,
44
gameOverShown: false
44
gameOverShown: false,
45
selectTween: null
45
46
};
46
47
47
48
window.onload = function ()
@@ -87,6 +88,7 @@ function startPlay ()
87
88
app.flames = Object.create(null);
88
89
app.cells = [];
89
90
app.pointerId = null;
91
app.selectTween = null;
90
92
91
93
layout();
92
94
app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH);
@@ -114,6 +116,7 @@ function returnToMenu ()
114
116
app.sprites = Object.create(null);
115
117
app.flames = Object.create(null);
116
118
app.cells = [];
119
app.selectTween = null;
117
120
118
121
hideEl('hud');
119
122
hideEl('game-over');
@@ -212,6 +215,7 @@ function syncSprites ()
212
215
}
213
216
obj.renderer.sprite = spriteFor(block);
214
217
placeTile(obj, block.col, block.y);
218
applyBlockVisual(obj, block);
215
219
obj.renderer.visible = true;
216
220
obj.renderer.alpha = 1;
217
221
@@ -251,6 +255,96 @@ function syncSprites ()
251
255
}
252
256
}
253
257
258
function applyBlockVisual (obj, block)
259
{
260
var t = app.tile;
261
var sx = 1;
262
var sy = 1;
263
var dy = 0;
264
if (block.bouncing && app.planet && app.planet.bounceDuration > 0
265
&& typeof Easing !== 'undefined' && Easing.bouncePose)
266
{
267
var u = block.bounceAge / app.planet.bounceDuration;
268
var pose = Easing.bouncePose(u, app.planet.bounceSquash);
269
sx = pose.sx;
270
sy = pose.sy;
271
dy = pose.dy;
272
}
273
var sel = 1;
274
var tw = app.selectTween;
275
if (tw && tw.blockId === block.id && tw.phase !== 'idle')
276
{ sel = tw.scale; }
277
obj.transform.size = { x: t * sx * sel, y: t * sy * sel };
278
if (dy)
279
{ obj.transform.position.y -= dy * t; }
280
}
281
282
function clickTween ()
283
{
284
return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null;
285
}
286
287
function startPressTween (blockId)
288
{
289
var c = clickTween();
290
if (!c)
291
{ return; }
292
var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1;
293
app.selectTween = {
294
blockId: blockId,
295
from: from,
296
to: c.pressScale,
297
t: 0,
298
duration: c.pressDuration,
299
ease: c.pressEase,
300
scale: from,
301
phase: 'press'
302
};
303
}
304
305
function startReleaseTween ()
306
{
307
var c = clickTween();
308
var tw = app.selectTween;
309
if (!c || !tw || tw.phase === 'idle')
310
{ return; }
311
app.selectTween = {
312
blockId: tw.blockId,
313
from: tw.scale,
314
to: c.releaseScale,
315
t: 0,
316
duration: c.releaseDuration,
317
ease: c.releaseEase,
318
scale: tw.scale,
319
phase: 'release'
320
};
321
}
322
323
function updateSelectTween (dt)
324
{
325
var tw = app.selectTween;
326
if (!tw || tw.phase === 'idle' || tw.phase === 'hold')
327
{ return; }
328
tw.t += dt;
329
var u = tw.duration > 0 ? tw.t / tw.duration : 1;
330
if (u >= 1)
331
{
332
tw.scale = tw.to;
333
tw.t = tw.duration;
334
if (tw.phase === 'press')
335
{ tw.phase = 'hold'; }
336
else
337
{
338
tw.phase = 'idle';
339
tw.scale = 1;
340
tw.blockId = null;
341
}
342
return;
343
}
344
var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack);
345
tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u;
346
}
347
254
348
function refreshHud ()
255
349
{
256
350
if (!app.core)
@@ -291,6 +385,7 @@ function tickView (dt)
291
385
}
292
386
293
387
app.core.tick(dt);
388
updateSelectTween(dt);
294
389
syncSprites();
295
390
refreshHud();
296
391
@@ -306,31 +401,51 @@ function tickView (dt)
306
401
}
307
402
308
403
/* ---------- pointer input (mouse + touch) --------------------------- */
404
/*
405
* Both mouse and touch use Pointer Events. After pointerdown we listen
406
* on window, so a drag that leaves the canvas (needed to launch a
407
* block at the top of a full column) still reaches moveDrag. We do
408
* not treat lostpointercapture as the end of a drag: on some touch
409
* browsers that fires mid-gesture and would cancel a launch.
410
*/
309
411
310
412
var boundCanvas = null;
413
var windowListening = false;
414
415
function addWindowPointer ()
416
{
417
if (windowListening)
418
{ return; }
419
window.addEventListener('pointermove', onPointerMove, true);
420
window.addEventListener('pointerup', onPointerUp, true);
421
window.addEventListener('pointercancel', onPointerUp, true);
422
windowListening = true;
423
}
424
425
function removeWindowPointer ()
426
{
427
if (!windowListening)
428
{ return; }
429
window.removeEventListener('pointermove', onPointerMove, true);
430
window.removeEventListener('pointerup', onPointerUp, true);
431
window.removeEventListener('pointercancel', onPointerUp, true);
432
windowListening = false;
433
}
311
434
312
435
function bindPointer (canvas)
313
436
{
314
437
unbindPointer();
315
438
boundCanvas = canvas;
316
439
canvas.addEventListener('pointerdown', onPointerDown);
317
canvas.addEventListener('pointermove', onPointerMove);
318
canvas.addEventListener('pointerup', onPointerUp);
319
canvas.addEventListener('pointercancel', onPointerUp);
320
canvas.addEventListener('lostpointercapture', onPointerUp);
321
440
canvas.style.touchAction = 'none';
322
441
canvas.style.cursor = 'pointer';
323
442
}
324
443
325
444
function unbindPointer ()
326
445
{
327
if (!boundCanvas)
328
{ return; }
329
boundCanvas.removeEventListener('pointerdown', onPointerDown);
330
boundCanvas.removeEventListener('pointermove', onPointerMove);
331
boundCanvas.removeEventListener('pointerup', onPointerUp);
332
boundCanvas.removeEventListener('pointercancel', onPointerUp);
333
boundCanvas.removeEventListener('lostpointercapture', onPointerUp);
446
removeWindowPointer();
447
if (boundCanvas)
448
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }
334
449
boundCanvas = null;
335
450
app.pointerId = null;
336
451
}
@@ -361,9 +476,10 @@ function onPointerDown (e)
361
476
try
362
477
{ boundCanvas.setPointerCapture(e.pointerId); }
363
478
catch (err) { /* ignore */ }
479
addWindowPointer();
364
480
var pos = fieldFromEvent(e);
365
if (pos)
366
{ app.core.beginDrag(pos.col, pos.y); }
481
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)
482
{ startPressTween(app.core.drag.blockId); }
367
483
}
368
484
369
485
function onPointerMove (e)
@@ -387,6 +503,14 @@ function onPointerUp (e)
387
503
if (e)
388
504
{ e.preventDefault(); }
389
505
app.pointerId = null;
506
removeWindowPointer();
507
if (boundCanvas)
508
{
509
try
510
{ boundCanvas.releasePointerCapture(e && e.pointerId); }
511
catch (err) { /* ignore */ }
512
}
513
startReleaseTween();
390
514
if (app.core)
391
515
{ app.core.endDrag(); }
392
516
}
@@ -41,7 +41,8 @@ var app = {41
flames: Object.create(null),41
flames: Object.create(null),42
cells: [],42
cells: [],43
pointerId: null,43
pointerId: null,44
gameOverShown: false44
gameOverShown: false,45
selectTween: null45
};46
};46
47
47
window.onload = function ()48
window.onload = function ()@@ -87,6 +88,7 @@ function startPlay ()87
app.flames = Object.create(null);88
app.flames = Object.create(null);88
app.cells = [];89
app.cells = [];89
app.pointerId = null;90
app.pointerId = null;91
app.selectTween = null;90
92
91
layout();93
layout();92
app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH);94
app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH);@@ -114,6 +116,7 @@ function returnToMenu ()114
app.sprites = Object.create(null);116
app.sprites = Object.create(null);115
app.flames = Object.create(null);117
app.flames = Object.create(null);116
app.cells = [];118
app.cells = [];119
app.selectTween = null;117
120
118
hideEl('hud');121
hideEl('hud');119
hideEl('game-over');122
hideEl('game-over');@@ -212,6 +215,7 @@ function syncSprites ()212
}215
}213
obj.renderer.sprite = spriteFor(block);216
obj.renderer.sprite = spriteFor(block);214
placeTile(obj, block.col, block.y);217
placeTile(obj, block.col, block.y);218
applyBlockVisual(obj, block);215
obj.renderer.visible = true;219
obj.renderer.visible = true;216
obj.renderer.alpha = 1;220
obj.renderer.alpha = 1;217
221
@@ -251,6 +255,96 @@ function syncSprites ()251
}255
}252
}256
}253
257
258
function applyBlockVisual (obj, block)259
{260
var t = app.tile;261
var sx = 1;262
var sy = 1;263
var dy = 0;264
if (block.bouncing && app.planet && app.planet.bounceDuration > 0265
&& typeof Easing !== 'undefined' && Easing.bouncePose)266
{267
var u = block.bounceAge / app.planet.bounceDuration;268
var pose = Easing.bouncePose(u, app.planet.bounceSquash);269
sx = pose.sx;270
sy = pose.sy;271
dy = pose.dy;272
}273
var sel = 1;274
var tw = app.selectTween;275
if (tw && tw.blockId === block.id && tw.phase !== 'idle')276
{ sel = tw.scale; }277
obj.transform.size = { x: t * sx * sel, y: t * sy * sel };278
if (dy)279
{ obj.transform.position.y -= dy * t; }280
}281
282
function clickTween ()283
{284
return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null;285
}286
287
function startPressTween (blockId)288
{289
var c = clickTween();290
if (!c)291
{ return; }292
var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1;293
app.selectTween = {294
blockId: blockId,295
from: from,296
to: c.pressScale,297
t: 0,298
duration: c.pressDuration,299
ease: c.pressEase,300
scale: from,301
phase: 'press'302
};303
}304
305
function startReleaseTween ()306
{307
var c = clickTween();308
var tw = app.selectTween;309
if (!c || !tw || tw.phase === 'idle')310
{ return; }311
app.selectTween = {312
blockId: tw.blockId,313
from: tw.scale,314
to: c.releaseScale,315
t: 0,316
duration: c.releaseDuration,317
ease: c.releaseEase,318
scale: tw.scale,319
phase: 'release'320
};321
}322
323
function updateSelectTween (dt)324
{325
var tw = app.selectTween;326
if (!tw || tw.phase === 'idle' || tw.phase === 'hold')327
{ return; }328
tw.t += dt;329
var u = tw.duration > 0 ? tw.t / tw.duration : 1;330
if (u >= 1)331
{332
tw.scale = tw.to;333
tw.t = tw.duration;334
if (tw.phase === 'press')335
{ tw.phase = 'hold'; }336
else337
{338
tw.phase = 'idle';339
tw.scale = 1;340
tw.blockId = null;341
}342
return;343
}344
var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack);345
tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u;346
}347
254
function refreshHud ()348
function refreshHud ()255
{349
{256
if (!app.core)350
if (!app.core)@@ -291,6 +385,7 @@ function tickView (dt)291
}385
}292
386
293
app.core.tick(dt);387
app.core.tick(dt);388
updateSelectTween(dt);294
syncSprites();389
syncSprites();295
refreshHud();390
refreshHud();296
391
@@ -306,31 +401,51 @@ function tickView (dt)306
}401
}307
402
308
/* ---------- pointer input (mouse + touch) --------------------------- */403
/* ---------- pointer input (mouse + touch) --------------------------- */404
/*405
* Both mouse and touch use Pointer Events. After pointerdown we listen406
* on window, so a drag that leaves the canvas (needed to launch a407
* block at the top of a full column) still reaches moveDrag. We do408
* not treat lostpointercapture as the end of a drag: on some touch409
* browsers that fires mid-gesture and would cancel a launch.410
*/309
411
310
var boundCanvas = null;412
var boundCanvas = null;413
var windowListening = false;414
415
function addWindowPointer ()416
{417
if (windowListening)418
{ return; }419
window.addEventListener('pointermove', onPointerMove, true);420
window.addEventListener('pointerup', onPointerUp, true);421
window.addEventListener('pointercancel', onPointerUp, true);422
windowListening = true;423
}424
425
function removeWindowPointer ()426
{427
if (!windowListening)428
{ return; }429
window.removeEventListener('pointermove', onPointerMove, true);430
window.removeEventListener('pointerup', onPointerUp, true);431
window.removeEventListener('pointercancel', onPointerUp, true);432
windowListening = false;433
}311
434
312
function bindPointer (canvas)435
function bindPointer (canvas)313
{436
{314
unbindPointer();437
unbindPointer();315
boundCanvas = canvas;438
boundCanvas = canvas;316
canvas.addEventListener('pointerdown', onPointerDown);439
canvas.addEventListener('pointerdown', onPointerDown);317
canvas.addEventListener('pointermove', onPointerMove);318
canvas.addEventListener('pointerup', onPointerUp);319
canvas.addEventListener('pointercancel', onPointerUp);320
canvas.addEventListener('lostpointercapture', onPointerUp);321
canvas.style.touchAction = 'none';440
canvas.style.touchAction = 'none';322
canvas.style.cursor = 'pointer';441
canvas.style.cursor = 'pointer';323
}442
}324
443
325
function unbindPointer ()444
function unbindPointer ()326
{445
{327
if (!boundCanvas)446
removeWindowPointer();328
{ return; }447
if (boundCanvas)329
boundCanvas.removeEventListener('pointerdown', onPointerDown);448
{ boundCanvas.removeEventListener('pointerdown', onPointerDown); }330
boundCanvas.removeEventListener('pointermove', onPointerMove);331
boundCanvas.removeEventListener('pointerup', onPointerUp);332
boundCanvas.removeEventListener('pointercancel', onPointerUp);333
boundCanvas.removeEventListener('lostpointercapture', onPointerUp);334
boundCanvas = null;449
boundCanvas = null;335
app.pointerId = null;450
app.pointerId = null;336
}451
}@@ -361,9 +476,10 @@ function onPointerDown (e)361
try476
try362
{ boundCanvas.setPointerCapture(e.pointerId); }477
{ boundCanvas.setPointerCapture(e.pointerId); }363
catch (err) { /* ignore */ }478
catch (err) { /* ignore */ }479
addWindowPointer();364
var pos = fieldFromEvent(e);480
var pos = fieldFromEvent(e);365
if (pos)481
if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag)366
{ app.core.beginDrag(pos.col, pos.y); }482
{ startPressTween(app.core.drag.blockId); }367
}483
}368
484
369
function onPointerMove (e)485
function onPointerMove (e)@@ -387,6 +503,14 @@ function onPointerUp (e)387
if (e)503
if (e)388
{ e.preventDefault(); }504
{ e.preventDefault(); }389
app.pointerId = null;505
app.pointerId = null;506
removeWindowPointer();507
if (boundCanvas)508
{509
try510
{ boundCanvas.releasePointerCapture(e && e.pointerId); }511
catch (err) { /* ignore */ }512
}513
startReleaseTween();390
if (app.core)514
if (app.core)391
{ app.core.endDrag(); }515
{ app.core.endDrag(); }392
}516
}