@adatechnology/game-terrain 0.2.0 → 0.2.2

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
@@ -481,6 +481,16 @@ function stratumAtDepth(profile, depth) {
481
481
  const stratum = profile.strata[index];
482
482
  return { index, stratum, top: top - stratum.thickness };
483
483
  }
484
+ function stratumMaterialAtDepth(profile, depth) {
485
+ const strata = profile.strata;
486
+ let top = 0;
487
+ for (let index = 0; index < strata.length; index++) {
488
+ const stratum = strata[index];
489
+ if (depth < top + stratum.thickness - 1e-6) return stratum.material;
490
+ top += stratum.thickness;
491
+ }
492
+ return strata[strata.length - 1].material;
493
+ }
484
494
  var totalThickness = (profile) => profile.strata.reduce((sum, stratum) => sum + stratum.thickness, 0);
485
495
  function createTerrain(options) {
486
496
  const [x0, z0, x1, z1] = options.bounds;
@@ -596,7 +606,7 @@ function createTerrain(options) {
596
606
  };
597
607
  function vertexMaterial(column, row) {
598
608
  const index = row * stride + column;
599
- return materials.get(stratumAtDepth(profileList[profileIndex[index]], dug[index]).stratum.material);
609
+ return materials.get(stratumMaterialAtDepth(profileList[profileIndex[index]], dug[index]));
600
610
  }
601
611
  function columnAt(x, z) {
602
612
  const { index } = vertexOf(x, z);
@@ -1117,24 +1127,49 @@ function createTerrain(options) {
1117
1127
  var PART_ID = "ground";
1118
1128
  var PART_DEPTH = 3;
1119
1129
  var RAY_BACKOFF = 1;
1130
+ var GROUND_HP = 1e12;
1120
1131
  function createTerrainImpl(terrain, options) {
1121
1132
  const { id } = options;
1122
1133
  const [x0, z0, x1, z1] = terrain.bounds;
1123
- const spec = () => ({
1134
+ const centerColumn = Math.max(0, Math.min(terrain.columns, Math.round((x1 - x0) / 2 / terrain.cellSize)));
1135
+ const centerRow = Math.max(0, Math.min(terrain.rows, Math.round((z1 - z0) / 2 / terrain.cellSize)));
1136
+ const centerMaterial = () => terrain.vertexMaterial(centerColumn, centerRow).id;
1137
+ const buildSpec = (material) => ({
1124
1138
  name: options.name ?? "Ground",
1125
1139
  parts: [
1126
1140
  {
1127
1141
  id: PART_ID,
1128
- material: terrain.columnAt((x0 + x1) / 2, (z0 + z1) / 2).material,
1142
+ material,
1129
1143
  shape: { kind: "box", size: [x1 - x0, PART_DEPTH, z1 - z0] },
1130
1144
  position: [(x0 + x1) / 2, -PART_DEPTH / 2, (z0 + z1) / 2],
1131
- hp: 1e12,
1145
+ hp: GROUND_HP,
1132
1146
  anchored: true,
1133
1147
  tags: ["terrain"]
1134
1148
  }
1135
1149
  ],
1136
1150
  joints: []
1137
1151
  });
1152
+ let cachedSpec;
1153
+ const spec = () => {
1154
+ const material = centerMaterial();
1155
+ if (cachedSpec?.parts[0]?.material !== material) cachedSpec = buildSpec(material);
1156
+ return cachedSpec;
1157
+ };
1158
+ const buildState = (material) => ({
1159
+ id,
1160
+ position: [0, 0, 0],
1161
+ rotation: [0, 0, 0, 1],
1162
+ parts: [{ id: PART_ID, material, status: "intact", hp: GROUND_HP, maxHp: GROUND_HP, mass: 0, temperature: 0, burning: false, fuel: 0, char: 0, melt: 0, dents: [], holes: [], cracks: [], group: 0 }],
1163
+ joints: [],
1164
+ groups: [[PART_ID]],
1165
+ destroyed: false
1166
+ });
1167
+ let cachedState;
1168
+ const state = () => {
1169
+ const material = centerMaterial();
1170
+ if (cachedState?.parts[0]?.material !== material) cachedState = buildState(material);
1171
+ return cachedState;
1172
+ };
1138
1173
  const resolve = (hit) => {
1139
1174
  if (hit.type !== "ballistic" && hit.type !== "blunt" && hit.type !== "slash") return hit;
1140
1175
  if (!hit.direction) return { ...hit, point: [hit.point[0], terrain.heightAt(hit.point[0], hit.point[2]), hit.point[2]] };
@@ -1152,26 +1187,20 @@ function createTerrainImpl(terrain, options) {
1152
1187
  const result = terrain.applyHit(landed);
1153
1188
  if (!result || result.radius <= 0) return [];
1154
1189
  options.onBlow?.(result, landed);
1155
- return [{ type: "damage", object: id, time: 0, part: PART_ID, amount: 0, hp: 1e12, damageType: hit.type, ...hit.source ? { source: hit.source } : {} }];
1190
+ return [{ type: "damage", object: id, time: 0, part: PART_ID, amount: 0, hp: GROUND_HP, damageType: hit.type, ...hit.source ? { source: hit.source } : {} }];
1156
1191
  },
1157
1192
  update(dt) {
1158
1193
  terrain.update(dt);
1159
1194
  return [];
1160
1195
  },
1161
- state() {
1162
- return {
1163
- id,
1164
- position: [0, 0, 0],
1165
- rotation: [0, 0, 0, 1],
1166
- parts: [],
1167
- joints: [],
1168
- groups: [[PART_ID]],
1169
- destroyed: false
1170
- };
1196
+ state,
1197
+ reset() {
1198
+ terrain.reset();
1171
1199
  },
1172
- reset: () => terrain.reset(),
1173
1200
  serialize: () => terrain.serialize(),
1174
- load: (data) => terrain.load(data)
1201
+ load(data) {
1202
+ terrain.load(data);
1203
+ }
1175
1204
  };
1176
1205
  }
1177
1206