| 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. |
| 8 | */ |
| 9 | |
| 10 | function planetImages () |
| 11 | { |
| 12 | var list = [ |
| 13 | { name: 'gray', url: 'images/block-gray.png' }, |
| 14 | { name: 'locked', url: 'images/block-locked.png' }, |
| 15 | { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, |
| 16 | { name: 'cell', url: 'images/cell.png' }, |
| 17 | { name: 'pixel', url: 'images/pixel.png' }, |
| 18 | // Keep the original Terra placeholders as aliases (Tim may still have them). |
| 19 | { name: 'ember', url: 'images/block-ember.png' }, |
| 20 | { name: 'aqua', url: 'images/block-aqua.png' }, |
| 21 | { name: 'leaf', url: 'images/block-leaf.png' }, |
| 22 | { name: 'sun', url: 'images/block-sun.png' }, |
| 23 | { name: 'orchid', url: 'images/block-orchid.png' } |
| 24 | ]; |
| 25 | var planets = (typeof Planet !== 'undefined' ? Planet.allPlanets() : []); |
| 26 | for (var i = 0; i < planets.length; i++) |
| 27 | { |
| 28 | var pl = planets[i]; |
| 29 | for (var c = 0; c < pl.colors.length; c++) |
| 30 | { |
| 31 | list.push({ |
| 32 | name: Planet.spriteKey(pl, c), |
| 33 | url: pl.colors[c].image |
| 34 | }); |
| 35 | } |
| 36 | } |
| 37 | return list; |
| 38 | } |
| 39 | |
| 40 | var IMAGES = planetImages(); |
| 41 | var GAME_OVER_DELAY = 2; |
| 42 | |
| 43 | var game = new Game(); |
| 44 | var app = { |
| 45 | mode: 'menu', |
| 46 | core: null, |
| 47 | scene: null, |
| 48 | manager: null, |
| 49 | planet: null, |
| 50 | lastScore: 0, |
| 51 | tile: 48, |
| 52 | fieldX: 0, |
| 53 | fieldY: 0, |
| 54 | fieldW: 0, |
| 55 | fieldH: 0, |
| 56 | hudH: 56, |
| 57 | sprites: Object.create(null), |
| 58 | flames: Object.create(null), |
| 59 | cells: [], |
| 60 | pointerId: null, |
| 61 | pointerType: null, |
| 62 | activeTouchId: null, |
| 63 | debugDrag: false, |
| 64 | gameOverShown: false, |
| 65 | selectTween: null, |
| 66 | online: false, |
| 67 | eliminatedReported: false, |
| 68 | spectating: false, |
| 69 | miniH: 0, |
| 70 | selectedPlanetId: 'terra', |
| 71 | planetSelectContext: 'solo' |
| 72 | }; |
| 73 | |
| 74 | window.onload = function () |
| 75 | { |
| 76 | app.debugDrag = isDragDebugEnabled(); |
| 77 | var play = document.getElementById('btn-play'); |
| 78 | play.addEventListener('click', onPlay); |
| 79 | play.addEventListener('pointerup', function (e) |
| 80 | { |
| 81 | // A touch-friendly path: some mobile browsers swallow the click |
| 82 | // after a slightly sloppy tap. Ignore the following click. |
| 83 | if (e.pointerType === 'touch' || e.pointerType === 'pen') |
| 84 | { |
| 85 | e.preventDefault(); |
| 86 | onPlay(); |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | game.preloadImagesThenStart(IMAGES, function () |
| 91 | { |
| 92 | if (game.images.flame && game.images.flame.addAnimationCycle) |
| 93 | { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } |
| 94 | }); |
| 95 | |
| 96 | bindOnlineUi(); |
| 97 | bindPlanetSelect(); |
| 98 | restoreSelectedPlanet(); |
| 99 | }; |
| 100 | |
| 101 | function onPlay () |
| 102 | { |
| 103 | if (app.mode === 'playing') |
| 104 | { return; } |
| 105 | openPlanetSelect('solo'); |
| 106 | } |
| 107 | |
| 108 | function startPlay (opts) |
| 109 | { |
| 110 | opts = opts || {}; |
| 111 | hideEl('menu'); |
| 112 | hideEl('game-over'); |
| 113 | hideEl('online-panel'); |
| 114 | hideEl('spectator'); |
| 115 | hideEl('podium'); |
| 116 | hideEl('planet-select'); |
| 117 | showEl('hud'); |
| 118 | |
| 119 | var planetMod = typeof Planet !== 'undefined' ? Planet : PlanetMod; |
| 120 | app.planet = opts.planet || planetMod.planetById(selectedPlanetId()); |
| 121 | var miss = opts.missChance; |
| 122 | if (typeof miss !== 'number' || !isFinite(miss)) |
| 123 | { miss = planetMod.missChance(app.planet); } |
| 124 | app.core = new GameCore(app.planet, Math.random, miss); |
| 125 | app.mode = 'playing'; |
| 126 | app.online = !!opts.online; |
| 127 | app.gameOverShown = false; |
| 128 | app.eliminatedReported = false; |
| 129 | app.spectating = false; |
| 130 | app.sprites = Object.create(null); |
| 131 | app.flames = Object.create(null); |
| 132 | app.cells = []; |
| 133 | app.pointerId = null; |
| 134 | app.selectTween = null; |
| 135 | |
| 136 | layout(); |
| 137 | if (app.scene) |
| 138 | { |
| 139 | app.scene.destroy(); |
| 140 | app.scene = null; |
| 141 | } |
| 142 | app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); |
| 143 | app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; |
| 144 | app.scene.renderBackground = true; |
| 145 | game.loadScene(app.scene); |
| 146 | |
| 147 | buildCells(); |
| 148 | app.manager = app.scene.addObject(makeManager()); |
| 149 | if (!app.spectating) |
| 150 | { bindPointer(app.scene.canvas); } |
| 151 | refreshHud(); |
| 152 | if (app.online) |
| 153 | { showEl('minis'); } |
| 154 | else |
| 155 | { hideEl('minis'); } |
| 156 | } |
| 157 | |
| 158 | function teardownPlay () |
| 159 | { |
| 160 | unbindPointer(); |
| 161 | if (app.scene) |
| 162 | { |
| 163 | app.scene.destroy(); |
| 164 | app.scene = null; |
| 165 | } |
| 166 | app.manager = null; |
| 167 | app.core = null; |
| 168 | app.sprites = Object.create(null); |
| 169 | app.flames = Object.create(null); |
| 170 | app.cells = []; |
| 171 | app.selectTween = null; |
| 172 | app.pointerId = null; |
| 173 | app.gameOverShown = false; |
| 174 | hideEl('hud'); |
| 175 | hideEl('game-over'); |
| 176 | hideEl('spectator'); |
| 177 | hideEl('minis'); |
| 178 | if (typeof Online !== 'undefined') |
| 179 | { Online.clearFlights(); } |
| 180 | } |
| 181 | |
| 182 | function returnToMenu () |
| 183 | { |
| 184 | if (app.online && typeof Online !== 'undefined') |
| 185 | { |
| 186 | // Online matches do not dump you back to the solo menu: |
| 187 | // elimination becomes spectating; a finished match shows the podium. |
| 188 | return; |
| 189 | } |
| 190 | teardownPlay(); |
| 191 | app.mode = 'menu'; |
| 192 | app.online = false; |
| 193 | hideEl('planet-select'); |
| 194 | showEl('menu'); |
| 195 | var last = document.getElementById('last-score'); |
| 196 | if (app.lastScore > 0) |
| 197 | { |
| 198 | last.hidden = false; |
| 199 | last.querySelector('strong').textContent = String(app.lastScore); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | function buildCells () |
| 204 | { |
| 205 | if (!app.scene) |
| 206 | { return; } |
| 207 | var img = game.images.cell; |
| 208 | for (var row = 0; row < 13; row++) |
| 209 | { |
| 210 | for (var col = 0; col < 10; col++) |
| 211 | { |
| 212 | var obj = new GameObject(0, 0, img, 0); |
| 213 | placeTile(obj, col, row); |
| 214 | obj.renderer.alpha = 0.9; |
| 215 | app.scene.addObject(obj); |
| 216 | app.cells.push(obj); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | function placeTile (obj, col, y) |
| 222 | { |
| 223 | var t = app.tile; |
| 224 | obj.transform.position.x = col * t + t / 2; |
| 225 | obj.transform.position.y = app.fieldH - (y + 0.5) * t; |
| 226 | obj.transform.size = { x: t, y: t }; |
| 227 | } |
| 228 | |
| 229 | function layoutIfNeeded () |
| 230 | { |
| 231 | if (app.core && app.core.drag) |
| 232 | { return; } |
| 233 | var vw = window.innerWidth || 320; |
| 234 | var vh = window.innerHeight || 480; |
| 235 | if (app._layoutVw === vw && app._layoutVh === vh && app.scene && app.tile) |
| 236 | { return; } |
| 237 | layout(); |
| 238 | } |
| 239 | |
| 240 | function layout () |
| 241 | { |
| 242 | if (app.core && app.core.drag) |
| 243 | { return; } |
| 244 | var vw = window.innerWidth || 320; |
| 245 | var vh = window.innerHeight || 480; |
| 246 | app._layoutVw = vw; |
| 247 | app._layoutVh = vh; |
| 248 | var safeTop = 0; |
| 249 | try |
| 250 | { |
| 251 | // env(safe-area) is a CSS thing; keep a conservative HUD height. |
| 252 | safeTop = 0; |
| 253 | } |
| 254 | catch (e) { /* ignore */ } |
| 255 | app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); |
| 256 | app.miniH = 0; |
| 257 | if (app.online) |
| 258 | { app.miniH = Math.min(150, Math.max(86, Math.round(vh * 0.20))); } |
| 259 | var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); |
| 260 | var availW = Math.max(80, vw - margin * 2); |
| 261 | var availH = Math.max(80, vh - app.hudH - app.miniH - margin); |
| 262 | app.tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); |
| 263 | app.fieldW = app.tile * 10; |
| 264 | app.fieldH = app.tile * 13; |
| 265 | app.fieldX = Math.floor((vw - app.fieldW) / 2); |
| 266 | var topBand = app.hudH + app.miniH; |
| 267 | app.fieldY = topBand + Math.floor((vh - topBand - app.fieldH - margin / 2) / 2); |
| 268 | if (app.fieldY < topBand) |
| 269 | { app.fieldY = topBand; } |
| 270 | var minis = document.getElementById('minis'); |
| 271 | if (minis) |
| 272 | { |
| 273 | minis.style.top = app.hudH + 'px'; |
| 274 | minis.style.maxHeight = app.miniH + 'px'; |
| 275 | } |
| 276 | |
| 277 | if (app.scene) |
| 278 | { |
| 279 | app.scene.position.x = app.fieldX; |
| 280 | app.scene.position.y = app.fieldY; |
| 281 | app.scene.size.x = app.fieldW; |
| 282 | app.scene.size.y = app.fieldH; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | function spriteFor (block) |
| 287 | { |
| 288 | if (block.state === 'locked') |
| 289 | { return game.images.locked; } |
| 290 | if (block.color < 0) |
| 291 | { return game.images.gray; } |
| 292 | var key = Planet.spriteKey(app.planet, block.color); |
| 293 | return game.images[key] || game.images.gray; |
| 294 | } |
| 295 | |
| 296 | function syncSprites () |
| 297 | { |
| 298 | if (!app.core || !app.scene) |
| 299 | { return; } |
| 300 | var seen = Object.create(null); |
| 301 | var blocks = app.core.blocks; |
| 302 | for (var i = 0; i < blocks.length; i++) |
| 303 | { |
| 304 | var block = blocks[i]; |
| 305 | seen[block.id] = true; |
| 306 | var obj = app.sprites[block.id]; |
| 307 | if (!obj) |
| 308 | { |
| 309 | obj = new GameObject(0, 0, spriteFor(block), 2); |
| 310 | app.scene.addObject(obj); |
| 311 | app.sprites[block.id] = obj; |
| 312 | } |
| 313 | obj.renderer.sprite = spriteFor(block); |
| 314 | placeTile(obj, block.col, block.y); |
| 315 | applyBlockVisual(obj, block); |
| 316 | obj.renderer.visible = true; |
| 317 | obj.renderer.alpha = 1; |
| 318 | |
| 319 | if (block.flame) |
| 320 | { |
| 321 | var flame = app.flames[block.id]; |
| 322 | if (!flame) |
| 323 | { |
| 324 | flame = new GameObject(0, 0, game.images.flame, 1); |
| 325 | flame.renderer.setAnimation('burn', 0, 0.08); |
| 326 | app.scene.addObject(flame); |
| 327 | app.flames[block.id] = flame; |
| 328 | } |
| 329 | flame.renderer.visible = true; |
| 330 | var t = app.tile; |
| 331 | // 3x as large (length and width, t1110). Keep the top of the |
| 332 | // flame at the same overlap into the block so the extra length |
| 333 | // extends downward under the rocket. |
| 334 | var flameW = t * 0.85 * 3; |
| 335 | var flameH = t * 0.85 * 3; |
| 336 | var origCenter = app.fieldH - block.y * t + t * 0.15; |
| 337 | var origTop = origCenter - (t * 0.85) / 2; |
| 338 | flame.transform.position.x = block.col * t + t / 2; |
| 339 | flame.transform.position.y = origTop + flameH / 2; |
| 340 | flame.transform.size = { x: flameW, y: flameH }; |
| 341 | } |
| 342 | else if (app.flames[block.id]) |
| 343 | { app.flames[block.id].renderer.visible = false; } |
| 344 | } |
| 345 | |
| 346 | var ids = Object.keys(app.sprites); |
| 347 | for (var i = 0; i < ids.length; i++) |
| 348 | { |
| 349 | if (!seen[ids[i]]) |
| 350 | { |
| 351 | app.sprites[ids[i]].destroy(); |
| 352 | delete app.sprites[ids[i]]; |
| 353 | if (app.flames[ids[i]]) |
| 354 | { |
| 355 | app.flames[ids[i]].destroy(); |
| 356 | delete app.flames[ids[i]]; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | function applyBlockVisual (obj, block) |
| 363 | { |
| 364 | var t = app.tile; |
| 365 | var sx = 1; |
| 366 | var sy = 1; |
| 367 | var dy = 0; |
| 368 | if (block.bouncing && app.planet && app.planet.bounceDuration > 0 |
| 369 | && typeof Easing !== 'undefined' && Easing.bouncePose) |
| 370 | { |
| 371 | var u = block.bounceAge / app.planet.bounceDuration; |
| 372 | var pose = Easing.bouncePose(u, app.planet.bounceSquash); |
| 373 | sx = pose.sx; |
| 374 | sy = pose.sy; |
| 375 | dy = pose.dy; |
| 376 | } |
| 377 | var sel = 1; |
| 378 | var tw = app.selectTween; |
| 379 | if (tw && tw.blockId === block.id && tw.phase !== 'idle') |
| 380 | { sel = tw.scale; } |
| 381 | obj.transform.size = { x: t * sx * sel, y: t * sy * sel }; |
| 382 | if (dy) |
| 383 | { obj.transform.position.y -= dy * t; } |
| 384 | } |
| 385 | |
| 386 | function clickTween () |
| 387 | { |
| 388 | return (typeof Easing !== 'undefined' && Easing.CLICK_TWEEN) ? Easing.CLICK_TWEEN : null; |
| 389 | } |
| 390 | |
| 391 | function startPressTween (blockId) |
| 392 | { |
| 393 | var c = clickTween(); |
| 394 | if (!c) |
| 395 | { return; } |
| 396 | var from = (app.selectTween && app.selectTween.phase !== 'idle') ? app.selectTween.scale : 1; |
| 397 | app.selectTween = { |
| 398 | blockId: blockId, |
| 399 | from: from, |
| 400 | to: c.pressScale, |
| 401 | t: 0, |
| 402 | duration: c.pressDuration, |
| 403 | ease: c.pressEase, |
| 404 | scale: from, |
| 405 | phase: 'press' |
| 406 | }; |
| 407 | } |
| 408 | |
| 409 | function startReleaseTween () |
| 410 | { |
| 411 | var c = clickTween(); |
| 412 | var tw = app.selectTween; |
| 413 | if (!c || !tw || tw.phase === 'idle') |
| 414 | { return; } |
| 415 | app.selectTween = { |
| 416 | blockId: tw.blockId, |
| 417 | from: tw.scale, |
| 418 | to: c.releaseScale, |
| 419 | t: 0, |
| 420 | duration: c.releaseDuration, |
| 421 | ease: c.releaseEase, |
| 422 | scale: tw.scale, |
| 423 | phase: 'release' |
| 424 | }; |
| 425 | } |
| 426 | |
| 427 | function updateSelectTween (dt) |
| 428 | { |
| 429 | var tw = app.selectTween; |
| 430 | if (!tw || tw.phase === 'idle' || tw.phase === 'hold') |
| 431 | { return; } |
| 432 | tw.t += dt; |
| 433 | var u = tw.duration > 0 ? tw.t / tw.duration : 1; |
| 434 | if (u >= 1) |
| 435 | { |
| 436 | tw.scale = tw.to; |
| 437 | tw.t = tw.duration; |
| 438 | if (tw.phase === 'press') |
| 439 | { tw.phase = 'hold'; } |
| 440 | else |
| 441 | { |
| 442 | tw.phase = 'idle'; |
| 443 | tw.scale = 1; |
| 444 | tw.blockId = null; |
| 445 | } |
| 446 | return; |
| 447 | } |
| 448 | var ease = tw.ease || (typeof Easing !== 'undefined' && Easing.easeOutBack); |
| 449 | tw.scale = ease ? Easing.tween(tw.from, tw.to, u, ease) : tw.from + (tw.to - tw.from) * u; |
| 450 | } |
| 451 | |
| 452 | function refreshHud () |
| 453 | { |
| 454 | if (!app.core) |
| 455 | { return; } |
| 456 | document.getElementById('score').textContent = String(app.core.score); |
| 457 | var planet = app.planet; |
| 458 | document.getElementById('planet-name').textContent = planet.name + ' · ' + planet.people; |
| 459 | document.getElementById('planet-stats').textContent = Planet.formatStats(planet); |
| 460 | } |
| 461 | |
| 462 | function makeManager () |
| 463 | { |
| 464 | var obj = new GameObject(0, 0, game.images.pixel, 0); |
| 465 | obj.renderer.visible = false; |
| 466 | var base = obj.update.bind(obj); |
| 467 | obj.update = function (dt) |
| 468 | { |
| 469 | base(dt); |
| 470 | tickView(dt); |
| 471 | }; |
| 472 | return obj; |
| 473 | } |
| 474 | |
| 475 | function tickView (dt) |
| 476 | { |
| 477 | if (app.mode !== 'playing' || !app.core) |
| 478 | { return; } |
| 479 | |
| 480 | // Never resize the canvas mid-drag: a URL-bar hide would move the |
| 481 | // field under the finger and jump the target row. Also skip work |
| 482 | // when the viewport has not actually changed. |
| 483 | if (!(app.core && app.core.drag)) |
| 484 | { layoutIfNeeded(); } |
| 485 | for (var i = 0; i < app.cells.length; i++) |
| 486 | { |
| 487 | var col = i % 10; |
| 488 | var row = Math.floor(i / 10); |
| 489 | placeTile(app.cells[i], col, row); |
| 490 | } |
| 491 | |
| 492 | app.core.tick(dt); |
| 493 | updateSelectTween(dt); |
| 494 | syncSprites(); |
| 495 | refreshHud(); |
| 496 | tickOnline(dt); |
| 497 | |
| 498 | if (app.core.state === 'gameover' && !app.gameOverShown) |
| 499 | { |
| 500 | app.gameOverShown = true; |
| 501 | app.lastScore = app.core.score; |
| 502 | document.getElementById('go-score').textContent = String(app.lastScore); |
| 503 | if (app.online) |
| 504 | { onOnlineEliminated(); } |
| 505 | else |
| 506 | { showEl('game-over'); } |
| 507 | } |
| 508 | if (!app.online && app.core.state === 'gameover' && app.core.gameOverTime >= GAME_OVER_DELAY) |
| 509 | { returnToMenu(); } |
| 510 | } |
| 511 | |
| 512 | /* ---------- pointer input (mouse + touch) --------------------------- */ |
| 513 | /* |
| 514 | * Mouse uses Pointer Events. Touch uses Touch Events as the authority: |
| 515 | * on a real phone, pointer clientX/Y can disagree with the visual |
| 516 | * finger (zoomed visual viewport, gesture takeover, capture cancel) |
| 517 | * while changedTouches stays correct. When a touch is active, pointer |
| 518 | * move/up/cancel are ignored so a poisoned pointer coordinate cannot |
| 519 | * walk the block to the top or bottom of the stack (t1106). |
| 520 | * |
| 521 | * After a down we also listen on the document, so a drag that leaves |
| 522 | * the canvas (needed to launch a block at the top of a full column) |
| 523 | * still reaches moveDrag. We do not call setPointerCapture: on mobile |
| 524 | * that is often cancelled when the browser takes the gesture, which |
| 525 | * would fire pointercancel mid-drag. |
| 526 | * |
| 527 | * A non-passive document touchstart/touchmove preventDefault during |
| 528 | * play stops scrolling, rubber-banding and the pointercancel that |
| 529 | * follows. pointercancel / touchcancel end the drag without launch. |
| 530 | * |
| 531 | * ?debug=drag (or localStorage meteors-debug-drag=1) shows an overlay. |
| 532 | */ |
| 533 | |
| 534 | var boundCanvas = null; |
| 535 | var windowListening = false; |
| 536 | var touchListening = false; |
| 537 | var lastDragDebug = null; |
| 538 | |
| 539 | function isDragDebugEnabled () |
| 540 | { |
| 541 | try |
| 542 | { |
| 543 | var q = window.location && window.location.search; |
| 544 | if (q && /(?:\?|&)debug=drag(?:&|$)/.test(q)) |
| 545 | { return true; } |
| 546 | if (window.localStorage && window.localStorage.getItem('meteors-debug-drag') === '1') |
| 547 | { return true; } |
| 548 | } |
| 549 | catch (err) { /* ignore */ } |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | function eventClient (e) |
| 554 | { |
| 555 | var t = null; |
| 556 | if (e.changedTouches && e.changedTouches.length) |
| 557 | { t = e.changedTouches[0]; } |
| 558 | else if (e.touches && e.touches.length) |
| 559 | { t = e.touches[0]; } |
| 560 | if (t) |
| 561 | { return { x: t.clientX, y: t.clientY, id: t.identifier, src: 'touch' }; } |
| 562 | if (e.clientX != null && e.clientY != null) |
| 563 | { return { x: e.clientX, y: e.clientY, id: e.pointerId, src: 'pointer' }; } |
| 564 | return null; |
| 565 | } |
| 566 | |
| 567 | function addWindowPointer () |
| 568 | { |
| 569 | if (windowListening) |
| 570 | { return; } |
| 571 | window.addEventListener('pointermove', onPointerMove, { capture: true, passive: false }); |
| 572 | window.addEventListener('pointerup', onPointerUp, { capture: true, passive: false }); |
| 573 | window.addEventListener('pointercancel', onPointerCancel, { capture: true, passive: false }); |
| 574 | windowListening = true; |
| 575 | } |
| 576 | |
| 577 | function removeWindowPointer () |
| 578 | { |
| 579 | if (!windowListening) |
| 580 | { return; } |
| 581 | window.removeEventListener('pointermove', onPointerMove, { capture: true }); |
| 582 | window.removeEventListener('pointerup', onPointerUp, { capture: true }); |
| 583 | window.removeEventListener('pointercancel', onPointerCancel, { capture: true }); |
| 584 | windowListening = false; |
| 585 | } |
| 586 | |
| 587 | function addTouchListeners () |
| 588 | { |
| 589 | if (touchListening) |
| 590 | { return; } |
| 591 | var opts = { capture: true, passive: false }; |
| 592 | document.addEventListener('touchstart', onTouchStart, opts); |
| 593 | document.addEventListener('touchmove', onTouchMove, opts); |
| 594 | document.addEventListener('touchend', onTouchEnd, opts); |
| 595 | document.addEventListener('touchcancel', onTouchCancel, opts); |
| 596 | touchListening = true; |
| 597 | } |
| 598 | |
| 599 | function removeTouchListeners () |
| 600 | { |
| 601 | if (!touchListening) |
| 602 | { return; } |
| 603 | var opts = { capture: true }; |
| 604 | document.removeEventListener('touchstart', onTouchStart, opts); |
| 605 | document.removeEventListener('touchmove', onTouchMove, opts); |
| 606 | document.removeEventListener('touchend', onTouchEnd, opts); |
| 607 | document.removeEventListener('touchcancel', onTouchCancel, opts); |
| 608 | touchListening = false; |
| 609 | } |
| 610 | |
| 611 | function bindPointer (canvas) |
| 612 | { |
| 613 | unbindPointer(); |
| 614 | boundCanvas = canvas; |
| 615 | canvas.addEventListener('pointerdown', onPointerDown, { passive: false }); |
| 616 | canvas.style.touchAction = 'none'; |
| 617 | canvas.style.position = 'fixed'; |
| 618 | canvas.style.cursor = 'pointer'; |
| 619 | canvas.style.webkitUserSelect = 'none'; |
| 620 | canvas.style.userSelect = 'none'; |
| 621 | addTouchListeners(); |
| 622 | if (app.debugDrag) |
| 623 | { ensureDragDebugOverlay(); } |
| 624 | } |
| 625 | |
| 626 | function unbindPointer () |
| 627 | { |
| 628 | removeWindowPointer(); |
| 629 | removeTouchListeners(); |
| 630 | if (boundCanvas) |
| 631 | { boundCanvas.removeEventListener('pointerdown', onPointerDown); } |
| 632 | boundCanvas = null; |
| 633 | app.pointerId = null; |
| 634 | app.pointerType = null; |
| 635 | app.activeTouchId = null; |
| 636 | } |
| 637 | |
| 638 | function fieldFromEvent (e) |
| 639 | { |
| 640 | var canvas = app.scene && app.scene.canvas; |
| 641 | if (!canvas) |
| 642 | { return null; } |
| 643 | var rect = canvas.getBoundingClientRect(); |
| 644 | if (rect.width <= 0 || rect.height <= 0) |
| 645 | { return null; } |
| 646 | var pt = eventClient(e); |
| 647 | if (!pt) |
| 648 | { return null; } |
| 649 | // Map through the CSS box only. Using canvas.width / app.tile would |
| 650 | // scale the pointer by the backing-store ratio whenever that differs |
| 651 | // from the layout size (high-DPR devices), so a short finger move |
| 652 | // would look like a long swipe. |
| 653 | var width = Planet.FIELD_WIDTH; |
| 654 | var height = Planet.FIELD_HEIGHT; |
| 655 | var col = ((pt.x - rect.left) / rect.width) * width; |
| 656 | var fieldY = (1 - (pt.y - rect.top) / rect.height) * height; |
| 657 | return { col: col, y: fieldY, src: pt.src, clientX: pt.x, clientY: pt.y, rect: rect }; |
| 658 | } |
| 659 | |
| 660 | function playingForDrag () |
| 661 | { |
| 662 | return app.core && app.core.state === 'playing'; |
| 663 | } |
| 664 | |
| 665 | function startDragFromEvent (e) |
| 666 | { |
| 667 | var pos = fieldFromEvent(e); |
| 668 | if (!pos) |
| 669 | { return; } |
| 670 | if (app.core.drag) |
| 671 | { |
| 672 | app.core.moveDrag(pos.y); |
| 673 | logDragDebug(e, pos, 'move-already'); |
| 674 | return; |
| 675 | } |
| 676 | if (app.core.beginDrag(pos.col, pos.y) && app.core.drag) |
| 677 | { startPressTween(app.core.drag.blockId); } |
| 678 | logDragDebug(e, pos, 'down'); |
| 679 | } |
| 680 | |
| 681 | function moveDragFromEvent (e) |
| 682 | { |
| 683 | if (!app.core || !app.core.drag) |
| 684 | { return; } |
| 685 | var pos = fieldFromEvent(e); |
| 686 | if (pos) |
| 687 | { app.core.moveDrag(pos.y); } |
| 688 | logDragDebug(e, pos, 'move'); |
| 689 | } |
| 690 | |
| 691 | function finishDrag (opts) |
| 692 | { |
| 693 | opts = opts || {}; |
| 694 | app.pointerId = null; |
| 695 | app.pointerType = null; |
| 696 | app.activeTouchId = null; |
| 697 | removeWindowPointer(); |
| 698 | if (boundCanvas) |
| 699 | { |
| 700 | try |
| 701 | { |
| 702 | if (opts.pointerId != null && boundCanvas.releasePointerCapture) |
| 703 | { boundCanvas.releasePointerCapture(opts.pointerId); } |
| 704 | } |
| 705 | catch (err) { /* ignore */ } |
| 706 | } |
| 707 | startReleaseTween(); |
| 708 | if (app.core && app.core.drag) |
| 709 | { |
| 710 | if (opts.noLaunch) |
| 711 | { app.core.drag.launchIntent = false; } |
| 712 | app.core.endDrag(); |
| 713 | } |
| 714 | else if (app.core) |
| 715 | { app.core.endDrag(); } |
| 716 | logDragDebug(null, null, opts.noLaunch ? 'cancel' : 'up'); |
| 717 | } |
| 718 | |
| 719 | function onPointerDown (e) |
| 720 | { |
| 721 | if (!playingForDrag()) |
| 722 | { return; } |
| 723 | if (app.pointerId != null && e.pointerId !== app.pointerId) |
| 724 | { return; } |
| 725 | if (e.preventDefault) |
| 726 | { e.preventDefault(); } |
| 727 | app.pointerId = e.pointerId; |
| 728 | app.pointerType = e.pointerType || 'mouse'; |
| 729 | addWindowPointer(); |
| 730 | // If a touch is already in charge, do not reset the grab from pointerdown. |
| 731 | if (app.activeTouchId != null) |
| 732 | { return; } |
| 733 | startDragFromEvent(e); |
| 734 | } |
| 735 | |
| 736 | function onPointerMove (e) |
| 737 | { |
| 738 | if (app.pointerId == null || e.pointerId !== app.pointerId) |
| 739 | { return; } |
| 740 | if (!app.core || !app.core.drag) |
| 741 | { return; } |
| 742 | // Touch coordinates are the authority; ignore pointer moves for a |
| 743 | // live touch so a wrong clientY cannot walk the whole column. |
| 744 | // Only skip when touchstart has actually arrived — some environments |
| 745 | // deliver touch-pointer events without a TouchEvent stream. |
| 746 | if (app.activeTouchId != null) |
| 747 | { |
| 748 | if (e.preventDefault) |
| 749 | { e.preventDefault(); } |
| 750 | logDragDebug(e, fieldFromEvent(e), 'pointer-ignored'); |
| 751 | return; |
| 752 | } |
| 753 | if (e.preventDefault) |
| 754 | { e.preventDefault(); } |
| 755 | moveDragFromEvent(e); |
| 756 | } |
| 757 | |
| 758 | function onPointerUp (e) |
| 759 | { |
| 760 | if (app.pointerId == null) |
| 761 | { return; } |
| 762 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 763 | { return; } |
| 764 | if (app.activeTouchId != null) |
| 765 | { |
| 766 | // touchend will finish the drag. |
| 767 | if (e && e.preventDefault) |
| 768 | { e.preventDefault(); } |
| 769 | logDragDebug(e, fieldFromEvent(e), 'pointer-up-ignored'); |
| 770 | return; |
| 771 | } |
| 772 | if (e && e.preventDefault) |
| 773 | { e.preventDefault(); } |
| 774 | finishDrag({ pointerId: e && e.pointerId }); |
| 775 | } |
| 776 | |
| 777 | function onPointerCancel (e) |
| 778 | { |
| 779 | if (app.pointerId == null) |
| 780 | { return; } |
| 781 | if (e && e.pointerId != null && e.pointerId !== app.pointerId) |
| 782 | { return; } |
| 783 | if (app.activeTouchId != null) |
| 784 | { |
| 785 | if (e && e.preventDefault) |
| 786 | { e.preventDefault(); } |
| 787 | logDragDebug(e, fieldFromEvent(e), 'pointer-cancel-ignored'); |
| 788 | return; |
| 789 | } |
| 790 | if (e && e.preventDefault) |
| 791 | { e.preventDefault(); } |
| 792 | // A cancelled gesture is not a launch pull. |
| 793 | finishDrag({ pointerId: e && e.pointerId, noLaunch: true }); |
| 794 | } |
| 795 | |
| 796 | function touchById (list, id) |
| 797 | { |
| 798 | if (!list) |
| 799 | { return null; } |
| 800 | for (var i = 0; i < list.length; i++) |
| 801 | { |
| 802 | if (list[i].identifier === id) |
| 803 | { return list[i]; } |
| 804 | } |
| 805 | return null; |
| 806 | } |
| 807 | |
| 808 | function onTouchStart (e) |
| 809 | { |
| 810 | if (!playingForDrag()) |
| 811 | { return; } |
| 812 | if (app.activeTouchId != null) |
| 813 | { |
| 814 | if (e.preventDefault) |
| 815 | { e.preventDefault(); } |
| 816 | return; |
| 817 | } |
| 818 | var t = e.changedTouches && e.changedTouches[0]; |
| 819 | if (!t) |
| 820 | { return; } |
| 821 | if (e.preventDefault) |
| 822 | { e.preventDefault(); } |
| 823 | app.activeTouchId = t.identifier; |
| 824 | addWindowPointer(); |
| 825 | startDragFromEvent(e); |
| 826 | } |
| 827 | |
| 828 | function onTouchMove (e) |
| 829 | { |
| 830 | if (app.activeTouchId == null) |
| 831 | { return; } |
| 832 | if (!touchById(e.changedTouches, app.activeTouchId) |
| 833 | && !touchById(e.touches, app.activeTouchId)) |
| 834 | { return; } |
| 835 | if (e.preventDefault) |
| 836 | { e.preventDefault(); } |
| 837 | moveDragFromEvent(e); |
| 838 | } |
| 839 | |
| 840 | function onTouchEnd (e) |
| 841 | { |
| 842 | if (app.activeTouchId == null) |
| 843 | { return; } |
| 844 | if (!touchById(e.changedTouches, app.activeTouchId)) |
| 845 | { return; } |
| 846 | if (e.preventDefault) |
| 847 | { e.preventDefault(); } |
| 848 | finishDrag({}); |
| 849 | } |
| 850 | |
| 851 | function onTouchCancel (e) |
| 852 | { |
| 853 | if (app.activeTouchId == null) |
| 854 | { return; } |
| 855 | if (!touchById(e.changedTouches, app.activeTouchId)) |
| 856 | { return; } |
| 857 | if (e.preventDefault) |
| 858 | { e.preventDefault(); } |
| 859 | finishDrag({ noLaunch: true }); |
| 860 | } |
| 861 | |
| 862 | function ensureDragDebugOverlay () |
| 863 | { |
| 864 | if (document.getElementById('drag-debug')) |
| 865 | { return; } |
| 866 | var el = document.createElement('div'); |
| 867 | el.id = 'drag-debug'; |
| 868 | el.setAttribute('aria-live', 'polite'); |
| 869 | document.body.appendChild(el); |
| 870 | } |
| 871 | |
| 872 | function logDragDebug (e, pos, phase) |
| 873 | { |
| 874 | if (!app.debugDrag) |
| 875 | { return; } |
| 876 | ensureDragDebugOverlay(); |
| 877 | var el = document.getElementById('drag-debug'); |
| 878 | if (!el) |
| 879 | { return; } |
| 880 | var block = null; |
| 881 | if (app.core && app.core.drag) |
| 882 | { block = app.core.blockById(app.core.drag.blockId); } |
| 883 | var pt = e ? eventClient(e) : null; |
| 884 | var evType = e ? e.type : phase; |
| 885 | var info = { |
| 886 | phase: phase, |
| 887 | ev: evType, |
| 888 | src: pos && pos.src, |
| 889 | fieldY: pos && Math.round(pos.y * 100) / 100, |
| 890 | col: pos && Math.round(pos.col * 100) / 100, |
| 891 | cy: pt && Math.round(pt.y * 10) / 10, |
| 892 | blockY: block && Math.round(block.y * 100) / 100, |
| 893 | target: block && Math.floor((pos && pos.y) + 1e-12), |
| 894 | launch: app.core && app.core.drag && app.core.drag.launchIntent, |
| 895 | touchId: app.activeTouchId, |
| 896 | pid: app.pointerId, |
| 897 | ptype: app.pointerType |
| 898 | }; |
| 899 | lastDragDebug = info; |
| 900 | el.textContent = 'drag ' + phase |
| 901 | + ' ev=' + evType |
| 902 | + ' src=' + (info.src || '-') |
| 903 | + ' y=' + (info.fieldY != null ? info.fieldY : '-') |
| 904 | + ' → ' + (info.target != null ? info.target : '-') |
| 905 | + ' block=' + (info.blockY != null ? info.blockY : '-') |
| 906 | + (info.launch ? ' LAUNCH' : '') |
| 907 | + ' tId=' + info.touchId |
| 908 | + ' pId=' + info.pid; |
| 909 | el.hidden = false; |
| 910 | } |
| 911 | |
| 912 | |
| 913 | function hideEl (id) |
| 914 | { |
| 915 | document.getElementById(id).hidden = true; |
| 916 | } |
| 917 | |
| 918 | function showEl (id) |
| 919 | { |
| 920 | document.getElementById(id).hidden = false; |
| 921 | } |
| 922 | |
| 923 | window.addEventListener('resize', function () |
| 924 | { |
| 925 | if (app.mode === 'playing') |
| 926 | { layout(); } |
| 927 | }); |
| 928 | |
| 929 | window.addEventListener('orientationchange', function () |
| 930 | { |
| 931 | setTimeout(function () |
| 932 | { |
| 933 | if (app.mode === 'playing') |
| 934 | { layout(); } |
| 935 | }, 120); |
| 936 | }); |
| 937 | |
| 938 | /* ---------- online play (stage 2) ---------------------------------- */ |
| 939 | |
| 940 | function tickOnline (dt) |
| 941 | { |
| 942 | if (!app.online || typeof Online === 'undefined') |
| 943 | { return; } |
| 944 | if (app.core) |
| 945 | { |
| 946 | var shots = app.core.consumeOutgoingShots(); |
| 947 | for (var i = 0; i < shots.length; i++) |
| 948 | { |
| 949 | var s = shots[i]; |
| 950 | var start = fieldToViewport(s.col, s.y + 1); |
| 951 | Online.rememberFlightLocal(s, start.x, start.y); |
| 952 | } |
| 953 | Online.queueShots(shots); |
| 954 | } |
| 955 | Online.tickFlights(); |
| 956 | Online.renderMinis(document.getElementById('minis')); |
| 957 | } |
| 958 | |
| 959 | function fieldToViewport (col, fieldY) |
| 960 | { |
| 961 | var canvas = app.scene && app.scene.canvas; |
| 962 | if (!canvas) |
| 963 | { return { x: window.innerWidth / 2, y: 40 }; } |
| 964 | var rect = canvas.getBoundingClientRect(); |
| 965 | var x = rect.left + ((col + 0.5) / 10) * rect.width; |
| 966 | var y = rect.top + (1 - (fieldY / 13)) * rect.height; |
| 967 | return { x: x, y: y }; |
| 968 | } |
| 969 | |
| 970 | function onlinePayload () |
| 971 | { |
| 972 | if (!app.online || !app.core) |
| 973 | { return {}; } |
| 974 | return { |
| 975 | snapshot: app.core.fieldSnapshot(), |
| 976 | score: app.core.score, |
| 977 | eliminated: app.core.state === 'gameover' || app.spectating || app.eliminatedReported |
| 978 | }; |
| 979 | } |
| 980 | |
| 981 | function onOnlineEliminated () |
| 982 | { |
| 983 | app.eliminatedReported = true; |
| 984 | app.spectating = true; |
| 985 | unbindPointer(); |
| 986 | hideEl('game-over'); |
| 987 | var lobby = typeof Online !== 'undefined' && Online.lobby; |
| 988 | if (lobby && lobby.status === 'finished') |
| 989 | { |
| 990 | showPodium(lobby); |
| 991 | return; |
| 992 | } |
| 993 | showEl('spectator'); |
| 994 | } |
| 995 | |
| 996 | function showPodium (lobby) |
| 997 | { |
| 998 | hideEl('spectator'); |
| 999 | hideEl('game-over'); |
| 1000 | hideEl('online-panel'); |
| 1001 | showEl('podium'); |
| 1002 | var list = document.getElementById('podium-list'); |
| 1003 | list.innerHTML = ''; |
| 1004 | var ranks = (lobby && lobby.rankings) || []; |
| 1005 | for (var i = 0; i < ranks.length; i++) |
| 1006 | { |
| 1007 | var r = ranks[i]; |
| 1008 | var li = document.createElement('li'); |
| 1009 | var medal = r.place === 1 ? '1e' : (r.place === 2 ? '2e' : (r.place === 3 ? '3e' : r.place + 'e')); |
| 1010 | li.textContent = medal + ' ' + r.name + ' · ' + r.score + ' punten'; |
| 1011 | list.appendChild(li); |
| 1012 | } |
| 1013 | var hint = document.getElementById('podium-hint'); |
| 1014 | var secs = (lobby && lobby.podium_seconds) || 5; |
| 1015 | hint.textContent = 'Over ' + secs + ' seconden teruggaan naar de lobby…'; |
| 1016 | } |
| 1017 | |
| 1018 | function startOnlineMatch () |
| 1019 | { |
| 1020 | if (app.mode === 'playing' && app.online) |
| 1021 | { return; } |
| 1022 | var planet = Planet.planetById(selectedPlanetId()); |
| 1023 | var me = (typeof Online !== 'undefined' && Online.mePlayer) ? Online.mePlayer() : null; |
| 1024 | if (me && me.planet && me.planet.id) |
| 1025 | { planet = Planet.planetById(me.planet.id); } |
| 1026 | startPlay({ online: true, planet: planet, missChance: Planet.missChance(planet) }); |
| 1027 | if (typeof Online !== 'undefined') |
| 1028 | { Online.startPolling(onlinePayload); } |
| 1029 | } |
| 1030 | |
| 1031 | function leaveOnlineMatch (goMenu) |
| 1032 | { |
| 1033 | if (typeof Online !== 'undefined') |
| 1034 | { |
| 1035 | Online.stopPolling(); |
| 1036 | Online.leaveLobby(); |
| 1037 | Online.clearFlights(); |
| 1038 | } |
| 1039 | teardownPlay(); |
| 1040 | app.online = false; |
| 1041 | if (goMenu) |
| 1042 | { |
| 1043 | app.mode = 'menu'; |
| 1044 | showEl('menu'); |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | app.mode = 'online'; |
| 1049 | openOnlinePanel(); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | function bindOnlineUi () |
| 1054 | { |
| 1055 | var onlineBtn = document.getElementById('btn-online'); |
| 1056 | if (!onlineBtn) |
| 1057 | { return; } |
| 1058 | onlineBtn.addEventListener('click', function () |
| 1059 | { openOnlinePanel(); }); |
| 1060 | |
| 1061 | var backs = document.querySelectorAll('[data-back-menu]'); |
| 1062 | for (var i = 0; i < backs.length; i++) |
| 1063 | { |
| 1064 | backs[i].addEventListener('click', function () |
| 1065 | { |
| 1066 | if (typeof Online !== 'undefined') |
| 1067 | { Online.stopPolling(); } |
| 1068 | hideEl('online-panel'); |
| 1069 | app.mode = 'menu'; |
| 1070 | app.online = false; |
| 1071 | showEl('menu'); |
| 1072 | }); |
| 1073 | } |
| 1074 | |
| 1075 | document.getElementById('btn-create').addEventListener('click', function () |
| 1076 | { |
| 1077 | Online.createLobby().then(function (data) |
| 1078 | { |
| 1079 | if (data && data.ok) |
| 1080 | { |
| 1081 | renderOnline(); |
| 1082 | Online.startPolling(onlinePayload); |
| 1083 | } |
| 1084 | else |
| 1085 | { renderOnline(); } |
| 1086 | }); |
| 1087 | }); |
| 1088 | |
| 1089 | document.getElementById('join-form').addEventListener('submit', function (e) |
| 1090 | { |
| 1091 | e.preventDefault(); |
| 1092 | var code = (document.getElementById('join-code').value || '').trim(); |
| 1093 | if (!code) |
| 1094 | { return; } |
| 1095 | Online.joinLobby(code).then(function () |
| 1096 | { |
| 1097 | renderOnline(); |
| 1098 | if (Online.lobby) |
| 1099 | { Online.startPolling(onlinePayload); } |
| 1100 | }); |
| 1101 | }); |
| 1102 | |
| 1103 | document.getElementById('btn-ready').addEventListener('click', function () |
| 1104 | { |
| 1105 | var me = Online.mePlayer(); |
| 1106 | Online.setReady(!(me && me.ready)).then(renderOnline); |
| 1107 | }); |
| 1108 | |
| 1109 | document.getElementById('btn-start').addEventListener('click', function () |
| 1110 | { |
| 1111 | Online.startGame().then(function (data) |
| 1112 | { |
| 1113 | if (data && data.ok) |
| 1114 | { startOnlineMatch(); } |
| 1115 | else |
| 1116 | { renderOnline(); } |
| 1117 | }); |
| 1118 | }); |
| 1119 | |
| 1120 | document.getElementById('btn-leave-lobby').addEventListener('click', function () |
| 1121 | { |
| 1122 | Online.leaveLobby().then(function () |
| 1123 | { |
| 1124 | Online.stopPolling(); |
| 1125 | renderOnline(); |
| 1126 | }); |
| 1127 | }); |
| 1128 | |
| 1129 | document.getElementById('btn-copy-code').addEventListener('click', function () |
| 1130 | { |
| 1131 | var code = document.getElementById('lobby-code').textContent; |
| 1132 | if (navigator.clipboard && navigator.clipboard.writeText) |
| 1133 | { navigator.clipboard.writeText(code); } |
| 1134 | }); |
| 1135 | |
| 1136 | document.getElementById('btn-spectate').addEventListener('click', function () |
| 1137 | { |
| 1138 | hideEl('spectator'); |
| 1139 | app.spectating = true; |
| 1140 | }); |
| 1141 | |
| 1142 | document.getElementById('btn-leave-game').addEventListener('click', function () |
| 1143 | { leaveOnlineMatch(false); }); |
| 1144 | |
| 1145 | if (typeof Online !== 'undefined') |
| 1146 | { |
| 1147 | Online.onChange = function () |
| 1148 | { renderOnline(); }; |
| 1149 | Online.onGameStart = function () |
| 1150 | { startOnlineMatch(); }; |
| 1151 | Online.onIncoming = function (incoming) |
| 1152 | { |
| 1153 | if (!app.core || app.core.state !== 'playing') |
| 1154 | { return; } |
| 1155 | for (var i = 0; i < incoming.length; i++) |
| 1156 | { app.core.ingestGarbage(); } |
| 1157 | }; |
| 1158 | Online.onFinished = function (lobby) |
| 1159 | { showPodium(lobby); }; |
| 1160 | Online.onLobbyReturn = function (lobby) |
| 1161 | { |
| 1162 | teardownPlay(); |
| 1163 | app.online = false; |
| 1164 | app.mode = 'online'; |
| 1165 | hideEl('podium'); |
| 1166 | showEl('online-panel'); |
| 1167 | renderOnline(); |
| 1168 | Online.startPolling(onlinePayload); |
| 1169 | }; |
| 1170 | } |
| 1171 | |
| 1172 | var params = new URLSearchParams(window.location.search); |
| 1173 | if (params.get('online') === '1' || params.get('auth') === 'unconfigured') |
| 1174 | { openOnlinePanel(); } |
| 1175 | } |
| 1176 | |
| 1177 | function openOnlinePanel () |
| 1178 | { |
| 1179 | hideEl('menu'); |
| 1180 | hideEl('game-over'); |
| 1181 | hideEl('podium'); |
| 1182 | hideEl('spectator'); |
| 1183 | hideEl('planet-select'); |
| 1184 | showEl('online-panel'); |
| 1185 | app.mode = 'online'; |
| 1186 | setOnlineStatus('Even kijken of online spelen beschikbaar is…'); |
| 1187 | if (typeof Online === 'undefined') |
| 1188 | { |
| 1189 | showOnlineError('Online spelen is in deze build niet geladen. Je kunt wel alleen spelen.'); |
| 1190 | showOnly('online-unconfigured'); |
| 1191 | return; |
| 1192 | } |
| 1193 | Online.refresh().then(function (data) |
| 1194 | { |
| 1195 | if (!data || data.ok === false) |
| 1196 | { |
| 1197 | var msg = (data && data.error) || 'Geen verbinding met de server.'; |
| 1198 | showOnlineError(msg + ' Je kunt wel alleen spelen.'); |
| 1199 | if (data && data.configured === false) |
| 1200 | { showOnly('online-unconfigured'); } |
| 1201 | else |
| 1202 | { showOnly('online-login'); } |
| 1203 | document.getElementById('btn-login').style.display = 'none'; |
| 1204 | return; |
| 1205 | } |
| 1206 | renderOnline(); |
| 1207 | if (Online.lobby) |
| 1208 | { Online.startPolling(onlinePayload); } |
| 1209 | }); |
| 1210 | } |
| 1211 | |
| 1212 | function setOnlineStatus (text) |
| 1213 | { |
| 1214 | var el = document.getElementById('online-status'); |
| 1215 | if (el) |
| 1216 | { el.textContent = text; } |
| 1217 | } |
| 1218 | |
| 1219 | function showOnlineError (text) |
| 1220 | { |
| 1221 | var el = document.getElementById('online-error'); |
| 1222 | if (!el) |
| 1223 | { return; } |
| 1224 | if (!text) |
| 1225 | { |
| 1226 | el.hidden = true; |
| 1227 | el.textContent = ''; |
| 1228 | return; |
| 1229 | } |
| 1230 | el.hidden = false; |
| 1231 | el.textContent = text; |
| 1232 | } |
| 1233 | |
| 1234 | function showOnly (id) |
| 1235 | { |
| 1236 | var ids = ['online-unconfigured', 'online-login', 'online-home', 'online-lobby']; |
| 1237 | for (var i = 0; i < ids.length; i++) |
| 1238 | { document.getElementById(ids[i]).hidden = (ids[i] !== id); } |
| 1239 | } |
| 1240 | |
| 1241 | function renderOnline () |
| 1242 | { |
| 1243 | if (typeof Online === 'undefined') |
| 1244 | { return; } |
| 1245 | if (app.mode === 'playing' && app.online) |
| 1246 | { |
| 1247 | Online.renderMinis(document.getElementById('minis')); |
| 1248 | if (Online.lobby && Online.lobby.status === 'finished') |
| 1249 | { showPodium(Online.lobby); } |
| 1250 | return; |
| 1251 | } |
| 1252 | showOnlineError(Online.error); |
| 1253 | if (!Online.configured && !Online.me && !Online.local) |
| 1254 | { |
| 1255 | setOnlineStatus('Online spelen is nog niet ingesteld.'); |
| 1256 | showOnly('online-unconfigured'); |
| 1257 | return; |
| 1258 | } |
| 1259 | if (!Online.me) |
| 1260 | { |
| 1261 | setOnlineStatus('Log in om een lobby te maken of eraan deel te nemen.'); |
| 1262 | showOnly('online-login'); |
| 1263 | document.getElementById('btn-login').style.display = ''; |
| 1264 | document.getElementById('btn-login').href = Online.loginUrl(); |
| 1265 | document.getElementById('local-login').hidden = !Online.local; |
| 1266 | return; |
| 1267 | } |
| 1268 | if (Online.lobby) |
| 1269 | { |
| 1270 | setOnlineStatus('Lobby'); |
| 1271 | showOnly('online-lobby'); |
| 1272 | document.getElementById('lobby-me-name').textContent = Online.me.name; |
| 1273 | document.getElementById('lobby-code').textContent = Online.lobby.code; |
| 1274 | var list = document.getElementById('lobby-players'); |
| 1275 | list.innerHTML = ''; |
| 1276 | var players = Online.lobby.players || []; |
| 1277 | for (var i = 0; i < players.length; i++) |
| 1278 | { |
| 1279 | var p = players[i]; |
| 1280 | var li = document.createElement('li'); |
| 1281 | if (p.is_me) |
| 1282 | { li.classList.add('you'); } |
| 1283 | var label = p.name + (p.is_host ? ' (host)' : '') + (p.is_me ? ' (jij)' : ''); |
| 1284 | var state = p.ready ? 'Gereed' : 'Wacht…'; |
| 1285 | var world = (p.planet && p.planet.name) ? (p.planet.name + ' · ' + p.planet.people) : ''; |
| 1286 | li.innerHTML = '<span>' + escapeText(label) |
| 1287 | + (world ? '<span class="planet-tag">' + escapeText(world) + '</span>' : '') |
| 1288 | + '</span><span class="' |
| 1289 | + (p.ready ? 'ready' : 'waiting') + '">' + state + '</span>'; |
| 1290 | list.appendChild(li); |
| 1291 | } |
| 1292 | renderPlanetGrid('lobby-planet-grid', true); |
| 1293 | var me = Online.mePlayer(); |
| 1294 | document.getElementById('btn-ready').textContent = (me && me.ready) ? 'Niet gereed' : 'Gereed'; |
| 1295 | var start = document.getElementById('btn-start'); |
| 1296 | start.hidden = !(Online.lobby && Online.me && Online.lobby.host_id === Online.me.id); |
| 1297 | start.disabled = !Online.lobby.can_start; |
| 1298 | var hint = document.getElementById('lobby-hint'); |
| 1299 | if (Online.lobby.status === 'playing') |
| 1300 | { hint.textContent = 'Het spel start…'; } |
| 1301 | else if (players.length < 2) |
| 1302 | { hint.textContent = 'Wacht op minstens één andere speler. Deel de code.'; } |
| 1303 | else if (!Online.lobby.all_ready) |
| 1304 | { hint.textContent = 'Iedereen moet op Gereed drukken. De host start daarna.'; } |
| 1305 | else if (Online.lobby.can_start) |
| 1306 | { hint.textContent = 'Iedereen is gereed. Druk op Starten.'; } |
| 1307 | else |
| 1308 | { hint.textContent = 'Wachten tot de host start.'; } |
| 1309 | return; |
| 1310 | } |
| 1311 | setOnlineStatus('Hoi ' + Online.me.name + '. Maak een lobby of val in met een code.'); |
| 1312 | showOnly('online-home'); |
| 1313 | document.getElementById('me-name').textContent = Online.me.name; |
| 1314 | document.getElementById('local-login').hidden = !Online.local; |
| 1315 | } |
| 1316 | |
| 1317 | function escapeText (s) |
| 1318 | { |
| 1319 | return String(s) |
| 1320 | .replace(/&/g, '&') |
| 1321 | .replace(/</g, '<') |
| 1322 | .replace(/>/g, '>') |
| 1323 | .replace(/"/g, '"'); |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | function selectedPlanetId () |
| 1328 | { |
| 1329 | if (app.selectedPlanetId) |
| 1330 | { return app.selectedPlanetId; } |
| 1331 | return 'terra'; |
| 1332 | } |
| 1333 | |
| 1334 | function restoreSelectedPlanet () |
| 1335 | { |
| 1336 | var id = 'terra'; |
| 1337 | try |
| 1338 | { id = localStorage.getItem('meteors-planet') || 'terra'; } |
| 1339 | catch (e) {} |
| 1340 | setSelectedPlanet(id, true); |
| 1341 | } |
| 1342 | |
| 1343 | function setSelectedPlanet (id, silent) |
| 1344 | { |
| 1345 | var all = Planet.allPlanets(); |
| 1346 | var found = null; |
| 1347 | for (var i = 0; i < all.length; i++) |
| 1348 | { |
| 1349 | if (all[i].id === id) |
| 1350 | { found = all[i]; break; } |
| 1351 | } |
| 1352 | if (!found) |
| 1353 | { found = Planet.defaultPlanet(); } |
| 1354 | app.selectedPlanetId = found.id; |
| 1355 | try |
| 1356 | { localStorage.setItem('meteors-planet', found.id); } |
| 1357 | catch (e) {} |
| 1358 | if (!silent) |
| 1359 | { |
| 1360 | var grid = document.getElementById('planet-grid'); |
| 1361 | if (grid && !grid.hidden && grid.children.length) |
| 1362 | { renderPlanetGrid('planet-grid', false); } |
| 1363 | var lobby = document.getElementById('lobby-planet-grid'); |
| 1364 | if (lobby && lobby.children.length) |
| 1365 | { renderPlanetGrid('lobby-planet-grid', true); } |
| 1366 | } |
| 1367 | return found; |
| 1368 | } |
| 1369 | |
| 1370 | function openPlanetSelect (context) |
| 1371 | { |
| 1372 | if (app.mode === 'playing') |
| 1373 | { return; } |
| 1374 | app.planetSelectContext = context || 'solo'; |
| 1375 | app.mode = 'planet-select'; |
| 1376 | hideEl('menu'); |
| 1377 | hideEl('online-panel'); |
| 1378 | hideEl('game-over'); |
| 1379 | showEl('planet-select'); |
| 1380 | renderPlanetGrid('planet-grid', false); |
| 1381 | var go = document.getElementById('btn-planet-go'); |
| 1382 | if (go) |
| 1383 | { go.textContent = 'Spelen'; } |
| 1384 | } |
| 1385 | |
| 1386 | function renderPlanetGrid (containerId, compact) |
| 1387 | { |
| 1388 | var grid = document.getElementById(containerId); |
| 1389 | if (!grid) |
| 1390 | { return; } |
| 1391 | var current = selectedPlanetId(); |
| 1392 | var all = Planet.allPlanets(); |
| 1393 | grid.innerHTML = ''; |
| 1394 | if (compact) |
| 1395 | { grid.classList.add('compact'); } |
| 1396 | for (var i = 0; i < all.length; i++) |
| 1397 | { |
| 1398 | var pl = all[i]; |
| 1399 | var btn = document.createElement('button'); |
| 1400 | btn.type = 'button'; |
| 1401 | btn.className = 'planet-card' + (pl.id === current ? ' selected' : ''); |
| 1402 | btn.setAttribute('role', 'listitem'); |
| 1403 | btn.dataset.planet = pl.id; |
| 1404 | var swatches = ''; |
| 1405 | for (var c = 0; c < pl.colors.length; c++) |
| 1406 | { |
| 1407 | swatches += '<img src="' + escapeText(pl.colors[c].image) + '" alt="' |
| 1408 | + escapeText(pl.colors[c].name) + '" title="' + escapeText(pl.colors[c].name) + '">'; |
| 1409 | } |
| 1410 | btn.innerHTML = '<div class="planet-title">' + escapeText(pl.name) + '</div>' |
| 1411 | + '<div class="planet-people">' + escapeText(pl.people) + '</div>' |
| 1412 | + '<div class="planet-nums">' + escapeText(Planet.formatStats(pl)) + '</div>' |
| 1413 | + '<div class="planet-swatches">' + swatches + '</div>'; |
| 1414 | btn.addEventListener('click', (function (id, isCompact) |
| 1415 | { |
| 1416 | return function () |
| 1417 | { onPlanetPicked(id, isCompact); }; |
| 1418 | })(pl.id, !!compact)); |
| 1419 | grid.appendChild(btn); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | function onPlanetPicked (id, fromLobby) |
| 1424 | { |
| 1425 | setSelectedPlanet(id); |
| 1426 | if (fromLobby && typeof Online !== 'undefined' && Online.lobby) |
| 1427 | { |
| 1428 | Online.setPlanet(id).then(function () |
| 1429 | { renderOnline(); }); |
| 1430 | } |
| 1431 | else |
| 1432 | { |
| 1433 | renderPlanetGrid('planet-grid', false); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | function confirmPlanetSelect () |
| 1438 | { |
| 1439 | var planet = setSelectedPlanet(selectedPlanetId()); |
| 1440 | hideEl('planet-select'); |
| 1441 | if (app.planetSelectContext === 'online') |
| 1442 | { |
| 1443 | openOnlinePanel(); |
| 1444 | return; |
| 1445 | } |
| 1446 | startPlay({ planet: planet }); |
| 1447 | } |
| 1448 | |
| 1449 | function cancelPlanetSelect () |
| 1450 | { |
| 1451 | hideEl('planet-select'); |
| 1452 | app.mode = 'menu'; |
| 1453 | showEl('menu'); |
| 1454 | } |
| 1455 | |
| 1456 | function bindPlanetSelect () |
| 1457 | { |
| 1458 | var go = document.getElementById('btn-planet-go'); |
| 1459 | var back = document.getElementById('btn-planet-back'); |
| 1460 | if (go) |
| 1461 | { |
| 1462 | go.addEventListener('click', confirmPlanetSelect); |
| 1463 | } |
| 1464 | if (back) |
| 1465 | { |
| 1466 | back.addEventListener('click', cancelPlanetSelect); |
| 1467 | } |
| 1468 | } |
projects-timfalken-com meteors