| 1 | /* |
| 2 | * AUTO-GENERATED FILE — DO NOT EDIT BY HAND. |
| 3 | * Built by src/build.sh from the sources in src/. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Start File: |
| 8 | * /workspace/meteors/src/rng.js |
| 9 | */ |
| 10 | 'use strict'; |
| 11 | |
| 12 | /** |
| 13 | * Seedable mulberry32 RNG, so the game core stays deterministic in tests |
| 14 | * and (later) for multiplayer replays. |
| 15 | */ |
| 16 | function createRng (seed) |
| 17 | { |
| 18 | var a = (seed >>> 0) || 1; |
| 19 | return function rng () |
| 20 | { |
| 21 | a |= 0; |
| 22 | a = a + 0x6D2B79F5 | 0; |
| 23 | var t = Math.imul(a ^ a >>> 15, 1 | a); |
| 24 | t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; |
| 25 | return ((t ^ t >>> 14) >>> 0) / 4294967296; |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | function pickIndex (rng, count) |
| 30 | { |
| 31 | var n = count | 0; |
| 32 | if (n <= 0) |
| 33 | { return 0; } |
| 34 | return Math.floor(rng() * n) % n; |
| 35 | } |
| 36 | |
| 37 | function pickFrom (rng, list) |
| 38 | { |
| 39 | if (!list || !list.length) |
| 40 | { return undefined; } |
| 41 | return list[pickIndex(rng, list.length)]; |
| 42 | } |
| 43 | |
| 44 | var Rng = { |
| 45 | create: createRng, |
| 46 | pickIndex: pickIndex, |
| 47 | pickFrom: pickFrom |
| 48 | }; |
| 49 | |
| 50 | if (typeof module !== 'undefined' && module.exports) |
| 51 | { module.exports = Rng; } |
| 52 | else if (typeof globalThis !== 'undefined') |
| 53 | { globalThis.Rng = Rng; } |
| 54 | |
| 55 | /* |
| 56 | * End File: |
| 57 | * /workspace/meteors/src/rng.js |
| 58 | */ |
| 59 | |
| 60 | /* |
| 61 | * Start File: |
| 62 | * /workspace/meteors/src/planet.js |
| 63 | */ |
| 64 | 'use strict'; |
| 65 | |
| 66 | /** |
| 67 | * Planet physics and visuals for Meteors. |
| 68 | * |
| 69 | * Stage 1 uses a single default planet. Stage 3 lets the player pick a |
| 70 | * planet; the numbers and jump-height rules are already applied here so |
| 71 | * that later planets only need new data, not new physics code. |
| 72 | * |
| 73 | * Gravity (jumpsToClear): 1 (easy, high jump) .. 4 (hard, short jump). |
| 74 | * A horizontal match of 3 jumps FIELD_HEIGHT / jumpsToClear tiles — |
| 75 | * at 1, one jump from the floor clears the field; at 4, you need four. |
| 76 | * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall). |
| 77 | * After a jump peaks, blocks float down at FIELD_HEIGHT / fallTime tiles/s. |
| 78 | * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump). |
| 79 | * Time to reach the apex, independent of jump height. |
| 80 | * |
| 81 | * Jump-height multipliers (never explained in the game): |
| 82 | * A vertical match of 3 is 120 % of a horizontal row of 3. |
| 83 | * Each extra block beyond 3 adds 5 %. |
| 84 | * |
| 85 | * Power is the average of the three difficulty axes (0 easy .. 1 hard) |
| 86 | * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at |
| 87 | * power 0 to 5 % at power 1. |
| 88 | */ |
| 89 | |
| 90 | var FIELD_WIDTH = 10; |
| 91 | var FIELD_HEIGHT = 13; |
| 92 | |
| 93 | function clamp (value, lo, hi) |
| 94 | { |
| 95 | return Math.max(lo, Math.min(hi, value)); |
| 96 | } |
| 97 | |
| 98 | function defaultColors () |
| 99 | { |
| 100 | return [ |
| 101 | { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' }, |
| 102 | { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' }, |
| 103 | { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' }, |
| 104 | { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' }, |
| 105 | { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' } |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | function createPlanet (opts) |
| 110 | { |
| 111 | opts = opts || {}; |
| 112 | var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4); |
| 113 | var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5); |
| 114 | var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3); |
| 115 | var colors = opts.colors || defaultColors(); |
| 116 | var planet = { |
| 117 | name: opts.name || 'Terra', |
| 118 | people: opts.people || 'Terrans', |
| 119 | jumpsToClear: jumpsToClear, |
| 120 | fallTime: fallTime, |
| 121 | riseTime: riseTime, |
| 122 | colors: colors, |
| 123 | baseJumpHeight: FIELD_HEIGHT / jumpsToClear, |
| 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 |
| 132 | }; |
| 133 | return planet; |
| 134 | } |
| 135 | |
| 136 | function defaultPlanet () |
| 137 | { |
| 138 | return createPlanet({ |
| 139 | name: 'Terra', |
| 140 | people: 'Terrans', |
| 141 | jumpsToClear: 2, |
| 142 | fallTime: 3, |
| 143 | riseTime: 1.5 |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Jump height in tiles for a match line. |
| 149 | * @param {object} planet |
| 150 | * @param {number} matchLength number of blocks in the line (>= 3) |
| 151 | * @param {boolean} vertical true for a vertical line |
| 152 | */ |
| 153 | function jumpHeight (planet, matchLength, vertical) |
| 154 | { |
| 155 | var extra = Math.max(0, (matchLength | 0) - 3); |
| 156 | var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra; |
| 157 | return planet.baseJumpHeight * multiplier; |
| 158 | } |
| 159 | |
| 160 | /** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */ |
| 161 | function launchVelocity (planet, height) |
| 162 | { |
| 163 | if (height <= 0) |
| 164 | { return 0; } |
| 165 | return (2 * height) / planet.riseTime; |
| 166 | } |
| 167 | |
| 168 | /** Constant deceleration (tiles/s^2) paired with launchVelocity. */ |
| 169 | function launchDeceleration (planet, height) |
| 170 | { |
| 171 | if (height <= 0) |
| 172 | { return 0; } |
| 173 | return (2 * height) / (planet.riseTime * planet.riseTime); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower |
| 178 | * chance that a shot-off block misses the opponent). |
| 179 | */ |
| 180 | function power (planet) |
| 181 | { |
| 182 | var gravity = (planet.jumpsToClear - 1) / 3; |
| 183 | var air = (5 - planet.fallTime) / 4; |
| 184 | var mass = (3 - planet.riseTime) / 2.5; |
| 185 | return (gravity + air + mass) / 3; |
| 186 | } |
| 187 | |
| 188 | /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */ |
| 189 | function missChance (planet) |
| 190 | { |
| 191 | return 0.60 - 0.55 * power(planet); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³), |
| 196 | * launch force (N), and power as a percentage. The numbers do not have to |
| 197 | * explain themselves. |
| 198 | */ |
| 199 | function displayValues (planet) |
| 200 | { |
| 201 | var gravity = 9.81 * planet.jumpsToClear / 2; |
| 202 | var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60; |
| 203 | var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160; |
| 204 | return { |
| 205 | gravity: gravity, |
| 206 | density: density, |
| 207 | newtons: newtons, |
| 208 | power: Math.round(power(planet) * 100), |
| 209 | missChance: missChance(planet) |
| 210 | }; |
| 211 | } |
| 212 | |
| 213 | var Planet = { |
| 214 | FIELD_WIDTH: FIELD_WIDTH, |
| 215 | FIELD_HEIGHT: FIELD_HEIGHT, |
| 216 | COLOR_COUNT: 5, |
| 217 | SETTLE_TIME: 3, |
| 218 | SPAWN_INTERVAL: 1, |
| 219 | JUMP_VERTICAL_BONUS: 1.2, |
| 220 | JUMP_EXTRA_BLOCK: 0.05, |
| 221 | createPlanet: createPlanet, |
| 222 | defaultPlanet: defaultPlanet, |
| 223 | defaultColors: defaultColors, |
| 224 | jumpHeight: jumpHeight, |
| 225 | launchVelocity: launchVelocity, |
| 226 | launchDeceleration: launchDeceleration, |
| 227 | power: power, |
| 228 | missChance: missChance, |
| 229 | displayValues: displayValues |
| 230 | }; |
| 231 | |
| 232 | if (typeof module !== 'undefined' && module.exports) |
| 233 | { module.exports = Planet; } |
| 234 | else if (typeof globalThis !== 'undefined') |
| 235 | { globalThis.Planet = Planet; } |
| 236 | |
| 237 | /* |
| 238 | * End File: |
| 239 | * /workspace/meteors/src/planet.js |
| 240 | */ |
| 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 | |
| 363 | /* |
| 364 | * Start File: |
| 365 | * /workspace/meteors/src/gameCore.js |
| 366 | */ |
| 367 | 'use strict'; |
| 368 | |
| 369 | /** |
| 370 | * Deterministic Meteors game core. No DOM, no rendering, no input devices. |
| 371 | * The view layer reads this state and translates pointer events into |
| 372 | * beginDrag / moveDrag / endDrag. |
| 373 | */ |
| 374 | |
| 375 | var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined') |
| 376 | ? require('./planet.js') |
| 377 | : (typeof Planet !== 'undefined' ? Planet : globalThis.Planet); |
| 378 | |
| 379 | var COLOR_GRAY = -1; |
| 380 | |
| 381 | var STATE_STILL = 'still'; |
| 382 | var STATE_FALLING = 'falling'; |
| 383 | var STATE_FLYING = 'flying'; |
| 384 | var STATE_SETTLING = 'settling'; |
| 385 | var STATE_LOCKED = 'locked'; |
| 386 | var STATE_MANUAL = 'manual'; |
| 387 | |
| 388 | var ALIGN_EPS = 0.15; |
| 389 | var SUBSTEP = 1 / 120; |
| 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; |
| 393 | |
| 394 | function UnionFind () |
| 395 | { |
| 396 | this.parent = Object.create(null); |
| 397 | } |
| 398 | |
| 399 | UnionFind.prototype.add = function (id) |
| 400 | { |
| 401 | if (this.parent[id] == null) |
| 402 | { this.parent[id] = id; } |
| 403 | }; |
| 404 | |
| 405 | UnionFind.prototype.find = function (id) |
| 406 | { |
| 407 | if (this.parent[id] !== id) |
| 408 | { this.parent[id] = this.find(this.parent[id]); } |
| 409 | return this.parent[id]; |
| 410 | }; |
| 411 | |
| 412 | UnionFind.prototype.union = function (a, b) |
| 413 | { |
| 414 | this.add(a); |
| 415 | this.add(b); |
| 416 | var ra = this.find(a); |
| 417 | var rb = this.find(b); |
| 418 | if (ra !== rb) |
| 419 | { this.parent[rb] = ra; } |
| 420 | }; |
| 421 | |
| 422 | function GameCore (planet, rng) |
| 423 | { |
| 424 | this.planet = planet || PlanetMod.defaultPlanet(); |
| 425 | this.rng = rng || Math.random; |
| 426 | this.width = PlanetMod.FIELD_WIDTH; |
| 427 | this.height = PlanetMod.FIELD_HEIGHT; |
| 428 | this.blocks = []; |
| 429 | this.clusters = Object.create(null); |
| 430 | this.nextBlockId = 1; |
| 431 | this.nextClusterId = 1; |
| 432 | this.spawnTimer = 0; |
| 433 | this.spawnInterval = PlanetMod.SPAWN_INTERVAL; |
| 434 | this.settleTime = PlanetMod.SETTLE_TIME; |
| 435 | this.score = 0; |
| 436 | this.state = 'playing'; |
| 437 | this.drag = null; |
| 438 | this.gameOverTime = 0; |
| 439 | } |
| 440 | |
| 441 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 442 | GameCore.LAUNCH_PULL = LAUNCH_PULL; |
| 443 | GameCore.STATE_STILL = STATE_STILL; |
| 444 | GameCore.STATE_FALLING = STATE_FALLING; |
| 445 | GameCore.STATE_FLYING = STATE_FLYING; |
| 446 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 447 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 448 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 449 | |
| 450 | GameCore.prototype.randomColor = function () |
| 451 | { |
| 452 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 453 | }; |
| 454 | |
| 455 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 456 | { |
| 457 | var block = { |
| 458 | id: this.nextBlockId++, |
| 459 | col: col, |
| 460 | y: y, |
| 461 | color: color, |
| 462 | state: state || STATE_STILL, |
| 463 | vy: 0, |
| 464 | clusterId: null, |
| 465 | settleTime: 0, |
| 466 | flame: false, |
| 467 | pushedClusters: null, |
| 468 | manualTarget: 0, |
| 469 | manualDecel: 0, |
| 470 | manualPhase: null, |
| 471 | bouncing: false, |
| 472 | bounceAge: 0 |
| 473 | }; |
| 474 | this.blocks.push(block); |
| 475 | return block; |
| 476 | }; |
| 477 | |
| 478 | GameCore.prototype.removeBlock = function (block) |
| 479 | { |
| 480 | if (block.clusterId != null) |
| 481 | { this.leaveCluster(block); } |
| 482 | var index = this.blocks.indexOf(block); |
| 483 | if (index >= 0) |
| 484 | { this.blocks.splice(index, 1); } |
| 485 | }; |
| 486 | |
| 487 | GameCore.prototype.leaveCluster = function (block) |
| 488 | { |
| 489 | var id = block.clusterId; |
| 490 | block.clusterId = null; |
| 491 | block.flame = false; |
| 492 | if (id == null || !this.clusters[id]) |
| 493 | { return; } |
| 494 | var still = 0; |
| 495 | for (var i = 0; i < this.blocks.length; i++) |
| 496 | { |
| 497 | if (this.blocks[i].clusterId === id) |
| 498 | { still++; } |
| 499 | } |
| 500 | if (still === 0) |
| 501 | { delete this.clusters[id]; } |
| 502 | }; |
| 503 | |
| 504 | GameCore.prototype.blocksInColumn = function (col) |
| 505 | { |
| 506 | var out = []; |
| 507 | for (var i = 0; i < this.blocks.length; i++) |
| 508 | { |
| 509 | if (this.blocks[i].col === col) |
| 510 | { out.push(this.blocks[i]); } |
| 511 | } |
| 512 | out.sort(function (a, b) { return a.y - b.y; }); |
| 513 | return out; |
| 514 | }; |
| 515 | |
| 516 | GameCore.prototype.isGrounded = function (block) |
| 517 | { |
| 518 | return block.state === STATE_STILL |
| 519 | || block.state === STATE_SETTLING |
| 520 | || block.state === STATE_LOCKED; |
| 521 | }; |
| 522 | |
| 523 | GameCore.prototype.stackTop = function (col, ignore) |
| 524 | { |
| 525 | var top = 0; |
| 526 | for (var i = 0; i < this.blocks.length; i++) |
| 527 | { |
| 528 | var block = this.blocks[i]; |
| 529 | if (block.col !== col) |
| 530 | { continue; } |
| 531 | if (ignore && ignore(block)) |
| 532 | { continue; } |
| 533 | if (this.isGrounded(block)) |
| 534 | { |
| 535 | var surface = block.y + 1; |
| 536 | if (surface > top) |
| 537 | { top = surface; } |
| 538 | } |
| 539 | } |
| 540 | return top; |
| 541 | }; |
| 542 | |
| 543 | GameCore.prototype.isColumnLocked = function (col) |
| 544 | { |
| 545 | for (var i = 0; i < this.blocks.length; i++) |
| 546 | { |
| 547 | var block = this.blocks[i]; |
| 548 | if (block.col === col && block.state === STATE_LOCKED) |
| 549 | { return true; } |
| 550 | } |
| 551 | return false; |
| 552 | }; |
| 553 | |
| 554 | GameCore.prototype.isColumnStill = function (col) |
| 555 | { |
| 556 | if (this.isColumnLocked(col)) |
| 557 | { return false; } |
| 558 | for (var i = 0; i < this.blocks.length; i++) |
| 559 | { |
| 560 | var block = this.blocks[i]; |
| 561 | if (block.col !== col) |
| 562 | { continue; } |
| 563 | if (block.state === STATE_FLYING |
| 564 | || block.state === STATE_FALLING |
| 565 | || block.state === STATE_MANUAL |
| 566 | || block.state === STATE_SETTLING) |
| 567 | { return false; } |
| 568 | } |
| 569 | return true; |
| 570 | }; |
| 571 | |
| 572 | GameCore.prototype.groundedCount = function (col) |
| 573 | { |
| 574 | var n = 0; |
| 575 | for (var i = 0; i < this.blocks.length; i++) |
| 576 | { |
| 577 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 578 | { n++; } |
| 579 | } |
| 580 | return n; |
| 581 | }; |
| 582 | |
| 583 | GameCore.prototype.blockAt = function (col, row) |
| 584 | { |
| 585 | for (var i = 0; i < this.blocks.length; i++) |
| 586 | { |
| 587 | var block = this.blocks[i]; |
| 588 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 589 | { return block; } |
| 590 | } |
| 591 | return null; |
| 592 | }; |
| 593 | |
| 594 | GameCore.prototype.place = function (col, row, color, state) |
| 595 | { |
| 596 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 597 | }; |
| 598 | |
| 599 | GameCore.prototype.lockedColumnCount = function () |
| 600 | { |
| 601 | var n = 0; |
| 602 | for (var c = 0; c < this.width; c++) |
| 603 | { |
| 604 | if (this.isColumnLocked(c)) |
| 605 | { n++; } |
| 606 | } |
| 607 | return n; |
| 608 | }; |
| 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 | |
| 803 | GameCore.prototype.tick = function (dt) |
| 804 | { |
| 805 | if (this.state === 'gameover') |
| 806 | { |
| 807 | this.gameOverTime += Math.max(0, dt); |
| 808 | return; |
| 809 | } |
| 810 | if (dt <= 0) |
| 811 | { return; } |
| 812 | dt = Math.min(dt, 0.1); |
| 813 | |
| 814 | this.spawnTimer += dt; |
| 815 | while (this.spawnTimer >= this.spawnInterval) |
| 816 | { |
| 817 | this.spawnTimer -= this.spawnInterval; |
| 818 | this.trySpawn(); |
| 819 | } |
| 820 | |
| 821 | var left = dt; |
| 822 | while (left > 1e-12) |
| 823 | { |
| 824 | var step = Math.min(SUBSTEP, left); |
| 825 | this.integrateFalling(step); |
| 826 | this.integrateClusters(step); |
| 827 | this.integrateManual(step); |
| 828 | this.cullOffTop(); |
| 829 | left -= step; |
| 830 | } |
| 831 | |
| 832 | this.releaseUnsupported(); |
| 833 | this.advanceSettle(dt); |
| 834 | this.advanceBounces(dt); |
| 835 | if (!this.drag) |
| 836 | { this.resolveMatches(); } |
| 837 | this.checkLocks(); |
| 838 | this.checkGameOver(); |
| 839 | }; |
| 840 | |
| 841 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 842 | GameCore.prototype.step = function (seconds, dt) |
| 843 | { |
| 844 | dt = dt || SUBSTEP; |
| 845 | var t = 0; |
| 846 | while (t < seconds - 1e-15) |
| 847 | { |
| 848 | var s = Math.min(dt, seconds - t); |
| 849 | this.tick(s); |
| 850 | t += s; |
| 851 | } |
| 852 | }; |
| 853 | |
| 854 | GameCore.prototype.trySpawn = function () |
| 855 | { |
| 856 | if (this.state !== 'playing') |
| 857 | { return null; } |
| 858 | var eligible = []; |
| 859 | for (var col = 0; col < this.width; col++) |
| 860 | { |
| 861 | if (this.isColumnLocked(col)) |
| 862 | { continue; } |
| 863 | if (this.groundedCount(col) >= this.height) |
| 864 | { continue; } |
| 865 | var alreadyFalling = false; |
| 866 | var list = this.blocksInColumn(col); |
| 867 | for (var i = 0; i < list.length; i++) |
| 868 | { |
| 869 | if (list[i].state === STATE_FALLING) |
| 870 | { |
| 871 | alreadyFalling = true; |
| 872 | break; |
| 873 | } |
| 874 | } |
| 875 | if (!alreadyFalling) |
| 876 | { eligible.push(col); } |
| 877 | } |
| 878 | if (!eligible.length) |
| 879 | { return null; } |
| 880 | var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 881 | var color = this.randomColor(); |
| 882 | var block = this.createBlock(col, this.height, color, STATE_FALLING); |
| 883 | block.vy = -this.planet.fallSpeed; |
| 884 | return block; |
| 885 | }; |
| 886 | |
| 887 | GameCore.prototype.integrateFalling = function (dt) |
| 888 | { |
| 889 | var falling = []; |
| 890 | for (var i = 0; i < this.blocks.length; i++) |
| 891 | { |
| 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]; |
| 900 | if (block.state !== STATE_FALLING) |
| 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 | } |
| 910 | var top = this.stackTop(block.col); |
| 911 | var below = this.highestFallingBelow(block); |
| 912 | if (below && below.y + 1 > top) |
| 913 | { top = below.y + 1; } |
| 914 | if (nextY <= top) |
| 915 | { |
| 916 | block.y = top; |
| 917 | this.landFallingBlock(block); |
| 918 | } |
| 919 | else |
| 920 | { block.y = nextY; } |
| 921 | } |
| 922 | }; |
| 923 | |
| 924 | GameCore.prototype.clusterMembers = function (clusterId) |
| 925 | { |
| 926 | var out = []; |
| 927 | for (var i = 0; i < this.blocks.length; i++) |
| 928 | { |
| 929 | if (this.blocks[i].clusterId === clusterId) |
| 930 | { out.push(this.blocks[i]); } |
| 931 | } |
| 932 | return out; |
| 933 | }; |
| 934 | |
| 935 | |
| 936 | /** |
| 937 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 938 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 939 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 940 | * upward deceleration. After the apex, vy eases from 0 to -fallSpeed |
| 941 | * at planet.fallAccel, then stays at terminal fallSpeed. |
| 942 | */ |
| 943 | GameCore.prototype.advanceJump = function (body, dt) |
| 944 | { |
| 945 | var dy = 0; |
| 946 | var left = dt; |
| 947 | if (body.phase === 'up') |
| 948 | { |
| 949 | var decel = body.decel; |
| 950 | var vy = body.vy; |
| 951 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 952 | if (tApex <= left + 1e-15) |
| 953 | { |
| 954 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 955 | body.vy = 0; |
| 956 | body.phase = 'down'; |
| 957 | left -= tApex; |
| 958 | } |
| 959 | else |
| 960 | { |
| 961 | var vyNew = vy - decel * left; |
| 962 | dy += (vy + vyNew) * 0.5 * left; |
| 963 | body.vy = vyNew; |
| 964 | left = 0; |
| 965 | } |
| 966 | } |
| 967 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 968 | { |
| 969 | body.vy = 0; |
| 970 | body.phase = 'down'; |
| 971 | } |
| 972 | if (body.phase === 'down' && left > 0) |
| 973 | { |
| 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 | } |
| 1003 | } |
| 1004 | return dy; |
| 1005 | }; |
| 1006 | |
| 1007 | GameCore.prototype.integrateClusters = function (dt) |
| 1008 | { |
| 1009 | var ids = Object.keys(this.clusters); |
| 1010 | for (var n = 0; n < ids.length; n++) |
| 1011 | { |
| 1012 | var cluster = this.clusters[ids[n]]; |
| 1013 | if (!cluster) |
| 1014 | { continue; } |
| 1015 | var members = this.clusterMembers(cluster.id); |
| 1016 | if (!members.length) |
| 1017 | { |
| 1018 | delete this.clusters[cluster.id]; |
| 1019 | continue; |
| 1020 | } |
| 1021 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1022 | { continue; } |
| 1023 | |
| 1024 | var dy = this.advanceJump(cluster, dt); |
| 1025 | for (var i = 0; i < members.length; i++) |
| 1026 | { |
| 1027 | members[i].y += dy; |
| 1028 | members[i].vy = cluster.vy; |
| 1029 | } |
| 1030 | |
| 1031 | this.catchFallingOntoCluster(cluster, members); |
| 1032 | |
| 1033 | if (cluster.phase === 'down') |
| 1034 | { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); } |
| 1035 | } |
| 1036 | }; |
| 1037 | |
| 1038 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 1039 | { |
| 1040 | if (!members.length) |
| 1041 | { return; } |
| 1042 | var shift = 0; |
| 1043 | var hit = false; |
| 1044 | for (var i = 0; i < members.length; i++) |
| 1045 | { |
| 1046 | var block = members[i]; |
| 1047 | var top = this.stackTop(block.col, function (other) |
| 1048 | { |
| 1049 | return other.clusterId === cluster.id; |
| 1050 | }); |
| 1051 | if (block.y <= top + 1e-9) |
| 1052 | { |
| 1053 | hit = true; |
| 1054 | var need = top - block.y; |
| 1055 | if (need > shift) |
| 1056 | { shift = need; } |
| 1057 | } |
| 1058 | } |
| 1059 | if (!hit) |
| 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; } |
| 1064 | this.landCluster(cluster, members); |
| 1065 | }; |
| 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 | */ |
| 1073 | GameCore.prototype.landCluster = function (cluster, members) |
| 1074 | { |
| 1075 | for (var i = 0; i < members.length; i++) |
| 1076 | { |
| 1077 | var block = members[i]; |
| 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); } |
| 1094 | } |
| 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++) |
| 1111 | { |
| 1112 | var block = members[i]; |
| 1113 | if (block.color === COLOR_GRAY) |
| 1114 | { |
| 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) |
| 1125 | { |
| 1126 | block.state = STATE_STILL; |
| 1127 | block.vy = 0; |
| 1128 | } |
| 1129 | else |
| 1130 | { |
| 1131 | block.state = STATE_FALLING; |
| 1132 | block.vy = 0; |
| 1133 | } |
| 1134 | } |
| 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; } |
| 1142 | for (var i = 0; i < this.blocks.length; i++) |
| 1143 | { |
| 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; } |
| 1153 | } |
| 1154 | return false; |
| 1155 | }; |
| 1156 | |
| 1157 | GameCore.prototype.integrateManual = function (dt) |
| 1158 | { |
| 1159 | for (var i = 0; i < this.blocks.length; i++) |
| 1160 | { |
| 1161 | var block = this.blocks[i]; |
| 1162 | if (block.state !== STATE_MANUAL) |
| 1163 | { continue; } |
| 1164 | |
| 1165 | if (block.manualPhase === 'up') |
| 1166 | { |
| 1167 | var oldY = block.y; |
| 1168 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 1169 | var dy = this.advanceJump(proxy, dt); |
| 1170 | var nextY = oldY + dy; |
| 1171 | this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget)); |
| 1172 | block.vy = proxy.vy; |
| 1173 | block.manualPhase = proxy.phase; |
| 1174 | block.y = Math.min(nextY, block.manualTarget); |
| 1175 | if (block.y >= block.manualTarget - 1e-12) |
| 1176 | { |
| 1177 | block.y = block.manualTarget; |
| 1178 | block.vy = 0; |
| 1179 | block.manualPhase = 'down'; |
| 1180 | } |
| 1181 | if (block.y > this.height - 1) |
| 1182 | { |
| 1183 | block.y = this.height - 1; |
| 1184 | block.vy = 0; |
| 1185 | block.manualPhase = 'down'; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | if (block.manualPhase === 'down') |
| 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; |
| 1195 | var top = this.stackTop(block.col); |
| 1196 | if (nextY <= top) |
| 1197 | { |
| 1198 | block.y = top; |
| 1199 | block.state = STATE_STILL; |
| 1200 | block.vy = 0; |
| 1201 | block.manualPhase = null; |
| 1202 | block.flame = false; |
| 1203 | this.startBounce(block); |
| 1204 | } |
| 1205 | else |
| 1206 | { |
| 1207 | block.y = Math.min(nextY, this.height - 1); |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | }; |
| 1212 | |
| 1213 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 1214 | { |
| 1215 | if (!manual.pushedClusters) |
| 1216 | { manual.pushedClusters = Object.create(null); } |
| 1217 | |
| 1218 | var manualTopOld = oldY + 1; |
| 1219 | var manualTopNew = nextY + 1; |
| 1220 | var low = Math.min(oldY, nextY); |
| 1221 | var high = Math.max(oldY, nextY) + 1; |
| 1222 | |
| 1223 | for (var i = 0; i < this.blocks.length; i++) |
| 1224 | { |
| 1225 | var other = this.blocks[i]; |
| 1226 | if (other === manual) |
| 1227 | { continue; } |
| 1228 | if (other.col !== manual.col) |
| 1229 | { continue; } |
| 1230 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 1231 | { continue; } |
| 1232 | if (manual.pushedClusters[other.clusterId]) |
| 1233 | { continue; } |
| 1234 | var otherBot = other.y; |
| 1235 | var otherTop = other.y + 1; |
| 1236 | var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9; |
| 1237 | var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9; |
| 1238 | if (overlapping || crossing) |
| 1239 | { |
| 1240 | if (this.pushClusterColumn(other.clusterId, manual.col)) |
| 1241 | { |
| 1242 | manual.pushedClusters[other.clusterId] = true; |
| 1243 | return; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | }; |
| 1248 | |
| 1249 | |
| 1250 | /** |
| 1251 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 1252 | * Other columns stay put (they remain linked). Then look for a new |
| 1253 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 1254 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 1255 | * with the same jump height from its current position. |
| 1256 | */ |
| 1257 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 1258 | { |
| 1259 | var cluster = this.clusters[clusterId]; |
| 1260 | if (!cluster) |
| 1261 | { return false; } |
| 1262 | var members = this.clusterMembers(clusterId); |
| 1263 | for (var i = 0; i < members.length; i++) |
| 1264 | { |
| 1265 | if (members[i].col === col) |
| 1266 | { members[i].y += 1; } |
| 1267 | } |
| 1268 | this.cullOffTop(); |
| 1269 | if (!this.clusters[clusterId]) |
| 1270 | { return true; } |
| 1271 | this.checkChainRelaunch(cluster); |
| 1272 | return true; |
| 1273 | }; |
| 1274 | |
| 1275 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 1276 | { |
| 1277 | if (!this.clusters[cluster.id]) |
| 1278 | { return; } |
| 1279 | var lines = this.findFlyingMatchLines(cluster.id); |
| 1280 | if (!lines.length) |
| 1281 | { return; } |
| 1282 | var newly = []; |
| 1283 | var seen = Object.create(null); |
| 1284 | for (var i = 0; i < lines.length; i++) |
| 1285 | { |
| 1286 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 1287 | { |
| 1288 | var block = lines[i].blocks[j]; |
| 1289 | if (seen[block.id]) |
| 1290 | { continue; } |
| 1291 | seen[block.id] = true; |
| 1292 | newly.push(block); |
| 1293 | } |
| 1294 | } |
| 1295 | for (var i = 0; i < newly.length; i++) |
| 1296 | { |
| 1297 | var block = newly[i]; |
| 1298 | block.color = COLOR_GRAY; |
| 1299 | block.flame = true; |
| 1300 | block.clusterId = cluster.id; |
| 1301 | block.state = STATE_FLYING; |
| 1302 | } |
| 1303 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 1304 | }; |
| 1305 | |
| 1306 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 1307 | { |
| 1308 | cluster.jumpHeight = height; |
| 1309 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1310 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 1311 | cluster.phase = 'up'; |
| 1312 | var members = this.clusterMembers(cluster.id); |
| 1313 | for (var i = 0; i < members.length; i++) |
| 1314 | { |
| 1315 | members[i].vy = cluster.vy; |
| 1316 | members[i].state = STATE_FLYING; |
| 1317 | if (members[i].color === COLOR_GRAY) |
| 1318 | { members[i].flame = true; } |
| 1319 | } |
| 1320 | }; |
| 1321 | |
| 1322 | GameCore.prototype.startManualLaunch = function (block) |
| 1323 | { |
| 1324 | var target = this.height - 1; |
| 1325 | var height = Math.max(0.05, target - block.y); |
| 1326 | block.state = STATE_MANUAL; |
| 1327 | block.pushedClusters = Object.create(null); |
| 1328 | block.manualTarget = target; |
| 1329 | block.manualPhase = 'up'; |
| 1330 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1331 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 1332 | block.flame = false; |
| 1333 | if (this.drag && this.drag.blockId === block.id) |
| 1334 | { this.drag = null; } |
| 1335 | }; |
| 1336 | |
| 1337 | GameCore.prototype.cullOffTop = function () |
| 1338 | { |
| 1339 | var gone = []; |
| 1340 | for (var i = 0; i < this.blocks.length; i++) |
| 1341 | { |
| 1342 | var block = this.blocks[i]; |
| 1343 | if (block.state === STATE_MANUAL) |
| 1344 | { |
| 1345 | if (block.y > this.height - 1) |
| 1346 | { block.y = this.height - 1; } |
| 1347 | continue; |
| 1348 | } |
| 1349 | if (block.y >= this.height - 1e-9) |
| 1350 | { gone.push(block); } |
| 1351 | } |
| 1352 | for (var i = 0; i < gone.length; i++) |
| 1353 | { |
| 1354 | this.score += 1; |
| 1355 | this.removeBlock(gone[i]); |
| 1356 | } |
| 1357 | }; |
| 1358 | |
| 1359 | GameCore.prototype.advanceSettle = function (dt) |
| 1360 | { |
| 1361 | for (var i = 0; i < this.blocks.length; i++) |
| 1362 | { |
| 1363 | var block = this.blocks[i]; |
| 1364 | if (block.state !== STATE_SETTLING) |
| 1365 | { continue; } |
| 1366 | var inLanded = block.clusterId != null |
| 1367 | && this.clusters[block.clusterId] |
| 1368 | && this.clusters[block.clusterId].phase === 'landed'; |
| 1369 | if (block.color === COLOR_GRAY) |
| 1370 | { |
| 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. |
| 1402 | this.leaveCluster(block); |
| 1403 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1404 | if (block.state === STATE_FALLING) |
| 1405 | { block.vy = 0; } |
| 1406 | } |
| 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 | } |
| 1425 | }; |
| 1426 | |
| 1427 | GameCore.prototype.findMatchLines = function () |
| 1428 | { |
| 1429 | var candidates = []; |
| 1430 | for (var i = 0; i < this.blocks.length; i++) |
| 1431 | { |
| 1432 | var block = this.blocks[i]; |
| 1433 | if (block.state === STATE_STILL && block.color >= 0) |
| 1434 | { candidates.push(block); } |
| 1435 | } |
| 1436 | return this.collectLines(candidates, true); |
| 1437 | }; |
| 1438 | |
| 1439 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 1440 | { |
| 1441 | var candidates = []; |
| 1442 | for (var i = 0; i < this.blocks.length; i++) |
| 1443 | { |
| 1444 | var block = this.blocks[i]; |
| 1445 | if (block.clusterId === clusterId && block.color >= 0 |
| 1446 | && (block.state === STATE_FLYING)) |
| 1447 | { candidates.push(block); } |
| 1448 | } |
| 1449 | return this.collectLines(candidates, false); |
| 1450 | }; |
| 1451 | |
| 1452 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 1453 | { |
| 1454 | var lines = []; |
| 1455 | if (!candidates.length) |
| 1456 | { return lines; } |
| 1457 | |
| 1458 | var byRow = Object.create(null); |
| 1459 | for (var i = 0; i < candidates.length; i++) |
| 1460 | { |
| 1461 | var block = candidates[i]; |
| 1462 | var row = Math.round(block.y); |
| 1463 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 1464 | { continue; } |
| 1465 | if (!byRow[row]) |
| 1466 | { byRow[row] = []; } |
| 1467 | byRow[row].push(block); |
| 1468 | } |
| 1469 | var rows = Object.keys(byRow); |
| 1470 | for (var r = 0; r < rows.length; r++) |
| 1471 | { |
| 1472 | var list = byRow[rows[r]]; |
| 1473 | list.sort(function (a, b) { return a.col - b.col; }); |
| 1474 | this.pushRuns(lines, list, false, function (a, b) |
| 1475 | { |
| 1476 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 1477 | }); |
| 1478 | } |
| 1479 | |
| 1480 | var byCol = Object.create(null); |
| 1481 | for (var i = 0; i < candidates.length; i++) |
| 1482 | { |
| 1483 | var block = candidates[i]; |
| 1484 | if (!byCol[block.col]) |
| 1485 | { byCol[block.col] = []; } |
| 1486 | byCol[block.col].push(block); |
| 1487 | } |
| 1488 | var cols = Object.keys(byCol); |
| 1489 | for (var c = 0; c < cols.length; c++) |
| 1490 | { |
| 1491 | var list = byCol[cols[c]]; |
| 1492 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1493 | this.pushRuns(lines, list, true, function (a, b) |
| 1494 | { |
| 1495 | return a.color === b.color && a.color >= 0 |
| 1496 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 1497 | }); |
| 1498 | } |
| 1499 | return lines; |
| 1500 | }; |
| 1501 | |
| 1502 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 1503 | { |
| 1504 | var start = 0; |
| 1505 | for (var i = 1; i <= list.length; i++) |
| 1506 | { |
| 1507 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 1508 | if (!same) |
| 1509 | { |
| 1510 | var length = i - start; |
| 1511 | if (length >= 3) |
| 1512 | { |
| 1513 | lines.push({ |
| 1514 | blocks: list.slice(start, i), |
| 1515 | length: length, |
| 1516 | vertical: vertical |
| 1517 | }); |
| 1518 | } |
| 1519 | start = i; |
| 1520 | } |
| 1521 | } |
| 1522 | }; |
| 1523 | |
| 1524 | GameCore.prototype.resolveMatches = function () |
| 1525 | { |
| 1526 | if (this.state !== 'playing' || this.drag) |
| 1527 | { return []; } |
| 1528 | var lines = this.findMatchLines(); |
| 1529 | if (!lines.length) |
| 1530 | { return []; } |
| 1531 | |
| 1532 | var uf = new UnionFind(); |
| 1533 | var blockById = Object.create(null); |
| 1534 | for (var i = 0; i < lines.length; i++) |
| 1535 | { |
| 1536 | var line = lines[i]; |
| 1537 | for (var j = 0; j < line.blocks.length; j++) |
| 1538 | { |
| 1539 | uf.add(line.blocks[j].id); |
| 1540 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 1541 | if (j > 0) |
| 1542 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 1543 | } |
| 1544 | } |
| 1545 | var matched = []; |
| 1546 | var seen = Object.create(null); |
| 1547 | for (var id in blockById) |
| 1548 | { |
| 1549 | if (!seen[id]) |
| 1550 | { |
| 1551 | seen[id] = true; |
| 1552 | matched.push(blockById[id]); |
| 1553 | } |
| 1554 | } |
| 1555 | for (var i = 0; i < matched.length; i++) |
| 1556 | { |
| 1557 | for (var j = i + 1; j < matched.length; j++) |
| 1558 | { |
| 1559 | if (matched[i].col === matched[j].col) |
| 1560 | { uf.union(matched[i].id, matched[j].id); } |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | var groups = Object.create(null); |
| 1565 | for (var i = 0; i < matched.length; i++) |
| 1566 | { |
| 1567 | var root = uf.find(matched[i].id); |
| 1568 | if (!groups[root]) |
| 1569 | { groups[root] = []; } |
| 1570 | groups[root].push(matched[i]); |
| 1571 | } |
| 1572 | |
| 1573 | var launched = []; |
| 1574 | for (var root in groups) |
| 1575 | { |
| 1576 | var group = groups[root]; |
| 1577 | var height = 0; |
| 1578 | for (var i = 0; i < lines.length; i++) |
| 1579 | { |
| 1580 | var line = lines[i]; |
| 1581 | var overlaps = false; |
| 1582 | for (var j = 0; j < line.blocks.length; j++) |
| 1583 | { |
| 1584 | for (var k = 0; k < group.length; k++) |
| 1585 | { |
| 1586 | if (line.blocks[j].id === group[k].id) |
| 1587 | { overlaps = true; } |
| 1588 | } |
| 1589 | } |
| 1590 | if (overlaps) |
| 1591 | { |
| 1592 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 1593 | if (h > height) |
| 1594 | { height = h; } |
| 1595 | } |
| 1596 | } |
| 1597 | this.launchCluster(group, height); |
| 1598 | launched.push({ blocks: group, jumpHeight: height }); |
| 1599 | } |
| 1600 | return launched; |
| 1601 | }; |
| 1602 | |
| 1603 | /** |
| 1604 | * Gray the matched blocks, take everything sitting on top of them in |
| 1605 | * those columns, link them, and launch with jump height `height`. |
| 1606 | */ |
| 1607 | GameCore.prototype.launchCluster = function (matchedBlocks, height) |
| 1608 | { |
| 1609 | var lowestInCol = Object.create(null); |
| 1610 | var matchedIds = Object.create(null); |
| 1611 | for (var i = 0; i < matchedBlocks.length; i++) |
| 1612 | { |
| 1613 | var block = matchedBlocks[i]; |
| 1614 | matchedIds[block.id] = true; |
| 1615 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 1616 | { lowestInCol[block.col] = block.y; } |
| 1617 | } |
| 1618 | |
| 1619 | var clusterId = this.nextClusterId++; |
| 1620 | var cluster = { |
| 1621 | id: clusterId, |
| 1622 | jumpHeight: height, |
| 1623 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 1624 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 1625 | phase: 'up' |
| 1626 | }; |
| 1627 | this.clusters[clusterId] = cluster; |
| 1628 | |
| 1629 | var riders = []; |
| 1630 | var cols = Object.keys(lowestInCol); |
| 1631 | for (var c = 0; c < cols.length; c++) |
| 1632 | { |
| 1633 | var col = +cols[c]; |
| 1634 | var lowest = lowestInCol[col]; |
| 1635 | var list = this.blocksInColumn(col); |
| 1636 | for (var i = 0; i < list.length; i++) |
| 1637 | { |
| 1638 | var block = list[i]; |
| 1639 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 1640 | { |
| 1641 | if (block.state !== STATE_SETTLING) |
| 1642 | { continue; } |
| 1643 | } |
| 1644 | if (block.y + ALIGN_EPS >= lowest) |
| 1645 | { riders.push(block); } |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | for (var i = 0; i < riders.length; i++) |
| 1650 | { |
| 1651 | var block = riders[i]; |
| 1652 | var isMatch = !!matchedIds[block.id]; |
| 1653 | if (isMatch || block.color === COLOR_GRAY) |
| 1654 | { |
| 1655 | block.color = COLOR_GRAY; |
| 1656 | block.flame = true; |
| 1657 | } |
| 1658 | else |
| 1659 | { block.flame = false; } |
| 1660 | block.state = STATE_FLYING; |
| 1661 | block.clusterId = clusterId; |
| 1662 | block.vy = cluster.vy; |
| 1663 | block.settleTime = 0; |
| 1664 | } |
| 1665 | return cluster; |
| 1666 | }; |
| 1667 | |
| 1668 | GameCore.prototype.checkLocks = function () |
| 1669 | { |
| 1670 | for (var col = 0; col < this.width; col++) |
| 1671 | { |
| 1672 | if (this.isColumnLocked(col)) |
| 1673 | { continue; } |
| 1674 | if (this.groundedCount(col) < this.height) |
| 1675 | { continue; } |
| 1676 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 1677 | var filled = true; |
| 1678 | for (var row = 0; row < this.height; row++) |
| 1679 | { |
| 1680 | var block = this.blockAt(col, row); |
| 1681 | if (!block || !this.isGrounded(block)) |
| 1682 | { |
| 1683 | filled = false; |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
| 1687 | if (!filled) |
| 1688 | { continue; } |
| 1689 | var list = this.blocksInColumn(col); |
| 1690 | for (var i = 0; i < list.length; i++) |
| 1691 | { |
| 1692 | var block = list[i]; |
| 1693 | if (!this.isGrounded(block)) |
| 1694 | { continue; } |
| 1695 | block.state = STATE_LOCKED; |
| 1696 | block.color = COLOR_GRAY; |
| 1697 | block.flame = false; |
| 1698 | block.vy = 0; |
| 1699 | block.settleTime = 0; |
| 1700 | if (block.clusterId != null) |
| 1701 | { this.leaveCluster(block); } |
| 1702 | } |
| 1703 | } |
| 1704 | }; |
| 1705 | |
| 1706 | GameCore.prototype.checkGameOver = function () |
| 1707 | { |
| 1708 | if (this.state !== 'playing') |
| 1709 | { return; } |
| 1710 | for (var col = 0; col < this.width; col++) |
| 1711 | { |
| 1712 | if (!this.isColumnLocked(col)) |
| 1713 | { return; } |
| 1714 | } |
| 1715 | this.state = 'gameover'; |
| 1716 | this.gameOverTime = 0; |
| 1717 | this.drag = null; |
| 1718 | }; |
| 1719 | |
| 1720 | /* ---------- dragging -------------------------------------------------- */ |
| 1721 | |
| 1722 | GameCore.prototype.beginDrag = function (col, y) |
| 1723 | { |
| 1724 | if (this.state !== 'playing') |
| 1725 | { return false; } |
| 1726 | col = col | 0; |
| 1727 | if (col < 0 || col >= this.width) |
| 1728 | { return false; } |
| 1729 | if (!this.isColumnStill(col)) |
| 1730 | { return false; } |
| 1731 | var row = Math.floor(y + 1e-6); |
| 1732 | if (row < 0) |
| 1733 | { return false; } |
| 1734 | var block = this.blockAt(col, row); |
| 1735 | if (!block || block.state !== STATE_STILL || block.color < 0) |
| 1736 | { return 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 | }; |
| 1746 | return true; |
| 1747 | }; |
| 1748 | |
| 1749 | GameCore.prototype.moveDrag = function (y) |
| 1750 | { |
| 1751 | if (!this.drag) |
| 1752 | { return; } |
| 1753 | var block = this.blockById(this.drag.blockId); |
| 1754 | if (!block || block.state !== STATE_STILL) |
| 1755 | { |
| 1756 | this.drag = null; |
| 1757 | return; |
| 1758 | } |
| 1759 | if (!this.isColumnStill(block.col)) |
| 1760 | { |
| 1761 | this.drag = null; |
| 1762 | return; |
| 1763 | } |
| 1764 | this.drag.pointerY = y; |
| 1765 | this.swapToward(block, y); |
| 1766 | var top = this.columnStackTopRow(block.col); |
| 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 | } |
| 1780 | }; |
| 1781 | |
| 1782 | GameCore.prototype.endDrag = function () |
| 1783 | { |
| 1784 | if (!this.drag) |
| 1785 | { return; } |
| 1786 | var block = this.blockById(this.drag.blockId); |
| 1787 | var launch = this.drag.launchIntent; |
| 1788 | this.drag = null; |
| 1789 | if (launch && block && block.state === STATE_STILL) |
| 1790 | { this.startManualLaunch(block); } |
| 1791 | else |
| 1792 | { this.resolveMatches(); } |
| 1793 | }; |
| 1794 | |
| 1795 | GameCore.prototype.blockById = function (id) |
| 1796 | { |
| 1797 | for (var i = 0; i < this.blocks.length; i++) |
| 1798 | { |
| 1799 | if (this.blocks[i].id === id) |
| 1800 | { return this.blocks[i]; } |
| 1801 | } |
| 1802 | return null; |
| 1803 | }; |
| 1804 | |
| 1805 | GameCore.prototype.columnStackTopRow = function (col) |
| 1806 | { |
| 1807 | var top = -1; |
| 1808 | var list = this.blocksInColumn(col); |
| 1809 | for (var i = 0; i < list.length; i++) |
| 1810 | { |
| 1811 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 1812 | { top = list[i].y; } |
| 1813 | } |
| 1814 | return top; |
| 1815 | }; |
| 1816 | |
| 1817 | GameCore.prototype.swapToward = function (block, targetY) |
| 1818 | { |
| 1819 | var guard = 0; |
| 1820 | while (guard++ < this.height + 2) |
| 1821 | { |
| 1822 | var current = block.y; |
| 1823 | if (targetY > current + 0.5) |
| 1824 | { |
| 1825 | var above = this.blockAt(block.col, current + 1); |
| 1826 | if (above && this.isGrounded(above) && above !== block) |
| 1827 | { this.swapBlocks(block, above); } |
| 1828 | else |
| 1829 | { break; } |
| 1830 | } |
| 1831 | else if (targetY < current - 0.5) |
| 1832 | { |
| 1833 | var below = this.blockAt(block.col, current - 1); |
| 1834 | if (below && this.isGrounded(below) && below !== block) |
| 1835 | { this.swapBlocks(block, below); } |
| 1836 | else |
| 1837 | { break; } |
| 1838 | } |
| 1839 | else |
| 1840 | { break; } |
| 1841 | } |
| 1842 | }; |
| 1843 | |
| 1844 | GameCore.prototype.swapBlocks = function (a, b) |
| 1845 | { |
| 1846 | var y = a.y; |
| 1847 | a.y = b.y; |
| 1848 | b.y = y; |
| 1849 | }; |
| 1850 | |
| 1851 | /** |
| 1852 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 1853 | * Returns the moved block, or null if the column is not still. |
| 1854 | */ |
| 1855 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 1856 | { |
| 1857 | if (!this.isColumnStill(col)) |
| 1858 | { return null; } |
| 1859 | var block = this.blockAt(col, fromRow); |
| 1860 | if (!block) |
| 1861 | { return null; } |
| 1862 | this.swapToward(block, toRow); |
| 1863 | return block; |
| 1864 | }; |
| 1865 | |
| 1866 | /** |
| 1867 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 1868 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 1869 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 1870 | * their color with a marker in `flying`. |
| 1871 | */ |
| 1872 | GameCore.prototype.snapshot = function () |
| 1873 | { |
| 1874 | var grid = []; |
| 1875 | for (var r = 0; r < this.height; r++) |
| 1876 | { |
| 1877 | var row = []; |
| 1878 | for (var c = 0; c < this.width; c++) |
| 1879 | { row.push('.'); } |
| 1880 | grid.push(row); |
| 1881 | } |
| 1882 | var airborne = []; |
| 1883 | for (var i = 0; i < this.blocks.length; i++) |
| 1884 | { |
| 1885 | var block = this.blocks[i]; |
| 1886 | var ch; |
| 1887 | if (block.state === STATE_LOCKED) |
| 1888 | { ch = 'L'; } |
| 1889 | else if (block.color === COLOR_GRAY) |
| 1890 | { ch = 'g'; } |
| 1891 | else |
| 1892 | { ch = String(block.color); } |
| 1893 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 1894 | { |
| 1895 | var row = Math.round(block.y); |
| 1896 | if (row >= 0 && row < this.height) |
| 1897 | { grid[row][block.col] = ch; } |
| 1898 | } |
| 1899 | else |
| 1900 | { |
| 1901 | airborne.push({ |
| 1902 | id: block.id, |
| 1903 | col: block.col, |
| 1904 | y: block.y, |
| 1905 | color: block.color, |
| 1906 | state: block.state, |
| 1907 | clusterId: block.clusterId, |
| 1908 | flame: block.flame, |
| 1909 | vy: block.vy |
| 1910 | }); |
| 1911 | } |
| 1912 | } |
| 1913 | var lines = []; |
| 1914 | for (var r = this.height - 1; r >= 0; r--) |
| 1915 | { lines.push(grid[r].join('')); } |
| 1916 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 1917 | }; |
| 1918 | |
| 1919 | var GameCoreAPI = { |
| 1920 | GameCore: GameCore, |
| 1921 | COLOR_GRAY: COLOR_GRAY, |
| 1922 | STATE_STILL: STATE_STILL, |
| 1923 | STATE_FALLING: STATE_FALLING, |
| 1924 | STATE_FLYING: STATE_FLYING, |
| 1925 | STATE_SETTLING: STATE_SETTLING, |
| 1926 | STATE_LOCKED: STATE_LOCKED, |
| 1927 | STATE_MANUAL: STATE_MANUAL |
| 1928 | }; |
| 1929 | |
| 1930 | if (typeof module !== 'undefined' && module.exports) |
| 1931 | { module.exports = GameCoreAPI; } |
| 1932 | else if (typeof globalThis !== 'undefined') |
| 1933 | { |
| 1934 | globalThis.GameCore = GameCore; |
| 1935 | globalThis.GameCoreAPI = GameCoreAPI; |
| 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | * End File: |
| 1940 | * /workspace/meteors/src/gameCore.js |
| 1941 | */ |
| 1942 | |
| 1943 | /* |
| 1944 | * Start File: |
| 1945 | * /workspace/meteors/src/main.js |
| 1946 | */ |
| 1947 | 'use strict'; |
| 1948 | |
| 1949 | /** |
| 1950 | * View and input for Meteors. The simulation lives in GameCore; this file |
| 1951 | * only draws it, scales it to the viewport, and translates pointer events |
| 1952 | * into beginDrag / moveDrag / endDrag. Ready for a later multiplayer view |
| 1953 | * that reuses the same core. |
| 1954 | */ |
| 1955 | |
| 1956 | var IMAGES = [ |
| 1957 | { name: 'ember', url: 'images/block-ember.png' }, |
| 1958 | { name: 'aqua', url: 'images/block-aqua.png' }, |
| 1959 | { name: 'leaf', url: 'images/block-leaf.png' }, |
| 1960 | { name: 'sun', url: 'images/block-sun.png' }, |
| 1961 | { name: 'orchid', url: 'images/block-orchid.png' }, |
| 1962 | { name: 'gray', url: 'images/block-gray.png' }, |
| 1963 | { name: 'locked', url: 'images/block-locked.png' }, |
| 1964 | { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, |
| 1965 | { name: 'cell', url: 'images/cell.png' }, |
| 1966 | { name: 'pixel', url: 'images/pixel.png' } |
| 1967 | ]; |
| 1968 | |
| 1969 | var COLOR_SPRITES = ['ember', 'aqua', 'leaf', 'sun', 'orchid']; |
| 1970 | var GAME_OVER_DELAY = 2; |
| 1971 | |
| 1972 | var game = new Game(); |
| 1973 | var app = { |
| 1974 | mode: 'menu', |
| 1975 | core: null, |
| 1976 | scene: null, |
| 1977 | manager: null, |
| 1978 | planet: null, |
| 1979 | lastScore: 0, |
| 1980 | tile: 48, |
| 1981 | fieldX: 0, |
| 1982 | fieldY: 0, |
| 1983 | fieldW: 0, |
| 1984 | fieldH: 0, |
| 1985 | hudH: 56, |
| 1986 | sprites: Object.create(null), |
| 1987 | flames: Object.create(null), |
| 1988 | cells: [], |
| 1989 | pointerId: null, |
| 1990 | gameOverShown: false, |
| 1991 | selectTween: null |
| 1992 | }; |
| 1993 | |
| 1994 | window.onload = function () |
| 1995 | { |
| 1996 | var play = document.getElementById('btn-play'); |
| 1997 | play.addEventListener('click', onPlay); |
| 1998 | play.addEventListener('pointerup', function (e) |
| 1999 | { |
| 2000 | // A touch-friendly path: some mobile browsers swallow the click |
| 2001 | // after a slightly sloppy tap. Ignore the following click. |
| 2002 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 2003 | { |
| 2004 | e.preventDefault(); |
| 2005 | onPlay(); |
| 2006 | } |
| 2007 | }); |
| 2008 | |
| 2009 | game.preloadImagesThenStart(IMAGES, function () |
| 2010 | { |
| 2011 | if (game.images.flame && game.images.flame.addAnimationCycle) |
| 2012 | { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } |
| 2013 | }); |
| 2014 | }; |
| 2015 | |
| 2016 | function onPlay () |
| 2017 | { |
| 2018 | if (app.mode === 'playing') |
| 2019 | { return; } |
| 2020 | startPlay(); |
| 2021 | } |
| 2022 | |
| 2023 | function startPlay () |
| 2024 | { |
| 2025 | hideEl('menu'); |
| 2026 | hideEl('game-over'); |
| 2027 | showEl('hud'); |
| 2028 | |
| 2029 | app.planet = (typeof Planet !== 'undefined' ? Planet.defaultPlanet() : PlanetMod.defaultPlanet()); |
| 2030 | app.core = new GameCore(app.planet, Math.random); |
| 2031 | app.mode = 'playing'; |
| 2032 | app.gameOverShown = false; |
| 2033 | app.sprites = Object.create(null); |
| 2034 | app.flames = Object.create(null); |
| 2035 | app.cells = []; |
| 2036 | app.pointerId = null; |
| 2037 | app.selectTween = null; |
| 2038 | |
| 2039 | layout(); |
| 2040 | app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); |
| 2041 | app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; |
| 2042 | app.scene.renderBackground = true; |
| 2043 | game.loadScene(app.scene); |
| 2044 | |
| 2045 | buildCells(); |
| 2046 | app.manager = app.scene.addObject(makeManager()); |
| 2047 | bindPointer(app.scene.canvas); |
| 2048 | refreshHud(); |
| 2049 | } |
| 2050 | |
| 2051 | function returnToMenu () |
| 2052 | { |
| 2053 | unbindPointer(); |
| 2054 | if (app.scene) |
| 2055 | { |
| 2056 | app.scene.destroy(); |
| 2057 | app.scene = null; |
| 2058 | } |
| 2059 | app.manager = null; |
| 2060 | app.core = null; |
| 2061 | app.mode = 'menu'; |
| 2062 | app.sprites = Object.create(null); |
| 2063 | app.flames = Object.create(null); |
| 2064 | app.cells = []; |
| 2065 | app.selectTween = null; |
| 2066 | |
| 2067 | hideEl('hud'); |
| 2068 | hideEl('game-over'); |
| 2069 | showEl('menu'); |
| 2070 | var last = document.getElementById('last-score'); |
| 2071 | if (app.lastScore > 0) |
| 2072 | { |
| 2073 | last.hidden = false; |
| 2074 | last.querySelector('strong').textContent = String(app.lastScore); |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | function buildCells () |
| 2079 | { |
| 2080 | if (!app.scene) |
| 2081 | { return; } |
| 2082 | var img = game.images.cell; |
| 2083 | for (var row = 0; row < 13; row++) |
| 2084 | { |
| 2085 | for (var col = 0; col < 10; col++) |
| 2086 | { |
| 2087 | var obj = new GameObject(0, 0, img, 0); |
| 2088 | placeTile(obj, col, row); |
| 2089 | obj.renderer.alpha = 0.9; |
| 2090 | app.scene.addObject(obj); |
| 2091 | app.cells.push(obj); |
| 2092 | } |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | function placeTile (obj, col, y) |
| 2097 | { |
| 2098 | var t = app.tile; |
| 2099 | obj.transform.position.x = col * t + t / 2; |
| 2100 | obj.transform.position.y = app.fieldH - (y + 0.5) * t; |
| 2101 | obj.transform.size = { x: t, y: t }; |
| 2102 | } |
| 2103 | |
| 2104 | function layout () |
| 2105 | { |
| 2106 | var vw = window.innerWidth || 320; |
| 2107 | var vh = window.innerHeight || 480; |
| 2108 | var safeTop = 0; |
| 2109 | try |
| 2110 | { |
| 2111 | // env(safe-area) is a CSS thing; keep a conservative HUD height. |
| 2112 | safeTop = 0; |
| 2113 | } |
| 2114 | catch (e) { /* ignore */ } |
| 2115 | app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); |
| 2116 | var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); |
| 2117 | var availW = Math.max(80, vw - margin * 2); |
| 2118 | var availH = Math.max(80, vh - app.hudH - margin); |
| 2119 | app.tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); |
| 2120 | app.fieldW = app.tile * 10; |
| 2121 | app.fieldH = app.tile * 13; |
| 2122 | app.fieldX = Math.floor((vw - app.fieldW) / 2); |
| 2123 | app.fieldY = app.hudH + Math.floor((vh - app.hudH - app.fieldH - margin / 2) / 2); |
| 2124 | if (app.fieldY < app.hudH) |
| 2125 | { app.fieldY = app.hudH; } |
| 2126 | |
| 2127 | if (app.scene) |
| 2128 | { |
| 2129 | app.scene.position.x = app.fieldX; |
| 2130 | app.scene.position.y = app.fieldY; |
| 2131 | app.scene.size.x = app.fieldW; |
| 2132 | app.scene.size.y = app.fieldH; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | function spriteFor (block) |
| 2137 | { |
| 2138 | if (block.state === 'locked') |
| 2139 | { return game.images.locked; } |
| 2140 | if (block.color < 0) |
| 2141 | { return game.images.gray; } |
| 2142 | return game.images[COLOR_SPRITES[block.color]] || game.images.gray; |
| 2143 | } |
| 2144 | |
| 2145 | function syncSprites () |
| 2146 | { |
| 2147 | if (!app.core || !app.scene) |
| 2148 | { return; } |
| 2149 | var seen = Object.create(null); |
| 2150 | var blocks = app.core.blocks; |
| 2151 | for (var i = 0; i < blocks.length; i++) |
| 2152 | { |
| 2153 | var block = blocks[i]; |
| 2154 | seen[block.id] = true; |
| 2155 | var obj = app.sprites[block.id]; |
| 2156 | if (!obj) |
| 2157 | { |
| 2158 | obj = new GameObject(0, 0, spriteFor(block), 2); |
| 2159 | app.scene.addObject(obj); |
| 2160 | app.sprites[block.id] = obj; |
| 2161 | } |
| 2162 | obj.renderer.sprite = spriteFor(block); |
| 2163 | placeTile(obj, block.col, block.y); |
| 2164 | applyBlockVisual(obj, block); |
| 2165 | obj.renderer.visible = true; |
| 2166 | obj.renderer.alpha = 1; |
| 2167 | |
| 2168 | if (block.flame) |
| 2169 | { |
| 2170 | var flame = app.flames[block.id]; |
| 2171 | if (!flame) |
| 2172 | { |
| 2173 | flame = new GameObject(0, 0, game.images.flame, 1); |
| 2174 | flame.renderer.setAnimation('burn', 0, 0.08); |
| 2175 | app.scene.addObject(flame); |
| 2176 | app.flames[block.id] = flame; |
| 2177 | } |
| 2178 | flame.renderer.visible = true; |
| 2179 | var t = app.tile; |
| 2180 | flame.transform.position.x = block.col * t + t / 2; |
| 2181 | flame.transform.position.y = app.fieldH - block.y * t + t * 0.15; |
| 2182 | flame.transform.size = { x: t * 0.85, y: t * 0.85 }; |
| 2183 | } |
| 2184 | else if (app.flames[block.id]) |
| 2185 | { app.flames[block.id].renderer.visible = false; } |
| 2186 | } |
| 2187 | |
| 2188 | var ids = Object.keys(app.sprites); |
| 2189 | for (var i = 0; i < ids.length; i++) |
| 2190 | { |
| 2191 | if (!seen[ids[i]]) |
| 2192 | { |
| 2193 | app.sprites[ids[i]].destroy(); |
| 2194 | delete app.sprites[ids[i]]; |
| 2195 | if (app.flames[ids[i]]) |
| 2196 | { |
| 2197 | app.flames[ids[i]].destroy(); |
| 2198 | delete app.flames[ids[i]]; |
| 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | } |
| 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 | |
| 2294 | function refreshHud () |
| 2295 | { |
| 2296 | if (!app.core) |
| 2297 | { return; } |
| 2298 | document.getElementById('score').textContent = String(app.core.score); |
| 2299 | var planet = app.planet; |
| 2300 | document.getElementById('planet-name').textContent = planet.name + ' · ' + planet.people; |
| 2301 | var d = Planet.displayValues(planet); |
| 2302 | document.getElementById('planet-stats').textContent = |
| 2303 | 'g ' + d.gravity.toFixed(2) + ' d ' + d.density.toFixed(2) + |
| 2304 | ' F ' + Math.round(d.newtons) + ' N Power ' + d.power + '%'; |
| 2305 | } |
| 2306 | |
| 2307 | function makeManager () |
| 2308 | { |
| 2309 | var obj = new GameObject(0, 0, game.images.pixel, 0); |
| 2310 | obj.renderer.visible = false; |
| 2311 | var base = obj.update.bind(obj); |
| 2312 | obj.update = function (dt) |
| 2313 | { |
| 2314 | base(dt); |
| 2315 | tickView(dt); |
| 2316 | }; |
| 2317 | return obj; |
| 2318 | } |
| 2319 | |
| 2320 | function tickView (dt) |
| 2321 | { |
| 2322 | if (app.mode !== 'playing' || !app.core) |
| 2323 | { return; } |
| 2324 | |
| 2325 | layout(); |
| 2326 | for (var i = 0; i < app.cells.length; i++) |
| 2327 | { |
| 2328 | var col = i % 10; |
| 2329 | var row = Math.floor(i / 10); |
| 2330 | placeTile(app.cells[i], col, row); |
| 2331 | } |
| 2332 | |
| 2333 | app.core.tick(dt); |
| 2334 | updateSelectTween(dt); |
| 2335 | syncSprites(); |
| 2336 | refreshHud(); |
| 2337 | |
| 2338 | if (app.core.state === 'gameover' && !app.gameOverShown) |
| 2339 | { |
| 2340 | app.gameOverShown = true; |
| 2341 | app.lastScore = app.core.score; |
| 2342 | document.getElementById('go-score').textContent = String(app.lastScore); |
| 2343 | showEl('game-over'); |
| 2344 | } |
| 2345 | if (app.core.state === 'gameover' && app.core.gameOverTime >= GAME_OVER_DELAY) |
| 2346 | { returnToMenu(); } |
| 2347 | } |
| 2348 | |
| 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 | */ |
| 2357 | |
| 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 | } |
| 2380 | |
| 2381 | function bindPointer (canvas) |
| 2382 | { |
| 2383 | unbindPointer(); |
| 2384 | boundCanvas = canvas; |
| 2385 | canvas.addEventListener('pointerdown', onPointerDown); |
| 2386 | canvas.style.touchAction = 'none'; |
| 2387 | canvas.style.cursor = 'pointer'; |
| 2388 | } |
| 2389 | |
| 2390 | function unbindPointer () |
| 2391 | { |
| 2392 | removeWindowPointer(); |
| 2393 | if (boundCanvas) |
| 2394 | { boundCanvas.removeEventListener('pointerdown', onPointerDown); } |
| 2395 | boundCanvas = null; |
| 2396 | app.pointerId = null; |
| 2397 | } |
| 2398 | |
| 2399 | function fieldFromEvent (e) |
| 2400 | { |
| 2401 | var canvas = app.scene && app.scene.canvas; |
| 2402 | if (!canvas) |
| 2403 | { return null; } |
| 2404 | var rect = canvas.getBoundingClientRect(); |
| 2405 | if (rect.width <= 0 || rect.height <= 0) |
| 2406 | { return null; } |
| 2407 | var x = (e.clientX - rect.left) * (canvas.width / rect.width); |
| 2408 | var y = (e.clientY - rect.top) * (canvas.height / rect.height); |
| 2409 | var col = Math.floor(x / app.tile); |
| 2410 | var fieldY = (canvas.height - y) / app.tile; |
| 2411 | return { col: col, y: fieldY }; |
| 2412 | } |
| 2413 | |
| 2414 | function onPointerDown (e) |
| 2415 | { |
| 2416 | if (!app.core || app.core.state !== 'playing') |
| 2417 | { return; } |
| 2418 | if (app.pointerId != null) |
| 2419 | { return; } |
| 2420 | e.preventDefault(); |
| 2421 | app.pointerId = e.pointerId; |
| 2422 | try |
| 2423 | { boundCanvas.setPointerCapture(e.pointerId); } |
| 2424 | catch (err) { /* ignore */ } |
| 2425 | addWindowPointer(); |
| 2426 | var pos = fieldFromEvent(e); |
| 2427 | if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag) |
| 2428 | { startPressTween(app.core.drag.blockId); } |
| 2429 | } |
| 2430 | |
| 2431 | function onPointerMove (e) |
| 2432 | { |
| 2433 | if (app.pointerId == null || e.pointerId !== app.pointerId) |
| 2434 | { return; } |
| 2435 | if (!app.core || !app.core.drag) |
| 2436 | { return; } |
| 2437 | e.preventDefault(); |
| 2438 | var pos = fieldFromEvent(e); |
| 2439 | if (pos) |
| 2440 | { app.core.moveDrag(pos.y); } |
| 2441 | } |
| 2442 | |
| 2443 | function onPointerUp (e) |
| 2444 | { |
| 2445 | if (app.pointerId == null) |
| 2446 | { return; } |
| 2447 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 2448 | { return; } |
| 2449 | if (e) |
| 2450 | { e.preventDefault(); } |
| 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(); |
| 2460 | if (app.core) |
| 2461 | { app.core.endDrag(); } |
| 2462 | } |
| 2463 | |
| 2464 | function hideEl (id) |
| 2465 | { |
| 2466 | document.getElementById(id).hidden = true; |
| 2467 | } |
| 2468 | |
| 2469 | function showEl (id) |
| 2470 | { |
| 2471 | document.getElementById(id).hidden = false; |
| 2472 | } |
| 2473 | |
| 2474 | window.addEventListener('resize', function () |
| 2475 | { |
| 2476 | if (app.mode === 'playing') |
| 2477 | { layout(); } |
| 2478 | }); |
| 2479 | |
| 2480 | window.addEventListener('orientationchange', function () |
| 2481 | { |
| 2482 | setTimeout(function () |
| 2483 | { |
| 2484 | if (app.mode === 'playing') |
| 2485 | { layout(); } |
| 2486 | }, 120); |
| 2487 | }); |
| 2488 | |
| 2489 | /* |
| 2490 | * End File: |
| 2491 | * /workspace/meteors/src/main.js |
| 2492 | */ |
projects-timfalken-com meteors