@adatechnology/game-vehicles-three 0.12.1 → 0.13.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.
@@ -49425,6 +49425,11 @@ var GameVehiclesThree = (() => {
49425
49425
  }
49426
49426
  };
49427
49427
  }
49428
+ function runOver(speed, rules = {}) {
49429
+ const { knockSpeed, killSpeed } = { ...defaultArcadeSpec.runOver, ...rules };
49430
+ const s = Math.abs(speed);
49431
+ return s > killSpeed ? "kill" : s > knockSpeed ? "knock" : "push";
49432
+ }
49428
49433
  var defaultFuelModel = { idleRate: 3e-4, fullRate: 35e-4, loadFactor: 0.5, damagedEngineExtra: 0.4 };
49429
49434
  function fuelRate(model, rpmFraction, throttle, loadRatio, engineHealth) {
49430
49435
  const base = model.idleRate + model.fullRate * Math.max(0, Math.min(1, rpmFraction)) * Math.max(0, Math.min(1, throttle));
@@ -50427,6 +50432,150 @@ var GameVehiclesThree = (() => {
50427
50432
  }
50428
50433
  };
50429
50434
  }
50435
+ var byIndexDescending = (a, b) => b - a;
50436
+ var MIN_SPEED = 1;
50437
+ var REFERENCE_MASS = 70;
50438
+ var PUSH_ASIDE_SPEED = 1;
50439
+ var createRunOver = (loop, options) => {
50440
+ const { crowd, agents } = options;
50441
+ const rules = { ...defaultArcadeSpec.runOver, ...options.rules };
50442
+ const world = loop.world;
50443
+ const vehicles = [];
50444
+ const struck = /* @__PURE__ */ new Map();
50445
+ const hits = [];
50446
+ const pose = new Float64Array(7);
50447
+ const velocity = new Float64Array(6);
50448
+ const box2 = { kind: "box", half: [0, 0, 0] };
50449
+ const centre = [0, 0, 0];
50450
+ const rotation = [0, 0, 0, 1];
50451
+ const impulse = [0, 0, 0];
50452
+ const point = [0, 0, 0];
50453
+ const direction = [0, 0];
50454
+ const car = { speed: 0, mass: 0 };
50455
+ const found = [];
50456
+ const now = /* @__PURE__ */ new Set();
50457
+ const indexOfBody = /* @__PURE__ */ new Map();
50458
+ let isIndexStale = true;
50459
+ let dying;
50460
+ let diedLast = false;
50461
+ const agentsOf = (bodies) => {
50462
+ if (isIndexStale) {
50463
+ indexOfBody.clear();
50464
+ for (let index = 0; index < agents.count; index++) {
50465
+ const body = agents.bodyOf(index);
50466
+ if (body !== void 0) indexOfBody.set(body, index);
50467
+ }
50468
+ isIndexStale = false;
50469
+ }
50470
+ found.length = 0;
50471
+ for (const body of bodies) {
50472
+ const index = indexOfBody.get(body);
50473
+ if (index !== void 0) found.push(index);
50474
+ }
50475
+ return found.sort(byIndexDescending);
50476
+ };
50477
+ const strike = (index, outcome, direction2, hit, h) => {
50478
+ const { store } = crowd;
50479
+ if (outcome === "push") {
50480
+ store.x[index] += -direction2[1] * PUSH_ASIDE_SPEED * h;
50481
+ store.z[index] += direction2[0] * PUSH_ASIDE_SPEED * h;
50482
+ return hit.carMomentum * (1 - Math.exp(-rules.holdDrag * h));
50483
+ }
50484
+ dying = [direction2[0] * hit.common, 0, direction2[1] * hit.common];
50485
+ if (outcome === "kill") crowd.kill(index);
50486
+ else if (!crowd.damage(index, { region: 1, amount: hit.reduced / REFERENCE_MASS * hit.closing * hit.closing * rules.knockDamage, distance: 0 }).died) {
50487
+ dying = void 0;
50488
+ crowd.knockDown(index, { x: direction2[0] * hit.common, z: direction2[1] * hit.common });
50489
+ }
50490
+ const died = dying !== void 0;
50491
+ dying = void 0;
50492
+ diedLast = died;
50493
+ return hit.reduced * hit.closing;
50494
+ };
50495
+ const hitOne = (vehicle, index, direction2, car2, h) => {
50496
+ const { store } = crowd;
50497
+ const mass = store.mass[index] > 0 ? store.mass[index] : REFERENCE_MASS;
50498
+ const closing = car2.speed - (store.vx[index] * direction2[0] + store.vz[index] * direction2[1]);
50499
+ if (closing <= 0) return void 0;
50500
+ const reduced = mass * car2.mass / (mass + car2.mass);
50501
+ const outcome = runOver(closing, rules);
50502
+ diedLast = false;
50503
+ const lost = strike(index, outcome, direction2, { reduced, closing, common: car2.speed - mass / (mass + car2.mass) * closing, carMomentum: car2.mass * car2.speed }, h);
50504
+ car2.speed -= lost / car2.mass;
50505
+ impulse[0] = -direction2[0] * lost;
50506
+ impulse[2] = -direction2[1] * lost;
50507
+ world.addImpulse(vehicle.body, impulse, point);
50508
+ options.onHit?.({ vehicle: vehicle.body, agent: index, outcome, speed: closing, energy: 0.5 * reduced * closing * closing, vehicleDamage: diedLast ? rules.killVehicleDamage : 0 });
50509
+ return outcome;
50510
+ };
50511
+ const sweep = (vehicle, h) => {
50512
+ world.readVelocity(vehicle.body, velocity, 0);
50513
+ const speed = Math.hypot(velocity[0], velocity[2]);
50514
+ if (speed < MIN_SPEED) return;
50515
+ world.readPose(vehicle.body, pose, 0);
50516
+ direction[0] = velocity[0] / speed;
50517
+ direction[1] = velocity[2] / speed;
50518
+ const reach = speed * h / 2;
50519
+ box2.half[0] = vehicle.half[0] + rules.margin;
50520
+ box2.half[1] = vehicle.half[1];
50521
+ box2.half[2] = vehicle.half[2] + rules.margin + reach;
50522
+ centre[0] = pose[0] + direction[0] * reach;
50523
+ centre[1] = pose[1];
50524
+ centre[2] = pose[2] + direction[1] * reach;
50525
+ for (let k = 0; k < 4; k++) rotation[k] = pose[3 + k];
50526
+ point[1] = pose[1];
50527
+ if (world.overlapShape(box2, centre, rotation, hits, { layers: ["agent"] }) === 0) {
50528
+ struck.delete(vehicle);
50529
+ return;
50530
+ }
50531
+ car.speed = speed;
50532
+ car.mass = world.massOf(vehicle.body);
50533
+ let before = struck.get(vehicle);
50534
+ if (!before) {
50535
+ before = /* @__PURE__ */ new Set();
50536
+ struck.set(vehicle, before);
50537
+ }
50538
+ for (const index of agentsOf(hits)) {
50539
+ const identity = crowd.store.identity[index];
50540
+ if (before.has(identity)) {
50541
+ now.add(identity);
50542
+ continue;
50543
+ }
50544
+ point[0] = crowd.store.x[index];
50545
+ point[2] = crowd.store.z[index];
50546
+ const outcome = hitOne(vehicle, index, direction, car, h);
50547
+ if (outcome === "push") continue;
50548
+ now.add(identity);
50549
+ if (outcome !== void 0) isIndexStale = true;
50550
+ }
50551
+ before.clear();
50552
+ for (const identity of now) before.add(identity);
50553
+ now.clear();
50554
+ };
50555
+ const beforeStep = (h) => {
50556
+ isIndexStale = true;
50557
+ for (const vehicle of vehicles) sweep(vehicle, h);
50558
+ };
50559
+ const removePhase = loop.add({ beforeStep }, "actors");
50560
+ return {
50561
+ track: (vehicle) => {
50562
+ vehicles.push(vehicle);
50563
+ return () => {
50564
+ const at = vehicles.indexOf(vehicle);
50565
+ if (at >= 0) vehicles.splice(at, 1);
50566
+ struck.delete(vehicle);
50567
+ };
50568
+ },
50569
+ get dyingVelocity() {
50570
+ return dying;
50571
+ },
50572
+ dispose: () => {
50573
+ removePhase();
50574
+ vehicles.length = 0;
50575
+ struck.clear();
50576
+ }
50577
+ };
50578
+ };
50430
50579
 
