@adatechnology/game-terrain 0.2.0 → 0.2.1

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.
@@ -516,6 +516,16 @@ var GameTerrain = (() => {
516
516
  const stratum = profile.strata[index];
517
517
  return { index, stratum, top: top - stratum.thickness };
518
518
  }
519
+ function stratumMaterialAtDepth(profile, depth) {
520
+ const strata = profile.strata;
521
+ let top = 0;
522
+ for (let index = 0; index < strata.length; index++) {
523
+ const stratum = strata[index];
524
+ if (depth < top + stratum.thickness - 1e-6) return stratum.material;
525
+ top += stratum.thickness;
526
+ }
527
+ return strata[strata.length - 1].material;
528
+ }
519
529
  var totalThickness = (profile) => profile.strata.reduce((sum, stratum) => sum + stratum.thickness, 0);
520
530
  function createTerrain(options) {
521
531
  const [x0, z0, x1, z1] = options.bounds;
@@ -631,7 +641,7 @@ var GameTerrain = (() => {
631
641
  };
632
642
  function vertexMaterial(column, row) {
633
643
  const index = row * stride + column;
634
- return materials.get(stratumAtDepth(profileList[profileIndex[index]], dug[index]).stratum.material);
644
+ return materials.get(stratumMaterialAtDepth(profileList[profileIndex[index]], dug[index]));
635
645
  }
636
646
  function columnAt(x, z) {
637
647
  const { index } = vertexOf(x, z);
@@ -1152,24 +1162,49 @@ var GameTerrain = (() => {
1152
1162
  var PART_ID = "ground";
1153
1163
  var PART_DEPTH = 3;
1154
1164
  var RAY_BACKOFF = 1;
1165
+ var GROUND_HP = 1e12;
1155
1166
  function createTerrainImpl(terrain, options) {
1156
1167
  const { id } = options;
1157
1168
  const [x0, z0, x1, z1] = terrain.bounds;
1158
- const spec = () => ({
1169
+ const centerColumn = Math.max(0, Math.min(terrain.columns, Math.round((x1 - x0) / 2 / terrain.cellSize)));
1170
+ const centerRow = Math.max(0, Math.min(terrain.rows, Math.round((z1 - z0) / 2 / terrain.cellSize)));
1171
+ const centerMaterial = () => terrain.vertexMaterial(centerColumn, centerRow).id;
1172
+ const buildSpec = (material) => ({
1159
1173
  name: options.name ?? "Ground",
1160
1174
  parts: [
1161
1175
  {
1162
1176
  id: PART_ID,
1163
- material: terrain.columnAt((x0 + x1) / 2, (z0 + z1) / 2).material,
1177
+ material,
1164
1178
  shape: { kind: "box", size: [x1 - x0, PART_DEPTH, z1 - z0] },
1165
1179
  position: [(x0 + x1) / 2, -PART_DEPTH / 2, (z0 + z1) / 2],
1166
- hp: 1e12,
1180
+ hp: GROUND_HP,
1167
1181
  anchored: true,
1168
1182
  tags: ["terrain"]
1169
1183
  }
1170
1184
  ],
1171
1185
  joints: []
1172
1186
  });
1187
+ let cachedSpec;
1188
+ const spec = () => {
1189
+ const material = centerMaterial();
1190
+ if (cachedSpec?.parts[0]?.material !== material) cachedSpec = buildSpec(material);
1191
+ return cachedSpec;
1192
+ };
1193
+ const buildState = (material) => ({
1194
+ id,
1195
+ position: [0, 0, 0],
1196
+ rotation: [0, 0, 0, 1],
1197
+ 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 }],
1198
+ joints: [],
1199
+ groups: [[PART_ID]],
1200
+ destroyed: false
1201
+ });
1202
+ let cachedState;
1203
+ const state = () => {
1204
+ const material = centerMaterial();
1205
+ if (cachedState?.parts[0]?.material !== material) cachedState = buildState(material);
1206
+ return cachedState;
1207
+ };
1173
1208
  const resolve = (hit) => {
1174
1209
  if (hit.type !== "ballistic" && hit.type !== "blunt" && hit.type !== "slash") return hit;
1175
1210
  if (!hit.direction) return { ...hit, point: [hit.point[0], terrain.heightAt(hit.point[0], hit.point[2]), hit.point[2]] };
@@ -1187,26 +1222,20 @@ var GameTerrain = (() => {
1187
1222
  const result = terrain.applyHit(landed);
1188
1223
  if (!result || result.radius <= 0) return [];
1189
1224
  options.onBlow?.(result, landed);
1190
- return [{ type: "damage", object: id, time: 0, part: PART_ID, amount: 0, hp: 1e12, damageType: hit.type, ...hit.source ? { source: hit.source } : {} }];
1225
+ return [{ type: "damage", object: id, time: 0, part: PART_ID, amount: 0, hp: GROUND_HP, damageType: hit.type, ...hit.source ? { source: hit.source } : {} }];
1191
1226
  },
1192
1227
  update(dt) {
1193
1228
  terrain.update(dt);
1194
1229
  return [];
1195
1230
  },
1196
- state() {
1197
- return {
1198
- id,
1199
- position: [0, 0, 0],
1200
- rotation: [0, 0, 0, 1],
1201
- parts: [],
1202
- joints: [],
1203
- groups: [[PART_ID]],
1204
- destroyed: false
1205
- };
1231
+ state,
1232
+ reset() {
1233
+ terrain.reset();
1206
1234
  },
1207
- reset: () => terrain.reset(),
1208
1235
  serialize: () => terrain.serialize(),
1209
- load: (data) => terrain.load(data)
1236
+ load(data) {
1237
+ terrain.load(data);
1238
+ }
1210
1239
  };
1211
1240
  }
1212
1241