| 1 | /* |
| 2 | * AUTO-GENERATED FILE — DO NOT EDIT BY HAND. |
| 3 | * Built by src/build.sh from the sources in src/. |
| 4 | */ |
| 5 | var PLANET_CATALOG = { |
| 6 | "planets": [ |
| 7 | { |
| 8 | "id": "terra", |
| 9 | "name": "Terra", |
| 10 | "people": "Terrans", |
| 11 | "jumpsToClear": 2, |
| 12 | "fallTime": 3, |
| 13 | "riseTime": 1.5, |
| 14 | "colors": [ |
| 15 | {"id": 0, "name": "ember", "hex": "#e85d3a", "shape": "triangle", "image": "images/block-terra-ember.png"}, |
| 16 | {"id": 1, "name": "aqua", "hex": "#3db8e8", "shape": "circle", "image": "images/block-terra-aqua.png"}, |
| 17 | {"id": 2, "name": "leaf", "hex": "#4cbf4c", "shape": "square", "image": "images/block-terra-leaf.png"}, |
| 18 | {"id": 3, "name": "sun", "hex": "#f0c53a", "shape": "diamond", "image": "images/block-terra-sun.png"}, |
| 19 | {"id": 4, "name": "orchid", "hex": "#c45ad4", "shape": "star", "image": "images/block-terra-orchid.png"} |
| 20 | ] |
| 21 | }, |
| 22 | { |
| 23 | "id": "aurel", |
| 24 | "name": "Aurel", |
| 25 | "people": "Aurelians", |
| 26 | "jumpsToClear": 1, |
| 27 | "fallTime": 5, |
| 28 | "riseTime": 3, |
| 29 | "colors": [ |
| 30 | {"id": 0, "name": "gold", "hex": "#f4c860", "shape": "hexagon", "image": "images/block-aurel-gold.png"}, |
| 31 | {"id": 1, "name": "cream", "hex": "#f6e7c1", "shape": "circle", "image": "images/block-aurel-cream.png"}, |
| 32 | {"id": 2, "name": "amber", "hex": "#e09a3e", "shape": "teardrop", "image": "images/block-aurel-amber.png"}, |
| 33 | {"id": 3, "name": "sage", "hex": "#8fb86f", "shape": "plus", "image": "images/block-aurel-sage.png"}, |
| 34 | {"id": 4, "name": "rose", "hex": "#e8a0a8", "shape": "pentagon", "image": "images/block-aurel-rose.png"} |
| 35 | ] |
| 36 | }, |
| 37 | { |
| 38 | "id": "cinder", |
| 39 | "name": "Cinder", |
| 40 | "people": "Cindari", |
| 41 | "jumpsToClear": 4, |
| 42 | "fallTime": 1, |
| 43 | "riseTime": 0.5, |
| 44 | "colors": [ |
| 45 | {"id": 0, "name": "magma", "hex": "#ff3b1f", "shape": "triangle", "image": "images/block-cinder-magma.png"}, |
| 46 | {"id": 1, "name": "coal", "hex": "#3a3232", "shape": "square", "image": "images/block-cinder-coal.png"}, |
| 47 | {"id": 2, "name": "spark", "hex": "#ffb347", "shape": "diamond", "image": "images/block-cinder-spark.png"}, |
| 48 | {"id": 3, "name": "ash", "hex": "#8a7a76", "shape": "circle", "image": "images/block-cinder-ash.png"}, |
| 49 | {"id": 4, "name": "core", "hex": "#ff6a00", "shape": "star", "image": "images/block-cinder-core.png"} |
| 50 | ] |
| 51 | }, |
| 52 | { |
| 53 | "id": "lunara", |
| 54 | "name": "Lunara", |
| 55 | "people": "Lunarites", |
| 56 | "jumpsToClear": 1, |
| 57 | "fallTime": 2, |
| 58 | "riseTime": 1.2, |
| 59 | "colors": [ |
| 60 | {"id": 0, "name": "silver", "hex": "#c5d0e0", "shape": "circle", "image": "images/block-lunara-silver.png"}, |
| 61 | {"id": 1, "name": "dust", "hex": "#8b90a0", "shape": "diamond", "image": "images/block-lunara-dust.png"}, |
| 62 | {"id": 2, "name": "pale", "hex": "#e8d9a8", "shape": "crescent", "image": "images/block-lunara-pale.png"}, |
| 63 | {"id": 3, "name": "void", "hex": "#2c3348", "shape": "square", "image": "images/block-lunara-void.png"}, |
| 64 | {"id": 4, "name": "lilac", "hex": "#b9a4d4", "shape": "star", "image": "images/block-lunara-lilac.png"} |
| 65 | ] |
| 66 | }, |
| 67 | { |
| 68 | "id": "mire", |
| 69 | "name": "Mire", |
| 70 | "people": "Mirians", |
| 71 | "jumpsToClear": 2, |
| 72 | "fallTime": 5, |
| 73 | "riseTime": 2.5, |
| 74 | "colors": [ |
| 75 | {"id": 0, "name": "moss", "hex": "#3d7a45", "shape": "hexagon", "image": "images/block-mire-moss.png"}, |
| 76 | {"id": 1, "name": "bog", "hex": "#1f4d3a", "shape": "teardrop", "image": "images/block-mire-bog.png"}, |
| 77 | {"id": 2, "name": "rust", "hex": "#8b5a2b", "shape": "pentagon", "image": "images/block-mire-rust.png"}, |
| 78 | {"id": 3, "name": "spore", "hex": "#9cbf5a", "shape": "circle", "image": "images/block-mire-spore.png"}, |
| 79 | {"id": 4, "name": "murk", "hex": "#2e3b2f", "shape": "diamond", "image": "images/block-mire-murk.png"} |
| 80 | ] |
| 81 | }, |
| 82 | { |
| 83 | "id": "galeon", |
| 84 | "name": "Galeon", |
| 85 | "people": "Galeoni", |
| 86 | "jumpsToClear": 3, |
| 87 | "fallTime": 1, |
| 88 | "riseTime": 1.8, |
| 89 | "colors": [ |
| 90 | {"id": 0, "name": "sky", "hex": "#4aa3ff", "shape": "arrow", "image": "images/block-galeon-sky.png"}, |
| 91 | {"id": 1, "name": "cloud", "hex": "#e8eef6", "shape": "circle", "image": "images/block-galeon-cloud.png"}, |
| 92 | {"id": 2, "name": "storm", "hex": "#3d4d6e", "shape": "square", "image": "images/block-galeon-storm.png"}, |
| 93 | {"id": 3, "name": "sand", "hex": "#d4b483", "shape": "diamond","image": "images/block-galeon-sand.png"}, |
| 94 | {"id": 4, "name": "kite", "hex": "#7fd0c0", "shape": "star", "image": "images/block-galeon-kite.png"} |
| 95 | ] |
| 96 | }, |
| 97 | { |
| 98 | "id": "obsid", |
| 99 | "name": "Obsid", |
| 100 | "people": "Obsidians", |
| 101 | "jumpsToClear": 4, |
| 102 | "fallTime": 3.5, |
| 103 | "riseTime": 2.2, |
| 104 | "colors": [ |
| 105 | {"id": 0, "name": "glass", "hex": "#1a1a22", "shape": "gem", "image": "images/block-obsid-glass.png"}, |
| 106 | {"id": 1, "name": "violet","hex": "#5a3d8a", "shape": "hexagon", "image": "images/block-obsid-violet.png"}, |
| 107 | {"id": 2, "name": "shard", "hex": "#8a8aa0", "shape": "triangle", "image": "images/block-obsid-shard.png"}, |
| 108 | {"id": 3, "name": "vein", "hex": "#c45a3a", "shape": "diamond", "image": "images/block-obsid-vein.png"}, |
| 109 | {"id": 4, "name": "basalt","hex": "#3a3a42", "shape": "square", "image": "images/block-obsid-basalt.png"} |
| 110 | ] |
| 111 | }, |
| 112 | { |
| 113 | "id": "wisp", |
| 114 | "name": "Wisp", |
| 115 | "people": "Wisplings", |
| 116 | "jumpsToClear": 2, |
| 117 | "fallTime": 3.5, |
| 118 | "riseTime": 0.5, |
| 119 | "colors": [ |
| 120 | {"id": 0, "name": "glow", "hex": "#c8ffe0", "shape": "circle", "image": "images/block-wisp-glow.png"}, |
| 121 | {"id": 1, "name": "mist", "hex": "#a8d4e8", "shape": "diamond", "image": "images/block-wisp-mist.png"}, |
| 122 | {"id": 2, "name": "sparkle", "hex": "#fff6a8", "shape": "star", "image": "images/block-wisp-sparkle.png"}, |
| 123 | {"id": 3, "name": "veil", "hex": "#d0b8f0", "shape": "teardrop", "image": "images/block-wisp-veil.png"}, |
| 124 | {"id": 4, "name": "gleam", "hex": "#f4f7ff", "shape": "plus", "image": "images/block-wisp-gleam.png"} |
| 125 | ] |
| 126 | }, |
| 127 | { |
| 128 | "id": "thorn", |
| 129 | "name": "Thorn", |
| 130 | "people": "Thornfolk", |
| 131 | "jumpsToClear": 3, |
| 132 | "fallTime": 2, |
| 133 | "riseTime": 1.0, |
| 134 | "colors": [ |
| 135 | {"id": 0, "name": "briar", "hex": "#5a7a30", "shape": "triangle", "image": "images/block-thorn-briar.png"}, |
| 136 | {"id": 1, "name": "berry", "hex": "#8b1a4a", "shape": "circle", "image": "images/block-thorn-berry.png"}, |
| 137 | {"id": 2, "name": "bark", "hex": "#5a3a20", "shape": "square", "image": "images/block-thorn-bark.png"}, |
| 138 | {"id": 3, "name": "petal", "hex": "#e89ab0", "shape": "diamond", "image": "images/block-thorn-petal.png"}, |
| 139 | {"id": 4, "name": "gold", "hex": "#c8a030", "shape": "star", "image": "images/block-thorn-gold.png"} |
| 140 | ] |
| 141 | }, |
| 142 | { |
| 143 | "id": "zephyr", |
| 144 | "name": "Zephyr", |
| 145 | "people": "Zephyrites", |
| 146 | "jumpsToClear": 1, |
| 147 | "fallTime": 1.2, |
| 148 | "riseTime": 2.8, |
| 149 | "colors": [ |
| 150 | {"id": 0, "name": "azure", "hex": "#6ec8ff", "shape": "crescent", "image": "images/block-zephyr-azure.png"}, |
| 151 | {"id": 1, "name": "breeze","hex": "#b8e8ff", "shape": "circle", "image": "images/block-zephyr-breeze.png"}, |
| 152 | {"id": 2, "name": "cirrus","hex": "#f0f7ff", "shape": "plus", "image": "images/block-zephyr-cirrus.png"}, |
| 153 | {"id": 3, "name": "dusk", "hex": "#7a6ab0", "shape": "diamond", "image": "images/block-zephyr-dusk.png"}, |
| 154 | {"id": 4, "name": "haze", "hex": "#ffe08a", "shape": "star", "image": "images/block-zephyr-haze.png"} |
| 155 | ] |
| 156 | }, |
| 157 | { |
| 158 | "id": "frore", |
| 159 | "name": "Frore", |
| 160 | "people": "Frorians", |
| 161 | "jumpsToClear": 3, |
| 162 | "fallTime": 4.5, |
| 163 | "riseTime": 0.8, |
| 164 | "colors": [ |
| 165 | {"id": 0, "name": "ice", "hex": "#a8d8f0", "shape": "hexagon", "image": "images/block-frore-ice.png"}, |
| 166 | {"id": 1, "name": "frost", "hex": "#eef7ff", "shape": "circle", "image": "images/block-frore-frost.png"}, |
| 167 | {"id": 2, "name": "deep", "hex": "#3a6a8a", "shape": "diamond", "image": "images/block-frore-deep.png"}, |
| 168 | {"id": 3, "name": "aurora", "hex": "#5ae8c0", "shape": "star", "image": "images/block-frore-aurora.png"}, |
| 169 | {"id": 4, "name": "rime", "hex": "#d0e4f0", "shape": "triangle", "image": "images/block-frore-rime.png"} |
| 170 | ] |
| 171 | }, |
| 172 | { |
| 173 | "id": "forge", |
| 174 | "name": "Forge", |
| 175 | "people": "Forgelings", |
| 176 | "jumpsToClear": 4, |
| 177 | "fallTime": 2, |
| 178 | "riseTime": 1.5, |
| 179 | "colors": [ |
| 180 | {"id": 0, "name": "iron", "hex": "#6a6a72", "shape": "square", "image": "images/block-forge-iron.png"}, |
| 181 | {"id": 1, "name": "slag", "hex": "#3a3028", "shape": "hexagon", "image": "images/block-forge-slag.png"}, |
| 182 | {"id": 2, "name": "spark", "hex": "#ff7a1a", "shape": "triangle", "image": "images/block-forge-spark.png"}, |
| 183 | {"id": 3, "name": "brass", "hex": "#c8a048", "shape": "diamond", "image": "images/block-forge-brass.png"}, |
| 184 | {"id": 4, "name": "soot", "hex": "#2a2a28", "shape": "circle", "image": "images/block-forge-soot.png"} |
| 185 | ] |
| 186 | } |
| 187 | ] |
| 188 | } |
| 189 | ; |
| 190 | |
| 191 | /* |
| 192 | * Start File: |
| 193 | * /workspace/meteors/src/rng.js |
| 194 | */ |
| 195 | 'use strict'; |
| 196 | |
| 197 | /** |
| 198 | * Seedable mulberry32 RNG, so the game core stays deterministic in tests |
| 199 | * and (later) for multiplayer replays. |
| 200 | */ |
| 201 | function createRng (seed) |
| 202 | { |
| 203 | var a = (seed >>> 0) || 1; |
| 204 | return function rng () |
| 205 | { |
| 206 | a |= 0; |
| 207 | a = a + 0x6D2B79F5 | 0; |
| 208 | var t = Math.imul(a ^ a >>> 15, 1 | a); |
| 209 | t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; |
| 210 | return ((t ^ t >>> 14) >>> 0) / 4294967296; |
| 211 | }; |
| 212 | } |
| 213 | |
| 214 | function pickIndex (rng, count) |
| 215 | { |
| 216 | var n = count | 0; |
| 217 | if (n <= 0) |
| 218 | { return 0; } |
| 219 | return Math.floor(rng() * n) % n; |
| 220 | } |
| 221 | |
| 222 | function pickFrom (rng, list) |
| 223 | { |
| 224 | if (!list || !list.length) |
| 225 | { return undefined; } |
| 226 | return list[pickIndex(rng, list.length)]; |
| 227 | } |
| 228 | |
| 229 | var Rng = { |
| 230 | create: createRng, |
| 231 | pickIndex: pickIndex, |
| 232 | pickFrom: pickFrom |
| 233 | }; |
| 234 | |
| 235 | if (typeof module !== 'undefined' && module.exports) |
| 236 | { module.exports = Rng; } |
| 237 | else if (typeof globalThis !== 'undefined') |
| 238 | { globalThis.Rng = Rng; } |
| 239 | |
| 240 | /* |
| 241 | * End File: |
| 242 | * /workspace/meteors/src/rng.js |
| 243 | */ |
| 244 | |
| 245 | /* |
| 246 | * Start File: |
| 247 | * /workspace/meteors/src/planet.js |
| 248 | */ |
| 249 | 'use strict'; |
| 250 | |
| 251 | /** |
| 252 | * Planet physics and visuals for Meteors. |
| 253 | * |
| 254 | * Stage 1 uses a single default planet. Stage 3 lets the player pick a |
| 255 | * planet; the numbers and jump-height rules are already applied here so |
| 256 | * that later planets only need new data, not new physics code. |
| 257 | * |
| 258 | * Gravity (jumpsToClear): 1 (easy, high jump) .. 4 (hard, short jump). |
| 259 | * A horizontal match of 3 jumps FIELD_HEIGHT / jumpsToClear tiles — |
| 260 | * at 1, one jump from the floor clears the field; at 4, you need four. |
| 261 | * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall). |
| 262 | * After a jump peaks, blocks float down at FIELD_HEIGHT / fallTime tiles/s. |
| 263 | * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump). |
| 264 | * Time to reach the apex, independent of jump height. |
| 265 | * |
| 266 | * Jump-height multipliers (never explained in the game): |
| 267 | * A vertical match of 3 is 120 % of a horizontal row of 3. |
| 268 | * Each extra block beyond 3 adds 5 %. |
| 269 | * |
| 270 | * Power is the average of the three difficulty axes (0 easy .. 1 hard) |
| 271 | * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at |
| 272 | * power 0 to 5 % at power 1. |
| 273 | */ |
| 274 | |
| 275 | var FIELD_WIDTH = 10; |
| 276 | var FIELD_HEIGHT = 13; |
| 277 | |
| 278 | function clamp (value, lo, hi) |
| 279 | { |
| 280 | return Math.max(lo, Math.min(hi, value)); |
| 281 | } |
| 282 | |
| 283 | function defaultColors () |
| 284 | { |
| 285 | return [ |
| 286 | { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' }, |
| 287 | { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' }, |
| 288 | { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' }, |
| 289 | { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' }, |
| 290 | { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' } |
| 291 | ]; |
| 292 | } |
| 293 | |
| 294 | function createPlanet (opts) |
| 295 | { |
| 296 | opts = opts || {}; |
| 297 | var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4); |
| 298 | var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5); |
| 299 | var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3); |
| 300 | var colors = opts.colors || defaultColors(); |
| 301 | var planet = { |
| 302 | id: opts.id || null, |
| 303 | name: opts.name || 'Terra', |
| 304 | people: opts.people || 'Terrans', |
| 305 | jumpsToClear: jumpsToClear, |
| 306 | fallTime: fallTime, |
| 307 | riseTime: riseTime, |
| 308 | colors: colors, |
| 309 | baseJumpHeight: FIELD_HEIGHT / jumpsToClear, |
| 310 | fallSpeed: FIELD_HEIGHT / fallTime, |
| 311 | // Time to ease from vy=0 at the apex to terminal fallSpeed. |
| 312 | // Shorter when air resistance is low (small fallTime). |
| 313 | apexEaseTime: 0.15 * fallTime, |
| 314 | fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime), |
| 315 | // Visual landing bounce: faster at low air resistance. Stage-3 params. |
| 316 | bounceDuration: 0.12 * fallTime, |
| 317 | bounceSquash: 0.16 |
| 318 | }; |
| 319 | return planet; |
| 320 | } |
| 321 | |
| 322 | function loadCatalog () |
| 323 | { |
| 324 | if (typeof PLANET_CATALOG !== 'undefined' && PLANET_CATALOG && PLANET_CATALOG.planets) |
| 325 | { return PLANET_CATALOG; } |
| 326 | if (typeof require !== 'undefined') |
| 327 | { |
| 328 | try |
| 329 | { return require('../server/lib/planets.json'); } |
| 330 | catch (e) {} |
| 331 | } |
| 332 | return { planets: [] }; |
| 333 | } |
| 334 | |
| 335 | function planetFromData (data) |
| 336 | { |
| 337 | var planet = createPlanet({ |
| 338 | name: data.name, |
| 339 | people: data.people, |
| 340 | jumpsToClear: data.jumpsToClear, |
| 341 | fallTime: data.fallTime, |
| 342 | riseTime: data.riseTime, |
| 343 | colors: data.colors |
| 344 | }); |
| 345 | planet.id = data.id; |
| 346 | return planet; |
| 347 | } |
| 348 | |
| 349 | function allPlanets () |
| 350 | { |
| 351 | var raw = loadCatalog().planets || []; |
| 352 | var out = []; |
| 353 | for (var i = 0; i < raw.length; i++) |
| 354 | { out.push(planetFromData(raw[i])); } |
| 355 | if (out.length === 0) |
| 356 | { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; } |
| 357 | return out; |
| 358 | } |
| 359 | |
| 360 | function planetById (id) |
| 361 | { |
| 362 | var all = allPlanets(); |
| 363 | for (var i = 0; i < all.length; i++) |
| 364 | { |
| 365 | if (all[i].id === id) |
| 366 | { return all[i]; } |
| 367 | } |
| 368 | return defaultPlanet(); |
| 369 | } |
| 370 | |
| 371 | function defaultPlanet () |
| 372 | { |
| 373 | var all = allPlanets(); |
| 374 | for (var i = 0; i < all.length; i++) |
| 375 | { |
| 376 | if (all[i].id === 'terra') |
| 377 | { return all[i]; } |
| 378 | } |
| 379 | return all[0]; |
| 380 | } |
| 381 | |
| 382 | function formatStats (planet) |
| 383 | { |
| 384 | var d = displayValues(planet); |
| 385 | return 'g ' + d.gravity.toFixed(2) |
| 386 | + ' d ' + d.density.toFixed(2) |
| 387 | + ' F ' + Math.round(d.newtons) + ' N' |
| 388 | + ' Power ' + d.power + '%'; |
| 389 | } |
| 390 | |
| 391 | function spriteKey (planet, colorIndex) |
| 392 | { |
| 393 | var colors = planet && planet.colors; |
| 394 | var c = colors && colors[colorIndex]; |
| 395 | if (!c) |
| 396 | { return 'gray'; } |
| 397 | return (planet.id || 'planet') + '-' + (c.name || String(colorIndex)); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Jump height in tiles for a match line. |
| 402 | * @param {object} planet |
| 403 | * @param {number} matchLength number of blocks in the line (>= 3) |
| 404 | * @param {boolean} vertical true for a vertical line |
| 405 | */ |
| 406 | function jumpHeight (planet, matchLength, vertical) |
| 407 | { |
| 408 | var extra = Math.max(0, (matchLength | 0) - 3); |
| 409 | var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra; |
| 410 | return planet.baseJumpHeight * multiplier; |
| 411 | } |
| 412 | |
| 413 | /** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */ |
| 414 | function launchVelocity (planet, height) |
| 415 | { |
| 416 | if (height <= 0) |
| 417 | { return 0; } |
| 418 | return (2 * height) / planet.riseTime; |
| 419 | } |
| 420 | |
| 421 | /** Constant deceleration (tiles/s^2) paired with launchVelocity. */ |
| 422 | function launchDeceleration (planet, height) |
| 423 | { |
| 424 | if (height <= 0) |
| 425 | { return 0; } |
| 426 | return (2 * height) / (planet.riseTime * planet.riseTime); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower |
| 431 | * chance that a shot-off block misses the opponent). |
| 432 | */ |
| 433 | function power (planet) |
| 434 | { |
| 435 | var gravity = (planet.jumpsToClear - 1) / 3; |
| 436 | var air = (5 - planet.fallTime) / 4; |
| 437 | var mass = (3 - planet.riseTime) / 2.5; |
| 438 | return (gravity + air + mass) / 3; |
| 439 | } |
| 440 | |
| 441 | /** Maximum miss chance, used by the gameplay formula and the displayed Power. */ |
| 442 | var MAX_MISS_CHANCE = 0.60; |
| 443 | var MIN_MISS_CHANCE = 0.05; |
| 444 | |
| 445 | /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */ |
| 446 | function missChance (planet) |
| 447 | { |
| 448 | return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet); |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * Displayed Power, as a 0–100 percentage. This is the complement of the |
| 453 | * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss |
| 454 | * → 0% Power. The gameplay power() and missChance() formulas are unchanged. |
| 455 | */ |
| 456 | function displayPower (planet) |
| 457 | { |
| 458 | return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³), |
| 463 | * launch force (N), and displayed Power as a percentage. The numbers do |
| 464 | * not have to explain themselves. |
| 465 | */ |
| 466 | function displayValues (planet) |
| 467 | { |
| 468 | var gravity = 9.81 * planet.jumpsToClear / 2; |
| 469 | var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60; |
| 470 | var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160; |
| 471 | return { |
| 472 | gravity: gravity, |
| 473 | density: density, |
| 474 | newtons: newtons, |
| 475 | power: displayPower(planet), |
| 476 | missChance: missChance(planet) |
| 477 | }; |
| 478 | } |
| 479 | |
| 480 | var Planet = { |
| 481 | FIELD_WIDTH: FIELD_WIDTH, |
| 482 | FIELD_HEIGHT: FIELD_HEIGHT, |
| 483 | COLOR_COUNT: 5, |
| 484 | SETTLE_TIME: 3, |
| 485 | SPAWN_INTERVAL: 1, |
| 486 | JUMP_VERTICAL_BONUS: 1.2, |
| 487 | JUMP_EXTRA_BLOCK: 0.05, |
| 488 | createPlanet: createPlanet, |
| 489 | defaultPlanet: defaultPlanet, |
| 490 | defaultColors: defaultColors, |
| 491 | allPlanets: allPlanets, |
| 492 | planetById: planetById, |
| 493 | planetFromData: planetFromData, |
| 494 | formatStats: formatStats, |
| 495 | spriteKey: spriteKey, |
| 496 | jumpHeight: jumpHeight, |
| 497 | launchVelocity: launchVelocity, |
| 498 | launchDeceleration: launchDeceleration, |
| 499 | MAX_MISS_CHANCE: MAX_MISS_CHANCE, |
| 500 | MIN_MISS_CHANCE: MIN_MISS_CHANCE, |
| 501 | power: power, |
| 502 | missChance: missChance, |
| 503 | displayPower: displayPower, |
| 504 | displayValues: displayValues |
| 505 | }; |
| 506 | |
| 507 | if (typeof module !== 'undefined' && module.exports) |
| 508 | { module.exports = Planet; } |
| 509 | else if (typeof globalThis !== 'undefined') |
| 510 | { globalThis.Planet = Planet; } |
| 511 | |
| 512 | /* |
| 513 | * End File: |
| 514 | * /workspace/meteors/src/planet.js |
| 515 | */ |
| 516 | |
| 517 | /* |
| 518 | * Start File: |
| 519 | * /workspace/meteors/src/easing.js |
| 520 | */ |
| 521 | 'use strict'; |
| 522 | |
| 523 | /** |
| 524 | * Easing curves matching Qt QEasingCurve defaults (the same values Tim's |
| 525 | * KWin Click Tween effect uses). Formulas and default parameters come from |
| 526 | * Qt 6's QEasingCurve and https://easings.net/. |
| 527 | * |
| 528 | * Source of the click/release defaults: |
| 529 | * /workspace/click-tween/settings.h (clickTime, releaseTime, easing names) |
| 530 | * /workspace/click-tween/clicktween.cpp (press scale 0.8, release scale 1.0) |
| 531 | * /workspace/click-tween/README.md |
| 532 | */ |
| 533 | |
| 534 | /** Qt QEasingCurve default overshoot for InBack / OutBack / InOutBack. */ |
| 535 | var BACK_OVERSHOOT = 1.70158; |
| 536 | |
| 537 | /** Qt QEasingCurve default period and amplitude for Elastic curves. */ |
| 538 | var ELASTIC_PERIOD = 0.3; |
| 539 | var ELASTIC_AMPLITUDE = 1.0; |
| 540 | |
| 541 | function clamp01 (t) |
| 542 | { |
| 543 | if (t <= 0) |
| 544 | { return 0; } |
| 545 | if (t >= 1) |
| 546 | { return 1; } |
| 547 | return t; |
| 548 | } |
| 549 | |
| 550 | /** easeOutBack: overshoots the end value, then settles. Qt / easings.net. */ |
| 551 | function easeOutBack (t) |
| 552 | { |
| 553 | t = clamp01(t); |
| 554 | var c1 = BACK_OVERSHOOT; |
| 555 | var c3 = c1 + 1; |
| 556 | var u = t - 1; |
| 557 | return 1 + c3 * u * u * u + c1 * u * u; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * easeOutElastic: decaying sine overshoot past the end value. |
| 562 | * Qt default amplitude 1, period 0.3 (equivalent to easings.net). |
| 563 | */ |
| 564 | function easeOutElastic (t) |
| 565 | { |
| 566 | t = clamp01(t); |
| 567 | if (t === 0 || t === 1) |
| 568 | { return t; } |
| 569 | var p = ELASTIC_PERIOD; |
| 570 | var s = p / 4; |
| 571 | return Math.pow(2, -10 * t) * Math.sin((t - s) * (2 * Math.PI) / p) + 1; |
| 572 | } |
| 573 | |
| 574 | function tween (from, to, t, ease) |
| 575 | { |
| 576 | return from + (to - from) * ease(clamp01(t)); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * DEFAULT settings of Tim's KWin Click Tween |
| 581 | * (git.timfalken.com/timfa/click-tween). |
| 582 | * |
| 583 | * Press: shrink to 80% in 150 ms with easeOutBack, stay at 80% while held. |
| 584 | * Release: grow back to 100% in 720 ms with easeOutElastic. |
| 585 | * Mid-animation release starts from the current scale, like QVariantAnimation. |
| 586 | */ |
| 587 | var CLICK_TWEEN = { |
| 588 | pressScale: 0.8, |
| 589 | releaseScale: 1.0, |
| 590 | pressDuration: 0.150, |
| 591 | releaseDuration: 0.720, |
| 592 | pressEase: easeOutBack, |
| 593 | releaseEase: easeOutElastic, |
| 594 | backOvershoot: BACK_OVERSHOOT, |
| 595 | elasticPeriod: ELASTIC_PERIOD, |
| 596 | elasticAmplitude: ELASTIC_AMPLITUDE, |
| 597 | source: 'click-tween settings.h + clicktween.cpp (Qt QEasingCurve defaults)' |
| 598 | }; |
| 599 | |
| 600 | /** |
| 601 | * Visual landing bounce, u in 0..1. Subtle squash with a slight stretch |
| 602 | * on the other axis and a tiny upward rebound (in tiles, visual only). |
| 603 | */ |
| 604 | function bouncePose (u, squash) |
| 605 | { |
| 606 | if (u <= 0 || u >= 1) |
| 607 | { return { sx: 1, sy: 1, dy: 0 }; } |
| 608 | var wave = Math.sin(Math.PI * u); |
| 609 | return { |
| 610 | sx: 1 + squash * 0.6 * wave, |
| 611 | sy: 1 - squash * wave, |
| 612 | dy: 0.07 * wave * (1 - u) |
| 613 | }; |
| 614 | } |
| 615 | |
| 616 | var Easing = { |
| 617 | BACK_OVERSHOOT: BACK_OVERSHOOT, |
| 618 | ELASTIC_PERIOD: ELASTIC_PERIOD, |
| 619 | ELASTIC_AMPLITUDE: ELASTIC_AMPLITUDE, |
| 620 | easeOutBack: easeOutBack, |
| 621 | easeOutElastic: easeOutElastic, |
| 622 | tween: tween, |
| 623 | CLICK_TWEEN: CLICK_TWEEN, |
| 624 | clamp01: clamp01, |
| 625 | bouncePose: bouncePose |
| 626 | }; |
| 627 | |
| 628 | if (typeof module !== 'undefined' && module.exports) |
| 629 | { module.exports = Easing; } |
| 630 | else if (typeof globalThis !== 'undefined') |
| 631 | { globalThis.Easing = Easing; } |
| 632 | |
| 633 | /* |
| 634 | * End File: |
| 635 | * /workspace/meteors/src/easing.js |
| 636 | */ |
| 637 | |
| 638 | /* |
| 639 | * Start File: |
| 640 | * /workspace/meteors/src/gameCore.js |
| 641 | */ |
| 642 | 'use strict'; |
| 643 | |
| 644 | /** |
| 645 | * Deterministic Meteors game core. No DOM, no rendering, no input devices. |
| 646 | * The view layer reads this state and translates pointer events into |
| 647 | * beginDrag / moveDrag / endDrag. |
| 648 | */ |
| 649 | |
| 650 | var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined') |
| 651 | ? require('./planet.js') |
| 652 | : (typeof Planet !== 'undefined' ? Planet : globalThis.Planet); |
| 653 | |
| 654 | var COLOR_GRAY = -1; |
| 655 | |
| 656 | var STATE_STILL = 'still'; |
| 657 | var STATE_FALLING = 'falling'; |
| 658 | var STATE_FLYING = 'flying'; |
| 659 | var STATE_SETTLING = 'settling'; |
| 660 | var STATE_LOCKED = 'locked'; |
| 661 | var STATE_MANUAL = 'manual'; |
| 662 | |
| 663 | var ALIGN_EPS = 0.15; |
| 664 | var SUBSTEP = 1 / 120; |
| 665 | var DRAG_LAUNCH_SLACK = 0.65; |
| 666 | // Upward pull (tiles past the press point) that launches an already-topmost block. |
| 667 | var LAUNCH_PULL = 0.35; |
| 668 | // Minimal accumulated vertical pointer travel (tiles) to swap one step. |
| 669 | // No hold delay: the first swap fires as soon as this distance is reached. |
| 670 | var SWAP_DISTANCE = 0.25; |
| 671 | // Grab a still block if the pointer is at most this far (tiles) from it. |
| 672 | var SNAP_Y = 0.65; |
| 673 | // Horizontal slop (tiles) for snapping to a neighbouring column on press. |
| 674 | var HORIZONTAL_SLOP = 0.35; |
| 675 | |
| 676 | function UnionFind () |
| 677 | { |
| 678 | this.parent = Object.create(null); |
| 679 | } |
| 680 | |
| 681 | UnionFind.prototype.add = function (id) |
| 682 | { |
| 683 | if (this.parent[id] == null) |
| 684 | { this.parent[id] = id; } |
| 685 | }; |
| 686 | |
| 687 | UnionFind.prototype.find = function (id) |
| 688 | { |
| 689 | if (this.parent[id] !== id) |
| 690 | { this.parent[id] = this.find(this.parent[id]); } |
| 691 | return this.parent[id]; |
| 692 | }; |
| 693 | |
| 694 | UnionFind.prototype.union = function (a, b) |
| 695 | { |
| 696 | this.add(a); |
| 697 | this.add(b); |
| 698 | var ra = this.find(a); |
| 699 | var rb = this.find(b); |
| 700 | if (ra !== rb) |
| 701 | { this.parent[rb] = ra; } |
| 702 | }; |
| 703 | |
| 704 | function GameCore (planet, rng) |
| 705 | { |
| 706 | this.planet = planet || PlanetMod.defaultPlanet(); |
| 707 | this.rng = rng || Math.random; |
| 708 | this.width = PlanetMod.FIELD_WIDTH; |
| 709 | this.height = PlanetMod.FIELD_HEIGHT; |
| 710 | this.blocks = []; |
| 711 | this.clusters = Object.create(null); |
| 712 | this.nextBlockId = 1; |
| 713 | this.nextClusterId = 1; |
| 714 | this.spawnTimer = 0; |
| 715 | this.spawnInterval = PlanetMod.SPAWN_INTERVAL; |
| 716 | this.settleTime = PlanetMod.SETTLE_TIME; |
| 717 | this.score = 0; |
| 718 | this.state = 'playing'; |
| 719 | this.drag = null; |
| 720 | this.gameOverTime = 0; |
| 721 | this.outgoingShots = []; |
| 722 | // Stage-2 default; stage 3 replaces this with Planet.missChance (Power). |
| 723 | this.missChance = optsMissChance(arguments); |
| 724 | } |
| 725 | |
| 726 | function optsMissChance (args) |
| 727 | { |
| 728 | // GameCore(planet, rng, missChance) — third arg is optional. |
| 729 | if (args.length >= 3 && typeof args[2] === 'number' && isFinite(args[2])) |
| 730 | { return Math.max(0, Math.min(1, args[2])); } |
| 731 | var planet = args[0]; |
| 732 | if (planet && typeof PlanetMod.missChance === 'function') |
| 733 | { return PlanetMod.missChance(planet); } |
| 734 | return GameCore.DEFAULT_MISS_CHANCE; |
| 735 | } |
| 736 | |
| 737 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 738 | GameCore.LAUNCH_PULL = LAUNCH_PULL; |
| 739 | GameCore.SWAP_DISTANCE = SWAP_DISTANCE; |
| 740 | GameCore.SNAP_Y = SNAP_Y; |
| 741 | GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP; |
| 742 | GameCore.DEFAULT_MISS_CHANCE = 0.30; |
| 743 | GameCore.STATE_STILL = STATE_STILL; |
| 744 | GameCore.STATE_FALLING = STATE_FALLING; |
| 745 | GameCore.STATE_FLYING = STATE_FLYING; |
| 746 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 747 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 748 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 749 | |
| 750 | GameCore.prototype.randomColor = function () |
| 751 | { |
| 752 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 753 | }; |
| 754 | |
| 755 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 756 | { |
| 757 | var block = { |
| 758 | id: this.nextBlockId++, |
| 759 | col: col, |
| 760 | y: y, |
| 761 | color: color, |
| 762 | state: state || STATE_STILL, |
| 763 | vy: 0, |
| 764 | clusterId: null, |
| 765 | settleTime: 0, |
| 766 | flame: false, |
| 767 | pushedClusters: null, |
| 768 | manualTarget: 0, |
| 769 | manualDecel: 0, |
| 770 | manualPhase: null, |
| 771 | bouncing: false, |
| 772 | bounceAge: 0 |
| 773 | }; |
| 774 | this.blocks.push(block); |
| 775 | return block; |
| 776 | }; |
| 777 | |
| 778 | GameCore.prototype.removeBlock = function (block) |
| 779 | { |
| 780 | if (block.clusterId != null) |
| 781 | { this.leaveCluster(block); } |
| 782 | var index = this.blocks.indexOf(block); |
| 783 | if (index >= 0) |
| 784 | { this.blocks.splice(index, 1); } |
| 785 | }; |
| 786 | |
| 787 | GameCore.prototype.leaveCluster = function (block) |
| 788 | { |
| 789 | var id = block.clusterId; |
| 790 | block.clusterId = null; |
| 791 | block.flame = false; |
| 792 | if (id == null || !this.clusters[id]) |
| 793 | { return; } |
| 794 | var still = 0; |
| 795 | for (var i = 0; i < this.blocks.length; i++) |
| 796 | { |
| 797 | if (this.blocks[i].clusterId === id) |
| 798 | { still++; } |
| 799 | } |
| 800 | if (still === 0) |
| 801 | { delete this.clusters[id]; } |
| 802 | }; |
| 803 | |
| 804 | GameCore.prototype.blocksInColumn = function (col) |
| 805 | { |
| 806 | var out = []; |
| 807 | for (var i = 0; i < this.blocks.length; i++) |
| 808 | { |
| 809 | if (this.blocks[i].col === col) |
| 810 | { out.push(this.blocks[i]); } |
| 811 | } |
| 812 | out.sort(function (a, b) { return a.y - b.y; }); |
| 813 | return out; |
| 814 | }; |
| 815 | |
| 816 | GameCore.prototype.isGrounded = function (block) |
| 817 | { |
| 818 | return block.state === STATE_STILL |
| 819 | || block.state === STATE_SETTLING |
| 820 | || block.state === STATE_LOCKED; |
| 821 | }; |
| 822 | |
| 823 | GameCore.prototype.stackTop = function (col, ignore) |
| 824 | { |
| 825 | var top = 0; |
| 826 | for (var i = 0; i < this.blocks.length; i++) |
| 827 | { |
| 828 | var block = this.blocks[i]; |
| 829 | if (block.col !== col) |
| 830 | { continue; } |
| 831 | if (ignore && ignore(block)) |
| 832 | { continue; } |
| 833 | if (this.isGrounded(block)) |
| 834 | { |
| 835 | var surface = block.y + 1; |
| 836 | if (surface > top) |
| 837 | { top = surface; } |
| 838 | } |
| 839 | } |
| 840 | return top; |
| 841 | }; |
| 842 | |
| 843 | GameCore.prototype.isColumnLocked = function (col) |
| 844 | { |
| 845 | for (var i = 0; i < this.blocks.length; i++) |
| 846 | { |
| 847 | var block = this.blocks[i]; |
| 848 | if (block.col === col && block.state === STATE_LOCKED) |
| 849 | { return true; } |
| 850 | } |
| 851 | return false; |
| 852 | }; |
| 853 | |
| 854 | GameCore.prototype.isColumnStill = function (col) |
| 855 | { |
| 856 | if (this.isColumnLocked(col)) |
| 857 | { return false; } |
| 858 | for (var i = 0; i < this.blocks.length; i++) |
| 859 | { |
| 860 | var block = this.blocks[i]; |
| 861 | if (block.col !== col) |
| 862 | { continue; } |
| 863 | if (block.state === STATE_FLYING |
| 864 | || block.state === STATE_FALLING |
| 865 | || block.state === STATE_MANUAL |
| 866 | || block.state === STATE_SETTLING) |
| 867 | { return false; } |
| 868 | } |
| 869 | return true; |
| 870 | }; |
| 871 | |
| 872 | GameCore.prototype.groundedCount = function (col) |
| 873 | { |
| 874 | var n = 0; |
| 875 | for (var i = 0; i < this.blocks.length; i++) |
| 876 | { |
| 877 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 878 | { n++; } |
| 879 | } |
| 880 | return n; |
| 881 | }; |
| 882 | |
| 883 | GameCore.prototype.blockAt = function (col, row) |
| 884 | { |
| 885 | for (var i = 0; i < this.blocks.length; i++) |
| 886 | { |
| 887 | var block = this.blocks[i]; |
| 888 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 889 | { return block; } |
| 890 | } |
| 891 | return null; |
| 892 | }; |
| 893 | |
| 894 | GameCore.prototype.place = function (col, row, color, state) |
| 895 | { |
| 896 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 897 | }; |
| 898 | |
| 899 | GameCore.prototype.lockedColumnCount = function () |
| 900 | { |
| 901 | var n = 0; |
| 902 | for (var c = 0; c < this.width; c++) |
| 903 | { |
| 904 | if (this.isColumnLocked(c)) |
| 905 | { n++; } |
| 906 | } |
| 907 | return n; |
| 908 | }; |
| 909 | |
| 910 | GameCore.prototype.startBounce = function (block) |
| 911 | { |
| 912 | if (!block) |
| 913 | { return; } |
| 914 | block.bouncing = true; |
| 915 | block.bounceAge = 0; |
| 916 | }; |
| 917 | |
| 918 | GameCore.prototype.advanceBounces = function (dt) |
| 919 | { |
| 920 | var duration = this.planet.bounceDuration; |
| 921 | for (var i = 0; i < this.blocks.length; i++) |
| 922 | { |
| 923 | var block = this.blocks[i]; |
| 924 | if (!block.bouncing) |
| 925 | { continue; } |
| 926 | block.bounceAge += dt; |
| 927 | if (duration <= 0 || block.bounceAge >= duration) |
| 928 | { |
| 929 | block.bouncing = false; |
| 930 | block.bounceAge = duration; |
| 931 | } |
| 932 | } |
| 933 | }; |
| 934 | |
| 935 | /** |
| 936 | * True if `block` rests on the floor or on a solid (still / settling / |
| 937 | * locked) block immediately below it in the same column. |
| 938 | */ |
| 939 | GameCore.prototype.hasSupport = function (block) |
| 940 | { |
| 941 | if (block.y <= 1e-9) |
| 942 | { return true; } |
| 943 | for (var i = 0; i < this.blocks.length; i++) |
| 944 | { |
| 945 | var other = this.blocks[i]; |
| 946 | if (other === block || other.col !== block.col) |
| 947 | { continue; } |
| 948 | if (!this.isGrounded(other)) |
| 949 | { continue; } |
| 950 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 951 | { return true; } |
| 952 | } |
| 953 | return false; |
| 954 | }; |
| 955 | |
| 956 | /** Highest flying block in this column whose top is at or below `block.y + 1`. */ |
| 957 | GameCore.prototype.highestFlyingBelow = function (block) |
| 958 | { |
| 959 | var best = null; |
| 960 | for (var i = 0; i < this.blocks.length; i++) |
| 961 | { |
| 962 | var other = this.blocks[i]; |
| 963 | if (other === block || other.col !== block.col) |
| 964 | { continue; } |
| 965 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 966 | { continue; } |
| 967 | var cluster = this.clusters[other.clusterId]; |
| 968 | if (!cluster || cluster.phase === 'landed') |
| 969 | { continue; } |
| 970 | // The falling block is at or above this flyer (or already overlapping). |
| 971 | if (block.y + 1e-9 < other.y) |
| 972 | { continue; } |
| 973 | if (!best || other.y > best.y) |
| 974 | { best = other; } |
| 975 | } |
| 976 | return best; |
| 977 | }; |
| 978 | |
| 979 | GameCore.prototype.highestFallingBelow = function (block) |
| 980 | { |
| 981 | var best = null; |
| 982 | for (var i = 0; i < this.blocks.length; i++) |
| 983 | { |
| 984 | var other = this.blocks[i]; |
| 985 | if (other === block || other.col !== block.col) |
| 986 | { continue; } |
| 987 | if (other.state !== STATE_FALLING) |
| 988 | { continue; } |
| 989 | if (other.y > block.y - 1e-9) |
| 990 | { continue; } |
| 991 | if (!best || other.y > best.y) |
| 992 | { best = other; } |
| 993 | } |
| 994 | return best; |
| 995 | }; |
| 996 | |
| 997 | /** |
| 998 | * Make an unsupported still block start falling from rest, so it eases |
| 999 | * into fallSpeed under normal gravity. Skips the block being dragged. |
| 1000 | */ |
| 1001 | /** |
| 1002 | * Strip leftover launch / manual kinematics so a block that has just |
| 1003 | * become still or falling starts from rest. Used after a landed grey |
| 1004 | * cluster recolors, and whenever an unsupported still block drops. |
| 1005 | */ |
| 1006 | GameCore.prototype.clearFlightState = function (block) |
| 1007 | { |
| 1008 | block.vy = 0; |
| 1009 | block.flame = false; |
| 1010 | block.manualPhase = null; |
| 1011 | block.manualTarget = null; |
| 1012 | block.manualDecel = 0; |
| 1013 | block.pushedClusters = null; |
| 1014 | }; |
| 1015 | |
| 1016 | GameCore.prototype.releaseUnsupported = function () |
| 1017 | { |
| 1018 | for (var i = 0; i < this.blocks.length; i++) |
| 1019 | { |
| 1020 | var block = this.blocks[i]; |
| 1021 | if (block.state !== STATE_STILL) |
| 1022 | { continue; } |
| 1023 | if (this.drag && this.drag.blockId === block.id) |
| 1024 | { continue; } |
| 1025 | if (this.hasSupport(block)) |
| 1026 | { continue; } |
| 1027 | this.leaveCluster(block); |
| 1028 | this.clearFlightState(block); |
| 1029 | block.state = STATE_FALLING; |
| 1030 | } |
| 1031 | }; |
| 1032 | |
| 1033 | GameCore.prototype.accelerateFall = function (block, dt) |
| 1034 | { |
| 1035 | var target = -this.planet.fallSpeed; |
| 1036 | if (block.vy > target) |
| 1037 | { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); } |
| 1038 | else |
| 1039 | { block.vy = target; } |
| 1040 | }; |
| 1041 | |
| 1042 | GameCore.prototype.landFallingBlock = function (block) |
| 1043 | { |
| 1044 | block.vy = 0; |
| 1045 | block.flame = false; |
| 1046 | this.startBounce(block); |
| 1047 | if (block.color === COLOR_GRAY) |
| 1048 | { |
| 1049 | block.state = STATE_SETTLING; |
| 1050 | block.settleTime = 0; |
| 1051 | } |
| 1052 | else |
| 1053 | { block.state = STATE_STILL; } |
| 1054 | }; |
| 1055 | |
| 1056 | /** |
| 1057 | * Sit `block` on top of the flying cluster that `flyer` belongs to, and |
| 1058 | * carry it along as cargo (no flame). Returns true if attached. |
| 1059 | */ |
| 1060 | GameCore.prototype.attachToFlyingCluster = function (block, flyer) |
| 1061 | { |
| 1062 | var cluster = this.clusters[flyer.clusterId]; |
| 1063 | if (!cluster || cluster.phase === 'landed') |
| 1064 | { return false; } |
| 1065 | var roof = flyer.y + 1; |
| 1066 | for (var i = 0; i < this.blocks.length; i++) |
| 1067 | { |
| 1068 | var other = this.blocks[i]; |
| 1069 | if (other === block || other.col !== block.col) |
| 1070 | { continue; } |
| 1071 | if (other.clusterId !== cluster.id) |
| 1072 | { continue; } |
| 1073 | if (other.y + 1 > roof) |
| 1074 | { roof = other.y + 1; } |
| 1075 | } |
| 1076 | block.y = roof; |
| 1077 | block.state = STATE_FLYING; |
| 1078 | block.clusterId = cluster.id; |
| 1079 | block.vy = cluster.vy; |
| 1080 | block.flame = false; |
| 1081 | block.settleTime = 0; |
| 1082 | return true; |
| 1083 | }; |
| 1084 | |
| 1085 | GameCore.prototype.catchFallingOntoCluster = function (cluster, members) |
| 1086 | { |
| 1087 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1088 | { return; } |
| 1089 | // Snapshot falling blocks; attaching mutates state. |
| 1090 | var falling = []; |
| 1091 | for (var i = 0; i < this.blocks.length; i++) |
| 1092 | { |
| 1093 | if (this.blocks[i].state === STATE_FALLING) |
| 1094 | { falling.push(this.blocks[i]); } |
| 1095 | } |
| 1096 | for (var i = 0; i < falling.length; i++) |
| 1097 | { |
| 1098 | var block = falling[i]; |
| 1099 | var flyer = null; |
| 1100 | for (var m = 0; m < members.length; m++) |
| 1101 | { |
| 1102 | var other = members[m]; |
| 1103 | if (other.col !== block.col) |
| 1104 | { continue; } |
| 1105 | // Overlap or rest on this member's top. |
| 1106 | if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9) |
| 1107 | { |
| 1108 | if (!flyer || other.y > flyer.y) |
| 1109 | { flyer = other; } |
| 1110 | } |
| 1111 | } |
| 1112 | if (flyer) |
| 1113 | { this.attachToFlyingCluster(block, flyer); } |
| 1114 | } |
| 1115 | }; |
| 1116 | |
| 1117 | GameCore.prototype.tick = function (dt) |
| 1118 | { |
| 1119 | if (this.state === 'gameover') |
| 1120 | { |
| 1121 | this.gameOverTime += Math.max(0, dt); |
| 1122 | return; |
| 1123 | } |
| 1124 | if (dt <= 0) |
| 1125 | { return; } |
| 1126 | dt = Math.min(dt, 0.1); |
| 1127 | |
| 1128 | this.spawnTimer += dt; |
| 1129 | while (this.spawnTimer >= this.spawnInterval) |
| 1130 | { |
| 1131 | this.spawnTimer -= this.spawnInterval; |
| 1132 | this.trySpawn(); |
| 1133 | } |
| 1134 | |
| 1135 | var left = dt; |
| 1136 | while (left > 1e-12) |
| 1137 | { |
| 1138 | var step = Math.min(SUBSTEP, left); |
| 1139 | this.integrateFalling(step); |
| 1140 | this.resolveFlyingMatches(); |
| 1141 | this.integrateClusters(step); |
| 1142 | this.mergeFlyingClusters(); |
| 1143 | this.integrateManual(step); |
| 1144 | this.cullOffTop(); |
| 1145 | left -= step; |
| 1146 | } |
| 1147 | |
| 1148 | this.releaseUnsupported(); |
| 1149 | this.advanceSettle(dt); |
| 1150 | // After a landed grey group is released, blocks that were sitting on |
| 1151 | // hanging members are still STILL (the earlier pass ran while those |
| 1152 | // members counted as support). Drop them from rest before matching, |
| 1153 | // so they cannot form a mid-air match and shoot off the top. |
| 1154 | this.releaseUnsupported(); |
| 1155 | this.advanceBounces(dt); |
| 1156 | if (!this.drag) |
| 1157 | { this.resolveMatches(); } |
| 1158 | this.checkLocks(); |
| 1159 | this.checkGameOver(); |
| 1160 | }; |
| 1161 | |
| 1162 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 1163 | GameCore.prototype.step = function (seconds, dt) |
| 1164 | { |
| 1165 | dt = dt || SUBSTEP; |
| 1166 | var t = 0; |
| 1167 | while (t < seconds - 1e-15) |
| 1168 | { |
| 1169 | var s = Math.min(dt, seconds - t); |
| 1170 | this.tick(s); |
| 1171 | t += s; |
| 1172 | } |
| 1173 | }; |
| 1174 | |
| 1175 | GameCore.prototype.trySpawn = function () |
| 1176 | { |
| 1177 | if (this.state !== 'playing') |
| 1178 | { return null; } |
| 1179 | var eligible = []; |
| 1180 | for (var col = 0; col < this.width; col++) |
| 1181 | { |
| 1182 | if (this.isColumnLocked(col)) |
| 1183 | { continue; } |
| 1184 | if (this.groundedCount(col) >= this.height) |
| 1185 | { continue; } |
| 1186 | var alreadyFalling = false; |
| 1187 | var list = this.blocksInColumn(col); |
| 1188 | for (var i = 0; i < list.length; i++) |
| 1189 | { |
| 1190 | if (list[i].state === STATE_FALLING) |
| 1191 | { |
| 1192 | alreadyFalling = true; |
| 1193 | break; |
| 1194 | } |
| 1195 | } |
| 1196 | if (!alreadyFalling) |
| 1197 | { eligible.push(col); } |
| 1198 | } |
| 1199 | if (!eligible.length) |
| 1200 | { return null; } |
| 1201 | var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 1202 | var color = this.randomColor(); |
| 1203 | var block = this.createBlock(col, this.height, color, STATE_FALLING); |
| 1204 | block.vy = -this.planet.fallSpeed; |
| 1205 | return block; |
| 1206 | }; |
| 1207 | |
| 1208 | GameCore.prototype.integrateFalling = function (dt) |
| 1209 | { |
| 1210 | var falling = []; |
| 1211 | for (var i = 0; i < this.blocks.length; i++) |
| 1212 | { |
| 1213 | if (this.blocks[i].state === STATE_FALLING) |
| 1214 | { falling.push(this.blocks[i]); } |
| 1215 | } |
| 1216 | // Lowest first, so a stack of falling blocks rests on itself. |
| 1217 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 1218 | for (var i = 0; i < falling.length; i++) |
| 1219 | { |
| 1220 | var block = falling[i]; |
| 1221 | if (block.state !== STATE_FALLING) |
| 1222 | { continue; } |
| 1223 | this.accelerateFall(block, dt); |
| 1224 | var nextY = block.y + block.vy * dt; |
| 1225 | var flyer = this.highestFlyingBelow(block); |
| 1226 | if (flyer && nextY <= flyer.y + 1 + 1e-9) |
| 1227 | { |
| 1228 | if (this.attachToFlyingCluster(block, flyer)) |
| 1229 | { continue; } |
| 1230 | } |
| 1231 | var top = this.stackTop(block.col); |
| 1232 | var below = this.highestFallingBelow(block); |
| 1233 | if (below && below.y + 1 > top) |
| 1234 | { top = below.y + 1; } |
| 1235 | if (nextY <= top) |
| 1236 | { |
| 1237 | block.y = top; |
| 1238 | this.landFallingBlock(block); |
| 1239 | } |
| 1240 | else |
| 1241 | { block.y = nextY; } |
| 1242 | } |
| 1243 | }; |
| 1244 | |
| 1245 | GameCore.prototype.clusterMembers = function (clusterId) |
| 1246 | { |
| 1247 | var out = []; |
| 1248 | for (var i = 0; i < this.blocks.length; i++) |
| 1249 | { |
| 1250 | if (this.blocks[i].clusterId === clusterId) |
| 1251 | { out.push(this.blocks[i]); } |
| 1252 | } |
| 1253 | return out; |
| 1254 | }; |
| 1255 | |
| 1256 | |
| 1257 | /** |
| 1258 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 1259 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 1260 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 1261 | * upward deceleration. After the apex, vy eases from 0 to -fallSpeed |
| 1262 | * at planet.fallAccel, then stays at terminal fallSpeed. |
| 1263 | */ |
| 1264 | GameCore.prototype.advanceJump = function (body, dt) |
| 1265 | { |
| 1266 | var dy = 0; |
| 1267 | var left = dt; |
| 1268 | if (body.phase === 'up') |
| 1269 | { |
| 1270 | var decel = body.decel; |
| 1271 | var vy = body.vy; |
| 1272 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 1273 | if (tApex <= left + 1e-15) |
| 1274 | { |
| 1275 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 1276 | body.vy = 0; |
| 1277 | body.phase = 'down'; |
| 1278 | left -= tApex; |
| 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | var vyNew = vy - decel * left; |
| 1283 | dy += (vy + vyNew) * 0.5 * left; |
| 1284 | body.vy = vyNew; |
| 1285 | left = 0; |
| 1286 | } |
| 1287 | } |
| 1288 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 1289 | { |
| 1290 | body.vy = 0; |
| 1291 | body.phase = 'down'; |
| 1292 | } |
| 1293 | if (body.phase === 'down' && left > 0) |
| 1294 | { |
| 1295 | var target = -this.planet.fallSpeed; |
| 1296 | var accel = this.planet.fallAccel; |
| 1297 | if (accel < 1e-12) |
| 1298 | { |
| 1299 | body.vy = target; |
| 1300 | dy += body.vy * left; |
| 1301 | } |
| 1302 | else if (body.vy > target + 1e-12) |
| 1303 | { |
| 1304 | var tReach = (body.vy - target) / accel; |
| 1305 | if (tReach <= left + 1e-15) |
| 1306 | { |
| 1307 | dy += (body.vy + target) * 0.5 * tReach; |
| 1308 | body.vy = target; |
| 1309 | left -= tReach; |
| 1310 | dy += body.vy * left; |
| 1311 | } |
| 1312 | else |
| 1313 | { |
| 1314 | var vyNew = body.vy - accel * left; |
| 1315 | dy += (body.vy + vyNew) * 0.5 * left; |
| 1316 | body.vy = vyNew; |
| 1317 | } |
| 1318 | } |
| 1319 | else |
| 1320 | { |
| 1321 | body.vy = target; |
| 1322 | dy += body.vy * left; |
| 1323 | } |
| 1324 | } |
| 1325 | return dy; |
| 1326 | }; |
| 1327 | |
| 1328 | GameCore.prototype.integrateClusters = function (dt) |
| 1329 | { |
| 1330 | var ids = Object.keys(this.clusters); |
| 1331 | for (var n = 0; n < ids.length; n++) |
| 1332 | { |
| 1333 | var cluster = this.clusters[ids[n]]; |
| 1334 | if (!cluster) |
| 1335 | { continue; } |
| 1336 | var members = this.clusterMembers(cluster.id); |
| 1337 | if (!members.length) |
| 1338 | { |
| 1339 | delete this.clusters[cluster.id]; |
| 1340 | continue; |
| 1341 | } |
| 1342 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1343 | { continue; } |
| 1344 | |
| 1345 | var dy = this.advanceJump(cluster, dt); |
| 1346 | for (var i = 0; i < members.length; i++) |
| 1347 | { |
| 1348 | members[i].y += dy; |
| 1349 | members[i].vy = cluster.vy; |
| 1350 | } |
| 1351 | |
| 1352 | this.catchFallingOntoCluster(cluster, members); |
| 1353 | this.checkChainRelaunch(cluster); |
| 1354 | |
| 1355 | if (cluster.phase === 'down' && this.clusters[cluster.id]) |
| 1356 | { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); } |
| 1357 | } |
| 1358 | }; |
| 1359 | |
| 1360 | |
| 1361 | /** |
| 1362 | * If two or more flying clusters overlap in the same column, merge them |
| 1363 | * into one cluster. Velocity (and deceleration and jumpHeight) is the |
| 1364 | * mass-weighted average by block count. Phase is 'up' when the merged |
| 1365 | * vy is positive, otherwise 'down'. Overlapping members in a column |
| 1366 | * are packed so the higher one sits flush on the lower. |
| 1367 | */ |
| 1368 | GameCore.prototype.mergeFlyingClusters = function () |
| 1369 | { |
| 1370 | var flying = []; |
| 1371 | var ids = Object.keys(this.clusters); |
| 1372 | for (var n = 0; n < ids.length; n++) |
| 1373 | { |
| 1374 | var cluster = this.clusters[ids[n]]; |
| 1375 | if (!cluster) |
| 1376 | { continue; } |
| 1377 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 1378 | { continue; } |
| 1379 | flying.push(cluster); |
| 1380 | } |
| 1381 | if (flying.length < 2) |
| 1382 | { return; } |
| 1383 | |
| 1384 | var uf = new UnionFind(); |
| 1385 | for (var i = 0; i < flying.length; i++) |
| 1386 | { uf.add(flying[i].id); } |
| 1387 | for (var i = 0; i < flying.length; i++) |
| 1388 | { |
| 1389 | for (var j = i + 1; j < flying.length; j++) |
| 1390 | { |
| 1391 | if (this.flyingClustersOverlap(flying[i], flying[j])) |
| 1392 | { uf.union(flying[i].id, flying[j].id); } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | var groups = Object.create(null); |
| 1397 | for (var i = 0; i < flying.length; i++) |
| 1398 | { |
| 1399 | var root = uf.find(flying[i].id); |
| 1400 | if (!groups[root]) |
| 1401 | { groups[root] = []; } |
| 1402 | groups[root].push(flying[i]); |
| 1403 | } |
| 1404 | for (var root in groups) |
| 1405 | { |
| 1406 | if (groups[root].length >= 2) |
| 1407 | { this.mergeClusterGroup(groups[root]); } |
| 1408 | } |
| 1409 | }; |
| 1410 | |
| 1411 | GameCore.prototype.flyingClustersOverlap = function (a, b) |
| 1412 | { |
| 1413 | var ma = this.clusterMembers(a.id); |
| 1414 | var mb = this.clusterMembers(b.id); |
| 1415 | for (var i = 0; i < ma.length; i++) |
| 1416 | { |
| 1417 | for (var j = 0; j < mb.length; j++) |
| 1418 | { |
| 1419 | if (ma[i].col !== mb[j].col) |
| 1420 | { continue; } |
| 1421 | if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9) |
| 1422 | { return true; } |
| 1423 | } |
| 1424 | } |
| 1425 | return false; |
| 1426 | }; |
| 1427 | |
| 1428 | GameCore.prototype.mergeClusterGroup = function (clusters) |
| 1429 | { |
| 1430 | clusters.sort(function (a, b) { return a.id - b.id; }); |
| 1431 | var keep = clusters[0]; |
| 1432 | var n = 0; |
| 1433 | var vySum = 0; |
| 1434 | var decelSum = 0; |
| 1435 | var hSum = 0; |
| 1436 | for (var c = 0; c < clusters.length; c++) |
| 1437 | { |
| 1438 | var mem = this.clusterMembers(clusters[c].id); |
| 1439 | var m = mem.length; |
| 1440 | n += m; |
| 1441 | vySum += clusters[c].vy * m; |
| 1442 | decelSum += (clusters[c].decel || 0) * m; |
| 1443 | hSum += (clusters[c].jumpHeight || 0) * m; |
| 1444 | } |
| 1445 | if (n === 0) |
| 1446 | { return; } |
| 1447 | keep.vy = vySum / n; |
| 1448 | keep.decel = decelSum / n; |
| 1449 | keep.jumpHeight = hSum / n; |
| 1450 | keep.phase = keep.vy > 1e-10 ? 'up' : 'down'; |
| 1451 | for (var c = 0; c < clusters.length; c++) |
| 1452 | { |
| 1453 | var mem = this.clusterMembers(clusters[c].id); |
| 1454 | for (var i = 0; i < mem.length; i++) |
| 1455 | { |
| 1456 | mem[i].clusterId = keep.id; |
| 1457 | mem[i].vy = keep.vy; |
| 1458 | mem[i].state = STATE_FLYING; |
| 1459 | } |
| 1460 | if (clusters[c] !== keep) |
| 1461 | { delete this.clusters[clusters[c].id]; } |
| 1462 | } |
| 1463 | this.packClusterOverlaps(keep); |
| 1464 | this.checkChainRelaunch(keep); |
| 1465 | if (keep.phase === 'down' && this.clusters[keep.id]) |
| 1466 | { this.tryLandCluster(keep, this.clusterMembers(keep.id)); } |
| 1467 | }; |
| 1468 | |
| 1469 | GameCore.prototype.packClusterOverlaps = function (cluster) |
| 1470 | { |
| 1471 | var members = this.clusterMembers(cluster.id); |
| 1472 | var byCol = Object.create(null); |
| 1473 | for (var i = 0; i < members.length; i++) |
| 1474 | { |
| 1475 | var col = members[i].col; |
| 1476 | if (!byCol[col]) |
| 1477 | { byCol[col] = []; } |
| 1478 | byCol[col].push(members[i]); |
| 1479 | } |
| 1480 | var cols = Object.keys(byCol); |
| 1481 | for (var c = 0; c < cols.length; c++) |
| 1482 | { |
| 1483 | var list = byCol[cols[c]]; |
| 1484 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1485 | for (var i = 1; i < list.length; i++) |
| 1486 | { |
| 1487 | var minY = list[i - 1].y + 1; |
| 1488 | if (list[i].y < minY - 1e-12) |
| 1489 | { list[i].y = minY; } |
| 1490 | } |
| 1491 | } |
| 1492 | }; |
| 1493 | |
| 1494 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 1495 | { |
| 1496 | if (!members.length) |
| 1497 | { return; } |
| 1498 | var shift = 0; |
| 1499 | var hit = false; |
| 1500 | for (var i = 0; i < members.length; i++) |
| 1501 | { |
| 1502 | var block = members[i]; |
| 1503 | var top = this.stackTop(block.col, function (other) |
| 1504 | { |
| 1505 | return other.clusterId === cluster.id; |
| 1506 | }); |
| 1507 | if (block.y <= top + 1e-9) |
| 1508 | { |
| 1509 | hit = true; |
| 1510 | var need = top - block.y; |
| 1511 | if (need > shift) |
| 1512 | { shift = need; } |
| 1513 | } |
| 1514 | } |
| 1515 | if (!hit) |
| 1516 | { return; } |
| 1517 | // Lift the whole rigid body so the deepest contact sits flush. |
| 1518 | for (var i = 0; i < members.length; i++) |
| 1519 | { members[i].y += shift; } |
| 1520 | this.landCluster(cluster, members); |
| 1521 | }; |
| 1522 | |
| 1523 | /** |
| 1524 | * Land as a rigid group: relative positions are kept, so part of the |
| 1525 | * cluster may hang above a gap. Grey members start the 3s settle timer; |
| 1526 | * coloured cargo stays put with them until every grey has recolored. |
| 1527 | * Members that actually rest on the floor or on another block bounce. |
| 1528 | */ |
| 1529 | GameCore.prototype.landCluster = function (cluster, members) |
| 1530 | { |
| 1531 | for (var i = 0; i < members.length; i++) |
| 1532 | { |
| 1533 | var block = members[i]; |
| 1534 | block.vy = 0; |
| 1535 | block.flame = false; |
| 1536 | block.clusterId = cluster.id; |
| 1537 | if (block.color === COLOR_GRAY) |
| 1538 | { |
| 1539 | block.state = STATE_SETTLING; |
| 1540 | block.settleTime = 0; |
| 1541 | } |
| 1542 | else |
| 1543 | { |
| 1544 | // Cargo stays in the rigid group (not matchable) until release. |
| 1545 | block.state = STATE_SETTLING; |
| 1546 | block.settleTime = 0; |
| 1547 | } |
| 1548 | if (this.hasSupport(block)) |
| 1549 | { this.startBounce(block); } |
| 1550 | } |
| 1551 | cluster.phase = 'landed'; |
| 1552 | cluster.vy = 0; |
| 1553 | }; |
| 1554 | |
| 1555 | /** |
| 1556 | * After every grey in a landed cluster has recolored, let the group go. |
| 1557 | * Unsupported blocks fall individually from rest; supported ones stay. |
| 1558 | */ |
| 1559 | GameCore.prototype.releaseLandedCluster = function (cluster) |
| 1560 | { |
| 1561 | var members = this.clusterMembers(cluster.id); |
| 1562 | var releasing = Object.create(null); |
| 1563 | for (var i = 0; i < members.length; i++) |
| 1564 | { releasing[members[i].id] = true; } |
| 1565 | |
| 1566 | for (var i = 0; i < members.length; i++) |
| 1567 | { |
| 1568 | var block = members[i]; |
| 1569 | if (block.color === COLOR_GRAY) |
| 1570 | { |
| 1571 | block.color = this.randomColor(); |
| 1572 | block.settleTime = 0; |
| 1573 | } |
| 1574 | this.leaveCluster(block); |
| 1575 | // Always drop leftover launch / manual velocity. A stale upward |
| 1576 | // vy here is what sends blocks off the top "very fast" after |
| 1577 | // a grey group recolors. |
| 1578 | this.clearFlightState(block); |
| 1579 | // Support from outside the group (floor or a foreign block). Fellow |
| 1580 | // members that will also fall do not count, so a hanging stack |
| 1581 | // drops together without the top one staying put. |
| 1582 | var supported = this.hasSupportOutside(block, releasing); |
| 1583 | block.state = supported ? STATE_STILL : STATE_FALLING; |
| 1584 | } |
| 1585 | delete this.clusters[cluster.id]; |
| 1586 | }; |
| 1587 | |
| 1588 | GameCore.prototype.hasSupportOutside = function (block, ignoreIds) |
| 1589 | { |
| 1590 | if (block.y <= 1e-9) |
| 1591 | { return true; } |
| 1592 | for (var i = 0; i < this.blocks.length; i++) |
| 1593 | { |
| 1594 | var other = this.blocks[i]; |
| 1595 | if (other === block || other.col !== block.col) |
| 1596 | { continue; } |
| 1597 | if (ignoreIds && ignoreIds[other.id]) |
| 1598 | { continue; } |
| 1599 | if (!this.isGrounded(other)) |
| 1600 | { continue; } |
| 1601 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 1602 | { return true; } |
| 1603 | } |
| 1604 | return false; |
| 1605 | }; |
| 1606 | |
| 1607 | GameCore.prototype.integrateManual = 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_MANUAL) |
| 1613 | { continue; } |
| 1614 | |
| 1615 | if (block.manualPhase === 'up') |
| 1616 | { |
| 1617 | var oldY = block.y; |
| 1618 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 1619 | var dy = this.advanceJump(proxy, dt); |
| 1620 | var nextY = oldY + dy; |
| 1621 | this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget)); |
| 1622 | block.vy = proxy.vy; |
| 1623 | block.manualPhase = proxy.phase; |
| 1624 | block.y = Math.min(nextY, block.manualTarget); |
| 1625 | if (block.y >= block.manualTarget - 1e-12) |
| 1626 | { |
| 1627 | block.y = block.manualTarget; |
| 1628 | block.vy = 0; |
| 1629 | block.manualPhase = 'down'; |
| 1630 | } |
| 1631 | if (block.y > this.height - 1) |
| 1632 | { |
| 1633 | block.y = this.height - 1; |
| 1634 | block.vy = 0; |
| 1635 | block.manualPhase = 'down'; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | if (block.manualPhase === 'down') |
| 1640 | { |
| 1641 | var proxy = { phase: 'down', vy: block.vy, decel: 0 }; |
| 1642 | var dy = this.advanceJump(proxy, dt); |
| 1643 | block.vy = proxy.vy; |
| 1644 | var nextY = block.y + dy; |
| 1645 | var top = this.stackTop(block.col); |
| 1646 | if (nextY <= top) |
| 1647 | { |
| 1648 | block.y = top; |
| 1649 | block.state = STATE_STILL; |
| 1650 | block.vy = 0; |
| 1651 | block.manualPhase = null; |
| 1652 | block.flame = false; |
| 1653 | this.startBounce(block); |
| 1654 | } |
| 1655 | else |
| 1656 | { |
| 1657 | block.y = Math.min(nextY, this.height - 1); |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | }; |
| 1662 | |
| 1663 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 1664 | { |
| 1665 | if (!manual.pushedClusters) |
| 1666 | { manual.pushedClusters = Object.create(null); } |
| 1667 | |
| 1668 | var manualTopOld = oldY + 1; |
| 1669 | var manualTopNew = nextY + 1; |
| 1670 | var low = Math.min(oldY, nextY); |
| 1671 | var high = Math.max(oldY, nextY) + 1; |
| 1672 | |
| 1673 | for (var i = 0; i < this.blocks.length; i++) |
| 1674 | { |
| 1675 | var other = this.blocks[i]; |
| 1676 | if (other === manual) |
| 1677 | { continue; } |
| 1678 | if (other.col !== manual.col) |
| 1679 | { continue; } |
| 1680 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 1681 | { continue; } |
| 1682 | if (manual.pushedClusters[other.clusterId]) |
| 1683 | { continue; } |
| 1684 | var otherBot = other.y; |
| 1685 | var otherTop = other.y + 1; |
| 1686 | var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9; |
| 1687 | var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9; |
| 1688 | if (overlapping || crossing) |
| 1689 | { |
| 1690 | if (this.pushClusterColumn(other.clusterId, manual.col)) |
| 1691 | { |
| 1692 | manual.pushedClusters[other.clusterId] = true; |
| 1693 | return; |
| 1694 | } |
| 1695 | } |
| 1696 | } |
| 1697 | }; |
| 1698 | |
| 1699 | |
| 1700 | /** |
| 1701 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 1702 | * Other columns stay put (they remain linked). Then look for a new |
| 1703 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 1704 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 1705 | * with the same jump height from its current position. |
| 1706 | */ |
| 1707 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 1708 | { |
| 1709 | var cluster = this.clusters[clusterId]; |
| 1710 | if (!cluster) |
| 1711 | { return false; } |
| 1712 | var members = this.clusterMembers(clusterId); |
| 1713 | for (var i = 0; i < members.length; i++) |
| 1714 | { |
| 1715 | if (members[i].col === col) |
| 1716 | { members[i].y += 1; } |
| 1717 | } |
| 1718 | this.cullOffTop(); |
| 1719 | if (!this.clusters[clusterId]) |
| 1720 | { return true; } |
| 1721 | this.checkChainRelaunch(cluster); |
| 1722 | return true; |
| 1723 | }; |
| 1724 | |
| 1725 | /** |
| 1726 | * After a cargo attach or a cluster merge (or any other mid-air state |
| 1727 | * change), turn spontaneous lines of 3+ into rockets and give the whole |
| 1728 | * already-flying cluster an extra launch from its current position — |
| 1729 | * the same re-launch as a player push that completes a row. |
| 1730 | */ |
| 1731 | GameCore.prototype.resolveFlyingMatches = function () |
| 1732 | { |
| 1733 | var ids = Object.keys(this.clusters); |
| 1734 | for (var n = 0; n < ids.length; n++) |
| 1735 | { |
| 1736 | var cluster = this.clusters[ids[n]]; |
| 1737 | if (!cluster || cluster.phase === 'landed') |
| 1738 | { continue; } |
| 1739 | this.checkChainRelaunch(cluster); |
| 1740 | } |
| 1741 | }; |
| 1742 | |
| 1743 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 1744 | { |
| 1745 | if (!this.clusters[cluster.id]) |
| 1746 | { return; } |
| 1747 | var lines = this.findFlyingMatchLines(cluster.id); |
| 1748 | if (!lines.length) |
| 1749 | { return; } |
| 1750 | var newly = []; |
| 1751 | var seen = Object.create(null); |
| 1752 | for (var i = 0; i < lines.length; i++) |
| 1753 | { |
| 1754 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 1755 | { |
| 1756 | var block = lines[i].blocks[j]; |
| 1757 | if (seen[block.id]) |
| 1758 | { continue; } |
| 1759 | seen[block.id] = true; |
| 1760 | newly.push(block); |
| 1761 | } |
| 1762 | } |
| 1763 | for (var i = 0; i < newly.length; i++) |
| 1764 | { |
| 1765 | var block = newly[i]; |
| 1766 | block.color = COLOR_GRAY; |
| 1767 | block.flame = true; |
| 1768 | block.clusterId = cluster.id; |
| 1769 | block.state = STATE_FLYING; |
| 1770 | } |
| 1771 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 1772 | }; |
| 1773 | |
| 1774 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 1775 | { |
| 1776 | cluster.jumpHeight = height; |
| 1777 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1778 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 1779 | cluster.phase = 'up'; |
| 1780 | var members = this.clusterMembers(cluster.id); |
| 1781 | for (var i = 0; i < members.length; i++) |
| 1782 | { |
| 1783 | members[i].vy = cluster.vy; |
| 1784 | members[i].state = STATE_FLYING; |
| 1785 | if (members[i].color === COLOR_GRAY) |
| 1786 | { members[i].flame = true; } |
| 1787 | } |
| 1788 | }; |
| 1789 | |
| 1790 | GameCore.prototype.startManualLaunch = function (block) |
| 1791 | { |
| 1792 | var target = this.height - 1; |
| 1793 | var height = Math.max(0.05, target - block.y); |
| 1794 | block.state = STATE_MANUAL; |
| 1795 | block.pushedClusters = Object.create(null); |
| 1796 | block.manualTarget = target; |
| 1797 | block.manualPhase = 'up'; |
| 1798 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1799 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 1800 | block.flame = false; |
| 1801 | if (this.drag && this.drag.blockId === block.id) |
| 1802 | { this.drag = null; } |
| 1803 | }; |
| 1804 | |
| 1805 | GameCore.prototype.cullOffTop = function () |
| 1806 | { |
| 1807 | var gone = []; |
| 1808 | for (var i = 0; i < this.blocks.length; i++) |
| 1809 | { |
| 1810 | var block = this.blocks[i]; |
| 1811 | if (block.state === STATE_MANUAL) |
| 1812 | { |
| 1813 | if (block.y > this.height - 1) |
| 1814 | { block.y = this.height - 1; } |
| 1815 | continue; |
| 1816 | } |
| 1817 | if (block.y >= this.height - 1e-9) |
| 1818 | { gone.push(block); } |
| 1819 | } |
| 1820 | for (var i = 0; i < gone.length; i++) |
| 1821 | { |
| 1822 | var block = gone[i]; |
| 1823 | this.outgoingShots.push({ |
| 1824 | id: block.id, |
| 1825 | color: block.color, |
| 1826 | col: block.col, |
| 1827 | y: block.y |
| 1828 | }); |
| 1829 | this.score += 1; |
| 1830 | this.removeBlock(block); |
| 1831 | } |
| 1832 | }; |
| 1833 | |
| 1834 | GameCore.prototype.consumeOutgoingShots = function () |
| 1835 | { |
| 1836 | var shots = this.outgoingShots; |
| 1837 | this.outgoingShots = []; |
| 1838 | return shots; |
| 1839 | }; |
| 1840 | |
| 1841 | /** |
| 1842 | * A compact field picture for the multiplayer snapshot (miniatures). |
| 1843 | * Includes every block's col, y, color, state and flame. |
| 1844 | */ |
| 1845 | GameCore.prototype.fieldSnapshot = function () |
| 1846 | { |
| 1847 | var blocks = []; |
| 1848 | for (var i = 0; i < this.blocks.length; i++) |
| 1849 | { |
| 1850 | var b = this.blocks[i]; |
| 1851 | blocks.push({ |
| 1852 | id: b.id, |
| 1853 | col: b.col, |
| 1854 | y: Math.round(b.y * 1000) / 1000, |
| 1855 | color: b.color, |
| 1856 | state: b.state, |
| 1857 | flame: !!b.flame |
| 1858 | }); |
| 1859 | } |
| 1860 | return { |
| 1861 | score: this.score, |
| 1862 | state: this.state, |
| 1863 | width: this.width, |
| 1864 | height: this.height, |
| 1865 | locked: this.lockedColumnCount(), |
| 1866 | blocks: blocks |
| 1867 | }; |
| 1868 | }; |
| 1869 | |
| 1870 | /** |
| 1871 | * Incoming garbage from another player: a dark-grey block that falls |
| 1872 | * normally from the top of an open column. After landing it uses the |
| 1873 | * usual 3s settle and then a random colour (landFallingBlock). |
| 1874 | * `col` is optional; a random eligible column is used otherwise. |
| 1875 | * Returns the new block, or null if every column is locked or full. |
| 1876 | */ |
| 1877 | GameCore.prototype.ingestGarbage = function (col) |
| 1878 | { |
| 1879 | if (this.state !== 'playing') |
| 1880 | { return null; } |
| 1881 | var chosen = null; |
| 1882 | if (col != null && col >= 0 && col < this.width |
| 1883 | && !this.isColumnLocked(col) && this.groundedCount(col) < this.height) |
| 1884 | { chosen = col | 0; } |
| 1885 | else |
| 1886 | { |
| 1887 | var eligible = []; |
| 1888 | for (var c = 0; c < this.width; c++) |
| 1889 | { |
| 1890 | if (this.isColumnLocked(c)) |
| 1891 | { continue; } |
| 1892 | if (this.groundedCount(c) >= this.height) |
| 1893 | { continue; } |
| 1894 | eligible.push(c); |
| 1895 | } |
| 1896 | if (!eligible.length) |
| 1897 | { return null; } |
| 1898 | chosen = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 1899 | } |
| 1900 | var block = this.createBlock(chosen, this.height, COLOR_GRAY, STATE_FALLING); |
| 1901 | block.vy = -this.planet.fallSpeed; |
| 1902 | block.flame = false; |
| 1903 | return block; |
| 1904 | }; |
| 1905 | |
| 1906 | GameCore.prototype.advanceSettle = function (dt) |
| 1907 | { |
| 1908 | for (var i = 0; i < this.blocks.length; i++) |
| 1909 | { |
| 1910 | var block = this.blocks[i]; |
| 1911 | if (block.state !== STATE_SETTLING) |
| 1912 | { continue; } |
| 1913 | var inLanded = block.clusterId != null |
| 1914 | && this.clusters[block.clusterId] |
| 1915 | && this.clusters[block.clusterId].phase === 'landed'; |
| 1916 | if (block.color === COLOR_GRAY) |
| 1917 | { |
| 1918 | block.settleTime += dt; |
| 1919 | if (block.settleTime >= this.settleTime) |
| 1920 | { |
| 1921 | block.color = this.randomColor(); |
| 1922 | block.settleTime = 0; |
| 1923 | block.flame = false; |
| 1924 | if (inLanded) |
| 1925 | { |
| 1926 | // Stay in the rigid group; releaseLandedCluster runs below. |
| 1927 | block.state = STATE_SETTLING; |
| 1928 | } |
| 1929 | else |
| 1930 | { |
| 1931 | this.leaveCluster(block); |
| 1932 | this.clearFlightState(block); |
| 1933 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | else if (!inLanded) |
| 1938 | { |
| 1939 | // Coloured settling that is not in a landed cluster (shouldn't |
| 1940 | // normally happen): just become still or fall, from rest. |
| 1941 | this.leaveCluster(block); |
| 1942 | this.clearFlightState(block); |
| 1943 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | var ids = Object.keys(this.clusters); |
| 1948 | for (var n = 0; n < ids.length; n++) |
| 1949 | { |
| 1950 | var cluster = this.clusters[ids[n]]; |
| 1951 | if (!cluster || cluster.phase !== 'landed') |
| 1952 | { continue; } |
| 1953 | var members = this.clusterMembers(cluster.id); |
| 1954 | var greySettling = false; |
| 1955 | for (var i = 0; i < members.length; i++) |
| 1956 | { |
| 1957 | if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING) |
| 1958 | { greySettling = true; } |
| 1959 | } |
| 1960 | if (!greySettling) |
| 1961 | { this.releaseLandedCluster(cluster); } |
| 1962 | } |
| 1963 | }; |
| 1964 | |
| 1965 | GameCore.prototype.findMatchLines = function () |
| 1966 | { |
| 1967 | var candidates = []; |
| 1968 | for (var i = 0; i < this.blocks.length; i++) |
| 1969 | { |
| 1970 | var block = this.blocks[i]; |
| 1971 | if (block.state === STATE_STILL && block.color >= 0) |
| 1972 | { candidates.push(block); } |
| 1973 | } |
| 1974 | return this.collectLines(candidates, true); |
| 1975 | }; |
| 1976 | |
| 1977 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 1978 | { |
| 1979 | var candidates = []; |
| 1980 | for (var i = 0; i < this.blocks.length; i++) |
| 1981 | { |
| 1982 | var block = this.blocks[i]; |
| 1983 | if (block.clusterId === clusterId && block.color >= 0 |
| 1984 | && (block.state === STATE_FLYING)) |
| 1985 | { candidates.push(block); } |
| 1986 | } |
| 1987 | return this.collectLines(candidates, false); |
| 1988 | }; |
| 1989 | |
| 1990 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 1991 | { |
| 1992 | var lines = []; |
| 1993 | if (!candidates.length) |
| 1994 | { return lines; } |
| 1995 | |
| 1996 | var byRow = Object.create(null); |
| 1997 | for (var i = 0; i < candidates.length; i++) |
| 1998 | { |
| 1999 | var block = candidates[i]; |
| 2000 | var row = Math.round(block.y); |
| 2001 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 2002 | { continue; } |
| 2003 | if (!byRow[row]) |
| 2004 | { byRow[row] = []; } |
| 2005 | byRow[row].push(block); |
| 2006 | } |
| 2007 | var rows = Object.keys(byRow); |
| 2008 | for (var r = 0; r < rows.length; r++) |
| 2009 | { |
| 2010 | var list = byRow[rows[r]]; |
| 2011 | list.sort(function (a, b) { return a.col - b.col; }); |
| 2012 | this.pushRuns(lines, list, false, function (a, b) |
| 2013 | { |
| 2014 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 2015 | }); |
| 2016 | } |
| 2017 | |
| 2018 | var byCol = Object.create(null); |
| 2019 | for (var i = 0; i < candidates.length; i++) |
| 2020 | { |
| 2021 | var block = candidates[i]; |
| 2022 | if (!byCol[block.col]) |
| 2023 | { byCol[block.col] = []; } |
| 2024 | byCol[block.col].push(block); |
| 2025 | } |
| 2026 | var cols = Object.keys(byCol); |
| 2027 | for (var c = 0; c < cols.length; c++) |
| 2028 | { |
| 2029 | var list = byCol[cols[c]]; |
| 2030 | list.sort(function (a, b) { return a.y - b.y; }); |
| 2031 | this.pushRuns(lines, list, true, function (a, b) |
| 2032 | { |
| 2033 | return a.color === b.color && a.color >= 0 |
| 2034 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 2035 | }); |
| 2036 | } |
| 2037 | return lines; |
| 2038 | }; |
| 2039 | |
| 2040 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 2041 | { |
| 2042 | var start = 0; |
| 2043 | for (var i = 1; i <= list.length; i++) |
| 2044 | { |
| 2045 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 2046 | if (!same) |
| 2047 | { |
| 2048 | var length = i - start; |
| 2049 | if (length >= 3) |
| 2050 | { |
| 2051 | lines.push({ |
| 2052 | blocks: list.slice(start, i), |
| 2053 | length: length, |
| 2054 | vertical: vertical |
| 2055 | }); |
| 2056 | } |
| 2057 | start = i; |
| 2058 | } |
| 2059 | } |
| 2060 | }; |
| 2061 | |
| 2062 | GameCore.prototype.resolveMatches = function () |
| 2063 | { |
| 2064 | if (this.state !== 'playing' || this.drag) |
| 2065 | { return []; } |
| 2066 | var lines = this.findMatchLines(); |
| 2067 | if (!lines.length) |
| 2068 | { return []; } |
| 2069 | |
| 2070 | var uf = new UnionFind(); |
| 2071 | var blockById = Object.create(null); |
| 2072 | for (var i = 0; i < lines.length; i++) |
| 2073 | { |
| 2074 | var line = lines[i]; |
| 2075 | for (var j = 0; j < line.blocks.length; j++) |
| 2076 | { |
| 2077 | uf.add(line.blocks[j].id); |
| 2078 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 2079 | if (j > 0) |
| 2080 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 2081 | } |
| 2082 | } |
| 2083 | var matched = []; |
| 2084 | var seen = Object.create(null); |
| 2085 | for (var id in blockById) |
| 2086 | { |
| 2087 | if (!seen[id]) |
| 2088 | { |
| 2089 | seen[id] = true; |
| 2090 | matched.push(blockById[id]); |
| 2091 | } |
| 2092 | } |
| 2093 | for (var i = 0; i < matched.length; i++) |
| 2094 | { |
| 2095 | for (var j = i + 1; j < matched.length; j++) |
| 2096 | { |
| 2097 | if (matched[i].col === matched[j].col) |
| 2098 | { uf.union(matched[i].id, matched[j].id); } |
| 2099 | } |
| 2100 | } |
| 2101 | |
| 2102 | var groups = Object.create(null); |
| 2103 | for (var i = 0; i < matched.length; i++) |
| 2104 | { |
| 2105 | var root = uf.find(matched[i].id); |
| 2106 | if (!groups[root]) |
| 2107 | { groups[root] = []; } |
| 2108 | groups[root].push(matched[i]); |
| 2109 | } |
| 2110 | |
| 2111 | var launched = []; |
| 2112 | for (var root in groups) |
| 2113 | { |
| 2114 | var group = groups[root]; |
| 2115 | var height = 0; |
| 2116 | for (var i = 0; i < lines.length; i++) |
| 2117 | { |
| 2118 | var line = lines[i]; |
| 2119 | var overlaps = false; |
| 2120 | for (var j = 0; j < line.blocks.length; j++) |
| 2121 | { |
| 2122 | for (var k = 0; k < group.length; k++) |
| 2123 | { |
| 2124 | if (line.blocks[j].id === group[k].id) |
| 2125 | { overlaps = true; } |
| 2126 | } |
| 2127 | } |
| 2128 | if (overlaps) |
| 2129 | { |
| 2130 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 2131 | if (h > height) |
| 2132 | { height = h; } |
| 2133 | } |
| 2134 | } |
| 2135 | this.launchCluster(group, height); |
| 2136 | launched.push({ blocks: group, jumpHeight: height }); |
| 2137 | } |
| 2138 | return launched; |
| 2139 | }; |
| 2140 | |
| 2141 | /** |
| 2142 | * Gray the matched blocks, take everything sitting on top of them in |
| 2143 | * those columns, link them, and launch with jump height `height`. |
| 2144 | */ |
| 2145 | GameCore.prototype.launchCluster = function (matchedBlocks, height) |
| 2146 | { |
| 2147 | var lowestInCol = Object.create(null); |
| 2148 | var matchedIds = Object.create(null); |
| 2149 | for (var i = 0; i < matchedBlocks.length; i++) |
| 2150 | { |
| 2151 | var block = matchedBlocks[i]; |
| 2152 | matchedIds[block.id] = true; |
| 2153 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 2154 | { lowestInCol[block.col] = block.y; } |
| 2155 | } |
| 2156 | |
| 2157 | var clusterId = this.nextClusterId++; |
| 2158 | var cluster = { |
| 2159 | id: clusterId, |
| 2160 | jumpHeight: height, |
| 2161 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 2162 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 2163 | phase: 'up' |
| 2164 | }; |
| 2165 | this.clusters[clusterId] = cluster; |
| 2166 | |
| 2167 | var riders = []; |
| 2168 | var cols = Object.keys(lowestInCol); |
| 2169 | for (var c = 0; c < cols.length; c++) |
| 2170 | { |
| 2171 | var col = +cols[c]; |
| 2172 | var lowest = lowestInCol[col]; |
| 2173 | var list = this.blocksInColumn(col); |
| 2174 | for (var i = 0; i < list.length; i++) |
| 2175 | { |
| 2176 | var block = list[i]; |
| 2177 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 2178 | { |
| 2179 | if (block.state !== STATE_SETTLING) |
| 2180 | { continue; } |
| 2181 | } |
| 2182 | if (block.y + ALIGN_EPS >= lowest) |
| 2183 | { riders.push(block); } |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | for (var i = 0; i < riders.length; i++) |
| 2188 | { |
| 2189 | var block = riders[i]; |
| 2190 | var isMatch = !!matchedIds[block.id]; |
| 2191 | if (isMatch || block.color === COLOR_GRAY) |
| 2192 | { |
| 2193 | block.color = COLOR_GRAY; |
| 2194 | block.flame = true; |
| 2195 | } |
| 2196 | else |
| 2197 | { block.flame = false; } |
| 2198 | block.state = STATE_FLYING; |
| 2199 | block.clusterId = clusterId; |
| 2200 | block.vy = cluster.vy; |
| 2201 | block.settleTime = 0; |
| 2202 | } |
| 2203 | return cluster; |
| 2204 | }; |
| 2205 | |
| 2206 | GameCore.prototype.checkLocks = function () |
| 2207 | { |
| 2208 | for (var col = 0; col < this.width; col++) |
| 2209 | { |
| 2210 | if (this.isColumnLocked(col)) |
| 2211 | { continue; } |
| 2212 | if (this.groundedCount(col) < this.height) |
| 2213 | { continue; } |
| 2214 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 2215 | var filled = true; |
| 2216 | for (var row = 0; row < this.height; row++) |
| 2217 | { |
| 2218 | var block = this.blockAt(col, row); |
| 2219 | if (!block || !this.isGrounded(block)) |
| 2220 | { |
| 2221 | filled = false; |
| 2222 | break; |
| 2223 | } |
| 2224 | } |
| 2225 | if (!filled) |
| 2226 | { continue; } |
| 2227 | var list = this.blocksInColumn(col); |
| 2228 | for (var i = 0; i < list.length; i++) |
| 2229 | { |
| 2230 | var block = list[i]; |
| 2231 | if (!this.isGrounded(block)) |
| 2232 | { continue; } |
| 2233 | block.state = STATE_LOCKED; |
| 2234 | block.color = COLOR_GRAY; |
| 2235 | block.flame = false; |
| 2236 | block.vy = 0; |
| 2237 | block.settleTime = 0; |
| 2238 | if (block.clusterId != null) |
| 2239 | { this.leaveCluster(block); } |
| 2240 | } |
| 2241 | } |
| 2242 | }; |
| 2243 | |
| 2244 | GameCore.prototype.checkGameOver = function () |
| 2245 | { |
| 2246 | if (this.state !== 'playing') |
| 2247 | { return; } |
| 2248 | for (var col = 0; col < this.width; col++) |
| 2249 | { |
| 2250 | if (!this.isColumnLocked(col)) |
| 2251 | { return; } |
| 2252 | } |
| 2253 | this.state = 'gameover'; |
| 2254 | this.gameOverTime = 0; |
| 2255 | this.drag = null; |
| 2256 | }; |
| 2257 | |
| 2258 | /* ---------- dragging -------------------------------------------------- */ |
| 2259 | |
| 2260 | GameCore.prototype.beginDrag = function (col, y) |
| 2261 | { |
| 2262 | if (this.state !== 'playing') |
| 2263 | { return false; } |
| 2264 | var block = this.snapDragBlock(col, y); |
| 2265 | if (!block) |
| 2266 | { return false; } |
| 2267 | var top = this.columnStackTopRow(block.col); |
| 2268 | var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 2269 | this.drag = { |
| 2270 | blockId: block.id, |
| 2271 | pointerY: y, |
| 2272 | startY: y, |
| 2273 | accum: 0, |
| 2274 | launchIntent: false, |
| 2275 | startedAsTop: startedAsTop |
| 2276 | }; |
| 2277 | return true; |
| 2278 | }; |
| 2279 | |
| 2280 | /** |
| 2281 | * Instant grab: the nearest still, coloured block to (col, y), allowing |
| 2282 | * a small vertical miss and a horizontal slop into a neighbouring |
| 2283 | * column. `col` may be fractional (pointer x in tiles). No hold delay. |
| 2284 | */ |
| 2285 | GameCore.prototype.snapDragBlock = function (col, y) |
| 2286 | { |
| 2287 | var raw = col; |
| 2288 | var c0 = Math.floor(raw + 1e-9); |
| 2289 | var frac = raw - c0; |
| 2290 | var candidates = []; |
| 2291 | if (c0 >= 0 && c0 < this.width) |
| 2292 | { candidates.push(c0); } |
| 2293 | // Only consult a neighbour when the pointer is actually near the edge |
| 2294 | // (a whole-number col, as in unit tests, stays in that column). |
| 2295 | if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0) |
| 2296 | { candidates.push(c0 - 1); } |
| 2297 | if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width) |
| 2298 | { candidates.push(c0 + 1); } |
| 2299 | |
| 2300 | var best = null; |
| 2301 | var bestDist = Infinity; |
| 2302 | for (var c = 0; c < candidates.length; c++) |
| 2303 | { |
| 2304 | var cc = candidates[c]; |
| 2305 | if (!this.isColumnStill(cc)) |
| 2306 | { continue; } |
| 2307 | var list = this.blocksInColumn(cc); |
| 2308 | for (var i = 0; i < list.length; i++) |
| 2309 | { |
| 2310 | var block = list[i]; |
| 2311 | if (block.state !== STATE_STILL || block.color < 0) |
| 2312 | { continue; } |
| 2313 | var dist = this.pointerDistToBlock(block, raw, y); |
| 2314 | // Prefer the column the pointer is actually in on a tie. |
| 2315 | if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0)) |
| 2316 | { |
| 2317 | bestDist = dist; |
| 2318 | best = block; |
| 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | if (!best || bestDist > SNAP_Y) |
| 2323 | { return null; } |
| 2324 | return best; |
| 2325 | }; |
| 2326 | |
| 2327 | GameCore.prototype.pointerDistToBlock = function (block, col, y) |
| 2328 | { |
| 2329 | var dx; |
| 2330 | if (col >= block.col && col < block.col + 1) |
| 2331 | { dx = 0; } |
| 2332 | else if (col < block.col) |
| 2333 | { dx = block.col - col; } |
| 2334 | else |
| 2335 | { dx = col - (block.col + 1); } |
| 2336 | var dy; |
| 2337 | if (y >= block.y && y < block.y + 1) |
| 2338 | { dy = 0; } |
| 2339 | else if (y < block.y) |
| 2340 | { dy = block.y - y; } |
| 2341 | else |
| 2342 | { dy = y - (block.y + 1); } |
| 2343 | return Math.sqrt(dx * dx + dy * dy); |
| 2344 | }; |
| 2345 | |
| 2346 | GameCore.prototype.moveDrag = function (y) |
| 2347 | { |
| 2348 | if (!this.drag) |
| 2349 | { return; } |
| 2350 | var block = this.blockById(this.drag.blockId); |
| 2351 | if (!block || block.state !== STATE_STILL) |
| 2352 | { |
| 2353 | this.drag = null; |
| 2354 | return; |
| 2355 | } |
| 2356 | if (!this.isColumnStill(block.col)) |
| 2357 | { |
| 2358 | this.drag = null; |
| 2359 | return; |
| 2360 | } |
| 2361 | this.drag.pointerY = y; |
| 2362 | // 1:1 tracking: the target row is the one under the pointer, clamped |
| 2363 | // to the still stack. Swap step by step with each neighbour, and stop |
| 2364 | // exactly there. When the pointer stops, nothing else moves. |
| 2365 | // Accumulated deltas are not used: a SWAP_DISTANCE (0.25) threshold |
| 2366 | // used to jump a full cell on a short pull, and one large event |
| 2367 | // could walk the entire column (the "jumps all the way" bug). |
| 2368 | var top = this.columnStackTopRow(block.col); |
| 2369 | var bottom = this.columnStackBottomRow(block.col); |
| 2370 | var target = Math.floor(y + 1e-12); |
| 2371 | if (bottom < 0) |
| 2372 | { bottom = 0; } |
| 2373 | if (target < bottom) |
| 2374 | { target = bottom; } |
| 2375 | if (target > top) |
| 2376 | { target = top; } |
| 2377 | this.swapToward(block, target); |
| 2378 | |
| 2379 | top = this.columnStackTopRow(block.col); |
| 2380 | var isTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 2381 | if (this.drag.startedAsTop) |
| 2382 | { |
| 2383 | // Already the topmost block: pulling upward past its own position |
| 2384 | // (LAUNCH_PULL tiles above the press point) starts a manual jump. |
| 2385 | this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL); |
| 2386 | } |
| 2387 | else |
| 2388 | { |
| 2389 | // Started mid-stack: swap first, and only launch once the pointer |
| 2390 | // is pulled into the empty space above the (new) top. |
| 2391 | this.drag.launchIntent = isTop && (y > top + 1 + 0.25); |
| 2392 | } |
| 2393 | }; |
| 2394 | |
| 2395 | /** Swap `block` one tile up (dir=+1) or down (dir=-1). Returns true if swapped. */ |
| 2396 | GameCore.prototype.swapOne = function (block, dir) |
| 2397 | { |
| 2398 | var neighbor = this.blockAt(block.col, block.y + dir); |
| 2399 | if (neighbor && this.isGrounded(neighbor) && neighbor !== block) |
| 2400 | { |
| 2401 | this.swapBlocks(block, neighbor); |
| 2402 | return true; |
| 2403 | } |
| 2404 | return false; |
| 2405 | }; |
| 2406 | |
| 2407 | GameCore.prototype.endDrag = function () |
| 2408 | { |
| 2409 | if (!this.drag) |
| 2410 | { return; } |
| 2411 | var block = this.blockById(this.drag.blockId); |
| 2412 | var launch = this.drag.launchIntent; |
| 2413 | this.drag = null; |
| 2414 | if (launch && block && block.state === STATE_STILL) |
| 2415 | { this.startManualLaunch(block); } |
| 2416 | else |
| 2417 | { this.resolveMatches(); } |
| 2418 | }; |
| 2419 | |
| 2420 | GameCore.prototype.blockById = function (id) |
| 2421 | { |
| 2422 | for (var i = 0; i < this.blocks.length; i++) |
| 2423 | { |
| 2424 | if (this.blocks[i].id === id) |
| 2425 | { return this.blocks[i]; } |
| 2426 | } |
| 2427 | return null; |
| 2428 | }; |
| 2429 | |
| 2430 | GameCore.prototype.columnStackTopRow = function (col) |
| 2431 | { |
| 2432 | var top = -1; |
| 2433 | var list = this.blocksInColumn(col); |
| 2434 | for (var i = 0; i < list.length; i++) |
| 2435 | { |
| 2436 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 2437 | { top = list[i].y; } |
| 2438 | } |
| 2439 | return top; |
| 2440 | }; |
| 2441 | |
| 2442 | GameCore.prototype.columnStackBottomRow = function (col) |
| 2443 | { |
| 2444 | var bottom = Infinity; |
| 2445 | var list = this.blocksInColumn(col); |
| 2446 | for (var i = 0; i < list.length; i++) |
| 2447 | { |
| 2448 | if (this.isGrounded(list[i]) && list[i].y < bottom) |
| 2449 | { bottom = list[i].y; } |
| 2450 | } |
| 2451 | return bottom === Infinity ? 0 : bottom; |
| 2452 | }; |
| 2453 | |
| 2454 | GameCore.prototype.swapToward = function (block, targetY) |
| 2455 | { |
| 2456 | var guard = 0; |
| 2457 | while (guard++ < this.height + 2) |
| 2458 | { |
| 2459 | var current = block.y; |
| 2460 | if (targetY > current + 0.5) |
| 2461 | { |
| 2462 | var above = this.blockAt(block.col, current + 1); |
| 2463 | if (above && this.isGrounded(above) && above !== block) |
| 2464 | { this.swapBlocks(block, above); } |
| 2465 | else |
| 2466 | { break; } |
| 2467 | } |
| 2468 | else if (targetY < current - 0.5) |
| 2469 | { |
| 2470 | var below = this.blockAt(block.col, current - 1); |
| 2471 | if (below && this.isGrounded(below) && below !== block) |
| 2472 | { this.swapBlocks(block, below); } |
| 2473 | else |
| 2474 | { break; } |
| 2475 | } |
| 2476 | else |
| 2477 | { break; } |
| 2478 | } |
| 2479 | }; |
| 2480 | |
| 2481 | GameCore.prototype.swapBlocks = function (a, b) |
| 2482 | { |
| 2483 | var y = a.y; |
| 2484 | a.y = b.y; |
| 2485 | b.y = y; |
| 2486 | }; |
| 2487 | |
| 2488 | /** |
| 2489 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 2490 | * Returns the moved block, or null if the column is not still. |
| 2491 | */ |
| 2492 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 2493 | { |
| 2494 | if (!this.isColumnStill(col)) |
| 2495 | { return null; } |
| 2496 | var block = this.blockAt(col, fromRow); |
| 2497 | if (!block) |
| 2498 | { return null; } |
| 2499 | this.swapToward(block, toRow); |
| 2500 | return block; |
| 2501 | }; |
| 2502 | |
| 2503 | /** |
| 2504 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 2505 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 2506 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 2507 | * their color with a marker in `flying`. |
| 2508 | */ |
| 2509 | GameCore.prototype.snapshot = function () |
| 2510 | { |
| 2511 | var grid = []; |
| 2512 | for (var r = 0; r < this.height; r++) |
| 2513 | { |
| 2514 | var row = []; |
| 2515 | for (var c = 0; c < this.width; c++) |
| 2516 | { row.push('.'); } |
| 2517 | grid.push(row); |
| 2518 | } |
| 2519 | var airborne = []; |
| 2520 | for (var i = 0; i < this.blocks.length; i++) |
| 2521 | { |
| 2522 | var block = this.blocks[i]; |
| 2523 | var ch; |
| 2524 | if (block.state === STATE_LOCKED) |
| 2525 | { ch = 'L'; } |
| 2526 | else if (block.color === COLOR_GRAY) |
| 2527 | { ch = 'g'; } |
| 2528 | else |
| 2529 | { ch = String(block.color); } |
| 2530 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 2531 | { |
| 2532 | var row = Math.round(block.y); |
| 2533 | if (row >= 0 && row < this.height) |
| 2534 | { grid[row][block.col] = ch; } |
| 2535 | } |
| 2536 | else |
| 2537 | { |
| 2538 | airborne.push({ |
| 2539 | id: block.id, |
| 2540 | col: block.col, |
| 2541 | y: block.y, |
| 2542 | color: block.color, |
| 2543 | state: block.state, |
| 2544 | clusterId: block.clusterId, |
| 2545 | flame: block.flame, |
| 2546 | vy: block.vy |
| 2547 | }); |
| 2548 | } |
| 2549 | } |
| 2550 | var lines = []; |
| 2551 | for (var r = this.height - 1; r >= 0; r--) |
| 2552 | { lines.push(grid[r].join('')); } |
| 2553 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 2554 | }; |
| 2555 | |
| 2556 | var GameCoreAPI = { |
| 2557 | GameCore: GameCore, |
| 2558 | COLOR_GRAY: COLOR_GRAY, |
| 2559 | STATE_STILL: STATE_STILL, |
| 2560 | STATE_FALLING: STATE_FALLING, |
| 2561 | STATE_FLYING: STATE_FLYING, |
| 2562 | STATE_SETTLING: STATE_SETTLING, |
| 2563 | STATE_LOCKED: STATE_LOCKED, |
| 2564 | STATE_MANUAL: STATE_MANUAL |
| 2565 | }; |
| 2566 | |
| 2567 | if (typeof module !== 'undefined' && module.exports) |
| 2568 | { module.exports = GameCoreAPI; } |
| 2569 | else if (typeof globalThis !== 'undefined') |
| 2570 | { |
| 2571 | globalThis.GameCore = GameCore; |
| 2572 | globalThis.GameCoreAPI = GameCoreAPI; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
| 2576 | * End File: |
| 2577 | * /workspace/meteors/src/gameCore.js |
| 2578 | */ |
| 2579 | |
| 2580 | /* |
| 2581 | * Start File: |
| 2582 | * /workspace/meteors/src/online.js |
| 2583 | */ |
| 2584 | 'use strict'; |
| 2585 | |
| 2586 | /** |
| 2587 | * Online play: talks to api.php (cookie session), polls lobby and game |
| 2588 | * state, and draws opponent miniatures plus flying-block tweens. |
| 2589 | * Real-time sync is not required; a 0.75 s poll is enough. |
| 2590 | */ |
| 2591 | |
| 2592 | var Online = { |
| 2593 | POLL_MS: 750, |
| 2594 | DEFAULT_MISS_CHANCE: 0.30, |
| 2595 | FLIGHT_DURATION: 1.2, |
| 2596 | configured: false, |
| 2597 | local: false, |
| 2598 | csrf: '', |
| 2599 | me: null, |
| 2600 | lobby: null, |
| 2601 | incoming: [], |
| 2602 | shotResults: [], |
| 2603 | error: '', |
| 2604 | _timer: null, |
| 2605 | _pendingShots: [], |
| 2606 | _flights: [], |
| 2607 | _lastStatus: null, |
| 2608 | onChange: null, |
| 2609 | onGameStart: null, |
| 2610 | onIncoming: null, |
| 2611 | onFinished: null, |
| 2612 | onLobbyReturn: null |
| 2613 | }; |
| 2614 | |
| 2615 | Online._headers = function (json) |
| 2616 | { |
| 2617 | var h = { 'Accept': 'application/json' }; |
| 2618 | if (json) |
| 2619 | { h['Content-Type'] = 'application/json'; } |
| 2620 | if (this.csrf) |
| 2621 | { h['X-CSRF-Token'] = this.csrf; } |
| 2622 | return h; |
| 2623 | }; |
| 2624 | |
| 2625 | Online._apply = function (data) |
| 2626 | { |
| 2627 | if (!data) |
| 2628 | { return; } |
| 2629 | if (typeof data.configured === 'boolean') |
| 2630 | { this.configured = data.configured; } |
| 2631 | if (typeof data.local === 'boolean') |
| 2632 | { this.local = data.local; } |
| 2633 | if (data.csrf) |
| 2634 | { this.csrf = data.csrf; } |
| 2635 | if (data.me !== undefined) |
| 2636 | { this.me = data.me; } |
| 2637 | if (data.lobby !== undefined) |
| 2638 | { this.lobby = data.lobby; } |
| 2639 | if (data.incoming) |
| 2640 | { this.incoming = data.incoming; } |
| 2641 | if (data.shot_results) |
| 2642 | { this.shotResults = data.shot_results; } |
| 2643 | if (data.miss_chance_default != null) |
| 2644 | { this.DEFAULT_MISS_CHANCE = data.miss_chance_default; } |
| 2645 | if (data.flight_duration != null) |
| 2646 | { this.FLIGHT_DURATION = data.flight_duration; } |
| 2647 | if (data.error) |
| 2648 | { this.error = data.error; } |
| 2649 | else if (data.ok) |
| 2650 | { this.error = ''; } |
| 2651 | }; |
| 2652 | |
| 2653 | Online.request = function (action, body) |
| 2654 | { |
| 2655 | var self = this; |
| 2656 | var isGet = action === 'me' || action === ''; |
| 2657 | var url = 'api.php?action=' + encodeURIComponent(action || 'me'); |
| 2658 | var opts = { |
| 2659 | method: isGet && !body ? 'GET' : 'POST', |
| 2660 | credentials: 'same-origin', |
| 2661 | headers: this._headers(!!body || !isGet) |
| 2662 | }; |
| 2663 | if (opts.method === 'POST') |
| 2664 | { |
| 2665 | var payload = body ? Object.assign({ action: action }, body) : { action: action }; |
| 2666 | opts.body = JSON.stringify(payload); |
| 2667 | } |
| 2668 | return fetch(url, opts).then(function (res) |
| 2669 | { |
| 2670 | return res.json().then(function (data) |
| 2671 | { |
| 2672 | data = data || {}; |
| 2673 | if (!res.ok && !data.error) |
| 2674 | { data.error = 'De server gaf ' + res.status; data.ok = false; } |
| 2675 | self._apply(data); |
| 2676 | return data; |
| 2677 | }, function () |
| 2678 | { |
| 2679 | var fail = { ok: false, error: 'Geen geldig antwoord van de server.' }; |
| 2680 | self.error = fail.error; |
| 2681 | return fail; |
| 2682 | }); |
| 2683 | }).catch(function (err) |
| 2684 | { |
| 2685 | var fail = { ok: false, error: 'Geen verbinding met de server.' }; |
| 2686 | self.error = fail.error; |
| 2687 | return fail; |
| 2688 | }); |
| 2689 | }; |
| 2690 | |
| 2691 | Online.refresh = function () |
| 2692 | { |
| 2693 | return this.request('me'); |
| 2694 | }; |
| 2695 | |
| 2696 | Online.testLogin = function (who) |
| 2697 | { |
| 2698 | return this.request('test_login', { user: who }); |
| 2699 | }; |
| 2700 | |
| 2701 | Online.createLobby = function (planetId) |
| 2702 | { |
| 2703 | return this.request('lobby_create', { |
| 2704 | miss_chance: this.DEFAULT_MISS_CHANCE, |
| 2705 | planet_id: planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra') |
| 2706 | }); |
| 2707 | }; |
| 2708 | |
| 2709 | Online.joinLobby = function (code, planetId) |
| 2710 | { |
| 2711 | return this.request('lobby_join', { |
| 2712 | code: code, |
| 2713 | planet_id: planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra') |
| 2714 | }); |
| 2715 | }; |
| 2716 | |
| 2717 | Online.setPlanet = function (planetId) |
| 2718 | { |
| 2719 | return this.request('lobby_set_planet', { planet_id: planetId }); |
| 2720 | }; |
| 2721 | |
| 2722 | Online.leaveLobby = function () |
| 2723 | { |
| 2724 | return this.request('lobby_leave'); |
| 2725 | }; |
| 2726 | |
| 2727 | Online.setReady = function (ready) |
| 2728 | { |
| 2729 | return this.request('lobby_ready', { ready: !!ready }); |
| 2730 | }; |
| 2731 | |
| 2732 | Online.startGame = function () |
| 2733 | { |
| 2734 | var self = this; |
| 2735 | var prev = this.lobby && this.lobby.status; |
| 2736 | return this.request('lobby_start').then(function (data) |
| 2737 | { |
| 2738 | self._handleTransitions(prev, data); |
| 2739 | return data; |
| 2740 | }); |
| 2741 | }; |
| 2742 | |
| 2743 | Online.queueShots = function (shots) |
| 2744 | { |
| 2745 | for (var i = 0; i < shots.length; i++) |
| 2746 | { |
| 2747 | this._pendingShots.push({ |
| 2748 | client_id: String(shots[i].id), |
| 2749 | color: shots[i].color, |
| 2750 | col: shots[i].col, |
| 2751 | y: shots[i].y |
| 2752 | }); |
| 2753 | } |
| 2754 | }; |
| 2755 | |
| 2756 | Online.poll = function (opts) |
| 2757 | { |
| 2758 | opts = opts || {}; |
| 2759 | var body = {}; |
| 2760 | if (opts.snapshot) |
| 2761 | { body.snapshot = opts.snapshot; } |
| 2762 | if (opts.score != null) |
| 2763 | { body.score = opts.score; } |
| 2764 | if (opts.eliminated) |
| 2765 | { body.eliminated = true; } |
| 2766 | if (this._pendingShots.length) |
| 2767 | { |
| 2768 | body.outgoing_shots = this._pendingShots.splice(0, this._pendingShots.length); |
| 2769 | } |
| 2770 | var self = this; |
| 2771 | var prevStatus = this.lobby && this.lobby.status; |
| 2772 | return this.request('poll', body).then(function (data) |
| 2773 | { |
| 2774 | self._handleTransitions(prevStatus, data); |
| 2775 | if (self.onChange) |
| 2776 | { self.onChange(self); } |
| 2777 | return data; |
| 2778 | }); |
| 2779 | }; |
| 2780 | |
| 2781 | Online._handleTransitions = function (prevStatus, data) |
| 2782 | { |
| 2783 | var lobby = this.lobby; |
| 2784 | var status = lobby && lobby.status; |
| 2785 | if (status === 'playing' && prevStatus !== 'playing' && this.onGameStart) |
| 2786 | { this.onGameStart(lobby); } |
| 2787 | if (data && data.incoming && data.incoming.length && this.onIncoming) |
| 2788 | { this.onIncoming(data.incoming); } |
| 2789 | if (data && data.shot_results && data.shot_results.length) |
| 2790 | { this.spawnFlights(data.shot_results, this._lastQueuedMeta); } |
| 2791 | if (status === 'finished' && prevStatus !== 'finished' && this.onFinished) |
| 2792 | { this.onFinished(lobby); } |
| 2793 | if (status === 'waiting' && prevStatus === 'finished' && this.onLobbyReturn) |
| 2794 | { this.onLobbyReturn(lobby); } |
| 2795 | }; |
| 2796 | |
| 2797 | Online.startPolling = function (getPayload) |
| 2798 | { |
| 2799 | this.stopPolling(); |
| 2800 | var self = this; |
| 2801 | var tick = function () |
| 2802 | { |
| 2803 | var payload = getPayload ? getPayload() : {}; |
| 2804 | self.poll(payload); |
| 2805 | }; |
| 2806 | tick(); |
| 2807 | this._timer = setInterval(tick, this.POLL_MS); |
| 2808 | }; |
| 2809 | |
| 2810 | Online.stopPolling = function () |
| 2811 | { |
| 2812 | if (this._timer) |
| 2813 | { |
| 2814 | clearInterval(this._timer); |
| 2815 | this._timer = null; |
| 2816 | } |
| 2817 | }; |
| 2818 | |
| 2819 | Online.loginUrl = function () |
| 2820 | { |
| 2821 | return 'auth.php'; |
| 2822 | }; |
| 2823 | |
| 2824 | Online.logoutUrl = function () |
| 2825 | { |
| 2826 | return 'logout.php'; |
| 2827 | }; |
| 2828 | |
| 2829 | Online.opponents = function () |
| 2830 | { |
| 2831 | if (!this.lobby || !this.lobby.players) |
| 2832 | { return []; } |
| 2833 | return this.lobby.players.filter(function (p) { return !p.is_me; }); |
| 2834 | }; |
| 2835 | |
| 2836 | Online.aliveOpponents = function () |
| 2837 | { |
| 2838 | return this.opponents().filter(function (p) { return !p.eliminated; }); |
| 2839 | }; |
| 2840 | |
| 2841 | Online.mePlayer = function () |
| 2842 | { |
| 2843 | if (!this.lobby || !this.lobby.players) |
| 2844 | { return null; } |
| 2845 | for (var i = 0; i < this.lobby.players.length; i++) |
| 2846 | { |
| 2847 | if (this.lobby.players[i].is_me) |
| 2848 | { return this.lobby.players[i]; } |
| 2849 | } |
| 2850 | return null; |
| 2851 | }; |
| 2852 | |
| 2853 | /* ---------- miniatures --------------------------------------------- */ |
| 2854 | |
| 2855 | Online.renderMinis = function (container) |
| 2856 | { |
| 2857 | if (!container) |
| 2858 | { return; } |
| 2859 | var opponents = this.opponents(); |
| 2860 | var existing = {}; |
| 2861 | var kids = container.querySelectorAll('[data-player]'); |
| 2862 | for (var i = 0; i < kids.length; i++) |
| 2863 | { existing[kids[i].getAttribute('data-player')] = kids[i]; } |
| 2864 | |
| 2865 | var seen = Object.create(null); |
| 2866 | for (var i = 0; i < opponents.length; i++) |
| 2867 | { |
| 2868 | var p = opponents[i]; |
| 2869 | seen[p.id] = true; |
| 2870 | var el = existing[p.id]; |
| 2871 | if (!el) |
| 2872 | { |
| 2873 | el = document.createElement('div'); |
| 2874 | el.className = 'mini'; |
| 2875 | el.setAttribute('data-player', p.id); |
| 2876 | el.innerHTML = '<div class="mini-name"></div>' |
| 2877 | + '<canvas class="mini-canvas" width="80" height="104"></canvas>' |
| 2878 | + '<div class="mini-meta"></div>'; |
| 2879 | container.appendChild(el); |
| 2880 | } |
| 2881 | el.classList.toggle('eliminated', !!p.eliminated); |
| 2882 | el.querySelector('.mini-name').textContent = p.name + (p.is_host ? ' (host)' : ''); |
| 2883 | el.querySelector('.mini-meta').textContent = p.eliminated |
| 2884 | ? ('Uit' + (p.place ? ' · ' + p.place + 'e' : '')) |
| 2885 | : ('Punten ' + (p.score || 0)); |
| 2886 | this._drawMini(el.querySelector('canvas'), p.snapshot, p.planet); |
| 2887 | } |
| 2888 | for (var id in existing) |
| 2889 | { |
| 2890 | if (!seen[id]) |
| 2891 | { existing[id].remove(); } |
| 2892 | } |
| 2893 | }; |
| 2894 | |
| 2895 | var MINI_COLORS = ['#d4553a', '#3a9ad4', '#4caf6a', '#e6c04a', '#b86ad4']; |
| 2896 | |
| 2897 | function miniColor (planet, color) |
| 2898 | { |
| 2899 | if (planet && planet.colors && planet.colors[color] && planet.colors[color].hex) |
| 2900 | { return planet.colors[color].hex; } |
| 2901 | return MINI_COLORS[color] || '#888'; |
| 2902 | } |
| 2903 | |
| 2904 | |
| 2905 | Online._drawMini = function (canvas, snapshot, planet) |
| 2906 | { |
| 2907 | if (!canvas) |
| 2908 | { return; } |
| 2909 | var ctx = canvas.getContext('2d'); |
| 2910 | var w = 10; |
| 2911 | var h = 13; |
| 2912 | var tw = canvas.width / w; |
| 2913 | var th = canvas.height / h; |
| 2914 | ctx.fillStyle = '#0c1020'; |
| 2915 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 2916 | ctx.strokeStyle = 'rgba(255,255,255,0.06)'; |
| 2917 | ctx.lineWidth = 1; |
| 2918 | for (var c = 0; c <= w; c++) |
| 2919 | { |
| 2920 | ctx.beginPath(); |
| 2921 | ctx.moveTo(c * tw + 0.5, 0); |
| 2922 | ctx.lineTo(c * tw + 0.5, canvas.height); |
| 2923 | ctx.stroke(); |
| 2924 | } |
| 2925 | for (var r = 0; r <= h; r++) |
| 2926 | { |
| 2927 | ctx.beginPath(); |
| 2928 | ctx.moveTo(0, r * th + 0.5); |
| 2929 | ctx.lineTo(canvas.width, r * th + 0.5); |
| 2930 | ctx.stroke(); |
| 2931 | } |
| 2932 | if (!snapshot || !snapshot.blocks) |
| 2933 | { return; } |
| 2934 | var blocks = snapshot.blocks; |
| 2935 | for (var i = 0; i < blocks.length; i++) |
| 2936 | { |
| 2937 | var b = blocks[i]; |
| 2938 | var x = b.col * tw; |
| 2939 | var y = canvas.height - (b.y + 1) * th; |
| 2940 | if (b.state === 'locked') |
| 2941 | { ctx.fillStyle = '#3a3d4a'; } |
| 2942 | else if (b.color < 0) |
| 2943 | { ctx.fillStyle = '#5a5d66'; } |
| 2944 | else |
| 2945 | { ctx.fillStyle = miniColor(planet, b.color); } |
| 2946 | ctx.fillRect(x + 0.5, y + 0.5, tw - 1, th - 1); |
| 2947 | } |
| 2948 | }; |
| 2949 | |
| 2950 | Online.miniElement = function (playerId) |
| 2951 | { |
| 2952 | return document.querySelector('#minis .mini[data-player="' + CSS.escape(playerId) + '"]'); |
| 2953 | }; |
| 2954 | |
| 2955 | /* ---------- flight tweens ------------------------------------------ */ |
| 2956 | |
| 2957 | function colorSpriteUrl (color, planetId) |
| 2958 | { |
| 2959 | if (color < 0) |
| 2960 | { return 'images/block-gray.png'; } |
| 2961 | var planet = null; |
| 2962 | if (typeof Planet !== 'undefined') |
| 2963 | { planet = Planet.planetById(planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra')); } |
| 2964 | if (planet && planet.colors && planet.colors[color] && planet.colors[color].image) |
| 2965 | { return planet.colors[color].image; } |
| 2966 | return 'images/block-gray.png'; |
| 2967 | } |
| 2968 | |
| 2969 | /** |
| 2970 | * Animate shot results. `locals` is a map of client_id → {col, y, color, |
| 2971 | * startX, startY} in viewport pixels, filled by the view just after |
| 2972 | * consumeOutgoingShots. |
| 2973 | */ |
| 2974 | Online.spawnFlights = function (results, locals) |
| 2975 | { |
| 2976 | locals = locals || this._flightLocals || {}; |
| 2977 | var layer = document.getElementById('flights'); |
| 2978 | if (!layer) |
| 2979 | { return; } |
| 2980 | for (var i = 0; i < results.length; i++) |
| 2981 | { |
| 2982 | var r = results[i]; |
| 2983 | var meta = locals[r.client_id] || {}; |
| 2984 | var start = this._flightStart(meta.col); |
| 2985 | var end; |
| 2986 | var miss = !!r.miss; |
| 2987 | if (miss) |
| 2988 | { end = this._missEnd(start); } |
| 2989 | else |
| 2990 | { end = this._targetEnd(r.target_id, start); } |
| 2991 | var el = document.createElement('img'); |
| 2992 | el.src = colorSpriteUrl(r.color != null ? r.color : meta.color); |
| 2993 | el.alt = ''; |
| 2994 | el.className = 'flight'; |
| 2995 | layer.appendChild(el); |
| 2996 | var spin = (Math.random() * 2 - 1) * 720; |
| 2997 | var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000; |
| 2998 | this._flights.push({ |
| 2999 | el: el, |
| 3000 | start: start, |
| 3001 | end: end, |
| 3002 | ctrl: miss ? this._missCtrl(start, end) : this._hitCtrl(start, end), |
| 3003 | t0: performance.now(), |
| 3004 | duration: duration, |
| 3005 | spin: spin, |
| 3006 | miss: miss, |
| 3007 | angle: Math.random() * 360 |
| 3008 | }); |
| 3009 | } |
| 3010 | this._flightLocals = {}; |
| 3011 | }; |
| 3012 | |
| 3013 | Online.rememberFlightLocal = function (shot, startX, startY) |
| 3014 | { |
| 3015 | if (!this._flightLocals) |
| 3016 | { this._flightLocals = {}; } |
| 3017 | this._flightLocals[String(shot.id)] = { |
| 3018 | col: shot.col, |
| 3019 | y: shot.y, |
| 3020 | color: shot.color, |
| 3021 | startX: startX, |
| 3022 | startY: startY |
| 3023 | }; |
| 3024 | }; |
| 3025 | |
| 3026 | Online._flightStart = function (col) |
| 3027 | { |
| 3028 | var field = document.querySelector('canvas'); |
| 3029 | var appRef = typeof app !== 'undefined' ? app : null; |
| 3030 | if (appRef && appRef.scene && appRef.scene.canvas) |
| 3031 | { |
| 3032 | var canvas = appRef.scene.canvas; |
| 3033 | var rect = canvas.getBoundingClientRect(); |
| 3034 | var c = col == null ? 4.5 : col; |
| 3035 | return { |
| 3036 | x: rect.left + (c + 0.5) * (rect.width / 10), |
| 3037 | y: rect.top + 4 |
| 3038 | }; |
| 3039 | } |
| 3040 | if (field) |
| 3041 | { |
| 3042 | var rect = field.getBoundingClientRect(); |
| 3043 | return { x: rect.left + rect.width / 2, y: rect.top }; |
| 3044 | } |
| 3045 | return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 }; |
| 3046 | }; |
| 3047 | |
| 3048 | Online._targetEnd = function (targetId, start) |
| 3049 | { |
| 3050 | var el = targetId && this.miniElement(targetId); |
| 3051 | if (el) |
| 3052 | { |
| 3053 | var r = el.getBoundingClientRect(); |
| 3054 | return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; |
| 3055 | } |
| 3056 | return { x: start.x + (Math.random() - 0.5) * 80, y: 40 }; |
| 3057 | }; |
| 3058 | |
| 3059 | Online._missEnd = function (start) |
| 3060 | { |
| 3061 | var dx = (Math.random() * 2 - 1) * window.innerWidth * 0.35; |
| 3062 | return { |
| 3063 | x: start.x + dx, |
| 3064 | y: start.y - (120 + Math.random() * 220) |
| 3065 | }; |
| 3066 | }; |
| 3067 | |
| 3068 | Online._missCtrl = function (start, end) |
| 3069 | { |
| 3070 | return { |
| 3071 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 160, |
| 3072 | y: Math.min(start.y, end.y) - 40 - Math.random() * 80 |
| 3073 | }; |
| 3074 | }; |
| 3075 | |
| 3076 | Online._hitCtrl = function (start, end) |
| 3077 | { |
| 3078 | return { |
| 3079 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 40, |
| 3080 | y: (start.y + end.y) / 2 - 30 |
| 3081 | }; |
| 3082 | }; |
| 3083 | |
| 3084 | Online.tickFlights = function (now) |
| 3085 | { |
| 3086 | now = now || performance.now(); |
| 3087 | var leftover = []; |
| 3088 | for (var i = 0; i < this._flights.length; i++) |
| 3089 | { |
| 3090 | var f = this._flights[i]; |
| 3091 | var u = (now - f.t0) / f.duration; |
| 3092 | if (u >= 1) |
| 3093 | { |
| 3094 | if (f.el && f.el.parentNode) |
| 3095 | { f.el.parentNode.removeChild(f.el); } |
| 3096 | continue; |
| 3097 | } |
| 3098 | // Quadratic bezier, ease-in so they feel like they recede. |
| 3099 | var e = u * u; |
| 3100 | var mt = 1 - e; |
| 3101 | var x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x; |
| 3102 | var y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y; |
| 3103 | var scale = f.miss ? (1 - e) : (1 - 0.85 * e); |
| 3104 | var angle = f.angle + f.spin * u; |
| 3105 | f.el.style.transform = 'translate(' + x + 'px,' + y + 'px) translate(-50%,-50%) rotate(' |
| 3106 | + angle + 'deg) scale(' + scale + ')'; |
| 3107 | f.el.style.opacity = f.miss ? String(1 - e) : '1'; |
| 3108 | leftover.push(f); |
| 3109 | } |
| 3110 | this._flights = leftover; |
| 3111 | }; |
| 3112 | |
| 3113 | Online.clearFlights = function () |
| 3114 | { |
| 3115 | for (var i = 0; i < this._flights.length; i++) |
| 3116 | { |
| 3117 | var el = this._flights[i].el; |
| 3118 | if (el && el.parentNode) |
| 3119 | { el.parentNode.removeChild(el); } |
| 3120 | } |
| 3121 | this._flights = []; |
| 3122 | this._flightLocals = {}; |
| 3123 | }; |
| 3124 | |
| 3125 | if (typeof globalThis !== 'undefined') |
| 3126 | { globalThis.Online = Online; } |
| 3127 | if (typeof module !== 'undefined' && module.exports) |
| 3128 | { module.exports = Online; } |
| 3129 | |
| 3130 | /* |
| 3131 | * End File: |
| 3132 | * /workspace/meteors/src/online.js |
| 3133 | */ |
| 3134 | |
| 3135 | /* |
| 3136 | * Start File: |
| 3137 | * /workspace/meteors/src/main.js |
| 3138 | */ |
| 3139 | 'use strict'; |
| 3140 | |
| 3141 | /** |
| 3142 | * View and input for Meteors. The simulation lives in GameCore; this file |
| 3143 | * only draws it, scales it to the viewport, and translates pointer events |
| 3144 | * into beginDrag / moveDrag / endDrag. Online play (stage 2) is wired |
| 3145 | * through Online (src/online.js) and the same GameCore. |
| 3146 | */ |
| 3147 | |
| 3148 | function planetImages () |
| 3149 | { |
| 3150 | var list = [ |
| 3151 | { name: 'gray', url: 'images/block-gray.png' }, |
| 3152 | { name: 'locked', url: 'images/block-locked.png' }, |
| 3153 | { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, |
| 3154 | { name: 'cell', url: 'images/cell.png' }, |
| 3155 | { name: 'pixel', url: 'images/pixel.png' }, |
| 3156 | // Keep the original Terra placeholders as aliases (Tim may still have them). |
| 3157 | { name: 'ember', url: 'images/block-ember.png' }, |
| 3158 | { name: 'aqua', url: 'images/block-aqua.png' }, |
| 3159 | { name: 'leaf', url: 'images/block-leaf.png' }, |
| 3160 | { name: 'sun', url: 'images/block-sun.png' }, |
| 3161 | { name: 'orchid', url: 'images/block-orchid.png' } |
| 3162 | ]; |
| 3163 | var planets = (typeof Planet !== 'undefined' ? Planet.allPlanets() : []); |
| 3164 | for (var i = 0; i < planets.length; i++) |
| 3165 | { |
| 3166 | var pl = planets[i]; |
| 3167 | for (var c = 0; c < pl.colors.length; c++) |
| 3168 | { |
| 3169 | list.push({ |
| 3170 | name: Planet.spriteKey(pl, c), |
| 3171 | url: pl.colors[c].image |
| 3172 | }); |
| 3173 | } |
| 3174 | } |
| 3175 | return list; |
| 3176 | } |
| 3177 | |
| 3178 | var IMAGES = planetImages(); |
| 3179 | var GAME_OVER_DELAY = 2; |
| 3180 | |
| 3181 | var game = new Game(); |
| 3182 | var app = { |
| 3183 | mode: 'menu', |
| 3184 | core: null, |
| 3185 | scene: null, |
| 3186 | manager: null, |
| 3187 | planet: null, |
| 3188 | lastScore: 0, |
| 3189 | tile: 48, |
| 3190 | fieldX: 0, |
| 3191 | fieldY: 0, |
| 3192 | fieldW: 0, |
| 3193 | fieldH: 0, |
| 3194 | hudH: 56, |
| 3195 | sprites: Object.create(null), |
| 3196 | flames: Object.create(null), |
| 3197 | cells: [], |
| 3198 | pointerId: null, |
| 3199 | gameOverShown: false, |
| 3200 | selectTween: null, |
| 3201 | online: false, |
| 3202 | eliminatedReported: false, |
| 3203 | spectating: false, |
| 3204 | miniH: 0, |
| 3205 | selectedPlanetId: 'terra', |
| 3206 | planetSelectContext: 'solo' |
| 3207 | }; |
| 3208 | |
| 3209 | window.onload = function () |
| 3210 | { |
| 3211 | var play = document.getElementById('btn-play'); |
| 3212 | play.addEventListener('click', onPlay); |
| 3213 | play.addEventListener('pointerup', function (e) |
| 3214 | { |
| 3215 | // A touch-friendly path: some mobile browsers swallow the click |
| 3216 | // after a slightly sloppy tap. Ignore the following click. |
| 3217 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 3218 | { |
| 3219 | e.preventDefault(); |
| 3220 | onPlay(); |
| 3221 | } |
| 3222 | }); |
| 3223 | |
| 3224 | game.preloadImagesThenStart(IMAGES, function () |
| 3225 | { |
| 3226 | if (game.images.flame && game.images.flame.addAnimationCycle) |
| 3227 | { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } |
| 3228 | }); |
| 3229 | |
| 3230 | bindOnlineUi(); |
| 3231 | bindPlanetSelect(); |
| 3232 | restoreSelectedPlanet(); |
| 3233 | }; |
| 3234 | |
| 3235 | function onPlay () |
| 3236 | { |
| 3237 | if (app.mode === 'playing') |
| 3238 | { return; } |
| 3239 | openPlanetSelect('solo'); |
| 3240 | } |
| 3241 | |
| 3242 | function startPlay (opts) |
| 3243 | { |
| 3244 | opts = opts || {}; |
| 3245 | hideEl('menu'); |
| 3246 | hideEl('game-over'); |
| 3247 | hideEl('online-panel'); |
| 3248 | hideEl('spectator'); |
| 3249 | hideEl('podium'); |
| 3250 | hideEl('planet-select'); |
| 3251 | showEl('hud'); |
| 3252 | |
| 3253 | var planetMod = typeof Planet !== 'undefined' ? Planet : PlanetMod; |
| 3254 | app.planet = opts.planet || planetMod.planetById(selectedPlanetId()); |
| 3255 | var miss = opts.missChance; |
| 3256 | if (typeof miss !== 'number' || !isFinite(miss)) |
| 3257 | { miss = planetMod.missChance(app.planet); } |
| 3258 | app.core = new GameCore(app.planet, Math.random, miss); |
| 3259 | app.mode = 'playing'; |
| 3260 | app.online = !!opts.online; |
| 3261 | app.gameOverShown = false; |
| 3262 | app.eliminatedReported = false; |
| 3263 | app.spectating = false; |
| 3264 | app.sprites = Object.create(null); |
| 3265 | app.flames = Object.create(null); |
| 3266 | app.cells = []; |
| 3267 | app.pointerId = null; |
| 3268 | app.selectTween = null; |
| 3269 | |
| 3270 | layout(); |
| 3271 | if (app.scene) |
| 3272 | { |
| 3273 | app.scene.destroy(); |
| 3274 | app.scene = null; |
| 3275 | } |
| 3276 | app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); |
| 3277 | app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; |
| 3278 | app.scene.renderBackground = true; |
| 3279 | game.loadScene(app.scene); |
| 3280 | |
| 3281 | buildCells(); |
| 3282 | app.manager = app.scene.addObject(makeManager()); |
| 3283 | if (!app.spectating) |
| 3284 | { bindPointer(app.scene.canvas); } |
| 3285 | refreshHud(); |
| 3286 | if (app.online) |
| 3287 | { showEl('minis'); } |
| 3288 | else |
| 3289 | { hideEl('minis'); } |
| 3290 | } |
| 3291 | |
| 3292 | function teardownPlay () |
| 3293 | { |
| 3294 | unbindPointer(); |
| 3295 | if (app.scene) |
| 3296 | { |
| 3297 | app.scene.destroy(); |
| 3298 | app.scene = null; |
| 3299 | } |
| 3300 | app.manager = null; |
| 3301 | app.core = null; |
| 3302 | app.sprites = Object.create(null); |
| 3303 | app.flames = Object.create(null); |
| 3304 | app.cells = []; |
| 3305 | app.selectTween = null; |
| 3306 | app.pointerId = null; |
| 3307 | app.gameOverShown = false; |
| 3308 | hideEl('hud'); |
| 3309 | hideEl('game-over'); |
| 3310 | hideEl('spectator'); |
| 3311 | hideEl('minis'); |
| 3312 | if (typeof Online !== 'undefined') |
| 3313 | { Online.clearFlights(); } |
| 3314 | } |
| 3315 | |
| 3316 | function returnToMenu () |
| 3317 | { |
| 3318 | if (app.online && typeof Online !== 'undefined') |
| 3319 | { |
| 3320 | // Online matches do not dump you back to the solo menu: |
| 3321 | // elimination becomes spectating; a finished match shows the podium. |
| 3322 | return; |
| 3323 | } |
| 3324 | teardownPlay(); |
| 3325 | app.mode = 'menu'; |
| 3326 | app.online = false; |
| 3327 | hideEl('planet-select'); |
| 3328 | showEl('menu'); |
| 3329 | var last = document.getElementById('last-score'); |
| 3330 | if (app.lastScore > 0) |
| 3331 | { |
| 3332 | last.hidden = false; |
| 3333 | last.querySelector('strong').textContent = String(app.lastScore); |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | function buildCells () |
| 3338 | { |
| 3339 | if (!app.scene) |
| 3340 | { return; } |
| 3341 | var img = game.images.cell; |
| 3342 | for (var row = 0; row < 13; row++) |
| 3343 | { |
| 3344 | for (var col = 0; col < 10; col++) |
| 3345 | { |
| 3346 | var obj = new GameObject(0, 0, img, 0); |
| 3347 | placeTile(obj, col, row); |
| 3348 | obj.renderer.alpha = 0.9; |
| 3349 | app.scene.addObject(obj); |
| 3350 | app.cells.push(obj); |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | |
| 3355 | function placeTile (obj, col, y) |
| 3356 | { |
| 3357 | var t = app.tile; |
| 3358 | obj.transform.position.x = col * t + t / 2; |
| 3359 | obj.transform.position.y = app.fieldH - (y + 0.5) * t; |
| 3360 | obj.transform.size = { x: t, y: t }; |
| 3361 | } |
| 3362 | |
| 3363 | function layout () |
| 3364 | { |
| 3365 | var vw = window.innerWidth || 320; |
| 3366 | var vh = window.innerHeight || 480; |
| 3367 | var safeTop = 0; |
| 3368 | try |
| 3369 | { |
| 3370 | // env(safe-area) is a CSS thing; keep a conservative HUD height. |
| 3371 | safeTop = 0; |
| 3372 | } |
| 3373 | catch (e) { /* ignore */ } |
| 3374 | app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); |
| 3375 | app.miniH = 0; |
| 3376 | if (app.online) |
| 3377 | { app.miniH = Math.min(150, Math.max(86, Math.round(vh * 0.20))); } |
| 3378 | var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); |
| 3379 | var availW = Math.max(80, vw - margin * 2); |
| 3380 | var availH = Math.max(80, vh - app.hudH - app.miniH - margin); |
| 3381 | app.tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); |
| 3382 | app.fieldW = app.tile * 10; |
| 3383 | app.fieldH = app.tile * 13; |
| 3384 | app.fieldX = Math.floor((vw - app.fieldW) / 2); |
| 3385 | var topBand = app.hudH + app.miniH; |
| 3386 | app.fieldY = topBand + Math.floor((vh - topBand - app.fieldH - margin / 2) / 2); |
| 3387 | if (app.fieldY < topBand) |
| 3388 | { app.fieldY = topBand; } |
| 3389 | var minis = document.getElementById('minis'); |
| 3390 | if (minis) |
| 3391 | { |
| 3392 | minis.style.top = app.hudH + 'px'; |
| 3393 | minis.style.maxHeight = app.miniH + 'px'; |
| 3394 | } |
| 3395 | |
| 3396 | if (app.scene) |
| 3397 | { |
| 3398 | app.scene.position.x = app.fieldX; |
| 3399 | app.scene.position.y = app.fieldY; |
| 3400 | app.scene.size.x = app.fieldW; |
| 3401 | app.scene.size.y = app.fieldH; |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | function spriteFor (block) |
| 3406 | { |
| 3407 | if (block.state === 'locked') |
| 3408 | { return game.images.locked; } |
| 3409 | if (block.color < 0) |
| 3410 | { return game.images.gray; } |
| 3411 | var key = Planet.spriteKey(app.planet, block.color); |
| 3412 | return game.images[key] || game.images.gray; |
| 3413 | } |
| 3414 | |
| 3415 | function syncSprites () |
| 3416 | { |
| 3417 | if (!app.core || !app.scene) |
| 3418 | { return; } |
| 3419 | var seen = Object.create(null); |
| 3420 | var blocks = app.core.blocks; |
| 3421 | for (var i = 0; i < blocks.length; i++) |
| 3422 | { |
| 3423 | var block = blocks[i]; |
| 3424 | seen[block.id] = true; |
| 3425 | var obj = app.sprites[block.id]; |
| 3426 | if (!obj) |
| 3427 | { |
| 3428 | obj = new GameObject(0, 0, spriteFor(block), 2); |
| 3429 | app.scene.addObject(obj); |
| 3430 | app.sprites[block.id] = obj; |
| 3431 | } |
| 3432 | obj.renderer.sprite = spriteFor(block); |
| 3433 | placeTile(obj, block.col, block.y); |
| 3434 | applyBlockVisual(obj, block); |
| 3435 | obj.renderer.visible = true; |
| 3436 | obj.renderer.alpha = 1; |
| 3437 | |
| 3438 | if (block.flame) |
| 3439 | { |
| 3440 | var flame = app.flames[block.id]; |
| 3441 | if (!flame) |
| 3442 | { |
| 3443 | flame = new GameObject(0, 0, game.images.flame, 1); |
| 3444 | flame.renderer.setAnimation('burn', 0, 0.08); |
| 3445 | app.scene.addObject(flame); |
| 3446 | app.flames[block.id] = flame; |
| 3447 | } |
| 3448 | flame.renderer.visible = true; |
| 3449 | var t = app.tile; |
| 3450 | flame.transform.position.x = block.col * t + t / 2; |
| 3451 | flame.transform.position.y = app.fieldH - block.y * t + t * 0.15; |
| 3452 | flame.transform.size = { x: t * 0.85, y: t * 0.85 }; |
| 3453 | } |
| 3454 | else if (app.flames[block.id]) |
| 3455 | { app.flames[block.id].renderer.visible = false; } |
| 3456 | } |
| 3457 | |
| 3458 | var ids = Object.keys(app.sprites); |
| 3459 | for (var i = 0; i < ids.length; i++) |
| 3460 | { |
| 3461 | if (!seen[ids[i]]) |
| 3462 | { |
| 3463 | app.sprites[ids[i]].destroy(); |
| 3464 | delete app.sprites[ids[i]]; |
| 3465 | if (app.flames[ids[i]]) |
| 3466 | { |
| 3467 | app.flames[ids[i]].destroy(); |
| 3468 | delete app.flames[ids[i]]; |
| 3469 | } |
| 3470 | } |
| 3471 | } |
| 3472 | } |
| 3473 | |
| 3474 | function applyBlockVisual (obj, block) |
| 3475 | { |
| 3476 | var t = app.tile; |
| 3477 | var sx = 1; |
| 3478 | var sy = 1; |
| 3479 | var dy = 0; |
| 3480 | if (block.bouncing && app.planet && app.planet.bounceDuration > 0 |
| 3481 | && typeof Easing !== 'undefined' && Easing.bouncePose) |
| 3482 | { |
| 3483 | var u = block.bounceAge / app.planet.bounceDuration; |
| 3484 | var pose = Easing.bouncePose(u, app.planet.bounceSquash); |
| 3485 | sx = pose.sx; |
| 3486 | sy = pose.sy; |
| 3487 | dy = pose.dy; |
| 3488 | } |
| 3489 | var sel = 1; |
| 3490 | var tw = app.selectTween; |
| 3491 | if (tw && tw.blockId === block.id && tw.phase !== 'idle') |
| 3492 | { sel = tw.scale; } |
| 3493 | obj.transform.size = { x: t * sx * sel, y: t * sy * sel }; |
| 3494 | if (dy) |
| 3495 | { obj.transform.position.y -= dy * t; } |
| 3496 | } |
| 3497 | |
| 3498 | function clickTween () |
| 3499 | { |
| 3500 | return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null; |
| 3501 | } |
| 3502 | |
| 3503 | function startPressTween (blockId) |
| 3504 | { |
| 3505 | var c = clickTween(); |
| 3506 | if (!c) |
| 3507 | { return; } |
| 3508 | var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1; |
| 3509 | app.selectTween = { |
| 3510 | blockId: blockId, |
| 3511 | from: from, |
| 3512 | to: c.pressScale, |
| 3513 | t: 0, |
| 3514 | duration: c.pressDuration, |
| 3515 | ease: c.pressEase, |
| 3516 | scale: from, |
| 3517 | phase: 'press' |
| 3518 | }; |
| 3519 | } |
| 3520 | |
| 3521 | function startReleaseTween () |
| 3522 | { |
| 3523 | var c = clickTween(); |
| 3524 | var tw = app.selectTween; |
| 3525 | if (!c || !tw || tw.phase === 'idle') |
| 3526 | { return; } |
| 3527 | app.selectTween = { |
| 3528 | blockId: tw.blockId, |
| 3529 | from: tw.scale, |
| 3530 | to: c.releaseScale, |
| 3531 | t: 0, |
| 3532 | duration: c.releaseDuration, |
| 3533 | ease: c.releaseEase, |
| 3534 | scale: tw.scale, |
| 3535 | phase: 'release' |
| 3536 | }; |
| 3537 | } |
| 3538 | |
| 3539 | function updateSelectTween (dt) |
| 3540 | { |
| 3541 | var tw = app.selectTween; |
| 3542 | if (!tw || tw.phase === 'idle' || tw.phase === 'hold') |
| 3543 | { return; } |
| 3544 | tw.t += dt; |
| 3545 | var u = tw.duration > 0 ? tw.t / tw.duration : 1; |
| 3546 | if (u >= 1) |
| 3547 | { |
| 3548 | tw.scale = tw.to; |
| 3549 | tw.t = tw.duration; |
| 3550 | if (tw.phase === 'press') |
| 3551 | { tw.phase = 'hold'; } |
| 3552 | else |
| 3553 | { |
| 3554 | tw.phase = 'idle'; |
| 3555 | tw.scale = 1; |
| 3556 | tw.blockId = null; |
| 3557 | } |
| 3558 | return; |
| 3559 | } |
| 3560 | var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack); |
| 3561 | tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u; |
| 3562 | } |
| 3563 | |
| 3564 | function refreshHud () |
| 3565 | { |
| 3566 | if (!app.core) |
| 3567 | { return; } |
| 3568 | document.getElementById('score').textContent = String(app.core.score); |
| 3569 | var planet = app.planet; |
| 3570 | document.getElementById('planet-name').textContent = planet.name + ' · ' + planet.people; |
| 3571 | document.getElementById('planet-stats').textContent = Planet.formatStats(planet); |
| 3572 | } |
| 3573 | |
| 3574 | function makeManager () |
| 3575 | { |
| 3576 | var obj = new GameObject(0, 0, game.images.pixel, 0); |
| 3577 | obj.renderer.visible = false; |
| 3578 | var base = obj.update.bind(obj); |
| 3579 | obj.update = function (dt) |
| 3580 | { |
| 3581 | base(dt); |
| 3582 | tickView(dt); |
| 3583 | }; |
| 3584 | return obj; |
| 3585 | } |
| 3586 | |
| 3587 | function tickView (dt) |
| 3588 | { |
| 3589 | if (app.mode !== 'playing' || !app.core) |
| 3590 | { return; } |
| 3591 | |
| 3592 | layout(); |
| 3593 | for (var i = 0; i < app.cells.length; i++) |
| 3594 | { |
| 3595 | var col = i % 10; |
| 3596 | var row = Math.floor(i / 10); |
| 3597 | placeTile(app.cells[i], col, row); |
| 3598 | } |
| 3599 | |
| 3600 | app.core.tick(dt); |
| 3601 | updateSelectTween(dt); |
| 3602 | syncSprites(); |
| 3603 | refreshHud(); |
| 3604 | tickOnline(dt); |
| 3605 | |
| 3606 | if (app.core.state === 'gameover' && !app.gameOverShown) |
| 3607 | { |
| 3608 | app.gameOverShown = true; |
| 3609 | app.lastScore = app.core.score; |
| 3610 | document.getElementById('go-score').textContent = String(app.lastScore); |
| 3611 | if (app.online) |
| 3612 | { onOnlineEliminated(); } |
| 3613 | else |
| 3614 | { showEl('game-over'); } |
| 3615 | } |
| 3616 | if (!app.online && app.core.state === 'gameover' && app.core.gameOverTime >= GAME_OVER_DELAY) |
| 3617 | { returnToMenu(); } |
| 3618 | } |
| 3619 | |
| 3620 | /* ---------- pointer input (mouse + touch) --------------------------- */ |
| 3621 | /* |
| 3622 | * Both mouse and touch use Pointer Events. After pointerdown we listen |
| 3623 | * on window, so a drag that leaves the canvas (needed to launch a |
| 3624 | * block at the top of a full column) still reaches moveDrag. We do |
| 3625 | * not treat lostpointercapture as the end of a drag: on some touch |
| 3626 | * browsers that fires mid-gesture and would cancel a launch. |
| 3627 | */ |
| 3628 | |
| 3629 | var boundCanvas = null; |
| 3630 | var windowListening = false; |
| 3631 | |
| 3632 | function addWindowPointer () |
| 3633 | { |
| 3634 | if (windowListening) |
| 3635 | { return; } |
| 3636 | window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false }); |
| 3637 | window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false }); |
| 3638 | window.addEventListener('pointercancel', onPointerUp, { capture: true, passive: false }); |
| 3639 | windowListening = true; |
| 3640 | } |
| 3641 | |
| 3642 | function removeWindowPointer () |
| 3643 | { |
| 3644 | if (!windowListening) |
| 3645 | { return; } |
| 3646 | window.removeEventListener('pointermove', onPointerMove, { capture: true }); |
| 3647 | window.removeEventListener('pointerup', onPointerUp, { capture: true }); |
| 3648 | window.removeEventListener('pointercancel', onPointerUp, { capture: true }); |
| 3649 | windowListening = false; |
| 3650 | } |
| 3651 | |
| 3652 | function bindPointer (canvas) |
| 3653 | { |
| 3654 | unbindPointer(); |
| 3655 | boundCanvas = canvas; |
| 3656 | canvas.addEventListener('pointerdown', onPointerDown, { passive: false }); |
| 3657 | canvas.style.touchAction = 'none'; |
| 3658 | canvas.style.cursor = 'pointer'; |
| 3659 | canvas.style.webkitUserSelect = 'none'; |
| 3660 | canvas.style.userSelect = 'none'; |
| 3661 | } |
| 3662 | |
| 3663 | function unbindPointer () |
| 3664 | { |
| 3665 | removeWindowPointer(); |
| 3666 | if (boundCanvas) |
| 3667 | { boundCanvas.removeEventListener('pointerdown', onPointerDown); } |
| 3668 | boundCanvas = null; |
| 3669 | app.pointerId = null; |
| 3670 | } |
| 3671 | |
| 3672 | function fieldFromEvent (e) |
| 3673 | { |
| 3674 | var canvas = app.scene && app.scene.canvas; |
| 3675 | if (!canvas) |
| 3676 | { return null; } |
| 3677 | var rect = canvas.getBoundingClientRect(); |
| 3678 | if (rect.width <= 0 || rect.height <= 0) |
| 3679 | { return null; } |
| 3680 | // Map through the CSS box only. Using canvas.width / app.tile would |
| 3681 | // scale the pointer by the backing-store ratio whenever that differs |
| 3682 | // from the layout size (high-DPR devices), so a short finger move |
| 3683 | // would look like a long swipe. |
| 3684 | var width = Planet.FIELD_WIDTH; |
| 3685 | var height = Planet.FIELD_HEIGHT; |
| 3686 | var col = ((e.clientX - rect.left) / rect.width) * width; |
| 3687 | var fieldY = (1 - (e.clientY - rect.top) / rect.height) * height; |
| 3688 | return { col: col, y: fieldY }; |
| 3689 | } |
| 3690 | |
| 3691 | function onPointerDown (e) |
| 3692 | { |
| 3693 | if (!app.core || app.core.state !== 'playing') |
| 3694 | { return; } |
| 3695 | if (app.pointerId != null) |
| 3696 | { return; } |
| 3697 | e.preventDefault(); |
| 3698 | app.pointerId = e.pointerId; |
| 3699 | try |
| 3700 | { boundCanvas.setPointerCapture(e.pointerId); } |
| 3701 | catch (err) { /* ignore */ } |
| 3702 | addWindowPointer(); |
| 3703 | var pos = fieldFromEvent(e); |
| 3704 | // Instant grab, no long-press or hold delay: the block is taken on |
| 3705 | // pointerdown and the click tween starts in the same moment. |
| 3706 | if (pos && app.core.beginDrag(pos.col, pos.y) && app.core.drag) |
| 3707 | { startPressTween(app.core.drag.blockId); } |
| 3708 | } |
| 3709 | |
| 3710 | function onPointerMove (e) |
| 3711 | { |
| 3712 | if (app.pointerId == null || e.pointerId !== app.pointerId) |
| 3713 | { return; } |
| 3714 | if (!app.core || !app.core.drag) |
| 3715 | { return; } |
| 3716 | e.preventDefault(); |
| 3717 | var pos = fieldFromEvent(e); |
| 3718 | if (pos) |
| 3719 | { app.core.moveDrag(pos.y); } |
| 3720 | } |
| 3721 | |
| 3722 | function onPointerUp (e) |
| 3723 | { |
| 3724 | if (app.pointerId == null) |
| 3725 | { return; } |
| 3726 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 3727 | { return; } |
| 3728 | if (e) |
| 3729 | { e.preventDefault(); } |
| 3730 | app.pointerId = null; |
| 3731 | removeWindowPointer(); |
| 3732 | if (boundCanvas) |
| 3733 | { |
| 3734 | try |
| 3735 | { boundCanvas.releasePointerCapture(e && e.pointerId); } |
| 3736 | catch (err) { /* ignore */ } |
| 3737 | } |
| 3738 | startReleaseTween(); |
| 3739 | if (app.core) |
| 3740 | { app.core.endDrag(); } |
| 3741 | } |
| 3742 | |
| 3743 | function hideEl (id) |
| 3744 | { |
| 3745 | document.getElementById(id).hidden = true; |
| 3746 | } |
| 3747 | |
| 3748 | function showEl (id) |
| 3749 | { |
| 3750 | document.getElementById(id).hidden = false; |
| 3751 | } |
| 3752 | |
| 3753 | window.addEventListener('resize', function () |
| 3754 | { |
| 3755 | if (app.mode === 'playing') |
| 3756 | { layout(); } |
| 3757 | }); |
| 3758 | |
| 3759 | window.addEventListener('orientationchange', function () |
| 3760 | { |
| 3761 | setTimeout(function () |
| 3762 | { |
| 3763 | if (app.mode === 'playing') |
| 3764 | { layout(); } |
| 3765 | }, 120); |
| 3766 | }); |
| 3767 | |
| 3768 | /* ---------- online play (stage 2) ---------------------------------- */ |
| 3769 | |
| 3770 | function tickOnline (dt) |
| 3771 | { |
| 3772 | if (!app.online || typeof Online === 'undefined') |
| 3773 | { return; } |
| 3774 | if (app.core) |
| 3775 | { |
| 3776 | var shots = app.core.consumeOutgoingShots(); |
| 3777 | for (var i = 0; i < shots.length; i++) |
| 3778 | { |
| 3779 | var s = shots[i]; |
| 3780 | var start = fieldToViewport(s.col, s.y + 1); |
| 3781 | Online.rememberFlightLocal(s, start.x, start.y); |
| 3782 | } |
| 3783 | Online.queueShots(shots); |
| 3784 | } |
| 3785 | Online.tickFlights(); |
| 3786 | Online.renderMinis(document.getElementById('minis')); |
| 3787 | } |
| 3788 | |
| 3789 | function fieldToViewport (col, fieldY) |
| 3790 | { |
| 3791 | var canvas = app.scene && app.scene.canvas; |
| 3792 | if (!canvas) |
| 3793 | { return { x: window.innerWidth / 2, y: 40 }; } |
| 3794 | var rect = canvas.getBoundingClientRect(); |
| 3795 | var x = rect.left + ((col + 0.5) / 10) * rect.width; |
| 3796 | var y = rect.top + (1 - (fieldY / 13)) * rect.height; |
| 3797 | return { x: x, y: y }; |
| 3798 | } |
| 3799 | |
| 3800 | function onlinePayload () |
| 3801 | { |
| 3802 | if (!app.online || !app.core) |
| 3803 | { return {}; } |
| 3804 | return { |
| 3805 | snapshot: app.core.fieldSnapshot(), |
| 3806 | score: app.core.score, |
| 3807 | eliminated: app.core.state === 'gameover' || app.spectating || app.eliminatedReported |
| 3808 | }; |
| 3809 | } |
| 3810 | |
| 3811 | function onOnlineEliminated () |
| 3812 | { |
| 3813 | app.eliminatedReported = true; |
| 3814 | app.spectating = true; |
| 3815 | unbindPointer(); |
| 3816 | hideEl('game-over'); |
| 3817 | var lobby = typeof Online !== 'undefined' && Online.lobby; |
| 3818 | if (lobby && lobby.status === 'finished') |
| 3819 | { |
| 3820 | showPodium(lobby); |
| 3821 | return; |
| 3822 | } |
| 3823 | showEl('spectator'); |
| 3824 | } |
| 3825 | |
| 3826 | function showPodium (lobby) |
| 3827 | { |
| 3828 | hideEl('spectator'); |
| 3829 | hideEl('game-over'); |
| 3830 | hideEl('online-panel'); |
| 3831 | showEl('podium'); |
| 3832 | var list = document.getElementById('podium-list'); |
| 3833 | list.innerHTML = ''; |
| 3834 | var ranks = (lobby && lobby.rankings) || []; |
| 3835 | for (var i = 0; i < ranks.length; i++) |
| 3836 | { |
| 3837 | var r = ranks[i]; |
| 3838 | var li = document.createElement('li'); |
| 3839 | var medal = r.place === 1 ? '1e' : (r.place === 2 ? '2e' : (r.place === 3 ? '3e' : r.place + 'e')); |
| 3840 | li.textContent = medal + ' ' + r.name + ' · ' + r.score + ' punten'; |
| 3841 | list.appendChild(li); |
| 3842 | } |
| 3843 | var hint = document.getElementById('podium-hint'); |
| 3844 | var secs = (lobby && lobby.podium_seconds) || 5; |
| 3845 | hint.textContent = 'Over ' + secs + ' seconden teruggaan naar de lobby…'; |
| 3846 | } |
| 3847 | |
| 3848 | function startOnlineMatch () |
| 3849 | { |
| 3850 | if (app.mode === 'playing' && app.online) |
| 3851 | { return; } |
| 3852 | var planet = Planet.planetById(selectedPlanetId()); |
| 3853 | var me = (typeof Online !== 'undefined' && Online.mePlayer) ? Online.mePlayer() : null; |
| 3854 | if (me && me.planet && me.planet.id) |
| 3855 | { planet = Planet.planetById(me.planet.id); } |
| 3856 | startPlay({ online: true, planet: planet, missChance: Planet.missChance(planet) }); |
| 3857 | if (typeof Online !== 'undefined') |
| 3858 | { Online.startPolling(onlinePayload); } |
| 3859 | } |
| 3860 | |
| 3861 | function leaveOnlineMatch (goMenu) |
| 3862 | { |
| 3863 | if (typeof Online !== 'undefined') |
| 3864 | { |
| 3865 | Online.stopPolling(); |
| 3866 | Online.leaveLobby(); |
| 3867 | Online.clearFlights(); |
| 3868 | } |
| 3869 | teardownPlay(); |
| 3870 | app.online = false; |
| 3871 | if (goMenu) |
| 3872 | { |
| 3873 | app.mode = 'menu'; |
| 3874 | showEl('menu'); |
| 3875 | } |
| 3876 | else |
| 3877 | { |
| 3878 | app.mode = 'online'; |
| 3879 | openOnlinePanel(); |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | function bindOnlineUi () |
| 3884 | { |
| 3885 | var onlineBtn = document.getElementById('btn-online'); |
| 3886 | if (!onlineBtn) |
| 3887 | { return; } |
| 3888 | onlineBtn.addEventListener('click', function () |
| 3889 | { openOnlinePanel(); }); |
| 3890 | |
| 3891 | var backs = document.querySelectorAll('[data-back-menu]'); |
| 3892 | for (var i = 0; i < backs.length; i++) |
| 3893 | { |
| 3894 | backs[i].addEventListener('click', function () |
| 3895 | { |
| 3896 | if (typeof Online !== 'undefined') |
| 3897 | { Online.stopPolling(); } |
| 3898 | hideEl('online-panel'); |
| 3899 | app.mode = 'menu'; |
| 3900 | app.online = false; |
| 3901 | showEl('menu'); |
| 3902 | }); |
| 3903 | } |
| 3904 | |
| 3905 | document.getElementById('btn-create').addEventListener('click', function () |
| 3906 | { |
| 3907 | Online.createLobby().then(function (data) |
| 3908 | { |
| 3909 | if (data && data.ok) |
| 3910 | { |
| 3911 | renderOnline(); |
| 3912 | Online.startPolling(onlinePayload); |
| 3913 | } |
| 3914 | else |
| 3915 | { renderOnline(); } |
| 3916 | }); |
| 3917 | }); |
| 3918 | |
| 3919 | document.getElementById('join-form').addEventListener('submit', function (e) |
| 3920 | { |
| 3921 | e.preventDefault(); |
| 3922 | var code = (document.getElementById('join-code').value || '').trim(); |
| 3923 | if (!code) |
| 3924 | { return; } |
| 3925 | Online.joinLobby(code).then(function () |
| 3926 | { |
| 3927 | renderOnline(); |
| 3928 | if (Online.lobby) |
| 3929 | { Online.startPolling(onlinePayload); } |
| 3930 | }); |
| 3931 | }); |
| 3932 | |
| 3933 | document.getElementById('btn-ready').addEventListener('click', function () |
| 3934 | { |
| 3935 | var me = Online.mePlayer(); |
| 3936 | Online.setReady(!(me && me.ready)).then(renderOnline); |
| 3937 | }); |
| 3938 | |
| 3939 | document.getElementById('btn-start').addEventListener('click', function () |
| 3940 | { |
| 3941 | Online.startGame().then(function (data) |
| 3942 | { |
| 3943 | if (data && data.ok) |
| 3944 | { startOnlineMatch(); } |
| 3945 | else |
| 3946 | { renderOnline(); } |
| 3947 | }); |
| 3948 | }); |
| 3949 | |
| 3950 | document.getElementById('btn-leave-lobby').addEventListener('click', function () |
| 3951 | { |
| 3952 | Online.leaveLobby().then(function () |
| 3953 | { |
| 3954 | Online.stopPolling(); |
| 3955 | renderOnline(); |
| 3956 | }); |
| 3957 | }); |
| 3958 | |
| 3959 | document.getElementById('btn-copy-code').addEventListener('click', function () |
| 3960 | { |
| 3961 | var code = document.getElementById('lobby-code').textContent; |
| 3962 | if (navigator.clipboard && navigator.clipboard.writeText) |
| 3963 | { navigator.clipboard.writeText(code); } |
| 3964 | }); |
| 3965 | |
| 3966 | document.getElementById('btn-spectate').addEventListener('click', function () |
| 3967 | { |
| 3968 | hideEl('spectator'); |
| 3969 | app.spectating = true; |
| 3970 | }); |
| 3971 | |
| 3972 | document.getElementById('btn-leave-game').addEventListener('click', function () |
| 3973 | { leaveOnlineMatch(false); }); |
| 3974 | |
| 3975 | if (typeof Online !== 'undefined') |
| 3976 | { |
| 3977 | Online.onChange = function () |
| 3978 | { renderOnline(); }; |
| 3979 | Online.onGameStart = function () |
| 3980 | { startOnlineMatch(); }; |
| 3981 | Online.onIncoming = function (incoming) |
| 3982 | { |
| 3983 | if (!app.core || app.core.state !== 'playing') |
| 3984 | { return; } |
| 3985 | for (var i = 0; i < incoming.length; i++) |
| 3986 | { app.core.ingestGarbage(); } |
| 3987 | }; |
| 3988 | Online.onFinished = function (lobby) |
| 3989 | { showPodium(lobby); }; |
| 3990 | Online.onLobbyReturn = function (lobby) |
| 3991 | { |
| 3992 | teardownPlay(); |
| 3993 | app.online = false; |
| 3994 | app.mode = 'online'; |
| 3995 | hideEl('podium'); |
| 3996 | showEl('online-panel'); |
| 3997 | renderOnline(); |
| 3998 | Online.startPolling(onlinePayload); |
| 3999 | }; |
| 4000 | } |
| 4001 | |
| 4002 | var params = new URLSearchParams(window.location.search); |
| 4003 | if (params.get('online') === '1' || params.get('auth') === 'unconfigured') |
| 4004 | { openOnlinePanel(); } |
| 4005 | } |
| 4006 | |
| 4007 | function openOnlinePanel () |
| 4008 | { |
| 4009 | hideEl('menu'); |
| 4010 | hideEl('game-over'); |
| 4011 | hideEl('podium'); |
| 4012 | hideEl('spectator'); |
| 4013 | hideEl('planet-select'); |
| 4014 | showEl('online-panel'); |
| 4015 | app.mode = 'online'; |
| 4016 | setOnlineStatus('Even kijken of online spelen beschikbaar is…'); |
| 4017 | if (typeof Online === 'undefined') |
| 4018 | { |
| 4019 | showOnlineError('Online spelen is in deze build niet geladen. Je kunt wel alleen spelen.'); |
| 4020 | showOnly('online-unconfigured'); |
| 4021 | return; |
| 4022 | } |
| 4023 | Online.refresh().then(function (data) |
| 4024 | { |
| 4025 | if (!data || data.ok === false) |
| 4026 | { |
| 4027 | var msg = (data && data.error) || 'Geen verbinding met de server.'; |
| 4028 | showOnlineError(msg + ' Je kunt wel alleen spelen.'); |
| 4029 | if (data && data.configured === false) |
| 4030 | { showOnly('online-unconfigured'); } |
| 4031 | else |
| 4032 | { showOnly('online-login'); } |
| 4033 | document.getElementById('btn-login').style.display = 'none'; |
| 4034 | return; |
| 4035 | } |
| 4036 | renderOnline(); |
| 4037 | if (Online.lobby) |
| 4038 | { Online.startPolling(onlinePayload); } |
| 4039 | }); |
| 4040 | } |
| 4041 | |
| 4042 | function setOnlineStatus (text) |
| 4043 | { |
| 4044 | var el = document.getElementById('online-status'); |
| 4045 | if (el) |
| 4046 | { el.textContent = text; } |
| 4047 | } |
| 4048 | |
| 4049 | function showOnlineError (text) |
| 4050 | { |
| 4051 | var el = document.getElementById('online-error'); |
| 4052 | if (!el) |
| 4053 | { return; } |
| 4054 | if (!text) |
| 4055 | { |
| 4056 | el.hidden = true; |
| 4057 | el.textContent = ''; |
| 4058 | return; |
| 4059 | } |
| 4060 | el.hidden = false; |
| 4061 | el.textContent = text; |
| 4062 | } |
| 4063 | |
| 4064 | function showOnly (id) |
| 4065 | { |
| 4066 | var ids = ['online-unconfigured', 'online-login', 'online-home', 'online-lobby']; |
| 4067 | for (var i = 0; i < ids.length; i++) |
| 4068 | { document.getElementById(ids[i]).hidden = (ids[i] !== id); } |
| 4069 | } |
| 4070 | |
| 4071 | function renderOnline () |
| 4072 | { |
| 4073 | if (typeof Online === 'undefined') |
| 4074 | { return; } |
| 4075 | if (app.mode === 'playing' && app.online) |
| 4076 | { |
| 4077 | Online.renderMinis(document.getElementById('minis')); |
| 4078 | if (Online.lobby && Online.lobby.status === 'finished') |
| 4079 | { showPodium(Online.lobby); } |
| 4080 | return; |
| 4081 | } |
| 4082 | showOnlineError(Online.error); |
| 4083 | if (!Online.configured && !Online.me && !Online.local) |
| 4084 | { |
| 4085 | setOnlineStatus('Online spelen is nog niet ingesteld.'); |
| 4086 | showOnly('online-unconfigured'); |
| 4087 | return; |
| 4088 | } |
| 4089 | if (!Online.me) |
| 4090 | { |
| 4091 | setOnlineStatus('Log in om een lobby te maken of eraan deel te nemen.'); |
| 4092 | showOnly('online-login'); |
| 4093 | document.getElementById('btn-login').style.display = ''; |
| 4094 | document.getElementById('btn-login').href = Online.loginUrl(); |
| 4095 | document.getElementById('local-login').hidden = !Online.local; |
| 4096 | return; |
| 4097 | } |
| 4098 | if (Online.lobby) |
| 4099 | { |
| 4100 | setOnlineStatus('Lobby'); |
| 4101 | showOnly('online-lobby'); |
| 4102 | document.getElementById('lobby-me-name').textContent = Online.me.name; |
| 4103 | document.getElementById('lobby-code').textContent = Online.lobby.code; |
| 4104 | var list = document.getElementById('lobby-players'); |
| 4105 | list.innerHTML = ''; |
| 4106 | var players = Online.lobby.players || []; |
| 4107 | for (var i = 0; i < players.length; i++) |
| 4108 | { |
| 4109 | var p = players[i]; |
| 4110 | var li = document.createElement('li'); |
| 4111 | if (p.is_me) |
| 4112 | { li.classList.add('you'); } |
| 4113 | var label = p.name + (p.is_host ? ' (host)' : '') + (p.is_me ? ' (jij)' : ''); |
| 4114 | var state = p.ready ? 'Gereed' : 'Wacht…'; |
| 4115 | var world = (p.planet && p.planet.name) ? (p.planet.name + ' · ' + p.planet.people) : ''; |
| 4116 | li.innerHTML = '<span>' + escapeText(label) |
| 4117 | + (world ? '<span class="planet-tag">' + escapeText(world) + '</span>' : '') |
| 4118 | + '</span><span class="' |
| 4119 | + (p.ready ? 'ready' : 'waiting') + '">' + state + '</span>'; |
| 4120 | list.appendChild(li); |
| 4121 | } |
| 4122 | renderPlanetGrid('lobby-planet-grid', true); |
| 4123 | var me = Online.mePlayer(); |
| 4124 | document.getElementById('btn-ready').textContent = (me && me.ready) ? 'Niet gereed' : 'Gereed'; |
| 4125 | var start = document.getElementById('btn-start'); |
| 4126 | start.hidden = !(Online.lobby && Online.me && Online.lobby.host_id === Online.me.id); |
| 4127 | start.disabled = !Online.lobby.can_start; |
| 4128 | var hint = document.getElementById('lobby-hint'); |
| 4129 | if (Online.lobby.status === 'playing') |
| 4130 | { hint.textContent = 'Het spel start…'; } |
| 4131 | else if (players.length < 2) |
| 4132 | { hint.textContent = 'Wacht op minstens één andere speler. Deel de code.'; } |
| 4133 | else if (!Online.lobby.all_ready) |
| 4134 | { hint.textContent = 'Iedereen moet op Gereed drukken. De host start daarna.'; } |
| 4135 | else if (Online.lobby.can_start) |
| 4136 | { hint.textContent = 'Iedereen is gereed. Druk op Starten.'; } |
| 4137 | else |
| 4138 | { hint.textContent = 'Wachten tot de host start.'; } |
| 4139 | return; |
| 4140 | } |
| 4141 | setOnlineStatus('Hoi ' + Online.me.name + '. Maak een lobby of val in met een code.'); |
| 4142 | showOnly('online-home'); |
| 4143 | document.getElementById('me-name').textContent = Online.me.name; |
| 4144 | document.getElementById('local-login').hidden = !Online.local; |
| 4145 | } |
| 4146 | |
| 4147 | function escapeText (s) |
| 4148 | { |
| 4149 | return String(s) |
| 4150 | .replace(/&/g, '&') |
| 4151 | .replace(/</g, '<') |
| 4152 | .replace(/>/g, '>') |
| 4153 | .replace(/"/g, '"'); |
| 4154 | } |
| 4155 | |
| 4156 | |
| 4157 | function selectedPlanetId () |
| 4158 | { |
| 4159 | if (app.selectedPlanetId) |
| 4160 | { return app.selectedPlanetId; } |
| 4161 | return 'terra'; |
| 4162 | } |
| 4163 | |
| 4164 | function restoreSelectedPlanet () |
| 4165 | { |
| 4166 | var id = 'terra'; |
| 4167 | try |
| 4168 | { id = localStorage.getItem('meteors-planet') || 'terra'; } |
| 4169 | catch (e) {} |
| 4170 | setSelectedPlanet(id, true); |
| 4171 | } |
| 4172 | |
| 4173 | function setSelectedPlanet (id, silent) |
| 4174 | { |
| 4175 | var all = Planet.allPlanets(); |
| 4176 | var found = null; |
| 4177 | for (var i = 0; i < all.length; i++) |
| 4178 | { |
| 4179 | if (all[i].id === id) |
| 4180 | { found = all[i]; break; } |
| 4181 | } |
| 4182 | if (!found) |
| 4183 | { found = Planet.defaultPlanet(); } |
| 4184 | app.selectedPlanetId = found.id; |
| 4185 | try |
| 4186 | { localStorage.setItem('meteors-planet', found.id); } |
| 4187 | catch (e) {} |
| 4188 | if (!silent) |
| 4189 | { |
| 4190 | var grid = document.getElementById('planet-grid'); |
| 4191 | if (grid && !grid.hidden && grid.children.length) |
| 4192 | { renderPlanetGrid('planet-grid', false); } |
| 4193 | var lobby = document.getElementById('lobby-planet-grid'); |
| 4194 | if (lobby && lobby.children.length) |
| 4195 | { renderPlanetGrid('lobby-planet-grid', true); } |
| 4196 | } |
| 4197 | return found; |
| 4198 | } |
| 4199 | |
| 4200 | function openPlanetSelect (context) |
| 4201 | { |
| 4202 | if (app.mode === 'playing') |
| 4203 | { return; } |
| 4204 | app.planetSelectContext = context || 'solo'; |
| 4205 | app.mode = 'planet-select'; |
| 4206 | hideEl('menu'); |
| 4207 | hideEl('online-panel'); |
| 4208 | hideEl('game-over'); |
| 4209 | showEl('planet-select'); |
| 4210 | renderPlanetGrid('planet-grid', false); |
| 4211 | var go = document.getElementById('btn-planet-go'); |
| 4212 | if (go) |
| 4213 | { go.textContent = 'Spelen'; } |
| 4214 | } |
| 4215 | |
| 4216 | function renderPlanetGrid (containerId, compact) |
| 4217 | { |
| 4218 | var grid = document.getElementById(containerId); |
| 4219 | if (!grid) |
| 4220 | { return; } |
| 4221 | var current = selectedPlanetId(); |
| 4222 | var all = Planet.allPlanets(); |
| 4223 | grid.innerHTML = ''; |
| 4224 | if (compact) |
| 4225 | { grid.classList.add('compact'); } |
| 4226 | for (var i = 0; i < all.length; i++) |
| 4227 | { |
| 4228 | var pl = all[i]; |
| 4229 | var btn = document.createElement('button'); |
| 4230 | btn.type = 'button'; |
| 4231 | btn.className = 'planet-card' + (pl.id === current ? ' selected' : ''); |
| 4232 | btn.setAttribute('role', 'listitem'); |
| 4233 | btn.dataset.planet = pl.id; |
| 4234 | var swatches = ''; |
| 4235 | for (var c = 0; c < pl.colors.length; c++) |
| 4236 | { |
| 4237 | swatches += '<img src="' + escapeText(pl.colors[c].image) + '" alt="' |
| 4238 | + escapeText(pl.colors[c].name) + '" title="' + escapeText(pl.colors[c].name) + '">'; |
| 4239 | } |
| 4240 | btn.innerHTML = '<div class="planet-title">' + escapeText(pl.name) + '</div>' |
| 4241 | + '<div class="planet-people">' + escapeText(pl.people) + '</div>' |
| 4242 | + '<div class="planet-nums">' + escapeText(Planet.formatStats(pl)) + '</div>' |
| 4243 | + '<div class="planet-swatches">' + swatches + '</div>'; |
| 4244 | btn.addEventListener('click', (function (id, isCompact) |
| 4245 | { |
| 4246 | return function () |
| 4247 | { onPlanetPicked(id, isCompact); }; |
| 4248 | })(pl.id, !!compact)); |
| 4249 | grid.appendChild(btn); |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | function onPlanetPicked (id, fromLobby) |
| 4254 | { |
| 4255 | setSelectedPlanet(id); |
| 4256 | if (fromLobby && typeof Online !== 'undefined' && Online.lobby) |
| 4257 | { |
| 4258 | Online.setPlanet(id).then(function () |
| 4259 | { renderOnline(); }); |
| 4260 | } |
| 4261 | else |
| 4262 | { |
| 4263 | renderPlanetGrid('planet-grid', false); |
| 4264 | } |
| 4265 | } |
| 4266 | |
| 4267 | function confirmPlanetSelect () |
| 4268 | { |
| 4269 | var planet = setSelectedPlanet(selectedPlanetId()); |
| 4270 | hideEl('planet-select'); |
| 4271 | if (app.planetSelectContext === 'online') |
| 4272 | { |
| 4273 | openOnlinePanel(); |
| 4274 | return; |
| 4275 | } |
| 4276 | startPlay({ planet: planet }); |
| 4277 | } |
| 4278 | |
| 4279 | function cancelPlanetSelect () |
| 4280 | { |
| 4281 | hideEl('planet-select'); |
| 4282 | app.mode = 'menu'; |
| 4283 | showEl('menu'); |
| 4284 | } |
| 4285 | |
| 4286 | function bindPlanetSelect () |
| 4287 | { |
| 4288 | var go = document.getElementById('btn-planet-go'); |
| 4289 | var back = document.getElementById('btn-planet-back'); |
| 4290 | if (go) |
| 4291 | { |
| 4292 | go.addEventListener('click', confirmPlanetSelect); |
| 4293 | } |
| 4294 | if (back) |
| 4295 | { |
| 4296 | back.addEventListener('click', cancelPlanetSelect); |
| 4297 | } |
| 4298 | } |
| 4299 | |
| 4300 | /* |
| 4301 | * End File: |
| 4302 | * /workspace/meteors/src/main.js |
| 4303 | */ |
projects-timfalken-com meteors