| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Online play: talks to api.php (cookie session), polls lobby and game |
| 5 | * state, and draws opponent miniatures plus flying-block tweens. |
| 6 | * Real-time sync is not required; a 0.75 s poll is enough. |
| 7 | */ |
| 8 | |
| 9 | var Online = { |
| 10 | POLL_MS: 750, |
| 11 | DEFAULT_MISS_CHANCE: 0.30, |
| 12 | FLIGHT_DURATION: 1.2, |
| 13 | configured: false, |
| 14 | local: false, |
| 15 | csrf: '', |
| 16 | me: null, |
| 17 | lobby: null, |
| 18 | incoming: [], |
| 19 | shotResults: [], |
| 20 | error: '', |
| 21 | _timer: null, |
| 22 | _pendingShots: [], |
| 23 | _flights: [], |
| 24 | _lastStatus: null, |
| 25 | onChange: null, |
| 26 | onGameStart: null, |
| 27 | onIncoming: null, |
| 28 | onFinished: null, |
| 29 | onLobbyReturn: null |
| 30 | }; |
| 31 | |
| 32 | Online._headers = function (json) |
| 33 | { |
| 34 | var h = { 'Accept': 'application/json' }; |
| 35 | if (json) |
| 36 | { h['Content-Type'] = 'application/json'; } |
| 37 | if (this.csrf) |
| 38 | { h['X-CSRF-Token'] = this.csrf; } |
| 39 | return h; |
| 40 | }; |
| 41 | |
| 42 | Online._apply = function (data) |
| 43 | { |
| 44 | if (!data) |
| 45 | { return; } |
| 46 | if (typeof data.configured === 'boolean') |
| 47 | { this.configured = data.configured; } |
| 48 | if (typeof data.local === 'boolean') |
| 49 | { this.local = data.local; } |
| 50 | if (data.csrf) |
| 51 | { this.csrf = data.csrf; } |
| 52 | if (data.me !== undefined) |
| 53 | { this.me = data.me; } |
| 54 | if (data.lobby !== undefined) |
| 55 | { this.lobby = data.lobby; } |
| 56 | if (data.incoming) |
| 57 | { this.incoming = data.incoming; } |
| 58 | if (data.shot_results) |
| 59 | { this.shotResults = data.shot_results; } |
| 60 | if (data.miss_chance_default != null) |
| 61 | { this.DEFAULT_MISS_CHANCE = data.miss_chance_default; } |
| 62 | if (data.flight_duration != null) |
| 63 | { this.FLIGHT_DURATION = data.flight_duration; } |
| 64 | if (data.error) |
| 65 | { this.error = data.error; } |
| 66 | else if (data.ok) |
| 67 | { this.error = ''; } |
| 68 | }; |
| 69 | |
| 70 | Online.request = function (action, body) |
| 71 | { |
| 72 | var self = this; |
| 73 | var isGet = action === 'me' || action === ''; |
| 74 | var url = 'api.php?action=' + encodeURIComponent(action || 'me'); |
| 75 | var opts = { |
| 76 | method: isGet && !body ? 'GET' : 'POST', |
| 77 | credentials: 'same-origin', |
| 78 | headers: this._headers(!!body || !isGet) |
| 79 | }; |
| 80 | if (opts.method === 'POST') |
| 81 | { |
| 82 | var payload = body ? Object.assign({ action: action }, body) : { action: action }; |
| 83 | opts.body = JSON.stringify(payload); |
| 84 | } |
| 85 | return fetch(url, opts).then(function (res) |
| 86 | { |
| 87 | return res.json().then(function (data) |
| 88 | { |
| 89 | data = data || {}; |
| 90 | if (!res.ok && !data.error) |
| 91 | { data.error = 'De server gaf ' + res.status; data.ok = false; } |
| 92 | self._apply(data); |
| 93 | return data; |
| 94 | }, function () |
| 95 | { |
| 96 | var fail = { ok: false, error: 'Geen geldig antwoord van de server.' }; |
| 97 | self.error = fail.error; |
| 98 | return fail; |
| 99 | }); |
| 100 | }).catch(function (err) |
| 101 | { |
| 102 | var fail = { ok: false, error: 'Geen verbinding met de server.' }; |
| 103 | self.error = fail.error; |
| 104 | return fail; |
| 105 | }); |
| 106 | }; |
| 107 | |
| 108 | Online.refresh = function () |
| 109 | { |
| 110 | return this.request('me'); |
| 111 | }; |
| 112 | |
| 113 | Online.testLogin = function (who) |
| 114 | { |
| 115 | return this.request('test_login', { user: who }); |
| 116 | }; |
| 117 | |
| 118 | Online.createLobby = function (planetId) |
| 119 | { |
| 120 | return this.request('lobby_create', { |
| 121 | miss_chance: this.DEFAULT_MISS_CHANCE, |
| 122 | planet_id: planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra') |
| 123 | }); |
| 124 | }; |
| 125 | |
| 126 | Online.joinLobby = function (code, planetId) |
| 127 | { |
| 128 | return this.request('lobby_join', { |
| 129 | code: code, |
| 130 | planet_id: planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra') |
| 131 | }); |
| 132 | }; |
| 133 | |
| 134 | Online.setPlanet = function (planetId) |
| 135 | { |
| 136 | return this.request('lobby_set_planet', { planet_id: planetId }); |
| 137 | }; |
| 138 | |
| 139 | Online.leaveLobby = function () |
| 140 | { |
| 141 | return this.request('lobby_leave'); |
| 142 | }; |
| 143 | |
| 144 | Online.setReady = function (ready) |
| 145 | { |
| 146 | return this.request('lobby_ready', { ready: !!ready }); |
| 147 | }; |
| 148 | |
| 149 | Online.startGame = function () |
| 150 | { |
| 151 | var self = this; |
| 152 | var prev = this.lobby && this.lobby.status; |
| 153 | return this.request('lobby_start').then(function (data) |
| 154 | { |
| 155 | self._handleTransitions(prev, data); |
| 156 | return data; |
| 157 | }); |
| 158 | }; |
| 159 | |
| 160 | Online.queueShots = function (shots) |
| 161 | { |
| 162 | for (var i = 0; i < shots.length; i++) |
| 163 | { |
| 164 | this._pendingShots.push({ |
| 165 | client_id: String(shots[i].id), |
| 166 | color: shots[i].color, |
| 167 | col: shots[i].col, |
| 168 | y: shots[i].y |
| 169 | }); |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | Online.poll = function (opts) |
| 174 | { |
| 175 | opts = opts || {}; |
| 176 | var body = {}; |
| 177 | if (opts.snapshot) |
| 178 | { body.snapshot = opts.snapshot; } |
| 179 | if (opts.score != null) |
| 180 | { body.score = opts.score; } |
| 181 | if (opts.eliminated) |
| 182 | { body.eliminated = true; } |
| 183 | if (this._pendingShots.length) |
| 184 | { |
| 185 | body.outgoing_shots = this._pendingShots.splice(0, this._pendingShots.length); |
| 186 | } |
| 187 | var self = this; |
| 188 | var prevStatus = this.lobby && this.lobby.status; |
| 189 | return this.request('poll', body).then(function (data) |
| 190 | { |
| 191 | self._handleTransitions(prevStatus, data); |
| 192 | if (self.onChange) |
| 193 | { self.onChange(self); } |
| 194 | return data; |
| 195 | }); |
| 196 | }; |
| 197 | |
| 198 | Online._handleTransitions = function (prevStatus, data) |
| 199 | { |
| 200 | var lobby = this.lobby; |
| 201 | var status = lobby && lobby.status; |
| 202 | if (status === 'playing' && prevStatus !== 'playing' && this.onGameStart) |
| 203 | { this.onGameStart(lobby); } |
| 204 | if (data && data.incoming && data.incoming.length && this.onIncoming) |
| 205 | { this.onIncoming(data.incoming); } |
| 206 | // Own outgoing results (immediate) and lobby-wide recent flights |
| 207 | // (so every client can draw opponent and enemy-to-enemy arcs, t1125). |
| 208 | var flights = []; |
| 209 | if (data && data.shot_results && data.shot_results.length) |
| 210 | { flights = flights.concat(data.shot_results); } |
| 211 | if (data && data.shot_flights && data.shot_flights.length) |
| 212 | { flights = flights.concat(data.shot_flights); } |
| 213 | if (flights.length) |
| 214 | { this.spawnFlights(flights, this._flightLocals); } |
| 215 | if (status === 'finished' && prevStatus !== 'finished' && this.onFinished) |
| 216 | { this.onFinished(lobby); } |
| 217 | if (status === 'waiting' && prevStatus === 'finished' && this.onLobbyReturn) |
| 218 | { this.onLobbyReturn(lobby); } |
| 219 | }; |
| 220 | |
| 221 | Online.startPolling = function (getPayload) |
| 222 | { |
| 223 | this.stopPolling(); |
| 224 | var self = this; |
| 225 | var tick = function () |
| 226 | { |
| 227 | var payload = getPayload ? getPayload() : {}; |
| 228 | self.poll(payload); |
| 229 | }; |
| 230 | tick(); |
| 231 | this._timer = setInterval(tick, this.POLL_MS); |
| 232 | }; |
| 233 | |
| 234 | Online.stopPolling = function () |
| 235 | { |
| 236 | if (this._timer) |
| 237 | { |
| 238 | clearInterval(this._timer); |
| 239 | this._timer = null; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | Online.loginUrl = function () |
| 244 | { |
| 245 | return 'auth.php'; |
| 246 | }; |
| 247 | |
| 248 | Online.logoutUrl = function () |
| 249 | { |
| 250 | return 'logout.php'; |
| 251 | }; |
| 252 | |
| 253 | Online.opponents = function () |
| 254 | { |
| 255 | if (!this.lobby || !this.lobby.players) |
| 256 | { return []; } |
| 257 | return this.lobby.players.filter(function (p) { return !p.is_me; }); |
| 258 | }; |
| 259 | |
| 260 | Online.aliveOpponents = function () |
| 261 | { |
| 262 | return this.opponents().filter(function (p) { return !p.eliminated; }); |
| 263 | }; |
| 264 | |
| 265 | Online.mePlayer = function () |
| 266 | { |
| 267 | if (!this.lobby || !this.lobby.players) |
| 268 | { return null; } |
| 269 | for (var i = 0; i < this.lobby.players.length; i++) |
| 270 | { |
| 271 | if (this.lobby.players[i].is_me) |
| 272 | { return this.lobby.players[i]; } |
| 273 | } |
| 274 | return null; |
| 275 | }; |
| 276 | |
| 277 | /* ---------- miniatures --------------------------------------------- */ |
| 278 | |
| 279 | Online.renderMinis = function (container) |
| 280 | { |
| 281 | if (!container) |
| 282 | { return; } |
| 283 | var opponents = this.opponents(); |
| 284 | var existing = {}; |
| 285 | var kids = container.querySelectorAll('[data-player]'); |
| 286 | for (var i = 0; i < kids.length; i++) |
| 287 | { existing[kids[i].getAttribute('data-player')] = kids[i]; } |
| 288 | |
| 289 | var seen = Object.create(null); |
| 290 | for (var i = 0; i < opponents.length; i++) |
| 291 | { |
| 292 | var p = opponents[i]; |
| 293 | seen[p.id] = true; |
| 294 | var el = existing[p.id]; |
| 295 | if (!el) |
| 296 | { |
| 297 | el = document.createElement('div'); |
| 298 | el.className = 'mini'; |
| 299 | el.setAttribute('data-player', p.id); |
| 300 | el.innerHTML = '<div class="mini-name"></div>' |
| 301 | + '<canvas class="mini-canvas" width="80" height="104"></canvas>' |
| 302 | + '<div class="mini-meta"></div>'; |
| 303 | container.appendChild(el); |
| 304 | } |
| 305 | var mz = (typeof app !== 'undefined' && app.miniLayout && app.miniLayout.mini) |
| 306 | ? app.miniLayout.mini |
| 307 | : { w: 80, h: 104 }; |
| 308 | var canvas = el.querySelector('canvas'); |
| 309 | if (canvas && (canvas.width !== mz.w || canvas.height !== mz.h)) |
| 310 | { |
| 311 | canvas.width = mz.w; |
| 312 | canvas.height = mz.h; |
| 313 | } |
| 314 | if (canvas) |
| 315 | { |
| 316 | canvas.style.width = mz.w + 'px'; |
| 317 | canvas.style.height = mz.h + 'px'; |
| 318 | } |
| 319 | el.classList.toggle('eliminated', !!p.eliminated); |
| 320 | el.querySelector('.mini-name').textContent = p.name + (p.is_host ? ' (host)' : ''); |
| 321 | el.querySelector('.mini-meta').textContent = p.eliminated |
| 322 | ? ('Uit' + (p.place ? ' · ' + p.place + 'e' : '')) |
| 323 | : ('Punten ' + (p.score || 0)); |
| 324 | this._drawMini(el.querySelector('canvas'), p.snapshot, p.planet); |
| 325 | } |
| 326 | for (var id in existing) |
| 327 | { |
| 328 | if (!seen[id]) |
| 329 | { existing[id].remove(); } |
| 330 | } |
| 331 | if (typeof applyMinisLayout === 'function') |
| 332 | { applyMinisLayout(); } |
| 333 | }; |
| 334 | |
| 335 | var MINI_COLORS = ['#d4553a', '#3a9ad4', '#4caf6a', '#e6c04a', '#b86ad4']; |
| 336 | |
| 337 | function miniColor (planet, color) |
| 338 | { |
| 339 | if (planet && planet.colors && planet.colors[color] && planet.colors[color].hex) |
| 340 | { return planet.colors[color].hex; } |
| 341 | return MINI_COLORS[color] || '#888'; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | Online._drawMini = function (canvas, snapshot, planet) |
| 346 | { |
| 347 | if (!canvas) |
| 348 | { return; } |
| 349 | var ctx = canvas.getContext('2d'); |
| 350 | var w = 10; |
| 351 | var h = 13; |
| 352 | var tw = canvas.width / w; |
| 353 | var th = canvas.height / h; |
| 354 | ctx.fillStyle = '#0c1020'; |
| 355 | ctx.fillRect(0, 0, canvas.width, canvas.height); |
| 356 | ctx.strokeStyle = 'rgba(255,255,255,0.06)'; |
| 357 | ctx.lineWidth = 1; |
| 358 | for (var c = 0; c <= w; c++) |
| 359 | { |
| 360 | ctx.beginPath(); |
| 361 | ctx.moveTo(c * tw + 0.5, 0); |
| 362 | ctx.lineTo(c * tw + 0.5, canvas.height); |
| 363 | ctx.stroke(); |
| 364 | } |
| 365 | for (var r = 0; r <= h; r++) |
| 366 | { |
| 367 | ctx.beginPath(); |
| 368 | ctx.moveTo(0, r * th + 0.5); |
| 369 | ctx.lineTo(canvas.width, r * th + 0.5); |
| 370 | ctx.stroke(); |
| 371 | } |
| 372 | if (!snapshot || !snapshot.blocks) |
| 373 | { return; } |
| 374 | // t1123: cheap sine pulse behind blocks of nearly-full columns. |
| 375 | if (typeof GameCore !== 'undefined' && GameCore.warnColumnsFromSnapshot) |
| 376 | { |
| 377 | var warns = GameCore.warnColumnsFromSnapshot(snapshot); |
| 378 | var pulse = GameCore.columnWarnPulse(); |
| 379 | for (var wc = 0; wc < warns.length; wc++) |
| 380 | { |
| 381 | if (!warns[wc]) |
| 382 | { continue; } |
| 383 | ctx.fillStyle = 'rgba(220, 36, 36,' + pulse + ')'; |
| 384 | ctx.fillRect(wc * tw, 0, tw, canvas.height); |
| 385 | } |
| 386 | } |
| 387 | var blocks = snapshot.blocks; |
| 388 | for (var i = 0; i < blocks.length; i++) |
| 389 | { |
| 390 | var b = blocks[i]; |
| 391 | var x = b.col * tw; |
| 392 | var y = canvas.height - (b.y + 1) * th; |
| 393 | if (b.state === 'locked') |
| 394 | { ctx.fillStyle = '#3a3d4a'; } |
| 395 | else if (b.color < 0) |
| 396 | { ctx.fillStyle = '#5a5d66'; } |
| 397 | else |
| 398 | { ctx.fillStyle = miniColor(planet, b.color); } |
| 399 | ctx.fillRect(x + 0.5, y + 0.5, tw - 1, th - 1); |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | Online.miniElement = function (playerId) |
| 404 | { |
| 405 | return document.querySelector('#minis .mini[data-player="' + CSS.escape(playerId) + '"]'); |
| 406 | }; |
| 407 | |
| 408 | /* ---------- flight tweens ------------------------------------------ */ |
| 409 | |
| 410 | function colorSpriteUrl (color, planetId) |
| 411 | { |
| 412 | if (color < 0) |
| 413 | { return 'images/block-gray.png'; } |
| 414 | var planet = null; |
| 415 | if (typeof Planet !== 'undefined') |
| 416 | { planet = Planet.planetById(planetId || (typeof selectedPlanetId === 'function' ? selectedPlanetId() : 'terra')); } |
| 417 | if (planet && planet.colors && planet.colors[color] && planet.colors[color].image) |
| 418 | { return planet.colors[color].image; } |
| 419 | return 'images/block-gray.png'; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Animate shot results. `locals` is a map of client_id → {col, y, color, |
| 424 | * startX, startY} in viewport pixels, filled by the view just after |
| 425 | * consumeOutgoingShots. |
| 426 | */ |
| 427 | Online.playerId = function () |
| 428 | { |
| 429 | if (typeof app !== 'undefined' && app.offline) |
| 430 | { return 'player'; } |
| 431 | if (this.lobby && this.lobby.players) |
| 432 | { |
| 433 | for (var i = 0; i < this.lobby.players.length; i++) |
| 434 | { |
| 435 | if (this.lobby.players[i].is_me) |
| 436 | { return this.lobby.players[i].id; } |
| 437 | } |
| 438 | } |
| 439 | return 'player'; |
| 440 | }; |
| 441 | |
| 442 | /** |
| 443 | * Classify a shot for visual treatment (t1125). |
| 444 | * player-to-enemy, enemy-to-player, enemy-to-enemy, |
| 445 | * player-miss, enemy-miss. |
| 446 | */ |
| 447 | Online.flightKind = function (fromId, targetId, miss) |
| 448 | { |
| 449 | var me = this.playerId(); |
| 450 | var fromMe = !fromId || fromId === me || fromId === 'player'; |
| 451 | if (miss) |
| 452 | { return fromMe ? 'player-miss' : 'enemy-miss'; } |
| 453 | if (fromMe) |
| 454 | { return 'player-to-enemy'; } |
| 455 | if (targetId === me || targetId === 'player') |
| 456 | { return 'enemy-to-player'; } |
| 457 | return 'enemy-to-enemy'; |
| 458 | }; |
| 459 | |
| 460 | /** |
| 461 | * Parabolic arc: linear in x, a rise of `height` pixels above the |
| 462 | * chord at mid-flight (screen y grows downward, so we subtract). |
| 463 | */ |
| 464 | Online.arcPoint = function (start, end, u, height) |
| 465 | { |
| 466 | var t = u < 0 ? 0 : (u > 1 ? 1 : u); |
| 467 | var x = start.x + (end.x - start.x) * t; |
| 468 | var y = start.y + (end.y - start.y) * t - 4 * height * t * (1 - t); |
| 469 | return { x: x, y: y }; |
| 470 | }; |
| 471 | |
| 472 | Online.arcHeight = function (start, end) |
| 473 | { |
| 474 | var dx = end.x - start.x; |
| 475 | var dy = end.y - start.y; |
| 476 | var dist = Math.sqrt(dx * dx + dy * dy); |
| 477 | return Math.max(48, Math.min(220, dist * 0.28)); |
| 478 | }; |
| 479 | |
| 480 | Online.easeInOutCubic = function (t) |
| 481 | { |
| 482 | if (t < 0.5) |
| 483 | { return 4 * t * t * t; } |
| 484 | var u = -2 * t + 2; |
| 485 | return 1 - (u * u * u) / 2; |
| 486 | }; |
| 487 | |
| 488 | Online._miniCellSize = function (playerId) |
| 489 | { |
| 490 | var el = this.miniElement(playerId); |
| 491 | var canvas = el && el.querySelector('canvas'); |
| 492 | if (canvas) |
| 493 | { |
| 494 | var r = canvas.getBoundingClientRect(); |
| 495 | return Math.max(6, r.width / 10); |
| 496 | } |
| 497 | return 10; |
| 498 | }; |
| 499 | |
| 500 | Online._fullTileSize = function () |
| 501 | { |
| 502 | if (typeof app !== 'undefined' && app.tile) |
| 503 | { return app.tile; } |
| 504 | return 40; |
| 505 | }; |
| 506 | |
| 507 | Online._miniAnchor = function (playerId, dir) |
| 508 | { |
| 509 | var el = this.miniElement(playerId); |
| 510 | var canvas = el && (el.querySelector ? el.querySelector('canvas') : null); |
| 511 | var box = canvas || el; |
| 512 | if (!box) |
| 513 | { return { x: window.innerWidth / 2, y: 48 }; } |
| 514 | var r = box.getBoundingClientRect(); |
| 515 | if (dir === 'out') |
| 516 | { return { x: r.left + r.width / 2, y: r.top + 4 }; } |
| 517 | return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; |
| 518 | }; |
| 519 | |
| 520 | Online._fieldTop = function (col) |
| 521 | { |
| 522 | return this._flightStart(col); |
| 523 | }; |
| 524 | |
| 525 | /** |
| 526 | * Animate shot results. `locals` is a map of client_id → {col, y, color, |
| 527 | * startX, startY, from_id} in viewport pixels. |
| 528 | * |
| 529 | * Visual-only (t1125): a parabolic arc whose scale goes from mini-cell |
| 530 | * to full tile (or the reverse). Enemy-to-enemy arcs stay small and |
| 531 | * are drawn behind the player's field. Delivery timing is unchanged; |
| 532 | * a late-seen flight starts mid-arc via `age`. |
| 533 | */ |
| 534 | Online.spawnFlights = function (results, locals) |
| 535 | { |
| 536 | locals = locals || this._flightLocals || {}; |
| 537 | var front = document.getElementById('flights'); |
| 538 | var back = document.getElementById('flights-back') || front; |
| 539 | if (!front) |
| 540 | { return; } |
| 541 | if (!this._flightSeen) |
| 542 | { this._flightSeen = Object.create(null); } |
| 543 | var now = performance.now(); |
| 544 | var full = this._fullTileSize(); |
| 545 | for (var i = 0; i < results.length; i++) |
| 546 | { |
| 547 | var r = results[i]; |
| 548 | var sid = String(r.id != null ? r.id : (r.client_id || ('tmp-' + i))); |
| 549 | if (this._flightSeen[sid]) |
| 550 | { continue; } |
| 551 | this._flightSeen[sid] = now; |
| 552 | |
| 553 | var meta = locals[r.client_id] || locals[sid] || {}; |
| 554 | var fromId = r.from_id || r.from_user || meta.from_id || null; |
| 555 | var targetId = r.target_id != null ? r.target_id : r.to_user; |
| 556 | var miss = !!r.miss; |
| 557 | var kind = this.flightKind(fromId, targetId, miss); |
| 558 | var duration = (r.flight_duration || this.FLIGHT_DURATION) * 1000; |
| 559 | if (r.age != null && isFinite(r.age) && r.age * 1000 >= duration) |
| 560 | { continue; } |
| 561 | |
| 562 | var start; |
| 563 | if (meta.startX != null && isFinite(meta.startX)) |
| 564 | { start = { x: meta.startX, y: meta.startY }; } |
| 565 | else if (kind === 'player-to-enemy' || kind === 'player-miss') |
| 566 | { start = this._flightStart(meta.col != null ? meta.col : r.col); } |
| 567 | else |
| 568 | { start = this._miniAnchor(fromId, 'out'); } |
| 569 | |
| 570 | var end; |
| 571 | if (miss) |
| 572 | { end = this._missEnd(start, kind === 'enemy-miss'); } |
| 573 | else if (kind === 'enemy-to-player') |
| 574 | { end = this._fieldTop(null); } |
| 575 | else |
| 576 | { end = this._targetEnd(targetId, start); } |
| 577 | |
| 578 | var startScale; |
| 579 | var endScale; |
| 580 | var mini = this._miniCellSize(fromId || targetId) / full; |
| 581 | if (mini < 0.12) |
| 582 | { mini = 0.2; } |
| 583 | if (kind === 'player-to-enemy') |
| 584 | { |
| 585 | startScale = 1; |
| 586 | endScale = mini; |
| 587 | } |
| 588 | else if (kind === 'enemy-to-player') |
| 589 | { |
| 590 | startScale = mini; |
| 591 | endScale = 1; |
| 592 | } |
| 593 | else if (kind === 'enemy-to-enemy') |
| 594 | { |
| 595 | startScale = mini; |
| 596 | endScale = mini; |
| 597 | } |
| 598 | else if (kind === 'enemy-miss') |
| 599 | { |
| 600 | startScale = mini; |
| 601 | endScale = 0; |
| 602 | } |
| 603 | else |
| 604 | { |
| 605 | startScale = 1; |
| 606 | endScale = 0; |
| 607 | } |
| 608 | |
| 609 | var layer = (kind === 'enemy-to-enemy' && back) ? back : front; |
| 610 | var el = document.createElement('img'); |
| 611 | el.src = colorSpriteUrl(r.color != null ? r.color : meta.color); |
| 612 | el.alt = ''; |
| 613 | el.className = 'flight' + (kind === 'enemy-to-enemy' ? ' flight-behind' : ''); |
| 614 | el.style.width = full + 'px'; |
| 615 | el.style.height = full + 'px'; |
| 616 | layer.appendChild(el); |
| 617 | |
| 618 | var t0 = now; |
| 619 | if (r.age != null && isFinite(r.age) && r.age > 0) |
| 620 | { |
| 621 | var u0 = Math.min(0.95, (r.age * 1000) / duration); |
| 622 | t0 = now - u0 * duration; |
| 623 | } |
| 624 | var height = miss |
| 625 | ? Math.max(40, Math.min(160, Math.abs(end.y - start.y) * 0.35)) |
| 626 | : this.arcHeight(start, end); |
| 627 | this._flights.push({ |
| 628 | el: el, |
| 629 | start: start, |
| 630 | end: end, |
| 631 | ctrl: miss ? this._missCtrl(start, end) : null, |
| 632 | height: height, |
| 633 | t0: t0, |
| 634 | duration: duration, |
| 635 | spin: (Math.random() * 2 - 1) * 720, |
| 636 | miss: miss, |
| 637 | kind: kind, |
| 638 | startScale: startScale, |
| 639 | endScale: endScale, |
| 640 | angle: Math.random() * 360, |
| 641 | id: sid |
| 642 | }); |
| 643 | } |
| 644 | this._flightLocals = {}; |
| 645 | }; |
| 646 | |
| 647 | Online.rememberFlightLocal = function (shot, startX, startY) |
| 648 | { |
| 649 | if (!this._flightLocals) |
| 650 | { this._flightLocals = {}; } |
| 651 | this._flightLocals[String(shot.id)] = { |
| 652 | col: shot.col, |
| 653 | y: shot.y, |
| 654 | color: shot.color, |
| 655 | startX: startX, |
| 656 | startY: startY, |
| 657 | from_id: shot.from_id |
| 658 | }; |
| 659 | }; |
| 660 | |
| 661 | Online._flightStart = function (col) |
| 662 | { |
| 663 | var appRef = typeof app !== 'undefined' ? app : null; |
| 664 | if (appRef && appRef.scene && appRef.scene.canvas) |
| 665 | { |
| 666 | var canvas = appRef.scene.canvas; |
| 667 | var rect = canvas.getBoundingClientRect(); |
| 668 | var c = col == null ? 4.5 : col; |
| 669 | return { |
| 670 | x: rect.left + (c + 0.5) * (rect.width / 10), |
| 671 | y: rect.top + 4 |
| 672 | }; |
| 673 | } |
| 674 | var field = document.querySelector('canvas'); |
| 675 | if (field) |
| 676 | { |
| 677 | var rect = field.getBoundingClientRect(); |
| 678 | return { x: rect.left + rect.width / 2, y: rect.top }; |
| 679 | } |
| 680 | return { x: window.innerWidth / 2, y: window.innerHeight * 0.3 }; |
| 681 | }; |
| 682 | |
| 683 | Online._targetEnd = function (targetId, start) |
| 684 | { |
| 685 | var el = targetId && this.miniElement(targetId); |
| 686 | if (el) |
| 687 | { |
| 688 | var canvas = el.querySelector('canvas') || el; |
| 689 | var r = canvas.getBoundingClientRect(); |
| 690 | return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; |
| 691 | } |
| 692 | return { x: start.x + (Math.random() - 0.5) * 80, y: 40 }; |
| 693 | }; |
| 694 | |
| 695 | Online._missEnd = function (start, fromMini) |
| 696 | { |
| 697 | var w = window.innerWidth || 800; |
| 698 | var outward; |
| 699 | if (start.x < w * 0.4) |
| 700 | { outward = -(80 + Math.random() * w * 0.25); } |
| 701 | else if (start.x > w * 0.6) |
| 702 | { outward = (80 + Math.random() * w * 0.25); } |
| 703 | else |
| 704 | { outward = (Math.random() * 2 - 1) * w * 0.35; } |
| 705 | var lift = fromMini ? (80 + Math.random() * 160) : (120 + Math.random() * 220); |
| 706 | return { x: start.x + outward, y: start.y - lift }; |
| 707 | }; |
| 708 | |
| 709 | Online._missCtrl = function (start, end) |
| 710 | { |
| 711 | return { |
| 712 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 160, |
| 713 | y: Math.min(start.y, end.y) - 40 - Math.random() * 80 |
| 714 | }; |
| 715 | }; |
| 716 | |
| 717 | Online._hitCtrl = function (start, end) |
| 718 | { |
| 719 | return { |
| 720 | x: (start.x + end.x) / 2 + (Math.random() - 0.5) * 40, |
| 721 | y: (start.y + end.y) / 2 - 30 |
| 722 | }; |
| 723 | }; |
| 724 | |
| 725 | Online.tickFlights = function (now) |
| 726 | { |
| 727 | now = now || performance.now(); |
| 728 | var leftover = []; |
| 729 | for (var i = 0; i < this._flights.length; i++) |
| 730 | { |
| 731 | var f = this._flights[i]; |
| 732 | var raw = (now - f.t0) / f.duration; |
| 733 | if (raw >= 1) |
| 734 | { |
| 735 | if (f.el && f.el.parentNode) |
| 736 | { f.el.parentNode.removeChild(f.el); } |
| 737 | continue; |
| 738 | } |
| 739 | if (raw < 0) |
| 740 | { raw = 0; } |
| 741 | var u = this.easeInOutCubic(raw); |
| 742 | var x, y; |
| 743 | if (f.miss && f.ctrl) |
| 744 | { |
| 745 | var e = raw * raw; |
| 746 | var mt = 1 - e; |
| 747 | x = mt * mt * f.start.x + 2 * mt * e * f.ctrl.x + e * e * f.end.x; |
| 748 | y = mt * mt * f.start.y + 2 * mt * e * f.ctrl.y + e * e * f.end.y; |
| 749 | } |
| 750 | else |
| 751 | { |
| 752 | var pt = this.arcPoint(f.start, f.end, u, f.height || 80); |
| 753 | x = pt.x; |
| 754 | y = pt.y; |
| 755 | } |
| 756 | var scale = f.startScale + (f.endScale - f.startScale) * u; |
| 757 | if (f.miss) |
| 758 | { |
| 759 | var fadeU = raw * raw; |
| 760 | scale = f.startScale * (1 - fadeU); |
| 761 | } |
| 762 | var angle = f.angle + f.spin * raw; |
| 763 | f.el.style.transform = 'translate(' + x + 'px,' + y + 'px) translate(-50%,-50%) rotate(' |
| 764 | + angle + 'deg) scale(' + scale + ')'; |
| 765 | f.el.style.opacity = f.miss ? String(Math.max(0, 1 - raw * raw)) : '1'; |
| 766 | leftover.push(f); |
| 767 | } |
| 768 | this._flights = leftover; |
| 769 | if (this._flightSeen) |
| 770 | { |
| 771 | var cutoff = now - 8000; |
| 772 | var ids = Object.keys(this._flightSeen); |
| 773 | for (var k = 0; k < ids.length; k++) |
| 774 | { |
| 775 | if (this._flightSeen[ids[k]] < cutoff) |
| 776 | { delete this._flightSeen[ids[k]]; } |
| 777 | } |
| 778 | } |
| 779 | }; |
| 780 | |
| 781 | Online.clearFlights = function () |
| 782 | { |
| 783 | for (var i = 0; i < this._flights.length; i++) |
| 784 | { |
| 785 | var el = this._flights[i].el; |
| 786 | if (el && el.parentNode) |
| 787 | { el.parentNode.removeChild(el); } |
| 788 | } |
| 789 | this._flights = []; |
| 790 | this._flightLocals = {}; |
| 791 | this._flightSeen = Object.create(null); |
| 792 | }; |
| 793 | |
| 794 | if (typeof globalThis !== 'undefined') |
| 795 | { globalThis.Online = Online; } |
| 796 | if (typeof module !== 'undefined' && module.exports) |
| 797 | { module.exports = Online; } |
projects-timfalken-com meteors