@adatechnology/game-interactables-three 0.7.0 → 0.8.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.
package/dist/index.js CHANGED
@@ -1844,6 +1844,24 @@ function createThreeAdapter(THREE, options = {}) {
1844
1844
 
1845
1845
  // src/contents.ts
1846
1846
  import { discardObject3D } from "@adatechnology/game-shapes-three";
1847
+ var LAY_TURN = { x: [0, 0, -Math.PI / 2], z: [Math.PI / 2, 0, 0] };
1848
+ function placeOnSlot(THREE, object, at, yaw = 0, lay) {
1849
+ object.position.set(0, 0, 0);
1850
+ object.rotation.set(0, yaw, 0);
1851
+ if (!lay) {
1852
+ object.position.set(at[0], at[1], at[2]);
1853
+ return;
1854
+ }
1855
+ const [rx, ry, rz] = LAY_TURN[lay];
1856
+ object.quaternion.multiply(new THREE.Quaternion().setFromEuler(new THREE.Euler(rx, ry, rz)));
1857
+ const parent = object.parent;
1858
+ parent?.remove(object);
1859
+ object.updateMatrixWorld(true);
1860
+ const box = new THREE.Box3().setFromObject(object);
1861
+ parent?.add(object);
1862
+ const middle = box.getCenter(new THREE.Vector3());
1863
+ object.position.set(at[0] - middle.x, at[1] - box.min.y, at[2] - middle.z);
1864
+ }
1847
1865
  function createContentsView(THREE, options) {
1848
1866
  const { world, objectId, root, buildItem } = options;
1849
1867
  const group = new THREE.Group();
@@ -1867,19 +1885,18 @@ function createContentsView(THREE, options) {
1867
1885
  }
1868
1886
  return carrier;
1869
1887
  };
1870
- const place = (object, carrier, pos, part) => {
1888
+ const place = (object, carrier, slot, part) => {
1871
1889
  carrier.add(object);
1872
1890
  if (!part) {
1873
- object.position.set(pos[0], pos[1], pos[2]);
1891
+ placeOnSlot(THREE, object, slot.pos, 0, slot.lay);
1874
1892
  return;
1875
1893
  }
1876
1894
  const ry = part.ry ?? 0;
1877
- const x = pos[0] - part.pivot[0];
1878
- const z = pos[2] - part.pivot[2];
1895
+ const x = slot.pos[0] - part.pivot[0];
1896
+ const z = slot.pos[2] - part.pivot[2];
1879
1897
  const c = Math.cos(ry);
1880
1898
  const s = Math.sin(ry);
1881
- object.position.set(c * x - s * z, pos[1] - part.pivot[1], s * x + c * z);
1882
- object.rotation.set(0, -ry, 0);
1899
+ placeOnSlot(THREE, object, [c * x - s * z, slot.pos[1] - part.pivot[1], s * x + c * z], -ry, slot.lay);
1883
1900
  };
1884
1901
  function sync() {
1885
1902
  const live = /* @__PURE__ */ new Set();
@@ -1902,7 +1919,7 @@ function createContentsView(THREE, options) {
1902
1919
  if (!object) return;
1903
1920
  object.name = `contents_${c.id}_${i}`;
1904
1921
  object.userData.contents = { object: objectId, container: c.id, slot: i, item: stack.type, qty: stack.qty, ...stack.data ? { data: stack.data } : {} };
1905
- place(object, carrierOf(slot.part), slot.pos, slot.part === void 0 ? void 0 : partsById.get(slot.part));
1922
+ place(object, carrierOf(slot.part), slot, slot.part === void 0 ? void 0 : partsById.get(slot.part));
1906
1923
  now = { type: stack.type, object };
1907
1924
  drawn.set(key, now);
1908
1925
  }
@@ -1933,6 +1950,33 @@ function createContentsView(THREE, options) {
1933
1950
  }
1934
1951
  };
1935
1952
  }
1953
+ function drawSlotContents(THREE, group, spec, options) {
1954
+ const drawn = [];
1955
+ const draw = (container, slot, index, at, stack) => {
1956
+ const object = options.buildItem(stack, THREE);
1957
+ if (!object) return;
1958
+ group.add(object);
1959
+ placeOnSlot(THREE, object, slot.pos, 0, slot.lay);
1960
+ object.name = `contents_${container.id}_${at}`;
1961
+ object.userData.contents = { container: container.id, slot: index, item: stack.type, qty: stack.qty, ...stack.data ? { data: stack.data } : {} };
1962
+ drawn.push(object);
1963
+ };
1964
+ for (const container of spec.containers ?? []) {
1965
+ const slots = container.slots.map((slot, index) => ({ slot, index })).filter(({ slot }) => !slot.part);
1966
+ const fixed = container.contents ?? [];
1967
+ slots.forEach(({ slot, index }, at) => {
1968
+ const stack = fixed[index];
1969
+ if (stack) draw(container, slot, index, at, stack);
1970
+ });
1971
+ const free = slots.map((entry, at) => ({ ...entry, at })).filter(({ index }) => !fixed[index]);
1972
+ if (!options.roll || free.length === 0) continue;
1973
+ options.roll(container, free.map(({ slot }) => slot)).forEach((stack, n) => {
1974
+ const entry = free[n];
1975
+ if (stack && entry) draw(container, entry.slot, entry.index, entry.at, stack);
1976
+ });
1977
+ }
1978
+ return drawn;
1979
+ }
1936
1980
 
1937
1981
  // src/cord.ts
1938
1982
  import { catenary as catenary2, createCord as createCord2 } from "@adatechnology/game-shapes-three";
@@ -2225,8 +2269,10 @@ export {
2225
2269
  createScreen,
2226
2270
  createSpanWire,
2227
2271
  createThreeAdapter,
2272
+ drawSlotContents,
2228
2273
  mergeParts,
2229
2274
  patchSwayShader,
2275
+ placeOnSlot,
2230
2276
  poseOf,
2231
2277
  rememberRest,
2232
2278
  standOutCoincidentBoxes,