50431
50580
  // src/collider-fit.ts
50432
50581
  var FIT = 0.01;
@@ -51286,6 +51435,7 @@ var GameVehiclesThree = (() => {
51286
51435
  var VELOCITY = new Float64Array(6);
51287
51436
  var POSE2 = new Float64Array(7);
51288
51437
  var GOLDEN_ANGLE = Math.PI * (1 + Math.sqrt(5));
51438
+ var CROWD_AGENTS_CHANNEL = "crowd.agents";
51289
51439
  var isPhysicsServices = (value) => {
51290
51440
  if (typeof value !== "object" || value === null) return false;
51291
51441
  const candidate = value;
@@ -51740,6 +51890,15 @@ var GameVehiclesThree = (() => {
51740
51890
  if (axis === 1) return { shape, position: center };
51741
51891
  return { shape, position: center, rotation: axis === 0 ? [0, 0, HALF_TURN, HALF_TURN] : [HALF_TURN, 0, 0, HALF_TURN] };
51742
51892
  };
51893
+ var runOverHalfOf = (parts) => {
51894
+ const half = [0, 0, 0];
51895
+ for (const { box: box2 } of parts) {
51896
+ half[0] = Math.max(half[0], Math.abs(box2.center[0]) + box2.size[0] / 2);
51897
+ half[1] = Math.max(half[1], box2.center[1] + box2.size[1] / 2);
51898
+ half[2] = Math.max(half[2], Math.abs(box2.center[2]) + box2.size[2] / 2);
51899
+ }
51900
+ return half;
51901
+ };
51743
51902
  var physicsServicesOf = (host) => host && isPhysicsServices(host.physics) ? host.physics : void 0;
51744
51903
  var physicalCarColliders = (THREE, vehicle, root, options) => createVehicleColliders(THREE, vehicle, root, {
51745
51904
  budget: options.budget,
@@ -51748,7 +51907,7 @@ var GameVehiclesThree = (() => {
51748
51907
  planCache: options.planCache,
51749
51908
  ...options.workers ? { workers: options.workers } : {}
51750
51909
  });
51751
- var createPhysicalCar = ({ THREE, services, vehicle, root, colliders }) => {
51910
+ var createPhysicalCar = ({ THREE, services, vehicle, root, colliders, runOver: runOver2 }) => {
51752
51911
  const { world } = services;
51753
51912
  const pose = new Float64Array(7);
51754
51913
  const matrix = new THREE.Matrix4();
@@ -51759,6 +51918,7 @@ var GameVehiclesThree = (() => {
51759
51918
  const wheelHandles = /* @__PURE__ */ new Map();
51760
51919
  let crash;
51761
51920
  let chassis;
51921
+ let stopRunOver;
51762
51922
  const withWheels = (hulls) => {
51763
51923
  if (hulls.kind !== "compound") return hulls;
51764
51924
  const tyres = vehicle.model.parts.filter((part) => part.role === "tyre");
@@ -51808,6 +51968,7 @@ var GameVehiclesThree = (() => {
51808
51968
  rotation: [rotation.x, rotation.y, rotation.z, rotation.w]
51809
51969
  });
51810
51970
  crash.bind(chassis);
51971
+ stopRunOver = runOver2?.track({ body: chassis, half: runOverHalfOf(vehicle.model.parts) });
51811
51972
  };
51812
51973
  const follow = () => {
51813
51974
  world.readPose(chassis, pose, 0);
@@ -51831,6 +51992,8 @@ var GameVehiclesThree = (() => {
51831
51992
  parts: () => crash,
51832
51993
  dispose: () => {
51833
51994
  stopPhase();
51995
+ stopRunOver?.();
51996
+ stopRunOver = void 0;
51834
51997
  crash?.dispose();
51835
51998
  if (chassis !== void 0 && world.has(chassis)) world.removeBody(chassis);
51836
51999
  crash = void 0;
@@ -51839,6 +52002,43 @@ var GameVehiclesThree = (() => {
51839
52002
  };
51840
52003
  };
51841
52004
 
52005
+ // src/test-area/run-over-link.ts
52006
+ var createRunOverLink = (services) => {
52007
+ const channel = services.channel(CROWD_AGENTS_CHANNEL);
52008
+ const vehicles = /* @__PURE__ */ new Set();
52009
+ const stops = /* @__PURE__ */ new Map();
52010
+ let runOver2;
52011
+ const follow = () => {
52012
+ runOver2?.dispose();
52013
+ runOver2 = void 0;
52014
+ stops.clear();
52015
+ const lent = channel.values[0];
52016
+ if (!lent) return;
52017
+ runOver2 = createRunOver(services.loop, { crowd: lent.crowd, agents: lent.agents });
52018
+ for (const vehicle of vehicles) stops.set(vehicle, runOver2.track(vehicle));
52019
+ };
52020
+ const stopListening = channel.onChange(follow);
52021
+ follow();
52022
+ return {
52023
+ track: (vehicle) => {
52024
+ vehicles.add(vehicle);
52025
+ if (runOver2) stops.set(vehicle, runOver2.track(vehicle));
52026
+ return () => {
52027
+ vehicles.delete(vehicle);
52028
+ stops.get(vehicle)?.();
52029
+ stops.delete(vehicle);
52030
+ };
52031
+ },
52032
+ dispose: () => {
52033
+ stopListening();
52034
+ runOver2?.dispose();
52035
+ runOver2 = void 0;
52036
+ vehicles.clear();
52037
+ stops.clear();
52038
+ }
52039
+ };
52040
+ };
52041
+
51842
52042
  // src/test-area/text.ts
51843
52043
  var scenarioIds = [
51844
52044
  "vehicles_9mm_windshield",
@@ -52281,6 +52481,7 @@ var GameVehiclesThree = (() => {
52281
52481
  const rainCovers = createVehicleRainCovers();
52282
52482
  let host = null;
52283
52483
  const physicalCars = /* @__PURE__ */ new Set();
52484
+ let runOverLink;
52284
52485
  const planCache = createHullPlanCache();
52285
52486
  let three = null;
52286
52487
  let counter = 0;
@@ -52355,7 +52556,7 @@ var GameVehiclesThree = (() => {
52355
52556
  const services = physicsServicesOf(host);
52356
52557
  if (services) {
52357
52558
  const colliders = physicalCarColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET, fleetKey: key, hullCeilingOf: fleet.hullCeilingOf, planCache, workers: host?.workers });
52358
- const car = createPhysicalCar({ THREE: kit.THREE, services, vehicle: d, root, colliders });
52559
+ const car = createPhysicalCar({ THREE: kit.THREE, services, vehicle: d, root, colliders, runOver: runOverLink ?? (runOverLink = createRunOverLink(services)) });
52359
52560
  const disposeVehicle = d.dispose?.bind(d);
52360
52561
  d.dispose = () => {
52361
52562
  physicalCars.delete(car);
@@ -52817,6 +53018,8 @@ var GameVehiclesThree = (() => {
52817
53018
  stopLodBatch?.();
52818
53019
  for (const car of physicalCars) car.dispose();
52819
53020
  physicalCars.clear();
53021
+ runOverLink?.dispose();
53022
+ runOverLink = void 0;
52820
53023
  cabins.clear(effects());
52821
53024
  rainCovers.clear();
52822
53025
  clearHelpers();