| 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 | // Minimal accumulated vertical pointer travel (tiles) to swap one step. |
| 394 | // No hold delay: the first swap fires as soon as this distance is reached. |
| 395 | var SWAP_DISTANCE = 0.25; |
| 396 | // Grab a still block if the pointer is at most this far (tiles) from it. |
| 397 | var SNAP_Y = 0.65; |
| 398 | // Horizontal slop (tiles) for snapping to a neighbouring column on press. |
| 399 | var HORIZONTAL_SLOP = 0.35; |
| 400 | |
| 401 | function UnionFind () |
| 402 | { |
| 403 | this.parent = Object.create(null); |
| 404 | } |
| 405 | |
| 406 | UnionFind.prototype.add = function (id) |
| 407 | { |
| 408 | if (this.parent[id] == null) |
| 409 | { this.parent[id] = id; } |
| 410 | }; |
| 411 | |
| 412 | UnionFind.prototype.find = function (id) |
| 413 | { |
| 414 | if (this.parent[id] !== id) |
| 415 | { this.parent[id] = this.find(this.parent[id]); } |
| 416 | return this.parent[id]; |
| 417 | }; |
| 418 | |
| 419 | UnionFind.prototype.union = function (a, b) |
| 420 | { |
| 421 | this.add(a); |
| 422 | this.add(b); |
| 423 | var ra = this.find(a); |
| 424 | var rb = this.find(b); |
| 425 | if (ra !== rb) |
| 426 | { this.parent[rb] = ra; } |
| 427 | }; |
| 428 | |
| 429 | function GameCore (planet, rng) |
| 430 | { |
| 431 | this.planet = planet || PlanetMod.defaultPlanet(); |
| 432 | this.rng = rng || Math.random; |
| 433 | this.width = PlanetMod.FIELD_WIDTH; |
| 434 | this.height = PlanetMod.FIELD_HEIGHT; |
| 435 | this.blocks = []; |
| 436 | this.clusters = Object.create(null); |
| 437 | this.nextBlockId = 1; |
| 438 | this.nextClusterId = 1; |
| 439 | this.spawnTimer = 0; |
| 440 | this.spawnInterval = PlanetMod.SPAWN_INTERVAL; |
| 441 | this.settleTime = PlanetMod.SETTLE_TIME; |
| 442 | this.score = 0; |
| 443 | this.state = 'playing'; |
| 444 | this.drag = null; |
| 445 | this.gameOverTime = 0; |
| 446 | } |
| 447 | |
| 448 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 449 | GameCore.LAUNCH_PULL = LAUNCH_PULL; |
| 450 | GameCore.SWAP_DISTANCE = SWAP_DISTANCE; |
| 451 | GameCore.SNAP_Y = SNAP_Y; |
| 452 | GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP; |
| 453 | GameCore.STATE_STILL = STATE_STILL; |
| 454 | GameCore.STATE_FALLING = STATE_FALLING; |
| 455 | GameCore.STATE_FLYING = STATE_FLYING; |
| 456 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 457 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 458 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 459 | |
| 460 | GameCore.prototype.randomColor = function () |
| 461 | { |
| 462 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 463 | }; |
| 464 | |
| 465 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 466 | { |
| 467 | var block = { |
| 468 | id: this.nextBlockId++, |
| 469 | col: col, |
| 470 | y: y, |
| 471 | color: color, |
| 472 | state: state || STATE_STILL, |
| 473 | vy: 0, |
| 474 | clusterId: null, |
| 475 | settleTime: 0, |
| 476 | flame: false, |
| 477 | pushedClusters: null, |
| 478 | manualTarget: 0, |
| 479 | manualDecel: 0, |
| 480 | manualPhase: null, |
| 481 | bouncing: false, |
| 482 | bounceAge: 0 |
| 483 | }; |
| 484 | this.blocks.push(block); |
| 485 | return block; |
| 486 | }; |
| 487 | |
| 488 | GameCore.prototype.removeBlock = function (block) |
| 489 | { |
| 490 | if (block.clusterId != null) |
| 491 | { this.leaveCluster(block); } |
| 492 | var index = this.blocks.indexOf(block); |
| 493 | if (index >= 0) |
| 494 | { this.blocks.splice(index, 1); } |
| 495 | }; |
| 496 | |
| 497 | GameCore.prototype.leaveCluster = function (block) |
| 498 | { |
| 499 | var id = block.clusterId; |
| 500 | block.clusterId = null; |
| 501 | block.flame = false; |
| 502 | if (id == null || !this.clusters[id]) |
| 503 | { return; } |
| 504 | var still = 0; |
| 505 | for (var i = 0; i < this.blocks.length; i++) |
| 506 | { |
| 507 | if (this.blocks[i].clusterId === id) |
| 508 | { still++; } |
| 509 | } |
| 510 | if (still === 0) |
| 511 | { delete this.clusters[id]; } |
| 512 | }; |
| 513 | |
| 514 | GameCore.prototype.blocksInColumn = function (col) |
| 515 | { |
| 516 | var out = []; |
| 517 | for (var i = 0; i < this.blocks.length; i++) |
| 518 | { |
| 519 | if (this.blocks[i].col === col) |
| 520 | { out.push(this.blocks[i]); } |
| 521 | } |
| 522 | out.sort(function (a, b) { return a.y - b.y; }); |
| 523 | return out; |
| 524 | }; |
| 525 | |
| 526 | GameCore.prototype.isGrounded = function (block) |
| 527 | { |
| 528 | return block.state === STATE_STILL |
| 529 | || block.state === STATE_SETTLING |
| 530 | || block.state === STATE_LOCKED; |
| 531 | }; |
| 532 | |
| 533 | GameCore.prototype.stackTop = function (col, ignore) |
| 534 | { |
| 535 | var top = 0; |
| 536 | for (var i = 0; i < this.blocks.length; i++) |
| 537 | { |
| 538 | var block = this.blocks[i]; |
| 539 | if (block.col !== col) |
| 540 | { continue; } |
| 541 | if (ignore && ignore(block)) |
| 542 | { continue; } |
| 543 | if (this.isGrounded(block)) |
| 544 | { |
| 545 | var surface = block.y + 1; |
| 546 | if (surface > top) |
| 547 | { top = surface; } |
| 548 | } |
| 549 | } |
| 550 | return top; |
| 551 | }; |
| 552 | |
| 553 | GameCore.prototype.isColumnLocked = function (col) |
| 554 | { |
| 555 | for (var i = 0; i < this.blocks.length; i++) |
| 556 | { |
| 557 | var block = this.blocks[i]; |
| 558 | if (block.col === col && block.state === STATE_LOCKED) |
| 559 | { return true; } |
| 560 | } |
| 561 | return false; |
| 562 | }; |
| 563 | |
| 564 | GameCore.prototype.isColumnStill = function (col) |
| 565 | { |
| 566 | if (this.isColumnLocked(col)) |
| 567 | { return false; } |
| 568 | for (var i = 0; i < this.blocks.length; i++) |
| 569 | { |
| 570 | var block = this.blocks[i]; |
| 571 | if (block.col !== col) |
| 572 | { continue; } |
| 573 | if (block.state === STATE_FLYING |
| 574 | || block.state === STATE_FALLING |
| 575 | || block.state === STATE_MANUAL |
| 576 | || block.state === STATE_SETTLING) |
| 577 | { return false; } |
| 578 | } |
| 579 | return true; |
| 580 | }; |
| 581 | |
| 582 | GameCore.prototype.groundedCount = function (col) |
| 583 | { |
| 584 | var n = 0; |
| 585 | for (var i = 0; i < this.blocks.length; i++) |
| 586 | { |
| 587 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 588 | { n++; } |
| 589 | } |
| 590 | return n; |
| 591 | }; |
| 592 | |
| 593 | GameCore.prototype.blockAt = function (col, row) |
| 594 | { |
| 595 | for (var i = 0; i < this.blocks.length; i++) |
| 596 | { |
| 597 | var block = this.blocks[i]; |
| 598 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 599 | { return block; } |
| 600 | } |
| 601 | return null; |
| 602 | }; |
| 603 | |
| 604 | GameCore.prototype.place = function (col, row, color, state) |
| 605 | { |
| 606 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 607 | }; |
| 608 | |
| 609 | GameCore.prototype.lockedColumnCount = function () |
| 610 | { |
| 611 | var n = 0; |
| 612 | for (var c = 0; c < this.width; c++) |
| 613 | { |
| 614 | if (this.isColumnLocked(c)) |
| 615 | { n++; } |
| 616 | } |
| 617 | return n; |
| 618 | }; |
| 619 | |
| 620 | GameCore.prototype.startBounce = function (block) |
| 621 | { |
| 622 | if (!block) |
| 623 | { return; } |
| 624 | block.bouncing = true; |
| 625 | block.bounceAge = 0; |
| 626 | }; |
| 627 | |
| 628 | GameCore.prototype.advanceBounces = function (dt) |
| 629 | { |
| 630 | var duration = this.planet.bounceDuration; |
| 631 | for (var i = 0; i < this.blocks.length; i++) |
| 632 | { |
| 633 | var block = this.blocks[i]; |
| 634 | if (!block.bouncing) |
| 635 | { continue; } |
| 636 | block.bounceAge += dt; |
| 637 | if (duration <= 0 || block.bounceAge >= duration) |
| 638 | { |
| 639 | block.bouncing = false; |
| 640 | block.bounceAge = duration; |
| 641 | } |
| 642 | } |
| 643 | }; |
| 644 | |
| 645 | /** |
| 646 | * True if `block` rests on the floor or on a solid (still / settling / |
| 647 | * locked) block immediately below it in the same column. |
| 648 | */ |
| 649 | GameCore.prototype.hasSupport = function (block) |
| 650 | { |
| 651 | if (block.y <= 1e-9) |
| 652 | { return true; } |
| 653 | for (var i = 0; i < this.blocks.length; i++) |
| 654 | { |
| 655 | var other = this.blocks[i]; |
| 656 | if (other === block || other.col !== block.col) |
| 657 | { continue; } |
| 658 | if (!this.isGrounded(other)) |
| 659 | { continue; } |
| 660 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 661 | { return true; } |
| 662 | } |
| 663 | return false; |
| 664 | }; |
| 665 | |
| 666 | /** Highest flying block in this column whose top is at or below `block.y + 1`. */ |
| 667 | GameCore.prototype.highestFlyingBelow = function (block) |
| 668 | { |
| 669 | var best = null; |
| 670 | for (var i = 0; i < this.blocks.length; i++) |
| 671 | { |
| 672 | var other = this.blocks[i]; |
| 673 | if (other === block || other.col !== block.col) |
| 674 | { continue; } |
| 675 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 676 | { continue; } |
| 677 | var cluster = this.clusters[other.clusterId]; |
| 678 | if (!cluster || cluster.phase === 'landed') |
| 679 | { continue; } |
| 680 | // The falling block is at or above this flyer (or already overlapping). |
| 681 | if (block.y + 1e-9 < other.y) |
| 682 | { continue; } |
| 683 | if (!best || other.y > best.y) |
| 684 | { best = other; } |
| 685 | } |
| 686 | return best; |
| 687 | }; |
| 688 | |
| 689 | GameCore.prototype.highestFallingBelow = function (block) |
| 690 | { |
| 691 | var best = null; |
| 692 | for (var i = 0; i < this.blocks.length; i++) |
| 693 | { |
| 694 | var other = this.blocks[i]; |
| 695 | if (other === block || other.col !== block.col) |
| 696 | { continue; } |
| 697 | if (other.state !== STATE_FALLING) |
| 698 | { continue; } |
| 699 | if (other.y > block.y - 1e-9) |
| 700 | { continue; } |
| 701 | if (!best || other.y > best.y) |
| 702 | { best = other; } |
| 703 | } |
| 704 | return best; |
| 705 | }; |
| 706 | |
| 707 | /** |
| 708 | * Make an unsupported still block start falling from rest, so it eases |
| 709 | * into fallSpeed under normal gravity. Skips the block being dragged. |
| 710 | */ |
| 711 | /** |
| 712 | * Strip leftover launch / manual kinematics so a block that has just |
| 713 | * become still or falling starts from rest. Used after a landed grey |
| 714 | * cluster recolors, and whenever an unsupported still block drops. |
| 715 | */ |
| 716 | GameCore.prototype.clearFlightState = function (block) |
| 717 | { |
| 718 | block.vy = 0; |
| 719 | block.flame = false; |
| 720 | block.manualPhase = null; |
| 721 | block.manualTarget = null; |
| 722 | block.manualDecel = 0; |
| 723 | block.pushedClusters = null; |
| 724 | }; |
| 725 | |
| 726 | GameCore.prototype.releaseUnsupported = function () |
| 727 | { |
| 728 | for (var i = 0; i < this.blocks.length; i++) |
| 729 | { |
| 730 | var block = this.blocks[i]; |
| 731 | if (block.state !== STATE_STILL) |
| 732 | { continue; } |
| 733 | if (this.drag && this.drag.blockId === block.id) |
| 734 | { continue; } |
| 735 | if (this.hasSupport(block)) |
| 736 | { continue; } |
| 737 | this.leaveCluster(block); |
| 738 | this.clearFlightState(block); |
| 739 | block.state = STATE_FALLING; |
| 740 | } |
| 741 | }; |
| 742 | |
| 743 | GameCore.prototype.accelerateFall = function (block, dt) |
| 744 | { |
| 745 | var target = -this.planet.fallSpeed; |
| 746 | if (block.vy > target) |
| 747 | { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); } |
| 748 | else |
| 749 | { block.vy = target; } |
| 750 | }; |
| 751 | |
| 752 | GameCore.prototype.landFallingBlock = function (block) |
| 753 | { |
| 754 | block.vy = 0; |
| 755 | block.flame = false; |
| 756 | this.startBounce(block); |
| 757 | if (block.color === COLOR_GRAY) |
| 758 | { |
| 759 | block.state = STATE_SETTLING; |
| 760 | block.settleTime = 0; |
| 761 | } |
| 762 | else |
| 763 | { block.state = STATE_STILL; } |
| 764 | }; |
| 765 | |
| 766 | /** |
| 767 | * Sit `block` on top of the flying cluster that `flyer` belongs to, and |
| 768 | * carry it along as cargo (no flame). Returns true if attached. |
| 769 | */ |
| 770 | GameCore.prototype.attachToFlyingCluster = function (block, flyer) |
| 771 | { |
| 772 | var cluster = this.clusters[flyer.clusterId]; |
| 773 | if (!cluster || cluster.phase === 'landed') |
| 774 | { return false; } |
| 775 | var roof = flyer.y + 1; |
| 776 | for (var i = 0; i < this.blocks.length; i++) |
| 777 | { |
| 778 | var other = this.blocks[i]; |
| 779 | if (other === block || other.col !== block.col) |
| 780 | { continue; } |
| 781 | if (other.clusterId !== cluster.id) |
| 782 | { continue; } |
| 783 | if (other.y + 1 > roof) |
| 784 | { roof = other.y + 1; } |
| 785 | } |
| 786 | block.y = roof; |
| 787 | block.state = STATE_FLYING; |
| 788 | block.clusterId = cluster.id; |
| 789 | block.vy = cluster.vy; |
| 790 | block.flame = false; |
| 791 | block.settleTime = 0; |
| 792 | return true; |
| 793 | }; |
| 794 | |
| 795 | GameCore.prototype.catchFallingOntoCluster = function (cluster, members) |
| 796 | { |
| 797 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 798 | { return; } |
| 799 | // Snapshot falling blocks; attaching mutates state. |
| 800 | var falling = []; |
| 801 | for (var i = 0; i < this.blocks.length; i++) |
| 802 | { |
| 803 | if (this.blocks[i].state === STATE_FALLING) |
| 804 | { falling.push(this.blocks[i]); } |
| 805 | } |
| 806 | for (var i = 0; i < falling.length; i++) |
| 807 | { |
| 808 | var block = falling[i]; |
| 809 | var flyer = null; |
| 810 | for (var m = 0; m < members.length; m++) |
| 811 | { |
| 812 | var other = members[m]; |
| 813 | if (other.col !== block.col) |
| 814 | { continue; } |
| 815 | // Overlap or rest on this member's top. |
| 816 | if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9) |
| 817 | { |
| 818 | if (!flyer || other.y > flyer.y) |
| 819 | { flyer = other; } |
| 820 | } |
| 821 | } |
| 822 | if (flyer) |
| 823 | { this.attachToFlyingCluster(block, flyer); } |
| 824 | } |
| 825 | }; |
| 826 | |
| 827 | GameCore.prototype.tick = function (dt) |
| 828 | { |
| 829 | if (this.state === 'gameover') |
| 830 | { |
| 831 | this.gameOverTime += Math.max(0, dt); |
| 832 | return; |
| 833 | } |
| 834 | if (dt <= 0) |
| 835 | { return; } |
| 836 | dt = Math.min(dt, 0.1); |
| 837 | |
| 838 | this.spawnTimer += dt; |
| 839 | while (this.spawnTimer >= this.spawnInterval) |
| 840 | { |
| 841 | this.spawnTimer -= this.spawnInterval; |
| 842 | this.trySpawn(); |
| 843 | } |
| 844 | |
| 845 | var left = dt; |
| 846 | while (left > 1e-12) |
| 847 | { |
| 848 | var step = Math.min(SUBSTEP, left); |
| 849 | this.integrateFalling(step); |
| 850 | this.integrateClusters(step); |
| 851 | this.mergeFlyingClusters(); |
| 852 | this.integrateManual(step); |
| 853 | this.cullOffTop(); |
| 854 | left -= step; |
| 855 | } |
| 856 | |
| 857 | this.releaseUnsupported(); |
| 858 | this.advanceSettle(dt); |
| 859 | // After a landed grey group is released, blocks that were sitting on |
| 860 | // hanging members are still STILL (the earlier pass ran while those |
| 861 | // members counted as support). Drop them from rest before matching, |
| 862 | // so they cannot form a mid-air match and shoot off the top. |
| 863 | this.releaseUnsupported(); |
| 864 | this.advanceBounces(dt); |
| 865 | if (!this.drag) |
| 866 | { this.resolveMatches(); } |
| 867 | this.checkLocks(); |
| 868 | this.checkGameOver(); |
| 869 | }; |
| 870 | |
| 871 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 872 | GameCore.prototype.step = function (seconds, dt) |
| 873 | { |
| 874 | dt = dt || SUBSTEP; |
| 875 | var t = 0; |
| 876 | while (t < seconds - 1e-15) |
| 877 | { |
| 878 | var s = Math.min(dt, seconds - t); |
| 879 | this.tick(s); |
| 880 | t += s; |
| 881 | } |
| 882 | }; |
| 883 | |
| 884 | GameCore.prototype.trySpawn = function () |
| 885 | { |
| 886 | if (this.state !== 'playing') |
| 887 | { return null; } |
| 888 | var eligible = []; |
| 889 | for (var col = 0; col < this.width; col++) |
| 890 | { |
| 891 | if (this.isColumnLocked(col)) |
| 892 | { continue; } |
| 893 | if (this.groundedCount(col) >= this.height) |
| 894 | { continue; } |
| 895 | var alreadyFalling = false; |
| 896 | var list = this.blocksInColumn(col); |
| 897 | for (var i = 0; i < list.length; i++) |
| 898 | { |
| 899 | if (list[i].state === STATE_FALLING) |
| 900 | { |
| 901 | alreadyFalling = true; |
| 902 | break; |
| 903 | } |
| 904 | } |
| 905 | if (!alreadyFalling) |
| 906 | { eligible.push(col); } |
| 907 | } |
| 908 | if (!eligible.length) |
| 909 | { return null; } |
| 910 | var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 911 | var color = this.randomColor(); |
| 912 | var block = this.createBlock(col, this.height, color, STATE_FALLING); |
| 913 | block.vy = -this.planet.fallSpeed; |
| 914 | return block; |
| 915 | }; |
| 916 | |
| 917 | GameCore.prototype.integrateFalling = function (dt) |
| 918 | { |
| 919 | var falling = []; |
| 920 | for (var i = 0; i < this.blocks.length; i++) |
| 921 | { |
| 922 | if (this.blocks[i].state === STATE_FALLING) |
| 923 | { falling.push(this.blocks[i]); } |
| 924 | } |
| 925 | // Lowest first, so a stack of falling blocks rests on itself. |
| 926 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 927 | for (var i = 0; i < falling.length; i++) |
| 928 | { |
| 929 | var block = falling[i]; |
| 930 | if (block.state !== STATE_FALLING) |
| 931 | { continue; } |
| 932 | this.accelerateFall(block, dt); |
| 933 | var nextY = block.y + block.vy * dt; |
| 934 | var flyer = this.highestFlyingBelow(block); |
| 935 | if (flyer && nextY <= flyer.y + 1 + 1e-9) |
| 936 | { |
| 937 | if (this.attachToFlyingCluster(block, flyer)) |
| 938 | { continue; } |
| 939 | } |
| 940 | var top = this.stackTop(block.col); |
| 941 | var below = this.highestFallingBelow(block); |
| 942 | if (below && below.y + 1 > top) |
| 943 | { top = below.y + 1; } |
| 944 | if (nextY <= top) |
| 945 | { |
| 946 | block.y = top; |
| 947 | this.landFallingBlock(block); |
| 948 | } |
| 949 | else |
| 950 | { block.y = nextY; } |
| 951 | } |
| 952 | }; |
| 953 | |
| 954 | GameCore.prototype.clusterMembers = function (clusterId) |
| 955 | { |
| 956 | var out = []; |
| 957 | for (var i = 0; i < this.blocks.length; i++) |
| 958 | { |
| 959 | if (this.blocks[i].clusterId === clusterId) |
| 960 | { out.push(this.blocks[i]); } |
| 961 | } |
| 962 | return out; |
| 963 | }; |
| 964 | |
| 965 | |
| 966 | /** |
| 967 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 968 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 969 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 970 | * upward deceleration. After the apex, vy eases from 0 to -fallSpeed |
| 971 | * at planet.fallAccel, then stays at terminal fallSpeed. |
| 972 | */ |
| 973 | GameCore.prototype.advanceJump = function (body, dt) |
| 974 | { |
| 975 | var dy = 0; |
| 976 | var left = dt; |
| 977 | if (body.phase === 'up') |
| 978 | { |
| 979 | var decel = body.decel; |
| 980 | var vy = body.vy; |
| 981 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 982 | if (tApex <= left + 1e-15) |
| 983 | { |
| 984 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 985 | body.vy = 0; |
| 986 | body.phase = 'down'; |
| 987 | left -= tApex; |
| 988 | } |
| 989 | else |
| 990 | { |
| 991 | var vyNew = vy - decel * left; |
| 992 | dy += (vy + vyNew) * 0.5 * left; |
| 993 | body.vy = vyNew; |
| 994 | left = 0; |
| 995 | } |
| 996 | } |
| 997 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 998 | { |
| 999 | body.vy = 0; |
| 1000 | body.phase = 'down'; |
| 1001 | } |
| 1002 | if (body.phase === 'down' && left > 0) |
| 1003 | { |
| 1004 | var target = -this.planet.fallSpeed; |
| 1005 | var accel = this.planet.fallAccel; |
| 1006 | if (accel < 1e-12) |
| 1007 | { |
| 1008 | body.vy = target; |
| 1009 | dy += body.vy * left; |
| 1010 | } |
| 1011 | else if (body.vy > target + 1e-12) |
| 1012 | { |
| 1013 | var tReach = (body.vy - target) / accel; |
| 1014 | if (tReach <= left + 1e-15) |
| 1015 | { |
| 1016 | dy += (body.vy + target) * 0.5 * tReach; |
| 1017 | body.vy = target; |
| 1018 | left -= tReach; |
| 1019 | dy += body.vy * left; |
| 1020 | } |
| 1021 | else |
| 1022 | { |
| 1023 | var vyNew = body.vy - accel * left; |
| 1024 | dy += (body.vy + vyNew) * 0.5 * left; |
| 1025 | body.vy = vyNew; |
| 1026 | } |
| 1027 | } |
| 1028 | else |
| 1029 | { |
| 1030 | body.vy = target; |
| 1031 | dy += body.vy * left; |
| 1032 | } |
| 1033 | } |
| 1034 | return dy; |
| 1035 | }; |
| 1036 | |
| 1037 | GameCore.prototype.integrateClusters = function (dt) |
| 1038 | { |
| 1039 | var ids = Object.keys(this.clusters); |
| 1040 | for (var n = 0; n < ids.length; n++) |
| 1041 | { |
| 1042 | var cluster = this.clusters[ids[n]]; |
| 1043 | if (!cluster) |
| 1044 | { continue; } |
| 1045 | var members = this.clusterMembers(cluster.id); |
| 1046 | if (!members.length) |
| 1047 | { |
| 1048 | delete this.clusters[cluster.id]; |
| 1049 | continue; |
| 1050 | } |
| 1051 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1052 | { continue; } |
| 1053 | |
| 1054 | var dy = this.advanceJump(cluster, dt); |
| 1055 | for (var i = 0; i < members.length; i++) |
| 1056 | { |
| 1057 | members[i].y += dy; |
| 1058 | members[i].vy = cluster.vy; |
| 1059 | } |
| 1060 | |
| 1061 | this.catchFallingOntoCluster(cluster, members); |
| 1062 | |
| 1063 | if (cluster.phase === 'down') |
| 1064 | { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); } |
| 1065 | } |
| 1066 | }; |
| 1067 | |
| 1068 | |
| 1069 | /** |
| 1070 | * If two or more flying clusters overlap in the same column, merge them |
| 1071 | * into one cluster. Velocity (and deceleration and jumpHeight) is the |
| 1072 | * mass-weighted average by block count. Phase is 'up' when the merged |
| 1073 | * vy is positive, otherwise 'down'. Overlapping members in a column |
| 1074 | * are packed so the higher one sits flush on the lower. |
| 1075 | */ |
| 1076 | GameCore.prototype.mergeFlyingClusters = function () |
| 1077 | { |
| 1078 | var flying = []; |
| 1079 | var ids = Object.keys(this.clusters); |
| 1080 | for (var n = 0; n < ids.length; n++) |
| 1081 | { |
| 1082 | var cluster = this.clusters[ids[n]]; |
| 1083 | if (!cluster) |
| 1084 | { continue; } |
| 1085 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1086 | { continue; } |
| 1087 | flying.push(cluster); |
| 1088 | } |
| 1089 | if (flying.length < 2) |
| 1090 | { return; } |
| 1091 | |
| 1092 | var uf = new UnionFind(); |
| 1093 | for (var i = 0; i < flying.length; i++) |
| 1094 | { uf.add(flying[i].id); } |
| 1095 | for (var i = 0; i < flying.length; i++) |
| 1096 | { |
| 1097 | for (var j = i + 1; j < flying.length; j++) |
| 1098 | { |
| 1099 | if (this.flyingClustersOverlap(flying[i], flying[j])) |
| 1100 | { uf.union(flying[i].id, flying[j].id); } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | var groups = Object.create(null); |
| 1105 | for (var i = 0; i < flying.length; i++) |
| 1106 | { |
| 1107 | var root = uf.find(flying[i].id); |
| 1108 | if (!groups[root]) |
| 1109 | { groups[root] = []; } |
| 1110 | groups[root].push(flying[i]); |
| 1111 | } |
| 1112 | for (var root in groups) |
| 1113 | { |
| 1114 | if (groups[root].length >= 2) |
| 1115 | { this.mergeClusterGroup(groups[root]); } |
| 1116 | } |
| 1117 | }; |
| 1118 | |
| 1119 | GameCore.prototype.flyingClustersOverlap = function (a, b) |
| 1120 | { |
| 1121 | var ma = this.clusterMembers(a.id); |
| 1122 | var mb = this.clusterMembers(b.id); |
| 1123 | for (var i = 0; i < ma.length; i++) |
| 1124 | { |
| 1125 | for (var j = 0; j < mb.length; j++) |
| 1126 | { |
| 1127 | if (ma[i].col !== mb[j].col) |
| 1128 | { continue; } |
| 1129 | if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9) |
| 1130 | { return true; } |
| 1131 | } |
| 1132 | } |
| 1133 | return false; |
| 1134 | }; |
| 1135 | |
| 1136 | GameCore.prototype.mergeClusterGroup = function (clusters) |
| 1137 | { |
| 1138 | clusters.sort(function (a, b) { return a.id - b.id; }); |
| 1139 | var keep = clusters[0]; |
| 1140 | var n = 0; |
| 1141 | var vySum = 0; |
| 1142 | var decelSum = 0; |
| 1143 | var hSum = 0; |
| 1144 | for (var c = 0; c < clusters.length; c++) |
| 1145 | { |
| 1146 | var mem = this.clusterMembers(clusters[c].id); |
| 1147 | var m = mem.length; |
| 1148 | n += m; |
| 1149 | vySum += clusters[c].vy * m; |
| 1150 | decelSum += (clusters[c].decel || 0) * m; |
| 1151 | hSum += (clusters[c].jumpHeight || 0) * m; |
| 1152 | } |
| 1153 | if (n === 0) |
| 1154 | { return; } |
| 1155 | keep.vy = vySum / n; |
| 1156 | keep.decel = decelSum / n; |
| 1157 | keep.jumpHeight = hSum / n; |
| 1158 | keep.phase = keep.vy > 1e-10 ? 'up' : 'down'; |
| 1159 | for (var c = 0; c < clusters.length; c++) |
| 1160 | { |
| 1161 | var mem = this.clusterMembers(clusters[c].id); |
| 1162 | for (var i = 0; i < mem.length; i++) |
| 1163 | { |
| 1164 | mem[i].clusterId = keep.id; |
| 1165 | mem[i].vy = keep.vy; |
| 1166 | mem[i].state = STATE_FLYING; |
| 1167 | } |
| 1168 | if (clusters[c] !== keep) |
| 1169 | { delete this.clusters[clusters[c].id]; } |
| 1170 | } |
| 1171 | this.packClusterOverlaps(keep); |
| 1172 | if (keep.phase === 'down') |
| 1173 | { this.tryLandCluster(keep, this.clusterMembers(keep.id)); } |
| 1174 | }; |
| 1175 | |
| 1176 | GameCore.prototype.packClusterOverlaps = function (cluster) |
| 1177 | { |
| 1178 | var members = this.clusterMembers(cluster.id); |
| 1179 | var byCol = Object.create(null); |
| 1180 | for (var i = 0; i < members.length; i++) |
| 1181 | { |
| 1182 | var col = members[i].col; |
| 1183 | if (!byCol[col]) |
| 1184 | { byCol[col] = []; } |
| 1185 | byCol[col].push(members[i]); |
| 1186 | } |
| 1187 | var cols = Object.keys(byCol); |
| 1188 | for (var c = 0; c < cols.length; c++) |
| 1189 | { |
| 1190 | var list = byCol[cols[c]]; |
| 1191 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1192 | for (var i = 1; i < list.length; i++) |
| 1193 | { |
| 1194 | var minY = list[i - 1].y + 1; |
| 1195 | if (list[i].y < minY - 1e-12) |
| 1196 | { list[i].y = minY; } |
| 1197 | } |
| 1198 | } |
| 1199 | }; |
| 1200 | |
| 1201 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 1202 | { |
| 1203 | if (!members.length) |
| 1204 | { return; } |
| 1205 | var shift = 0; |
| 1206 | var hit = false; |
| 1207 | for (var i = 0; i < members.length; i++) |
| 1208 | { |
| 1209 | var block = members[i]; |
| 1210 | var top = this.stackTop(block.col, function (other) |
| 1211 | { |
| 1212 | return other.clusterId === cluster.id; |
| 1213 | }); |
| 1214 | if (block.y <= top + 1e-9) |
| 1215 | { |
| 1216 | hit = true; |
| 1217 | var need = top - block.y; |
| 1218 | if (need > shift) |
| 1219 | { shift = need; } |
| 1220 | } |
| 1221 | } |
| 1222 | if (!hit) |
| 1223 | { return; } |
| 1224 | // Lift the whole rigid body so the deepest contact sits flush. |
| 1225 | for (var i = 0; i < members.length; i++) |
| 1226 | { members[i].y += shift; } |
| 1227 | this.landCluster(cluster, members); |
| 1228 | }; |
| 1229 | |
| 1230 | /** |
| 1231 | * Land as a rigid group: relative positions are kept, so part of the |
| 1232 | * cluster may hang above a gap. Grey members start the 3s settle timer; |
| 1233 | * coloured cargo stays put with them until every grey has recolored. |
| 1234 | * Members that actually rest on the floor or on another block bounce. |
| 1235 | */ |
| 1236 | GameCore.prototype.landCluster = function (cluster, members) |
| 1237 | { |
| 1238 | for (var i = 0; i < members.length; i++) |
| 1239 | { |
| 1240 | var block = members[i]; |
| 1241 | block.vy = 0; |
| 1242 | block.flame = false; |
| 1243 | block.clusterId = cluster.id; |
| 1244 | if (block.color === COLOR_GRAY) |
| 1245 | { |
| 1246 | block.state = STATE_SETTLING; |
| 1247 | block.settleTime = 0; |
| 1248 | } |
| 1249 | else |
| 1250 | { |
| 1251 | // Cargo stays in the rigid group (not matchable) until release. |
| 1252 | block.state = STATE_SETTLING; |
| 1253 | block.settleTime = 0; |
| 1254 | } |
| 1255 | if (this.hasSupport(block)) |
| 1256 | { this.startBounce(block); } |
| 1257 | } |
| 1258 | cluster.phase = 'landed'; |
| 1259 | cluster.vy = 0; |
| 1260 | }; |
| 1261 | |
| 1262 | /** |
| 1263 | * After every grey in a landed cluster has recolored, let the group go. |
| 1264 | * Unsupported blocks fall individually from rest; supported ones stay. |
| 1265 | */ |
| 1266 | GameCore.prototype.releaseLandedCluster = function (cluster) |
| 1267 | { |
| 1268 | var members = this.clusterMembers(cluster.id); |
| 1269 | var releasing = Object.create(null); |
| 1270 | for (var i = 0; i < members.length; i++) |
| 1271 | { releasing[members[i].id] = true; } |
| 1272 | |
| 1273 | for (var i = 0; i < members.length; i++) |
| 1274 | { |
| 1275 | var block = members[i]; |
| 1276 | if (block.color === COLOR_GRAY) |
| 1277 | { |
| 1278 | block.color = this.randomColor(); |
| 1279 | block.settleTime = 0; |
| 1280 | } |
| 1281 | this.leaveCluster(block); |
| 1282 | // Always drop leftover launch / manual velocity. A stale upward |
| 1283 | // vy here is what sends blocks off the top "very fast" after |
| 1284 | // a grey group recolors. |
| 1285 | this.clearFlightState(block); |
| 1286 | // Support from outside the group (floor or a foreign block). Fellow |
| 1287 | // members that will also fall do not count, so a hanging stack |
| 1288 | // drops together without the top one staying put. |
| 1289 | var supported = this.hasSupportOutside(block, releasing); |
| 1290 | block.state = supported ? STATE_STILL : STATE_FALLING; |
| 1291 | } |
| 1292 | delete this.clusters[cluster.id]; |
| 1293 | }; |
| 1294 | |
| 1295 | GameCore.prototype.hasSupportOutside = function (block, ignoreIds) |
| 1296 | { |
| 1297 | if (block.y <= 1e-9) |
| 1298 | { return true; } |
| 1299 | for (var i = 0; i < this.blocks.length; i++) |
| 1300 | { |
| 1301 | var other = this.blocks[i]; |
| 1302 | if (other === block || other.col !== block.col) |
| 1303 | { continue; } |
| 1304 | if (ignoreIds && ignoreIds[other.id]) |
| 1305 | { continue; } |
| 1306 | if (!this.isGrounded(other)) |
| 1307 | { continue; } |
| 1308 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 1309 | { return true; } |
| 1310 | } |
| 1311 | return false; |
| 1312 | }; |
| 1313 | |
| 1314 | GameCore.prototype.integrateManual = function (dt) |
| 1315 | { |
| 1316 | for (var i = 0; i < this.blocks.length; i++) |
| 1317 | { |
| 1318 | var block = this.blocks[i]; |
| 1319 | if (block.state !== STATE_MANUAL) |
| 1320 | { continue; } |
| 1321 | |
| 1322 | if (block.manualPhase === 'up') |
| 1323 | { |
| 1324 | var oldY = block.y; |
| 1325 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 1326 | var dy = this.advanceJump(proxy, dt); |
| 1327 | var nextY = oldY + dy; |
| 1328 | this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget)); |
| 1329 | block.vy = proxy.vy; |
| 1330 | block.manualPhase = proxy.phase; |
| 1331 | block.y = Math.min(nextY, block.manualTarget); |
| 1332 | if (block.y >= block.manualTarget - 1e-12) |
| 1333 | { |
| 1334 | block.y = block.manualTarget; |
| 1335 | block.vy = 0; |
| 1336 | block.manualPhase = 'down'; |
| 1337 | } |
| 1338 | if (block.y > this.height - 1) |
| 1339 | { |
| 1340 | block.y = this.height - 1; |
| 1341 | block.vy = 0; |
| 1342 | block.manualPhase = 'down'; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | if (block.manualPhase === 'down') |
| 1347 | { |
| 1348 | var proxy = { phase: 'down', vy: block.vy, decel: 0 }; |
| 1349 | var dy = this.advanceJump(proxy, dt); |
| 1350 | block.vy = proxy.vy; |
| 1351 | var nextY = block.y + dy; |
| 1352 | var top = this.stackTop(block.col); |
| 1353 | if (nextY <= top) |
| 1354 | { |
| 1355 | block.y = top; |
| 1356 | block.state = STATE_STILL; |
| 1357 | block.vy = 0; |
| 1358 | block.manualPhase = null; |
| 1359 | block.flame = false; |
| 1360 | this.startBounce(block); |
| 1361 | } |
| 1362 | else |
| 1363 | { |
| 1364 | block.y = Math.min(nextY, this.height - 1); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | }; |
| 1369 | |
| 1370 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 1371 | { |
| 1372 | if (!manual.pushedClusters) |
| 1373 | { manual.pushedClusters = Object.create(null); } |
| 1374 | |
| 1375 | var manualTopOld = oldY + 1; |
| 1376 | var manualTopNew = nextY + 1; |
| 1377 | var low = Math.min(oldY, nextY); |
| 1378 | var high = Math.max(oldY, nextY) + 1; |
| 1379 | |
| 1380 | for (var i = 0; i < this.blocks.length; i++) |
| 1381 | { |
| 1382 | var other = this.blocks[i]; |
| 1383 | if (other === manual) |
| 1384 | { continue; } |
| 1385 | if (other.col !== manual.col) |
| 1386 | { continue; } |
| 1387 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 1388 | { continue; } |
| 1389 | if (manual.pushedClusters[other.clusterId]) |
| 1390 | { continue; } |
| 1391 | var otherBot = other.y; |
| 1392 | var otherTop = other.y + 1; |
| 1393 | var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9; |
| 1394 | var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9; |
| 1395 | if (overlapping || crossing) |
| 1396 | { |
| 1397 | if (this.pushClusterColumn(other.clusterId, manual.col)) |
| 1398 | { |
| 1399 | manual.pushedClusters[other.clusterId] = true; |
| 1400 | return; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | }; |
| 1405 | |
| 1406 | |
| 1407 | /** |
| 1408 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 1409 | * Other columns stay put (they remain linked). Then look for a new |
| 1410 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 1411 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 1412 | * with the same jump height from its current position. |
| 1413 | */ |
| 1414 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 1415 | { |
| 1416 | var cluster = this.clusters[clusterId]; |
| 1417 | if (!cluster) |
| 1418 | { return false; } |
| 1419 | var members = this.clusterMembers(clusterId); |
| 1420 | for (var i = 0; i < members.length; i++) |
| 1421 | { |
| 1422 | if (members[i].col === col) |
| 1423 | { members[i].y += 1; } |
| 1424 | } |
| 1425 | this.cullOffTop(); |
| 1426 | if (!this.clusters[clusterId]) |
| 1427 | { return true; } |
| 1428 | this.checkChainRelaunch(cluster); |
| 1429 | return true; |
| 1430 | }; |
| 1431 | |
| 1432 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 1433 | { |
| 1434 | if (!this.clusters[cluster.id]) |
| 1435 | { return; } |
| 1436 | var lines = this.findFlyingMatchLines(cluster.id); |
| 1437 | if (!lines.length) |
| 1438 | { return; } |
| 1439 | var newly = []; |
| 1440 | var seen = Object.create(null); |
| 1441 | for (var i = 0; i < lines.length; i++) |
| 1442 | { |
| 1443 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 1444 | { |
| 1445 | var block = lines[i].blocks[j]; |
| 1446 | if (seen[block.id]) |
| 1447 | { continue; } |
| 1448 | seen[block.id] = true; |
| 1449 | newly.push(block); |
| 1450 | } |
| 1451 | } |
| 1452 | for (var i = 0; i < newly.length; i++) |
| 1453 | { |
| 1454 | var block = newly[i]; |
| 1455 | block.color = COLOR_GRAY; |
| 1456 | block.flame = true; |
| 1457 | block.clusterId = cluster.id; |
| 1458 | block.state = STATE_FLYING; |
| 1459 | } |
| 1460 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 1461 | }; |
| 1462 | |
| 1463 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 1464 | { |
| 1465 | cluster.jumpHeight = height; |
| 1466 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1467 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 1468 | cluster.phase = 'up'; |
| 1469 | var members = this.clusterMembers(cluster.id); |
| 1470 | for (var i = 0; i < members.length; i++) |
| 1471 | { |
| 1472 | members[i].vy = cluster.vy; |
| 1473 | members[i].state = STATE_FLYING; |
| 1474 | if (members[i].color === COLOR_GRAY) |
| 1475 | { members[i].flame = true; } |
| 1476 | } |
| 1477 | }; |
| 1478 | |
| 1479 | GameCore.prototype.startManualLaunch = function (block) |
| 1480 | { |
| 1481 | var target = this.height - 1; |
| 1482 | var height = Math.max(0.05, target - block.y); |
| 1483 | block.state = STATE_MANUAL; |
| 1484 | block.pushedClusters = Object.create(null); |
| 1485 | block.manualTarget = target; |
| 1486 | block.manualPhase = 'up'; |
| 1487 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1488 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 1489 | block.flame = false; |
| 1490 | if (this.drag && this.drag.blockId === block.id) |
| 1491 | { this.drag = null; } |
| 1492 | }; |
| 1493 | |
| 1494 | GameCore.prototype.cullOffTop = function () |
| 1495 | { |
| 1496 | var gone = []; |
| 1497 | for (var i = 0; i < this.blocks.length; i++) |
| 1498 | { |
| 1499 | var block = this.blocks[i]; |
| 1500 | if (block.state === STATE_MANUAL) |
| 1501 | { |
| 1502 | if (block.y > this.height - 1) |
| 1503 | { block.y = this.height - 1; } |
| 1504 | continue; |
| 1505 | } |
| 1506 | if (block.y >= this.height - 1e-9) |
| 1507 | { gone.push(block); } |
| 1508 | } |
| 1509 | for (var i = 0; i < gone.length; i++) |
| 1510 | { |
| 1511 | this.score += 1; |
| 1512 | this.removeBlock(gone[i]); |
| 1513 | } |
| 1514 | }; |
| 1515 | |
| 1516 | GameCore.prototype.advanceSettle = function (dt) |
| 1517 | { |
| 1518 | for (var i = 0; i < this.blocks.length; i++) |
| 1519 | { |
| 1520 | var block = this.blocks[i]; |
| 1521 | if (block.state !== STATE_SETTLING) |
| 1522 | { continue; } |
| 1523 | var inLanded = block.clusterId != null |
| 1524 | && this.clusters[block.clusterId] |
| 1525 | && this.clusters[block.clusterId].phase === 'landed'; |
| 1526 | if (block.color === COLOR_GRAY) |
| 1527 | { |
| 1528 | block.settleTime += dt; |
| 1529 | if (block.settleTime >= this.settleTime) |
| 1530 | { |
| 1531 | block.color = this.randomColor(); |
| 1532 | block.settleTime = 0; |
| 1533 | block.flame = false; |
| 1534 | if (inLanded) |
| 1535 | { |
| 1536 | // Stay in the rigid group; releaseLandedCluster runs below. |
| 1537 | block.state = STATE_SETTLING; |
| 1538 | } |
| 1539 | else |
| 1540 | { |
| 1541 | this.leaveCluster(block); |
| 1542 | this.clearFlightState(block); |
| 1543 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1544 | } |
| 1545 | } |
| 1546 | } |
| 1547 | else if (!inLanded) |
| 1548 | { |
| 1549 | // Coloured settling that is not in a landed cluster (shouldn't |
| 1550 | // normally happen): just become still or fall, from rest. |
| 1551 | this.leaveCluster(block); |
| 1552 | this.clearFlightState(block); |
| 1553 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | var ids = Object.keys(this.clusters); |
| 1558 | for (var n = 0; n < ids.length; n++) |
| 1559 | { |
| 1560 | var cluster = this.clusters[ids[n]]; |
| 1561 | if (!cluster || cluster.phase !== 'landed') |
| 1562 | { continue; } |
| 1563 | var members = this.clusterMembers(cluster.id); |
| 1564 | var greySettling = false; |
| 1565 | for (var i = 0; i < members.length; i++) |
| 1566 | { |
| 1567 | if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING) |
| 1568 | { greySettling = true; } |
| 1569 | } |
| 1570 | if (!greySettling) |
| 1571 | { this.releaseLandedCluster(cluster); } |
| 1572 | } |
| 1573 | }; |
| 1574 | |
| 1575 | GameCore.prototype.findMatchLines = function () |
| 1576 | { |
| 1577 | var candidates = []; |
| 1578 | for (var i = 0; i < this.blocks.length; i++) |
| 1579 | { |
| 1580 | var block = this.blocks[i]; |
| 1581 | if (block.state === STATE_STILL && block.color >= 0) |
| 1582 | { candidates.push(block); } |
| 1583 | } |
| 1584 | return this.collectLines(candidates, true); |
| 1585 | }; |
| 1586 | |
| 1587 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 1588 | { |
| 1589 | var candidates = []; |
| 1590 | for (var i = 0; i < this.blocks.length; i++) |
| 1591 | { |
| 1592 | var block = this.blocks[i]; |
| 1593 | if (block.clusterId === clusterId && block.color >= 0 |
| 1594 | && (block.state === STATE_FLYING)) |
| 1595 | { candidates.push(block); } |
| 1596 | } |
| 1597 | return this.collectLines(candidates, false); |
| 1598 | }; |
| 1599 | |
| 1600 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 1601 | { |
| 1602 | var lines = []; |
| 1603 | if (!candidates.length) |
| 1604 | { return lines; } |
| 1605 | |
| 1606 | var byRow = Object.create(null); |
| 1607 | for (var i = 0; i < candidates.length; i++) |
| 1608 | { |
| 1609 | var block = candidates[i]; |
| 1610 | var row = Math.round(block.y); |
| 1611 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 1612 | { continue; } |
| 1613 | if (!byRow[row]) |
| 1614 | { byRow[row] = []; } |
| 1615 | byRow[row].push(block); |
| 1616 | } |
| 1617 | var rows = Object.keys(byRow); |
| 1618 | for (var r = 0; r < rows.length; r++) |
| 1619 | { |
| 1620 | var list = byRow[rows[r]]; |
| 1621 | list.sort(function (a, b) { return a.col - b.col; }); |
| 1622 | this.pushRuns(lines, list, false, function (a, b) |
| 1623 | { |
| 1624 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 1625 | }); |
| 1626 | } |
| 1627 | |
| 1628 | var byCol = Object.create(null); |
| 1629 | for (var i = 0; i < candidates.length; i++) |
| 1630 | { |
| 1631 | var block = candidates[i]; |
| 1632 | if (!byCol[block.col]) |
| 1633 | { byCol[block.col] = []; } |
| 1634 | byCol[block.col].push(block); |
| 1635 | } |
| 1636 | var cols = Object.keys(byCol); |
| 1637 | for (var c = 0; c < cols.length; c++) |
| 1638 | { |
| 1639 | var list = byCol[cols[c]]; |
| 1640 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1641 | this.pushRuns(lines, list, true, function (a, b) |
| 1642 | { |
| 1643 | return a.color === b.color && a.color >= 0 |
| 1644 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 1645 | }); |
| 1646 | } |
| 1647 | return lines; |
| 1648 | }; |
| 1649 | |
| 1650 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 1651 | { |
| 1652 | var start = 0; |
| 1653 | for (var i = 1; i <= list.length; i++) |
| 1654 | { |
| 1655 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 1656 | if (!same) |
| 1657 | { |
| 1658 | var length = i - start; |
| 1659 | if (length >= 3) |
| 1660 | { |
| 1661 | lines.push({ |
| 1662 | blocks: list.slice(start, i), |
| 1663 | length: length, |
| 1664 | vertical: vertical |
| 1665 | }); |
| 1666 | } |
| 1667 | start = i; |
| 1668 | } |
| 1669 | } |
| 1670 | }; |
| 1671 | |
| 1672 | GameCore.prototype.resolveMatches = function () |
| 1673 | { |
| 1674 | if (this.state !== 'playing' || this.drag) |
| 1675 | { return []; } |
| 1676 | var lines = this.findMatchLines(); |
| 1677 | if (!lines.length) |
| 1678 | { return []; } |
| 1679 | |
| 1680 | var uf = new UnionFind(); |
| 1681 | var blockById = Object.create(null); |
| 1682 | for (var i = 0; i < lines.length; i++) |
| 1683 | { |
| 1684 | var line = lines[i]; |
| 1685 | for (var j = 0; j < line.blocks.length; j++) |
| 1686 | { |
| 1687 | uf.add(line.blocks[j].id); |
| 1688 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 1689 | if (j > 0) |
| 1690 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 1691 | } |
| 1692 | } |
| 1693 | var matched = []; |
| 1694 | var seen = Object.create(null); |
| 1695 | for (var id in blockById) |
| 1696 | { |
| 1697 | if (!seen[id]) |
| 1698 | { |
| 1699 | seen[id] = true; |
| 1700 | matched.push(blockById[id]); |
| 1701 | } |
| 1702 | } |
| 1703 | for (var i = 0; i < matched.length; i++) |
| 1704 | { |
| 1705 | for (var j = i + 1; j < matched.length; j++) |
| 1706 | { |
| 1707 | if (matched[i].col === matched[j].col) |
| 1708 | { uf.union(matched[i].id, matched[j].id); } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | var groups = Object.create(null); |
| 1713 | for (var i = 0; i < matched.length; i++) |
| 1714 | { |
| 1715 | var root = uf.find(matched[i].id); |
| 1716 | if (!groups[root]) |
| 1717 | { groups[root] = []; } |
| 1718 | groups[root].push(matched[i]); |
| 1719 | } |
| 1720 | |
| 1721 | var launched = []; |
| 1722 | for (var root in groups) |
| 1723 | { |
| 1724 | var group = groups[root]; |
| 1725 | var height = 0; |
| 1726 | for (var i = 0; i < lines.length; i++) |
| 1727 | { |
| 1728 | var line = lines[i]; |
| 1729 | var overlaps = false; |
| 1730 | for (var j = 0; j < line.blocks.length; j++) |
| 1731 | { |
| 1732 | for (var k = 0; k < group.length; k++) |
| 1733 | { |
| 1734 | if (line.blocks[j].id === group[k].id) |
| 1735 | { overlaps = true; } |
| 1736 | } |
| 1737 | } |
| 1738 | if (overlaps) |
| 1739 | { |
| 1740 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 1741 | if (h > height) |
| 1742 | { height = h; } |
| 1743 | } |
| 1744 | } |
| 1745 | this.launchCluster(group, height); |
| 1746 | launched.push({ blocks: group, jumpHeight: height }); |
| 1747 | } |
| 1748 | return launched; |
| 1749 | }; |
| 1750 | |
| 1751 | /** |
| 1752 | * Gray the matched blocks, take everything sitting on top of them in |
| 1753 | * those columns, link them, and launch with jump height `height`. |
| 1754 | */ |
| 1755 | GameCore.prototype.launchCluster = function (matchedBlocks, height) |
| 1756 | { |
| 1757 | var lowestInCol = Object.create(null); |
| 1758 | var matchedIds = Object.create(null); |
| 1759 | for (var i = 0; i < matchedBlocks.length; i++) |
| 1760 | { |
| 1761 | var block = matchedBlocks[i]; |
| 1762 | matchedIds[block.id] = true; |
| 1763 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 1764 | { lowestInCol[block.col] = block.y; } |
| 1765 | } |
| 1766 | |
| 1767 | var clusterId = this.nextClusterId++; |
| 1768 | var cluster = { |
| 1769 | id: clusterId, |
| 1770 | jumpHeight: height, |
| 1771 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 1772 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 1773 | phase: 'up' |
| 1774 | }; |
| 1775 | this.clusters[clusterId] = cluster; |
| 1776 | |
| 1777 | var riders = []; |
| 1778 | var cols = Object.keys(lowestInCol); |
| 1779 | for (var c = 0; c < cols.length; c++) |
| 1780 | { |
| 1781 | var col = +cols[c]; |
| 1782 | var lowest = lowestInCol[col]; |
| 1783 | var list = this.blocksInColumn(col); |
| 1784 | for (var i = 0; i < list.length; i++) |
| 1785 | { |
| 1786 | var block = list[i]; |
| 1787 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 1788 | { |
| 1789 | if (block.state !== STATE_SETTLING) |
| 1790 | { continue; } |
| 1791 | } |
| 1792 | if (block.y + ALIGN_EPS >= lowest) |
| 1793 | { riders.push(block); } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | for (var i = 0; i < riders.length; i++) |
| 1798 | { |
| 1799 | var block = riders[i]; |
| 1800 | var isMatch = !!matchedIds[block.id]; |
| 1801 | if (isMatch || block.color === COLOR_GRAY) |
| 1802 | { |
| 1803 | block.color = COLOR_GRAY; |
| 1804 | block.flame = true; |
| 1805 | } |
| 1806 | else |
| 1807 | { block.flame = false; } |
| 1808 | block.state = STATE_FLYING; |
| 1809 | block.clusterId = clusterId; |
| 1810 | block.vy = cluster.vy; |
| 1811 | block.settleTime = 0; |
| 1812 | } |
| 1813 | return cluster; |
| 1814 | }; |
| 1815 | |
| 1816 | GameCore.prototype.checkLocks = function () |
| 1817 | { |
| 1818 | for (var col = 0; col < this.width; col++) |
| 1819 | { |
| 1820 | if (this.isColumnLocked(col)) |
| 1821 | { continue; } |
| 1822 | if (this.groundedCount(col) < this.height) |
| 1823 | { continue; } |
| 1824 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 1825 | var filled = true; |
| 1826 | for (var row = 0; row < this.height; row++) |
| 1827 | { |
| 1828 | var block = this.blockAt(col, row); |
| 1829 | if (!block || !this.isGrounded(block)) |
| 1830 | { |
| 1831 | filled = false; |
| 1832 | break; |
| 1833 | } |
| 1834 | } |
| 1835 | if (!filled) |
| 1836 | { continue; } |
| 1837 | var list = this.blocksInColumn(col); |
| 1838 | for (var i = 0; i < list.length; i++) |
| 1839 | { |
| 1840 | var block = list[i]; |
| 1841 | if (!this.isGrounded(block)) |
| 1842 | { continue; } |
| 1843 | block.state = STATE_LOCKED; |
| 1844 | block.color = COLOR_GRAY; |
| 1845 | block.flame = false; |
| 1846 | block.vy = 0; |
| 1847 | block.settleTime = 0; |
| 1848 | if (block.clusterId != null) |
| 1849 | { this.leaveCluster(block); } |
| 1850 | } |
| 1851 | } |
| 1852 | }; |
| 1853 | |
| 1854 | GameCore.prototype.checkGameOver = function () |
| 1855 | { |
| 1856 | if (this.state !== 'playing') |
| 1857 | { return; } |
| 1858 | for (var col = 0; col < this.width; col++) |
| 1859 | { |
| 1860 | if (!this.isColumnLocked(col)) |
| 1861 | { return; } |
| 1862 | } |
| 1863 | this.state = 'gameover'; |
| 1864 | this.gameOverTime = 0; |
| 1865 | this.drag = null; |
| 1866 | }; |
| 1867 | |
| 1868 | /* ---------- dragging -------------------------------------------------- */ |
| 1869 | |
| 1870 | GameCore.prototype.beginDrag = function (col, y) |
| 1871 | { |
| 1872 | if (this.state !== 'playing') |
| 1873 | { return false; } |
| 1874 | var block = this.snapDragBlock(col, y); |
| 1875 | if (!block) |
| 1876 | { return false; } |
| 1877 | var top = this.columnStackTopRow(block.col); |
| 1878 | var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 1879 | this.drag = { |
| 1880 | blockId: block.id, |
| 1881 | pointerY: y, |
| 1882 | startY: y, |
| 1883 | accum: 0, |
| 1884 | launchIntent: false, |
| 1885 | startedAsTop: startedAsTop |
| 1886 | }; |
| 1887 | return true; |
| 1888 | }; |
| 1889 | |
| 1890 | /** |
| 1891 | * Instant grab: the nearest still, coloured block to (col, y), allowing |
| 1892 | * a small vertical miss and a horizontal slop into a neighbouring |
| 1893 | * column. `col` may be fractional (pointer x in tiles). No hold delay. |
| 1894 | */ |
| 1895 | GameCore.prototype.snapDragBlock = function (col, y) |
| 1896 | { |
| 1897 | var raw = col; |
| 1898 | var c0 = Math.floor(raw + 1e-9); |
| 1899 | var frac = raw - c0; |
| 1900 | var candidates = []; |
| 1901 | if (c0 >= 0 && c0 < this.width) |
| 1902 | { candidates.push(c0); } |
| 1903 | // Only consult a neighbour when the pointer is actually near the edge |
| 1904 | // (a whole-number col, as in unit tests, stays in that column). |
| 1905 | if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0) |
| 1906 | { candidates.push(c0 - 1); } |
| 1907 | if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width) |
| 1908 | { candidates.push(c0 + 1); } |
| 1909 | |
| 1910 | var best = null; |
| 1911 | var bestDist = Infinity; |
| 1912 | for (var c = 0; c < candidates.length; c++) |
| 1913 | { |
| 1914 | var cc = candidates[c]; |
| 1915 | if (!this.isColumnStill(cc)) |
| 1916 | { continue; } |
| 1917 | var list = this.blocksInColumn(cc); |
| 1918 | for (var i = 0; i < list.length; i++) |
| 1919 | { |
| 1920 | var block = list[i]; |
| 1921 | if (block.state !== STATE_STILL || block.color < 0) |
| 1922 | { continue; } |
| 1923 | var dist = this.pointerDistToBlock(block, raw, y); |
| 1924 | // Prefer the column the pointer is actually in on a tie. |
| 1925 | if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0)) |
| 1926 | { |
| 1927 | bestDist = dist; |
| 1928 | best = block; |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | if (!best || bestDist > SNAP_Y) |
| 1933 | { return null; } |
| 1934 | return best; |
| 1935 | }; |
| 1936 | |
| 1937 | GameCore.prototype.pointerDistToBlock = function (block, col, y) |
| 1938 | { |
| 1939 | var dx; |
| 1940 | if (col >= block.col && col < block.col + 1) |
| 1941 | { dx = 0; } |
| 1942 | else if (col < block.col) |
| 1943 | { dx = block.col - col; } |
| 1944 | else |
| 1945 | { dx = col - (block.col + 1); } |
| 1946 | var dy; |
| 1947 | if (y >= block.y && y < block.y + 1) |
| 1948 | { dy = 0; } |
| 1949 | else if (y < block.y) |
| 1950 | { dy = block.y - y; } |
| 1951 | else |
| 1952 | { dy = y - (block.y + 1); } |
| 1953 | return Math.sqrt(dx * dx + dy * dy); |
| 1954 | }; |
| 1955 | |
| 1956 | GameCore.prototype.moveDrag = function (y) |
| 1957 | { |
| 1958 | if (!this.drag) |
| 1959 | { return; } |
| 1960 | var block = this.blockById(this.drag.blockId); |
| 1961 | if (!block || block.state !== STATE_STILL) |
| 1962 | { |
| 1963 | this.drag = null; |
| 1964 | return; |
| 1965 | } |
| 1966 | if (!this.isColumnStill(block.col)) |
| 1967 | { |
| 1968 | this.drag = null; |
| 1969 | return; |
| 1970 | } |
| 1971 | var dy = y - this.drag.pointerY; |
| 1972 | this.drag.pointerY = y; |
| 1973 | if (this.drag.accum == null) |
| 1974 | { this.drag.accum = 0; } |
| 1975 | this.drag.accum += dy; |
| 1976 | // One swap per tile of travel, triggered after a minimal SWAP_DISTANCE. |
| 1977 | // A single large move (fast swipe) walks every tile; none are skipped. |
| 1978 | var guard = 0; |
| 1979 | while (guard++ < this.height + 2 |
| 1980 | && (this.drag.accum >= SWAP_DISTANCE || this.drag.accum <= -SWAP_DISTANCE)) |
| 1981 | { |
| 1982 | var dir = this.drag.accum > 0 ? 1 : -1; |
| 1983 | if (!this.swapOne(block, dir)) |
| 1984 | { |
| 1985 | if (dir > 0) |
| 1986 | { this.drag.accum = Math.min(this.drag.accum, SWAP_DISTANCE - 1e-9); } |
| 1987 | else |
| 1988 | { this.drag.accum = Math.max(this.drag.accum, -(SWAP_DISTANCE - 1e-9)); } |
| 1989 | break; |
| 1990 | } |
| 1991 | this.drag.accum -= dir; |
| 1992 | // A short move (SWAP_DISTANCE < 1) leaves a negative leftover; |
| 1993 | // do not let that immediately swap back the other way. |
| 1994 | if ((dir > 0 && this.drag.accum < 0) || (dir < 0 && this.drag.accum > 0)) |
| 1995 | { this.drag.accum = 0; } |
| 1996 | if (!this.isColumnStill(block.col)) |
| 1997 | { |
| 1998 | this.drag = null; |
| 1999 | return; |
| 2000 | } |
| 2001 | } |
| 2002 | var top = this.columnStackTopRow(block.col); |
| 2003 | var isTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 2004 | if (this.drag.startedAsTop) |
| 2005 | { |
| 2006 | // Already the topmost block: pulling upward past its own position |
| 2007 | // (LAUNCH_PULL tiles above the press point) starts a manual jump. |
| 2008 | this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL); |
| 2009 | } |
| 2010 | else |
| 2011 | { |
| 2012 | // Started mid-stack: swap first, and only launch once the pointer |
| 2013 | // is pulled into the empty space above the (new) top. |
| 2014 | this.drag.launchIntent = isTop && (y > top + 1 + 0.25); |
| 2015 | } |
| 2016 | }; |
| 2017 | |
| 2018 | /** Swap `block` one tile up (dir=+1) or down (dir=-1). Returns true if swapped. */ |
| 2019 | GameCore.prototype.swapOne = function (block, dir) |
| 2020 | { |
| 2021 | var neighbor = this.blockAt(block.col, block.y + dir); |
| 2022 | if (neighbor && this.isGrounded(neighbor) && neighbor !== block) |
| 2023 | { |
| 2024 | this.swapBlocks(block, neighbor); |
| 2025 | return true; |
| 2026 | } |
| 2027 | return false; |
| 2028 | }; |
| 2029 | |
| 2030 | GameCore.prototype.endDrag = function () |
| 2031 | { |
| 2032 | if (!this.drag) |
| 2033 | { return; } |
| 2034 | var block = this.blockById(this.drag.blockId); |
| 2035 | var launch = this.drag.launchIntent; |
| 2036 | this.drag = null; |
| 2037 | if (launch && block && block.state === STATE_STILL) |
| 2038 | { this.startManualLaunch(block); } |
| 2039 | else |
| 2040 | { this.resolveMatches(); } |
| 2041 | }; |
| 2042 | |
| 2043 | GameCore.prototype.blockById = function (id) |
| 2044 | { |
| 2045 | for (var i = 0; i < this.blocks.length; i++) |
| 2046 | { |
| 2047 | if (this.blocks[i].id === id) |
| 2048 | { return this.blocks[i]; } |
| 2049 | } |
| 2050 | return null; |
| 2051 | }; |
| 2052 | |
| 2053 | GameCore.prototype.columnStackTopRow = function (col) |
| 2054 | { |
| 2055 | var top = -1; |
| 2056 | var list = this.blocksInColumn(col); |
| 2057 | for (var i = 0; i < list.length; i++) |
| 2058 | { |
| 2059 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 2060 | { top = list[i].y; } |
| 2061 | } |
| 2062 | return top; |
| 2063 | }; |
| 2064 | |
| 2065 | GameCore.prototype.swapToward = function (block, targetY) |
| 2066 | { |
| 2067 | var guard = 0; |
| 2068 | while (guard++ < this.height + 2) |
| 2069 | { |
| 2070 | var current = block.y; |
| 2071 | if (targetY > current + 0.5) |
| 2072 | { |
| 2073 | var above = this.blockAt(block.col, current + 1); |
| 2074 | if (above && this.isGrounded(above) && above !== block) |
| 2075 | { this.swapBlocks(block, above); } |
| 2076 | else |
| 2077 | { break; } |
| 2078 | } |
| 2079 | else if (targetY < current - 0.5) |
| 2080 | { |
| 2081 | var below = this.blockAt(block.col, current - 1); |
| 2082 | if (below && this.isGrounded(below) && below !== block) |
| 2083 | { this.swapBlocks(block, below); } |
| 2084 | else |
| 2085 | { break; } |
| 2086 | } |
| 2087 | else |
| 2088 | { break; } |
| 2089 | } |
| 2090 | }; |
| 2091 | |
| 2092 | GameCore.prototype.swapBlocks = function (a, b) |
| 2093 | { |
| 2094 | var y = a.y; |
| 2095 | a.y = b.y; |
| 2096 | b.y = y; |
| 2097 | }; |
| 2098 | |
| 2099 | /** |
| 2100 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 2101 | * Returns the moved block, or null if the column is not still. |
| 2102 | */ |
| 2103 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 2104 | { |
| 2105 | if (!this.isColumnStill(col)) |
| 2106 | { return null; } |
| 2107 | var block = this.blockAt(col, fromRow); |
| 2108 | if (!block) |
| 2109 | { return null; } |
| 2110 | this.swapToward(block, toRow); |
| 2111 | return block; |
| 2112 | }; |
| 2113 | |
| 2114 | /** |
| 2115 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 2116 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 2117 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 2118 | * their color with a marker in `flying`. |
| 2119 | */ |
| 2120 | GameCore.prototype.snapshot = function () |
| 2121 | { |
| 2122 | var grid = []; |
| 2123 | for (var r = 0; r < this.height; r++) |
| 2124 | { |
| 2125 | var row = []; |
| 2126 | for (var c = 0; c < this.width; c++) |
| 2127 | { row.push('.'); } |
| 2128 | grid.push(row); |
| 2129 | } |
| 2130 | var airborne = []; |
| 2131 | for (var i = 0; i < this.blocks.length; i++) |
| 2132 | { |
| 2133 | var block = this.blocks[i]; |
| 2134 | var ch; |
| 2135 | if (block.state === STATE_LOCKED) |
| 2136 | { ch = 'L'; } |
| 2137 | else if (block.color === COLOR_GRAY) |
| 2138 | { ch = 'g'; } |
| 2139 | else |
| 2140 | { ch = String(block.color); } |
| 2141 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 2142 | { |
| 2143 | var row = Math.round(block.y); |
| 2144 | if (row >= 0 && row < this.height) |
| 2145 | { grid[row][block.col] = ch; } |
| 2146 | } |
| 2147 | else |
| 2148 | { |
| 2149 | airborne.push({ |
| 2150 | id: block.id, |
| 2151 | col: block.col, |
| 2152 | y: block.y, |
| 2153 | color: block.color, |
| 2154 | state: block.state, |
| 2155 | clusterId: block.clusterId, |
| 2156 | flame: block.flame, |
| 2157 | vy: block.vy |
| 2158 | }); |
| 2159 | } |
| 2160 | } |
| 2161 | var lines = []; |
| 2162 | for (var r = this.height - 1; r >= 0; r--) |
| 2163 | { lines.push(grid[r].join('')); } |
| 2164 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 2165 | }; |
| 2166 | |
| 2167 | var GameCoreAPI = { |
| 2168 | GameCore: GameCore, |
| 2169 | COLOR_GRAY: COLOR_GRAY, |
| 2170 | STATE_STILL: STATE_STILL, |
| 2171 | STATE_FALLING: STATE_FALLING, |
| 2172 | STATE_FLYING: STATE_FLYING, |
| 2173 | STATE_SETTLING: STATE_SETTLING, |
| 2174 | STATE_LOCKED: STATE_LOCKED, |
| 2175 | STATE_MANUAL: STATE_MANUAL |
| 2176 | }; |
| 2177 | |
| 2178 | if (typeof module !== 'undefined' && module.exports) |
| 2179 | { module.exports = GameCoreAPI; } |
| 2180 | else if (typeof globalThis !== 'undefined') |
| 2181 | { |
| 2182 | globalThis.GameCore = GameCore; |
| 2183 | globalThis.GameCoreAPI = GameCoreAPI; |
| 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * End File: |
| 2188 | * /workspace/meteors/src/gameCore.js |
| 2189 | */ |
| 2190 | |
| 2191 | /* |
| 2192 | * Start File: |
| 2193 | * /workspace/meteors/src/main.js |
| 2194 | */ |
| 2195 | 'use strict'; |
| 2196 | |
| 2197 | /** |
| 2198 | * View and input for Meteors. The simulation lives in GameCore; this file |
| 2199 | * only draws it, scales it to the viewport, and translates pointer events |
| 2200 | * into beginDrag / moveDrag / endDrag. Ready for a later multiplayer view |
| 2201 | * that reuses the same core. |
| 2202 | */ |
| 2203 | |
| 2204 | var IMAGES = [ |
| 2205 | { name: 'ember', url: 'images/block-ember.png' }, |
| 2206 | { name: 'aqua', url: 'images/block-aqua.png' }, |
| 2207 | { name: 'leaf', url: 'images/block-leaf.png' }, |
| 2208 | { name: 'sun', url: 'images/block-sun.png' }, |
| 2209 | { name: 'orchid', url: 'images/block-orchid.png' }, |
| 2210 | { name: 'gray', url: 'images/block-gray.png' }, |
| 2211 | { name: 'locked', url: 'images/block-locked.png' }, |
| 2212 | { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, |
| 2213 | { name: 'cell', url: 'images/cell.png' }, |
| 2214 | { name: 'pixel', url: 'images/pixel.png' } |
| 2215 | ]; |
| 2216 | |
| 2217 | var COLOR_SPRITES = ['ember', 'aqua', 'leaf', 'sun', 'orchid']; |
| 2218 | var GAME_OVER_DELAY = 2; |
| 2219 | |
| 2220 | var game = new Game(); |
| 2221 | var app = { |
| 2222 | mode: 'menu', |
| 2223 | core: null, |
| 2224 | scene: null, |
| 2225 | manager: null, |
| 2226 | planet: null, |
| 2227 | lastScore: 0, |
| 2228 | tile: 48, |
| 2229 | fieldX: 0, |
| 2230 | fieldY: 0, |
| 2231 | fieldW: 0, |
| 2232 | fieldH: 0, |
| 2233 | hudH: 56, |
| 2234 | sprites: Object.create(null), |
| 2235 | flames: Object.create(null), |
| 2236 | cells: [], |
| 2237 | pointerId: null, |
| 2238 | gameOverShown: false, |
| 2239 | selectTween: null |
| 2240 | }; |
| 2241 | |
| 2242 | window.onload = function () |
| 2243 | { |
| 2244 | var play = document.getElementById('btn-play'); |
| 2245 | play.addEventListener('click', onPlay); |
| 2246 | play.addEventListener('pointerup', function (e) |
| 2247 | { |
| 2248 | // A touch-friendly path: some mobile browsers swallow the click |
| 2249 | // after a slightly sloppy tap. Ignore the following click. |
| 2250 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 2251 | { |
| 2252 | e.preventDefault(); |
| 2253 | onPlay(); |
| 2254 | } |
| 2255 | }); |
| 2256 | |
| 2257 | game.preloadImagesThenStart(IMAGES, function () |
| 2258 | { |
| 2259 | if (game.images.flame && game.images.flame.addAnimationCycle) |
| 2260 | { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } |
| 2261 | }); |
| 2262 | }; |
| 2263 | |
| 2264 | function onPlay () |
| 2265 | { |
| 2266 | if (app.mode === 'playing') |
| 2267 | { return; } |
| 2268 | startPlay(); |
| 2269 | } |
| 2270 | |
| 2271 | function startPlay () |
| 2272 | { |
| 2273 | hideEl('menu'); |
| 2274 | hideEl('game-over'); |
| 2275 | showEl('hud'); |
| 2276 | |
| 2277 | app.planet = (typeof Planet !== 'undefined' ? Planet.defaultPlanet() : PlanetMod.defaultPlanet()); |
| 2278 | app.core = new GameCore(app.planet, Math.random); |
| 2279 | app.mode = 'playing'; |
| 2280 | app.gameOverShown = false; |
| 2281 | app.sprites = Object.create(null); |
| 2282 | app.flames = Object.create(null); |
| 2283 | app.cells = []; |
| 2284 | app.pointerId = null; |
| 2285 | app.selectTween = null; |
| 2286 | |
| 2287 | layout(); |
| 2288 | app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); |
| 2289 | app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; |
| 2290 | app.scene.renderBackground = true; |
| 2291 | game.loadScene(app.scene); |
| 2292 | |
| 2293 | buildCells(); |
| 2294 | app.manager = app.scene.addObject(makeManager()); |
| 2295 | bindPointer(app.scene.canvas); |
| 2296 | refreshHud(); |
| 2297 | } |
| 2298 | |
| 2299 | function returnToMenu () |
| 2300 | { |
| 2301 | unbindPointer(); |
| 2302 | if (app.scene) |
| 2303 | { |
| 2304 | app.scene.destroy(); |
| 2305 | app.scene = null; |
| 2306 | } |
| 2307 | app.manager = null; |
| 2308 | app.core = null; |
| 2309 | app.mode = 'menu'; |
| 2310 | app.sprites = Object.create(null); |
| 2311 | app.flames = Object.create(null); |
| 2312 | app.cells = []; |
| 2313 | app.selectTween = null; |
| 2314 | |
| 2315 | hideEl('hud'); |
| 2316 | hideEl('game-over'); |
| 2317 | showEl('menu'); |
| 2318 | var last = document.getElementById('last-score'); |
| 2319 | if (app.lastScore > 0) |
| 2320 | { |
| 2321 | last.hidden = false; |
| 2322 | last.querySelector('strong').textContent = String(app.lastScore); |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | function buildCells () |
| 2327 | { |
| 2328 | if (!app.scene) |
| 2329 | { return; } |
| 2330 | var img = game.images.cell; |
| 2331 | for (var row = 0; row < 13; row++) |
| 2332 | { |
| 2333 | for (var col = 0; col < 10; col++) |
| 2334 | { |
| 2335 | var obj = new GameObject(0, 0, img, 0); |
| 2336 | placeTile(obj, col, row); |
| 2337 | obj.renderer.alpha = 0.9; |
| 2338 | app.scene.addObject(obj); |
| 2339 | app.cells.push(obj); |
| 2340 | } |
| 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | function placeTile (obj, col, y) |
| 2345 | { |
| 2346 | var t = app.tile; |
| 2347 | obj.transform.position.x = col * t + t / 2; |
| 2348 | obj.transform.position.y = app.fieldH - (y + 0.5) * t; |
| 2349 | obj.transform.size = { x: t, y: t }; |
| 2350 | } |
| 2351 | |
| 2352 | function layout () |
| 2353 | { |
| 2354 | var vw = window.innerWidth || 320; |
| 2355 | var vh = window.innerHeight || 480; |
| 2356 | var safeTop = 0; |
| 2357 | try |
| 2358 | { |
| 2359 | // env(safe-area) is a CSS thing; keep a conservative HUD height. |
| 2360 | safeTop = 0; |
| 2361 | } |
| 2362 | catch (e) { /* ignore */ } |
| 2363 | app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); |
| 2364 | var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); |
| 2365 | var availW = Math.max(80, vw - margin * 2); |
| 2366 | var availH = Math.max(80, vh - app.hudH - margin); |
| 2367 | app.tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); |
| 2368 | app.fieldW = app.tile * 10; |
| 2369 | app.fieldH = app.tile * 13; |
| 2370 | app.fieldX = Math.floor((vw - app.fieldW) / 2); |
| 2371 | app.fieldY = app.hudH + Math.floor((vh - app.hudH - app.fieldH - margin / 2) / 2); |
| 2372 | if (app.fieldY < app.hudH) |
| 2373 | { app.fieldY = app.hudH; } |
| 2374 | |
| 2375 | if (app.scene) |
| 2376 | { |
| 2377 | app.scene.position.x = app.fieldX; |
| 2378 | app.scene.position.y = app.fieldY; |
| 2379 | app.scene.size.x = app.fieldW; |
| 2380 | app.scene.size.y = app.fieldH; |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | function spriteFor (block) |
| 2385 | { |
| 2386 | if (block.state === 'locked') |
| 2387 | { return game.images.locked; } |
| 2388 | if (block.color < 0) |
| 2389 | { return game.images.gray; } |
| 2390 | return game.images[COLOR_SPRITES[block.color]] || game.images.gray; |
| 2391 | } |
| 2392 | |
| 2393 | function syncSprites () |
| 2394 | { |
| 2395 | if (!app.core || !app.scene) |
| 2396 | { return; } |
| 2397 | var seen = Object.create(null); |
| 2398 | var blocks = app.core.blocks; |
| 2399 | for (var i = 0; i < blocks.length; i++) |
| 2400 | { |
| 2401 | var block = blocks[i]; |
| 2402 | seen[block.id] = true; |
| 2403 | var obj = app.sprites[block.id]; |
| 2404 | if (!obj) |
| 2405 | { |
| 2406 | obj = new GameObject(0, 0, spriteFor(block), 2); |
| 2407 | app.scene.addObject(obj); |
| 2408 | app.sprites[block.id] = obj; |
| 2409 | } |
| 2410 | obj.renderer.sprite = spriteFor(block); |
| 2411 | placeTile(obj, block.col, block.y); |
| 2412 | applyBlockVisual(obj, block); |
| 2413 | obj.renderer.visible = true; |
| 2414 | obj.renderer.alpha = 1; |
| 2415 | |
| 2416 | if (block.flame) |
| 2417 | { |
| 2418 | var flame = app.flames[block.id]; |
| 2419 | if (!flame) |
| 2420 | { |
| 2421 | flame = new GameObject(0, 0, game.images.flame, 1); |
| 2422 | flame.renderer.setAnimation('burn', 0, 0.08); |
| 2423 | app.scene.addObject(flame); |
| 2424 | app.flames[block.id] = flame; |
| 2425 | } |
| 2426 | flame.renderer.visible = true; |
| 2427 | var t = app.tile; |
| 2428 | flame.transform.position.x = block.col * t + t / 2; |
| 2429 | flame.transform.position.y = app.fieldH - block.y * t + t * 0.15; |
| 2430 | flame.transform.size = { x: t * 0.85, y: t * 0.85 }; |
| 2431 | } |
| 2432 | else if (app.flames[block.id]) |
| 2433 | { app.flames[block.id].renderer.visible = false; } |
| 2434 | } |
| 2435 | |
| 2436 | var ids = Object.keys(app.sprites); |
| 2437 | for (var i = 0; i < ids.length; i++) |
| 2438 | { |
| 2439 | if (!seen[ids[i]]) |
| 2440 | { |
| 2441 | app.sprites[ids[i]].destroy(); |
| 2442 | delete app.sprites[ids[i]]; |
| 2443 | if (app.flames[ids[i]]) |
| 2444 | { |
| 2445 | app.flames[ids[i]].destroy(); |
| 2446 | delete app.flames[ids[i]]; |
| 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | function applyBlockVisual (obj, block) |
| 2453 | { |
| 2454 | var t = app.tile; |
| 2455 | var sx = 1; |
| 2456 | var sy = 1; |
| 2457 | var dy = 0; |
| 2458 | if (block.bouncing && app.planet && app.planet.bounceDuration > 0 |
| 2459 | && typeof Easing !== 'undefined' && Easing.bouncePose) |
| 2460 | { |
| 2461 | var u = block.bounceAge / app.planet.bounceDuration; |
| 2462 | var pose = Easing.bouncePose(u, app.planet.bounceSquash); |
| 2463 | sx = pose.sx; |
| 2464 | sy = pose.sy; |
| 2465 | dy = pose.dy; |
| 2466 | } |
| 2467 | var sel = 1; |
| 2468 | var tw = app.selectTween; |
| 2469 | if (tw && tw.blockId === block.id && tw.phase !== 'idle') |
| 2470 | { sel = tw.scale; } |
| 2471 | obj.transform.size = { x: t * sx * sel, y: t * sy * sel }; |
| 2472 | if (dy) |
| 2473 | { obj.transform.position.y -= dy * t; } |
| 2474 | } |
| 2475 | |
| 2476 | function clickTween () |
| 2477 | { |
| 2478 | return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null; |
| 2479 | } |
| 2480 | |
| 2481 | function startPressTween (blockId) |
| 2482 | { |
| 2483 | var c = clickTween(); |
| 2484 | if (!c) |
| 2485 | { return; } |
| 2486 | var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1; |
| 2487 | app.selectTween = { |
| 2488 | blockId: blockId, |
| 2489 | from: from, |
| 2490 | to: c.pressScale, |
| 2491 | t: 0, |
| 2492 | duration: c.pressDuration, |
| 2493 | ease: c.pressEase, |
| 2494 | scale: from, |
| 2495 | phase: 'press' |
| 2496 | }; |
| 2497 | } |
| 2498 | |
| 2499 | function startReleaseTween () |
| 2500 | { |
| 2501 | var c = clickTween(); |
| 2502 | var tw = app.selectTween; |
| 2503 | if (!c || !tw || tw.phase === 'idle') |
| 2504 | { return; } |
| 2505 | app.selectTween = { |
| 2506 | blockId: tw.blockId, |
| 2507 | from: tw.scale, |
| 2508 | to: c.releaseScale, |
| 2509 | t: 0, |
| 2510 | duration: c.releaseDuration, |
| 2511 | ease: c.releaseEase, |
| 2512 | scale: tw.scale, |
| 2513 | phase: 'release' |
| 2514 | }; |
| 2515 | } |
| 2516 | |
| 2517 | function updateSelectTween (dt) |
| 2518 | { |
| 2519 | var tw = app.selectTween; |
| 2520 | if (!tw || tw.phase === 'idle' || tw.phase === 'hold') |
| 2521 | { return; } |
| 2522 | tw.t += dt; |
| 2523 | var u = tw.duration > 0 ? tw.t / tw.duration : 1; |
| 2524 | if (u >= 1) |
| 2525 | { |
| 2526 | tw.scale = tw.to; |
| 2527 | tw.t = tw.duration; |
| 2528 | if (tw.phase === 'press') |
| 2529 | { tw.phase = 'hold'; } |
| 2530 | else |
| 2531 | { |
| 2532 | tw.phase = 'idle'; |
| 2533 | tw.scale = 1; |
| 2534 | tw.blockId = null; |
| 2535 | } |
| 2536 | return; |
| 2537 | } |
| 2538 | var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack); |
| 2539 | tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u; |
| 2540 | } |
| 2541 | |
| 2542 | function refreshHud () |
| 2543 | { |
| 2544 | if (!app.core) |
| 2545 | { return; } |
| 2546 | document.getElementById('score').textContent = String(app.core.score); |
| 2547 | var planet = app.planet; |
| 2548 | document.getElementById('planet-name').textContent = planet.name + ' · ' + planet.people; |
| 2549 | var d = Planet.displayValues(planet); |
| 2550 | document.getElementById('planet-stats').textContent = |
| 2551 | 'g ' + d.gravity.toFixed(2) + ' d ' + d.density.toFixed(2) + |
| 2552 | ' F ' + Math.round(d.newtons) + ' N Power ' + d.power + '%'; |
| 2553 | } |
| 2554 | |
| 2555 | function makeManager () |
| 2556 | { |
| 2557 | var obj = new GameObject(0, 0, game.images.pixel, 0); |
| 2558 | obj.renderer.visible = false; |
| 2559 | var base = obj.update.bind(obj); |
| 2560 | obj.update = function (dt) |
| 2561 | { |
| 2562 | base(dt); |
| 2563 | tickView(dt); |
| 2564 | }; |
| 2565 | return obj; |
| 2566 | } |
| 2567 | |
| 2568 | function tickView (dt) |
| 2569 | { |
| 2570 | if (app.mode !== 'playing' || !app.core) |
| 2571 | { return; } |
| 2572 | |
| 2573 | layout(); |
| 2574 | for (var i = 0; i < app.cells.length; i++) |
| 2575 | { |
| 2576 | var col = i % 10; |
| 2577 | var row = Math.floor(i / 10); |
| 2578 | placeTile(app.cells[i], col, row); |
| 2579 | } |
| 2580 | |
| 2581 | app.core.tick(dt); |
| 2582 | updateSelectTween(dt); |
| 2583 | syncSprites(); |
| 2584 | refreshHud(); |
| 2585 | |
| 2586 | if (app.core.state === 'gameover' && !app.gameOverShown) |
| 2587 | { |
| 2588 | app.gameOverShown = true; |
| 2589 | app.lastScore = app.core.score; |
| 2590 | document.getElementById('go-score').textContent = String(app.lastScore); |
| 2591 | showEl('game-over'); |
| 2592 | } |
| 2593 | if (app.core.state === 'gameover' && app.core.gameOverTime >= GAME_OVER_DELAY) |
| 2594 | { returnToMenu(); } |
| 2595 | } |
| 2596 | |
| 2597 | /* ---------- pointer input (mouse + touch) --------------------------- */ |
| 2598 | /* |
| 2599 | * Both mouse and touch use Pointer Events. After pointerdown we listen |
| 2600 | * on window, so a drag that leaves the canvas (needed to launch a |
| 2601 | * block at the top of a full column) still reaches moveDrag. We do |
| 2602 | * not treat lostpointercapture as the end of a drag: on some touch |
| 2603 | * browsers that fires mid-gesture and would cancel a launch. |
| 2604 | */ |
| 2605 | |
| 2606 | var boundCanvas = null; |
| 2607 | var windowListening = false; |
| 2608 | |
| 2609 | function addWindowPointer () |
| 2610 | { |
| 2611 | if (windowListening) |
| 2612 | { return; } |
| 2613 | window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false }); |
| 2614 | window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false }); |
| 2615 | window.addEventListener('pointercancel', onPointerUp, { capture: true, passive: false }); |
| 2616 | windowListening = true; |
| 2617 | } |
| 2618 | |
| 2619 | function removeWindowPointer () |
| 2620 | { |
| 2621 | if (!windowListening) |
| 2622 | { return; } |
| 2623 | window.removeEventListener('pointermove', onPointerMove, { capture: true }); |
| 2624 | window.removeEventListener('pointerup', onPointerUp, { capture: true }); |
| 2625 | window.removeEventListener('pointercancel', onPointerUp, { capture: true }); |
| 2626 | windowListening = false; |
| 2627 | } |
| 2628 | |
| 2629 | function bindPointer (canvas) |
| 2630 | { |
| 2631 | unbindPointer(); |
| 2632 | boundCanvas = canvas; |
| 2633 | canvas.addEventListener('pointerdown', onPointerDown, { passive: false }); |
| 2634 | canvas.style.touchAction = 'none'; |
| 2635 | canvas.style.cursor = 'pointer'; |
| 2636 | canvas.style.webkitUserSelect = 'none'; |
| 2637 | canvas.style.userSelect = 'none'; |
| 2638 | } |
| 2639 | |
| 2640 | function unbindPointer () |
| 2641 | { |
| 2642 | removeWindowPointer(); |
| 2643 | if (boundCanvas) |
| 2644 | { boundCanvas.removeEventListener('pointerdown', onPointerDown); } |
| 2645 | boundCanvas = null; |
| 2646 | app.pointerId = null; |
| 2647 | } |
| 2648 | |
| 2649 | function fieldFromEvent (e) |
| 2650 | { |
| 2651 | var canvas = app.scene && app.scene.canvas; |
| 2652 | if (!canvas) |
| 2653 | { return null; } |
| 2654 | var rect = canvas.getBoundingClientRect(); |
| 2655 | if (rect.width <= 0 || rect.height <= 0) |
| 2656 | { return null; } |
| 2657 | var x = (e.clientX - rect.left) * (canvas.width / rect.width); |
| 2658 | var y = (e.clientY - rect.top) * (canvas.height / rect.height); |
| 2659 | // Fractional column so beginDrag can snap across a nearby edge. |
| 2660 | var col = x / app.tile; |
| 2661 | var fieldY = (canvas.height - y) / app.tile; |
| 2662 | return { col: col, y: fieldY }; |
| 2663 | } |
| 2664 | |
| 2665 | function onPointerDown (e) |
| 2666 | { |
| 2667 | if (!app.core || app.core.state !== 'playing') |
| 2668 | { return; } |
| 2669 | if (app.pointerId != null) |
| 2670 | { return; } |
| 2671 | e.preventDefault(); |
| 2672 | app.pointerId = e.pointerId; |
| 2673 | try |
| 2674 | { boundCanvas.setPointerCapture(e.pointerId); } |
| 2675 | catch (err) { /* ignore */ } |
| 2676 | addWindowPointer(); |
| 2677 | var pos = fieldFromEvent(e); |
| 2678 | // Instant grab, no long-press or hold delay: the block is taken on |
| 2679 | // pointerdown and the click tween starts in the same moment. |
| 2680 | if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag) |
| 2681 | { startPressTween(app.core.drag.blockId); } |
| 2682 | } |
| 2683 | |
| 2684 | function onPointerMove (e) |
| 2685 | { |
| 2686 | if (app.pointerId == null || e.pointerId !== app.pointerId) |
| 2687 | { return; } |
| 2688 | if (!app.core || !app.core.drag) |
| 2689 | { return; } |
| 2690 | e.preventDefault(); |
| 2691 | var pos = fieldFromEvent(e); |
| 2692 | if (pos) |
| 2693 | { app.core.moveDrag(pos.y); } |
| 2694 | } |
| 2695 | |
| 2696 | function onPointerUp (e) |
| 2697 | { |
| 2698 | if (app.pointerId == null) |
| 2699 | { return; } |
| 2700 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 2701 | { return; } |
| 2702 | if (e) |
| 2703 | { e.preventDefault(); } |
| 2704 | app.pointerId = null; |
| 2705 | removeWindowPointer(); |
| 2706 | if (boundCanvas) |
| 2707 | { |
| 2708 | try |
| 2709 | { boundCanvas.releasePointerCapture(e && e.pointerId); } |
| 2710 | catch (err) { /* ignore */ } |
| 2711 | } |
| 2712 | startReleaseTween(); |
| 2713 | if (app.core) |
| 2714 | { app.core.endDrag(); } |
| 2715 | } |
| 2716 | |
| 2717 | function hideEl (id) |
| 2718 | { |
| 2719 | document.getElementById(id).hidden = true; |
| 2720 | } |
| 2721 | |
| 2722 | function showEl (id) |
| 2723 | { |
| 2724 | document.getElementById(id).hidden = false; |
| 2725 | } |
| 2726 | |
| 2727 | window.addEventListener('resize', function () |
| 2728 | { |
| 2729 | if (app.mode === 'playing') |
| 2730 | { layout(); } |
| 2731 | }); |
| 2732 | |
| 2733 | window.addEventListener('orientationchange', function () |
| 2734 | { |
| 2735 | setTimeout(function () |
| 2736 | { |
| 2737 | if (app.mode === 'playing') |
| 2738 | { layout(); } |
| 2739 | }, 120); |
| 2740 | }); |
| 2741 | |
| 2742 | /* |
| 2743 | * End File: |
| 2744 | * /workspace/meteors/src/main.js |
| 2745 | */ |
projects-timfalken-com meteors