| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * View and input for Meteors. The simulation lives in GameCore; this file |
| 5 | * only draws it, scales it to the viewport, and translates pointer events |
| 6 | * into beginDrag / moveDrag / endDrag. Online play (stage 2) is wired |
| 7 | * through Online (src/online.js) and the same GameCore. Offline Strijd |
| 8 | * (t1119) uses Offline (src/offline.js) against local bots. |
| 9 | */ |
| 10 | |
| 11 | function planetImages () |
| 12 | { |
| 13 | var list = [ |
| 14 | { name: 'gray', url: 'images/block-gray.png' }, |
| 15 | { name: 'locked', url: 'images/block-locked.png' }, |
| 16 | { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, |
| 17 | { name: 'cell', url: 'images/cell.png' }, |
| 18 | { name: 'pixel', url: 'images/pixel.png' }, |
| 19 | { name: 'columnWarn', url: 'images/column-warn.png' }, |
| 20 | // Keep the original Terra placeholders as aliases (Tim may still have them). |
| 21 | { name: 'ember', url: 'images/block-ember.png' }, |
| 22 | { name: 'aqua', url: 'images/block-aqua.png' }, |
| 23 | { name: 'leaf', url: 'images/block-leaf.png' }, |
| 24 | { name: 'sun', url: 'images/block-sun.png' }, |
| 25 | { name: 'orchid', url: 'images/block-orchid.png' } |
| 26 | ]; |
| 27 | var planets = (typeof Planet !== 'undefined' ? Planet.allPlanets() : []); |
| 28 | for (var i = 0; i < planets.length; i++) |
| 29 | { |
| 30 | var pl = planets[i]; |
| 31 | for (var c = 0; c < pl.colors.length; c++) |
| 32 | { |
| 33 | list.push({ |
| 34 | name: Planet.spriteKey(pl, c), |
| 35 | url: pl.colors[c].image |
| 36 | }); |
| 37 | } |
| 38 | } |
| 39 | if (typeof Blocks !== 'undefined' && Blocks.images) |
| 40 | { |
| 41 | var extra = Blocks.images(); |
| 42 | for (var b = 0; b < extra.length; b++) |
| 43 | { list.push(extra[b]); } |
| 44 | } |
| 45 | return list; |
| 46 | } |
| 47 | |
| 48 | var IMAGES = planetImages(); |
| 49 | var GAME_OVER_DELAY = 2; |
| 50 | |
| 51 | var game = new Game(); |
| 52 | var app = { |
| 53 | mode: 'menu', |
| 54 | core: null, |
| 55 | scene: null, |
| 56 | manager: null, |
| 57 | planet: null, |
| 58 | lastScore: 0, |
| 59 | tile: 48, |
| 60 | fieldX: 0, |
| 61 | fieldY: 0, |
| 62 | fieldW: 0, |
| 63 | fieldH: 0, |
| 64 | hudH: 56, |
| 65 | sprites: Object.create(null), |
| 66 | flames: Object.create(null), |
| 67 | cells: [], |
| 68 | pointerId: null, |
| 69 | pointerType: null, |
| 70 | activeTouchId: null, |
| 71 | debugDrag: false, |
| 72 | debugPhysics: false, |
| 73 | gameOverShown: false, |
| 74 | selectTween: null, |
| 75 | online: false, |
| 76 | eliminatedReported: false, |
| 77 | spectating: false, |
| 78 | miniH: 0, |
| 79 | selectedPlanetId: 'terra', |
| 80 | planetSelectContext: 'solo', |
| 81 | offline: false, |
| 82 | battle: null, |
| 83 | swapTweens: null, |
| 84 | columnWarn: null, |
| 85 | warnClock: 0, |
| 86 | miniAcc: 0, |
| 87 | offlineResultShown: false, |
| 88 | roundWonDelay: 0, |
| 89 | roundWonHandled: false, |
| 90 | rewardActive: false, |
| 91 | onlineBefore: 0, |
| 92 | pendingLobbyReturn: null |
| 93 | }; |
| 94 | |
| 95 | window.onload = function () |
| 96 | { |
| 97 | app.debugDrag = isDragDebugEnabled(); |
| 98 | app.debugPhysics = isPhysicsDebugEnabled(); |
| 99 | var play = document.getElementById('btn-play'); |
| 100 | play.addEventListener('click', onPlay); |
| 101 | bindGameMenu(); |
| 102 | play.addEventListener('pointerup', function (e) |
| 103 | { |
| 104 | // A touch-friendly path: some mobile browsers swallow the click |
| 105 | // after a slightly sloppy tap. Ignore the following click. |
| 106 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 107 | { |
| 108 | e.preventDefault(); |
| 109 | onPlay(); |
| 110 | } |
| 111 | }); |
| 112 | |
| 113 | game.preloadImagesThenStart(IMAGES, function () |
| 114 | { |
| 115 | if (game.images.flame && game.images.flame.addAnimationCycle) |
| 116 | { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } |
| 117 | }); |
| 118 | |
| 119 | if (typeof I18n !== 'undefined') |
| 120 | { |
| 121 | I18n.init(); |
| 122 | I18n.bindPicker(); |
| 123 | I18n.onChange(function () |
| 124 | { refreshAllUi(); }); |
| 125 | } |
| 126 | bindOnlineUi(); |
| 127 | bindOfflineUi(); |
| 128 | bindPlanetSelect(); |
| 129 | restoreSelectedPlanet(); |
| 130 | if (typeof Designer !== 'undefined' && Designer.bind) |
| 131 | { Designer.bind(); } |
| 132 | bindLoginPrompt(); |
| 133 | if (typeof Online !== 'undefined' && Online.refresh) |
| 134 | { |
| 135 | Online.refresh().then(function () |
| 136 | { |
| 137 | applyProfileLocale(); |
| 138 | refreshMenuAccount(); |
| 139 | restoreSelectedPlanet(); |
| 140 | renderPlanetGrid('planet-grid', false); |
| 141 | renderPlanetGrid('lobby-planet-grid', true); |
| 142 | maybeShowLoginPrompt(); |
| 143 | }); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | refreshMenuAccount(); |
| 148 | maybeShowLoginPrompt(); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | /** |
| 153 | * t1201: the big "Play" button starts the rounds against bots (internal |
| 154 | * mode 'offline', formerly "Offline Battle"); "Survival" starts the endless |
| 155 | * solo game (internal mode 'solo', formerly "Play"). Internal ids and |
| 156 | * play_finish modes are unchanged. |
| 157 | */ |
| 158 | function onPlay () |
| 159 | { |
| 160 | if (app.mode === 'playing') |
| 161 | { return; } |
| 162 | clearStartError(); |
| 163 | openPlanetSelect('offline'); |
| 164 | } |
| 165 | |
| 166 | function onSurvival () |
| 167 | { |
| 168 | if (app.mode === 'playing') |
| 169 | { return; } |
| 170 | clearStartError(); |
| 171 | openPlanetSelect('solo'); |
| 172 | } |
| 173 | |
| 174 | /** t1201: player-facing name of a mode / planet-select context. */ |
| 175 | function modeLabel (context) |
| 176 | { |
| 177 | var key = context === 'offline' ? 'menu.play' |
| 178 | : (context === 'online' ? 'menu.online' : 'menu.survival'); |
| 179 | var fallback = context === 'offline' ? 'Play' : (context === 'online' ? 'Play online' : 'Survival'); |
| 180 | if (typeof I18n === 'undefined') |
| 181 | { return fallback; } |
| 182 | var s = I18n.t(key); |
| 183 | return (s && s !== key) ? s : fallback; |
| 184 | } |
| 185 | |
| 186 | function startPlay (opts) |
| 187 | { |
| 188 | opts = opts || {}; |
| 189 | var planetMod = typeof Planet !== 'undefined' ? Planet : PlanetMod; |
| 190 | var planet = opts.planet || planetMod.planetById(selectedPlanetId()); |
| 191 | var miss = opts.missChance; |
| 192 | if (typeof miss !== 'number' || !isFinite(miss)) |
| 193 | { miss = planetMod.missChance(planet); } |
| 194 | |
| 195 | hideEl('menu'); |
| 196 | hideEl('game-over'); |
| 197 | hideEl('online-panel'); |
| 198 | hideEl('spectator'); |
| 199 | hideEl('podium'); |
| 200 | hideEl('planet-select'); |
| 201 | hideEl('offline-result'); |
| 202 | hideEl('designer'); |
| 203 | hideEl('designer-edit'); |
| 204 | if (typeof Designer !== 'undefined' && Designer.closeModal) |
| 205 | { Designer.closeModal(); } |
| 206 | // Do not show the HUD until the scene is on screen. Showing it first |
| 207 | // left the default "Terra" / "0" labels on a blank starfield when |
| 208 | // `new Scene` threw (WebGL unavailable) after inserting a transparent |
| 209 | // canvas — the t1143 live blank-screen report. |
| 210 | |
| 211 | app.planet = planet; |
| 212 | app.core = new GameCore(planet, Math.random, miss); |
| 213 | if (app.debugPhysics) |
| 214 | { app.core.debugInvariants = true; } |
| 215 | app.mode = 'playing'; |
| 216 | app.online = !!opts.online; |
| 217 | app.offline = !!opts.offline; |
| 218 | app.gameOverShown = false; |
| 219 | app.eliminatedReported = false; |
| 220 | app.spectating = false; |
| 221 | app.sprites = Object.create(null); |
| 222 | app.flames = Object.create(null); |
| 223 | app.cells = []; |
| 224 | app.pointerId = null; |
| 225 | app.selectTween = null; |
| 226 | app.swapTweens = Object.create(null); |
| 227 | app.columnWarn = []; |
| 228 | app.warnClock = 0; |
| 229 | app.miniAcc = 1; // draw minis on the first frame |
| 230 | app.offlineResultShown = false; |
| 231 | if (!opts.keepBattle) |
| 232 | { |
| 233 | app.roundWonDelay = 0; |
| 234 | app.roundWonHandled = false; |
| 235 | } |
| 236 | if (app.offline && !opts.keepBattle && typeof Offline !== 'undefined') |
| 237 | { |
| 238 | app.battle = Offline.createBattle({ |
| 239 | playerPlanet: app.planet, |
| 240 | rng: Math.random, |
| 241 | round: opts.round || 1 |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | layout(); |
| 246 | if (app.scene) |
| 247 | { |
| 248 | app.scene.destroy(); |
| 249 | app.scene = null; |
| 250 | } |
| 251 | try |
| 252 | { |
| 253 | app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); |
| 254 | app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; |
| 255 | app.scene.renderBackground = true; |
| 256 | game.loadScene(app.scene); |
| 257 | buildCells(); |
| 258 | app.manager = app.scene.addObject(makeManager()); |
| 259 | if (!app.spectating) |
| 260 | { bindPointer(app.scene.canvas); } |
| 261 | refreshHud(); |
| 262 | showEl('hud'); |
| 263 | setGameMenuButton(!app.online && !app.spectating); |
| 264 | } |
| 265 | catch (err) |
| 266 | { |
| 267 | abortStartPlay(err); |
| 268 | return; |
| 269 | } |
| 270 | if (app.online || app.offline) |
| 271 | { showEl('minis'); } |
| 272 | else |
| 273 | { hideEl('minis'); } |
| 274 | buildColumnWarn(); |
| 275 | refreshOfflineHud(); |
| 276 | refreshChainIndicator(); |
| 277 | if (!opts.online) |
| 278 | { beginPlaySession(opts.offline ? 'offline' : 'solo'); } |
| 279 | } |
| 280 | |
| 281 | function removeOrphanPlayCanvases () |
| 282 | { |
| 283 | // Scene's constructor inserts a body canvas, then GLContext may throw, |
| 284 | // leaving a transparent leftover that hides nothing and shows stars. |
| 285 | var nodes = document.querySelectorAll('body > canvas'); |
| 286 | for (var i = 0; i < nodes.length; i++) |
| 287 | { |
| 288 | if (nodes[i].parentNode) |
| 289 | { nodes[i].parentNode.removeChild(nodes[i]); } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | function abortStartPlay (err) |
| 294 | { |
| 295 | if (typeof console !== 'undefined' && console.error) |
| 296 | { console.error('startPlay failed', err); } |
| 297 | try |
| 298 | { |
| 299 | unbindPointer(); |
| 300 | if (app.scene && app.scene.destroy) |
| 301 | { app.scene.destroy(); } |
| 302 | } |
| 303 | catch (e) { /* ignore cleanup errors */ } |
| 304 | app.scene = null; |
| 305 | app.core = null; |
| 306 | app.manager = null; |
| 307 | app.mode = 'menu'; |
| 308 | app.online = false; |
| 309 | app.offline = false; |
| 310 | app.battle = null; |
| 311 | app.cells = []; |
| 312 | removeOrphanPlayCanvases(); |
| 313 | hideEl('hud'); |
| 314 | hideEl('minis'); |
| 315 | hideEl('planet-select'); |
| 316 | showEl('menu'); |
| 317 | refreshMenuAccount(); |
| 318 | var box = document.getElementById('start-error'); |
| 319 | if (box) |
| 320 | { |
| 321 | var msg = (err && err.message) ? String(err.message) : ''; |
| 322 | var webgl = /webgl/i.test(msg); |
| 323 | if (typeof I18n !== 'undefined') |
| 324 | { box.textContent = I18n.t(webgl ? 'error.webgl' : 'error.startFailed'); } |
| 325 | else |
| 326 | { |
| 327 | box.textContent = webgl |
| 328 | ? 'This game needs WebGL. Please try another browser, or turn on hardware acceleration.' |
| 329 | : 'Could not start the game.'; |
| 330 | } |
| 331 | box.hidden = false; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | function clearStartError () |
| 336 | { |
| 337 | var box = document.getElementById('start-error'); |
| 338 | if (box) |
| 339 | { |
| 340 | box.hidden = true; |
| 341 | box.textContent = ''; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | function teardownPlay () |
| 346 | { |
| 347 | unbindPointer(); |
| 348 | if (app.scene) |
| 349 | { |
| 350 | app.scene.destroy(); |
| 351 | app.scene = null; |
| 352 | } |
| 353 | app.manager = null; |
| 354 | app.core = null; |
| 355 | app.sprites = Object.create(null); |
| 356 | app.flames = Object.create(null); |
| 357 | app.cells = []; |
| 358 | app.selectTween = null; |
| 359 | app.swapTweens = Object.create(null); |
| 360 | app.columnWarn = []; |
| 361 | app.pointerId = null; |
| 362 | app.gameOverShown = false; |
| 363 | app.paused = false; |
| 364 | app.quitting = false; |
| 365 | hideEl('game-menu'); |
| 366 | setGameMenuButton(false); |
| 367 | hideEl('hud'); |
| 368 | hideEl('game-over'); |
| 369 | hideEl('spectator'); |
| 370 | hideEl('minis'); |
| 371 | hideEl('offline-result'); |
| 372 | hideOfflineHud(); |
| 373 | hideChainIndicator(); |
| 374 | if (typeof Online !== 'undefined') |
| 375 | { Online.clearFlights(); } |
| 376 | if (!app._keepBattleOnTeardown) |
| 377 | { |
| 378 | app.battle = null; |
| 379 | app.offline = false; |
| 380 | } |
| 381 | app._keepBattleOnTeardown = false; |
| 382 | } |
| 383 | |
| 384 | function returnToMenu () |
| 385 | { |
| 386 | clearStartError(); |
| 387 | if (app.online && typeof Online !== 'undefined') |
| 388 | { |
| 389 | // Online matches do not dump you back to the solo menu: |
| 390 | // elimination becomes spectating; a finished match shows the podium. |
| 391 | return; |
| 392 | } |
| 393 | teardownPlay(); |
| 394 | app.mode = 'menu'; |
| 395 | app.online = false; |
| 396 | app.offline = false; |
| 397 | app.battle = null; |
| 398 | hideEl('planet-select'); |
| 399 | showEl('menu'); |
| 400 | var last = document.getElementById('last-score'); |
| 401 | if (app.lastScore > 0) |
| 402 | { |
| 403 | last.hidden = false; |
| 404 | last.querySelector('strong').textContent = String(app.lastScore); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | function buildCells () |
| 409 | { |
| 410 | if (!app.scene) |
| 411 | { return; } |
| 412 | var img = game.images.cell; |
| 413 | for (var row = 0; row < 13; row++) |
| 414 | { |
| 415 | for (var col = 0; col < 10; col++) |
| 416 | { |
| 417 | var obj = new GameObject(0, 0, img, 0); |
| 418 | placeTile(obj, col, row); |
| 419 | obj.renderer.alpha = 0.9; |
| 420 | app.scene.addObject(obj); |
| 421 | app.cells.push(obj); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | function placeTile (obj, col, y) |
| 427 | { |
| 428 | var t = app.tile; |
| 429 | obj.transform.position.x = col * t + t / 2; |
| 430 | obj.transform.position.y = app.fieldH - (y + 0.5) * t; |
| 431 | obj.transform.size = { x: t, y: t }; |
| 432 | } |
| 433 | |
| 434 | function opponentCount () |
| 435 | { |
| 436 | if (app.offline && app.battle && app.battle.bots) |
| 437 | { return app.battle.bots.length; } |
| 438 | if (app.online && typeof Online !== 'undefined' && Online.opponents) |
| 439 | { return Online.opponents().length; } |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | function currentMiniSize () |
| 444 | { |
| 445 | if (app.miniLayout && app.miniLayout.mini) |
| 446 | { return { w: app.miniLayout.mini.w, h: app.miniLayout.mini.h }; } |
| 447 | var n = opponentCount(); |
| 448 | var mode = (app.miniLayout && app.miniLayout.mode) || 'top'; |
| 449 | if (typeof MiniLayout !== 'undefined' && MiniLayout.canvasSize) |
| 450 | { return MiniLayout.canvasSize(n, mode, 0); } |
| 451 | if (n >= 10) |
| 452 | { return { w: 40, h: 52 }; } |
| 453 | if (n >= 6) |
| 454 | { return { w: 56, h: 73 }; } |
| 455 | return { w: 80, h: 104 }; |
| 456 | } |
| 457 | |
| 458 | function applyMinisContainer (plan) |
| 459 | { |
| 460 | var minis = document.getElementById('minis'); |
| 461 | if (!minis) |
| 462 | { return; } |
| 463 | var n = plan ? plan.n : 0; |
| 464 | minis.classList.toggle('crowded', n > 5); |
| 465 | if (plan && n > 0 && plan.mode === 'top' && plan.mini && plan.mini.cols) |
| 466 | { |
| 467 | // t1203: top row is placed absolutely too, scaled to fit above the field. |
| 468 | minis.classList.remove('sides'); |
| 469 | minis.classList.add('placed'); |
| 470 | minis.style.top = '0'; |
| 471 | minis.style.bottom = '0'; |
| 472 | minis.style.left = '0'; |
| 473 | minis.style.right = '0'; |
| 474 | minis.style.maxHeight = 'none'; |
| 475 | return; |
| 476 | } |
| 477 | minis.classList.remove('placed'); |
| 478 | if (!plan || n === 0 || plan.mode !== 'sides') |
| 479 | { |
| 480 | minis.classList.remove('sides'); |
| 481 | minis.style.top = (app.hudH || 56) + 'px'; |
| 482 | minis.style.maxHeight = (plan && plan.miniH ? plan.miniH : 0) + 'px'; |
| 483 | minis.style.bottom = ''; |
| 484 | minis.style.left = ''; |
| 485 | minis.style.right = ''; |
| 486 | var kids = minis.querySelectorAll('.mini'); |
| 487 | for (var i = 0; i < kids.length; i++) |
| 488 | { |
| 489 | kids[i].style.left = ''; |
| 490 | kids[i].style.top = ''; |
| 491 | kids[i].style.width = ''; |
| 492 | kids[i].style.position = ''; |
| 493 | kids[i].style.fontSize = ''; |
| 494 | } |
| 495 | return; |
| 496 | } |
| 497 | minis.classList.add('sides'); |
| 498 | minis.style.top = '0'; |
| 499 | minis.style.bottom = '0'; |
| 500 | minis.style.left = '0'; |
| 501 | minis.style.right = '0'; |
| 502 | minis.style.maxHeight = 'none'; |
| 503 | } |
| 504 | |
| 505 | function applyMinisLayout () |
| 506 | { |
| 507 | var plan = app.miniLayout; |
| 508 | var minis = document.getElementById('minis'); |
| 509 | if (!minis) |
| 510 | { return; } |
| 511 | applyMinisContainer(plan); |
| 512 | if (!plan || !plan.n || (plan.mode !== 'sides' && plan.mode !== 'top')) |
| 513 | { return; } |
| 514 | var positions = plan.positions || []; |
| 515 | var kids = minis.querySelectorAll('.mini'); |
| 516 | if (plan.mode === 'top' && kids.length && typeof MiniLayout !== 'undefined' && MiniLayout.topPositions) |
| 517 | { |
| 518 | // t1203: if they do not all fit even at the minimum size, defeated |
| 519 | // opponents move to the back (stable), so the living stay visible. |
| 520 | var elim = []; |
| 521 | for (var k = 0; k < kids.length; k++) |
| 522 | { elim.push(kids[k].classList.contains('eliminated')); } |
| 523 | positions = MiniLayout.topPositions(elim, plan); |
| 524 | plan.positions = positions; |
| 525 | } |
| 526 | for (var i = 0; i < positions.length && i < kids.length; i++) |
| 527 | { |
| 528 | var p = positions[i]; |
| 529 | var el = kids[i]; |
| 530 | el.style.position = 'absolute'; |
| 531 | el.style.left = p.x + 'px'; |
| 532 | el.style.top = p.y + 'px'; |
| 533 | el.style.width = p.w + 'px'; |
| 534 | el.style.fontSize = p.font ? p.font + 'px' : ''; |
| 535 | el.setAttribute('data-slot', p.slot != null ? String(p.slot) : String(i)); |
| 536 | var canvas = el.querySelector('canvas'); |
| 537 | if (canvas) |
| 538 | { |
| 539 | canvas.style.width = p.canvasW + 'px'; |
| 540 | canvas.style.height = p.canvasH + 'px'; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | function applyFlightsBackClip () |
| 546 | { |
| 547 | var back = document.getElementById('flights-back'); |
| 548 | if (!back) |
| 549 | { return; } |
| 550 | var x = app.fieldX || 0; |
| 551 | var y = app.fieldY || 0; |
| 552 | var w = app.fieldW || 0; |
| 553 | var h = app.fieldH || 0; |
| 554 | if (!w || !h) |
| 555 | { |
| 556 | back.style.clipPath = ''; |
| 557 | return; |
| 558 | } |
| 559 | // Even-odd polygon: full viewport with a hole over the player's field, |
| 560 | // so enemy-to-enemy arcs travel behind the field (t1125). |
| 561 | back.style.clipPath = 'polygon(evenodd, 0 0, 100% 0, 100% 100%, 0 100%, 0 0, ' |
| 562 | + x + 'px ' + y + 'px, ' + (x + w) + 'px ' + y + 'px, ' |
| 563 | + (x + w) + 'px ' + (y + h) + 'px, ' + x + 'px ' + (y + h) + 'px, ' |
| 564 | + x + 'px ' + y + 'px)'; |
| 565 | } |
| 566 | |
| 567 | function layoutIfNeeded () |
| 568 | { |
| 569 | if (app.core && app.core.drag) |
| 570 | { return; } |
| 571 | var vw = window.innerWidth || 320; |
| 572 | var vh = window.innerHeight || 480; |
| 573 | var n = (app.online || app.offline) ? opponentCount() : 0; |
| 574 | if (app._layoutVw === vw && app._layoutVh === vh && app._layoutN === n |
| 575 | && app.scene && app.tile) |
| 576 | { return; } |
| 577 | layout(); |
| 578 | } |
| 579 | |
| 580 | function layout () |
| 581 | { |
| 582 | if (app.core && app.core.drag) |
| 583 | { return; } |
| 584 | var vw = window.innerWidth || 320; |
| 585 | var vh = window.innerHeight || 480; |
| 586 | var n = (app.online || app.offline) ? opponentCount() : 0; |
| 587 | app._layoutVw = vw; |
| 588 | app._layoutVh = vh; |
| 589 | app._layoutN = n; |
| 590 | var safeTop = 0; |
| 591 | try |
| 592 | { |
| 593 | // env(safe-area) is a CSS thing; keep a conservative HUD height. |
| 594 | safeTop = 0; |
| 595 | } |
| 596 | catch (e) { /* ignore */ } |
| 597 | app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); |
| 598 | var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); |
| 599 | var plan; |
| 600 | if (typeof MiniLayout !== 'undefined' && MiniLayout.plan) |
| 601 | { plan = MiniLayout.plan(vw, vh, n, app.hudH, margin); } |
| 602 | else |
| 603 | { |
| 604 | plan = { |
| 605 | mode: 'top', |
| 606 | tile: 14, |
| 607 | fieldW: 140, |
| 608 | fieldH: 182, |
| 609 | fieldX: Math.floor((vw - 140) / 2), |
| 610 | fieldY: app.hudH, |
| 611 | miniH: n > 0 ? 100 : 0, |
| 612 | mini: { w: 80, h: 104 }, |
| 613 | positions: [], |
| 614 | n: n |
| 615 | }; |
| 616 | } |
| 617 | app.miniLayout = plan; |
| 618 | app.miniH = plan.miniH; |
| 619 | app.tile = plan.tile; |
| 620 | app.fieldW = plan.fieldW; |
| 621 | app.fieldH = plan.fieldH; |
| 622 | app.fieldX = plan.fieldX; |
| 623 | app.fieldY = plan.fieldY; |
| 624 | applyMinisLayout(); |
| 625 | applyFlightsBackClip(); |
| 626 | |
| 627 | if (app.scene) |
| 628 | { |
| 629 | app.scene.position.x = app.fieldX; |
| 630 | app.scene.position.y = app.fieldY; |
| 631 | app.scene.size.x = app.fieldW; |
| 632 | app.scene.size.y = app.fieldH; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | function spriteFor (block) |
| 637 | { |
| 638 | if (block.state === 'locked') |
| 639 | { return game.images.locked; } |
| 640 | if (block.color < 0) |
| 641 | { return game.images.gray; } |
| 642 | var key = Planet.spriteKey(app.planet, block.color); |
| 643 | return game.images[key] || game.images.gray; |
| 644 | } |
| 645 | |
| 646 | /** Frames in the flame sprite (from the loaded image, default 4). */ |
| 647 | function flameFrameCount () |
| 648 | { |
| 649 | var img = typeof game !== 'undefined' && game.images ? game.images.flame : null; |
| 650 | return img && img.subImages ? img.subImages : 4; |
| 651 | } |
| 652 | |
| 653 | function syncSprites (dt) |
| 654 | { |
| 655 | if (!app.core || !app.scene) |
| 656 | { return; } |
| 657 | dt = dt || 0; |
| 658 | consumeAndStartSwapTweens(); |
| 659 | advanceSwapTweens(dt); |
| 660 | var seen = Object.create(null); |
| 661 | var blocks = app.core.blocks; |
| 662 | for (var i = 0; i < blocks.length; i++) |
| 663 | { |
| 664 | var block = blocks[i]; |
| 665 | seen[block.id] = true; |
| 666 | var obj = app.sprites[block.id]; |
| 667 | if (!obj) |
| 668 | { |
| 669 | obj = new GameObject(0, 0, spriteFor(block), 2); |
| 670 | app.scene.addObject(obj); |
| 671 | app.sprites[block.id] = obj; |
| 672 | } |
| 673 | obj.renderer.sprite = spriteFor(block); |
| 674 | placeTile(obj, block.col, blockVisualY(block)); |
| 675 | applyBlockVisual(obj, block); |
| 676 | obj.renderer.visible = true; |
| 677 | obj.renderer.alpha = 1; |
| 678 | if (typeof Online !== 'undefined' && Online.hasExitVisual && Online.hasExitVisual(block.id)) |
| 679 | { |
| 680 | obj.renderer.visible = false; |
| 681 | obj.renderer.alpha = 0; |
| 682 | } |
| 683 | |
| 684 | if (block.flame) |
| 685 | { |
| 686 | var flame = app.flames[block.id]; |
| 687 | if (!flame) |
| 688 | { |
| 689 | flame = new GameObject(0, 0, game.images.flame, 1); |
| 690 | // t1183: frames are chosen by FlameFx, not the framework cycle. |
| 691 | flame.fx = FlameFx.create(flameFrameCount(), Math.random, FlameFx.reducedMotion()); |
| 692 | flame.renderer.setSubImage(flame.fx.frame); |
| 693 | app.scene.addObject(flame); |
| 694 | app.flames[block.id] = flame; |
| 695 | } |
| 696 | flame.renderer.visible = true; |
| 697 | FlameFx.advance(flame.fx, dt, Math.random, FlameFx.reducedMotion()); |
| 698 | var t = app.tile; |
| 699 | // 3x as large (length and width, t1110). Keep the top of the |
| 700 | // flame at the same overlap into the block so the extra length |
| 701 | // extends downward under the rocket. t1183: random frame, flip, |
| 702 | // scale and rotation all pivot on that top-centre anchor. |
| 703 | var flameW = t * 0.85 * 3; |
| 704 | var flameH = t * 0.85 * 3; |
| 705 | var origCenter = app.fieldH - block.y * t + t * 0.15; |
| 706 | var origTop = origCenter - (t * 0.85) / 2; |
| 707 | var pose = FlameFx.pose(flame.fx, block.col * t + t / 2, origTop, flameW, flameH); |
| 708 | flame.renderer.subImage = pose.frame; |
| 709 | flame.renderer.mirrorX = pose.mirrorX; |
| 710 | flame.transform.rotation = pose.rotation; |
| 711 | flame.transform.position.x = pose.x; |
| 712 | flame.transform.position.y = pose.y; |
| 713 | flame.transform.size = { x: pose.w, y: pose.h }; |
| 714 | } |
| 715 | else if (app.flames[block.id]) |
| 716 | { app.flames[block.id].renderer.visible = false; } |
| 717 | if (block.flame && app.flames[block.id] |
| 718 | && typeof Online !== 'undefined' && Online.hasExitVisual |
| 719 | && Online.hasExitVisual(block.id)) |
| 720 | { app.flames[block.id].renderer.visible = false; } |
| 721 | } |
| 722 | |
| 723 | var ids = Object.keys(app.sprites); |
| 724 | for (var i = 0; i < ids.length; i++) |
| 725 | { |
| 726 | if (!seen[ids[i]]) |
| 727 | { |
| 728 | app.sprites[ids[i]].destroy(); |
| 729 | delete app.sprites[ids[i]]; |
| 730 | if (app.flames[ids[i]]) |
| 731 | { |
| 732 | app.flames[ids[i]].destroy(); |
| 733 | delete app.flames[ids[i]]; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | function applyBlockVisual (obj, block) |
| 740 | { |
| 741 | var t = app.tile; |
| 742 | var sx = 1; |
| 743 | var sy = 1; |
| 744 | var dy = 0; |
| 745 | if (block.bouncing && app.planet && app.planet.bounceDuration > 0 |
| 746 | && typeof Easing !== 'undefined' && Easing.bouncePose) |
| 747 | { |
| 748 | var u = block.bounceAge / app.planet.bounceDuration; |
| 749 | var pose = Easing.bouncePose(u, app.planet.bounceSquash); |
| 750 | sx = pose.sx; |
| 751 | sy = pose.sy; |
| 752 | dy = pose.dy; |
| 753 | } |
| 754 | var sel = 1; |
| 755 | var tw = app.selectTween; |
| 756 | if (tw && tw.blockId === block.id && tw.phase !== 'idle') |
| 757 | { sel = tw.scale; } |
| 758 | obj.transform.size = { x: t * sx * sel, y: t * sy * sel }; |
| 759 | if (dy) |
| 760 | { obj.transform.position.y -= dy * t; } |
| 761 | } |
| 762 | |
| 763 | function clickTween () |
| 764 | { |
| 765 | return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null; |
| 766 | } |
| 767 | |
| 768 | function startPressTween (blockId) |
| 769 | { |
| 770 | var c = clickTween(); |
| 771 | if (!c) |
| 772 | { return; } |
| 773 | var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1; |
| 774 | app.selectTween = { |
| 775 | blockId: blockId, |
| 776 | from: from, |
| 777 | to: c.pressScale, |
| 778 | t: 0, |
| 779 | duration: c.pressDuration, |
| 780 | ease: c.pressEase, |
| 781 | scale: from, |
| 782 | phase: 'press' |
| 783 | }; |
| 784 | } |
| 785 | |
| 786 | function startReleaseTween () |
| 787 | { |
| 788 | var c = clickTween(); |
| 789 | var tw = app.selectTween; |
| 790 | if (!c || !tw || tw.phase === 'idle') |
| 791 | { return; } |
| 792 | app.selectTween = { |
| 793 | blockId: tw.blockId, |
| 794 | from: tw.scale, |
| 795 | to: c.releaseScale, |
| 796 | t: 0, |
| 797 | duration: c.releaseDuration, |
| 798 | ease: c.releaseEase, |
| 799 | scale: tw.scale, |
| 800 | phase: 'release' |
| 801 | }; |
| 802 | } |
| 803 | |
| 804 | function updateSelectTween (dt) |
| 805 | { |
| 806 | var tw = app.selectTween; |
| 807 | if (!tw || tw.phase === 'idle' || tw.phase === 'hold') |
| 808 | { return; } |
| 809 | tw.t += dt; |
| 810 | var u = tw.duration > 0 ? tw.t / tw.duration : 1; |
| 811 | if (u >= 1) |
| 812 | { |
| 813 | tw.scale = tw.to; |
| 814 | tw.t = tw.duration; |
| 815 | if (tw.phase === 'press') |
| 816 | { tw.phase = 'hold'; } |
| 817 | else |
| 818 | { |
| 819 | tw.phase = 'idle'; |
| 820 | tw.scale = 1; |
| 821 | tw.blockId = null; |
| 822 | } |
| 823 | return; |
| 824 | } |
| 825 | var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack); |
| 826 | tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u; |
| 827 | } |
| 828 | |
| 829 | |
| 830 | /* ---------- visual-only swap tween (t1121) -------------------------- */ |
| 831 | |
| 832 | function swapTweenSpec () |
| 833 | { |
| 834 | if (typeof Easing !== 'undefined' && Easing.SWAP_TWEEN) |
| 835 | { return Easing.SWAP_TWEEN; } |
| 836 | return { duration: 0.100, ease: null }; |
| 837 | } |
| 838 | |
| 839 | function swapEase (u) |
| 840 | { |
| 841 | if (typeof Easing !== 'undefined' && Easing.easeOutCubic) |
| 842 | { return Easing.easeOutCubic(u); } |
| 843 | var v = 1 - Math.max(0, Math.min(1, u)); |
| 844 | return 1 - v * v * v; |
| 845 | } |
| 846 | |
| 847 | function tweenVisualY (tw) |
| 848 | { |
| 849 | var u = tw.duration > 0 ? Math.min(1, tw.age / tw.duration) : 1; |
| 850 | return tw.fromY + (tw.toY - tw.fromY) * swapEase(u); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * Consume GameCore swap events. The currently dragged block is skipped |
| 855 | * (it follows the pointer). A block that is mid-tween and is swapped |
| 856 | * again restarts from its current visual y, so rapid swaps chain |
| 857 | * smoothly without snapping. |
| 858 | */ |
| 859 | function consumeAndStartSwapTweens () |
| 860 | { |
| 861 | if (!app.core || !app.core.consumeSwapEvents) |
| 862 | { return; } |
| 863 | if (!app.swapTweens) |
| 864 | { app.swapTweens = Object.create(null); } |
| 865 | var ev = app.core.consumeSwapEvents(); |
| 866 | if (!ev.length) |
| 867 | { return; } |
| 868 | var dragId = app.core.drag && app.core.drag.blockId; |
| 869 | var duration = swapTweenSpec().duration; |
| 870 | for (var i = 0; i < ev.length; i++) |
| 871 | { |
| 872 | var e = ev[i]; |
| 873 | if (e.id === dragId) |
| 874 | { |
| 875 | delete app.swapTweens[e.id]; |
| 876 | continue; |
| 877 | } |
| 878 | var fromY = e.fromY; |
| 879 | var existing = app.swapTweens[e.id]; |
| 880 | if (existing) |
| 881 | { fromY = tweenVisualY(existing); } |
| 882 | app.swapTweens[e.id] = { |
| 883 | fromY: fromY, |
| 884 | toY: e.toY, |
| 885 | age: 0, |
| 886 | duration: duration |
| 887 | }; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | function advanceSwapTweens (dt) |
| 892 | { |
| 893 | if (!app.swapTweens) |
| 894 | { return; } |
| 895 | var ids = Object.keys(app.swapTweens); |
| 896 | for (var i = 0; i < ids.length; i++) |
| 897 | { |
| 898 | var tw = app.swapTweens[ids[i]]; |
| 899 | tw.age += dt; |
| 900 | if (tw.age >= tw.duration) |
| 901 | { delete app.swapTweens[ids[i]]; } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | function blockVisualY (block) |
| 906 | { |
| 907 | if (!app.swapTweens) |
| 908 | { return block.y; } |
| 909 | var dragId = app.core && app.core.drag && app.core.drag.blockId; |
| 910 | if (block.id === dragId) |
| 911 | { return block.y; } |
| 912 | var tw = app.swapTweens[block.id]; |
| 913 | if (!tw) |
| 914 | { return block.y; } |
| 915 | return tweenVisualY(tw); |
| 916 | } |
| 917 | |
| 918 | function refreshHud () |
| 919 | { |
| 920 | if (!app.core) |
| 921 | { return; } |
| 922 | document.getElementById('score').textContent = String(app.core.score); |
| 923 | var planet = app.planet; |
| 924 | var pname = (typeof I18n !== 'undefined') ? I18n.planetName(planet) : planet.name; |
| 925 | var people = (typeof I18n !== 'undefined') ? I18n.peopleName(planet) : planet.people; |
| 926 | document.getElementById('planet-name').textContent = pname + ' · ' + people; |
| 927 | document.getElementById('planet-stats').textContent = Planet.formatStats(planet); |
| 928 | } |
| 929 | |
| 930 | function makeManager () |
| 931 | { |
| 932 | var obj = new GameObject(0, 0, game.images.pixel, 0); |
| 933 | obj.renderer.visible = false; |
| 934 | var base = obj.update.bind(obj); |
| 935 | obj.update = function (dt) |
| 936 | { |
| 937 | base(dt); |
| 938 | tickView(dt); |
| 939 | }; |
| 940 | return obj; |
| 941 | } |
| 942 | |
| 943 | function tickView (dt) |
| 944 | { |
| 945 | if (app.mode !== 'playing' || !app.core) |
| 946 | { return; } |
| 947 | |
| 948 | // Never resize the canvas mid-drag: a URL-bar hide would move the |
| 949 | // field under the finger and jump the target row. Also skip work |
| 950 | // when the viewport has not actually changed. |
| 951 | if (!(app.core && app.core.drag)) |
| 952 | { layoutIfNeeded(); } |
| 953 | for (var i = 0; i < app.cells.length; i++) |
| 954 | { |
| 955 | var col = i % 10; |
| 956 | var row = Math.floor(i / 10); |
| 957 | placeTile(app.cells[i], col, row); |
| 958 | } |
| 959 | |
| 960 | // t1193: in-game menu open (Spelen / Offline Strijd) or quitting: |
| 961 | // the field, bots and timers stand still; only the view is kept. |
| 962 | if (app.paused) |
| 963 | { |
| 964 | syncSprites(0); |
| 965 | return; |
| 966 | } |
| 967 | |
| 968 | app.core.tick(dt); |
| 969 | if (app.offline && app.battle) |
| 970 | { tickOffline(dt); } |
| 971 | updateSelectTween(dt); |
| 972 | syncSprites(dt); |
| 973 | updateColumnWarn(dt); |
| 974 | refreshHud(); |
| 975 | refreshOfflineHud(); |
| 976 | refreshChainIndicator(); |
| 977 | tickOnline(dt); |
| 978 | if (app.debugPhysics) |
| 979 | { logPhysicsDebug(); } |
| 980 | |
| 981 | if (app.core.state === 'gameover' && !app.gameOverShown) |
| 982 | { |
| 983 | app.gameOverShown = true; |
| 984 | app.lastScore = app.core.score; |
| 985 | document.getElementById('go-score').textContent = String(app.lastScore); |
| 986 | if (app.online) |
| 987 | { onOnlineEliminated(); } |
| 988 | else if (app.offline) |
| 989 | { /* Offline result is shown from tickOffline. */ } |
| 990 | else if (rewardEnabled()) |
| 991 | { showReward(false, finishPlaySession(app.lastScore), returnToMenu); } |
| 992 | else |
| 993 | { |
| 994 | showEl('game-over'); |
| 995 | finishPlaySession(app.lastScore); |
| 996 | } |
| 997 | } |
| 998 | if (!app.online && !app.offline && !app.rewardActive && app.core.state === 'gameover' |
| 999 | && app.core.gameOverTime >= GAME_OVER_DELAY) |
| 1000 | { returnToMenu(); } |
| 1001 | } |
| 1002 | |
| 1003 | /* ---------- pointer input (mouse + touch) --------------------------- */ |
| 1004 | /* |
| 1005 | * Mouse uses Pointer Events. Touch uses Touch Events as the authority: |
| 1006 | * on a real phone, pointer clientX/Y can disagree with the visual |
| 1007 | * finger (zoomed visual viewport, gesture takeover, capture cancel) |
| 1008 | * while changedTouches stays correct. When a touch is active, pointer |
| 1009 | * move/up/cancel are ignored so a poisoned pointer coordinate cannot |
| 1010 | * walk the block to the top or bottom of the stack (t1106). |
| 1011 | * |
| 1012 | * After a down we also listen on the document, so a drag that leaves |
| 1013 | * the canvas (needed to launch a block at the top of a full column) |
| 1014 | * still reaches moveDrag. We do not call setPointerCapture: on mobile |
| 1015 | * that is often cancelled when the browser takes the gesture, which |
| 1016 | * would fire pointercancel mid-drag. |
| 1017 | * |
| 1018 | * A non-passive document touchstart/touchmove preventDefault during |
| 1019 | * play stops scrolling, rubber-banding and the pointercancel that |
| 1020 | * follows. pointercancel / touchcancel end the drag without launch. |
| 1021 | * |
| 1022 | * ?debug=drag (or localStorage meteors-debug-drag=1) shows an overlay. |
| 1023 | */ |
| 1024 | |
| 1025 | var boundCanvas = null; |
| 1026 | var windowListening = false; |
| 1027 | var touchListening = false; |
| 1028 | var lastDragDebug = null; |
| 1029 | |
| 1030 | function isDragDebugEnabled () |
| 1031 | { |
| 1032 | try |
| 1033 | { |
| 1034 | var q = window.location && window.location.search; |
| 1035 | if (q && /(?:\?|&)debug=drag(?:&|$)/.test(q)) |
| 1036 | { return true; } |
| 1037 | if (window.localStorage && window.localStorage.getItem('meteors-debug-drag') === '1') |
| 1038 | { return true; } |
| 1039 | } |
| 1040 | catch (err) { /* ignore */ } |
| 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | /** ?debug, ?debug=physics, ?debug=drag, or localStorage meteors-debug=1. */ |
| 1045 | function isPhysicsDebugEnabled () |
| 1046 | { |
| 1047 | try |
| 1048 | { |
| 1049 | var q = window.location && window.location.search; |
| 1050 | if (q && /(?:\?|&)debug(?:=|&|$)/.test(q)) |
| 1051 | { return true; } |
| 1052 | if (window.localStorage && (window.localStorage.getItem('meteors-debug') === '1' |
| 1053 | || window.localStorage.getItem('meteors-debug-drag') === '1')) |
| 1054 | { return true; } |
| 1055 | } |
| 1056 | catch (err) { /* ignore */ } |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | function eventClient (e) |
| 1061 | { |
| 1062 | var t = null; |
| 1063 | if (e.changedTouches && e.changedTouches.length) |
| 1064 | { t = e.changedTouches[0]; } |
| 1065 | else if (e.touches && e.touches.length) |
| 1066 | { t = e.touches[0]; } |
| 1067 | if (t) |
| 1068 | { return { x: t.clientX, y: t.clientY, id: t.identifier, src: 'touch' }; } |
| 1069 | if (e.clientX != null && e.clientY != null) |
| 1070 | { return { x: e.clientX, y: e.clientY, id: e.pointerId, src: 'pointer' }; } |
| 1071 | return null; |
| 1072 | } |
| 1073 | |
| 1074 | function addWindowPointer () |
| 1075 | { |
| 1076 | if (windowListening) |
| 1077 | { return; } |
| 1078 | window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false }); |
| 1079 | window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false }); |
| 1080 | window.addEventListener('pointercancel', onPointerCancel, { capture: true, passive: false }); |
| 1081 | windowListening = true; |
| 1082 | } |
| 1083 | |
| 1084 | function removeWindowPointer () |
| 1085 | { |
| 1086 | if (!windowListening) |
| 1087 | { return; } |
| 1088 | window.removeEventListener('pointermove', onPointerMove, { capture: true }); |
| 1089 | window.removeEventListener('pointerup', onPointerUp, { capture: true }); |
| 1090 | window.removeEventListener('pointercancel', onPointerCancel, { capture: true }); |
| 1091 | windowListening = false; |
| 1092 | } |
| 1093 | |
| 1094 | function addTouchListeners () |
| 1095 | { |
| 1096 | if (touchListening) |
| 1097 | { return; } |
| 1098 | var opts = { capture: true, passive: false }; |
| 1099 | document.addEventListener('touchstart', onTouchStart, opts); |
| 1100 | document.addEventListener('touchmove', onTouchMove, opts); |
| 1101 | document.addEventListener('touchend', onTouchEnd, opts); |
| 1102 | document.addEventListener('touchcancel', onTouchCancel, opts); |
| 1103 | touchListening = true; |
| 1104 | } |
| 1105 | |
| 1106 | function removeTouchListeners () |
| 1107 | { |
| 1108 | if (!touchListening) |
| 1109 | { return; } |
| 1110 | var opts = { capture: true }; |
| 1111 | document.removeEventListener('touchstart', onTouchStart, opts); |
| 1112 | document.removeEventListener('touchmove', onTouchMove, opts); |
| 1113 | document.removeEventListener('touchend', onTouchEnd, opts); |
| 1114 | document.removeEventListener('touchcancel', onTouchCancel, opts); |
| 1115 | touchListening = false; |
| 1116 | } |
| 1117 | |
| 1118 | function bindPointer (canvas) |
| 1119 | { |
| 1120 | unbindPointer(); |
| 1121 | boundCanvas = canvas; |
| 1122 | canvas.addEventListener('pointerdown', onPointerDown, { passive: false }); |
| 1123 | canvas.style.touchAction = 'none'; |
| 1124 | canvas.style.position = 'fixed'; |
| 1125 | canvas.style.cursor = 'pointer'; |
| 1126 | canvas.style.webkitUserSelect = 'none'; |
| 1127 | canvas.style.userSelect = 'none'; |
| 1128 | addTouchListeners(); |
| 1129 | if (app.debugDrag) |
| 1130 | { ensureDragDebugOverlay(); } |
| 1131 | } |
| 1132 | |
| 1133 | function unbindPointer () |
| 1134 | { |
| 1135 | removeWindowPointer(); |
| 1136 | removeTouchListeners(); |
| 1137 | if (boundCanvas) |
| 1138 | { boundCanvas.removeEventListener('pointerdown', onPointerDown); } |
| 1139 | boundCanvas = null; |
| 1140 | app.pointerId = null; |
| 1141 | app.pointerType = null; |
| 1142 | app.activeTouchId = null; |
| 1143 | } |
| 1144 | |
| 1145 | function fieldFromEvent (e) |
| 1146 | { |
| 1147 | var canvas = app.scene && app.scene.canvas; |
| 1148 | if (!canvas) |
| 1149 | { return null; } |
| 1150 | var rect = canvas.getBoundingClientRect(); |
| 1151 | if (rect.width <= 0 || rect.height <= 0) |
| 1152 | { return null; } |
| 1153 | var pt = eventClient(e); |
| 1154 | if (!pt) |
| 1155 | { return null; } |
| 1156 | // Map through the CSS box only. Using canvas.width / app.tile would |
| 1157 | // scale the pointer by the backing-store ratio whenever that differs |
| 1158 | // from the layout size (high-DPR devices), so a short finger move |
| 1159 | // would look like a long swipe. |
| 1160 | var width = Planet.FIELD_WIDTH; |
| 1161 | var height = Planet.FIELD_HEIGHT; |
| 1162 | var col = ((pt.x - rect.left) / rect.width) * width; |
| 1163 | var fieldY = (1 - (pt.y - rect.top) / rect.height) * height; |
| 1164 | return { col: col, y: fieldY, src: pt.src, clientX: pt.x, clientY: pt.y, rect: rect }; |
| 1165 | } |
| 1166 | |
| 1167 | function playingForDrag () |
| 1168 | { |
| 1169 | return app.core && app.core.state === 'playing' && !app.paused; |
| 1170 | } |
| 1171 | |
| 1172 | /** Touches on HUD buttons / menus are UI taps, not field drags (t1193). */ |
| 1173 | function isUiTouchTarget (e) |
| 1174 | { |
| 1175 | var el = e && e.target; |
| 1176 | if (!el || !el.closest) |
| 1177 | { return false; } |
| 1178 | return !!el.closest('button, a, input, select, #game-menu, #reward'); |
| 1179 | } |
| 1180 | |
| 1181 | function startDragFromEvent (e) |
| 1182 | { |
| 1183 | var pos = fieldFromEvent(e); |
| 1184 | if (!pos) |
| 1185 | { return; } |
| 1186 | if (app.core.drag) |
| 1187 | { |
| 1188 | app.core.moveDrag(pos.y); |
| 1189 | logDragDebug(e, pos, 'move-already'); |
| 1190 | return; |
| 1191 | } |
| 1192 | if (app.core.beginDrag(pos.col, pos.y) && app.core.drag) |
| 1193 | { startPressTween(app.core.drag.blockId); } |
| 1194 | logDragDebug(e, pos, 'down'); |
| 1195 | } |
| 1196 | |
| 1197 | function moveDragFromEvent (e) |
| 1198 | { |
| 1199 | if (!app.core || !app.core.drag) |
| 1200 | { return; } |
| 1201 | var pos = fieldFromEvent(e); |
| 1202 | if (pos) |
| 1203 | { app.core.moveDrag(pos.y); } |
| 1204 | logDragDebug(e, pos, 'move'); |
| 1205 | } |
| 1206 | |
| 1207 | function finishDrag (opts) |
| 1208 | { |
| 1209 | opts = opts || {}; |
| 1210 | app.pointerId = null; |
| 1211 | app.pointerType = null; |
| 1212 | app.activeTouchId = null; |
| 1213 | removeWindowPointer(); |
| 1214 | if (boundCanvas) |
| 1215 | { |
| 1216 | try |
| 1217 | { |
| 1218 | if (opts.pointerId != null && boundCanvas.releasePointerCapture) |
| 1219 | { boundCanvas.releasePointerCapture(opts.pointerId); } |
| 1220 | } |
| 1221 | catch (err) { /* ignore */ } |
| 1222 | } |
| 1223 | startReleaseTween(); |
| 1224 | if (app.core && app.core.drag) |
| 1225 | { |
| 1226 | if (opts.noLaunch) |
| 1227 | { app.core.drag.launchIntent = false; } |
| 1228 | app.core.endDrag(); |
| 1229 | } |
| 1230 | else if (app.core) |
| 1231 | { app.core.endDrag(); } |
| 1232 | logDragDebug(null, null, opts.noLaunch ? 'cancel' : 'up'); |
| 1233 | } |
| 1234 | |
| 1235 | function onPointerDown (e) |
| 1236 | { |
| 1237 | if (!playingForDrag()) |
| 1238 | { return; } |
| 1239 | if (app.pointerId != null && e.pointerId !== app.pointerId) |
| 1240 | { return; } |
| 1241 | if (e.preventDefault) |
| 1242 | { e.preventDefault(); } |
| 1243 | app.pointerId = e.pointerId; |
| 1244 | app.pointerType = e.pointerType || 'mouse'; |
| 1245 | addWindowPointer(); |
| 1246 | // If a touch is already in charge, do not reset the grab from pointerdown. |
| 1247 | if (app.activeTouchId != null) |
| 1248 | { return; } |
| 1249 | startDragFromEvent(e); |
| 1250 | } |
| 1251 | |
| 1252 | function onPointerMove (e) |
| 1253 | { |
| 1254 | if (app.pointerId == null || e.pointerId !== app.pointerId) |
| 1255 | { return; } |
| 1256 | if (!app.core || !app.core.drag) |
| 1257 | { return; } |
| 1258 | // Touch coordinates are the authority; ignore pointer moves for a |
| 1259 | // live touch so a wrong clientY cannot walk the whole column. |
| 1260 | // Only skip when touchstart has actually arrived — some environments |
| 1261 | // deliver touch-pointer events without a TouchEvent stream. |
| 1262 | if (app.activeTouchId != null) |
| 1263 | { |
| 1264 | if (e.preventDefault) |
| 1265 | { e.preventDefault(); } |
| 1266 | logDragDebug(e, fieldFromEvent(e), 'pointer-ignored'); |
| 1267 | return; |
| 1268 | } |
| 1269 | if (e.preventDefault) |
| 1270 | { e.preventDefault(); } |
| 1271 | moveDragFromEvent(e); |
| 1272 | } |
| 1273 | |
| 1274 | function onPointerUp (e) |
| 1275 | { |
| 1276 | if (app.pointerId == null) |
| 1277 | { return; } |
| 1278 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 1279 | { return; } |
| 1280 | if (app.activeTouchId != null) |
| 1281 | { |
| 1282 | // touchend will finish the drag. |
| 1283 | if (e && e.preventDefault) |
| 1284 | { e.preventDefault(); } |
| 1285 | logDragDebug(e, fieldFromEvent(e), 'pointer-up-ignored'); |
| 1286 | return; |
| 1287 | } |
| 1288 | if (e && e.preventDefault) |
| 1289 | { e.preventDefault(); } |
| 1290 | finishDrag({ pointerId: e && e.pointerId }); |
| 1291 | } |
| 1292 | |
| 1293 | function onPointerCancel (e) |
| 1294 | { |
| 1295 | if (app.pointerId == null) |
| 1296 | { return; } |
| 1297 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 1298 | { return; } |
| 1299 | if (app.activeTouchId != null) |
| 1300 | { |
| 1301 | if (e && e.preventDefault) |
| 1302 | { e.preventDefault(); } |
| 1303 | logDragDebug(e, fieldFromEvent(e), 'pointer-cancel-ignored'); |
| 1304 | return; |
| 1305 | } |
| 1306 | if (e && e.preventDefault) |
| 1307 | { e.preventDefault(); } |
| 1308 | // A cancelled gesture is not a launch pull. |
| 1309 | finishDrag({ pointerId: e && e.pointerId, noLaunch: true }); |
| 1310 | } |
| 1311 | |
| 1312 | function touchById (list, id) |
| 1313 | { |
| 1314 | if (!list) |
| 1315 | { return null; } |
| 1316 | for (var i = 0; i < list.length; i++) |
| 1317 | { |
| 1318 | if (list[i].identifier === id) |
| 1319 | { return list[i]; } |
| 1320 | } |
| 1321 | return null; |
| 1322 | } |
| 1323 | |
| 1324 | function onTouchStart (e) |
| 1325 | { |
| 1326 | if (!playingForDrag()) |
| 1327 | { return; } |
| 1328 | if (app.activeTouchId == null && isUiTouchTarget(e)) |
| 1329 | { return; } |
| 1330 | if (app.activeTouchId != null) |
| 1331 | { |
| 1332 | if (e.preventDefault) |
| 1333 | { e.preventDefault(); } |
| 1334 | return; |
| 1335 | } |
| 1336 | var t = e.changedTouches && e.changedTouches[0]; |
| 1337 | if (!t) |
| 1338 | { return; } |
| 1339 | if (e.preventDefault) |
| 1340 | { e.preventDefault(); } |
| 1341 | app.activeTouchId = t.identifier; |
| 1342 | addWindowPointer(); |
| 1343 | startDragFromEvent(e); |
| 1344 | } |
| 1345 | |
| 1346 | function onTouchMove (e) |
| 1347 | { |
| 1348 | if (app.activeTouchId == null) |
| 1349 | { return; } |
| 1350 | if (!touchById(e.changedTouches, app.activeTouchId) |
| 1351 | && !touchById(e.touches, app.activeTouchId)) |
| 1352 | { return; } |
| 1353 | if (e.preventDefault) |
| 1354 | { e.preventDefault(); } |
| 1355 | moveDragFromEvent(e); |
| 1356 | } |
| 1357 | |
| 1358 | function onTouchEnd (e) |
| 1359 | { |
| 1360 | if (app.activeTouchId == null) |
| 1361 | { return; } |
| 1362 | if (!touchById(e.changedTouches, app.activeTouchId)) |
| 1363 | { return; } |
| 1364 | if (e.preventDefault) |
| 1365 | { e.preventDefault(); } |
| 1366 | finishDrag({}); |
| 1367 | } |
| 1368 | |
| 1369 | function onTouchCancel (e) |
| 1370 | { |
| 1371 | if (app.activeTouchId == null) |
| 1372 | { return; } |
| 1373 | if (!touchById(e.changedTouches, app.activeTouchId)) |
| 1374 | { return; } |
| 1375 | if (e.preventDefault) |
| 1376 | { e.preventDefault(); } |
| 1377 | finishDrag({ noLaunch: true }); |
| 1378 | } |
| 1379 | |
| 1380 | function logPhysicsDebug () |
| 1381 | { |
| 1382 | if (!app.debugPhysics || !app.core) |
| 1383 | { return; } |
| 1384 | ensureDragDebugOverlay(); |
| 1385 | var el = document.getElementById('drag-debug'); |
| 1386 | if (!el) |
| 1387 | { return; } |
| 1388 | var issues = app.core.lastInvariantViolations || []; |
| 1389 | var flying = 0; |
| 1390 | var manuals = 0; |
| 1391 | var clusters = app.core.clusters ? Object.keys(app.core.clusters).length : 0; |
| 1392 | for (var i = 0; i < app.core.blocks.length; i++) |
| 1393 | { |
| 1394 | if (app.core.blocks[i].state === 'flying') |
| 1395 | { flying++; } |
| 1396 | if (app.core.blocks[i].state === 'manual') |
| 1397 | { manuals++; } |
| 1398 | } |
| 1399 | var status = issues.length |
| 1400 | ? ('PHYSICS FAIL x' + issues.length + '\n' + issues.slice(0, 6).join('\n')) |
| 1401 | : ('physics ok blocks=' + app.core.blocks.length |
| 1402 | + ' fly=' + flying + ' man=' + manuals + ' cl=' + clusters); |
| 1403 | // Keep any live drag line above the physics status. |
| 1404 | var dragLine = ''; |
| 1405 | if (app.debugDrag && lastDragDebug) |
| 1406 | { |
| 1407 | dragLine = el.textContent && el.textContent.indexOf('drag ') === 0 |
| 1408 | ? el.textContent.split('\n')[0] + '\n' |
| 1409 | : ''; |
| 1410 | } |
| 1411 | el.textContent = dragLine + status; |
| 1412 | el.hidden = false; |
| 1413 | el.style.color = issues.length ? '#ffb4b4' : '#c6f6c8'; |
| 1414 | } |
| 1415 | |
| 1416 | function ensureDragDebugOverlay () |
| 1417 | { |
| 1418 | if (document.getElementById('drag-debug')) |
| 1419 | { return; } |
| 1420 | var el = document.createElement('div'); |
| 1421 | el.id = 'drag-debug'; |
| 1422 | el.setAttribute('aria-live', 'polite'); |
| 1423 | document.body.appendChild(el); |
| 1424 | } |
| 1425 | |
| 1426 | function logDragDebug (e, pos, phase) |
| 1427 | { |
| 1428 | if (!app.debugDrag) |
| 1429 | { return; } |
| 1430 | ensureDragDebugOverlay(); |
| 1431 | var el = document.getElementById('drag-debug'); |
| 1432 | if (!el) |
| 1433 | { return; } |
| 1434 | var block = null; |
| 1435 | if (app.core && app.core.drag) |
| 1436 | { block = app.core.blockById(app.core.drag.blockId); } |
| 1437 | var pt = e ? eventClient(e) : null; |
| 1438 | var evType = e ? e.type : phase; |
| 1439 | var info = { |
| 1440 | phase: phase, |
| 1441 | ev: evType, |
| 1442 | src: pos && pos.src, |
| 1443 | fieldY: pos && Math.round(pos.y * 100) / 100, |
| 1444 | col: pos && Math.round(pos.col * 100) / 100, |
| 1445 | cy: pt && Math.round(pt.y * 10) / 10, |
| 1446 | blockY: block && Math.round(block.y * 100) / 100, |
| 1447 | target: block && Math.floor((pos && pos.y) + 1e-12), |
| 1448 | launch: app.core && app.core.drag && app.core.drag.launchIntent, |
| 1449 | touchId: app.activeTouchId, |
| 1450 | pid: app.pointerId, |
| 1451 | ptype: app.pointerType |
| 1452 | }; |
| 1453 | lastDragDebug = info; |
| 1454 | el.textContent = 'drag ' + phase |
| 1455 | + ' ev=' + evType |
| 1456 | + ' src=' + (info.src || '-') |
| 1457 | + ' y=' + (info.fieldY != null ? info.fieldY : '-') |
| 1458 | + ' → ' + (info.target != null ? info.target : '-') |
| 1459 | + ' block=' + (info.blockY != null ? info.blockY : '-') |
| 1460 | + (info.launch ? ' LAUNCH' : '') |
| 1461 | + ' tId=' + info.touchId |
| 1462 | + ' pId=' + info.pid; |
| 1463 | el.hidden = false; |
| 1464 | } |
| 1465 | |
| 1466 | |
| 1467 | function hideEl (id) |
| 1468 | { |
| 1469 | document.getElementById(id).hidden = true; |
| 1470 | } |
| 1471 | |
| 1472 | function showEl (id) |
| 1473 | { |
| 1474 | document.getElementById(id).hidden = false; |
| 1475 | } |
| 1476 | |
| 1477 | window.addEventListener('resize', function () |
| 1478 | { |
| 1479 | if (app.mode === 'playing') |
| 1480 | { layout(); } |
| 1481 | }); |
| 1482 | |
| 1483 | window.addEventListener('orientationchange', function () |
| 1484 | { |
| 1485 | setTimeout(function () |
| 1486 | { |
| 1487 | if (app.mode === 'playing') |
| 1488 | { layout(); } |
| 1489 | }, 120); |
| 1490 | }); |
| 1491 | |
| 1492 | /* ---------- online play (stage 2) ---------------------------------- */ |
| 1493 | |
| 1494 | function tickOnline (dt) |
| 1495 | { |
| 1496 | if (!app.online || typeof Online === 'undefined') |
| 1497 | { return; } |
| 1498 | if (app.core) |
| 1499 | { |
| 1500 | var shots = app.core.consumeOutgoingShots(); |
| 1501 | for (var i = 0; i < shots.length; i++) |
| 1502 | { |
| 1503 | var s = shots[i]; |
| 1504 | var start = fieldToViewport(s.col, s.y); |
| 1505 | Online.rememberFlightLocal(s, start.x, start.y); |
| 1506 | // Draw the flight locally right away (t1181); the server |
| 1507 | // answer only retargets it. |
| 1508 | if (Online.startLocalShot) |
| 1509 | { Online.startLocalShot(s, start); } |
| 1510 | } |
| 1511 | Online.queueShots(shots); |
| 1512 | if (shots.length && Online.flushShotsSoon) |
| 1513 | { Online.flushShotsSoon(); } |
| 1514 | } |
| 1515 | Online.tickFlights(); |
| 1516 | Online.renderMinis(document.getElementById('minis')); |
| 1517 | applyMinisLayout(); |
| 1518 | } |
| 1519 | |
| 1520 | |
| 1521 | /* ---------- Offline Strijd (t1119) + column warn (t1123) ----------- */ |
| 1522 | |
| 1523 | function bindOfflineUi () |
| 1524 | { |
| 1525 | // t1201: the second menu button is now "Survival" (endless solo). |
| 1526 | var btn = document.getElementById('btn-survival'); |
| 1527 | if (btn) |
| 1528 | { |
| 1529 | btn.addEventListener('click', onSurvival); |
| 1530 | btn.addEventListener('pointerup', function (e) |
| 1531 | { |
| 1532 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 1533 | { |
| 1534 | e.preventDefault(); |
| 1535 | onSurvival(); |
| 1536 | } |
| 1537 | }); |
| 1538 | } |
| 1539 | var back = document.getElementById('btn-offline-menu'); |
| 1540 | if (back) |
| 1541 | { |
| 1542 | back.addEventListener('click', function () |
| 1543 | { |
| 1544 | teardownPlay(); |
| 1545 | app.mode = 'menu'; |
| 1546 | app.offline = false; |
| 1547 | app.battle = null; |
| 1548 | hideEl('offline-result'); |
| 1549 | showEl('menu'); |
| 1550 | }); |
| 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | function tickOffline (dt) |
| 1555 | { |
| 1556 | if (!app.battle || !app.core || typeof Offline === 'undefined') |
| 1557 | { return; } |
| 1558 | |
| 1559 | if (app.battle.state === 'round_won') |
| 1560 | { |
| 1561 | if (!app.roundWonHandled) |
| 1562 | { |
| 1563 | app.roundWonHandled = true; |
| 1564 | app.roundWonDelay = Offline.ROUND_WON_DELAY || 1.5; |
| 1565 | if (rewardEnabled()) |
| 1566 | { showReward(true, finishPlaySession(app.core.score), startNextOfflineRound); } |
| 1567 | } |
| 1568 | if (app.rewardActive) |
| 1569 | { |
| 1570 | updateOfflineMinis(dt, true); |
| 1571 | return; |
| 1572 | } |
| 1573 | app.roundWonDelay -= dt; |
| 1574 | if (app.roundWonDelay <= 0) |
| 1575 | { startNextOfflineRound(); } |
| 1576 | updateOfflineMinis(dt, true); |
| 1577 | return; |
| 1578 | } |
| 1579 | |
| 1580 | var result = app.battle.tick(dt, app.core); |
| 1581 | if (result && result.flights && result.flights.length |
| 1582 | && typeof Online !== 'undefined' && Online.spawnFlights) |
| 1583 | { |
| 1584 | var locals = Object.create(null); |
| 1585 | var list = result.locals || []; |
| 1586 | for (var i = 0; i < list.length; i++) |
| 1587 | { |
| 1588 | var s = list[i]; |
| 1589 | var start; |
| 1590 | if (s.from_id === 'player') |
| 1591 | { start = fieldToViewport(s.col, s.y); } |
| 1592 | else if (typeof Online !== 'undefined' && Online.miniCellViewport) |
| 1593 | { start = Online.miniCellViewport(s.from_id, s.col, s.y != null ? s.y : 12); } |
| 1594 | else |
| 1595 | { start = miniViewportCenter(s.from_id); } |
| 1596 | locals[s.client_id] = { |
| 1597 | col: s.col, |
| 1598 | y: s.y, |
| 1599 | color: s.color, |
| 1600 | startX: start.x, |
| 1601 | startY: start.y, |
| 1602 | from_id: s.from_id, |
| 1603 | land_col: s.land_col |
| 1604 | }; |
| 1605 | } |
| 1606 | Online.spawnFlights(result.flights, locals); |
| 1607 | } |
| 1608 | if (typeof Online !== 'undefined' && Online.tickFlights) |
| 1609 | { Online.tickFlights(); } |
| 1610 | updateOfflineMinis(dt, false); |
| 1611 | |
| 1612 | if (app.battle.state === 'lost' && !app.offlineResultShown) |
| 1613 | { |
| 1614 | if (rewardEnabled()) |
| 1615 | { |
| 1616 | // Guard against starting the reward twice; showOfflineResult |
| 1617 | // sets the flag again when it runs afterwards. |
| 1618 | app.offlineResultShown = true; |
| 1619 | showReward(false, finishPlaySession(app.core ? app.core.score : 0), showOfflineResult); |
| 1620 | } |
| 1621 | else |
| 1622 | { showOfflineResult(); } |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | function startNextOfflineRound () |
| 1627 | { |
| 1628 | if (!app.battle) |
| 1629 | { return; } |
| 1630 | app.battle.advanceRound(); |
| 1631 | app.roundWonHandled = false; |
| 1632 | app.roundWonDelay = 0; |
| 1633 | app._keepBattleOnTeardown = true; |
| 1634 | // Tear down the current scene then start a fresh player field, |
| 1635 | // keeping the same battle (already advanced). |
| 1636 | var planet = app.battle.playerPlanet; |
| 1637 | var roundScore = app.core ? app.core.score : 0; |
| 1638 | finishPlaySession(roundScore); |
| 1639 | app._keepBattleOnTeardown = true; |
| 1640 | teardownPlay(); |
| 1641 | app.offline = true; |
| 1642 | app.battle = app.battle; // kept |
| 1643 | startPlay({ planet: planet, offline: true, keepBattle: true }); |
| 1644 | } |
| 1645 | |
| 1646 | function showOfflineResult () |
| 1647 | { |
| 1648 | app.offlineResultShown = true; |
| 1649 | var battle = app.battle; |
| 1650 | var reached = document.getElementById('offline-reached'); |
| 1651 | var line = document.getElementById('offline-reached-line'); |
| 1652 | if (line && typeof I18n !== 'undefined') |
| 1653 | { line.textContent = I18n.t('offline.reached', { n: battle.round }); } |
| 1654 | else if (reached) |
| 1655 | { reached.textContent = String(battle.round); } |
| 1656 | // t1198: "X opponents defeated" over all rounds (replaces the |
| 1657 | // opponents / skill line). |
| 1658 | var detail = document.getElementById('offline-result-detail'); |
| 1659 | if (detail) |
| 1660 | { |
| 1661 | var beaten = battle.defeated || 0; |
| 1662 | detail.textContent = (typeof I18n !== 'undefined') |
| 1663 | ? I18n.plural('offline.defeated', beaten, { n: beaten }) |
| 1664 | : beaten + (beaten === 1 ? ' opponent defeated' : ' opponents defeated'); |
| 1665 | } |
| 1666 | hideEl('game-over'); |
| 1667 | showEl('offline-result'); |
| 1668 | finishPlaySession(app.core ? app.core.score : 0); |
| 1669 | } |
| 1670 | |
| 1671 | function refreshOfflineHud () |
| 1672 | { |
| 1673 | var el = document.getElementById('offline-hud'); |
| 1674 | if (!el) |
| 1675 | { return; } |
| 1676 | if (!(app.offline && app.battle && app.mode === 'playing')) |
| 1677 | { |
| 1678 | el.hidden = true; |
| 1679 | return; |
| 1680 | } |
| 1681 | el.hidden = false; |
| 1682 | // t1195: the Offline Strijd HUD shows only "Round N". |
| 1683 | var sch = app.battle.schedule; |
| 1684 | var r = document.getElementById('offline-round'); |
| 1685 | if (r) |
| 1686 | { r.textContent = (typeof I18n !== 'undefined') ? I18n.t('hud.round', { n: sch.round }) : 'Round ' + sch.round; } |
| 1687 | } |
| 1688 | |
| 1689 | function hideOfflineHud () |
| 1690 | { |
| 1691 | var el = document.getElementById('offline-hud'); |
| 1692 | if (el) |
| 1693 | { el.hidden = true; } |
| 1694 | } |
| 1695 | |
| 1696 | function refreshChainIndicator () |
| 1697 | { |
| 1698 | var el = document.getElementById('chain-indicator'); |
| 1699 | if (!el) |
| 1700 | { return; } |
| 1701 | // t1200: the yellow "+x%" shows ONLY the combo bonus |
| 1702 | // (0.01·min(combo, 30)); the landing-chain bonus still boosts the jump |
| 1703 | // but is not part of this number. Visible while that bonus is > 0. |
| 1704 | var pct = (app.core && app.core.comboBonusPct) ? app.core.comboBonusPct() : 0; |
| 1705 | if (!(pct > 0) || app.mode !== 'playing') |
| 1706 | { |
| 1707 | el.hidden = true; |
| 1708 | return; |
| 1709 | } |
| 1710 | el.hidden = false; |
| 1711 | el.textContent = '+' + pct + '%'; |
| 1712 | } |
| 1713 | |
| 1714 | function hideChainIndicator () |
| 1715 | { |
| 1716 | var el = document.getElementById('chain-indicator'); |
| 1717 | if (el) |
| 1718 | { el.hidden = true; } |
| 1719 | } |
| 1720 | |
| 1721 | function miniViewportCenter (botId) |
| 1722 | { |
| 1723 | var el = document.querySelector('#minis .mini[data-player="' + botId + '"] canvas'); |
| 1724 | if (!el) |
| 1725 | { return { x: window.innerWidth / 2, y: 40 }; } |
| 1726 | var rect = el.getBoundingClientRect(); |
| 1727 | var plan = app.miniLayout; |
| 1728 | if (plan && plan.mode === 'sides') |
| 1729 | { |
| 1730 | // Leave from the top of the mini, then the arc goes up and over (t1131). |
| 1731 | return { x: rect.left + rect.width / 2, y: rect.top + 4 }; |
| 1732 | } |
| 1733 | return { x: rect.left + rect.width / 2, y: rect.bottom }; |
| 1734 | } |
| 1735 | |
| 1736 | function offlineMiniSize (n) |
| 1737 | { |
| 1738 | return currentMiniSize(); |
| 1739 | } |
| 1740 | |
| 1741 | function updateOfflineMinis (dt, force) |
| 1742 | { |
| 1743 | app.miniAcc = (app.miniAcc || 0) + dt; |
| 1744 | if (!force && app.miniAcc < 0.1) |
| 1745 | { return; } |
| 1746 | app.miniAcc = 0; |
| 1747 | renderOfflineMinis(); |
| 1748 | } |
| 1749 | |
| 1750 | function renderOfflineMinis () |
| 1751 | { |
| 1752 | var container = document.getElementById('minis'); |
| 1753 | if (!container || !app.battle) |
| 1754 | { return; } |
| 1755 | var bots = app.battle.bots; |
| 1756 | var size = offlineMiniSize(bots.length); |
| 1757 | container.classList.toggle('crowded', bots.length > 5); |
| 1758 | var existing = {}; |
| 1759 | var kids = container.querySelectorAll('[data-player]'); |
| 1760 | for (var i = 0; i < kids.length; i++) |
| 1761 | { existing[kids[i].getAttribute('data-player')] = kids[i]; } |
| 1762 | var seen = Object.create(null); |
| 1763 | for (var i = 0; i < bots.length; i++) |
| 1764 | { |
| 1765 | var bot = bots[i]; |
| 1766 | seen[bot.id] = true; |
| 1767 | var el = existing[bot.id]; |
| 1768 | if (!el) |
| 1769 | { |
| 1770 | el = document.createElement('div'); |
| 1771 | el.className = 'mini'; |
| 1772 | el.setAttribute('data-player', bot.id); |
| 1773 | el.innerHTML = '<div class="mini-name"></div>' |
| 1774 | + '<canvas class="mini-canvas" width="' + size.w |
| 1775 | + '" height="' + size.h + '"></canvas>' |
| 1776 | + '<div class="mini-meta"></div>'; |
| 1777 | container.appendChild(el); |
| 1778 | } |
| 1779 | var canvas = el.querySelector('canvas'); |
| 1780 | if (canvas && (canvas.width !== size.w || canvas.height !== size.h)) |
| 1781 | { |
| 1782 | canvas.width = size.w; |
| 1783 | canvas.height = size.h; |
| 1784 | } |
| 1785 | el.classList.toggle('eliminated', !!bot.eliminated); |
| 1786 | var planetName = bot.planet && bot.planet.name ? bot.planet.name : ''; |
| 1787 | var shownPlanet = planetName; |
| 1788 | if (bot.planet && typeof I18n !== 'undefined') |
| 1789 | { shownPlanet = I18n.planetName(bot.planet); } |
| 1790 | el.querySelector('.mini-name').textContent = bot.name |
| 1791 | + (shownPlanet ? ' · ' + shownPlanet : ''); |
| 1792 | el.querySelector('.mini-meta').textContent = bot.eliminated |
| 1793 | ? ((typeof I18n !== 'undefined') ? I18n.t('hud.out') : 'Out') |
| 1794 | : ((typeof I18n !== 'undefined') ? I18n.t('hud.miniPoints', { n: bot.score || 0 }) : ('Points ' + (bot.score || 0))); |
| 1795 | var snap = bot.core ? bot.core.fieldSnapshot() : { blocks: [] }; |
| 1796 | if (typeof Online !== 'undefined' && Online._drawMini) |
| 1797 | { Online._drawMini(canvas, snap, bot.planet); } |
| 1798 | } |
| 1799 | for (var id in existing) |
| 1800 | { |
| 1801 | if (!seen[id]) |
| 1802 | { existing[id].remove(); } |
| 1803 | } |
| 1804 | applyMinisLayout(); |
| 1805 | } |
| 1806 | |
| 1807 | function buildColumnWarn () |
| 1808 | { |
| 1809 | app.columnWarn = []; |
| 1810 | if (!app.scene) |
| 1811 | { return; } |
| 1812 | var img = (game.images && (game.images.columnWarn || game.images.pixel)) || null; |
| 1813 | if (!img) |
| 1814 | { return; } |
| 1815 | for (var c = 0; c < 10; c++) |
| 1816 | { |
| 1817 | var obj = new GameObject(0, 0, img, 0); |
| 1818 | obj.renderer.alpha = 0; |
| 1819 | obj.renderer.visible = false; |
| 1820 | app.scene.addObject(obj); |
| 1821 | app.columnWarn.push(obj); |
| 1822 | } |
| 1823 | updateColumnWarn(0); |
| 1824 | } |
| 1825 | |
| 1826 | function updateColumnWarn (dt) |
| 1827 | { |
| 1828 | if (!app.columnWarn || !app.core) |
| 1829 | { return; } |
| 1830 | app.warnClock = (app.warnClock || 0) + (dt || 0); |
| 1831 | var pulse = (typeof GameCore !== 'undefined' && GameCore.columnWarnPulse) |
| 1832 | ? GameCore.columnWarnPulse(app.warnClock) |
| 1833 | : 0.16 + 0.28 * (0.5 + 0.5 * Math.sin(app.warnClock * 3.45575)); |
| 1834 | var t = app.tile; |
| 1835 | for (var c = 0; c < app.columnWarn.length; c++) |
| 1836 | { |
| 1837 | var obj = app.columnWarn[c]; |
| 1838 | var on = app.core.columnNeedsWarn && app.core.columnNeedsWarn(c); |
| 1839 | obj.renderer.visible = !!on; |
| 1840 | obj.renderer.alpha = on ? pulse : 0; |
| 1841 | if (t) |
| 1842 | { |
| 1843 | obj.transform.position.x = c * t + t / 2; |
| 1844 | obj.transform.position.y = app.fieldH / 2; |
| 1845 | obj.transform.size = { x: t, y: app.fieldH }; |
| 1846 | } |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | /** |
| 1851 | * A flight starts only when a block has actually left the field |
| 1852 | * (y ≥ height, the same threshold as cullOffTop). Rockets that |
| 1853 | * peak below the top and fall back must never get an arc (t1137). |
| 1854 | * Shared by the player's field, enemy minis, offline and online. |
| 1855 | */ |
| 1856 | function previewOutgoingFlights () |
| 1857 | { |
| 1858 | // Intentionally not called before core.tick: a block still on |
| 1859 | // the field (y < 13) is not a shot yet. Flights are spawned |
| 1860 | // from consumeOutgoingShots after cull, at the exit pose. |
| 1861 | if (typeof Online === 'undefined' || !Online.previewExit) |
| 1862 | { return; } |
| 1863 | if (app.core) |
| 1864 | { previewCoreExits(app.core, null); } |
| 1865 | if (app.offline && app.battle && app.battle.bots) |
| 1866 | { |
| 1867 | var bots = app.battle.bots; |
| 1868 | for (var i = 0; i < bots.length; i++) |
| 1869 | { |
| 1870 | var bot = bots[i]; |
| 1871 | if (bot && bot.core && !bot.eliminated) |
| 1872 | { previewCoreExits(bot.core, bot.id); } |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | function previewCoreExits (core, fromId) |
| 1878 | { |
| 1879 | if (!core || !core.blocks) |
| 1880 | { return; } |
| 1881 | var height = core.height != null ? core.height : 13; |
| 1882 | var blocks = core.blocks; |
| 1883 | for (var i = 0; i < blocks.length; i++) |
| 1884 | { |
| 1885 | var b = blocks[i]; |
| 1886 | if (b.state === 'manual') |
| 1887 | { continue; } |
| 1888 | if (typeof Online.hasLeftTheField === 'function' |
| 1889 | ? !Online.hasLeftTheField(b.y, height) |
| 1890 | : !(b.y >= height - 1e-9)) |
| 1891 | { continue; } |
| 1892 | if (!(b.vy > 0)) |
| 1893 | { continue; } |
| 1894 | var start = fromId |
| 1895 | ? (Online.miniCellViewport |
| 1896 | ? Online.miniCellViewport(fromId, b.col, b.y) |
| 1897 | : miniViewportCenter(fromId)) |
| 1898 | : fieldToViewport(b.col, b.y); |
| 1899 | Online.previewExit({ |
| 1900 | id: b.id, |
| 1901 | col: b.col, |
| 1902 | y: b.y, |
| 1903 | vy: b.vy, |
| 1904 | color: b.color, |
| 1905 | start: start, |
| 1906 | from_id: fromId || null |
| 1907 | }); |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | function fieldToViewport (col, fieldY) |
| 1912 | { |
| 1913 | var canvas = app.scene && app.scene.canvas; |
| 1914 | if (!canvas) |
| 1915 | { return { x: window.innerWidth / 2, y: 40 }; } |
| 1916 | var rect = canvas.getBoundingClientRect(); |
| 1917 | var x = rect.left + ((col + 0.5) / 10) * rect.width; |
| 1918 | // Cell centre matches placeTile: y = fieldH - (fieldY + 0.5) * tile (t1134). |
| 1919 | var y = rect.top + (1 - ((fieldY + 0.5) / 13)) * rect.height; |
| 1920 | return { x: x, y: y }; |
| 1921 | } |
| 1922 | |
| 1923 | function onlinePayload () |
| 1924 | { |
| 1925 | if (!app.online || !app.core) |
| 1926 | { return {}; } |
| 1927 | var snap = app.core.fieldSnapshot(); |
| 1928 | // t1182: sender time (ms) so opponents can place the snapshot in |
| 1929 | // time and predict falling blocks smoothly. Visual only. |
| 1930 | snap.t = Math.round(typeof performance !== 'undefined' ? performance.now() : Date.now()); |
| 1931 | return { |
| 1932 | snapshot: snap, |
| 1933 | score: app.core.score, |
| 1934 | eliminated: app.core.state === 'gameover' || app.spectating || app.eliminatedReported |
| 1935 | }; |
| 1936 | } |
| 1937 | |
| 1938 | function onOnlineEliminated () |
| 1939 | { |
| 1940 | app.eliminatedReported = true; |
| 1941 | app.spectating = true; |
| 1942 | unbindPointer(); |
| 1943 | hideEl('game-over'); |
| 1944 | if (app.rewardActive) |
| 1945 | { return; } |
| 1946 | var lobby = typeof Online !== 'undefined' && Online.lobby; |
| 1947 | if (lobby && lobby.status === 'finished') |
| 1948 | { |
| 1949 | showPodium(lobby); |
| 1950 | return; |
| 1951 | } |
| 1952 | showEl('spectator'); |
| 1953 | } |
| 1954 | |
| 1955 | function showPodium (lobby) |
| 1956 | { |
| 1957 | hideEl('spectator'); |
| 1958 | hideEl('game-over'); |
| 1959 | hideEl('online-panel'); |
| 1960 | showEl('podium'); |
| 1961 | var list = document.getElementById('podium-list'); |
| 1962 | list.innerHTML = ''; |
| 1963 | var ranks = (lobby && lobby.rankings) || []; |
| 1964 | for (var i = 0; i < ranks.length; i++) |
| 1965 | { |
| 1966 | var r = ranks[i]; |
| 1967 | var li = document.createElement('li'); |
| 1968 | var medal = (typeof I18n !== 'undefined') ? I18n.place(r.place) |
| 1969 | : (r.place === 1 ? '1st' : (r.place === 2 ? '2nd' : (r.place === 3 ? '3rd' : r.place + 'th'))); |
| 1970 | li.textContent = (typeof I18n !== 'undefined') |
| 1971 | ? I18n.t('podium.line', { place: medal, name: r.name, score: r.score }) |
| 1972 | : (medal + ' ' + r.name + ' · ' + r.score + ' points'); |
| 1973 | list.appendChild(li); |
| 1974 | } |
| 1975 | var hint = document.getElementById('podium-hint'); |
| 1976 | var secs = (lobby && lobby.podium_seconds) || 5; |
| 1977 | if (hint) |
| 1978 | { |
| 1979 | hint.textContent = (typeof I18n !== 'undefined') |
| 1980 | ? I18n.t('podium.hint', { n: secs }) |
| 1981 | : ('Returning to the lobby in ' + secs + ' seconds…'); |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | function startOnlineMatch () |
| 1986 | { |
| 1987 | if (app.mode === 'playing' && app.online) |
| 1988 | { return; } |
| 1989 | var planet = Planet.planetById(selectedPlanetId()); |
| 1990 | var miss = Planet.missChance(planet); |
| 1991 | var me = (typeof Online !== 'undefined' && Online.mePlayer) ? Online.mePlayer() : null; |
| 1992 | if (me && me.planet) |
| 1993 | { |
| 1994 | if (typeof Planet.rememberPlanet === 'function') |
| 1995 | { planet = Planet.rememberPlanet(me.planet) || planet; } |
| 1996 | else if (me.planet.id) |
| 1997 | { planet = Planet.planetById(me.planet.id); } |
| 1998 | if (typeof me.planet.miss_chance === 'number') |
| 1999 | { miss = me.planet.miss_chance; } |
| 2000 | } |
| 2001 | app.onlineBefore = (typeof Online !== 'undefined' && Online.lifetimePoints) || 0; |
| 2002 | app.pendingLobbyReturn = null; |
| 2003 | startPlay({ online: true, planet: planet, missChance: miss }); |
| 2004 | if (typeof Online !== 'undefined') |
| 2005 | { Online.startPolling(onlinePayload); } |
| 2006 | } |
| 2007 | |
| 2008 | function leaveOnlineMatch (goMenu) |
| 2009 | { |
| 2010 | if (typeof Online !== 'undefined') |
| 2011 | { |
| 2012 | Online.stopPolling(); |
| 2013 | Online.leaveLobby(); |
| 2014 | Online.clearFlights(); |
| 2015 | } |
| 2016 | teardownPlay(); |
| 2017 | app.online = false; |
| 2018 | if (goMenu) |
| 2019 | { |
| 2020 | app.mode = 'menu'; |
| 2021 | showEl('menu'); |
| 2022 | } |
| 2023 | else |
| 2024 | { |
| 2025 | app.mode = 'online'; |
| 2026 | openOnlinePanel(); |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | function bindOnlineUi () |
| 2031 | { |
| 2032 | var onlineBtn = document.getElementById('btn-online'); |
| 2033 | if (!onlineBtn) |
| 2034 | { return; } |
| 2035 | onlineBtn.addEventListener('click', function () |
| 2036 | { openOnlinePanel(); }); |
| 2037 | |
| 2038 | var backs = document.querySelectorAll('[data-back-menu]'); |
| 2039 | for (var i = 0; i < backs.length; i++) |
| 2040 | { |
| 2041 | backs[i].addEventListener('click', function () |
| 2042 | { |
| 2043 | if (typeof Online !== 'undefined') |
| 2044 | { Online.stopPolling(); } |
| 2045 | hideEl('online-panel'); |
| 2046 | app.mode = 'menu'; |
| 2047 | app.online = false; |
| 2048 | showEl('menu'); |
| 2049 | }); |
| 2050 | } |
| 2051 | |
| 2052 | document.getElementById('btn-create').addEventListener('click', function () |
| 2053 | { |
| 2054 | Online.createLobby().then(function (data) |
| 2055 | { |
| 2056 | if (data && data.ok) |
| 2057 | { |
| 2058 | renderOnline(); |
| 2059 | Online.startPolling(onlinePayload); |
| 2060 | } |
| 2061 | else |
| 2062 | { renderOnline(); } |
| 2063 | }); |
| 2064 | }); |
| 2065 | |
| 2066 | document.getElementById('join-form').addEventListener('submit', function (e) |
| 2067 | { |
| 2068 | e.preventDefault(); |
| 2069 | var code = (document.getElementById('join-code').value || '').trim(); |
| 2070 | if (!code) |
| 2071 | { return; } |
| 2072 | Online.joinLobby(code).then(function () |
| 2073 | { |
| 2074 | renderOnline(); |
| 2075 | if (Online.lobby) |
| 2076 | { Online.startPolling(onlinePayload); } |
| 2077 | }); |
| 2078 | }); |
| 2079 | |
| 2080 | document.getElementById('btn-ready').addEventListener('click', function () |
| 2081 | { |
| 2082 | var me = Online.mePlayer(); |
| 2083 | Online.setReady(!(me && me.ready)).then(renderOnline); |
| 2084 | }); |
| 2085 | |
| 2086 | document.getElementById('btn-start').addEventListener('click', function () |
| 2087 | { |
| 2088 | Online.startGame().then(function (data) |
| 2089 | { |
| 2090 | if (data && data.ok) |
| 2091 | { startOnlineMatch(); } |
| 2092 | else |
| 2093 | { renderOnline(); } |
| 2094 | }); |
| 2095 | }); |
| 2096 | |
| 2097 | document.getElementById('btn-leave-lobby').addEventListener('click', function () |
| 2098 | { |
| 2099 | Online.leaveLobby().then(function () |
| 2100 | { |
| 2101 | Online.stopPolling(); |
| 2102 | renderOnline(); |
| 2103 | }); |
| 2104 | }); |
| 2105 | |
| 2106 | document.getElementById('btn-copy-code').addEventListener('click', function () |
| 2107 | { |
| 2108 | var code = document.getElementById('lobby-code').textContent; |
| 2109 | var btn = document.getElementById('btn-copy-code'); |
| 2110 | var done = function () |
| 2111 | { |
| 2112 | if (!btn) |
| 2113 | { return; } |
| 2114 | btn.textContent = (typeof I18n !== 'undefined') ? I18n.t('online.copied') : 'Copied'; |
| 2115 | setTimeout(function () |
| 2116 | { |
| 2117 | btn.textContent = (typeof I18n !== 'undefined') ? I18n.t('online.copy') : 'Copy'; |
| 2118 | }, 1500); |
| 2119 | }; |
| 2120 | if (navigator.clipboard && navigator.clipboard.writeText) |
| 2121 | { navigator.clipboard.writeText(code).then(done, done); } |
| 2122 | else |
| 2123 | { done(); } |
| 2124 | }); |
| 2125 | |
| 2126 | document.getElementById('btn-spectate').addEventListener('click', function () |
| 2127 | { |
| 2128 | hideEl('spectator'); |
| 2129 | app.spectating = true; |
| 2130 | }); |
| 2131 | |
| 2132 | document.getElementById('btn-leave-game').addEventListener('click', function () |
| 2133 | { leaveOnlineMatch(false); }); |
| 2134 | |
| 2135 | if (typeof Online !== 'undefined') |
| 2136 | { |
| 2137 | Online.onChange = function () |
| 2138 | { renderOnline(); }; |
| 2139 | Online.onGameStart = function () |
| 2140 | { startOnlineMatch(); }; |
| 2141 | Online.onIncoming = function (incoming) |
| 2142 | { |
| 2143 | if (!app.core || app.core.state !== 'playing') |
| 2144 | { return; } |
| 2145 | for (var i = 0; i < incoming.length; i++) |
| 2146 | { |
| 2147 | var col = Online.takeLandCol ? Online.takeLandCol(incoming[i]) : incoming[i].land_col; |
| 2148 | app.core.ingestGarbage(col); |
| 2149 | } |
| 2150 | }; |
| 2151 | Online.onFinished = function (lobby) |
| 2152 | { |
| 2153 | if (app.mode === 'playing' && app.online && rewardEnabled() && !app.rewardActive) |
| 2154 | { |
| 2155 | // Lifetime points in this same poll already include the |
| 2156 | // server's online award (maybeAwardOnlineLobby). |
| 2157 | var result = { before: app.onlineBefore || 0, after: Online.lifetimePoints || 0 }; |
| 2158 | showReward(myOnlinePlace(lobby) === 1, Promise.resolve(result), function () |
| 2159 | { |
| 2160 | var pending = app.pendingLobbyReturn; |
| 2161 | app.pendingLobbyReturn = null; |
| 2162 | if (pending || !(Online.lobby && Online.lobby.status === 'finished')) |
| 2163 | { Online.onLobbyReturn(pending || Online.lobby); } |
| 2164 | else |
| 2165 | { showPodium(Online.lobby); } |
| 2166 | }); |
| 2167 | return; |
| 2168 | } |
| 2169 | showPodium(lobby); |
| 2170 | }; |
| 2171 | Online.onLobbyReturn = function (lobby) |
| 2172 | { |
| 2173 | if (app.rewardActive) |
| 2174 | { |
| 2175 | app.pendingLobbyReturn = lobby; |
| 2176 | return; |
| 2177 | } |
| 2178 | teardownPlay(); |
| 2179 | app.online = false; |
| 2180 | app.mode = 'online'; |
| 2181 | hideEl('podium'); |
| 2182 | showEl('online-panel'); |
| 2183 | renderOnline(); |
| 2184 | Online.startPolling(onlinePayload); |
| 2185 | }; |
| 2186 | } |
| 2187 | |
| 2188 | var params = new URLSearchParams(window.location.search); |
| 2189 | if (params.get('online') === '1' || params.get('auth') === 'unconfigured') |
| 2190 | { |
| 2191 | openOnlinePanel(); |
| 2192 | // t1193: one-shot; a reload must not jump to online play again. |
| 2193 | try |
| 2194 | { |
| 2195 | params.delete('online'); |
| 2196 | var rest = params.toString(); |
| 2197 | window.history.replaceState(null, '', window.location.pathname + (rest ? '?' + rest : '') + window.location.hash); |
| 2198 | } |
| 2199 | catch (e) { /* ignore */ } |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | function openOnlinePanel () |
| 2204 | { |
| 2205 | hideEl('menu'); |
| 2206 | hideEl('game-over'); |
| 2207 | hideEl('podium'); |
| 2208 | hideEl('spectator'); |
| 2209 | hideEl('planet-select'); |
| 2210 | showEl('online-panel'); |
| 2211 | app.mode = 'online'; |
| 2212 | setOnlineStatus((typeof I18n !== 'undefined') ? I18n.t('online.checking') : 'Checking whether online play is available…'); |
| 2213 | if (typeof Online === 'undefined') |
| 2214 | { |
| 2215 | showOnlineError((typeof I18n !== 'undefined') ? I18n.t('online.notLoaded') : 'Online play is not included in this build. You can still play offline.'); |
| 2216 | showOnly('online-unconfigured'); |
| 2217 | return; |
| 2218 | } |
| 2219 | Online.refresh().then(function (data) |
| 2220 | { |
| 2221 | if (!data || data.ok === false) |
| 2222 | { |
| 2223 | var msg = (data && data.error) |
| 2224 | ? ((typeof I18n !== 'undefined') ? I18n.error(data.error) : data.error) |
| 2225 | : ((typeof I18n !== 'undefined') ? I18n.t('error.connection') : 'No connection to the server.'); |
| 2226 | showOnlineError(msg + ' ' + ((typeof I18n !== 'undefined') ? I18n.t('online.canPlaySolo') : 'You can still play offline.')); |
| 2227 | if (data && data.configured === false) |
| 2228 | { showOnly('online-unconfigured'); } |
| 2229 | else |
| 2230 | { showOnly('online-login'); } |
| 2231 | document.getElementById('btn-login').style.display = 'none'; |
| 2232 | return; |
| 2233 | } |
| 2234 | renderOnline(); |
| 2235 | if (Online.lobby) |
| 2236 | { Online.startPolling(onlinePayload); } |
| 2237 | }); |
| 2238 | } |
| 2239 | |
| 2240 | function setOnlineStatus (text) |
| 2241 | { |
| 2242 | var el = document.getElementById('online-status'); |
| 2243 | if (el) |
| 2244 | { el.textContent = text; } |
| 2245 | } |
| 2246 | |
| 2247 | function showOnlineError (text) |
| 2248 | { |
| 2249 | var el = document.getElementById('online-error'); |
| 2250 | if (!el) |
| 2251 | { return; } |
| 2252 | if (!text) |
| 2253 | { |
| 2254 | el.hidden = true; |
| 2255 | el.textContent = ''; |
| 2256 | return; |
| 2257 | } |
| 2258 | el.hidden = false; |
| 2259 | el.textContent = text; |
| 2260 | } |
| 2261 | |
| 2262 | function showOnly (id) |
| 2263 | { |
| 2264 | var ids = ['online-unconfigured', 'online-login', 'online-home', 'online-lobby']; |
| 2265 | for (var i = 0; i < ids.length; i++) |
| 2266 | { document.getElementById(ids[i]).hidden = (ids[i] !== id); } |
| 2267 | } |
| 2268 | |
| 2269 | function renderOnline () |
| 2270 | { |
| 2271 | if (typeof Online === 'undefined') |
| 2272 | { return; } |
| 2273 | if (app.mode === 'playing' && app.online) |
| 2274 | { |
| 2275 | Online.renderMinis(document.getElementById('minis')); |
| 2276 | if (Online.lobby && Online.lobby.status === 'finished' && !app.rewardActive) |
| 2277 | { showPodium(Online.lobby); } |
| 2278 | return; |
| 2279 | } |
| 2280 | showOnlineError((typeof I18n !== 'undefined' && Online.error) ? I18n.error(Online.error) : Online.error); |
| 2281 | if (!Online.configured && !Online.me && !Online.local) |
| 2282 | { |
| 2283 | setOnlineStatus((typeof I18n !== 'undefined') ? I18n.t('online.statusUnconfigured') : 'Online play is not set up yet.'); |
| 2284 | showOnly('online-unconfigured'); |
| 2285 | return; |
| 2286 | } |
| 2287 | if (!Online.me) |
| 2288 | { |
| 2289 | setOnlineStatus((typeof I18n !== 'undefined') ? I18n.t('online.statusLogin') : 'Log in to create a lobby or join one.'); |
| 2290 | showOnly('online-login'); |
| 2291 | document.getElementById('btn-login').style.display = ''; |
| 2292 | document.getElementById('btn-login').href = Online.loginUrl(); |
| 2293 | document.getElementById('local-login').hidden = !Online.local; |
| 2294 | return; |
| 2295 | } |
| 2296 | if (Online.lobby) |
| 2297 | { |
| 2298 | setOnlineStatus((typeof I18n !== 'undefined') ? I18n.t('online.statusLobby') : 'Lobby'); |
| 2299 | showOnly('online-lobby'); |
| 2300 | document.getElementById('lobby-me-name').textContent = Online.me.name; |
| 2301 | document.getElementById('lobby-code').textContent = Online.lobby.code; |
| 2302 | var list = document.getElementById('lobby-players'); |
| 2303 | list.innerHTML = ''; |
| 2304 | var players = Online.lobby.players || []; |
| 2305 | for (var i = 0; i < players.length; i++) |
| 2306 | { |
| 2307 | var p = players[i]; |
| 2308 | var li = document.createElement('li'); |
| 2309 | if (p.is_me) |
| 2310 | { li.classList.add('you'); } |
| 2311 | var host = (typeof I18n !== 'undefined') ? I18n.t('online.host') : 'host'; |
| 2312 | var you = (typeof I18n !== 'undefined') ? I18n.t('online.you') : 'you'; |
| 2313 | var label = p.name + (p.is_host ? ' (' + host + ')' : '') + (p.is_me ? ' (' + you + ')' : ''); |
| 2314 | var state = p.ready ? 'Gereed' : 'Wacht…'; |
| 2315 | var world = (p.planet && p.planet.name) ? (p.planet.name + ' · ' + p.planet.people) : ''; |
| 2316 | li.innerHTML = '<span>' + escapeText(label) |
| 2317 | + (world ? '<span class="planet-tag">' + escapeText(world) + '</span>' : '') |
| 2318 | + '</span><span class="' |
| 2319 | + (p.ready ? 'ready' : 'waiting') + '">' + state + '</span>'; |
| 2320 | list.appendChild(li); |
| 2321 | } |
| 2322 | renderPlanetGrid('lobby-planet-grid', true); |
| 2323 | var me = Online.mePlayer(); |
| 2324 | document.getElementById('btn-ready').textContent = (me && me.ready) |
| 2325 | ? ((typeof I18n !== 'undefined') ? I18n.t('online.notReady') : 'Not ready') |
| 2326 | : ((typeof I18n !== 'undefined') ? I18n.t('online.ready') : 'Ready'); |
| 2327 | var start = document.getElementById('btn-start'); |
| 2328 | start.hidden = !(Online.lobby && Online.me && Online.lobby.host_id === Online.me.id); |
| 2329 | start.disabled = !Online.lobby.can_start; |
| 2330 | var hint = document.getElementById('lobby-hint'); |
| 2331 | if (Online.lobby.status === 'playing') |
| 2332 | { hint.textContent = (typeof I18n !== 'undefined') ? I18n.t('online.hintStarting') : 'The game is starting…'; } |
| 2333 | else if (players.length < 2) |
| 2334 | { hint.textContent = (typeof I18n !== 'undefined') ? I18n.t('online.hintNeedPlayers') : 'Wait for at least one other player. Share the code.'; } |
| 2335 | else if (!Online.lobby.all_ready) |
| 2336 | { hint.textContent = (typeof I18n !== 'undefined') ? I18n.t('online.hintNeedReady') : 'Everyone must press Ready. The host starts afterwards.'; } |
| 2337 | else if (Online.lobby.can_start) |
| 2338 | { hint.textContent = (typeof I18n !== 'undefined') ? I18n.t('online.hintHostStart') : 'Everyone is ready. Press Start.'; } |
| 2339 | else |
| 2340 | { hint.textContent = (typeof I18n !== 'undefined') ? I18n.t('online.hintWaitHost') : 'Waiting for the host to start.'; } |
| 2341 | return; |
| 2342 | } |
| 2343 | setOnlineStatus((typeof I18n !== 'undefined') ? I18n.t('online.statusHome', { name: Online.me.name }) : ('Hi ' + Online.me.name + '. Create a lobby or join with a code.')); |
| 2344 | showOnly('online-home'); |
| 2345 | document.getElementById('me-name').textContent = Online.me.name; |
| 2346 | document.getElementById('local-login').hidden = !Online.local; |
| 2347 | } |
| 2348 | |
| 2349 | function escapeText (s) |
| 2350 | { |
| 2351 | return String(s) |
| 2352 | .replace(/&/g, '&') |
| 2353 | .replace(/</g, '<') |
| 2354 | .replace(/>/g, '>') |
| 2355 | .replace(/"/g, '"'); |
| 2356 | } |
| 2357 | |
| 2358 | |
| 2359 | function selectedPlanetId () |
| 2360 | { |
| 2361 | if (app.selectedPlanetId) |
| 2362 | { return app.selectedPlanetId; } |
| 2363 | return 'terra'; |
| 2364 | } |
| 2365 | |
| 2366 | /** |
| 2367 | * Pre-select the last chosen planet (t1196): logged in the profile value |
| 2368 | * (server), otherwise / as fallback localStorage, otherwise Terra. A |
| 2369 | * deleted custom planet falls back to the default (setSelectedPlanet). |
| 2370 | */ |
| 2371 | function restoreSelectedPlanet () |
| 2372 | { |
| 2373 | var id = null; |
| 2374 | if (typeof Online !== 'undefined' && Online.me && Online.lastPlanet) |
| 2375 | { id = Online.lastPlanet; } |
| 2376 | if (!id) |
| 2377 | { |
| 2378 | try |
| 2379 | { id = localStorage.getItem('meteors-planet'); } |
| 2380 | catch (e) {} |
| 2381 | } |
| 2382 | setSelectedPlanet(id || 'terra', true); |
| 2383 | } |
| 2384 | |
| 2385 | function rememberPlanetChoice (id) |
| 2386 | { |
| 2387 | if (typeof Online === 'undefined' || !Online.me || !Online.setLastPlanet) |
| 2388 | { return; } |
| 2389 | if (Online.lastPlanet === id) |
| 2390 | { return; } |
| 2391 | Promise.resolve(Online.setLastPlanet(id)).then(null, function () {}); |
| 2392 | } |
| 2393 | |
| 2394 | function setSelectedPlanet (id, silent) |
| 2395 | { |
| 2396 | var all = Planet.allPlanets(); |
| 2397 | var found = null; |
| 2398 | for (var i = 0; i < all.length; i++) |
| 2399 | { |
| 2400 | if (all[i].id === id) |
| 2401 | { found = all[i]; break; } |
| 2402 | } |
| 2403 | if (!found) |
| 2404 | { found = Planet.defaultPlanet(); } |
| 2405 | app.selectedPlanetId = found.id; |
| 2406 | try |
| 2407 | { localStorage.setItem('meteors-planet', found.id); } |
| 2408 | catch (e) {} |
| 2409 | if (!silent) |
| 2410 | { |
| 2411 | rememberPlanetChoice(found.id); |
| 2412 | var grid = document.getElementById('planet-grid'); |
| 2413 | if (grid && !grid.hidden && grid.children.length) |
| 2414 | { renderPlanetGrid('planet-grid', false); } |
| 2415 | var lobby = document.getElementById('lobby-planet-grid'); |
| 2416 | if (lobby && lobby.children.length) |
| 2417 | { renderPlanetGrid('lobby-planet-grid', true); } |
| 2418 | } |
| 2419 | return found; |
| 2420 | } |
| 2421 | |
| 2422 | function openPlanetSelect (context) |
| 2423 | { |
| 2424 | if (app.mode === 'playing') |
| 2425 | { return; } |
| 2426 | app.planetSelectContext = context || 'solo'; |
| 2427 | app.mode = 'planet-select'; |
| 2428 | hideEl('menu'); |
| 2429 | hideEl('online-panel'); |
| 2430 | hideEl('game-over'); |
| 2431 | showEl('planet-select'); |
| 2432 | renderPlanetGrid('planet-grid', false); |
| 2433 | var go = document.getElementById('btn-planet-go'); |
| 2434 | if (go) |
| 2435 | { go.textContent = (typeof I18n !== 'undefined') ? I18n.t('menu.play') : 'Play'; } |
| 2436 | // t1201: show which mode this planet choice is for. |
| 2437 | var modeEl = document.getElementById('planet-mode'); |
| 2438 | if (modeEl) |
| 2439 | { modeEl.textContent = modeLabel(app.planetSelectContext); } |
| 2440 | } |
| 2441 | |
| 2442 | function renderPlanetGrid (containerId, compact) |
| 2443 | { |
| 2444 | var grid = document.getElementById(containerId); |
| 2445 | if (!grid) |
| 2446 | { return; } |
| 2447 | var current = selectedPlanetId(); |
| 2448 | var all = Planet.allPlanets(); |
| 2449 | grid.innerHTML = ''; |
| 2450 | if (compact) |
| 2451 | { grid.classList.add('compact'); } |
| 2452 | for (var i = 0; i < all.length; i++) |
| 2453 | { |
| 2454 | var pl = all[i]; |
| 2455 | var btn = document.createElement('button'); |
| 2456 | btn.type = 'button'; |
| 2457 | btn.className = 'planet-card' + (pl.id === current ? ' selected' : '') |
| 2458 | + (pl.custom ? ' custom' : ''); |
| 2459 | btn.setAttribute('role', 'listitem'); |
| 2460 | btn.dataset.planet = pl.id; |
| 2461 | var swatches = ''; |
| 2462 | var colors = pl.colors || []; |
| 2463 | for (var c = 0; c < colors.length; c++) |
| 2464 | { |
| 2465 | var cname = colors[c].name; |
| 2466 | if (typeof I18n !== 'undefined' && (colors[c].block || (!pl.custom && colors[c].name))) |
| 2467 | { cname = I18n.blockName(colors[c].block || (pl.id + '-' + colors[c].name), colors[c].name); } |
| 2468 | swatches += '<img src="' + escapeText(colors[c].image) + '" alt="' |
| 2469 | + escapeText(cname) + '" title="' + escapeText(cname) + '">'; |
| 2470 | } |
| 2471 | var title = (typeof I18n !== 'undefined') ? I18n.planetName(pl) : pl.name; |
| 2472 | var people = (typeof I18n !== 'undefined') ? I18n.peopleName(pl) : pl.people; |
| 2473 | var badge = (typeof I18n !== 'undefined') ? I18n.t('planet.customBadge') : 'Yours'; |
| 2474 | btn.innerHTML = '<div class="planet-title">' + escapeText(title) + '</div>' |
| 2475 | + '<div class="planet-people">' + escapeText(people) + '</div>' |
| 2476 | + '<div class="planet-nums">' + escapeText(Planet.formatStats(pl)) + '</div>' |
| 2477 | + '<div class="planet-swatches">' + swatches + '</div>' |
| 2478 | + (pl.custom ? '<span class="custom-badge">' + escapeText(badge) + '</span>' : ''); |
| 2479 | btn.addEventListener('click', (function (id, isCompact) |
| 2480 | { |
| 2481 | return function () |
| 2482 | { onPlanetPicked(id, isCompact); }; |
| 2483 | })(pl.id, !!compact)); |
| 2484 | grid.appendChild(btn); |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | function onPlanetPicked (id, fromLobby) |
| 2489 | { |
| 2490 | setSelectedPlanet(id); |
| 2491 | if (fromLobby && typeof Online !== 'undefined' && Online.lobby) |
| 2492 | { |
| 2493 | Online.setPlanet(id).then(function () |
| 2494 | { renderOnline(); }); |
| 2495 | } |
| 2496 | else |
| 2497 | { |
| 2498 | renderPlanetGrid('planet-grid', false); |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | function confirmPlanetSelect () |
| 2503 | { |
| 2504 | var planet = setSelectedPlanet(selectedPlanetId()); |
| 2505 | hideEl('planet-select'); |
| 2506 | if (app.planetSelectContext === 'online') |
| 2507 | { |
| 2508 | openOnlinePanel(); |
| 2509 | return; |
| 2510 | } |
| 2511 | if (app.planetSelectContext === 'offline') |
| 2512 | { |
| 2513 | startPlay({ planet: planet, offline: true }); |
| 2514 | return; |
| 2515 | } |
| 2516 | startPlay({ planet: planet }); |
| 2517 | } |
| 2518 | |
| 2519 | function cancelPlanetSelect () |
| 2520 | { |
| 2521 | hideEl('planet-select'); |
| 2522 | app.mode = 'menu'; |
| 2523 | showEl('menu'); |
| 2524 | } |
| 2525 | |
| 2526 | function bindPlanetSelect () |
| 2527 | { |
| 2528 | var go = document.getElementById('btn-planet-go'); |
| 2529 | var back = document.getElementById('btn-planet-back'); |
| 2530 | if (go) |
| 2531 | { |
| 2532 | go.addEventListener('click', confirmPlanetSelect); |
| 2533 | } |
| 2534 | if (back) |
| 2535 | { |
| 2536 | back.addEventListener('click', cancelPlanetSelect); |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | |
| 2541 | /** t1246: ask a logged-out player to log in, or continue as guest. */ |
| 2542 | function bindLoginPrompt () |
| 2543 | { |
| 2544 | if (typeof LoginPrompt !== 'undefined' && LoginPrompt.isAuthReturn) |
| 2545 | { |
| 2546 | var search = (window.location && window.location.search) || ''; |
| 2547 | var referrer = (typeof document !== 'undefined' && document.referrer) || ''; |
| 2548 | if (LoginPrompt.isAuthReturn(search, referrer)) |
| 2549 | { LoginPrompt.remember(); } |
| 2550 | } |
| 2551 | var guest = document.getElementById('btn-play-guest'); |
| 2552 | if (guest) |
| 2553 | { guest.addEventListener('click', playAsGuest); } |
| 2554 | var login = document.getElementById('btn-login-prompt'); |
| 2555 | if (login) |
| 2556 | { |
| 2557 | login.addEventListener('click', function () |
| 2558 | { |
| 2559 | if (typeof LoginPrompt !== 'undefined') |
| 2560 | { LoginPrompt.remember(); } |
| 2561 | }); |
| 2562 | } |
| 2563 | var local = document.getElementById('login-prompt-local'); |
| 2564 | if (local) |
| 2565 | { |
| 2566 | local.addEventListener('click', function (e) |
| 2567 | { |
| 2568 | var a = e.target && e.target.closest ? e.target.closest('a') : null; |
| 2569 | if (a && typeof LoginPrompt !== 'undefined') |
| 2570 | { LoginPrompt.remember(); } |
| 2571 | }); |
| 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | function maybeShowLoginPrompt () |
| 2576 | { |
| 2577 | var me = (typeof Online !== 'undefined') ? Online.me : null; |
| 2578 | var remembered = (typeof LoginPrompt !== 'undefined') && LoginPrompt.remembered(); |
| 2579 | var fromAuth = (typeof LoginPrompt !== 'undefined') && LoginPrompt.isAuthReturn( |
| 2580 | (window.location && window.location.search) || '', |
| 2581 | (typeof document !== 'undefined' && document.referrer) || '' |
| 2582 | ); |
| 2583 | var show = (typeof LoginPrompt === 'undefined') |
| 2584 | ? (!me && !remembered && !fromAuth) |
| 2585 | : LoginPrompt.shouldShow({ me: me, remembered: remembered, fromAuthReturn: fromAuth }); |
| 2586 | if (show) |
| 2587 | { showLoginPrompt(); } |
| 2588 | else |
| 2589 | { hideLoginPrompt(); } |
| 2590 | app.loginPromptReady = true; |
| 2591 | } |
| 2592 | |
| 2593 | function showLoginPrompt () |
| 2594 | { |
| 2595 | var el = document.getElementById('login-prompt'); |
| 2596 | if (!el) |
| 2597 | { return; } |
| 2598 | el.hidden = false; |
| 2599 | var login = document.getElementById('btn-login-prompt'); |
| 2600 | if (login && login.focus) |
| 2601 | { login.focus(); } |
| 2602 | } |
| 2603 | |
| 2604 | function hideLoginPrompt () |
| 2605 | { |
| 2606 | var el = document.getElementById('login-prompt'); |
| 2607 | if (el) |
| 2608 | { el.hidden = true; } |
| 2609 | } |
| 2610 | |
| 2611 | function playAsGuest () |
| 2612 | { |
| 2613 | if (typeof LoginPrompt !== 'undefined') |
| 2614 | { LoginPrompt.remember(); } |
| 2615 | hideLoginPrompt(); |
| 2616 | } |
| 2617 | |
| 2618 | function refreshMenuAccount () |
| 2619 | { |
| 2620 | var guest = document.getElementById('menu-guest'); |
| 2621 | var user = document.getElementById('menu-user'); |
| 2622 | var designerBtn = document.getElementById('btn-designer'); |
| 2623 | var localLogin = document.getElementById('menu-local-login'); |
| 2624 | var me = (typeof Online !== 'undefined') ? Online.me : null; |
| 2625 | if (me) |
| 2626 | { |
| 2627 | if (guest) |
| 2628 | { guest.hidden = true; } |
| 2629 | if (user) |
| 2630 | { user.hidden = false; } |
| 2631 | var name = document.getElementById('menu-name'); |
| 2632 | if (name) |
| 2633 | { name.textContent = me.name || me.id || ((typeof I18n !== 'undefined') ? I18n.t('menu.player') : 'player'); } |
| 2634 | var pts = document.getElementById('menu-points'); |
| 2635 | if (pts) |
| 2636 | { pts.textContent = String((typeof Online !== 'undefined' && Online.lifetimePoints) || 0); } |
| 2637 | if (designerBtn) |
| 2638 | { designerBtn.hidden = false; } |
| 2639 | } |
| 2640 | else |
| 2641 | { |
| 2642 | if (guest) |
| 2643 | { guest.hidden = false; } |
| 2644 | if (user) |
| 2645 | { user.hidden = true; } |
| 2646 | if (designerBtn) |
| 2647 | { designerBtn.hidden = true; } |
| 2648 | } |
| 2649 | if (localLogin) |
| 2650 | { localLogin.hidden = !(typeof Online !== 'undefined' && Online.local); } |
| 2651 | var promptLocal = document.getElementById('login-prompt-local'); |
| 2652 | if (promptLocal) |
| 2653 | { promptLocal.hidden = !(typeof Online !== 'undefined' && Online.local); } |
| 2654 | } |
| 2655 | |
| 2656 | /* ---------- t1193 in-game menu (Spelen / Offline Strijd) ----------- */ |
| 2657 | |
| 2658 | function setGameMenuButton (show) |
| 2659 | { |
| 2660 | var btn = document.getElementById('btn-game-menu'); |
| 2661 | if (btn) |
| 2662 | { btn.hidden = !show; } |
| 2663 | } |
| 2664 | |
| 2665 | /** The menu can open during a running solo / offline field only. */ |
| 2666 | function canOpenGameMenu () |
| 2667 | { |
| 2668 | return app.mode === 'playing' && !!app.core && !app.online && !app.spectating |
| 2669 | && app.core.state === 'playing' && !app.rewardActive && !app.quitting |
| 2670 | && !(app.offline && app.battle && app.battle.state !== 'playing'); |
| 2671 | } |
| 2672 | |
| 2673 | function openGameMenu () |
| 2674 | { |
| 2675 | if (!canOpenGameMenu()) |
| 2676 | { return false; } |
| 2677 | if (app.core.drag) |
| 2678 | { finishDrag({ noLaunch: true }); } |
| 2679 | app.paused = true; |
| 2680 | // t1201: name the running mode ("Play" = rounds vs bots, "Survival" = endless). |
| 2681 | var modeEl = document.getElementById('game-menu-mode'); |
| 2682 | if (modeEl) |
| 2683 | { modeEl.textContent = modeLabel(app.offline ? 'offline' : 'solo'); } |
| 2684 | showEl('game-menu'); |
| 2685 | var resume = document.getElementById('btn-game-resume'); |
| 2686 | if (resume && resume.focus) |
| 2687 | { resume.focus(); } |
| 2688 | return true; |
| 2689 | } |
| 2690 | |
| 2691 | function resumeFromGameMenu () |
| 2692 | { |
| 2693 | hideEl('game-menu'); |
| 2694 | if (!app.quitting) |
| 2695 | { app.paused = false; } |
| 2696 | } |
| 2697 | |
| 2698 | /** |
| 2699 | * "Back to main menu": ends the match now. Logged in, the points so far |
| 2700 | * go through the normal play_finish + reward screen first (the server |
| 2701 | * still applies its caps); logged out, straight to the menu. Offline |
| 2702 | * Strijd ends the whole battle (no next round). |
| 2703 | */ |
| 2704 | function quitToMainMenu () |
| 2705 | { |
| 2706 | if (!app.core || app.quitting) |
| 2707 | { return; } |
| 2708 | hideEl('game-menu'); |
| 2709 | app.paused = true; |
| 2710 | app.quitting = true; |
| 2711 | app.gameOverShown = true; |
| 2712 | app.lastScore = app.core.score || 0; |
| 2713 | setGameMenuButton(false); |
| 2714 | var score = app.lastScore; |
| 2715 | var done = function () |
| 2716 | { |
| 2717 | app.quitting = false; |
| 2718 | app.paused = false; |
| 2719 | returnToMenu(); |
| 2720 | }; |
| 2721 | if (rewardEnabled()) |
| 2722 | { |
| 2723 | var title = (typeof I18n !== 'undefined') ? I18n.t('reward.ended') : 'Game ended'; |
| 2724 | showReward(false, finishPlaySession(score), done, title); |
| 2725 | } |
| 2726 | else |
| 2727 | { |
| 2728 | finishPlaySession(score); |
| 2729 | done(); |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | function bindGameMenu () |
| 2734 | { |
| 2735 | var btn = document.getElementById('btn-game-menu'); |
| 2736 | if (btn) |
| 2737 | { btn.addEventListener('click', function () { openGameMenu(); }); } |
| 2738 | var resume = document.getElementById('btn-game-resume'); |
| 2739 | if (resume) |
| 2740 | { resume.addEventListener('click', resumeFromGameMenu); } |
| 2741 | var quit = document.getElementById('btn-game-quit'); |
| 2742 | if (quit) |
| 2743 | { quit.addEventListener('click', quitToMainMenu); } |
| 2744 | document.addEventListener('keydown', function (e) |
| 2745 | { |
| 2746 | if (e.key !== 'Escape') |
| 2747 | { return; } |
| 2748 | var menu = document.getElementById('game-menu'); |
| 2749 | if (menu && !menu.hidden) |
| 2750 | { resumeFromGameMenu(); } |
| 2751 | else |
| 2752 | { openGameMenu(); } |
| 2753 | }); |
| 2754 | } |
| 2755 | |
| 2756 | function beginPlaySession (mode) |
| 2757 | { |
| 2758 | if (app.playToken) |
| 2759 | { finishPlaySession(app.core ? app.core.score : (app.lastScore || 0)); } |
| 2760 | app.playToken = null; |
| 2761 | if (typeof Online === 'undefined' || !Online.me || !Online.playBegin) |
| 2762 | { return; } |
| 2763 | Online.playBegin(mode || 'solo').then(function (data) |
| 2764 | { |
| 2765 | if (data && data.ok && data.token) |
| 2766 | { app.playToken = data.token; } |
| 2767 | }); |
| 2768 | } |
| 2769 | |
| 2770 | /** |
| 2771 | * Ends the solo/offline play session. Resolves to the server-confirmed |
| 2772 | * { before, after } profile totals (t1173), or null when there is no |
| 2773 | * logged-in user or the server did not confirm. Without a token (e.g. |
| 2774 | * play_begin failed) a logged-in player gets { before = after = current }. |
| 2775 | */ |
| 2776 | function finishPlaySession (score) |
| 2777 | { |
| 2778 | var token = app.playToken; |
| 2779 | app.playToken = null; |
| 2780 | var hasOnline = typeof Online !== 'undefined'; |
| 2781 | var current = hasOnline ? (Online.lifetimePoints || 0) : 0; |
| 2782 | if (!token || !hasOnline || !Online.playFinish) |
| 2783 | { |
| 2784 | return Promise.resolve(hasOnline && Online.me ? { before: current, after: current } : null); |
| 2785 | } |
| 2786 | return Promise.resolve(Online.playFinish(token, score || 0)).then(function (data) |
| 2787 | { |
| 2788 | refreshMenuAccount(); |
| 2789 | if (data && data.ok !== false && typeof data.lifetime_points === 'number') |
| 2790 | { |
| 2791 | var awarded = Math.max(0, Number(data.awarded) || 0); |
| 2792 | return { before: data.lifetime_points - awarded, after: data.lifetime_points }; |
| 2793 | } |
| 2794 | return null; |
| 2795 | }, function () |
| 2796 | { return null; }); |
| 2797 | } |
| 2798 | |
| 2799 | /* ---------- t1173 reward screen (logged in only) ------------------- */ |
| 2800 | |
| 2801 | function rewardEnabled () |
| 2802 | { |
| 2803 | return typeof Reward !== 'undefined' && typeof Online !== 'undefined' && !!Online.me; |
| 2804 | } |
| 2805 | |
| 2806 | /** |
| 2807 | * Shows "You won!" / "You lost..." and the points fly-in, then calls |
| 2808 | * next() — the same destination the mode used before (menu, next |
| 2809 | * offline round, offline result, or the online lobby/podium). |
| 2810 | */ |
| 2811 | function showReward (won, resultPromise, next, title) |
| 2812 | { |
| 2813 | app.rewardActive = true; |
| 2814 | hideEl('game-over'); |
| 2815 | hideEl('spectator'); |
| 2816 | hideEl('podium'); |
| 2817 | var handle = Reward.play({ |
| 2818 | won: won, |
| 2819 | title: title || null, |
| 2820 | planet: app.planet, |
| 2821 | result: resultPromise, |
| 2822 | onDone: function () |
| 2823 | { |
| 2824 | app.rewardActive = false; |
| 2825 | if (next) |
| 2826 | { next(); } |
| 2827 | } |
| 2828 | }); |
| 2829 | if (!handle) |
| 2830 | { |
| 2831 | app.rewardActive = false; |
| 2832 | // No reward overlay (e.g. missing DOM): still continue (t1193). |
| 2833 | if (title && next) |
| 2834 | { next(); } |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | function myOnlinePlace (lobby) |
| 2839 | { |
| 2840 | var me = typeof Online !== 'undefined' ? Online.me : null; |
| 2841 | var ranks = (lobby && lobby.rankings) || []; |
| 2842 | for (var i = 0; i < ranks.length; i++) |
| 2843 | { |
| 2844 | if (me && ranks[i].id === me.id) |
| 2845 | { return ranks[i].place; } |
| 2846 | } |
| 2847 | return null; |
| 2848 | } |
| 2849 | |
| 2850 | |
| 2851 | |
| 2852 | function applyProfileLocale () |
| 2853 | { |
| 2854 | if (typeof I18n === 'undefined' || typeof Online === 'undefined') |
| 2855 | { return; } |
| 2856 | if (Online.locale) |
| 2857 | { I18n.applyFromProfile(Online.locale); } |
| 2858 | else if (Online.me) |
| 2859 | { Online.setLocale(I18n.locale); } |
| 2860 | } |
| 2861 | |
| 2862 | function refreshAllUi () |
| 2863 | { |
| 2864 | if (typeof I18n !== 'undefined') |
| 2865 | { I18n.apply(); } |
| 2866 | refreshMenuAccount(); |
| 2867 | if (typeof renderPlanetGrid === 'function') |
| 2868 | { |
| 2869 | renderPlanetGrid('planet-grid', false); |
| 2870 | renderPlanetGrid('lobby-planet-grid', true); |
| 2871 | } |
| 2872 | if (app && app.mode === 'online' && typeof renderOnline === 'function') |
| 2873 | { renderOnline(); } |
| 2874 | if (typeof Designer !== 'undefined') |
| 2875 | { |
| 2876 | var des = document.getElementById('designer'); |
| 2877 | var edit = document.getElementById('designer-edit'); |
| 2878 | if (des && !des.hidden) |
| 2879 | { Designer.renderList(); } |
| 2880 | if (edit && !edit.hidden) |
| 2881 | { |
| 2882 | Designer.syncPreview(); |
| 2883 | Designer.renderSlots(); |
| 2884 | var modal = document.getElementById('des-modal'); |
| 2885 | if (modal && !modal.hidden) |
| 2886 | { Designer.openModal(Designer.openSlot); } |
| 2887 | } |
| 2888 | } |
| 2889 | if (app && app.mode === 'playing') |
| 2890 | { |
| 2891 | if (typeof refreshHud === 'function') |
| 2892 | { refreshHud(); } |
| 2893 | if (typeof refreshOfflineHud === 'function') |
| 2894 | { refreshOfflineHud(); } |
| 2895 | } |
| 2896 | } |
projects-timfalken-com meteors