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