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