@adatechnology/game-crowd 0.7.1 → 0.8.0

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
@@ -1861,6 +1861,7 @@ var DEFAULT_DRIVE_FORCE = 250;
1861
1861
  var GRAVITY_FORCE2 = 9.8;
1862
1862
  var EXTRA_BODIES = 500;
1863
1863
  var PAIRS_PER_AGENT3 = 12;
1864
+ var KNOCK_STUN_SECONDS = 1;
1864
1865
  function createPhysics(base, options, stats = createPhysicsStats()) {
1865
1866
  const moundOn = options.mound !== null;
1866
1867
  const ctx = { ...base, options, stats, step: -1, h: 0, centre: { x: 0, z: 0 }, lastBlastStep: -1e3, moundAt: (cell) => moundOn ? base.moundHeight[cell] : 0, weightOn: () => 0 };
@@ -1998,6 +1999,20 @@ function createPhysics(base, options, stats = createPhysicsStats()) {
1998
1999
  balance,
1999
2000
  beginStep,
2000
2001
  endStep,
2002
+ knockDown: (i, vx, vz) => {
2003
+ if (i < 0 || i >= store.n || store.hp[i] <= 0) return;
2004
+ if (!balance) {
2005
+ store.state[i] = STATE.STUNNED;
2006
+ store.stateTimer[i] = KNOCK_STUN_SECONDS;
2007
+ store.vx[i] = vx;
2008
+ store.vz[i] = vz;
2009
+ return;
2010
+ }
2011
+ if (store.balance[i] !== BALANCE.STANDING && store.balance[i] !== BALANCE.STAGGERING) return;
2012
+ store.pushX[i] += vx;
2013
+ store.pushZ[i] += vz;
2014
+ balance.requestFall(i, FALL_CAUSE.SHOVE, Math.hypot(vx, vz) / options.balance.shoveFall);
2015
+ },
2001
2016
  remap: (removed, moved) => {
2002
2017
  thrown?.remap(removed, moved);
2003
2018
  offload?.remap(removed, moved);
@@ -3690,6 +3705,7 @@ function createCrowd(options) {
3690
3705
  mound: moundHeight,
3691
3706
  thrown: engine.thrown,
3692
3707
  blast: (blast) => engine.blasts.enqueue(blast),
3708
+ knockDown: (i, velocity) => engine.knockDown(i, velocity.x, velocity.z),
3693
3709
  pileCount: () => pileN,
3694
3710
  trail,
3695
3711
  variantsOf,
@@ -3762,6 +3778,12 @@ function createCrowd(options) {
3762
3778
  return crowd;
3763
3779
  }
3764
3780
 
3781
+ // src/corpse-relocate.ts
3782
+ var relocateCorpse = (crowd, move) => {
3783
+ const entry = crowd.corpses.entries.find((candidate) => candidate.id === move.id);
3784
+ return entry !== void 0 && crowd.corpses.replace(move.id, { ...entry.data, x: move.x, z: move.z, yaw: move.yaw });
3785
+ };
3786
+
3765
3787
  // src/obstacles.ts
3766
3788
  import { NAV_OBSTACLE_KIND } from "@adatechnology/game-contracts";
3767
3789
  var LOW_WALL_MAX_HEIGHT = 1.2;
@@ -4709,6 +4731,187 @@ var hordeSimModule = Object.freeze({
4709
4731
  function hordeSnapshotBytes(capacity) {
4710
4732
  return hordeSnapshotLayout(createStore(capacity)).bytes;
4711
4733
  }
4734
+
4735
+ // src/engine-agents.ts
4736
+ var STILL = [0, 0, 0];
4737
+ var DEFAULT_RADIUS = 0.25;
4738
+ var DEFAULT_HEIGHT = 1.7;
4739
+ var createAgentBodies = (loop, crowd, options = {}) => {
4740
+ const { store } = crowd;
4741
+ const world = loop.world;
4742
+ const radius = options.radius ?? DEFAULT_RADIUS;
4743
+ const fallbackHeight = options.fallbackHeight ?? DEFAULT_HEIGHT;
4744
+ const bodies = [];
4745
+ const centreHeight = new Float32Array(store.capacity);
4746
+ const target = [0, 0, 0];
4747
+ const rotation = [0, 0, 0, 1];
4748
+ const shouldTeleport = new Uint8Array(store.capacity);
4749
+ const delivered = new Float64Array(store.capacity * 4);
4750
+ const isSettled = new Uint8Array(store.capacity);
4751
+ const stopTakingVacancies = [
4752
+ crowd.on("remove", (event) => {
4753
+ if (event.moved >= 0) shouldTeleport[event.agent] = 1;
4754
+ }),
4755
+ crowd.on("spawn", (event) => {
4756
+ shouldTeleport[event.agent] = 1;
4757
+ })
4758
+ ];
4759
+ const create = (i) => {
4760
+ const height = store.height[i] > 0 ? store.height[i] : fallbackHeight;
4761
+ const halfHeight = Math.max(0, height / 2 - radius);
4762
+ centreHeight[i] = halfHeight + radius;
4763
+ return world.createBody({
4764
+ shape: { kind: "capsule", halfHeight, radius },
4765
+ motion: "kinematic",
4766
+ position: [store.x[i], store.y[i] + centreHeight[i], store.z[i]],
4767
+ layer: "agent",
4768
+ user: i
4769
+ });
4770
+ };
4771
+ const beforeStep = (h) => {
4772
+ const count = store.n;
4773
+ while (bodies.length > count) world.removeBody(bodies.pop());
4774
+ for (let i = 0; i < count; i++) {
4775
+ const body = bodies[i];
4776
+ if (body === void 0) {
4777
+ bodies.push(create(i));
4778
+ shouldTeleport[i] = 0;
4779
+ isSettled[i] = 0;
4780
+ delivered[i * 4] = Number.NaN;
4781
+ continue;
4782
+ }
4783
+ const x = store.x[i];
4784
+ const y = store.y[i] + centreHeight[i];
4785
+ const z = store.z[i];
4786
+ const yaw = store.yaw[i];
4787
+ const at = i * 4;
4788
+ const isSamePose = delivered[at] === x && delivered[at + 1] === y && delivered[at + 2] === z && delivered[at + 3] === yaw;
4789
+ if (isSamePose && isSettled[i] && !shouldTeleport[i]) continue;
4790
+ const half = yaw / 2;
4791
+ target[0] = x;
4792
+ target[1] = y;
4793
+ target[2] = z;
4794
+ rotation[1] = Math.sin(half);
4795
+ rotation[3] = Math.cos(half);
4796
+ delivered[at] = x;
4797
+ delivered[at + 1] = y;
4798
+ delivered[at + 2] = z;
4799
+ delivered[at + 3] = yaw;
4800
+ isSettled[i] = isSamePose ? 1 : 0;
4801
+ if (shouldTeleport[i]) {
4802
+ shouldTeleport[i] = 0;
4803
+ world.setPose(body, target, rotation, true);
4804
+ world.setVelocity(body, STILL, STILL);
4805
+ isSettled[i] = 1;
4806
+ } else world.moveKinematic(body, target, rotation, h);
4807
+ }
4808
+ };
4809
+ const removePhase = loop.add({ beforeStep }, "crowd");
4810
+ return {
4811
+ get count() {
4812
+ return bodies.length;
4813
+ },
4814
+ bodyOf: (i) => bodies[i],
4815
+ dispose: () => {
4816
+ removePhase();
4817
+ for (const stop of stopTakingVacancies) stop();
4818
+ for (const body of bodies) if (world.has(body)) world.removeBody(body);
4819
+ bodies.length = 0;
4820
+ }
4821
+ };
4822
+ };
4823
+
4824
+ // src/engine-doors.ts
4825
+ var DEFAULT_LEAF_MASS = 40;
4826
+ var DEFAULT_BREAK_IMPULSE = 40;
4827
+ var LEAF_HALF_THICKNESS = 0.03;
4828
+ var HINGE_PLAY = 0.05;
4829
+ var createDoorBodies = (loop, doors, options = {}) => {
4830
+ const world = loop.world;
4831
+ const leafMass = options.leafMass ?? DEFAULT_LEAF_MASS;
4832
+ const breakImpulse = options.breakImpulse ?? DEFAULT_BREAK_IMPULSE;
4833
+ const height = options.height ?? DOOR_DEFAULTS.height;
4834
+ const hung = /* @__PURE__ */ new Map();
4835
+ const doorOfHinge = /* @__PURE__ */ new Map();
4836
+ const tornLeaves = /* @__PURE__ */ new Map();
4837
+ const removeTornLeaf = (index) => {
4838
+ const leaf = tornLeaves.get(index);
4839
+ if (leaf === void 0) return;
4840
+ tornLeaves.delete(index);
4841
+ if (world.has(leaf)) world.removeBody(leaf);
4842
+ };
4843
+ const hang = (index) => {
4844
+ const door = doors.list[index];
4845
+ if (!door || door.open || door.broken || hung.has(index)) return;
4846
+ removeTornLeaf(index);
4847
+ const alongX = door.axis === "x";
4848
+ const half = door.width / 2;
4849
+ const hingeAt = alongX ? [door.x - half, height / 2, door.z] : [door.x, height / 2, door.z - half];
4850
+ const frame = world.createBody({
4851
+ shape: { kind: "box", half: [0.05, height / 2, 0.05] },
4852
+ motion: "static",
4853
+ position: hingeAt,
4854
+ user: -1
4855
+ });
4856
+ const leaf = world.createBody({
4857
+ shape: { kind: "box", half: alongX ? [half - 0.02, height / 2 - 0.02, LEAF_HALF_THICKNESS] : [LEAF_HALF_THICKNESS, height / 2 - 0.02, half - 0.02] },
4858
+ motion: "dynamic",
4859
+ mass: leafMass,
4860
+ position: [door.x, height / 2, door.z],
4861
+ layer: "prop",
4862
+ user: -2 - index
4863
+ });
4864
+ const hinge = world.createJoint({
4865
+ kind: "hinge",
4866
+ a: frame,
4867
+ b: leaf,
4868
+ point: hingeAt,
4869
+ axis: [0, 1, 0],
4870
+ normal: alongX ? [1, 0, 0] : [0, 0, 1],
4871
+ min: -HINGE_PLAY,
4872
+ max: HINGE_PLAY,
4873
+ breakImpulse
4874
+ });
4875
+ hung.set(index, { door: index, frame, leaf, hinge });
4876
+ doorOfHinge.set(hinge, index);
4877
+ };
4878
+ const drop = (entry, keepLeaf) => {
4879
+ doorOfHinge.delete(entry.hinge);
4880
+ hung.delete(entry.door);
4881
+ if (world.jointExists(entry.hinge)) world.removeJoint(entry.hinge);
4882
+ if (world.has(entry.frame)) world.removeBody(entry.frame);
4883
+ if (!keepLeaf && world.has(entry.leaf)) world.removeBody(entry.leaf);
4884
+ };
4885
+ const afterSteps = () => {
4886
+ for (const joint of world.brokenJoints) {
4887
+ const index = doorOfHinge.get(joint);
4888
+ const entry = index === void 0 ? void 0 : hung.get(index);
4889
+ if (!entry) continue;
4890
+ drop(entry, true);
4891
+ removeTornLeaf(entry.door);
4892
+ tornLeaves.set(entry.door, entry.leaf);
4893
+ doors.hit(entry.door, doors.list[entry.door].hp);
4894
+ }
4895
+ sync();
4896
+ };
4897
+ const sync = () => {
4898
+ for (const entry of hung.values()) {
4899
+ const door = doors.list[entry.door];
4900
+ if (door.open || door.broken) drop(entry, false);
4901
+ }
4902
+ for (let index = 0; index < doors.list.length; index++) hang(index);
4903
+ };
4904
+ const removePhase = loop.add({ afterSteps }, "contacts");
4905
+ sync();
4906
+ return {
4907
+ leafOf: (index) => hung.get(index)?.leaf,
4908
+ dispose: () => {
4909
+ removePhase();
4910
+ for (const entry of [...hung.values()]) drop(entry, false);
4911
+ for (const index of [...tornLeaves.keys()]) removeTornLeaf(index);
4912
+ }
4913
+ };
4914
+ };
4712
4915
  export {
4713
4916
  AGENT_FIELDS,
4714
4917
  BALANCE,
@@ -4729,10 +4932,12 @@ export {
4729
4932
  bindNavObstacles,
4730
4933
  carrionCenter,
4731
4934
  contactStep,
4935
+ createAgentBodies,
4732
4936
  createCivilians,
4733
4937
  createContactView,
4734
4938
  createCoordinatorRunner,
4735
4939
  createCrowd,
4940
+ createDoorBodies,
4736
4941
  createDoors,
4737
4942
  createFlock,
4738
4943
  createHerd,
@@ -4748,6 +4953,7 @@ export {
4748
4953
  identityOf,
4749
4954
  mix32,
4750
4955
  readHordeSnapshot,
4956
+ relocateCorpse,
4751
4957
  resolvePhysics,
4752
4958
  unit,
4753
4959
  walksInto,