| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Deterministic Meteors game core. No DOM, no rendering, no input devices. |
| 5 | * The view layer reads this state and translates pointer events into |
| 6 | * beginDrag / moveDrag / endDrag. |
| 7 | */ |
| 8 | |
| 9 | var PlanetMod = (typeof require !== 'undefined' && typeof module !== 'undefined') |
| 10 | ? require('./planet.js') |
| 11 | : (typeof Planet !== 'undefined' ? Planet : globalThis.Planet); |
| 12 | |
| 13 | var COLOR_GRAY = -1; |
| 14 | |
| 15 | var STATE_STILL = 'still'; |
| 16 | var STATE_FALLING = 'falling'; |
| 17 | var STATE_FLYING = 'flying'; |
| 18 | var STATE_SETTLING = 'settling'; |
| 19 | var STATE_LOCKED = 'locked'; |
| 20 | var STATE_MANUAL = 'manual'; |
| 21 | |
| 22 | var ALIGN_EPS = 0.15; |
| 23 | var SUBSTEP = 1 / 120; |
| 24 | var DRAG_LAUNCH_SLACK = 0.65; |
| 25 | // Upward pull (tiles past the press point) that launches an already-topmost block. |
| 26 | var LAUNCH_PULL = 0.35; |
| 27 | // Minimal accumulated vertical pointer travel (tiles) to swap one step. |
| 28 | // No hold delay: the first swap fires as soon as this distance is reached. |
| 29 | var SWAP_DISTANCE = 0.25; |
| 30 | // Grab a still block if the pointer is at most this far (tiles) from it. |
| 31 | var SNAP_Y = 0.65; |
| 32 | // Horizontal slop (tiles) for snapping to a neighbouring column on press. |
| 33 | var HORIZONTAL_SLOP = 0.35; |
| 34 | |
| 35 | function UnionFind () |
| 36 | { |
| 37 | this.parent = Object.create(null); |
| 38 | } |
| 39 | |
| 40 | UnionFind.prototype.add = function (id) |
| 41 | { |
| 42 | if (this.parent[id] == null) |
| 43 | { this.parent[id] = id; } |
| 44 | }; |
| 45 | |
| 46 | UnionFind.prototype.find = function (id) |
| 47 | { |
| 48 | if (this.parent[id] !== id) |
| 49 | { this.parent[id] = this.find(this.parent[id]); } |
| 50 | return this.parent[id]; |
| 51 | }; |
| 52 | |
| 53 | UnionFind.prototype.union = function (a, b) |
| 54 | { |
| 55 | this.add(a); |
| 56 | this.add(b); |
| 57 | var ra = this.find(a); |
| 58 | var rb = this.find(b); |
| 59 | if (ra !== rb) |
| 60 | { this.parent[rb] = ra; } |
| 61 | }; |
| 62 | |
| 63 | function GameCore (planet, rng) |
| 64 | { |
| 65 | this.planet = planet || PlanetMod.defaultPlanet(); |
| 66 | this.rng = rng || Math.random; |
| 67 | this.width = PlanetMod.FIELD_WIDTH; |
| 68 | this.height = PlanetMod.FIELD_HEIGHT; |
| 69 | this.blocks = []; |
| 70 | this.clusters = Object.create(null); |
| 71 | this.nextBlockId = 1; |
| 72 | this.nextClusterId = 1; |
| 73 | this.spawnTimer = 0; |
| 74 | this.spawnInterval = PlanetMod.SPAWN_INTERVAL; |
| 75 | this.settleTime = PlanetMod.SETTLE_TIME; |
| 76 | this.score = 0; |
| 77 | this.state = 'playing'; |
| 78 | this.drag = null; |
| 79 | this.gameOverTime = 0; |
| 80 | } |
| 81 | |
| 82 | GameCore.COLOR_GRAY = COLOR_GRAY; |
| 83 | GameCore.LAUNCH_PULL = LAUNCH_PULL; |
| 84 | GameCore.SWAP_DISTANCE = SWAP_DISTANCE; |
| 85 | GameCore.SNAP_Y = SNAP_Y; |
| 86 | GameCore.HORIZONTAL_SLOP = HORIZONTAL_SLOP; |
| 87 | GameCore.STATE_STILL = STATE_STILL; |
| 88 | GameCore.STATE_FALLING = STATE_FALLING; |
| 89 | GameCore.STATE_FLYING = STATE_FLYING; |
| 90 | GameCore.STATE_SETTLING = STATE_SETTLING; |
| 91 | GameCore.STATE_LOCKED = STATE_LOCKED; |
| 92 | GameCore.STATE_MANUAL = STATE_MANUAL; |
| 93 | |
| 94 | GameCore.prototype.randomColor = function () |
| 95 | { |
| 96 | return Math.floor(this.rng() * this.planet.colors.length) % this.planet.colors.length; |
| 97 | }; |
| 98 | |
| 99 | GameCore.prototype.createBlock = function (col, y, color, state) |
| 100 | { |
| 101 | var block = { |
| 102 | id: this.nextBlockId++, |
| 103 | col: col, |
| 104 | y: y, |
| 105 | color: color, |
| 106 | state: state || STATE_STILL, |
| 107 | vy: 0, |
| 108 | clusterId: null, |
| 109 | settleTime: 0, |
| 110 | flame: false, |
| 111 | pushedClusters: null, |
| 112 | manualTarget: 0, |
| 113 | manualDecel: 0, |
| 114 | manualPhase: null, |
| 115 | bouncing: false, |
| 116 | bounceAge: 0 |
| 117 | }; |
| 118 | this.blocks.push(block); |
| 119 | return block; |
| 120 | }; |
| 121 | |
| 122 | GameCore.prototype.removeBlock = function (block) |
| 123 | { |
| 124 | if (block.clusterId != null) |
| 125 | { this.leaveCluster(block); } |
| 126 | var index = this.blocks.indexOf(block); |
| 127 | if (index >= 0) |
| 128 | { this.blocks.splice(index, 1); } |
| 129 | }; |
| 130 | |
| 131 | GameCore.prototype.leaveCluster = function (block) |
| 132 | { |
| 133 | var id = block.clusterId; |
| 134 | block.clusterId = null; |
| 135 | block.flame = false; |
| 136 | if (id == null || !this.clusters[id]) |
| 137 | { return; } |
| 138 | var still = 0; |
| 139 | for (var i = 0; i < this.blocks.length; i++) |
| 140 | { |
| 141 | if (this.blocks[i].clusterId === id) |
| 142 | { still++; } |
| 143 | } |
| 144 | if (still === 0) |
| 145 | { delete this.clusters[id]; } |
| 146 | }; |
| 147 | |
| 148 | GameCore.prototype.blocksInColumn = function (col) |
| 149 | { |
| 150 | var out = []; |
| 151 | for (var i = 0; i < this.blocks.length; i++) |
| 152 | { |
| 153 | if (this.blocks[i].col === col) |
| 154 | { out.push(this.blocks[i]); } |
| 155 | } |
| 156 | out.sort(function (a, b) { return a.y - b.y; }); |
| 157 | return out; |
| 158 | }; |
| 159 | |
| 160 | GameCore.prototype.isGrounded = function (block) |
| 161 | { |
| 162 | return block.state === STATE_STILL |
| 163 | || block.state === STATE_SETTLING |
| 164 | || block.state === STATE_LOCKED; |
| 165 | }; |
| 166 | |
| 167 | GameCore.prototype.stackTop = function (col, ignore) |
| 168 | { |
| 169 | var top = 0; |
| 170 | for (var i = 0; i < this.blocks.length; i++) |
| 171 | { |
| 172 | var block = this.blocks[i]; |
| 173 | if (block.col !== col) |
| 174 | { continue; } |
| 175 | if (ignore && ignore(block)) |
| 176 | { continue; } |
| 177 | if (this.isGrounded(block)) |
| 178 | { |
| 179 | var surface = block.y + 1; |
| 180 | if (surface > top) |
| 181 | { top = surface; } |
| 182 | } |
| 183 | } |
| 184 | return top; |
| 185 | }; |
| 186 | |
| 187 | GameCore.prototype.isColumnLocked = function (col) |
| 188 | { |
| 189 | for (var i = 0; i < this.blocks.length; i++) |
| 190 | { |
| 191 | var block = this.blocks[i]; |
| 192 | if (block.col === col && block.state === STATE_LOCKED) |
| 193 | { return true; } |
| 194 | } |
| 195 | return false; |
| 196 | }; |
| 197 | |
| 198 | GameCore.prototype.isColumnStill = function (col) |
| 199 | { |
| 200 | if (this.isColumnLocked(col)) |
| 201 | { return false; } |
| 202 | for (var i = 0; i < this.blocks.length; i++) |
| 203 | { |
| 204 | var block = this.blocks[i]; |
| 205 | if (block.col !== col) |
| 206 | { continue; } |
| 207 | if (block.state === STATE_FLYING |
| 208 | || block.state === STATE_FALLING |
| 209 | || block.state === STATE_MANUAL |
| 210 | || block.state === STATE_SETTLING) |
| 211 | { return false; } |
| 212 | } |
| 213 | return true; |
| 214 | }; |
| 215 | |
| 216 | GameCore.prototype.groundedCount = function (col) |
| 217 | { |
| 218 | var n = 0; |
| 219 | for (var i = 0; i < this.blocks.length; i++) |
| 220 | { |
| 221 | if (this.blocks[i].col === col && this.isGrounded(this.blocks[i])) |
| 222 | { n++; } |
| 223 | } |
| 224 | return n; |
| 225 | }; |
| 226 | |
| 227 | GameCore.prototype.blockAt = function (col, row) |
| 228 | { |
| 229 | for (var i = 0; i < this.blocks.length; i++) |
| 230 | { |
| 231 | var block = this.blocks[i]; |
| 232 | if (block.col === col && Math.abs(block.y - row) < ALIGN_EPS) |
| 233 | { return block; } |
| 234 | } |
| 235 | return null; |
| 236 | }; |
| 237 | |
| 238 | GameCore.prototype.place = function (col, row, color, state) |
| 239 | { |
| 240 | return this.createBlock(col, row, color, state || STATE_STILL); |
| 241 | }; |
| 242 | |
| 243 | GameCore.prototype.lockedColumnCount = function () |
| 244 | { |
| 245 | var n = 0; |
| 246 | for (var c = 0; c < this.width; c++) |
| 247 | { |
| 248 | if (this.isColumnLocked(c)) |
| 249 | { n++; } |
| 250 | } |
| 251 | return n; |
| 252 | }; |
| 253 | |
| 254 | GameCore.prototype.startBounce = function (block) |
| 255 | { |
| 256 | if (!block) |
| 257 | { return; } |
| 258 | block.bouncing = true; |
| 259 | block.bounceAge = 0; |
| 260 | }; |
| 261 | |
| 262 | GameCore.prototype.advanceBounces = function (dt) |
| 263 | { |
| 264 | var duration = this.planet.bounceDuration; |
| 265 | for (var i = 0; i < this.blocks.length; i++) |
| 266 | { |
| 267 | var block = this.blocks[i]; |
| 268 | if (!block.bouncing) |
| 269 | { continue; } |
| 270 | block.bounceAge += dt; |
| 271 | if (duration <= 0 || block.bounceAge >= duration) |
| 272 | { |
| 273 | block.bouncing = false; |
| 274 | block.bounceAge = duration; |
| 275 | } |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | /** |
| 280 | * True if `block` rests on the floor or on a solid (still / settling / |
| 281 | * locked) block immediately below it in the same column. |
| 282 | */ |
| 283 | GameCore.prototype.hasSupport = function (block) |
| 284 | { |
| 285 | if (block.y <= 1e-9) |
| 286 | { return true; } |
| 287 | for (var i = 0; i < this.blocks.length; i++) |
| 288 | { |
| 289 | var other = this.blocks[i]; |
| 290 | if (other === block || other.col !== block.col) |
| 291 | { continue; } |
| 292 | if (!this.isGrounded(other)) |
| 293 | { continue; } |
| 294 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 295 | { return true; } |
| 296 | } |
| 297 | return false; |
| 298 | }; |
| 299 | |
| 300 | /** Highest flying block in this column whose top is at or below `block.y + 1`. */ |
| 301 | GameCore.prototype.highestFlyingBelow = function (block) |
| 302 | { |
| 303 | var best = null; |
| 304 | for (var i = 0; i < this.blocks.length; i++) |
| 305 | { |
| 306 | var other = this.blocks[i]; |
| 307 | if (other === block || other.col !== block.col) |
| 308 | { continue; } |
| 309 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 310 | { continue; } |
| 311 | var cluster = this.clusters[other.clusterId]; |
| 312 | if (!cluster || cluster.phase === 'landed') |
| 313 | { continue; } |
| 314 | // The falling block is at or above this flyer (or already overlapping). |
| 315 | if (block.y + 1e-9 < other.y) |
| 316 | { continue; } |
| 317 | if (!best || other.y > best.y) |
| 318 | { best = other; } |
| 319 | } |
| 320 | return best; |
| 321 | }; |
| 322 | |
| 323 | GameCore.prototype.highestFallingBelow = function (block) |
| 324 | { |
| 325 | var best = null; |
| 326 | for (var i = 0; i < this.blocks.length; i++) |
| 327 | { |
| 328 | var other = this.blocks[i]; |
| 329 | if (other === block || other.col !== block.col) |
| 330 | { continue; } |
| 331 | if (other.state !== STATE_FALLING) |
| 332 | { continue; } |
| 333 | if (other.y > block.y - 1e-9) |
| 334 | { continue; } |
| 335 | if (!best || other.y > best.y) |
| 336 | { best = other; } |
| 337 | } |
| 338 | return best; |
| 339 | }; |
| 340 | |
| 341 | /** |
| 342 | * Make an unsupported still block start falling from rest, so it eases |
| 343 | * into fallSpeed under normal gravity. Skips the block being dragged. |
| 344 | */ |
| 345 | /** |
| 346 | * Strip leftover launch / manual kinematics so a block that has just |
| 347 | * become still or falling starts from rest. Used after a landed grey |
| 348 | * cluster recolors, and whenever an unsupported still block drops. |
| 349 | */ |
| 350 | GameCore.prototype.clearFlightState = function (block) |
| 351 | { |
| 352 | block.vy = 0; |
| 353 | block.flame = false; |
| 354 | block.manualPhase = null; |
| 355 | block.manualTarget = null; |
| 356 | block.manualDecel = 0; |
| 357 | block.pushedClusters = null; |
| 358 | }; |
| 359 | |
| 360 | GameCore.prototype.releaseUnsupported = function () |
| 361 | { |
| 362 | for (var i = 0; i < this.blocks.length; i++) |
| 363 | { |
| 364 | var block = this.blocks[i]; |
| 365 | if (block.state !== STATE_STILL) |
| 366 | { continue; } |
| 367 | if (this.drag && this.drag.blockId === block.id) |
| 368 | { continue; } |
| 369 | if (this.hasSupport(block)) |
| 370 | { continue; } |
| 371 | this.leaveCluster(block); |
| 372 | this.clearFlightState(block); |
| 373 | block.state = STATE_FALLING; |
| 374 | } |
| 375 | }; |
| 376 | |
| 377 | GameCore.prototype.accelerateFall = function (block, dt) |
| 378 | { |
| 379 | var target = -this.planet.fallSpeed; |
| 380 | if (block.vy > target) |
| 381 | { block.vy = Math.max(target, block.vy - this.planet.fallAccel * dt); } |
| 382 | else |
| 383 | { block.vy = target; } |
| 384 | }; |
| 385 | |
| 386 | GameCore.prototype.landFallingBlock = function (block) |
| 387 | { |
| 388 | block.vy = 0; |
| 389 | block.flame = false; |
| 390 | this.startBounce(block); |
| 391 | if (block.color === COLOR_GRAY) |
| 392 | { |
| 393 | block.state = STATE_SETTLING; |
| 394 | block.settleTime = 0; |
| 395 | } |
| 396 | else |
| 397 | { block.state = STATE_STILL; } |
| 398 | }; |
| 399 | |
| 400 | /** |
| 401 | * Sit `block` on top of the flying cluster that `flyer` belongs to, and |
| 402 | * carry it along as cargo (no flame). Returns true if attached. |
| 403 | */ |
| 404 | GameCore.prototype.attachToFlyingCluster = function (block, flyer) |
| 405 | { |
| 406 | var cluster = this.clusters[flyer.clusterId]; |
| 407 | if (!cluster || cluster.phase === 'landed') |
| 408 | { return false; } |
| 409 | var roof = flyer.y + 1; |
| 410 | for (var i = 0; i < this.blocks.length; i++) |
| 411 | { |
| 412 | var other = this.blocks[i]; |
| 413 | if (other === block || other.col !== block.col) |
| 414 | { continue; } |
| 415 | if (other.clusterId !== cluster.id) |
| 416 | { continue; } |
| 417 | if (other.y + 1 > roof) |
| 418 | { roof = other.y + 1; } |
| 419 | } |
| 420 | block.y = roof; |
| 421 | block.state = STATE_FLYING; |
| 422 | block.clusterId = cluster.id; |
| 423 | block.vy = cluster.vy; |
| 424 | block.flame = false; |
| 425 | block.settleTime = 0; |
| 426 | return true; |
| 427 | }; |
| 428 | |
| 429 | GameCore.prototype.catchFallingOntoCluster = function (cluster, members) |
| 430 | { |
| 431 | if (!cluster || cluster.phase === 'landed' || cluster.phase === 'hold') |
| 432 | { return; } |
| 433 | // Snapshot falling blocks; attaching mutates state. |
| 434 | var falling = []; |
| 435 | for (var i = 0; i < this.blocks.length; i++) |
| 436 | { |
| 437 | if (this.blocks[i].state === STATE_FALLING) |
| 438 | { falling.push(this.blocks[i]); } |
| 439 | } |
| 440 | for (var i = 0; i < falling.length; i++) |
| 441 | { |
| 442 | var block = falling[i]; |
| 443 | var flyer = null; |
| 444 | for (var m = 0; m < members.length; m++) |
| 445 | { |
| 446 | var other = members[m]; |
| 447 | if (other.col !== block.col) |
| 448 | { continue; } |
| 449 | // Overlap or rest on this member's top. |
| 450 | if (block.y < other.y + 1 + 1e-9 && block.y + 1 > other.y - 1e-9) |
| 451 | { |
| 452 | if (!flyer || other.y > flyer.y) |
| 453 | { flyer = other; } |
| 454 | } |
| 455 | } |
| 456 | if (flyer) |
| 457 | { this.attachToFlyingCluster(block, flyer); } |
| 458 | } |
| 459 | }; |
| 460 | |
| 461 | GameCore.prototype.tick = function (dt) |
| 462 | { |
| 463 | if (this.state === 'gameover') |
| 464 | { |
| 465 | this.gameOverTime += Math.max(0, dt); |
| 466 | return; |
| 467 | } |
| 468 | if (dt <= 0) |
| 469 | { return; } |
| 470 | dt = Math.min(dt, 0.1); |
| 471 | |
| 472 | this.spawnTimer += dt; |
| 473 | while (this.spawnTimer >= this.spawnInterval) |
| 474 | { |
| 475 | this.spawnTimer -= this.spawnInterval; |
| 476 | this.trySpawn(); |
| 477 | } |
| 478 | |
| 479 | var left = dt; |
| 480 | while (left > 1e-12) |
| 481 | { |
| 482 | var step = Math.min(SUBSTEP, left); |
| 483 | this.integrateFalling(step); |
| 484 | this.integrateClusters(step); |
| 485 | this.mergeFlyingClusters(); |
| 486 | this.integrateManual(step); |
| 487 | this.cullOffTop(); |
| 488 | left -= step; |
| 489 | } |
| 490 | |
| 491 | this.releaseUnsupported(); |
| 492 | this.advanceSettle(dt); |
| 493 | // After a landed grey group is released, blocks that were sitting on |
| 494 | // hanging members are still STILL (the earlier pass ran while those |
| 495 | // members counted as support). Drop them from rest before matching, |
| 496 | // so they cannot form a mid-air match and shoot off the top. |
| 497 | this.releaseUnsupported(); |
| 498 | this.advanceBounces(dt); |
| 499 | if (!this.drag) |
| 500 | { this.resolveMatches(); } |
| 501 | this.checkLocks(); |
| 502 | this.checkGameOver(); |
| 503 | }; |
| 504 | |
| 505 | /** Step a stretch of time in fixed substeps. Useful for tests. */ |
| 506 | GameCore.prototype.step = function (seconds, dt) |
| 507 | { |
| 508 | dt = dt || SUBSTEP; |
| 509 | var t = 0; |
| 510 | while (t < seconds - 1e-15) |
| 511 | { |
| 512 | var s = Math.min(dt, seconds - t); |
| 513 | this.tick(s); |
| 514 | t += s; |
| 515 | } |
| 516 | }; |
| 517 | |
| 518 | GameCore.prototype.trySpawn = function () |
| 519 | { |
| 520 | if (this.state !== 'playing') |
| 521 | { return null; } |
| 522 | var eligible = []; |
| 523 | for (var col = 0; col < this.width; col++) |
| 524 | { |
| 525 | if (this.isColumnLocked(col)) |
| 526 | { continue; } |
| 527 | if (this.groundedCount(col) >= this.height) |
| 528 | { continue; } |
| 529 | var alreadyFalling = false; |
| 530 | var list = this.blocksInColumn(col); |
| 531 | for (var i = 0; i < list.length; i++) |
| 532 | { |
| 533 | if (list[i].state === STATE_FALLING) |
| 534 | { |
| 535 | alreadyFalling = true; |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | if (!alreadyFalling) |
| 540 | { eligible.push(col); } |
| 541 | } |
| 542 | if (!eligible.length) |
| 543 | { return null; } |
| 544 | var col = eligible[Math.floor(this.rng() * eligible.length) % eligible.length]; |
| 545 | var color = this.randomColor(); |
| 546 | var block = this.createBlock(col, this.height, color, STATE_FALLING); |
| 547 | block.vy = -this.planet.fallSpeed; |
| 548 | return block; |
| 549 | }; |
| 550 | |
| 551 | GameCore.prototype.integrateFalling = function (dt) |
| 552 | { |
| 553 | var falling = []; |
| 554 | for (var i = 0; i < this.blocks.length; i++) |
| 555 | { |
| 556 | if (this.blocks[i].state === STATE_FALLING) |
| 557 | { falling.push(this.blocks[i]); } |
| 558 | } |
| 559 | // Lowest first, so a stack of falling blocks rests on itself. |
| 560 | falling.sort(function (a, b) { return a.y - b.y; }); |
| 561 | for (var i = 0; i < falling.length; i++) |
| 562 | { |
| 563 | var block = falling[i]; |
| 564 | if (block.state !== STATE_FALLING) |
| 565 | { continue; } |
| 566 | this.accelerateFall(block, dt); |
| 567 | var nextY = block.y + block.vy * dt; |
| 568 | var flyer = this.highestFlyingBelow(block); |
| 569 | if (flyer && nextY <= flyer.y + 1 + 1e-9) |
| 570 | { |
| 571 | if (this.attachToFlyingCluster(block, flyer)) |
| 572 | { continue; } |
| 573 | } |
| 574 | var top = this.stackTop(block.col); |
| 575 | var below = this.highestFallingBelow(block); |
| 576 | if (below && below.y + 1 > top) |
| 577 | { top = below.y + 1; } |
| 578 | if (nextY <= top) |
| 579 | { |
| 580 | block.y = top; |
| 581 | this.landFallingBlock(block); |
| 582 | } |
| 583 | else |
| 584 | { block.y = nextY; } |
| 585 | } |
| 586 | }; |
| 587 | |
| 588 | GameCore.prototype.clusterMembers = function (clusterId) |
| 589 | { |
| 590 | var out = []; |
| 591 | for (var i = 0; i < this.blocks.length; i++) |
| 592 | { |
| 593 | if (this.blocks[i].clusterId === clusterId) |
| 594 | { out.push(this.blocks[i]); } |
| 595 | } |
| 596 | return out; |
| 597 | }; |
| 598 | |
| 599 | |
| 600 | /** |
| 601 | * Advance a jump (cluster or manual) by dt. Uses exact kinematics so a |
| 602 | * jump of height H peaks at exactly H after riseTime. Returns dy. |
| 603 | * Mutates body.vy and body.phase ('up' / 'down'). body.decel is the |
| 604 | * upward deceleration. After the apex, vy eases from 0 to -fallSpeed |
| 605 | * at planet.fallAccel, then stays at terminal fallSpeed. |
| 606 | */ |
| 607 | GameCore.prototype.advanceJump = function (body, dt) |
| 608 | { |
| 609 | var dy = 0; |
| 610 | var left = dt; |
| 611 | if (body.phase === 'up') |
| 612 | { |
| 613 | var decel = body.decel; |
| 614 | var vy = body.vy; |
| 615 | var tApex = decel > 1e-12 ? vy / decel : Infinity; |
| 616 | if (tApex <= left + 1e-15) |
| 617 | { |
| 618 | dy += vy * tApex - 0.5 * decel * tApex * tApex; |
| 619 | body.vy = 0; |
| 620 | body.phase = 'down'; |
| 621 | left -= tApex; |
| 622 | } |
| 623 | else |
| 624 | { |
| 625 | var vyNew = vy - decel * left; |
| 626 | dy += (vy + vyNew) * 0.5 * left; |
| 627 | body.vy = vyNew; |
| 628 | left = 0; |
| 629 | } |
| 630 | } |
| 631 | if (body.phase === 'up' && body.vy <= 1e-10) |
| 632 | { |
| 633 | body.vy = 0; |
| 634 | body.phase = 'down'; |
| 635 | } |
| 636 | if (body.phase === 'down' && left > 0) |
| 637 | { |
| 638 | var target = -this.planet.fallSpeed; |
| 639 | var accel = this.planet.fallAccel; |
| 640 | if (accel < 1e-12) |
| 641 | { |
| 642 | body.vy = target; |
| 643 | dy += body.vy * left; |
| 644 | } |
| 645 | else if (body.vy > target + 1e-12) |
| 646 | { |
| 647 | var tReach = (body.vy - target) / accel; |
| 648 | if (tReach <= left + 1e-15) |
| 649 | { |
| 650 | dy += (body.vy + target) * 0.5 * tReach; |
| 651 | body.vy = target; |
| 652 | left -= tReach; |
| 653 | dy += body.vy * left; |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | var vyNew = body.vy - accel * left; |
| 658 | dy += (body.vy + vyNew) * 0.5 * left; |
| 659 | body.vy = vyNew; |
| 660 | } |
| 661 | } |
| 662 | else |
| 663 | { |
| 664 | body.vy = target; |
| 665 | dy += body.vy * left; |
| 666 | } |
| 667 | } |
| 668 | return dy; |
| 669 | }; |
| 670 | |
| 671 | GameCore.prototype.integrateClusters = function (dt) |
| 672 | { |
| 673 | var ids = Object.keys(this.clusters); |
| 674 | for (var n = 0; n < ids.length; n++) |
| 675 | { |
| 676 | var cluster = this.clusters[ids[n]]; |
| 677 | if (!cluster) |
| 678 | { continue; } |
| 679 | var members = this.clusterMembers(cluster.id); |
| 680 | if (!members.length) |
| 681 | { |
| 682 | delete this.clusters[cluster.id]; |
| 683 | continue; |
| 684 | } |
| 685 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 686 | { continue; } |
| 687 | |
| 688 | var dy = this.advanceJump(cluster, dt); |
| 689 | for (var i = 0; i < members.length; i++) |
| 690 | { |
| 691 | members[i].y += dy; |
| 692 | members[i].vy = cluster.vy; |
| 693 | } |
| 694 | |
| 695 | this.catchFallingOntoCluster(cluster, members); |
| 696 | |
| 697 | if (cluster.phase === 'down') |
| 698 | { this.tryLandCluster(cluster, this.clusterMembers(cluster.id)); } |
| 699 | } |
| 700 | }; |
| 701 | |
| 702 | |
| 703 | /** |
| 704 | * If two or more flying clusters overlap in the same column, merge them |
| 705 | * into one cluster. Velocity (and deceleration and jumpHeight) is the |
| 706 | * mass-weighted average by block count. Phase is 'up' when the merged |
| 707 | * vy is positive, otherwise 'down'. Overlapping members in a column |
| 708 | * are packed so the higher one sits flush on the lower. |
| 709 | */ |
| 710 | GameCore.prototype.mergeFlyingClusters = function () |
| 711 | { |
| 712 | var flying = []; |
| 713 | var ids = Object.keys(this.clusters); |
| 714 | for (var n = 0; n < ids.length; n++) |
| 715 | { |
| 716 | var cluster = this.clusters[ids[n]]; |
| 717 | if (!cluster) |
| 718 | { continue; } |
| 719 | if (cluster.phase === 'landed' || cluster.phase === 'hold') |
| 720 | { continue; } |
| 721 | flying.push(cluster); |
| 722 | } |
| 723 | if (flying.length < 2) |
| 724 | { return; } |
| 725 | |
| 726 | var uf = new UnionFind(); |
| 727 | for (var i = 0; i < flying.length; i++) |
| 728 | { uf.add(flying[i].id); } |
| 729 | for (var i = 0; i < flying.length; i++) |
| 730 | { |
| 731 | for (var j = i + 1; j < flying.length; j++) |
| 732 | { |
| 733 | if (this.flyingClustersOverlap(flying[i], flying[j])) |
| 734 | { uf.union(flying[i].id, flying[j].id); } |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | var groups = Object.create(null); |
| 739 | for (var i = 0; i < flying.length; i++) |
| 740 | { |
| 741 | var root = uf.find(flying[i].id); |
| 742 | if (!groups[root]) |
| 743 | { groups[root] = []; } |
| 744 | groups[root].push(flying[i]); |
| 745 | } |
| 746 | for (var root in groups) |
| 747 | { |
| 748 | if (groups[root].length >= 2) |
| 749 | { this.mergeClusterGroup(groups[root]); } |
| 750 | } |
| 751 | }; |
| 752 | |
| 753 | GameCore.prototype.flyingClustersOverlap = function (a, b) |
| 754 | { |
| 755 | var ma = this.clusterMembers(a.id); |
| 756 | var mb = this.clusterMembers(b.id); |
| 757 | for (var i = 0; i < ma.length; i++) |
| 758 | { |
| 759 | for (var j = 0; j < mb.length; j++) |
| 760 | { |
| 761 | if (ma[i].col !== mb[j].col) |
| 762 | { continue; } |
| 763 | if (ma[i].y < mb[j].y + 1 - 1e-9 && mb[j].y < ma[i].y + 1 - 1e-9) |
| 764 | { return true; } |
| 765 | } |
| 766 | } |
| 767 | return false; |
| 768 | }; |
| 769 | |
| 770 | GameCore.prototype.mergeClusterGroup = function (clusters) |
| 771 | { |
| 772 | clusters.sort(function (a, b) { return a.id - b.id; }); |
| 773 | var keep = clusters[0]; |
| 774 | var n = 0; |
| 775 | var vySum = 0; |
| 776 | var decelSum = 0; |
| 777 | var hSum = 0; |
| 778 | for (var c = 0; c < clusters.length; c++) |
| 779 | { |
| 780 | var mem = this.clusterMembers(clusters[c].id); |
| 781 | var m = mem.length; |
| 782 | n += m; |
| 783 | vySum += clusters[c].vy * m; |
| 784 | decelSum += (clusters[c].decel || 0) * m; |
| 785 | hSum += (clusters[c].jumpHeight || 0) * m; |
| 786 | } |
| 787 | if (n === 0) |
| 788 | { return; } |
| 789 | keep.vy = vySum / n; |
| 790 | keep.decel = decelSum / n; |
| 791 | keep.jumpHeight = hSum / n; |
| 792 | keep.phase = keep.vy > 1e-10 ? 'up' : 'down'; |
| 793 | for (var c = 0; c < clusters.length; c++) |
| 794 | { |
| 795 | var mem = this.clusterMembers(clusters[c].id); |
| 796 | for (var i = 0; i < mem.length; i++) |
| 797 | { |
| 798 | mem[i].clusterId = keep.id; |
| 799 | mem[i].vy = keep.vy; |
| 800 | mem[i].state = STATE_FLYING; |
| 801 | } |
| 802 | if (clusters[c] !== keep) |
| 803 | { delete this.clusters[clusters[c].id]; } |
| 804 | } |
| 805 | this.packClusterOverlaps(keep); |
| 806 | if (keep.phase === 'down') |
| 807 | { this.tryLandCluster(keep, this.clusterMembers(keep.id)); } |
| 808 | }; |
| 809 | |
| 810 | GameCore.prototype.packClusterOverlaps = function (cluster) |
| 811 | { |
| 812 | var members = this.clusterMembers(cluster.id); |
| 813 | var byCol = Object.create(null); |
| 814 | for (var i = 0; i < members.length; i++) |
| 815 | { |
| 816 | var col = members[i].col; |
| 817 | if (!byCol[col]) |
| 818 | { byCol[col] = []; } |
| 819 | byCol[col].push(members[i]); |
| 820 | } |
| 821 | var cols = Object.keys(byCol); |
| 822 | for (var c = 0; c < cols.length; c++) |
| 823 | { |
| 824 | var list = byCol[cols[c]]; |
| 825 | list.sort(function (a, b) { return a.y - b.y; }); |
| 826 | for (var i = 1; i < list.length; i++) |
| 827 | { |
| 828 | var minY = list[i - 1].y + 1; |
| 829 | if (list[i].y < minY - 1e-12) |
| 830 | { list[i].y = minY; } |
| 831 | } |
| 832 | } |
| 833 | }; |
| 834 | |
| 835 | GameCore.prototype.tryLandCluster = function (cluster, members) |
| 836 | { |
| 837 | if (!members.length) |
| 838 | { return; } |
| 839 | var shift = 0; |
| 840 | var hit = false; |
| 841 | for (var i = 0; i < members.length; i++) |
| 842 | { |
| 843 | var block = members[i]; |
| 844 | var top = this.stackTop(block.col, function (other) |
| 845 | { |
| 846 | return other.clusterId === cluster.id; |
| 847 | }); |
| 848 | if (block.y <= top + 1e-9) |
| 849 | { |
| 850 | hit = true; |
| 851 | var need = top - block.y; |
| 852 | if (need > shift) |
| 853 | { shift = need; } |
| 854 | } |
| 855 | } |
| 856 | if (!hit) |
| 857 | { return; } |
| 858 | // Lift the whole rigid body so the deepest contact sits flush. |
| 859 | for (var i = 0; i < members.length; i++) |
| 860 | { members[i].y += shift; } |
| 861 | this.landCluster(cluster, members); |
| 862 | }; |
| 863 | |
| 864 | /** |
| 865 | * Land as a rigid group: relative positions are kept, so part of the |
| 866 | * cluster may hang above a gap. Grey members start the 3s settle timer; |
| 867 | * coloured cargo stays put with them until every grey has recolored. |
| 868 | * Members that actually rest on the floor or on another block bounce. |
| 869 | */ |
| 870 | GameCore.prototype.landCluster = function (cluster, members) |
| 871 | { |
| 872 | for (var i = 0; i < members.length; i++) |
| 873 | { |
| 874 | var block = members[i]; |
| 875 | block.vy = 0; |
| 876 | block.flame = false; |
| 877 | block.clusterId = cluster.id; |
| 878 | if (block.color === COLOR_GRAY) |
| 879 | { |
| 880 | block.state = STATE_SETTLING; |
| 881 | block.settleTime = 0; |
| 882 | } |
| 883 | else |
| 884 | { |
| 885 | // Cargo stays in the rigid group (not matchable) until release. |
| 886 | block.state = STATE_SETTLING; |
| 887 | block.settleTime = 0; |
| 888 | } |
| 889 | if (this.hasSupport(block)) |
| 890 | { this.startBounce(block); } |
| 891 | } |
| 892 | cluster.phase = 'landed'; |
| 893 | cluster.vy = 0; |
| 894 | }; |
| 895 | |
| 896 | /** |
| 897 | * After every grey in a landed cluster has recolored, let the group go. |
| 898 | * Unsupported blocks fall individually from rest; supported ones stay. |
| 899 | */ |
| 900 | GameCore.prototype.releaseLandedCluster = function (cluster) |
| 901 | { |
| 902 | var members = this.clusterMembers(cluster.id); |
| 903 | var releasing = Object.create(null); |
| 904 | for (var i = 0; i < members.length; i++) |
| 905 | { releasing[members[i].id] = true; } |
| 906 | |
| 907 | for (var i = 0; i < members.length; i++) |
| 908 | { |
| 909 | var block = members[i]; |
| 910 | if (block.color === COLOR_GRAY) |
| 911 | { |
| 912 | block.color = this.randomColor(); |
| 913 | block.settleTime = 0; |
| 914 | } |
| 915 | this.leaveCluster(block); |
| 916 | // Always drop leftover launch / manual velocity. A stale upward |
| 917 | // vy here is what sends blocks off the top "very fast" after |
| 918 | // a grey group recolors. |
| 919 | this.clearFlightState(block); |
| 920 | // Support from outside the group (floor or a foreign block). Fellow |
| 921 | // members that will also fall do not count, so a hanging stack |
| 922 | // drops together without the top one staying put. |
| 923 | var supported = this.hasSupportOutside(block, releasing); |
| 924 | block.state = supported ? STATE_STILL : STATE_FALLING; |
| 925 | } |
| 926 | delete this.clusters[cluster.id]; |
| 927 | }; |
| 928 | |
| 929 | GameCore.prototype.hasSupportOutside = function (block, ignoreIds) |
| 930 | { |
| 931 | if (block.y <= 1e-9) |
| 932 | { return true; } |
| 933 | for (var i = 0; i < this.blocks.length; i++) |
| 934 | { |
| 935 | var other = this.blocks[i]; |
| 936 | if (other === block || other.col !== block.col) |
| 937 | { continue; } |
| 938 | if (ignoreIds && ignoreIds[other.id]) |
| 939 | { continue; } |
| 940 | if (!this.isGrounded(other)) |
| 941 | { continue; } |
| 942 | if (Math.abs((other.y + 1) - block.y) < ALIGN_EPS) |
| 943 | { return true; } |
| 944 | } |
| 945 | return false; |
| 946 | }; |
| 947 | |
| 948 | GameCore.prototype.integrateManual = function (dt) |
| 949 | { |
| 950 | for (var i = 0; i < this.blocks.length; i++) |
| 951 | { |
| 952 | var block = this.blocks[i]; |
| 953 | if (block.state !== STATE_MANUAL) |
| 954 | { continue; } |
| 955 | |
| 956 | if (block.manualPhase === 'up') |
| 957 | { |
| 958 | var oldY = block.y; |
| 959 | var proxy = { phase: 'up', vy: block.vy, decel: block.manualDecel }; |
| 960 | var dy = this.advanceJump(proxy, dt); |
| 961 | var nextY = oldY + dy; |
| 962 | this.checkManualPush(block, oldY, Math.min(nextY, block.manualTarget)); |
| 963 | block.vy = proxy.vy; |
| 964 | block.manualPhase = proxy.phase; |
| 965 | block.y = Math.min(nextY, block.manualTarget); |
| 966 | if (block.y >= block.manualTarget - 1e-12) |
| 967 | { |
| 968 | block.y = block.manualTarget; |
| 969 | block.vy = 0; |
| 970 | block.manualPhase = 'down'; |
| 971 | } |
| 972 | if (block.y > this.height - 1) |
| 973 | { |
| 974 | block.y = this.height - 1; |
| 975 | block.vy = 0; |
| 976 | block.manualPhase = 'down'; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if (block.manualPhase === 'down') |
| 981 | { |
| 982 | var proxy = { phase: 'down', vy: block.vy, decel: 0 }; |
| 983 | var dy = this.advanceJump(proxy, dt); |
| 984 | block.vy = proxy.vy; |
| 985 | var nextY = block.y + dy; |
| 986 | var top = this.stackTop(block.col); |
| 987 | if (nextY <= top) |
| 988 | { |
| 989 | block.y = top; |
| 990 | block.state = STATE_STILL; |
| 991 | block.vy = 0; |
| 992 | block.manualPhase = null; |
| 993 | block.flame = false; |
| 994 | this.startBounce(block); |
| 995 | } |
| 996 | else |
| 997 | { |
| 998 | block.y = Math.min(nextY, this.height - 1); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | }; |
| 1003 | |
| 1004 | GameCore.prototype.checkManualPush = function (manual, oldY, nextY) |
| 1005 | { |
| 1006 | if (!manual.pushedClusters) |
| 1007 | { manual.pushedClusters = Object.create(null); } |
| 1008 | |
| 1009 | var manualTopOld = oldY + 1; |
| 1010 | var manualTopNew = nextY + 1; |
| 1011 | var low = Math.min(oldY, nextY); |
| 1012 | var high = Math.max(oldY, nextY) + 1; |
| 1013 | |
| 1014 | for (var i = 0; i < this.blocks.length; i++) |
| 1015 | { |
| 1016 | var other = this.blocks[i]; |
| 1017 | if (other === manual) |
| 1018 | { continue; } |
| 1019 | if (other.col !== manual.col) |
| 1020 | { continue; } |
| 1021 | if (other.state !== STATE_FLYING || other.clusterId == null) |
| 1022 | { continue; } |
| 1023 | if (manual.pushedClusters[other.clusterId]) |
| 1024 | { continue; } |
| 1025 | var otherBot = other.y; |
| 1026 | var otherTop = other.y + 1; |
| 1027 | var overlapping = otherBot < high + 1e-9 && otherTop > low - 1e-9; |
| 1028 | var crossing = manualTopOld <= otherBot + 1e-9 && manualTopNew >= otherBot - 1e-9; |
| 1029 | if (overlapping || crossing) |
| 1030 | { |
| 1031 | if (this.pushClusterColumn(other.clusterId, manual.col)) |
| 1032 | { |
| 1033 | manual.pushedClusters[other.clusterId] = true; |
| 1034 | return; |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | }; |
| 1039 | |
| 1040 | |
| 1041 | /** |
| 1042 | * Push every flying block of the cluster that sits in `col` one tile up. |
| 1043 | * Other columns stay put (they remain linked). Then look for a new |
| 1044 | * same-color line of 3+ in the flying cluster; if there is one, those |
| 1045 | * blocks turn gray, join the link, and the whole cluster re-launches |
| 1046 | * with the same jump height from its current position. |
| 1047 | */ |
| 1048 | GameCore.prototype.pushClusterColumn = function (clusterId, col) |
| 1049 | { |
| 1050 | var cluster = this.clusters[clusterId]; |
| 1051 | if (!cluster) |
| 1052 | { return false; } |
| 1053 | var members = this.clusterMembers(clusterId); |
| 1054 | for (var i = 0; i < members.length; i++) |
| 1055 | { |
| 1056 | if (members[i].col === col) |
| 1057 | { members[i].y += 1; } |
| 1058 | } |
| 1059 | this.cullOffTop(); |
| 1060 | if (!this.clusters[clusterId]) |
| 1061 | { return true; } |
| 1062 | this.checkChainRelaunch(cluster); |
| 1063 | return true; |
| 1064 | }; |
| 1065 | |
| 1066 | GameCore.prototype.checkChainRelaunch = function (cluster) |
| 1067 | { |
| 1068 | if (!this.clusters[cluster.id]) |
| 1069 | { return; } |
| 1070 | var lines = this.findFlyingMatchLines(cluster.id); |
| 1071 | if (!lines.length) |
| 1072 | { return; } |
| 1073 | var newly = []; |
| 1074 | var seen = Object.create(null); |
| 1075 | for (var i = 0; i < lines.length; i++) |
| 1076 | { |
| 1077 | for (var j = 0; j < lines[i].blocks.length; j++) |
| 1078 | { |
| 1079 | var block = lines[i].blocks[j]; |
| 1080 | if (seen[block.id]) |
| 1081 | { continue; } |
| 1082 | seen[block.id] = true; |
| 1083 | newly.push(block); |
| 1084 | } |
| 1085 | } |
| 1086 | for (var i = 0; i < newly.length; i++) |
| 1087 | { |
| 1088 | var block = newly[i]; |
| 1089 | block.color = COLOR_GRAY; |
| 1090 | block.flame = true; |
| 1091 | block.clusterId = cluster.id; |
| 1092 | block.state = STATE_FLYING; |
| 1093 | } |
| 1094 | this.relaunchCluster(cluster, cluster.jumpHeight); |
| 1095 | }; |
| 1096 | |
| 1097 | GameCore.prototype.relaunchCluster = function (cluster, height) |
| 1098 | { |
| 1099 | cluster.jumpHeight = height; |
| 1100 | cluster.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1101 | cluster.decel = PlanetMod.launchDeceleration(this.planet, height); |
| 1102 | cluster.phase = 'up'; |
| 1103 | var members = this.clusterMembers(cluster.id); |
| 1104 | for (var i = 0; i < members.length; i++) |
| 1105 | { |
| 1106 | members[i].vy = cluster.vy; |
| 1107 | members[i].state = STATE_FLYING; |
| 1108 | if (members[i].color === COLOR_GRAY) |
| 1109 | { members[i].flame = true; } |
| 1110 | } |
| 1111 | }; |
| 1112 | |
| 1113 | GameCore.prototype.startManualLaunch = function (block) |
| 1114 | { |
| 1115 | var target = this.height - 1; |
| 1116 | var height = Math.max(0.05, target - block.y); |
| 1117 | block.state = STATE_MANUAL; |
| 1118 | block.pushedClusters = Object.create(null); |
| 1119 | block.manualTarget = target; |
| 1120 | block.manualPhase = 'up'; |
| 1121 | block.vy = PlanetMod.launchVelocity(this.planet, height); |
| 1122 | block.manualDecel = PlanetMod.launchDeceleration(this.planet, height); |
| 1123 | block.flame = false; |
| 1124 | if (this.drag && this.drag.blockId === block.id) |
| 1125 | { this.drag = null; } |
| 1126 | }; |
| 1127 | |
| 1128 | GameCore.prototype.cullOffTop = function () |
| 1129 | { |
| 1130 | var gone = []; |
| 1131 | for (var i = 0; i < this.blocks.length; i++) |
| 1132 | { |
| 1133 | var block = this.blocks[i]; |
| 1134 | if (block.state === STATE_MANUAL) |
| 1135 | { |
| 1136 | if (block.y > this.height - 1) |
| 1137 | { block.y = this.height - 1; } |
| 1138 | continue; |
| 1139 | } |
| 1140 | if (block.y >= this.height - 1e-9) |
| 1141 | { gone.push(block); } |
| 1142 | } |
| 1143 | for (var i = 0; i < gone.length; i++) |
| 1144 | { |
| 1145 | this.score += 1; |
| 1146 | this.removeBlock(gone[i]); |
| 1147 | } |
| 1148 | }; |
| 1149 | |
| 1150 | GameCore.prototype.advanceSettle = function (dt) |
| 1151 | { |
| 1152 | for (var i = 0; i < this.blocks.length; i++) |
| 1153 | { |
| 1154 | var block = this.blocks[i]; |
| 1155 | if (block.state !== STATE_SETTLING) |
| 1156 | { continue; } |
| 1157 | var inLanded = block.clusterId != null |
| 1158 | && this.clusters[block.clusterId] |
| 1159 | && this.clusters[block.clusterId].phase === 'landed'; |
| 1160 | if (block.color === COLOR_GRAY) |
| 1161 | { |
| 1162 | block.settleTime += dt; |
| 1163 | if (block.settleTime >= this.settleTime) |
| 1164 | { |
| 1165 | block.color = this.randomColor(); |
| 1166 | block.settleTime = 0; |
| 1167 | block.flame = false; |
| 1168 | if (inLanded) |
| 1169 | { |
| 1170 | // Stay in the rigid group; releaseLandedCluster runs below. |
| 1171 | block.state = STATE_SETTLING; |
| 1172 | } |
| 1173 | else |
| 1174 | { |
| 1175 | this.leaveCluster(block); |
| 1176 | this.clearFlightState(block); |
| 1177 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | else if (!inLanded) |
| 1182 | { |
| 1183 | // Coloured settling that is not in a landed cluster (shouldn't |
| 1184 | // normally happen): just become still or fall, from rest. |
| 1185 | this.leaveCluster(block); |
| 1186 | this.clearFlightState(block); |
| 1187 | block.state = this.hasSupport(block) ? STATE_STILL : STATE_FALLING; |
| 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | var ids = Object.keys(this.clusters); |
| 1192 | for (var n = 0; n < ids.length; n++) |
| 1193 | { |
| 1194 | var cluster = this.clusters[ids[n]]; |
| 1195 | if (!cluster || cluster.phase !== 'landed') |
| 1196 | { continue; } |
| 1197 | var members = this.clusterMembers(cluster.id); |
| 1198 | var greySettling = false; |
| 1199 | for (var i = 0; i < members.length; i++) |
| 1200 | { |
| 1201 | if (members[i].color === COLOR_GRAY && members[i].state === STATE_SETTLING) |
| 1202 | { greySettling = true; } |
| 1203 | } |
| 1204 | if (!greySettling) |
| 1205 | { this.releaseLandedCluster(cluster); } |
| 1206 | } |
| 1207 | }; |
| 1208 | |
| 1209 | GameCore.prototype.findMatchLines = function () |
| 1210 | { |
| 1211 | var candidates = []; |
| 1212 | for (var i = 0; i < this.blocks.length; i++) |
| 1213 | { |
| 1214 | var block = this.blocks[i]; |
| 1215 | if (block.state === STATE_STILL && block.color >= 0) |
| 1216 | { candidates.push(block); } |
| 1217 | } |
| 1218 | return this.collectLines(candidates, true); |
| 1219 | }; |
| 1220 | |
| 1221 | GameCore.prototype.findFlyingMatchLines = function (clusterId) |
| 1222 | { |
| 1223 | var candidates = []; |
| 1224 | for (var i = 0; i < this.blocks.length; i++) |
| 1225 | { |
| 1226 | var block = this.blocks[i]; |
| 1227 | if (block.clusterId === clusterId && block.color >= 0 |
| 1228 | && (block.state === STATE_FLYING)) |
| 1229 | { candidates.push(block); } |
| 1230 | } |
| 1231 | return this.collectLines(candidates, false); |
| 1232 | }; |
| 1233 | |
| 1234 | GameCore.prototype.collectLines = function (candidates, snapInteger) |
| 1235 | { |
| 1236 | var lines = []; |
| 1237 | if (!candidates.length) |
| 1238 | { return lines; } |
| 1239 | |
| 1240 | var byRow = Object.create(null); |
| 1241 | for (var i = 0; i < candidates.length; i++) |
| 1242 | { |
| 1243 | var block = candidates[i]; |
| 1244 | var row = Math.round(block.y); |
| 1245 | if (Math.abs(block.y - row) > (snapInteger ? ALIGN_EPS : 0.5)) |
| 1246 | { continue; } |
| 1247 | if (!byRow[row]) |
| 1248 | { byRow[row] = []; } |
| 1249 | byRow[row].push(block); |
| 1250 | } |
| 1251 | var rows = Object.keys(byRow); |
| 1252 | for (var r = 0; r < rows.length; r++) |
| 1253 | { |
| 1254 | var list = byRow[rows[r]]; |
| 1255 | list.sort(function (a, b) { return a.col - b.col; }); |
| 1256 | this.pushRuns(lines, list, false, function (a, b) |
| 1257 | { |
| 1258 | return a.color === b.color && a.col === b.col - 1 && a.color >= 0; |
| 1259 | }); |
| 1260 | } |
| 1261 | |
| 1262 | var byCol = Object.create(null); |
| 1263 | for (var i = 0; i < candidates.length; i++) |
| 1264 | { |
| 1265 | var block = candidates[i]; |
| 1266 | if (!byCol[block.col]) |
| 1267 | { byCol[block.col] = []; } |
| 1268 | byCol[block.col].push(block); |
| 1269 | } |
| 1270 | var cols = Object.keys(byCol); |
| 1271 | for (var c = 0; c < cols.length; c++) |
| 1272 | { |
| 1273 | var list = byCol[cols[c]]; |
| 1274 | list.sort(function (a, b) { return a.y - b.y; }); |
| 1275 | this.pushRuns(lines, list, true, function (a, b) |
| 1276 | { |
| 1277 | return a.color === b.color && a.color >= 0 |
| 1278 | && Math.abs((b.y - a.y) - 1) < ALIGN_EPS; |
| 1279 | }); |
| 1280 | } |
| 1281 | return lines; |
| 1282 | }; |
| 1283 | |
| 1284 | GameCore.prototype.pushRuns = function (lines, list, vertical, adjacent) |
| 1285 | { |
| 1286 | var start = 0; |
| 1287 | for (var i = 1; i <= list.length; i++) |
| 1288 | { |
| 1289 | var same = i < list.length && adjacent(list[i - 1], list[i]); |
| 1290 | if (!same) |
| 1291 | { |
| 1292 | var length = i - start; |
| 1293 | if (length >= 3) |
| 1294 | { |
| 1295 | lines.push({ |
| 1296 | blocks: list.slice(start, i), |
| 1297 | length: length, |
| 1298 | vertical: vertical |
| 1299 | }); |
| 1300 | } |
| 1301 | start = i; |
| 1302 | } |
| 1303 | } |
| 1304 | }; |
| 1305 | |
| 1306 | GameCore.prototype.resolveMatches = function () |
| 1307 | { |
| 1308 | if (this.state !== 'playing' || this.drag) |
| 1309 | { return []; } |
| 1310 | var lines = this.findMatchLines(); |
| 1311 | if (!lines.length) |
| 1312 | { return []; } |
| 1313 | |
| 1314 | var uf = new UnionFind(); |
| 1315 | var blockById = Object.create(null); |
| 1316 | for (var i = 0; i < lines.length; i++) |
| 1317 | { |
| 1318 | var line = lines[i]; |
| 1319 | for (var j = 0; j < line.blocks.length; j++) |
| 1320 | { |
| 1321 | uf.add(line.blocks[j].id); |
| 1322 | blockById[line.blocks[j].id] = line.blocks[j]; |
| 1323 | if (j > 0) |
| 1324 | { uf.union(line.blocks[0].id, line.blocks[j].id); } |
| 1325 | } |
| 1326 | } |
| 1327 | var matched = []; |
| 1328 | var seen = Object.create(null); |
| 1329 | for (var id in blockById) |
| 1330 | { |
| 1331 | if (!seen[id]) |
| 1332 | { |
| 1333 | seen[id] = true; |
| 1334 | matched.push(blockById[id]); |
| 1335 | } |
| 1336 | } |
| 1337 | for (var i = 0; i < matched.length; i++) |
| 1338 | { |
| 1339 | for (var j = i + 1; j < matched.length; j++) |
| 1340 | { |
| 1341 | if (matched[i].col === matched[j].col) |
| 1342 | { uf.union(matched[i].id, matched[j].id); } |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | var groups = Object.create(null); |
| 1347 | for (var i = 0; i < matched.length; i++) |
| 1348 | { |
| 1349 | var root = uf.find(matched[i].id); |
| 1350 | if (!groups[root]) |
| 1351 | { groups[root] = []; } |
| 1352 | groups[root].push(matched[i]); |
| 1353 | } |
| 1354 | |
| 1355 | var launched = []; |
| 1356 | for (var root in groups) |
| 1357 | { |
| 1358 | var group = groups[root]; |
| 1359 | var height = 0; |
| 1360 | for (var i = 0; i < lines.length; i++) |
| 1361 | { |
| 1362 | var line = lines[i]; |
| 1363 | var overlaps = false; |
| 1364 | for (var j = 0; j < line.blocks.length; j++) |
| 1365 | { |
| 1366 | for (var k = 0; k < group.length; k++) |
| 1367 | { |
| 1368 | if (line.blocks[j].id === group[k].id) |
| 1369 | { overlaps = true; } |
| 1370 | } |
| 1371 | } |
| 1372 | if (overlaps) |
| 1373 | { |
| 1374 | var h = PlanetMod.jumpHeight(this.planet, line.length, line.vertical); |
| 1375 | if (h > height) |
| 1376 | { height = h; } |
| 1377 | } |
| 1378 | } |
| 1379 | this.launchCluster(group, height); |
| 1380 | launched.push({ blocks: group, jumpHeight: height }); |
| 1381 | } |
| 1382 | return launched; |
| 1383 | }; |
| 1384 | |
| 1385 | /** |
| 1386 | * Gray the matched blocks, take everything sitting on top of them in |
| 1387 | * those columns, link them, and launch with jump height `height`. |
| 1388 | */ |
| 1389 | GameCore.prototype.launchCluster = function (matchedBlocks, height) |
| 1390 | { |
| 1391 | var lowestInCol = Object.create(null); |
| 1392 | var matchedIds = Object.create(null); |
| 1393 | for (var i = 0; i < matchedBlocks.length; i++) |
| 1394 | { |
| 1395 | var block = matchedBlocks[i]; |
| 1396 | matchedIds[block.id] = true; |
| 1397 | if (lowestInCol[block.col] == null || block.y < lowestInCol[block.col]) |
| 1398 | { lowestInCol[block.col] = block.y; } |
| 1399 | } |
| 1400 | |
| 1401 | var clusterId = this.nextClusterId++; |
| 1402 | var cluster = { |
| 1403 | id: clusterId, |
| 1404 | jumpHeight: height, |
| 1405 | vy: PlanetMod.launchVelocity(this.planet, height), |
| 1406 | decel: PlanetMod.launchDeceleration(this.planet, height), |
| 1407 | phase: 'up' |
| 1408 | }; |
| 1409 | this.clusters[clusterId] = cluster; |
| 1410 | |
| 1411 | var riders = []; |
| 1412 | var cols = Object.keys(lowestInCol); |
| 1413 | for (var c = 0; c < cols.length; c++) |
| 1414 | { |
| 1415 | var col = +cols[c]; |
| 1416 | var lowest = lowestInCol[col]; |
| 1417 | var list = this.blocksInColumn(col); |
| 1418 | for (var i = 0; i < list.length; i++) |
| 1419 | { |
| 1420 | var block = list[i]; |
| 1421 | if (!this.isGrounded(block) && block.state !== STATE_STILL) |
| 1422 | { |
| 1423 | if (block.state !== STATE_SETTLING) |
| 1424 | { continue; } |
| 1425 | } |
| 1426 | if (block.y + ALIGN_EPS >= lowest) |
| 1427 | { riders.push(block); } |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | for (var i = 0; i < riders.length; i++) |
| 1432 | { |
| 1433 | var block = riders[i]; |
| 1434 | var isMatch = !!matchedIds[block.id]; |
| 1435 | if (isMatch || block.color === COLOR_GRAY) |
| 1436 | { |
| 1437 | block.color = COLOR_GRAY; |
| 1438 | block.flame = true; |
| 1439 | } |
| 1440 | else |
| 1441 | { block.flame = false; } |
| 1442 | block.state = STATE_FLYING; |
| 1443 | block.clusterId = clusterId; |
| 1444 | block.vy = cluster.vy; |
| 1445 | block.settleTime = 0; |
| 1446 | } |
| 1447 | return cluster; |
| 1448 | }; |
| 1449 | |
| 1450 | GameCore.prototype.checkLocks = function () |
| 1451 | { |
| 1452 | for (var col = 0; col < this.width; col++) |
| 1453 | { |
| 1454 | if (this.isColumnLocked(col)) |
| 1455 | { continue; } |
| 1456 | if (this.groundedCount(col) < this.height) |
| 1457 | { continue; } |
| 1458 | // Must fill the column from floor to top: 13 grounded blocks at y 0..12. |
| 1459 | var filled = true; |
| 1460 | for (var row = 0; row < this.height; row++) |
| 1461 | { |
| 1462 | var block = this.blockAt(col, row); |
| 1463 | if (!block || !this.isGrounded(block)) |
| 1464 | { |
| 1465 | filled = false; |
| 1466 | break; |
| 1467 | } |
| 1468 | } |
| 1469 | if (!filled) |
| 1470 | { continue; } |
| 1471 | var list = this.blocksInColumn(col); |
| 1472 | for (var i = 0; i < list.length; i++) |
| 1473 | { |
| 1474 | var block = list[i]; |
| 1475 | if (!this.isGrounded(block)) |
| 1476 | { continue; } |
| 1477 | block.state = STATE_LOCKED; |
| 1478 | block.color = COLOR_GRAY; |
| 1479 | block.flame = false; |
| 1480 | block.vy = 0; |
| 1481 | block.settleTime = 0; |
| 1482 | if (block.clusterId != null) |
| 1483 | { this.leaveCluster(block); } |
| 1484 | } |
| 1485 | } |
| 1486 | }; |
| 1487 | |
| 1488 | GameCore.prototype.checkGameOver = function () |
| 1489 | { |
| 1490 | if (this.state !== 'playing') |
| 1491 | { return; } |
| 1492 | for (var col = 0; col < this.width; col++) |
| 1493 | { |
| 1494 | if (!this.isColumnLocked(col)) |
| 1495 | { return; } |
| 1496 | } |
| 1497 | this.state = 'gameover'; |
| 1498 | this.gameOverTime = 0; |
| 1499 | this.drag = null; |
| 1500 | }; |
| 1501 | |
| 1502 | /* ---------- dragging -------------------------------------------------- */ |
| 1503 | |
| 1504 | GameCore.prototype.beginDrag = function (col, y) |
| 1505 | { |
| 1506 | if (this.state !== 'playing') |
| 1507 | { return false; } |
| 1508 | var block = this.snapDragBlock(col, y); |
| 1509 | if (!block) |
| 1510 | { return false; } |
| 1511 | var top = this.columnStackTopRow(block.col); |
| 1512 | var startedAsTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 1513 | this.drag = { |
| 1514 | blockId: block.id, |
| 1515 | pointerY: y, |
| 1516 | startY: y, |
| 1517 | accum: 0, |
| 1518 | launchIntent: false, |
| 1519 | startedAsTop: startedAsTop |
| 1520 | }; |
| 1521 | return true; |
| 1522 | }; |
| 1523 | |
| 1524 | /** |
| 1525 | * Instant grab: the nearest still, coloured block to (col, y), allowing |
| 1526 | * a small vertical miss and a horizontal slop into a neighbouring |
| 1527 | * column. `col` may be fractional (pointer x in tiles). No hold delay. |
| 1528 | */ |
| 1529 | GameCore.prototype.snapDragBlock = function (col, y) |
| 1530 | { |
| 1531 | var raw = col; |
| 1532 | var c0 = Math.floor(raw + 1e-9); |
| 1533 | var frac = raw - c0; |
| 1534 | var candidates = []; |
| 1535 | if (c0 >= 0 && c0 < this.width) |
| 1536 | { candidates.push(c0); } |
| 1537 | // Only consult a neighbour when the pointer is actually near the edge |
| 1538 | // (a whole-number col, as in unit tests, stays in that column). |
| 1539 | if (frac > 1e-9 && frac < HORIZONTAL_SLOP && c0 - 1 >= 0) |
| 1540 | { candidates.push(c0 - 1); } |
| 1541 | if (frac > 1 - HORIZONTAL_SLOP && frac < 1 - 1e-9 && c0 + 1 < this.width) |
| 1542 | { candidates.push(c0 + 1); } |
| 1543 | |
| 1544 | var best = null; |
| 1545 | var bestDist = Infinity; |
| 1546 | for (var c = 0; c < candidates.length; c++) |
| 1547 | { |
| 1548 | var cc = candidates[c]; |
| 1549 | if (!this.isColumnStill(cc)) |
| 1550 | { continue; } |
| 1551 | var list = this.blocksInColumn(cc); |
| 1552 | for (var i = 0; i < list.length; i++) |
| 1553 | { |
| 1554 | var block = list[i]; |
| 1555 | if (block.state !== STATE_STILL || block.color < 0) |
| 1556 | { continue; } |
| 1557 | var dist = this.pointerDistToBlock(block, raw, y); |
| 1558 | // Prefer the column the pointer is actually in on a tie. |
| 1559 | if (dist < bestDist - 1e-12 || (Math.abs(dist - bestDist) < 1e-12 && cc === c0)) |
| 1560 | { |
| 1561 | bestDist = dist; |
| 1562 | best = block; |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | if (!best || bestDist > SNAP_Y) |
| 1567 | { return null; } |
| 1568 | return best; |
| 1569 | }; |
| 1570 | |
| 1571 | GameCore.prototype.pointerDistToBlock = function (block, col, y) |
| 1572 | { |
| 1573 | var dx; |
| 1574 | if (col >= block.col && col < block.col + 1) |
| 1575 | { dx = 0; } |
| 1576 | else if (col < block.col) |
| 1577 | { dx = block.col - col; } |
| 1578 | else |
| 1579 | { dx = col - (block.col + 1); } |
| 1580 | var dy; |
| 1581 | if (y >= block.y && y < block.y + 1) |
| 1582 | { dy = 0; } |
| 1583 | else if (y < block.y) |
| 1584 | { dy = block.y - y; } |
| 1585 | else |
| 1586 | { dy = y - (block.y + 1); } |
| 1587 | return Math.sqrt(dx * dx + dy * dy); |
| 1588 | }; |
| 1589 | |
| 1590 | GameCore.prototype.moveDrag = function (y) |
| 1591 | { |
| 1592 | if (!this.drag) |
| 1593 | { return; } |
| 1594 | var block = this.blockById(this.drag.blockId); |
| 1595 | if (!block || block.state !== STATE_STILL) |
| 1596 | { |
| 1597 | this.drag = null; |
| 1598 | return; |
| 1599 | } |
| 1600 | if (!this.isColumnStill(block.col)) |
| 1601 | { |
| 1602 | this.drag = null; |
| 1603 | return; |
| 1604 | } |
| 1605 | var dy = y - this.drag.pointerY; |
| 1606 | this.drag.pointerY = y; |
| 1607 | if (this.drag.accum == null) |
| 1608 | { this.drag.accum = 0; } |
| 1609 | this.drag.accum += dy; |
| 1610 | // One swap per tile of travel, triggered after a minimal SWAP_DISTANCE. |
| 1611 | // A single large move (fast swipe) walks every tile; none are skipped. |
| 1612 | var guard = 0; |
| 1613 | while (guard++ < this.height + 2 |
| 1614 | && (this.drag.accum >= SWAP_DISTANCE || this.drag.accum <= -SWAP_DISTANCE)) |
| 1615 | { |
| 1616 | var dir = this.drag.accum > 0 ? 1 : -1; |
| 1617 | if (!this.swapOne(block, dir)) |
| 1618 | { |
| 1619 | if (dir > 0) |
| 1620 | { this.drag.accum = Math.min(this.drag.accum, SWAP_DISTANCE - 1e-9); } |
| 1621 | else |
| 1622 | { this.drag.accum = Math.max(this.drag.accum, -(SWAP_DISTANCE - 1e-9)); } |
| 1623 | break; |
| 1624 | } |
| 1625 | this.drag.accum -= dir; |
| 1626 | // A short move (SWAP_DISTANCE < 1) leaves a negative leftover; |
| 1627 | // do not let that immediately swap back the other way. |
| 1628 | if ((dir > 0 && this.drag.accum < 0) || (dir < 0 && this.drag.accum > 0)) |
| 1629 | { this.drag.accum = 0; } |
| 1630 | if (!this.isColumnStill(block.col)) |
| 1631 | { |
| 1632 | this.drag = null; |
| 1633 | return; |
| 1634 | } |
| 1635 | } |
| 1636 | var top = this.columnStackTopRow(block.col); |
| 1637 | var isTop = Math.abs(block.y - top) < ALIGN_EPS; |
| 1638 | if (this.drag.startedAsTop) |
| 1639 | { |
| 1640 | // Already the topmost block: pulling upward past its own position |
| 1641 | // (LAUNCH_PULL tiles above the press point) starts a manual jump. |
| 1642 | this.drag.launchIntent = isTop && (y > this.drag.startY + LAUNCH_PULL); |
| 1643 | } |
| 1644 | else |
| 1645 | { |
| 1646 | // Started mid-stack: swap first, and only launch once the pointer |
| 1647 | // is pulled into the empty space above the (new) top. |
| 1648 | this.drag.launchIntent = isTop && (y > top + 1 + 0.25); |
| 1649 | } |
| 1650 | }; |
| 1651 | |
| 1652 | /** Swap `block` one tile up (dir=+1) or down (dir=-1). Returns true if swapped. */ |
| 1653 | GameCore.prototype.swapOne = function (block, dir) |
| 1654 | { |
| 1655 | var neighbor = this.blockAt(block.col, block.y + dir); |
| 1656 | if (neighbor && this.isGrounded(neighbor) && neighbor !== block) |
| 1657 | { |
| 1658 | this.swapBlocks(block, neighbor); |
| 1659 | return true; |
| 1660 | } |
| 1661 | return false; |
| 1662 | }; |
| 1663 | |
| 1664 | GameCore.prototype.endDrag = function () |
| 1665 | { |
| 1666 | if (!this.drag) |
| 1667 | { return; } |
| 1668 | var block = this.blockById(this.drag.blockId); |
| 1669 | var launch = this.drag.launchIntent; |
| 1670 | this.drag = null; |
| 1671 | if (launch && block && block.state === STATE_STILL) |
| 1672 | { this.startManualLaunch(block); } |
| 1673 | else |
| 1674 | { this.resolveMatches(); } |
| 1675 | }; |
| 1676 | |
| 1677 | GameCore.prototype.blockById = function (id) |
| 1678 | { |
| 1679 | for (var i = 0; i < this.blocks.length; i++) |
| 1680 | { |
| 1681 | if (this.blocks[i].id === id) |
| 1682 | { return this.blocks[i]; } |
| 1683 | } |
| 1684 | return null; |
| 1685 | }; |
| 1686 | |
| 1687 | GameCore.prototype.columnStackTopRow = function (col) |
| 1688 | { |
| 1689 | var top = -1; |
| 1690 | var list = this.blocksInColumn(col); |
| 1691 | for (var i = 0; i < list.length; i++) |
| 1692 | { |
| 1693 | if (this.isGrounded(list[i]) && list[i].y > top) |
| 1694 | { top = list[i].y; } |
| 1695 | } |
| 1696 | return top; |
| 1697 | }; |
| 1698 | |
| 1699 | GameCore.prototype.swapToward = function (block, targetY) |
| 1700 | { |
| 1701 | var guard = 0; |
| 1702 | while (guard++ < this.height + 2) |
| 1703 | { |
| 1704 | var current = block.y; |
| 1705 | if (targetY > current + 0.5) |
| 1706 | { |
| 1707 | var above = this.blockAt(block.col, current + 1); |
| 1708 | if (above && this.isGrounded(above) && above !== block) |
| 1709 | { this.swapBlocks(block, above); } |
| 1710 | else |
| 1711 | { break; } |
| 1712 | } |
| 1713 | else if (targetY < current - 0.5) |
| 1714 | { |
| 1715 | var below = this.blockAt(block.col, current - 1); |
| 1716 | if (below && this.isGrounded(below) && below !== block) |
| 1717 | { this.swapBlocks(block, below); } |
| 1718 | else |
| 1719 | { break; } |
| 1720 | } |
| 1721 | else |
| 1722 | { break; } |
| 1723 | } |
| 1724 | }; |
| 1725 | |
| 1726 | GameCore.prototype.swapBlocks = function (a, b) |
| 1727 | { |
| 1728 | var y = a.y; |
| 1729 | a.y = b.y; |
| 1730 | b.y = y; |
| 1731 | }; |
| 1732 | |
| 1733 | /** |
| 1734 | * Test helper: stepwise-swap the block at (col, fromRow) to toRow. |
| 1735 | * Returns the moved block, or null if the column is not still. |
| 1736 | */ |
| 1737 | GameCore.prototype.swapTo = function (col, fromRow, toRow) |
| 1738 | { |
| 1739 | if (!this.isColumnStill(col)) |
| 1740 | { return null; } |
| 1741 | var block = this.blockAt(col, fromRow); |
| 1742 | if (!block) |
| 1743 | { return null; } |
| 1744 | this.swapToward(block, toRow); |
| 1745 | return block; |
| 1746 | }; |
| 1747 | |
| 1748 | /** |
| 1749 | * Test helper: a compact picture of grounded/falling/flying blocks. |
| 1750 | * Bottom row is index 0. Characters: 0-4 color, g gray flying/settling, |
| 1751 | * L locked, f falling, m manual, . empty. Flying colored blocks use |
| 1752 | * their color with a marker in `flying`. |
| 1753 | */ |
| 1754 | GameCore.prototype.snapshot = function () |
| 1755 | { |
| 1756 | var grid = []; |
| 1757 | for (var r = 0; r < this.height; r++) |
| 1758 | { |
| 1759 | var row = []; |
| 1760 | for (var c = 0; c < this.width; c++) |
| 1761 | { row.push('.'); } |
| 1762 | grid.push(row); |
| 1763 | } |
| 1764 | var airborne = []; |
| 1765 | for (var i = 0; i < this.blocks.length; i++) |
| 1766 | { |
| 1767 | var block = this.blocks[i]; |
| 1768 | var ch; |
| 1769 | if (block.state === STATE_LOCKED) |
| 1770 | { ch = 'L'; } |
| 1771 | else if (block.color === COLOR_GRAY) |
| 1772 | { ch = 'g'; } |
| 1773 | else |
| 1774 | { ch = String(block.color); } |
| 1775 | if (this.isGrounded(block) || block.state === STATE_FALLING) |
| 1776 | { |
| 1777 | var row = Math.round(block.y); |
| 1778 | if (row >= 0 && row < this.height) |
| 1779 | { grid[row][block.col] = ch; } |
| 1780 | } |
| 1781 | else |
| 1782 | { |
| 1783 | airborne.push({ |
| 1784 | id: block.id, |
| 1785 | col: block.col, |
| 1786 | y: block.y, |
| 1787 | color: block.color, |
| 1788 | state: block.state, |
| 1789 | clusterId: block.clusterId, |
| 1790 | flame: block.flame, |
| 1791 | vy: block.vy |
| 1792 | }); |
| 1793 | } |
| 1794 | } |
| 1795 | var lines = []; |
| 1796 | for (var r = this.height - 1; r >= 0; r--) |
| 1797 | { lines.push(grid[r].join('')); } |
| 1798 | return { grid: grid, picture: lines.join('\n'), airborne: airborne, score: this.score, state: this.state }; |
| 1799 | }; |
| 1800 | |
| 1801 | var GameCoreAPI = { |
| 1802 | GameCore: GameCore, |
| 1803 | COLOR_GRAY: COLOR_GRAY, |
| 1804 | STATE_STILL: STATE_STILL, |
| 1805 | STATE_FALLING: STATE_FALLING, |
| 1806 | STATE_FLYING: STATE_FLYING, |
| 1807 | STATE_SETTLING: STATE_SETTLING, |
| 1808 | STATE_LOCKED: STATE_LOCKED, |
| 1809 | STATE_MANUAL: STATE_MANUAL |
| 1810 | }; |
| 1811 | |
| 1812 | if (typeof module !== 'undefined' && module.exports) |
| 1813 | { module.exports = GameCoreAPI; } |
| 1814 | else if (typeof globalThis !== 'undefined') |
| 1815 | { |
| 1816 | globalThis.GameCore = GameCore; |
| 1817 | globalThis.GameCoreAPI = GameCoreAPI; |
| 1818 | } |
projects-timfalken-com meteors