| 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 | this.outgoingShots = []; |
| 447 | // Stage-2 default; stage 3 replaces this with Planet.missChance (Power). |
| 448 | this.missChance = optsMissChance(arguments); |
| 449 | } |
| 450 | |
| 451 | function optsMissChance (args) |
| 452 | { |
| 453 | // GameCore(planet, rng, missChance) — third arg is optional. |
| 454 | if (args.length >= 3 && typeof args[2] === 'number' && isFinite(args[2])) |
| 455 | { return Math.max(0, Math.min(1, args[2])); } |
| 456 | return GameCore.DEFAULT_MISS_CHANCE; |
| 457 | } |
| 458 | |
| 459 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 460 | GameCore.LAUNCH_PULL = LAUNCH_PULL; |
| 461 | GameCore.SWAP_DISTANCE = SWAP_DISTANCE; |
| 462 | GameCore.SNAP_Y = SNAP_Y; |
| 463 | GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP; |
| 464 | GameCore.DEFAULT_MISS_CHANCE = 0.30; |
| 465 | GameCore.STATE_STILL = STATE_STILL; |
| 466 | GameCore.STATE_FALLING = STATE_FALLING; |
| 467 | GameCore.STATE_FLYING = STATE_FLYING; |
| 468 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 469 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 470 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 471 | |
| 472 | GameCore.prototype.randomColor = function () |
| 473 | { |
| 474 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 475 | }; |
| 476 | |
| 477 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 478 | { |
| 479 | var block = { |
| 480 | id: this.nextBlockId++, |
| 481 | col: col, |
| 482 | y: y, |
| 483 | color: color, |
| 484 | state: state || STATE_STILL, |
| 485 | vy: 0, |
| 486 | clusterId: null, |
| 487 | settleTime: 0, |
| 488 | flame: false, |
| 489 | pushedClusters: null, |
| 490 | manualTarget: 0, |
| 491 | manualDecel: 0, |
| 492 | manualPhase: null, |
| 493 | bouncing: false, |
| 494 | bounceAge: 0 |
| 495 | }; |
| 496 | this.blocks.push(block); |
| 497 | return block; |
| 498 | }; |
| 499 | |
| 500 | GameCore.prototype.removeBlock = function (block) |
| 501 | { |
| 502 | if (block.clusterId != null) |
| 503 | { this.leaveCluster(block); } |
| 504 | var index = this.blocks.indexOf(block); |
| 505 | if (index >= 0) |
| 506 | { this.blocks.splice(index, 1); } |
| 507 | }; |
| 508 | |
| 509 | GameCore.prototype.leaveCluster = function (block) |
| 510 | { |
| 511 | var id = block.clusterId; |
| 512 | block.clusterId = null; |
| 513 | block.flame = false; |
| 514 | if (id == null || !this.clusters[id]) |
| 515 | { return; } |
| 516 | var still = 0; |
| 517 | for (var i = 0; i < this.blocks.length; i++) |
| 518 | { |
| 519 | if (this.blocks[i].clusterId === id) |
| 520 | { still++; } |
| 521 | } |
| 522 | if (still === 0) |
| 523 | { delete this.clusters[id]; } |
| 524 | }; |
| 525 | |
| 526 | GameCore.prototype.blocksInColumn = function (col) |
| 527 | { |
| 528 | var out = []; |
| 529 | for (var i = 0; i < this.blocks.length; i++) |
| 530 | { |
| 531 | if (this.blocks[i].col === col) |
| 532 | { out.push(this.blocks[i]); } |
| 533 | } |
| 534 | out.sort(function (a, b) { return a.y - b.y; }); |
| 535 | return out; |
| 536 | }; |
| 537 | |
| 538 | GameCore.prototype.isGrounded = function (block) |
| 539 | { |
| 540 | return block.state === STATE_STILL |
| 541 | || block.state === STATE_SETTLING |
| 542 | || block.state === STATE_LOCKED; |
| 543 | }; |
| 544 | |
| 545 | GameCore.prototype.stackTop = function (col, ignore) |
| 546 | { |
| 547 | var top = 0; |
| 548 | for (var i = 0; i < this.blocks.length; i++) |
| 549 | { |
| 550 | var block = this.blocks[i]; |
| 551 | if (block.col !== col) |
| 552 | { continue; } |
| 553 | if (ignore && ignore(block)) |
| 554 | { continue; } |
| 555 | if (this.isGrounded(block)) |
| 556 | { |
| 557 | var surface = block.y + 1; |
| 558 | if (surface > top) |
| 559 | { top = surface; } |
| 560 | } |
| 561 | } |
| 562 | return top; |
| 563 | }; |
| 564 | |
| 565 | GameCore.prototype.isColumnLocked = function (col) |
| 566 | { |
| 567 | for (var i = 0; i < this.blocks.length; i++) |
| 568 | { |
| 569 | var block = this.blocks[i]; |
| 570 | if (block.col === col && block.state === STATE_LOCKED) |
| 571 | { return true; } |
| 572 | } |
| 573 | return false; |
| 574 | }; |
| 575 | |
| 576 | GameCore.prototype.isColumnStill = function (col) |
| 577 | { |
| 578 | if (this.isColumnLocked(col)) |
| 579 | { return false; } |
| 580 | for (var i = 0; i < this.blocks.length; i++) |
| 581 | { |
| 582 | var block = this.blocks[i]; |
| 583 | if (block.col !== col) |
| 584 | { continue; } |
| 585 | if (block.state === STATE_FLYING |
| 586 | || block.state === STATE_FALLING |
| 587 | || block.state === STATE_MANUAL |
| 588 | || block.state === STATE_SETTLING) |
| 589 | { return false; } |
| 590 | } |
| 591 | return true; |
| 592 | }; |
| 593 | |
| 594 | GameCore.prototype.groundedCount = function (col) |
| 595 | { |
| 596 | var n = 0; |
| 597 | for (var i = 0; i < this.blocks.length; i++) |
| 598 | { |
| 599 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 600 | { n++; } |
| 601 | } |
| 602 | return n; |
| 603 | }; |
| 604 | |
| 605 | GameCore.prototype.blockAt = function (col, row) |
| 606 | { |
| 607 | for (var i = 0; i < this.blocks.length; i++) |
| 608 | { |
| 609 | var block = this.blocks[i]; |
| 610 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 611 | { return block; } |
| 612 | } |
| 613 | return null; |
| 614 | }; |
| 615 | |
| 616 | GameCore.prototype.place = function (col, row, color, state) |
| 617 | { |
| 618 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 619 | }; |
| 620 | |
| 621 | GameCore.prototype.lockedColumnCount = function () |
| 622 | { |
| 623 | var n = 0; |
| 624 | for (var c = 0; c < this.width; c++) |
| 625 | { |
| 626 | if (this.isColumnLocked(c)) |
| 627 | { n++; } |
| 628 | } |
| 629 | return n; |
| 630 | }; |
| 631 | |
| 632 | GameCore.prototype.startBounce = function (block) |
| 633 | { |
| 634 | if (!block) |
| 635 | { return; } |
| 636 | block.bouncing = true; |
| 637 | block.bounceAge = 0; |
| 638 | }; |
| 639 | |
| 640 | GameCore.prototype.advanceBounces = function (dt) |
| 641 | { |
| 642 | var duration = this.planet.bounceDuration; |
| 643 | for (var i = 0; i < this.blocks.length; i++) |
| 644 | { |
| 645 | var block = this.blocks[i]; |
| 646 | if (!block.bouncing) |
| 647 | { continue; } |
| 648 | block.bounceAge += dt; |
| 649 | if (duration <= 0 || block.bounceAge >= duration) |
| 650 | { |
| 651 | block.bouncing = false; |
| 652 | block.bounceAge = duration; |
| 653 | } |
| 654 | } |
| 655 | }; |
| 656 | |
| 657 | /** |
| 658 | * True if `block` rests on the floor or on a solid (still / settling / |
| 659 | * locked) block immediately below it in the same column. |
| 660 | */ |
| 661 | GameCore.prototype.hasSupport = function (block) |
| 662 | { |
| 663 | if (block.y <= 1e-9) |
| 664 | { return true; } |
| 665 | for (var i = 0; i < this.blocks.length; i++) |
| 666 | { |
| 667 | var other = this.blocks[i]; |
| 668 | if (other === block || other.col !== block.col) |
| 669 | { continue; } |
| 670 | if (!this.isGrounded(other)) |
| 671 | { continue; } |
| 672 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 673 | { return true; } |
| 674 | } |
| 675 | return false; |
| 676 | }; |
| 677 | |
| 678 | /** Highest flying block in this column whose top is at or below `block.y + 1`. */ |
| 679 | GameCore.prototype.highestFlyingBelow = function (block) |
| 680 | { |
| 681 | var best = null; |
| 682 | for (var i = 0; i < this.blocks.length; i++) |
| 683 | { |
| 684 | var other = this.blocks[i]; |
| 685 | if (other === block || other.col !== block.col) |
| 686 | { continue; } |
| 687 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 688 | { continue; } |
| 689 | var cluster = this.clusters[other.clusterId]; |
| 690 | if (!cluster || cluster.phase === 'landed') |
| 691 | { continue; } |
| 692 | // The falling block is at or above this flyer (or already overlapping). |
| 693 | if (block.y + 1e-9 < other.y) |
| 694 | { continue; } |
| 695 | if (!best || other.y > best.y) |
| 696 | { best = other; } |
| 697 | } |
| 698 | return best; |
| 699 | }; |
| 700 | |
| 701 | GameCore.prototype.highestFallingBelow = function (block) |
| 702 | { |
| 703 | var best = null; |
| 704 | for (var i = 0; i < this.blocks.length; i++) |
| 705 | { |
| 706 | var other = this.blocks[i]; |
| 707 | if (other === block || other.col !== block.col) |
| 708 | { continue; } |
| 709 | if (other.state !== STATE_FALLING) |
| 710 | { continue; } |
| 711 | if (other.y > block.y - 1e-9) |
| 712 | { continue; } |
| 713 | if (!best || other.y > best.y) |
| 714 | { best = other; } |
| 715 | } |
| 716 | return best; |
| 717 | }; |
| 718 | |
| 719 | /** |
| 720 | * Make an unsupported still block start falling from rest, so it eases |
| 721 | * into fallSpeed under normal gravity. Skips the block being dragged. |
| 722 | */ |
| 723 | /** |
| 724 | * Strip leftover launch / manual kinematics so a block that has just |
| 725 | * become still or falling starts from rest. Used after a landed grey |
| 726 | * cluster recolors, and whenever an unsupported still block drops. |
| 727 | */ |
| 728 | GameCore.prototype.clearFlightState = function (block) |
| 729 | { |
| 730 | block.vy = 0; |
| 731 | block.flame = false; |
| 732 | block.manualPhase = null; |
| 733 | block.manualTarget = null; |
| 734 | block.manualDecel = 0; |
| 735 | block.pushedClusters = null; |
| 736 | }; |
| 737 | |
| 738 | GameCore.prototype.releaseUnsupported = function () |
| 739 | { |
| 740 | for (var i = 0; i < this.blocks.length; i++) |
| 741 | { |
| 742 | var block = this.blocks[i]; |
| 743 | if (block.state !== STATE_STILL) |
| 744 | { continue; } |
| 745 | if (this.drag && this.drag.blockId === block.id) |
| 746 | { continue; } |
| 747 | if (this.hasSupport(block)) |
| 748 | { continue; } |
| 749 | this.leaveCluster(block); |
| 750 | this.clearFlightState(block); |
| 751 | block.state = STATE_FALLING; |
| 752 | } |
| 753 | }; |
| 754 | |
| 755 | GameCore.prototype.accelerateFall = function (block, dt) |
| 756 | { |
| 757 | var target = -this.planet.fallSpeed; |
| 758 | if (block.vy > target) |
| 759 | { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); } |
| 760 | else |
| 761 | { block.vy = target; } |
| 762 | }; |
| 763 | |
| 764 | GameCore.prototype.landFallingBlock = function (block) |
| 765 | { |
| 766 | block.vy = 0; |
| 767 | block.flame = false; |
| 768 | this.startBounce(block); |
| 769 | if (block.color === COLOR_GRAY) |
| 770 | { |
| 771 | block.state = STATE_SETTLING; |
| 772 | block.settleTime = 0; |
| 773 | } |
| 774 | else |
| 775 | { block.state = STATE_STILL; } |
| 776 | }; |
| 777 | |
| 778 | /** |
| 779 | * Sit `block` on top of the flying cluster that `flyer` belongs to, and |
| 780 | * carry it along as cargo (no flame). Returns true if attached. |
| 781 | */ |
| 782 | GameCore.prototype.attachToFlyingCluster = function (block, flyer) |
| 783 | { |
| 784 | var cluster = this.clusters[flyer.clusterId]; |
| 785 | if (!cluster || cluster.phase === 'landed') |
| 786 | { return false; } |
| 787 | var roof = flyer.y + 1; |
| 788 | for (var i = 0; i < this.blocks.length; i++) |
| 789 | { |
| 790 | var other = this.blocks[i]; |
| 791 | if (other === block || other.col !== block.col) |
| 792 | { continue; } |
| 793 | if (other.clusterId !== cluster.id) |
| 794 | { continue; } |
| 795 | if (other.y + 1 > roof) |
| 796 | { roof = other.y + 1; } |
| 797 | } |
| 798 | block.y = roof; |
| 799 | block.state = STATE_FLYING; |
| 800 | block.clusterId = cluster.id; |
| 801 | block.vy = cluster.vy; |
| 802 | block.flame = false; |
| 803 | block.settleTime = 0; |
| 804 | return true; |
| 805 | }; |
| 806 | |
| 807 | GameCore.prototype.catchFallingOntoCluster = function (cluster, members) |
| 808 | { |
| 809 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 810 | { return; } |
| 811 | // Snapshot falling blocks; attaching mutates state. |
| 812 | var falling = []; |
| 813 | for (var i = 0; i < this.blocks.length; i++) |
| 814 | { |
| 815 | if (this.blocks[i].state === STATE_FALLING) |
| 816 | { falling.push(this.blocks[i]); } |
| 817 | } |
| 818 | for (var i = 0; i < falling.length; i++) |
| 819 | { |
| 820 | var block = falling[i]; |
| 821 | var flyer = null; |
| 822 | for (var m = 0; m < members.length; m++) |
| 823 | { |
| 824 | var other = members[m]; |
| 825 | if (other.col !== block.col) |
| 826 | { continue; } |
| 827 | // Overlap or rest on this member's top. |
| 828 | if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9) |
| 829 | { |
| 830 | if (!flyer || other.y > flyer.y) |
| 831 | { flyer = other; } |
| 832 | } |
| 833 | } |
| 834 | if (flyer) |
| 835 | { this.attachToFlyingCluster(block, flyer); } |
| 836 | } |
| 837 | }; |
| 838 | |
| 839 | GameCore.prototype.tick = function (dt) |
| 840 | { |
| 841 | if (this.state === 'gameover') |
| 842 | { |
| 843 | this.gameOverTime += Math.max(0, dt); |
| 844 | return; |
| 845 | } |
| 846 | if (dt <= 0) |
| 847 | { return; } |
| 848 | dt = Math.min(dt, 0.1); |
| 849 | |
| 850 | this.spawnTimer += dt; |
| 851 | while (this.spawnTimer >= this.spawnInterval) |
| 852 | { |
| 853 | this.spawnTimer -= this.spawnInterval; |
| 854 | this.trySpawn(); |
| 855 | } |
| 856 | |
| 857 | var left = dt; |
| 858 | while (left > 1e-12) |
| 859 | { |
| 860 | var step = Math.min(SUBSTEP, left); |
| 861 | this.integrateFalling(step); |
| 862 | this.integrateClusters(step); |
| 863 | this.mergeFlyingClusters(); |
| 864 | this.integrateManual(step); |
| 865 | this.cullOffTop(); |
| 866 | left -= step; |
| 867 | } |
| 868 | |
| 869 | this.releaseUnsupported(); |
| 870 | this.advanceSettle(dt); |
| 871 | // After a landed grey group is released, blocks that were sitting on |
| 872 | // hanging members are still STILL (the earlier pass ran while those |
| 873 | // members counted as support). Drop them from rest before matching, |
| 874 | // so they cannot form a mid-air match and shoot off the top. |
| 875 | this.releaseUnsupported(); |
| 876 | this.advanceBounces(dt); |
| 877 | if (!this.drag) |
| 878 | { this.resolveMatches(); } |
| 879 | this.checkLocks(); |
| 880 | this.checkGameOver(); |
| 881 | }; |
| 882 | |
| 883 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 884 | GameCore.prototype.step = function (seconds, dt) |
| 885 | { |
| 886 | dt = dt || SUBSTEP; |
| 887 | var t = 0; |
| 888 | while (t < seconds - 1e-15) |
| 889 | { |
| 890 | var s = Math.min(dt, seconds - t); |
| 891 | this.tick(s); |
| 892 | t += s; |
| 893 | } |
| 894 | }; |
| 895 | |
| 896 | GameCore.prototype.trySpawn = function () |
| 897 | { |
| 898 | if (this.state !== 'playing') |
| 899 | { return null; } |
| 900 | var eligible = []; |
| 901 | for (var col = 0; col < this.width; col++) |
| 902 | { |
| 903 | if (this.isColumnLocked(col)) |
| 904 | { continue; } |
| 905 | if (this.groundedCount(col) >= this.height) |
| 906 | { continue; } |
| 907 | var alreadyFalling = false; |
| 908 | var list = this.blocksInColumn(col); |
| 909 | for (var i = 0; i < list.length; i++) |
| 910 | { |
| 911 | if (list[i].state === STATE_FALLING) |
| 912 | { |
| 913 | alreadyFalling = true; |
| 914 | break; |
| 915 | } |
| 916 | } |
| 917 | if (!alreadyFalling) |
| 918 | { eligible.push(col); } |
| 919 | } |
| 920 | if (!eligible.length) |
| 921 | { return null; } |
| 922 | var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 923 | var color = this.randomColor(); |
| 924 | var block = this.createBlock(col, this.height, color, STATE_FALLING); |
| 925 | block.vy = -this.planet.fallSpeed; |
| 926 | return block; |
| 927 | }; |
| 928 | |
| 929 | GameCore.prototype.integrateFalling = function (dt) |
| 930 | { |
| 931 | var falling = []; |
| 932 | for (var i = 0; i < this.blocks.length; i++) |
| 933 | { |
| 934 | if (this.blocks[i].state === STATE_FALLING) |
| 935 | { falling.push(this.blocks[i]); } |
| 936 | } |
| 937 | // Lowest first, so a stack of falling blocks rests on itself. |
| 938 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 939 | for (var i = 0; i < falling.length; i++) |
| 940 | { |
| 941 | var block = falling[i]; |
| 942 | if (block.state !== STATE_FALLING) |
| 943 | { continue; } |
| 944 | this.accelerateFall(block, dt); |
| 945 | var nextY = block.y + block.vy * dt; |
| 946 | var flyer = this.highestFlyingBelow(block); |
| 947 | if (flyer && nextY <= flyer.y + 1 + 1e-9) |
| 948 | { |
| 949 | if (this.attachToFlyingCluster(block, flyer)) |
| 950 | { continue; } |
| 951 | } |
| 952 | var top = this.stackTop(block.col); |
| 953 | var below = this.highestFallingBelow(block); |
| 954 | if (below && below.y + 1 > top) |
| 955 | { top = below.y + 1; } |
| 956 | if (nextY <= top) |
| 957 | { |
| 958 | block.y = top; |
| 959 | this.landFallingBlock(block); |
| 960 | } |
| 961 | else |
| 962 | { block.y = nextY; } |
| 963 | } |
| 964 | }; |
| 965 | |
| 966 | GameCore.prototype.clusterMembers = function (clusterId) |
| 967 | { |
| 968 | var out = []; |
| 969 | for (var i = 0; i < this.blocks.length; i++) |
| 970 | { |
| 971 | if (this.blocks[i].clusterId === clusterId) |
| 972 | { out.push(this.blocks[i]); } |
| 973 | } |
| 974 | return out; |
| 975 | }; |
| 976 | |
| 977 | |
| 978 | /** |
| 979 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 980 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 981 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 982 | * upward deceleration. After the apex, vy eases from 0 to -fallSpeed |
| 983 | * at planet.fallAccel, then stays at terminal fallSpeed. |
| 984 | */ |
| 985 | GameCore.prototype.advanceJump = function (body, dt) |
| 986 | { |
| 987 | var dy = 0; |
| 988 | var left = dt; |
| 989 | if (body.phase === 'up') |
| 990 | { |
| 991 | var decel = body.decel; |
| 992 | var vy = body.vy; |
| 993 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 994 | if (tApex <= left + 1e-15) |
| 995 | { |
| 996 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 997 | body.vy = 0; |
| 998 | body.phase = 'down'; |
| 999 | left -= tApex; |
| 1000 | } |
| 1001 | else |
| 1002 | { |
| 1003 | var vyNew = vy - decel * left; |
| 1004 | dy += (vy + vyNew) * 0.5 * left; |
| 1005 | body.vy = vyNew; |
| 1006 | left = 0; |
| 1007 | } |
| 1008 | } |
| 1009 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 1010 | { |
| 1011 | body.vy = 0; |
| 1012 | body.phase = 'down'; |
| 1013 | } |
| 1014 | if (body.phase === 'down' && left > 0) |
| 1015 | { |
| 1016 | var target = -this.planet.fallSpeed; |
| 1017 | var accel = this.planet.fallAccel; |
| 1018 | if (accel < 1e-12) |
| 1019 | { |
| 1020 | body.vy = target; |
| 1021 | dy += body.vy * left; |
| 1022 | } |
| 1023 | else if (body.vy > target + 1e-12) |
| 1024 | { |
| 1025 | var tReach = (body.vy - target) / accel; |
| 1026 | if (tReach <= left + 1e-15) |
| 1027 | { |
| 1028 | dy += (body.vy + target) * 0.5 * tReach; |
| 1029 | body.vy = target; |
| 1030 | left -= tReach; |
| 1031 | dy += body.vy * left; |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | var vyNew = body.vy - accel * left; |
| 1036 | dy += (body.vy + vyNew) * 0.5 * left; |
| 1037 | body.vy = vyNew; |
| 1038 | } |
| 1039 | } |
| 1040 | else |
| 1041 | { |
| 1042 | body.vy = target; |
| 1043 | dy += body.vy * left; |
| 1044 | } |
| 1045 | } |
| 1046 | return dy; |
| 1047 | }; |
| 1048 | |
| 1049 | GameCore.prototype.integrateClusters = function (dt) |
| 1050 | { |
| 1051 | var ids = Object.keys(this.clusters); |
| 1052 | for (var n = 0; n < ids.length; n++) |
| 1053 | { |
| 1054 | var cluster = this.clusters[ids[n]]; |
| 1055 | if (!cluster) |
| 1056 | { continue; } |
| 1057 | var members = this.clusterMembers(cluster.id); |
| 1058 | if (!members.length) |
| 1059 | { |
| 1060 | delete this.clusters[cluster.id]; |
| 1061 | continue; |
| 1062 | } |
| 1063 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1064 | { continue; } |
| 1065 | |
| 1066 | var dy = this.advanceJump(cluster, dt); |
| 1067 | for (var i = 0; i < members.length; i++) |
| 1068 | { |
| 1069 | members[i].y += dy; |
| 1070 | members[i].vy = cluster.vy; |
| 1071 | } |
| 1072 | |
| 1073 | this.catchFallingOntoCluster(cluster, members); |
| 1074 | |
| 1075 | if (cluster.phase === 'down') |
| 1076 | { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); } |
| 1077 | } |
| 1078 | }; |
| 1079 | |
| 1080 | |
| 1081 | /** |
| 1082 | * If two or more flying clusters overlap in the same column, merge them |
| 1083 | * into one cluster. Velocity (and deceleration and jumpHeight) is the |
| 1084 | * mass-weighted average by block count. Phase is 'up' when the merged |
| 1085 | * vy is positive, otherwise 'down'. Overlapping members in a column |
| 1086 | * are packed so the higher one sits flush on the lower. |
| 1087 | */ |
| 1088 | GameCore.prototype.mergeFlyingClusters = function () |
| 1089 | { |
| 1090 | var flying = []; |
| 1091 | var ids = Object.keys(this.clusters); |
| 1092 | for (var n = 0; n < ids.length; n++) |
| 1093 | { |
| 1094 | var cluster = this.clusters[ids[n]]; |
| 1095 | if (!cluster) |
| 1096 | { continue; } |
| 1097 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1098 | { continue; } |
| 1099 | flying.push(cluster); |
| 1100 | } |
| 1101 | if (flying.length < 2) |
| 1102 | { return; } |
| 1103 | |
| 1104 | var uf = new UnionFind(); |
| 1105 | for (var i = 0; i < flying.length; i++) |
| 1106 | { uf.add(flying[i].id); } |
| 1107 | for (var i = 0; i < flying.length; i++) |
| 1108 | { |
| 1109 | for (var j = i + 1; j < flying.length; j++) |
| 1110 | { |
| 1111 | if (this.flyingClustersOverlap(flying[i], flying[j])) |
| 1112 | { uf.union(flying[i].id, flying[j].id); } |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | var groups = Object.create(null); |
| 1117 | for (var i = 0; i < flying.length; i++) |
| 1118 | { |
| 1119 | var root = uf.find(flying[i].id); |
| 1120 | if (!groups[root]) |
| 1121 | { groups[root] = []; } |
| 1122 | groups[root].push(flying[i]); |
| 1123 | } |
| 1124 | for (var root in groups) |
| 1125 | { |
| 1126 | if (groups[root].length >= 2) |
| 1127 | { this.mergeClusterGroup(groups[root]); } |
| 1128 | } |
| 1129 | }; |
| 1130 | |
| 1131 | GameCore.prototype.flyingClustersOverlap = function (a, b) |
| 1132 | { |
| 1133 | var ma = this.clusterMembers(a.id); |
| 1134 | var mb = this.clusterMembers(b.id); |
| 1135 | for (var i = 0; i < ma.length; i++) |
| 1136 | { |
| 1137 | for (var j = 0; j < mb.length; j++) |
| 1138 | { |
| 1139 | if (ma[i].col !== mb[j].col) |
| 1140 | { continue; } |
| 1141 | if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9) |
| 1142 | { return true; } |
| 1143 | } |
| 1144 | } |
| 1145 | return false; |
| 1146 | }; |
| 1147 | |
| 1148 | GameCore.prototype.mergeClusterGroup = function (clusters) |
| 1149 | { |
| 1150 | clusters.sort(function (a, b) { return a.id - b.id; }); |
| 1151 | var keep = clusters[0]; |
| 1152 | var n = 0; |
| 1153 | var vySum = 0; |
| 1154 | var decelSum = 0; |
| 1155 | var hSum = 0; |
| 1156 | for (var c = 0; c < clusters.length; c++) |
| 1157 | { |
| 1158 | var mem = this.clusterMembers(clusters[c].id); |
| 1159 | var m = mem.length; |
| 1160 | n += m; |
| 1161 | vySum += clusters[c].vy * m; |
| 1162 | decelSum += (clusters[c].decel || 0) * m; |
| 1163 | hSum += (clusters[c].jumpHeight || 0) * m; |
| 1164 | } |
| 1165 | if (n === 0) |
| 1166 | { return; } |
| 1167 | keep.vy = vySum / n; |
| 1168 | keep.decel = decelSum / n; |
| 1169 | keep.jumpHeight = hSum / n; |
| 1170 | keep.phase = keep.vy > 1e-10 ? 'up' : 'down'; |
| 1171 | for (var c = 0; c < clusters.length; c++) |
| 1172 | { |
| 1173 | var mem = this.clusterMembers(clusters[c].id); |
| 1174 | for (var i = 0; i < mem.length; i++) |
| 1175 | { |
| 1176 | mem[i].clusterId = keep.id; |
| 1177 | mem[i].vy = keep.vy; |
| 1178 | mem[i].state = STATE_FLYING; |
| 1179 | } |
| 1180 | if (clusters[c] !== keep) |
| 1181 | { delete this.clusters[clusters[c].id]; } |
| 1182 | } |
| 1183 | this.packClusterOverlaps(keep); |
| 1184 | if (keep.phase === 'down') |
| 1185 | { this.tryLandCluster(keep, this.clusterMembers(keep.id)); } |
| 1186 | }; |
| 1187 | |
| 1188 | GameCore.prototype.packClusterOverlaps = function (cluster) |
| 1189 | { |
| 1190 | var members = this.clusterMembers(cluster.id); |
| 1191 | var byCol = Object.create(null); |
| 1192 | for (var i = 0; i < members.length; i++) |
| 1193 | { |
| 1194 | var col = members[i].col; |
| 1195 | if (!byCol[col]) |
| 1196 | { byCol[col] = []; } |
| 1197 | byCol[col].push(members[i]); |
| 1198 | } |
| 1199 | var cols = Object.keys(byCol); |
| 1200 | for (var c = 0; c < cols.length; c++) |
| 1201 | { |
| 1202 | var list = byCol[cols[c]]; |
| 1203 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1204 | for (var i = 1; i < list.length; i++) |
| 1205 | { |
| 1206 | var minY = list[i - 1].y + 1; |
| 1207 | if (list[i].y < minY - 1e-12) |
| 1208 | { list[i].y = minY; } |
| 1209 | } |
| 1210 | } |
| 1211 | }; |
| 1212 | |
| 1213 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 1214 | { |
| 1215 | if (!members.length) |
| 1216 | { return; } |
| 1217 | var shift = 0; |
| 1218 | var hit = false; |
| 1219 | for (var i = 0; i < members.length; i++) |
| 1220 | { |
| 1221 | var block = members[i]; |
| 1222 | var top = this.stackTop(block.col, function (other) |
| 1223 | { |
| 1224 | return other.clusterId === cluster.id; |
| 1225 | }); |
| 1226 | if (block.y <= top + 1e-9) |
| 1227 | { |
| 1228 | hit = true; |
| 1229 | var need = top - block.y; |
| 1230 | if (need > shift) |
| 1231 | { shift = need; } |
| 1232 | } |
| 1233 | } |
| 1234 | if (!hit) |
| 1235 | { return; } |
| 1236 | // Lift the whole rigid body so the deepest contact sits flush. |
| 1237 | for (var i = 0; i < members.length; i++) |
| 1238 | { members[i].y += shift; } |
| 1239 | this.landCluster(cluster, members); |
| 1240 | }; |
| 1241 | |
| 1242 | /** |
| 1243 | * Land as a rigid group: relative positions are kept, so part of the |
| 1244 | * cluster may hang above a gap. Grey members start the 3s settle timer; |
| 1245 | * coloured cargo stays put with them until every grey has recolored. |
| 1246 | * Members that actually rest on the floor or on another block bounce. |
| 1247 | */ |
| 1248 | GameCore.prototype.landCluster = function (cluster, members) |
| 1249 | { |
| 1250 | for (var i = 0; i < members.length; i++) |
| 1251 | { |
| 1252 | var block = members[i]; |
| 1253 | block.vy = 0; |
| 1254 | block.flame = false; |
| 1255 | block.clusterId = cluster.id; |
| 1256 | if (block.color === COLOR_GRAY) |
| 1257 | { |
| 1258 | block.state = STATE_SETTLING; |
| 1259 | block.settleTime = 0; |
| 1260 | } |
| 1261 | else |
| 1262 | { |
| 1263 | // Cargo stays in the rigid group (not matchable) until release. |
| 1264 | block.state = STATE_SETTLING; |
| 1265 | block.settleTime = 0; |
| 1266 | } |
| 1267 | if (this.hasSupport(block)) |
| 1268 | { this.startBounce(block); } |
| 1269 | } |
| 1270 | cluster.phase = 'landed'; |
| 1271 | cluster.vy = 0; |
| 1272 | }; |
| 1273 | |
| 1274 | /** |
| 1275 | * After every grey in a landed cluster has recolored, let the group go. |
| 1276 | * Unsupported blocks fall individually from rest; supported ones stay. |
| 1277 | */ |
| 1278 | GameCore.prototype.releaseLandedCluster = function (cluster) |
| 1279 | { |
| 1280 | var members = this.clusterMembers(cluster.id); |
| 1281 | var releasing = Object.create(null); |
| 1282 | for (var i = 0; i < members.length; i++) |
| 1283 | { releasing[members[i].id] = true; } |
| 1284 | |
| 1285 | for (var i = 0; i < members.length; i++) |
| 1286 | { |
| 1287 | var block = members[i]; |
| 1288 | if (block.color === COLOR_GRAY) |
| 1289 | { |
| 1290 | block.color = this.randomColor(); |
| 1291 | block.settleTime = 0; |
| 1292 | } |
| 1293 | this.leaveCluster(block); |
| 1294 | // Always drop leftover launch / manual velocity. A stale upward |
| 1295 | // vy here is what sends blocks off the top "very fast" after |
| 1296 | // a grey group recolors. |
| 1297 | this.clearFlightState(block); |
| 1298 | // Support from outside the group (floor or a foreign block). Fellow |
| 1299 | // members that will also fall do not count, so a hanging stack |
| 1300 | // drops together without the top one staying put. |
| 1301 | var supported = this.hasSupportOutside(block, releasing); |
| 1302 | block.state = supported ? STATE_STILL : STATE_FALLING; |
| 1303 | } |
| 1304 | delete this.clusters[cluster.id]; |
| 1305 | }; |
| 1306 | |
| 1307 | GameCore.prototype.hasSupportOutside = function (block, ignoreIds) |
| 1308 | { |
| 1309 | if (block.y <= 1e-9) |
| 1310 | { return true; } |
| 1311 | for (var i = 0; i < this.blocks.length; i++) |
| 1312 | { |
| 1313 | var other = this.blocks[i]; |
| 1314 | if (other === block || other.col !== block.col) |
| 1315 | { continue; } |
| 1316 | if (ignoreIds && ignoreIds[other.id]) |
| 1317 | { continue; } |
| 1318 | if (!this.isGrounded(other)) |
| 1319 | { continue; } |
| 1320 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 1321 | { return true; } |
| 1322 | } |
| 1323 | return false; |
| 1324 | }; |
| 1325 | |
| 1326 | GameCore.prototype.integrateManual = function (dt) |
| 1327 | { |
| 1328 | for (var i = 0; i < this.blocks.length; i++) |
| 1329 | { |
| 1330 | var block = this.blocks[i]; |
| 1331 | if (block.state !== STATE_MANUAL) |
| 1332 | { continue; } |
| 1333 | |
| 1334 | if (block.manualPhase === 'up') |
| 1335 | { |
| 1336 | var oldY = block.y; |
| 1337 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 1338 | var dy = this.advanceJump(proxy, dt); |
| 1339 | var nextY = oldY + dy; |
| 1340 | this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget)); |
| 1341 | block.vy = proxy.vy; |
| 1342 | block.manualPhase = proxy.phase; |
| 1343 | block.y = Math.min(nextY, block.manualTarget); |
| 1344 | if (block.y >= block.manualTarget - 1e-12) |
| 1345 | { |
| 1346 | block.y = block.manualTarget; |
| 1347 | block.vy = 0; |
| 1348 | block.manualPhase = 'down'; |
| 1349 | } |
| 1350 | if (block.y > this.height - 1) |
| 1351 | { |
| 1352 | block.y = this.height - 1; |
| 1353 | block.vy = 0; |
| 1354 | block.manualPhase = 'down'; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | if (block.manualPhase === 'down') |
| 1359 | { |
| 1360 | var proxy = { phase: 'down', vy: block.vy, decel: 0 }; |
| 1361 | var dy = this.advanceJump(proxy, dt); |
| 1362 | block.vy = proxy.vy; |
| 1363 | var nextY = block.y + dy; |
| 1364 | var top = this.stackTop(block.col); |
| 1365 | if (nextY <= top) |
| 1366 | { |
| 1367 | block.y = top; |
| 1368 | block.state = STATE_STILL; |
| 1369 | block.vy = 0; |
| 1370 | block.manualPhase = null; |
| 1371 | block.flame = false; |
| 1372 | this.startBounce(block); |
| 1373 | } |
| 1374 | else |
| 1375 | { |
| 1376 | block.y = Math.min(nextY, this.height - 1); |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | }; |
| 1381 | |
| 1382 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 1383 | { |
| 1384 | if (!manual.pushedClusters) |
| 1385 | { manual.pushedClusters = Object.create(null); } |
| 1386 | |
| 1387 | var manualTopOld = oldY + 1; |
| 1388 | var manualTopNew = nextY + 1; |
| 1389 | var low = Math.min(oldY, nextY); |
| 1390 | var high = Math.max(oldY, nextY) + 1; |
| 1391 | |
| 1392 | for (var i = 0; i < this.blocks.length; i++) |
| 1393 | { |
| 1394 | var other = this.blocks[i]; |
| 1395 | if (other === manual) |
| 1396 | { continue; } |
| 1397 | if (other.col !== manual.col) |
| 1398 | { continue; } |
| 1399 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 1400 | { continue; } |
| 1401 | if (manual.pushedClusters[other.clusterId]) |
| 1402 | { continue; } |
| 1403 | var otherBot = other.y; |
| 1404 | var otherTop = other.y + 1; |
| 1405 | var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9; |
| 1406 | var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9; |
| 1407 | if (overlapping || crossing) |
| 1408 | { |
| 1409 | if (this.pushClusterColumn(other.clusterId, manual.col)) |
| 1410 | { |
| 1411 | manual.pushedClusters[other.clusterId] = true; |
| 1412 | return; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | }; |
| 1417 | |
| 1418 | |
| 1419 | /** |
| 1420 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 1421 | * Other columns stay put (they remain linked). Then look for a new |
| 1422 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 1423 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 1424 | * with the same jump height from its current position. |
| 1425 | */ |
| 1426 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 1427 | { |
| 1428 | var cluster = this.clusters[clusterId]; |
| 1429 | if (!cluster) |
| 1430 | { return false; } |
| 1431 | var members = this.clusterMembers(clusterId); |
| 1432 | for (var i = 0; i < members.length; i++) |
| 1433 | { |
| 1434 | if (members[i].col === col) |
| 1435 | { members[i].y += 1; } |
| 1436 | } |
| 1437 | this.cullOffTop(); |
| 1438 | if (!this.clusters[clusterId]) |
| 1439 | { return true; } |
| 1440 | this.checkChainRelaunch(cluster); |
| 1441 | return true; |
| 1442 | }; |
| 1443 | |
| 1444 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 1445 | { |
| 1446 | if (!this.clusters[cluster.id]) |
| 1447 | { return; } |
| 1448 | var lines = this.findFlyingMatchLines(cluster.id); |
| 1449 | if (!lines.length) |
| 1450 | { return; } |
| 1451 | var newly = []; |
| 1452 | var seen = Object.create(null); |
| 1453 | for (var i = 0; i < lines.length; i++) |
| 1454 | { |
| 1455 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 1456 | { |
| 1457 | var block = lines[i].blocks[j]; |
| 1458 | if (seen[block.id]) |
| 1459 | { continue; } |
| 1460 | seen[block.id] = true; |
| 1461 | newly.push(block); |
| 1462 | } |
| 1463 | } |
| 1464 | for (var i = 0; i < newly.length; i++) |
| 1465 | { |
| 1466 | var block = newly[i]; |
| 1467 | block.color = COLOR_GRAY; |
| 1468 | block.flame = true; |
| 1469 | block.clusterId = cluster.id; |
| 1470 | block.state = STATE_FLYING; |
| 1471 | } |
| 1472 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 1473 | }; |
| 1474 | |
| 1475 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 1476 | { |
| 1477 | cluster.jumpHeight = height; |
| 1478 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1479 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 1480 | cluster.phase = 'up'; |
| 1481 | var members = this.clusterMembers(cluster.id); |
| 1482 | for (var i = 0; i < members.length; i++) |
| 1483 | { |
| 1484 | members[i].vy = cluster.vy; |
| 1485 | members[i].state = STATE_FLYING; |
| 1486 | if (members[i].color === COLOR_GRAY) |
| 1487 | { members[i].flame = true; } |
| 1488 | } |
| 1489 | }; |
| 1490 | |
| 1491 | GameCore.prototype.startManualLaunch = function (block) |
| 1492 | { |
| 1493 | var target = this.height - 1; |
| 1494 | var height = Math.max(0.05, target - block.y); |
| 1495 | block.state = STATE_MANUAL; |
| 1496 | block.pushedClusters = Object.create(null); |
| 1497 | block.manualTarget = target; |
| 1498 | block.manualPhase = 'up'; |
| 1499 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1500 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 1501 | block.flame = false; |
| 1502 | if (this.drag && this.drag.blockId === block.id) |
| 1503 | { this.drag = null; } |
| 1504 | }; |
| 1505 | |
| 1506 | GameCore.prototype.cullOffTop = function () |
| 1507 | { |
| 1508 | var gone = []; |
| 1509 | for (var i = 0; i < this.blocks.length; i++) |
| 1510 | { |
| 1511 | var block = this.blocks[i]; |
| 1512 | if (block.state === STATE_MANUAL) |
| 1513 | { |
| 1514 | if (block.y > this.height - 1) |
| 1515 | { block.y = this.height - 1; } |
| 1516 | continue; |
| 1517 | } |
| 1518 | if (block.y >= this.height - 1e-9) |
| 1519 | { gone.push(block); } |
| 1520 | } |
| 1521 | for (var i = 0; i < gone.length; i++) |
| 1522 | { |
| 1523 | var block = gone[i]; |
| 1524 | this.outgoingShots.push({ |
| 1525 | id: block.id, |
| 1526 | color: block.color, |
| 1527 | col: block.col, |
| 1528 | y: block.y |
| 1529 | }); |
| 1530 | this.score += 1; |
| 1531 | this.removeBlock(block); |
| 1532 | } |
| 1533 | }; |
| 1534 | |
| 1535 | GameCore.prototype.consumeOutgoingShots = function () |
| 1536 | { |
| 1537 | var shots = this.outgoingShots; |
| 1538 | this.outgoingShots = []; |
| 1539 | return shots; |
| 1540 | }; |
| 1541 | |
| 1542 | /** |
| 1543 | * A compact field picture for the multiplayer snapshot (miniatures). |
| 1544 | * Includes every block's col, y, color, state and flame. |
| 1545 | */ |
| 1546 | GameCore.prototype.fieldSnapshot = function () |
| 1547 | { |
| 1548 | var blocks = []; |
| 1549 | for (var i = 0; i < this.blocks.length; i++) |
| 1550 | { |
| 1551 | var b = this.blocks[i]; |
| 1552 | blocks.push({ |
| 1553 | id: b.id, |
| 1554 | col: b.col, |
| 1555 | y: Math.round(b.y * 1000) / 1000, |
| 1556 | color: b.color, |
| 1557 | state: b.state, |
| 1558 | flame: !!b.flame |
| 1559 | }); |
| 1560 | } |
| 1561 | return { |
| 1562 | score: this.score, |
| 1563 | state: this.state, |
| 1564 | width: this.width, |
| 1565 | height: this.height, |
| 1566 | locked: this.lockedColumnCount(), |
| 1567 | blocks: blocks |
| 1568 | }; |
| 1569 | }; |
| 1570 | |
| 1571 | /** |
| 1572 | * Incoming garbage from another player: a dark-grey block that falls |
| 1573 | * normally from the top of an open column. After landing it uses the |
| 1574 | * usual 3s settle and then a random colour (landFallingBlock). |
| 1575 | * `col` is optional; a random eligible column is used otherwise. |
| 1576 | * Returns the new block, or null if every column is locked or full. |
| 1577 | */ |
| 1578 | GameCore.prototype.ingestGarbage = function (col) |
| 1579 | { |
| 1580 | if (this.state !== 'playing') |
| 1581 | { return null; } |
| 1582 | var chosen = null; |
| 1583 | if (col != null && col >= 0 && col < this.width |
| 1584 | && !this.isColumnLocked(col) && this.groundedCount(col) < this.height) |
| 1585 | { chosen = col | 0; } |
| 1586 | else |
| 1587 | { |
| 1588 | var eligible = []; |
| 1589 | for (var c = 0; c < this.width; c++) |
| 1590 | { |
| 1591 | if (this.isColumnLocked(c)) |
| 1592 | { continue; } |
| 1593 | if (this.groundedCount(c) >= this.height) |
| 1594 | { continue; } |
| 1595 | eligible.push(c); |
| 1596 | } |
| 1597 | if (!eligible.length) |
| 1598 | { return null; } |
| 1599 | chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 1600 | } |
| 1601 | var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING); |
| 1602 | block.vy = -this.planet.fallSpeed; |
| 1603 | block.flame = false; |
| 1604 | return block; |
| 1605 | }; |
| 1606 | |
| 1607 | GameCore.prototype.advanceSettle = function (dt) |
| 1608 | { |
| 1609 | for (var i = 0; i < this.blocks.length; i++) |
| 1610 | { |
| 1611 | var block = this.blocks[i]; |
| 1612 | if (block.state !== STATE_SETTLING) |
| 1613 | { continue; } |
| 1614 | var inLanded = block.clusterId != null |
| 1615 | && this.clusters[block.clusterId] |
| 1616 | && this.clusters[block.clusterId].phase === 'landed'; |
| 1617 | if (block.color === COLOR_GRAY) |
| 1618 | { |
| 1619 | block.settleTime += dt; |
| 1620 | if (block.settleTime >= this.settleTime) |
| 1621 | { |
| 1622 | block.color = this.randomColor(); |
| 1623 | block.settleTime = 0; |
| 1624 | block.flame = false; |
| 1625 | if (inLanded) |
| 1626 | { |
| 1627 | // Stay in the rigid group; releaseLandedCluster runs below. |
| 1628 | block.state = STATE_SETTLING; |
| 1629 | } |
| 1630 | else |
| 1631 | { |
| 1632 | this.leaveCluster(block); |
| 1633 | this.clearFlightState(block); |
| 1634 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | else if (!inLanded) |
| 1639 | { |
| 1640 | // Coloured settling that is not in a landed cluster (shouldn't |
| 1641 | // normally happen): just become still or fall, from rest. |
| 1642 | this.leaveCluster(block); |
| 1643 | this.clearFlightState(block); |
| 1644 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | var ids = Object.keys(this.clusters); |
| 1649 | for (var n = 0; n < ids.length; n++) |
| 1650 | { |
| 1651 | var cluster = this.clusters[ids[n]]; |
| 1652 | if (!cluster || cluster.phase !== 'landed') |
| 1653 | { continue; } |
| 1654 | var members = this.clusterMembers(cluster.id); |
| 1655 | var greySettling = false; |
| 1656 | for (var i = 0; i < members.length; i++) |
| 1657 | { |
| 1658 | if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING) |
| 1659 | { greySettling = true; } |
| 1660 | } |
| 1661 | if (!greySettling) |
| 1662 | { this.releaseLandedCluster(cluster); } |
| 1663 | } |
| 1664 | }; |
| 1665 | |
| 1666 | GameCore.prototype.findMatchLines = function () |
| 1667 | { |
| 1668 | var candidates = []; |
| 1669 | for (var i = 0; i < this.blocks.length; i++) |
| 1670 | { |
| 1671 | var block = this.blocks[i]; |
| 1672 | if (block.state === STATE_STILL && block.color >= 0) |
| 1673 | { candidates.push(block); } |
| 1674 | } |
| 1675 | return this.collectLines(candidates, true); |
| 1676 | }; |
| 1677 | |
| 1678 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 1679 | { |
| 1680 | var candidates = []; |
| 1681 | for (var i = 0; i < this.blocks.length; i++) |
| 1682 | { |
| 1683 | var block = this.blocks[i]; |
| 1684 | if (block.clusterId === clusterId && block.color >= 0 |
| 1685 | && (block.state === STATE_FLYING)) |
| 1686 | { candidates.push(block); } |
| 1687 | } |
| 1688 | return this.collectLines(candidates, false); |
| 1689 | }; |
| 1690 | |
| 1691 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 1692 | { |
| 1693 | var lines = []; |
| 1694 | if (!candidates.length) |
| 1695 | { return lines; } |
| 1696 | |
| 1697 | var byRow = Object.create(null); |
| 1698 | for (var i = 0; i < candidates.length; i++) |
| 1699 | { |
| 1700 | var block = candidates[i]; |
| 1701 | var row = Math.round(block.y); |
| 1702 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 1703 | { continue; } |
| 1704 | if (!byRow[row]) |
| 1705 | { byRow[row] = []; } |
| 1706 | byRow[row].push(block); |
| 1707 | } |
| 1708 | var rows = Object.keys(byRow); |
| 1709 | for (var r = 0; r < rows.length; r++) |
| 1710 | { |
| 1711 | var list = byRow[rows[r]]; |
| 1712 | list.sort(function (a, b) { return a.col - b.col; }); |
| 1713 | this.pushRuns(lines, list, false, function (a, b) |
| 1714 | { |
| 1715 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 1716 | }); |
| 1717 | } |
| 1718 | |
| 1719 | var byCol = Object.create(null); |
| 1720 | for (var i = 0; i < candidates.length; i++) |
| 1721 | { |
| 1722 | var block = candidates[i]; |
| 1723 | if (!byCol[block.col]) |
| 1724 | { byCol[block.col] = []; } |
| 1725 | byCol[block.col].push(block); |
| 1726 | } |
| 1727 | var cols = Object.keys(byCol); |
| 1728 | for (var c = 0; c < cols.length; c++) |
| 1729 | { |
| 1730 | var list = byCol[cols[c]]; |
| 1731 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1732 | this.pushRuns(lines, list, true, function (a, b) |
| 1733 | { |
| 1734 | return a.color === b.color && a.color >= 0 |
| 1735 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 1736 | }); |
| 1737 | } |
| 1738 | return lines; |
| 1739 | }; |
| 1740 | |
| 1741 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 1742 | { |
| 1743 | var start = 0; |
| 1744 | for (var i = 1; i <= list.length; i++) |
| 1745 | { |
| 1746 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 1747 | if (!same) |
| 1748 | { |
| 1749 | var length = i - start; |
| 1750 | if (length >= 3) |
| 1751 | { |
| 1752 | lines.push({ |
| 1753 | blocks: list.slice(start, i), |
| 1754 | length: length, |
| 1755 | vertical: vertical |
| 1756 | }); |
| 1757 | } |
| 1758 | start = i; |
| 1759 | } |
| 1760 | } |
| 1761 | }; |
| 1762 | |
| 1763 | GameCore.prototype.resolveMatches = function () |
| 1764 | { |
| 1765 | if (this.state !== 'playing' || this.drag) |
| 1766 | { return []; } |
| 1767 | var lines = this.findMatchLines(); |
| 1768 | if (!lines.length) |
| 1769 | { return []; } |
| 1770 | |
| 1771 | var uf = new UnionFind(); |
| 1772 | var blockById = Object.create(null); |
| 1773 | for (var i = 0; i < lines.length; i++) |
| 1774 | { |
| 1775 | var line = lines[i]; |
| 1776 | for (var j = 0; j < line.blocks.length; j++) |
| 1777 | { |
| 1778 | uf.add(line.blocks[j].id); |
| 1779 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 1780 | if (j > 0) |
| 1781 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 1782 | } |
| 1783 | } |
| 1784 | var matched = []; |
| 1785 | var seen = Object.create(null); |
| 1786 | for (var id in blockById) |
| 1787 | { |
| 1788 | if (!seen[id]) |
| 1789 | { |
| 1790 | seen[id] = true; |
| 1791 | matched.push(blockById[id]); |
| 1792 | } |
| 1793 | } |
| 1794 | for (var i = 0; i < matched.length; i++) |
| 1795 | { |
| 1796 | for (var j = i + 1; j < matched.length; j++) |
| 1797 | { |
| 1798 | if (matched[i].col === matched[j].col) |
| 1799 | { uf.union(matched[i].id, matched[j].id); } |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | var groups = Object.create(null); |
| 1804 | for (var i = 0; i < matched.length; i++) |
| 1805 | { |
| 1806 | var root = uf.find(matched[i].id); |
| 1807 | if (!groups[root]) |
| 1808 | { groups[root] = []; } |
| 1809 | groups[root].push(matched[i]); |
| 1810 | } |
| 1811 | |
| 1812 | var launched = []; |
| 1813 | for (var root in groups) |
| 1814 | { |
| 1815 | var group = groups[root]; |
| 1816 | var height = 0; |
| 1817 | for (var i = 0; i < lines.length; i++) |
| 1818 | { |
| 1819 | var line = lines[i]; |
| 1820 | var overlaps = false; |
| 1821 | for (var j = 0; j < line.blocks.length; j++) |
| 1822 | { |
| 1823 | for (var k = 0; k < group.length; k++) |
| 1824 | { |
| 1825 | if (line.blocks[j].id === group[k].id) |
| 1826 | { overlaps = true; } |
| 1827 | } |
| 1828 | } |
| 1829 | if (overlaps) |
| 1830 | { |
| 1831 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 1832 | if (h > height) |
| 1833 | { height = h; } |
| 1834 | } |
| 1835 | } |
| 1836 | this.launchCluster(group, height); |
| 1837 | launched.push({ blocks: group, jumpHeight: height }); |
| 1838 | } |
| 1839 | return launched; |
| 1840 | }; |
| 1841 | |
| 1842 | /** |
| 1843 | * Gray the matched blocks, take everything sitting on top of them in |
| 1844 | * those columns, link them, and launch with jump height `height`. |
| 1845 | */ |
| 1846 | GameCore.prototype.launchCluster = function (matchedBlocks, height) |
| 1847 | { |
| 1848 | var lowestInCol = Object.create(null); |
| 1849 | var matchedIds = Object.create(null); |
| 1850 | for (var i = 0; i < matchedBlocks.length; i++) |
| 1851 | { |
| 1852 | var block = matchedBlocks[i]; |
| 1853 | matchedIds[block.id] = true; |
| 1854 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 1855 | { lowestInCol[block.col] = block.y; } |
| 1856 | } |
| 1857 | |
| 1858 | var clusterId = this.nextClusterId++; |
| 1859 | var cluster = { |
| 1860 | id: clusterId, |
| 1861 | jumpHeight: height, |
| 1862 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 1863 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 1864 | phase: 'up' |
| 1865 | }; |
| 1866 | this.clusters[clusterId] = cluster; |
| 1867 | |
| 1868 | var riders = []; |
| 1869 | var cols = Object.keys(lowestInCol); |
| 1870 | for (var c = 0; c < cols.length; c++) |
| 1871 | { |
| 1872 | var col = +cols[c]; |
| 1873 | var lowest = lowestInCol[col]; |
| 1874 | var list = this.blocksInColumn(col); |
| 1875 | for (var i = 0; i < list.length; i++) |
| 1876 | { |
| 1877 | var block = list[i]; |
| 1878 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 1879 | { |
| 1880 | if (block.state !== STATE_SETTLING) |
| 1881 | { continue; } |
| 1882 | } |
| 1883 | if (block.y + ALIGN_EPS >= lowest) |
| 1884 | { riders.push(block); } |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | for (var i = 0; i < riders.length; i++) |
| 1889 | { |
| 1890 | var block = riders[i]; |
| 1891 | var isMatch = !!matchedIds[block.id]; |
| 1892 | if (isMatch || block.color === COLOR_GRAY) |
| 1893 | { |
| 1894 | block.color = COLOR_GRAY; |
| 1895 | block.flame = true; |
| 1896 | } |
| 1897 | else |
| 1898 | { block.flame = false; } |
| 1899 | block.state = STATE_FLYING; |
| 1900 | block.clusterId = clusterId; |
| 1901 | block.vy = cluster.vy; |
| 1902 | block.settleTime = 0; |
| 1903 | } |
| 1904 | return cluster; |
| 1905 | }; |
| 1906 | |
| 1907 | GameCore.prototype.checkLocks = function () |
| 1908 | { |
| 1909 | for (var col = 0; col < this.width; col++) |
| 1910 | { |
| 1911 | if (this.isColumnLocked(col)) |
| 1912 | { continue; } |
| 1913 | if (this.groundedCount(col) < this.height) |
| 1914 | { continue; } |
| 1915 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 1916 | var filled = true; |
| 1917 | for (var row = 0; row < this.height; row++) |
| 1918 | { |
| 1919 | var block = this.blockAt(col, row); |
| 1920 | if (!block || !this.isGrounded(block)) |
| 1921 | { |
| 1922 | filled = false; |
| 1923 | break; |
| 1924 | } |
| 1925 | } |
| 1926 | if (!filled) |
| 1927 | { continue; } |
| 1928 | var list = this.blocksInColumn(col); |
| 1929 | for (var i = 0; i < list.length; i++) |
| 1930 | { |
| 1931 | var block = list[i]; |
| 1932 | if (!this.isGrounded(block)) |
| 1933 | { continue; } |
| 1934 | block.state = STATE_LOCKED; |
| 1935 | block.color = COLOR_GRAY; |
| 1936 | block.flame = false; |
| 1937 | block.vy = 0; |
| 1938 | block.settleTime = 0; |
| 1939 | if (block.clusterId != null) |
| 1940 | { this.leaveCluster(block); } |
| 1941 | } |
| 1942 | } |
| 1943 | }; |
| 1944 | |
| 1945 | GameCore.prototype.checkGameOver = function () |
| 1946 | { |
| 1947 | if (this.state !== 'playing') |
| 1948 | { return; } |
| 1949 | for (var col = 0; col < this.width; col++) |
| 1950 | { |
| 1951 | if (!this.isColumnLocked(col)) |
| 1952 | { return; } |
| 1953 | } |
| 1954 | this.state = 'gameover'; |
| 1955 | this.gameOverTime = 0; |
| 1956 | this.drag = null; |
| 1957 | }; |
| 1958 | |
| 1959 | /* ---------- dragging -------------------------------------------------- */ |
| 1960 | |
| 1961 | GameCore.prototype.beginDrag = function (col, y) |
| 1962 | { |
| 1963 | if (this.state !== 'playing') |
| 1964 | { return false; } |
| 1965 | var block = this.snapDragBlock(col, y); |
| 1966 | if (!block) |
| 1967 | { return false; } |
| 1968 | var top = this.columnStackTopRow(block.col); |
| 1969 | var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 1970 | this.drag = { |
| 1971 | blockId: block.id, |
| 1972 | pointerY: y, |
| 1973 | startY: y, |
| 1974 | accum: 0, |
| 1975 | launchIntent: false, |
| 1976 | startedAsTop: startedAsTop |
| 1977 | }; |
| 1978 | return true; |
| 1979 | }; |
| 1980 | |
| 1981 | /** |
| 1982 | * Instant grab: the nearest still, coloured block to (col, y), allowing |
| 1983 | * a small vertical miss and a horizontal slop into a neighbouring |
| 1984 | * column. `col` may be fractional (pointer x in tiles). No hold delay. |
| 1985 | */ |
| 1986 | GameCore.prototype.snapDragBlock = function (col, y) |
| 1987 | { |
| 1988 | var raw = col; |
| 1989 | var c0 = Math.floor(raw + 1e-9); |
| 1990 | var frac = raw - c0; |
| 1991 | var candidates = []; |
| 1992 | if (c0 >= 0 && c0 < this.width) |
| 1993 | { candidates.push(c0); } |
| 1994 | // Only consult a neighbour when the pointer is actually near the edge |
| 1995 | // (a whole-number col, as in unit tests, stays in that column). |
| 1996 | if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0) |
| 1997 | { candidates.push(c0 - 1); } |
| 1998 | if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width) |
| 1999 | { candidates.push(c0 + 1); } |
| 2000 | |
| 2001 | var best = null; |
| 2002 | var bestDist = Infinity; |
| 2003 | for (var c = 0; c < candidates.length; c++) |
| 2004 | { |
| 2005 | var cc = candidates[c]; |
| 2006 | if (!this.isColumnStill(cc)) |
| 2007 | { continue; } |
| 2008 | var list = this.blocksInColumn(cc); |
| 2009 | for (var i = 0; i < list.length; i++) |
| 2010 | { |
| 2011 | var block = list[i]; |
| 2012 | if (block.state !== STATE_STILL || block.color < 0) |
| 2013 | { continue; } |
| 2014 | var dist = this.pointerDistToBlock(block, raw, y); |
| 2015 | // Prefer the column the pointer is actually in on a tie. |
| 2016 | if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0)) |
| 2017 | { |
| 2018 | bestDist = dist; |
| 2019 | best = block; |
| 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | if (!best || bestDist > SNAP_Y) |
| 2024 | { return null; } |
| 2025 | return best; |
| 2026 | }; |
| 2027 | |
| 2028 | GameCore.prototype.pointerDistToBlock = function (block, col, y) |
| 2029 | { |
| 2030 | var dx; |
| 2031 | if (col >= block.col && col < block.col + 1) |
| 2032 | { dx = 0; } |
| 2033 | else if (col < block.col) |
| 2034 | { dx = block.col - col; } |
| 2035 | else |
| 2036 | { dx = col - (block.col + 1); } |
| 2037 | var dy; |
| 2038 | if (y >= block.y && y < block.y + 1) |
| 2039 | { dy = 0; } |
| 2040 | else if (y < block.y) |
| 2041 | { dy = block.y - y; } |
| 2042 | else |
| 2043 | { dy = y - (block.y + 1); } |
| 2044 | return Math.sqrt(dx * dx + dy * dy); |
| 2045 | }; |
| 2046 | |
| 2047 | GameCore.prototype.moveDrag = function (y) |
| 2048 | { |
| 2049 | if (!this.drag) |
| 2050 | { return; } |
| 2051 | var block = this.blockById(this.drag.blockId); |
| 2052 | if (!block || block.state !== STATE_STILL) |
| 2053 | { |
| 2054 | this.drag = null; |
| 2055 | return; |
| 2056 | } |
| 2057 | if (!this.isColumnStill(block.col)) |
| 2058 | { |
| 2059 | this.drag = null; |
| 2060 | return; |
| 2061 | } |
| 2062 | var dy = y - this.drag.pointerY; |
| 2063 | this.drag.pointerY = y; |
| 2064 | if (this.drag.accum == null) |
| 2065 | { this.drag.accum = 0; } |
| 2066 | this.drag.accum += dy; |
| 2067 | // One swap per tile of travel, triggered after a minimal SWAP_DISTANCE. |
| 2068 | // A single large move (fast swipe) walks every tile; none are skipped. |
| 2069 | var guard = 0; |
| 2070 | while (guard++ < this.height + 2 |
| 2071 | && (this.drag.accum >= SWAP_DISTANCE || this.drag.accum <= -SWAP_DISTANCE)) |
| 2072 | { |
| 2073 | var dir = this.drag.accum > 0 ? 1 : -1; |
| 2074 | if (!this.swapOne(block, dir)) |
| 2075 | { |
| 2076 | if (dir > 0) |
| 2077 | { this.drag.accum = Math.min(this.drag.accum, SWAP_DISTANCE - 1e-9); } |
| 2078 | else |
| 2079 | { this.drag.accum = Math.max(this.drag.accum, -(SWAP_DISTANCE - 1e-9)); } |
| 2080 | break; |
| 2081 | } |
| 2082 | this.drag.accum -= dir; |
| 2083 | // A short move (SWAP_DISTANCE < 1) leaves a negative leftover; |
| 2084 | // do not let that immediately swap back the other way. |
| 2085 | if ((dir > 0 && this.drag.accum < 0) || (dir < 0 && this.drag.accum > 0)) |
| 2086 | { this.drag.accum = 0; } |
| 2087 | if (!this.isColumnStill(block.col)) |
| 2088 | { |
| 2089 | this.drag = null; |
| 2090 | return; |
| 2091 | } |
| 2092 | } |
| 2093 | var top = this.columnStackTopRow(block.col); |
| 2094 | var isTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 2095 | if (this.drag.startedAsTop) |
| 2096 | { |
| 2097 | // Already the topmost block: pulling upward past its own position |
| 2098 | // (LAUNCH_PULL tiles above the press point) starts a manual jump. |
| 2099 | this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL); |
| 2100 | } |
| 2101 | else |
| 2102 | { |
| 2103 | // Started mid-stack: swap first, and only launch once the pointer |
| 2104 | // is pulled into the empty space above the (new) top. |
| 2105 | this.drag.launchIntent = isTop && (y > top + 1 + 0.25); |
| 2106 | } |
| 2107 | }; |
| 2108 | |
| 2109 | /** Swap `block` one tile up (dir=+1) or down (dir=-1). Returns true if swapped. */ |
| 2110 | GameCore.prototype.swapOne = function (block, dir) |
| 2111 | { |
| 2112 | var neighbor = this.blockAt(block.col, block.y + dir); |
| 2113 | if (neighbor && this.isGrounded(neighbor) && neighbor !== block) |
| 2114 | { |
| 2115 | this.swapBlocks(block, neighbor); |
| 2116 | return true; |
| 2117 | } |
| 2118 | return false; |
| 2119 | }; |
| 2120 | |
| 2121 | GameCore.prototype.endDrag = function () |
| 2122 | { |
| 2123 | if (!this.drag) |
| 2124 | { return; } |
| 2125 | var block = this.blockById(this.drag.blockId); |
| 2126 | var launch = this.drag.launchIntent; |
| 2127 | this.drag = null; |
| 2128 | if (launch && block && block.state === STATE_STILL) |
| 2129 | { this.startManualLaunch(block); } |
| 2130 | else |
| 2131 | { this.resolveMatches(); } |
| 2132 | }; |
| 2133 | |
| 2134 | GameCore.prototype.blockById = function (id) |
| 2135 | { |
| 2136 | for (var i = 0; i < this.blocks.length; i++) |
| 2137 | { |
| 2138 | if (this.blocks[i].id === id) |
| 2139 | { return this.blocks[i]; } |
| 2140 | } |
| 2141 | return null; |
| 2142 | }; |
| 2143 | |
| 2144 | GameCore.prototype.columnStackTopRow = function (col) |
| 2145 | { |
| 2146 | var top = -1; |
| 2147 | var list = this.blocksInColumn(col); |
| 2148 | for (var i = 0; i < list.length; i++) |
| 2149 | { |
| 2150 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 2151 | { top = list[i].y; } |
| 2152 | } |
| 2153 | return top; |
| 2154 | }; |
| 2155 | |
| 2156 | GameCore.prototype.swapToward = function (block, targetY) |
| 2157 | { |
| 2158 | var guard = 0; |
| 2159 | while (guard++ < this.height + 2) |
| 2160 | { |
| 2161 | var current = block.y; |
| 2162 | if (targetY > current + 0.5) |
| 2163 | { |
| 2164 | var above = this.blockAt(block.col, current + 1); |
| 2165 | if (above && this.isGrounded(above) && above !== block) |
| 2166 | { this.swapBlocks(block, above); } |
| 2167 | else |
| 2168 | { break; } |
| 2169 | } |
| 2170 | else if (targetY < current - 0.5) |
| 2171 | { |
| 2172 | var below = this.blockAt(block.col, current - 1); |
| 2173 | if (below && this.isGrounded(below) && below !== block) |
| 2174 | { this.swapBlocks(block, below); } |
| 2175 | else |
| 2176 | { break; } |
| 2177 | } |
| 2178 | else |
| 2179 | { break; } |
| 2180 | } |
| 2181 | }; |
| 2182 | |
| 2183 | GameCore.prototype.swapBlocks = function (a, b) |
| 2184 | { |
| 2185 | var y = a.y; |
| 2186 | a.y = b.y; |
| 2187 | b.y = y; |
| 2188 | }; |
| 2189 | |
| 2190 | /** |
| 2191 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 2192 | * Returns the moved block, or null if the column is not still. |
| 2193 | */ |
| 2194 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 2195 | { |
| 2196 | if (!this.isColumnStill(col)) |
| 2197 | { return null; } |
| 2198 | var block = this.blockAt(col, fromRow); |
| 2199 | if (!block) |
| 2200 | { return null; } |
| 2201 | this.swapToward(block, toRow); |
| 2202 | return block; |
| 2203 | }; |
| 2204 | |
| 2205 | /** |
| 2206 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 2207 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 2208 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 2209 | * their color with a marker in `flying`. |
| 2210 | */ |
| 2211 | GameCore.prototype.snapshot = function () |
| 2212 | { |
| 2213 | var grid = []; |
| 2214 | for (var r = 0; r < this.height; r++) |
| 2215 | { |
| 2216 | var row = []; |
| 2217 | for (var c = 0; c < this.width; c++) |
| 2218 | { row.push('.'); } |
| 2219 | grid.push(row); |
| 2220 | } |
| 2221 | var airborne = []; |
| 2222 | for (var i = 0; i < this.blocks.length; i++) |
| 2223 | { |
| 2224 | var block = this.blocks[i]; |
| 2225 | var ch; |
| 2226 | if (block.state === STATE_LOCKED) |
| 2227 | { ch = 'L'; } |
| 2228 | else if (block.color === COLOR_GRAY) |
| 2229 | { ch = 'g'; } |
| 2230 | else |
| 2231 | { ch = String(block.color); } |
| 2232 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 2233 | { |
| 2234 | var row = Math.round(block.y); |
| 2235 | if (row >= 0 && row < this.height) |
| 2236 | { grid[row][block.col] = ch; } |
| 2237 | } |
| 2238 | else |
| 2239 | { |
| 2240 | airborne.push({ |
| 2241 | id: block.id, |
| 2242 | col: block.col, |
| 2243 | y: block.y, |
| 2244 | color: block.color, |
| 2245 | state: block.state, |
| 2246 | clusterId: block.clusterId, |
| 2247 | flame: block.flame, |
| 2248 | vy: block.vy |
| 2249 | }); |
| 2250 | } |
| 2251 | } |
| 2252 | var lines = []; |
| 2253 | for (var r = this.height - 1; r >= 0; r--) |
| 2254 | { lines.push(grid[r].join('')); } |
| 2255 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 2256 | }; |
| 2257 | |
| 2258 | var GameCoreAPI = { |
| 2259 | GameCore: GameCore, |
| 2260 | COLOR_GRAY: COLOR_GRAY, |
| 2261 | STATE_STILL: STATE_STILL, |
| 2262 | STATE_FALLING: STATE_FALLING, |
| 2263 | STATE_FLYING: STATE_FLYING, |
| 2264 | STATE_SETTLING: STATE_SETTLING, |
| 2265 | STATE_LOCKED: STATE_LOCKED, |
| 2266 | STATE_MANUAL: STATE_MANUAL |
| 2267 | }; |
| 2268 | |
| 2269 | if (typeof module !== 'undefined' && module.exports) |
| 2270 | { module.exports = GameCoreAPI; } |
| 2271 | else if (typeof globalThis !== 'undefined') |
| 2272 | { |
| 2273 | globalThis.GameCore = GameCore; |
| 2274 | globalThis.GameCoreAPI = GameCoreAPI; |
| 2275 | } |
| 2276 | |
| 2277 | /* |
| 2278 | * End File: |
| 2279 | * /workspace/meteors/src/gameCore.js |
| 2280 | */ |
| 2281 | |
| 2282 | /* |
| 2283 | * Start File: |
| 2284 | * /workspace/meteors/src/online.js |
| 2285 | */ |
| 2286 | 'use strict'; |
| 2287 | |
| 2288 | /** |
| 2289 | * Online play: talks to api.php (cookie session), polls lobby and game |
| 2290 | * state, and draws opponent miniatures plus flying-block tweens. |
| 2291 | * Real-time sync is not required; a 0.75 s poll is enough. |
| 2292 | */ |
| 2293 | |
| 2294 | var Online = { |
| 2295 | POLL_MS: 750, |
| 2296 | DEFAULT_MISS_CHANCE: 0.30, |
| 2297 | FLIGHT_DURATION: 1.2, |
| 2298 | configured: false, |
| 2299 | local: false, |
| 2300 | csrf: '', |
| 2301 | me: null, |
| 2302 | lobby: null, |
| 2303 | incoming: [], |
| 2304 | shotResults: [], |
| 2305 | error: '', |
| 2306 | _timer: null, |
| 2307 | _pendingShots: [], |
| 2308 | _flights: [], |
| 2309 | _lastStatus: null, |
| 2310 | onChange: null, |
| 2311 | onGameStart: null, |
| 2312 | onIncoming: null, |
| 2313 | onFinished: null, |
| 2314 | onLobbyReturn: null |
| 2315 | }; |
| 2316 | |
| 2317 | Online._headers = function (json) |
| 2318 | { |
| 2319 | var h = { 'Accept': 'application/json' }; |
| 2320 | if (json) |
| 2321 | { h['Content-Type'] = 'application/json'; } |
| 2322 | if (this.csrf) |
| 2323 | { h['X-CSRF-Token'] = this.csrf; } |
| 2324 | return h; |
| 2325 | }; |
| 2326 | |
| 2327 | Online._apply = function (data) |
| 2328 | { |
| 2329 | if (!data) |
| 2330 | { return; } |
| 2331 | if (typeof data.configured === 'boolean') |
| 2332 | { this.configured = data.configured; } |
| 2333 | if (typeof data.local === 'boolean') |
| 2334 | { this.local = data.local; } |
| 2335 | if (data.csrf) |
| 2336 | { this.csrf = data.csrf; } |
| 2337 | if (data.me !== undefined) |
| 2338 | { this.me = data.me; } |
| 2339 | if (data.lobby !== undefined) |
| 2340 | { this.lobby = data.lobby; } |
| 2341 | if (data.incoming) |
| 2342 | { this.incoming = data.incoming; } |
| 2343 | if (data.shot_results) |
| 2344 | { this.shotResults = data.shot_results; } |
| 2345 | if (data.miss_chance_default != null) |
| 2346 | { this.DEFAULT_MISS_CHANCE = data.miss_chance_default; } |
| 2347 | if (data.flight_duration != null) |
| 2348 | { this.FLIGHT_DURATION = data.flight_duration; } |
| 2349 | if (data.error) |
| 2350 | { this.error = data.error; } |
| 2351 | else if (data.ok) |
| 2352 | { this.error = ''; } |
| 2353 | }; |
| 2354 | |
| 2355 | Online.request = function (action, body) |
| 2356 | { |
| 2357 | var self = this; |
| 2358 | var isGet = action === 'me' || action === ''; |
| 2359 | var url = 'api.php?action=' + encodeURIComponent(action || 'me'); |
| 2360 | var opts = { |
| 2361 | method: isGet && !body ? 'GET' : 'POST', |
| 2362 | credentials: 'same-origin', |
| 2363 | headers: this._headers(!!body || !isGet) |
| 2364 | }; |
| 2365 | if (opts.method === 'POST') |
| 2366 | { |
| 2367 | var payload = body ? Object.assign({ action: action }, body) : { action: action }; |
| 2368 | opts.body = JSON.stringify(payload); |
| 2369 | } |
| 2370 | return fetch(url, opts).then(function (res) |
| 2371 | { |
| 2372 | return res.json().then(function (data) |
| 2373 | { |
| 2374 | data = data || {}; |
| 2375 | if (!res.ok && !data.error) |
| 2376 | { data.error = 'De server gaf ' + res.status; data.ok = false; } |
| 2377 | self._apply(data); |
| 2378 | return data; |
| 2379 | }, function () |
| 2380 | { |
| 2381 | var fail = { ok: false, error: 'Geen geldig antwoord van de server.' }; |
| 2382 | self.error = fail.error; |
| 2383 | return fail; |
| 2384 | }); |
| 2385 | }).catch(function (err) |
| 2386 | { |
| 2387 | var fail = { ok: false, error: 'Geen verbinding met de server.' }; |
| 2388 | self.error = fail.error; |
| 2389 | return fail; |
| 2390 | }); |
| 2391 | }; |
| 2392 | |
| 2393 | Online.refresh = function () |
| 2394 | { |
| 2395 | return this.request('me'); |
| 2396 | }; |
| 2397 | |
| 2398 | Online.testLogin = function (who) |
| 2399 | { |
| 2400 | return this.request('test_login', { user: who }); |
| 2401 | }; |
| 2402 | |
| 2403 | Online.createLobby = function () |
| 2404 | { |
| 2405 | return this.request('lobby_create', { miss_chance: this.DEFAULT_MISS_CHANCE }); |
| 2406 | }; |
| 2407 | |
| 2408 | Online.joinLobby = function (code) |
| 2409 | { |
| 2410 | return this.request('lobby_join', { code: code }); |
| 2411 | }; |
| 2412 | |
| 2413 | Online.leaveLobby = function () |
| 2414 | { |
| 2415 | return this.request('lobby_leave'); |
| 2416 | }; |
| 2417 | |
| 2418 | Online.setReady = function (ready) |
| 2419 | { |
| 2420 | return this.request('lobby_ready', { ready: !!ready }); |
| 2421 | }; |
| 2422 | |
| 2423 | Online.startGame = function () |
| 2424 | { |
| 2425 | var self = this; |
| 2426 | var prev = this.lobby && this.lobby.status; |
| 2427 | return this.request('lobby_start').then(function (data) |
| 2428 | { |
| 2429 | self._handleTransitions(prev, data); |
| 2430 | return data; |
| 2431 | }); |
| 2432 | }; |
| 2433 | |
| 2434 | Online.queueShots = function (shots) |
| 2435 | { |
| 2436 | for (var i = 0; i < shots.length; i++) |
| 2437 | { |
| 2438 | this._pendingShots.push({ |
| 2439 | client_id: String(shots[i].id), |
| 2440 | color: shots[i].color, |
| 2441 | col: shots[i].col, |
| 2442 | y: shots[i].y |
| 2443 | }); |
| 2444 | } |
| 2445 | }; |
| 2446 | |
| 2447 | Online.poll = function (opts) |
| 2448 | { |
| 2449 | opts = opts || {}; |
| 2450 | var body = {}; |
| 2451 | if (opts.snapshot) |
| 2452 | { body.snapshot = opts.snapshot; } |
| 2453 | if (opts.score != null) |
| 2454 | { body.score = opts.score; } |
| 2455 | if (opts.eliminated) |
| 2456 | { body.eliminated = true; } |
| 2457 | if (this._pendingShots.length) |
| 2458 | { |
| 2459 | body.outgoing_shots = this._pendingShots.splice(0, this._pendingShots.length); |
| 2460 | } |
| 2461 | var self = this; |
| 2462 | var prevStatus = this.lobby && this.lobby.status; |
| 2463 | return this.request('poll', body).then(function (data) |
| 2464 | { |
| 2465 | self._handleTransitions(prevStatus, data); |
| 2466 | if (self.onChange) |
| 2467 | { self.onChange(self); } |
| 2468 | return data; |
| 2469 | }); |
| 2470 | }; |
| 2471 | |
| 2472 | Online._handleTransitions = function (prevStatus, data) |
| 2473 | { |
| 2474 | var lobby = this.lobby; |
| 2475 | var status = lobby && lobby.status; |
| 2476 | if (status === 'playing' && prevStatus !== 'playing' && this.onGameStart) |
| 2477 | { this.onGameStart(lobby); } |
| 2478 | if (data && data.incoming && data.incoming.length && this.onIncoming) |
| 2479 | { this.onIncoming(data.incoming); } |
| 2480 | if (data && data.shot_results && data.shot_results.length) |
| 2481 | { this.spawnFlights(data.shot_results, this._lastQueuedMeta); } |
| 2482 | if (status === 'finished' && prevStatus !== 'finished' && this.onFinished) |
| 2483 | { this.onFinished(lobby); } |
| 2484 | if (status === 'waiting' && prevStatus === 'finished' && this.onLobbyReturn) |
| 2485 | { this.onLobbyReturn(lobby); } |
| 2486 | }; |
| 2487 | |
| 2488 | Online.startPolling = function (getPayload) |
| 2489 | { |
| 2490 | this.stopPolling(); |
| 2491 | var self = this; |
| 2492 | var tick = function () |
| 2493 | { |
| 2494 | var payload = getPayload ? getPayload() : {}; |
| 2495 | self.poll(payload); |
| 2496 | }; |
| 2497 | tick(); |
| 2498 | this._timer = setInterval(tick, this.POLL_MS); |
| 2499 | }; |
| 2500 | |
| 2501 | Online.stopPolling = function () |
| 2502 | { |
| 2503 | if (this._timer) |
| 2504 | { |
| 2505 | clearInterval(this._timer); |
| 2506 | this._timer = null; |
| 2507 | } |
| 2508 | }; |
| 2509 | |
| 2510 | Online.loginUrl = function () |
| 2511 | { |
| 2512 | return 'auth.php'; |
| 2513 | }; |
| 2514 | |
| 2515 | Online.logoutUrl = function () |
| 2516 | { |
| 2517 | return 'logout.php'; |
| 2518 | }; |
| 2519 | |
| 2520 | Online.opponents = function () |
| 2521 | { |
| 2522 | if (!this.lobby || !this.lobby.players) |
| 2523 | { return []; } |
| 2524 | return this.lobby.players.filter(function (p) { return !p.is_me; }); |
| 2525 | }; |
| 2526 | |
| 2527 | Online.aliveOpponents = function () |
| 2528 | { |
| 2529 | return this.opponents().filter(function (p) { return !p.eliminated; }); |
| 2530 | }; |
| 2531 | |
| 2532 | Online.mePlayer = function () |
| 2533 | { |
| 2534 | if (!this.lobby || !this.lobby.players) |
| 2535 | { return null; } |
| 2536 | for (var i = 0; i < this.lobby.players.length; i++) |
| 2537 | { |
| 2538 | if (this.lobby.players[i].is_me) |
| 2539 | { return this.lobby.players[i]; } |
| 2540 | } |
| 2541 | return null; |
| 2542 | }; |
| 2543 | |
| 2544 | /* ---------- miniatures --------------------------------------------- */ |
| 2545 | |
| 2546 | Online.renderMinis = function (container) |
| 2547 | { |
| 2548 | if (!container) |
| 2549 | { return; } |
| 2550 | var opponents = this.opponents(); |
| 2551 | var existing = {}; |
| 2552 | var kids = container.querySelectorAll('[data-player]'); |
| 2553 | for (var i = 0; i < kids.length; i++) |
| 2554 | { existing[kids[i].getAttribute('data-player')] = kids[i]; } |
| 2555 | |
| 2556 | var seen = Object.create(null); |
| 2557 | for (var i = 0; i < opponents.length; i++) |
| 2558 | { |
| 2559 | var p = opponents[i]; |
| 2560 | seen[p.id] = true; |
| 2561 | var el = existing[p.id]; |
| 2562 | if (!el) |
| 2563 | { |
| 2564 | el = document.createElement('div'); |
| 2565 | el.className = 'mini'; |
| 2566 | el.setAttribute('data-player', p.id); |
| 2567 | el.innerHTML = '<div class="mini-name"></div>' |
| 2568 | + '<canvas class="mini-canvas" width="80" height="104"></canvas>' |
| 2569 | + '<div class="mini-meta"></div>'; |
| 2570 | container.appendChild(el); |
| 2571 | } |
| 2572 | el.classList.toggle('eliminated', !!p.eliminated); |
| 2573 | el.querySelector('.mini-name').textContent = p.name + (p.is_host ? ' (host)' : ''); |
| 2574 | el.querySelector('.mini-meta').textContent = p.eliminated |
| 2575 | ? ('Uit' + (p.place ? ' · ' + p.place + 'e' : '')) |
| 2576 | : ('Punten ' + (p.score || 0)); |
| 2577 | this._drawMini(el.querySelector('canvas'), p.snapshot); |
| 2578 | } |
| 2579 | for (var id in existing) |
| 2580 | { |
| 2581 | if (!seen[id]) |
| 2582 | { existing[id].remove(); } |
| 2583 | } |
| 2584 | }; |
| 2585 | |
| 2586 | var MINI_COLORS = ['#d4553a', '#3a9ad4', '#4caf6a', '#e6c04a', '#b86ad4']; |
| 2587 | |
| 2588 | Online._drawMini = function (canvas, snapshot) |
| 2589 | { |
| 2590 | if (!canvas) |
| 2591 | { return; } |
| 2592 | var ctx = canvas.getContext('2d'); |
| 2593 | var w = 10; |
| 2594 | var h = 13; |
| 2595 | var tw = canvas.width / w; |
| 2596 | var th = canvas.height / h; |
| 2597 | ctx.fillStyle = '#0c1020'; |
| 2598 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 2599 | ctx.strokeStyle = 'rgba(255,255,255,0.06)'; |
| 2600 | ctx.lineWidth = 1; |
| 2601 | for (var c = 0; c <= w; c++) |
| 2602 | { |
| 2603 | ctx.beginPath(); |
| 2604 | ctx.moveTo(c * tw + 0.5, 0); |
| 2605 | ctx.lineTo(c * tw + 0.5, canvas.height); |
| 2606 | ctx.stroke(); |
| 2607 | } |
| 2608 | for (var r = 0; r <= h; r++) |
| 2609 | { |
| 2610 | ctx.beginPath(); |
| 2611 | ctx.moveTo(0, r * th + 0.5); |
| 2612 | ctx.lineTo(canvas.width, r * th + 0.5); |
| 2613 | ctx.stroke(); |
| 2614 | } |
| 2615 | if (!snapshot || !snapshot.blocks) |
| 2616 | { return; } |
| 2617 | var blocks = snapshot.blocks; |
| 2618 | for (var i = 0; i < blocks.length; i++) |
| 2619 | { |
| 2620 | var b = blocks[i]; |
| 2621 | var x = b.col * tw; |
| 2622 | var y = canvas.height - (b.y + 1) * th; |
| 2623 | if (b.state === 'locked') |
| 2624 | { ctx.fillStyle = '#3a3d4a'; } |
| 2625 | else if (b.color < 0) |
| 2626 | { ctx.fillStyle = '#5a5d66'; } |
| 2627 | else |
| 2628 | { ctx.fillStyle = MINI_COLORS[b.color] || '#888'; } |
| 2629 | ctx.fillRect(x + 0.5, y + 0.5, tw - 1, th - 1); |
| 2630 | } |
| 2631 | }; |
| 2632 | |
| 2633 | Online.miniElement = function (playerId) |
| 2634 | { |
| 2635 | return document.querySelector('#minis .mini[data-player="' + CSS.escape(playerId) + '"]'); |
| 2636 | }; |
| 2637 | |
| 2638 | /* ---------- flight tweens ------------------------------------------ */ |
| 2639 | |
| 2640 | function colorSpriteUrl (color) |
| 2641 | { |
| 2642 | if (color < 0) |
| 2643 | { return 'images/block-gray.png'; } |
| 2644 | var names = ['ember', 'aqua', 'leaf', 'sun', 'orchid']; |
| 2645 | return 'images/block-' + (names[color] || 'gray') + '.png'; |
| 2646 | } |
| 2647 | |
| 2648 | /** |
| 2649 | * Animate shot results. `locals` is a map of client_id → {col, y, color, |
| 2650 | * startX, startY} in viewport pixels, filled by the view just after |
| 2651 | * consumeOutgoingShots. |
| 2652 | */ |
| 2653 | Online.spawnFlights = function (results, locals) |
| 2654 | { |
| 2655 | locals = locals || this._flightLocals || {}; |
| 2656 | var layer = document.getElementById('flights'); |
| 2657 | if (!layer) |
| 2658 | { return; } |
| 2659 | for (var i = 0; i < results.length; i++) |
| 2660 | { |
| 2661 | var r = results[i]; |
| 2662 | var meta = locals[r.client_id] || {}; |
| 2663 | var start = this._flightStart(meta.col); |
| 2664 | var end; |
| 2665 | var miss = !!r.miss; |
| 2666 | if (miss) |
| 2667 | { end = this._missEnd(start); } |
| 2668 | else |
| 2669 | { end = this._targetEnd(r.target_id, start); } |
| 2670 | var el = document.createElement('img'); |
| 2671 | el.src = colorSpriteUrl(r.color != null ? r.color : meta.color); |
| 2672 | el.alt = ''; |
| 2673 | el.className = 'flight'; |
| 2674 | layer.appendChild(el); |
| 2675 | var spin = (Math.random() * 2 - 1) * 720; |
| 2676 | var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000; |
| 2677 | this._flights.push({ |
| 2678 | el: el, |
| 2679 | start: start, |
| 2680 | end: end, |
| 2681 | ctrl: miss ? this._missCtrl(start, end) : this._hitCtrl(start, end), |
| 2682 | t0: performance.now(), |
| 2683 | duration: duration, |
| 2684 | spin: spin, |
| 2685 | miss: miss, |
| 2686 | angle: Math.random() * 360 |
| 2687 | }); |
| 2688 | } |
| 2689 | this._flightLocals = {}; |
| 2690 | }; |
| 2691 | |
| 2692 | Online.rememberFlightLocal = function (shot, startX, startY) |
| 2693 | { |
| 2694 | if (!this._flightLocals) |
| 2695 | { this._flightLocals = {}; } |
| 2696 | this._flightLocals[String(shot.id)] = { |
| 2697 | col: shot.col, |
| 2698 | y: shot.y, |
| 2699 | color: shot.color, |
| 2700 | startX: startX, |
| 2701 | startY: startY |
| 2702 | }; |
| 2703 | }; |
| 2704 | |
| 2705 | Online._flightStart = function (col) |
| 2706 | { |
| 2707 | var field = document.querySelector('canvas'); |
| 2708 | var appRef = typeof app !== 'undefined' ? app : null; |
| 2709 | if (appRef && appRef.scene && appRef.scene.canvas) |
| 2710 | { |
| 2711 | var canvas = appRef.scene.canvas; |
| 2712 | var rect = canvas.getBoundingClientRect(); |
| 2713 | var c = col == null ? 4.5 : col; |
| 2714 | return { |
| 2715 | x: rect.left + (c + 0.5) * (rect.width / 10), |
| 2716 | y: rect.top + 4 |
| 2717 | }; |
| 2718 | } |
| 2719 | if (field) |
| 2720 | { |
| 2721 | var rect = field.getBoundingClientRect(); |
| 2722 | return { x: rect.left + rect.width / 2, y: rect.top }; |
| 2723 | } |
| 2724 | return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 }; |
| 2725 | }; |
| 2726 | |
| 2727 | Online._targetEnd = function (targetId, start) |
| 2728 | { |
| 2729 | var el = targetId && this.miniElement(targetId); |
| 2730 | if (el) |
| 2731 | { |
| 2732 | var r = el.getBoundingClientRect(); |
| 2733 | return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; |
| 2734 | } |
| 2735 | return { x: start.x + (Math.random() - 0.5) * 80, y: 40 }; |
| 2736 | }; |
| 2737 | |
| 2738 | Online._missEnd = function (start) |
| 2739 | { |
| 2740 | var dx = (Math.random() * 2 - 1) * window.innerWidth * 0.35; |
| 2741 | return { |
| 2742 | x: start.x + dx, |
| 2743 | y: start.y - (120 + Math.random() * 220) |
| 2744 | }; |
| 2745 | }; |
| 2746 | |
| 2747 | Online._missCtrl = function (start, end) |
| 2748 | { |
| 2749 | return { |
| 2750 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 160, |
| 2751 | y: Math.min(start.y, end.y) - 40 - Math.random() * 80 |
| 2752 | }; |
| 2753 | }; |
| 2754 | |
| 2755 | Online._hitCtrl = function (start, end) |
| 2756 | { |
| 2757 | return { |
| 2758 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 40, |
| 2759 | y: (start.y + end.y) / 2 - 30 |
| 2760 | }; |
| 2761 | }; |
| 2762 | |
| 2763 | Online.tickFlights = function (now) |
| 2764 | { |
| 2765 | now = now || performance.now(); |
| 2766 | var leftover = []; |
| 2767 | for (var i = 0; i < this._flights.length; i++) |
| 2768 | { |
| 2769 | var f = this._flights[i]; |
| 2770 | var u = (now - f.t0) / f.duration; |
| 2771 | if (u >= 1) |
| 2772 | { |
| 2773 | if (f.el && f.el.parentNode) |
| 2774 | { f.el.parentNode.removeChild(f.el); } |
| 2775 | continue; |
| 2776 | } |
| 2777 | // Quadratic bezier, ease-in so they feel like they recede. |
| 2778 | var e = u * u; |
| 2779 | var mt = 1 - e; |
| 2780 | var x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x; |
| 2781 | var y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y; |
| 2782 | var scale = f.miss ? (1 - e) : (1 - 0.85 * e); |
| 2783 | var angle = f.angle + f.spin * u; |
| 2784 | f.el.style.transform = 'translate(' + x + 'px,' + y + 'px) translate(-50%,-50%) rotate(' |
| 2785 | + angle + 'deg) scale(' + scale + ')'; |
| 2786 | f.el.style.opacity = f.miss ? String(1 - e) : '1'; |
| 2787 | leftover.push(f); |
| 2788 | } |
| 2789 | this._flights = leftover; |
| 2790 | }; |
| 2791 | |
| 2792 | Online.clearFlights = function () |
| 2793 | { |
| 2794 | for (var i = 0; i < this._flights.length; i++) |
| 2795 | { |
| 2796 | var el = this._flights[i].el; |
| 2797 | if (el && el.parentNode) |
| 2798 | { el.parentNode.removeChild(el); } |
| 2799 | } |
| 2800 | this._flights = []; |
| 2801 | this._flightLocals = {}; |
| 2802 | }; |
| 2803 | |
| 2804 | if (typeof globalThis !== 'undefined') |
| 2805 | { globalThis.Online = Online; } |
| 2806 | if (typeof module !== 'undefined' && module.exports) |
| 2807 | { module.exports = Online; } |
| 2808 | |
| 2809 | /* |
| 2810 | * End File: |
| 2811 | * /workspace/meteors/src/online.js |
| 2812 | */ |
| 2813 | |
| 2814 | /* |
| 2815 | * Start File: |
| 2816 | * /workspace/meteors/src/main.js |
| 2817 | */ |
| 2818 | 'use strict'; |
| 2819 | |
| 2820 | /** |
| 2821 | * View and input for Meteors. The simulation lives in GameCore; this file |
| 2822 | * only draws it, scales it to the viewport, and translates pointer events |
| 2823 | * into beginDrag / moveDrag / endDrag. Online play (stage 2) is wired |
| 2824 | * through Online (src/online.js) and the same GameCore. |
| 2825 | */ |
| 2826 | |
| 2827 | var IMAGES = [ |
| 2828 | { name: 'ember', url: 'images/block-ember.png' }, |
| 2829 | { name: 'aqua', url: 'images/block-aqua.png' }, |
| 2830 | { name: 'leaf', url: 'images/block-leaf.png' }, |
| 2831 | { name: 'sun', url: 'images/block-sun.png' }, |
| 2832 | { name: 'orchid', url: 'images/block-orchid.png' }, |
| 2833 | { name: 'gray', url: 'images/block-gray.png' }, |
| 2834 | { name: 'locked', url: 'images/block-locked.png' }, |
| 2835 | { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, |
| 2836 | { name: 'cell', url: 'images/cell.png' }, |
| 2837 | { name: 'pixel', url: 'images/pixel.png' } |
| 2838 | ]; |
| 2839 | |
| 2840 | var COLOR_SPRITES = ['ember', 'aqua', 'leaf', 'sun', 'orchid']; |
| 2841 | var GAME_OVER_DELAY = 2; |
| 2842 | |
| 2843 | var game = new Game(); |
| 2844 | var app = { |
| 2845 | mode: 'menu', |
| 2846 | core: null, |
| 2847 | scene: null, |
| 2848 | manager: null, |
| 2849 | planet: null, |
| 2850 | lastScore: 0, |
| 2851 | tile: 48, |
| 2852 | fieldX: 0, |
| 2853 | fieldY: 0, |
| 2854 | fieldW: 0, |
| 2855 | fieldH: 0, |
| 2856 | hudH: 56, |
| 2857 | sprites: Object.create(null), |
| 2858 | flames: Object.create(null), |
| 2859 | cells: [], |
| 2860 | pointerId: null, |
| 2861 | gameOverShown: false, |
| 2862 | selectTween: null, |
| 2863 | online: false, |
| 2864 | eliminatedReported: false, |
| 2865 | spectating: false, |
| 2866 | miniH: 0 |
| 2867 | }; |
| 2868 | |
| 2869 | window.onload = function () |
| 2870 | { |
| 2871 | var play = document.getElementById('btn-play'); |
| 2872 | play.addEventListener('click', onPlay); |
| 2873 | play.addEventListener('pointerup', function (e) |
| 2874 | { |
| 2875 | // A touch-friendly path: some mobile browsers swallow the click |
| 2876 | // after a slightly sloppy tap. Ignore the following click. |
| 2877 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 2878 | { |
| 2879 | e.preventDefault(); |
| 2880 | onPlay(); |
| 2881 | } |
| 2882 | }); |
| 2883 | |
| 2884 | game.preloadImagesThenStart(IMAGES, function () |
| 2885 | { |
| 2886 | if (game.images.flame && game.images.flame.addAnimationCycle) |
| 2887 | { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } |
| 2888 | }); |
| 2889 | |
| 2890 | bindOnlineUi(); |
| 2891 | }; |
| 2892 | |
| 2893 | function onPlay () |
| 2894 | { |
| 2895 | if (app.mode === 'playing') |
| 2896 | { return; } |
| 2897 | startPlay(); |
| 2898 | } |
| 2899 | |
| 2900 | function startPlay (opts) |
| 2901 | { |
| 2902 | opts = opts || {}; |
| 2903 | hideEl('menu'); |
| 2904 | hideEl('game-over'); |
| 2905 | hideEl('online-panel'); |
| 2906 | hideEl('spectator'); |
| 2907 | hideEl('podium'); |
| 2908 | showEl('hud'); |
| 2909 | |
| 2910 | app.planet = (typeof Planet !== 'undefined' ? Planet.defaultPlanet() : PlanetMod.defaultPlanet()); |
| 2911 | var miss = opts.missChance; |
| 2912 | if (typeof miss === 'number' && isFinite(miss)) |
| 2913 | { app.core = new GameCore(app.planet, Math.random, miss); } |
| 2914 | else |
| 2915 | { app.core = new GameCore(app.planet, Math.random); } |
| 2916 | app.mode = 'playing'; |
| 2917 | app.online = !!opts.online; |
| 2918 | app.gameOverShown = false; |
| 2919 | app.eliminatedReported = false; |
| 2920 | app.spectating = false; |
| 2921 | app.sprites = Object.create(null); |
| 2922 | app.flames = Object.create(null); |
| 2923 | app.cells = []; |
| 2924 | app.pointerId = null; |
| 2925 | app.selectTween = null; |
| 2926 | |
| 2927 | layout(); |
| 2928 | if (app.scene) |
| 2929 | { |
| 2930 | app.scene.destroy(); |
| 2931 | app.scene = null; |
| 2932 | } |
| 2933 | app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); |
| 2934 | app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; |
| 2935 | app.scene.renderBackground = true; |
| 2936 | game.loadScene(app.scene); |
| 2937 | |
| 2938 | buildCells(); |
| 2939 | app.manager = app.scene.addObject(makeManager()); |
| 2940 | if (!app.spectating) |
| 2941 | { bindPointer(app.scene.canvas); } |
| 2942 | refreshHud(); |
| 2943 | if (app.online) |
| 2944 | { showEl('minis'); } |
| 2945 | else |
| 2946 | { hideEl('minis'); } |
| 2947 | } |
| 2948 | |
| 2949 | function teardownPlay () |
| 2950 | { |
| 2951 | unbindPointer(); |
| 2952 | if (app.scene) |
| 2953 | { |
| 2954 | app.scene.destroy(); |
| 2955 | app.scene = null; |
| 2956 | } |
| 2957 | app.manager = null; |
| 2958 | app.core = null; |
| 2959 | app.sprites = Object.create(null); |
| 2960 | app.flames = Object.create(null); |
| 2961 | app.cells = []; |
| 2962 | app.selectTween = null; |
| 2963 | app.pointerId = null; |
| 2964 | app.gameOverShown = false; |
| 2965 | hideEl('hud'); |
| 2966 | hideEl('game-over'); |
| 2967 | hideEl('spectator'); |
| 2968 | hideEl('minis'); |
| 2969 | if (typeof Online !== 'undefined') |
| 2970 | { Online.clearFlights(); } |
| 2971 | } |
| 2972 | |
| 2973 | function returnToMenu () |
| 2974 | { |
| 2975 | if (app.online && typeof Online !== 'undefined') |
| 2976 | { |
| 2977 | // Online matches do not dump you back to the solo menu: |
| 2978 | // elimination becomes spectating; a finished match shows the podium. |
| 2979 | return; |
| 2980 | } |
| 2981 | teardownPlay(); |
| 2982 | app.mode = 'menu'; |
| 2983 | app.online = false; |
| 2984 | showEl('menu'); |
| 2985 | var last = document.getElementById('last-score'); |
| 2986 | if (app.lastScore > 0) |
| 2987 | { |
| 2988 | last.hidden = false; |
| 2989 | last.querySelector('strong').textContent = String(app.lastScore); |
| 2990 | } |
| 2991 | } |
| 2992 | |
| 2993 | function buildCells () |
| 2994 | { |
| 2995 | if (!app.scene) |
| 2996 | { return; } |
| 2997 | var img = game.images.cell; |
| 2998 | for (var row = 0; row < 13; row++) |
| 2999 | { |
| 3000 | for (var col = 0; col < 10; col++) |
| 3001 | { |
| 3002 | var obj = new GameObject(0, 0, img, 0); |
| 3003 | placeTile(obj, col, row); |
| 3004 | obj.renderer.alpha = 0.9; |
| 3005 | app.scene.addObject(obj); |
| 3006 | app.cells.push(obj); |
| 3007 | } |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | function placeTile (obj, col, y) |
| 3012 | { |
| 3013 | var t = app.tile; |
| 3014 | obj.transform.position.x = col * t + t / 2; |
| 3015 | obj.transform.position.y = app.fieldH - (y + 0.5) * t; |
| 3016 | obj.transform.size = { x: t, y: t }; |
| 3017 | } |
| 3018 | |
| 3019 | function layout () |
| 3020 | { |
| 3021 | var vw = window.innerWidth || 320; |
| 3022 | var vh = window.innerHeight || 480; |
| 3023 | var safeTop = 0; |
| 3024 | try |
| 3025 | { |
| 3026 | // env(safe-area) is a CSS thing; keep a conservative HUD height. |
| 3027 | safeTop = 0; |
| 3028 | } |
| 3029 | catch (e) { /* ignore */ } |
| 3030 | app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); |
| 3031 | app.miniH = 0; |
| 3032 | if (app.online) |
| 3033 | { app.miniH = Math.min(150, Math.max(86, Math.round(vh * 0.20))); } |
| 3034 | var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); |
| 3035 | var availW = Math.max(80, vw - margin * 2); |
| 3036 | var availH = Math.max(80, vh - app.hudH - app.miniH - margin); |
| 3037 | app.tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); |
| 3038 | app.fieldW = app.tile * 10; |
| 3039 | app.fieldH = app.tile * 13; |
| 3040 | app.fieldX = Math.floor((vw - app.fieldW) / 2); |
| 3041 | var topBand = app.hudH + app.miniH; |
| 3042 | app.fieldY = topBand + Math.floor((vh - topBand - app.fieldH - margin / 2) / 2); |
| 3043 | if (app.fieldY < topBand) |
| 3044 | { app.fieldY = topBand; } |
| 3045 | var minis = document.getElementById('minis'); |
| 3046 | if (minis) |
| 3047 | { |
| 3048 | minis.style.top = app.hudH + 'px'; |
| 3049 | minis.style.maxHeight = app.miniH + 'px'; |
| 3050 | } |
| 3051 | |
| 3052 | if (app.scene) |
| 3053 | { |
| 3054 | app.scene.position.x = app.fieldX; |
| 3055 | app.scene.position.y = app.fieldY; |
| 3056 | app.scene.size.x = app.fieldW; |
| 3057 | app.scene.size.y = app.fieldH; |
| 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | function spriteFor (block) |
| 3062 | { |
| 3063 | if (block.state === 'locked') |
| 3064 | { return game.images.locked; } |
| 3065 | if (block.color < 0) |
| 3066 | { return game.images.gray; } |
| 3067 | return game.images[COLOR_SPRITES[block.color]] || game.images.gray; |
| 3068 | } |
| 3069 | |
| 3070 | function syncSprites () |
| 3071 | { |
| 3072 | if (!app.core || !app.scene) |
| 3073 | { return; } |
| 3074 | var seen = Object.create(null); |
| 3075 | var blocks = app.core.blocks; |
| 3076 | for (var i = 0; i < blocks.length; i++) |
| 3077 | { |
| 3078 | var block = blocks[i]; |
| 3079 | seen[block.id] = true; |
| 3080 | var obj = app.sprites[block.id]; |
| 3081 | if (!obj) |
| 3082 | { |
| 3083 | obj = new GameObject(0, 0, spriteFor(block), 2); |
| 3084 | app.scene.addObject(obj); |
| 3085 | app.sprites[block.id] = obj; |
| 3086 | } |
| 3087 | obj.renderer.sprite = spriteFor(block); |
| 3088 | placeTile(obj, block.col, block.y); |
| 3089 | applyBlockVisual(obj, block); |
| 3090 | obj.renderer.visible = true; |
| 3091 | obj.renderer.alpha = 1; |
| 3092 | |
| 3093 | if (block.flame) |
| 3094 | { |
| 3095 | var flame = app.flames[block.id]; |
| 3096 | if (!flame) |
| 3097 | { |
| 3098 | flame = new GameObject(0, 0, game.images.flame, 1); |
| 3099 | flame.renderer.setAnimation('burn', 0, 0.08); |
| 3100 | app.scene.addObject(flame); |
| 3101 | app.flames[block.id] = flame; |
| 3102 | } |
| 3103 | flame.renderer.visible = true; |
| 3104 | var t = app.tile; |
| 3105 | flame.transform.position.x = block.col * t + t / 2; |
| 3106 | flame.transform.position.y = app.fieldH - block.y * t + t * 0.15; |
| 3107 | flame.transform.size = { x: t * 0.85, y: t * 0.85 }; |
| 3108 | } |
| 3109 | else if (app.flames[block.id]) |
| 3110 | { app.flames[block.id].renderer.visible = false; } |
| 3111 | } |
| 3112 | |
| 3113 | var ids = Object.keys(app.sprites); |
| 3114 | for (var i = 0; i < ids.length; i++) |
| 3115 | { |
| 3116 | if (!seen[ids[i]]) |
| 3117 | { |
| 3118 | app.sprites[ids[i]].destroy(); |
| 3119 | delete app.sprites[ids[i]]; |
| 3120 | if (app.flames[ids[i]]) |
| 3121 | { |
| 3122 | app.flames[ids[i]].destroy(); |
| 3123 | delete app.flames[ids[i]]; |
| 3124 | } |
| 3125 | } |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | function applyBlockVisual (obj, block) |
| 3130 | { |
| 3131 | var t = app.tile; |
| 3132 | var sx = 1; |
| 3133 | var sy = 1; |
| 3134 | var dy = 0; |
| 3135 | if (block.bouncing && app.planet && app.planet.bounceDuration > 0 |
| 3136 | && typeof Easing !== 'undefined' && Easing.bouncePose) |
| 3137 | { |
| 3138 | var u = block.bounceAge / app.planet.bounceDuration; |
| 3139 | var pose = Easing.bouncePose(u, app.planet.bounceSquash); |
| 3140 | sx = pose.sx; |
| 3141 | sy = pose.sy; |
| 3142 | dy = pose.dy; |
| 3143 | } |
| 3144 | var sel = 1; |
| 3145 | var tw = app.selectTween; |
| 3146 | if (tw && tw.blockId === block.id && tw.phase !== 'idle') |
| 3147 | { sel = tw.scale; } |
| 3148 | obj.transform.size = { x: t * sx * sel, y: t * sy * sel }; |
| 3149 | if (dy) |
| 3150 | { obj.transform.position.y -= dy * t; } |
| 3151 | } |
| 3152 | |
| 3153 | function clickTween () |
| 3154 | { |
| 3155 | return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null; |
| 3156 | } |
| 3157 | |
| 3158 | function startPressTween (blockId) |
| 3159 | { |
| 3160 | var c = clickTween(); |
| 3161 | if (!c) |
| 3162 | { return; } |
| 3163 | var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1; |
| 3164 | app.selectTween = { |
| 3165 | blockId: blockId, |
| 3166 | from: from, |
| 3167 | to: c.pressScale, |
| 3168 | t: 0, |
| 3169 | duration: c.pressDuration, |
| 3170 | ease: c.pressEase, |
| 3171 | scale: from, |
| 3172 | phase: 'press' |
| 3173 | }; |
| 3174 | } |
| 3175 | |
| 3176 | function startReleaseTween () |
| 3177 | { |
| 3178 | var c = clickTween(); |
| 3179 | var tw = app.selectTween; |
| 3180 | if (!c || !tw || tw.phase === 'idle') |
| 3181 | { return; } |
| 3182 | app.selectTween = { |
| 3183 | blockId: tw.blockId, |
| 3184 | from: tw.scale, |
| 3185 | to: c.releaseScale, |
| 3186 | t: 0, |
| 3187 | duration: c.releaseDuration, |
| 3188 | ease: c.releaseEase, |
| 3189 | scale: tw.scale, |
| 3190 | phase: 'release' |
| 3191 | }; |
| 3192 | } |
| 3193 | |
| 3194 | function updateSelectTween (dt) |
| 3195 | { |
| 3196 | var tw = app.selectTween; |
| 3197 | if (!tw || tw.phase === 'idle' || tw.phase === 'hold') |
| 3198 | { return; } |
| 3199 | tw.t += dt; |
| 3200 | var u = tw.duration > 0 ? tw.t / tw.duration : 1; |
| 3201 | if (u >= 1) |
| 3202 | { |
| 3203 | tw.scale = tw.to; |
| 3204 | tw.t = tw.duration; |
| 3205 | if (tw.phase === 'press') |
| 3206 | { tw.phase = 'hold'; } |
| 3207 | else |
| 3208 | { |
| 3209 | tw.phase = 'idle'; |
| 3210 | tw.scale = 1; |
| 3211 | tw.blockId = null; |
| 3212 | } |
| 3213 | return; |
| 3214 | } |
| 3215 | var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack); |
| 3216 | tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u; |
| 3217 | } |
| 3218 | |
| 3219 | function refreshHud () |
| 3220 | { |
| 3221 | if (!app.core) |
| 3222 | { return; } |
| 3223 | document.getElementById('score').textContent = String(app.core.score); |
| 3224 | var planet = app.planet; |
| 3225 | document.getElementById('planet-name').textContent = planet.name + ' · ' + planet.people; |
| 3226 | var d = Planet.displayValues(planet); |
| 3227 | document.getElementById('planet-stats').textContent = |
| 3228 | 'g ' + d.gravity.toFixed(2) + ' d ' + d.density.toFixed(2) + |
| 3229 | ' F ' + Math.round(d.newtons) + ' N Power ' + d.power + '%'; |
| 3230 | } |
| 3231 | |
| 3232 | function makeManager () |
| 3233 | { |
| 3234 | var obj = new GameObject(0, 0, game.images.pixel, 0); |
| 3235 | obj.renderer.visible = false; |
| 3236 | var base = obj.update.bind(obj); |
| 3237 | obj.update = function (dt) |
| 3238 | { |
| 3239 | base(dt); |
| 3240 | tickView(dt); |
| 3241 | }; |
| 3242 | return obj; |
| 3243 | } |
| 3244 | |
| 3245 | function tickView (dt) |
| 3246 | { |
| 3247 | if (app.mode !== 'playing' || !app.core) |
| 3248 | { return; } |
| 3249 | |
| 3250 | layout(); |
| 3251 | for (var i = 0; i < app.cells.length; i++) |
| 3252 | { |
| 3253 | var col = i % 10; |
| 3254 | var row = Math.floor(i / 10); |
| 3255 | placeTile(app.cells[i], col, row); |
| 3256 | } |
| 3257 | |
| 3258 | app.core.tick(dt); |
| 3259 | updateSelectTween(dt); |
| 3260 | syncSprites(); |
| 3261 | refreshHud(); |
| 3262 | tickOnline(dt); |
| 3263 | |
| 3264 | if (app.core.state === 'gameover' && !app.gameOverShown) |
| 3265 | { |
| 3266 | app.gameOverShown = true; |
| 3267 | app.lastScore = app.core.score; |
| 3268 | document.getElementById('go-score').textContent = String(app.lastScore); |
| 3269 | if (app.online) |
| 3270 | { onOnlineEliminated(); } |
| 3271 | else |
| 3272 | { showEl('game-over'); } |
| 3273 | } |
| 3274 | if (!app.online && app.core.state === 'gameover' && app.core.gameOverTime >= GAME_OVER_DELAY) |
| 3275 | { returnToMenu(); } |
| 3276 | } |
| 3277 | |
| 3278 | /* ---------- pointer input (mouse + touch) --------------------------- */ |
| 3279 | /* |
| 3280 | * Both mouse and touch use Pointer Events. After pointerdown we listen |
| 3281 | * on window, so a drag that leaves the canvas (needed to launch a |
| 3282 | * block at the top of a full column) still reaches moveDrag. We do |
| 3283 | * not treat lostpointercapture as the end of a drag: on some touch |
| 3284 | * browsers that fires mid-gesture and would cancel a launch. |
| 3285 | */ |
| 3286 | |
| 3287 | var boundCanvas = null; |
| 3288 | var windowListening = false; |
| 3289 | |
| 3290 | function addWindowPointer () |
| 3291 | { |
| 3292 | if (windowListening) |
| 3293 | { return; } |
| 3294 | window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false }); |
| 3295 | window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false }); |
| 3296 | window.addEventListener('pointercancel', onPointerUp, { capture: true, passive: false }); |
| 3297 | windowListening = true; |
| 3298 | } |
| 3299 | |
| 3300 | function removeWindowPointer () |
| 3301 | { |
| 3302 | if (!windowListening) |
| 3303 | { return; } |
| 3304 | window.removeEventListener('pointermove', onPointerMove, { capture: true }); |
| 3305 | window.removeEventListener('pointerup', onPointerUp, { capture: true }); |
| 3306 | window.removeEventListener('pointercancel', onPointerUp, { capture: true }); |
| 3307 | windowListening = false; |
| 3308 | } |
| 3309 | |
| 3310 | function bindPointer (canvas) |
| 3311 | { |
| 3312 | unbindPointer(); |
| 3313 | boundCanvas = canvas; |
| 3314 | canvas.addEventListener('pointerdown', onPointerDown, { passive: false }); |
| 3315 | canvas.style.touchAction = 'none'; |
| 3316 | canvas.style.cursor = 'pointer'; |
| 3317 | canvas.style.webkitUserSelect = 'none'; |
| 3318 | canvas.style.userSelect = 'none'; |
| 3319 | } |
| 3320 | |
| 3321 | function unbindPointer () |
| 3322 | { |
| 3323 | removeWindowPointer(); |
| 3324 | if (boundCanvas) |
| 3325 | { boundCanvas.removeEventListener('pointerdown', onPointerDown); } |
| 3326 | boundCanvas = null; |
| 3327 | app.pointerId = null; |
| 3328 | } |
| 3329 | |
| 3330 | function fieldFromEvent (e) |
| 3331 | { |
| 3332 | var canvas = app.scene && app.scene.canvas; |
| 3333 | if (!canvas) |
| 3334 | { return null; } |
| 3335 | var rect = canvas.getBoundingClientRect(); |
| 3336 | if (rect.width <= 0 || rect.height <= 0) |
| 3337 | { return null; } |
| 3338 | var x = (e.clientX - rect.left) * (canvas.width / rect.width); |
| 3339 | var y = (e.clientY - rect.top) * (canvas.height / rect.height); |
| 3340 | // Fractional column so beginDrag can snap across a nearby edge. |
| 3341 | var col = x / app.tile; |
| 3342 | var fieldY = (canvas.height - y) / app.tile; |
| 3343 | return { col: col, y: fieldY }; |
| 3344 | } |
| 3345 | |
| 3346 | function onPointerDown (e) |
| 3347 | { |
| 3348 | if (!app.core || app.core.state !== 'playing') |
| 3349 | { return; } |
| 3350 | if (app.pointerId != null) |
| 3351 | { return; } |
| 3352 | e.preventDefault(); |
| 3353 | app.pointerId = e.pointerId; |
| 3354 | try |
| 3355 | { boundCanvas.setPointerCapture(e.pointerId); } |
| 3356 | catch (err) { /* ignore */ } |
| 3357 | addWindowPointer(); |
| 3358 | var pos = fieldFromEvent(e); |
| 3359 | // Instant grab, no long-press or hold delay: the block is taken on |
| 3360 | // pointerdown and the click tween starts in the same moment. |
| 3361 | if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag) |
| 3362 | { startPressTween(app.core.drag.blockId); } |
| 3363 | } |
| 3364 | |
| 3365 | function onPointerMove (e) |
| 3366 | { |
| 3367 | if (app.pointerId == null || e.pointerId !== app.pointerId) |
| 3368 | { return; } |
| 3369 | if (!app.core || !app.core.drag) |
| 3370 | { return; } |
| 3371 | e.preventDefault(); |
| 3372 | var pos = fieldFromEvent(e); |
| 3373 | if (pos) |
| 3374 | { app.core.moveDrag(pos.y); } |
| 3375 | } |
| 3376 | |
| 3377 | function onPointerUp (e) |
| 3378 | { |
| 3379 | if (app.pointerId == null) |
| 3380 | { return; } |
| 3381 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 3382 | { return; } |
| 3383 | if (e) |
| 3384 | { e.preventDefault(); } |
| 3385 | app.pointerId = null; |
| 3386 | removeWindowPointer(); |
| 3387 | if (boundCanvas) |
| 3388 | { |
| 3389 | try |
| 3390 | { boundCanvas.releasePointerCapture(e && e.pointerId); } |
| 3391 | catch (err) { /* ignore */ } |
| 3392 | } |
| 3393 | startReleaseTween(); |
| 3394 | if (app.core) |
| 3395 | { app.core.endDrag(); } |
| 3396 | } |
| 3397 | |
| 3398 | function hideEl (id) |
| 3399 | { |
| 3400 | document.getElementById(id).hidden = true; |
| 3401 | } |
| 3402 | |
| 3403 | function showEl (id) |
| 3404 | { |
| 3405 | document.getElementById(id).hidden = false; |
| 3406 | } |
| 3407 | |
| 3408 | window.addEventListener('resize', function () |
| 3409 | { |
| 3410 | if (app.mode === 'playing') |
| 3411 | { layout(); } |
| 3412 | }); |
| 3413 | |
| 3414 | window.addEventListener('orientationchange', function () |
| 3415 | { |
| 3416 | setTimeout(function () |
| 3417 | { |
| 3418 | if (app.mode === 'playing') |
| 3419 | { layout(); } |
| 3420 | }, 120); |
| 3421 | }); |
| 3422 | |
| 3423 | /* ---------- online play (stage 2) ---------------------------------- */ |
| 3424 | |
| 3425 | function tickOnline (dt) |
| 3426 | { |
| 3427 | if (!app.online || typeof Online === 'undefined') |
| 3428 | { return; } |
| 3429 | if (app.core) |
| 3430 | { |
| 3431 | var shots = app.core.consumeOutgoingShots(); |
| 3432 | for (var i = 0; i < shots.length; i++) |
| 3433 | { |
| 3434 | var s = shots[i]; |
| 3435 | var start = fieldToViewport(s.col, s.y + 1); |
| 3436 | Online.rememberFlightLocal(s, start.x, start.y); |
| 3437 | } |
| 3438 | Online.queueShots(shots); |
| 3439 | } |
| 3440 | Online.tickFlights(); |
| 3441 | Online.renderMinis(document.getElementById('minis')); |
| 3442 | } |
| 3443 | |
| 3444 | function fieldToViewport (col, fieldY) |
| 3445 | { |
| 3446 | var canvas = app.scene && app.scene.canvas; |
| 3447 | if (!canvas) |
| 3448 | { return { x: window.innerWidth / 2, y: 40 }; } |
| 3449 | var rect = canvas.getBoundingClientRect(); |
| 3450 | var x = rect.left + ((col + 0.5) / 10) * rect.width; |
| 3451 | var y = rect.top + (1 - (fieldY / 13)) * rect.height; |
| 3452 | return { x: x, y: y }; |
| 3453 | } |
| 3454 | |
| 3455 | function onlinePayload () |
| 3456 | { |
| 3457 | if (!app.online || !app.core) |
| 3458 | { return {}; } |
| 3459 | return { |
| 3460 | snapshot: app.core.fieldSnapshot(), |
| 3461 | score: app.core.score, |
| 3462 | eliminated: app.core.state === 'gameover' || app.spectating || app.eliminatedReported |
| 3463 | }; |
| 3464 | } |
| 3465 | |
| 3466 | function onOnlineEliminated () |
| 3467 | { |
| 3468 | app.eliminatedReported = true; |
| 3469 | app.spectating = true; |
| 3470 | unbindPointer(); |
| 3471 | hideEl('game-over'); |
| 3472 | var lobby = typeof Online !== 'undefined' && Online.lobby; |
| 3473 | if (lobby && lobby.status === 'finished') |
| 3474 | { |
| 3475 | showPodium(lobby); |
| 3476 | return; |
| 3477 | } |
| 3478 | showEl('spectator'); |
| 3479 | } |
| 3480 | |
| 3481 | function showPodium (lobby) |
| 3482 | { |
| 3483 | hideEl('spectator'); |
| 3484 | hideEl('game-over'); |
| 3485 | hideEl('online-panel'); |
| 3486 | showEl('podium'); |
| 3487 | var list = document.getElementById('podium-list'); |
| 3488 | list.innerHTML = ''; |
| 3489 | var ranks = (lobby && lobby.rankings) || []; |
| 3490 | for (var i = 0; i < ranks.length; i++) |
| 3491 | { |
| 3492 | var r = ranks[i]; |
| 3493 | var li = document.createElement('li'); |
| 3494 | var medal = r.place === 1 ? '1e' : (r.place === 2 ? '2e' : (r.place === 3 ? '3e' : r.place + 'e')); |
| 3495 | li.textContent = medal + ' ' + r.name + ' · ' + r.score + ' punten'; |
| 3496 | list.appendChild(li); |
| 3497 | } |
| 3498 | var hint = document.getElementById('podium-hint'); |
| 3499 | var secs = (lobby && lobby.podium_seconds) || 5; |
| 3500 | hint.textContent = 'Over ' + secs + ' seconden teruggaan naar de lobby…'; |
| 3501 | } |
| 3502 | |
| 3503 | function startOnlineMatch () |
| 3504 | { |
| 3505 | if (app.mode === 'playing' && app.online) |
| 3506 | { return; } |
| 3507 | var miss = (typeof Online !== 'undefined' && Online.lobby && Online.lobby.miss_chance != null) |
| 3508 | ? Online.lobby.miss_chance |
| 3509 | : (typeof Online !== 'undefined' ? Online.DEFAULT_MISS_CHANCE : 0.30); |
| 3510 | startPlay({ online: true, missChance: miss }); |
| 3511 | if (typeof Online !== 'undefined') |
| 3512 | { Online.startPolling(onlinePayload); } |
| 3513 | } |
| 3514 | |
| 3515 | function leaveOnlineMatch (goMenu) |
| 3516 | { |
| 3517 | if (typeof Online !== 'undefined') |
| 3518 | { |
| 3519 | Online.stopPolling(); |
| 3520 | Online.leaveLobby(); |
| 3521 | Online.clearFlights(); |
| 3522 | } |
| 3523 | teardownPlay(); |
| 3524 | app.online = false; |
| 3525 | if (goMenu) |
| 3526 | { |
| 3527 | app.mode = 'menu'; |
| 3528 | showEl('menu'); |
| 3529 | } |
| 3530 | else |
| 3531 | { |
| 3532 | app.mode = 'online'; |
| 3533 | openOnlinePanel(); |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | function bindOnlineUi () |
| 3538 | { |
| 3539 | var onlineBtn = document.getElementById('btn-online'); |
| 3540 | if (!onlineBtn) |
| 3541 | { return; } |
| 3542 | onlineBtn.addEventListener('click', function () |
| 3543 | { openOnlinePanel(); }); |
| 3544 | |
| 3545 | var backs = document.querySelectorAll('[data-back-menu]'); |
| 3546 | for (var i = 0; i < backs.length; i++) |
| 3547 | { |
| 3548 | backs[i].addEventListener('click', function () |
| 3549 | { |
| 3550 | if (typeof Online !== 'undefined') |
| 3551 | { Online.stopPolling(); } |
| 3552 | hideEl('online-panel'); |
| 3553 | app.mode = 'menu'; |
| 3554 | app.online = false; |
| 3555 | showEl('menu'); |
| 3556 | }); |
| 3557 | } |
| 3558 | |
| 3559 | document.getElementById('btn-create').addEventListener('click', function () |
| 3560 | { |
| 3561 | Online.createLobby().then(function (data) |
| 3562 | { |
| 3563 | if (data && data.ok) |
| 3564 | { |
| 3565 | renderOnline(); |
| 3566 | Online.startPolling(onlinePayload); |
| 3567 | } |
| 3568 | else |
| 3569 | { renderOnline(); } |
| 3570 | }); |
| 3571 | }); |
| 3572 | |
| 3573 | document.getElementById('join-form').addEventListener('submit', function (e) |
| 3574 | { |
| 3575 | e.preventDefault(); |
| 3576 | var code = (document.getElementById('join-code').value || '').trim(); |
| 3577 | if (!code) |
| 3578 | { return; } |
| 3579 | Online.joinLobby(code).then(function () |
| 3580 | { |
| 3581 | renderOnline(); |
| 3582 | if (Online.lobby) |
| 3583 | { Online.startPolling(onlinePayload); } |
| 3584 | }); |
| 3585 | }); |
| 3586 | |
| 3587 | document.getElementById('btn-ready').addEventListener('click', function () |
| 3588 | { |
| 3589 | var me = Online.mePlayer(); |
| 3590 | Online.setReady(!(me && me.ready)).then(renderOnline); |
| 3591 | }); |
| 3592 | |
| 3593 | document.getElementById('btn-start').addEventListener('click', function () |
| 3594 | { |
| 3595 | Online.startGame().then(function (data) |
| 3596 | { |
| 3597 | if (data && data.ok) |
| 3598 | { startOnlineMatch(); } |
| 3599 | else |
| 3600 | { renderOnline(); } |
| 3601 | }); |
| 3602 | }); |
| 3603 | |
| 3604 | document.getElementById('btn-leave-lobby').addEventListener('click', function () |
| 3605 | { |
| 3606 | Online.leaveLobby().then(function () |
| 3607 | { |
| 3608 | Online.stopPolling(); |
| 3609 | renderOnline(); |
| 3610 | }); |
| 3611 | }); |
| 3612 | |
| 3613 | document.getElementById('btn-copy-code').addEventListener('click', function () |
| 3614 | { |
| 3615 | var code = document.getElementById('lobby-code').textContent; |
| 3616 | if (navigator.clipboard && navigator.clipboard.writeText) |
| 3617 | { navigator.clipboard.writeText(code); } |
| 3618 | }); |
| 3619 | |
| 3620 | document.getElementById('btn-spectate').addEventListener('click', function () |
| 3621 | { |
| 3622 | hideEl('spectator'); |
| 3623 | app.spectating = true; |
| 3624 | }); |
| 3625 | |
| 3626 | document.getElementById('btn-leave-game').addEventListener('click', function () |
| 3627 | { leaveOnlineMatch(false); }); |
| 3628 | |
| 3629 | if (typeof Online !== 'undefined') |
| 3630 | { |
| 3631 | Online.onChange = function () |
| 3632 | { renderOnline(); }; |
| 3633 | Online.onGameStart = function () |
| 3634 | { startOnlineMatch(); }; |
| 3635 | Online.onIncoming = function (incoming) |
| 3636 | { |
| 3637 | if (!app.core || app.core.state !== 'playing') |
| 3638 | { return; } |
| 3639 | for (var i = 0; i < incoming.length; i++) |
| 3640 | { app.core.ingestGarbage(); } |
| 3641 | }; |
| 3642 | Online.onFinished = function (lobby) |
| 3643 | { showPodium(lobby); }; |
| 3644 | Online.onLobbyReturn = function (lobby) |
| 3645 | { |
| 3646 | teardownPlay(); |
| 3647 | app.online = false; |
| 3648 | app.mode = 'online'; |
| 3649 | hideEl('podium'); |
| 3650 | showEl('online-panel'); |
| 3651 | renderOnline(); |
| 3652 | Online.startPolling(onlinePayload); |
| 3653 | }; |
| 3654 | } |
| 3655 | |
| 3656 | var params = new URLSearchParams(window.location.search); |
| 3657 | if (params.get('online') === '1' || params.get('auth') === 'unconfigured') |
| 3658 | { openOnlinePanel(); } |
| 3659 | } |
| 3660 | |
| 3661 | function openOnlinePanel () |
| 3662 | { |
| 3663 | hideEl('menu'); |
| 3664 | hideEl('game-over'); |
| 3665 | hideEl('podium'); |
| 3666 | hideEl('spectator'); |
| 3667 | showEl('online-panel'); |
| 3668 | app.mode = 'online'; |
| 3669 | setOnlineStatus('Even kijken of online spelen beschikbaar is…'); |
| 3670 | if (typeof Online === 'undefined') |
| 3671 | { |
| 3672 | showOnlineError('Online spelen is in deze build niet geladen. Je kunt wel alleen spelen.'); |
| 3673 | showOnly('online-unconfigured'); |
| 3674 | return; |
| 3675 | } |
| 3676 | Online.refresh().then(function (data) |
| 3677 | { |
| 3678 | if (!data || data.ok === false) |
| 3679 | { |
| 3680 | var msg = (data && data.error) || 'Geen verbinding met de server.'; |
| 3681 | showOnlineError(msg + ' Je kunt wel alleen spelen.'); |
| 3682 | if (data && data.configured === false) |
| 3683 | { showOnly('online-unconfigured'); } |
| 3684 | else |
| 3685 | { showOnly('online-login'); } |
| 3686 | document.getElementById('btn-login').style.display = 'none'; |
| 3687 | return; |
| 3688 | } |
| 3689 | renderOnline(); |
| 3690 | if (Online.lobby) |
| 3691 | { Online.startPolling(onlinePayload); } |
| 3692 | }); |
| 3693 | } |
| 3694 | |
| 3695 | function setOnlineStatus (text) |
| 3696 | { |
| 3697 | var el = document.getElementById('online-status'); |
| 3698 | if (el) |
| 3699 | { el.textContent = text; } |
| 3700 | } |
| 3701 | |
| 3702 | function showOnlineError (text) |
| 3703 | { |
| 3704 | var el = document.getElementById('online-error'); |
| 3705 | if (!el) |
| 3706 | { return; } |
| 3707 | if (!text) |
| 3708 | { |
| 3709 | el.hidden = true; |
| 3710 | el.textContent = ''; |
| 3711 | return; |
| 3712 | } |
| 3713 | el.hidden = false; |
| 3714 | el.textContent = text; |
| 3715 | } |
| 3716 | |
| 3717 | function showOnly (id) |
| 3718 | { |
| 3719 | var ids = ['online-unconfigured', 'online-login', 'online-home', 'online-lobby']; |
| 3720 | for (var i = 0; i < ids.length; i++) |
| 3721 | { document.getElementById(ids[i]).hidden = (ids[i] !== id); } |
| 3722 | } |
| 3723 | |
| 3724 | function renderOnline () |
| 3725 | { |
| 3726 | if (typeof Online === 'undefined') |
| 3727 | { return; } |
| 3728 | if (app.mode === 'playing' && app.online) |
| 3729 | { |
| 3730 | Online.renderMinis(document.getElementById('minis')); |
| 3731 | if (Online.lobby && Online.lobby.status === 'finished') |
| 3732 | { showPodium(Online.lobby); } |
| 3733 | return; |
| 3734 | } |
| 3735 | showOnlineError(Online.error); |
| 3736 | if (!Online.configured && !Online.me && !Online.local) |
| 3737 | { |
| 3738 | setOnlineStatus('Online spelen is nog niet ingesteld.'); |
| 3739 | showOnly('online-unconfigured'); |
| 3740 | return; |
| 3741 | } |
| 3742 | if (!Online.me) |
| 3743 | { |
| 3744 | setOnlineStatus('Log in om een lobby te maken of eraan deel te nemen.'); |
| 3745 | showOnly('online-login'); |
| 3746 | document.getElementById('btn-login').style.display = ''; |
| 3747 | document.getElementById('btn-login').href = Online.loginUrl(); |
| 3748 | document.getElementById('local-login').hidden = !Online.local; |
| 3749 | return; |
| 3750 | } |
| 3751 | if (Online.lobby) |
| 3752 | { |
| 3753 | setOnlineStatus('Lobby'); |
| 3754 | showOnly('online-lobby'); |
| 3755 | document.getElementById('lobby-me-name').textContent = Online.me.name; |
| 3756 | document.getElementById('lobby-code').textContent = Online.lobby.code; |
| 3757 | var list = document.getElementById('lobby-players'); |
| 3758 | list.innerHTML = ''; |
| 3759 | var players = Online.lobby.players || []; |
| 3760 | for (var i = 0; i < players.length; i++) |
| 3761 | { |
| 3762 | var p = players[i]; |
| 3763 | var li = document.createElement('li'); |
| 3764 | if (p.is_me) |
| 3765 | { li.classList.add('you'); } |
| 3766 | var label = p.name + (p.is_host ? ' (host)' : '') + (p.is_me ? ' (jij)' : ''); |
| 3767 | var state = p.ready ? 'Gereed' : 'Wacht…'; |
| 3768 | li.innerHTML = '<span>' + escapeText(label) + '</span><span class="' |
| 3769 | + (p.ready ? 'ready' : 'waiting') + '">' + state + '</span>'; |
| 3770 | list.appendChild(li); |
| 3771 | } |
| 3772 | var me = Online.mePlayer(); |
| 3773 | document.getElementById('btn-ready').textContent = (me && me.ready) ? 'Niet gereed' : 'Gereed'; |
| 3774 | var start = document.getElementById('btn-start'); |
| 3775 | start.hidden = !(Online.lobby && Online.me && Online.lobby.host_id === Online.me.id); |
| 3776 | start.disabled = !Online.lobby.can_start; |
| 3777 | var hint = document.getElementById('lobby-hint'); |
| 3778 | if (Online.lobby.status === 'playing') |
| 3779 | { hint.textContent = 'Het spel start…'; } |
| 3780 | else if (players.length < 2) |
| 3781 | { hint.textContent = 'Wacht op minstens één andere speler. Deel de code.'; } |
| 3782 | else if (!Online.lobby.all_ready) |
| 3783 | { hint.textContent = 'Iedereen moet op Gereed drukken. De host start daarna.'; } |
| 3784 | else if (Online.lobby.can_start) |
| 3785 | { hint.textContent = 'Iedereen is gereed. Druk op Starten.'; } |
| 3786 | else |
| 3787 | { hint.textContent = 'Wachten tot de host start.'; } |
| 3788 | return; |
| 3789 | } |
| 3790 | setOnlineStatus('Hoi ' + Online.me.name + '. Maak een lobby of val in met een code.'); |
| 3791 | showOnly('online-home'); |
| 3792 | document.getElementById('me-name').textContent = Online.me.name; |
| 3793 | document.getElementById('local-login').hidden = !Online.local; |
| 3794 | } |
| 3795 | |
| 3796 | function escapeText (s) |
| 3797 | { |
| 3798 | return String(s) |
| 3799 | .replace(/&/g, '&') |
| 3800 | .replace(/</g, '<') |
| 3801 | .replace(/>/g, '>') |
| 3802 | .replace(/"/g, '"'); |
| 3803 | } |
| 3804 | |
| 3805 | /* |
| 3806 | * End File: |
| 3807 | * /workspace/meteors/src/main.js |
| 3808 | */ |
projects-timfalken-com meteors