@adatechnology/game-terrain 0.9.11 → 0.9.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -409,6 +409,7 @@ var REST_GAP = 0.01;
409
409
  function createDebrisStore(max) {
410
410
  const list = [];
411
411
  let serial = 0;
412
+ let version = 0;
412
413
  function makeRoom() {
413
414
  if (list.length < max()) return;
414
415
  let at = 0;
@@ -429,7 +430,11 @@ function createDebrisStore(max) {
429
430
  }
430
431
  return {
431
432
  list,
433
+ get version() {
434
+ return version;
435
+ },
432
436
  throwPiece(spec) {
437
+ version++;
433
438
  makeRoom();
434
439
  list.push({ ...spec, id: ++serial, born: serial, atRest: false, bounces: 0 });
435
440
  },
@@ -439,9 +444,13 @@ function createDebrisStore(max) {
439
444
  if (piece.atRest) {
440
445
  const under = world.heightAt(piece.position[0], piece.position[2]) + piece.radius * 0.6;
441
446
  if (piece.position[1] > under + REST_GAP) piece.atRest = false;
442
- else if (piece.position[1] < under - REST_GAP) piece.position[1] = under;
447
+ else if (piece.position[1] < under - REST_GAP) {
448
+ piece.position[1] = under;
449
+ version++;
450
+ }
443
451
  if (piece.atRest) continue;
444
452
  }
453
+ version++;
445
454
  const { position, velocity } = piece;
446
455
  velocity[1] -= world.gravity * dt;
447
456
  position[0] += velocity[0] * dt;
@@ -463,6 +472,7 @@ function createDebrisStore(max) {
463
472
  }
464
473
  },
465
474
  clear() {
475
+ version++;
466
476
  list.length = 0;
467
477
  serial = 0;
468
478
  }
@@ -728,6 +738,9 @@ var BARS_SHARE_REACH = 0.6;
728
738
  var RIM_REACH = 1.45;
729
739
  var FLAME_MINIMUM = 200;
730
740
  var FLAME_REACH = 0.6;
741
+ var SCORCH_STEPS = 64;
742
+ var BURN_AWAY_STEPS = 1e3;
743
+ var visibleStep = (value, steps) => Math.floor(value * steps);
731
744
  var BLAST_HEIGHT_REACH = 0.35;
732
745
  var BLAST_SOOT_REACH = 1.2;
733
746
  var BLAST_SOOT = 0.9;
@@ -1616,19 +1629,25 @@ function createTerrain(options) {
1616
1629
  }
1617
1630
  function burn(spec) {
1618
1631
  if (spec.temperature < FLAME_MINIMUM) return;
1619
- const column0 = clampIndex(Math.floor((spec.x - spec.radius - x0) / cellSize), columns);
1620
- const column1 = clampIndex(Math.ceil((spec.x + spec.radius - x0) / cellSize), columns);
1621
- const row0 = clampIndex(Math.floor((spec.z - spec.radius - z0) / cellSize), rows);
1622
- const row1 = clampIndex(Math.ceil((spec.z + spec.radius - z0) / cellSize), rows);
1632
+ burnDisc(spec.x, spec.z, spec.radius, spec.seconds);
1633
+ }
1634
+ function burnDisc(x, z, radius, seconds) {
1635
+ const column0 = clampIndex(Math.floor((x - radius - x0) / cellSize), columns);
1636
+ const column1 = clampIndex(Math.ceil((x + radius - x0) / cellSize), columns);
1637
+ const row0 = clampIndex(Math.floor((z - radius - z0) / cellSize), rows);
1638
+ const row1 = clampIndex(Math.ceil((z + radius - z0) / cellSize), rows);
1639
+ const reach = radius * radius;
1623
1640
  let melted = false;
1624
1641
  for (let row = row0; row <= row1; row++) {
1642
+ const dz = z0 + row * cellSize - z;
1625
1643
  for (let column = column0; column <= column1; column++) {
1626
- if (Math.hypot(x0 + column * cellSize - spec.x, z0 + row * cellSize - spec.z) > spec.radius) continue;
1644
+ const dx = x0 + column * cellSize - x;
1645
+ if (dx * dx + dz * dz > reach) continue;
1627
1646
  if (hasWater && base[row * stride + column] < surface[row * stride + column]) continue;
1628
- melted = burnVertex(column, row, spec.seconds) || melted;
1647
+ melted = burnVertex(column, row, seconds) || melted;
1629
1648
  }
1630
1649
  }
1631
- if (melted) emitDeform(spec.x, spec.z, spec.radius);
1650
+ if (melted) emitDeform(x, z, radius);
1632
1651
  }
1633
1652
  function soot(x, z, radius) {
1634
1653
  if (radius <= 0) return;
@@ -1655,8 +1674,9 @@ function createTerrain(options) {
1655
1674
  if (!response) return false;
1656
1675
  const before = scorch[index];
1657
1676
  scorch[index] = Math.max(before, Math.min(response.maxScorch, before + response.scorchPerSecond * seconds));
1677
+ const dugBefore = dug[index];
1658
1678
  if (response.burnsAway) removeFromVertex(index, response.burnsAway * seconds);
1659
- if (scorch[index] !== before || response.burnsAway) touchVertex(column, row);
1679
+ if (visibleStep(scorch[index], SCORCH_STEPS) !== visibleStep(before, SCORCH_STEPS) || visibleStep(dug[index], BURN_AWAY_STEPS) !== visibleStep(dugBefore, BURN_AWAY_STEPS)) touchVertex(column, row);
1660
1680
  if (response.flammable) ignite(x0 + column * cellSize, z0 + row * cellSize);
1661
1681
  return (response.burnsAway ?? 0) > 0;
1662
1682
  }
@@ -1668,13 +1688,24 @@ function createTerrain(options) {
1668
1688
  if (scorch[index] >= 1) return;
1669
1689
  burning.set(key, FIRE_SECONDS);
1670
1690
  }
1691
+ const fireKeys = [];
1692
+ const fireLeft = [];
1693
+ const keepFireCell = (remaining, key) => {
1694
+ fireKeys.push(key);
1695
+ fireLeft.push(remaining);
1696
+ };
1671
1697
  function updateFire(dt) {
1672
- for (const [key, remaining] of [...burning]) {
1698
+ fireKeys.length = 0;
1699
+ fireLeft.length = 0;
1700
+ burning.forEach(keepFireCell);
1701
+ for (let cell = 0; cell < fireKeys.length; cell++) {
1702
+ const key = fireKeys[cell];
1703
+ const remaining = fireLeft[cell];
1673
1704
  const cellRow = Math.floor(key / 65536);
1674
1705
  const cellColumn = key - cellRow * 65536;
1675
1706
  const x = x0 + (cellColumn + 0.5) * FIRE_CELL;
1676
1707
  const z = z0 + (cellRow + 0.5) * FIRE_CELL;
1677
- burn({ x, z, radius: FIRE_CELL * 0.75, temperature: 600, seconds: dt });
1708
+ burnDisc(x, z, FIRE_CELL * 0.75, dt);
1678
1709
  if (random() < FIRE_SPREAD_PER_SECOND * dt) {
1679
1710
  const angle = Math.floor(random() * 4) * (Math.PI / 2);
1680
1711
  const nextX = x + Math.cos(angle) * FIRE_CELL;
@@ -1782,7 +1813,7 @@ function createTerrain(options) {
1782
1813
  case "fire":
1783
1814
  case "heat":
1784
1815
  if (y - heightAt(x, z) > (hit.radius ?? 0.2) + FLAME_REACH) return null;
1785
- burn({ x, z, radius: hit.radius ?? 0.2, temperature: hit.temperature ?? 0, seconds: hit.duration ?? 0.016 });
1816
+ if ((hit.temperature ?? 0) >= FLAME_MINIMUM) burnDisc(x, z, hit.radius ?? 0.2, hit.duration ?? 0.016);
1786
1817
  return null;
1787
1818
  default:
1788
1819
  return null;
@@ -1935,6 +1966,9 @@ function createTerrain(options) {
1935
1966
  return cracks.version;
1936
1967
  },
1937
1968
  debris: () => debrisStore.list,
1969
+ get debrisVersion() {
1970
+ return debrisStore.version;
1971
+ },
1938
1972
  burningPoints: () => [...burning.keys()].map((key) => {
1939
1973
  const cellRow = Math.floor(key / 65536);
1940
1974
  const cellColumn = key - cellRow * 65536;