@adatechnology/game-weather-three 0.3.3 → 0.4.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.d.ts CHANGED
@@ -3,7 +3,7 @@ import { WorkshopEngine, VfxWorld, Puddle } from '@adatechnology/game-vfx';
3
3
  import { WeatherSample, WeatherVisualFactory } from '@adatechnology/game-weather';
4
4
 
5
5
  /** The only three.js members the renderer needs at runtime. Pass your own `THREE` instance (the whole namespace works). */
6
- type ThreeLib = Pick<typeof THREE_NS, 'AdditiveBlending' | 'BoxGeometry' | 'BufferAttribute' | 'BufferGeometry' | 'Color' | 'ConeGeometry' | 'DoubleSide' | 'Fog' | 'FogExp2' | 'Group' | 'HemisphereLight' | 'Mesh' | 'MeshBasicMaterial' | 'Object3D' | 'PlaneGeometry' | 'Points' | 'ShaderMaterial' | 'SpotLight' | 'Vector3'>;
6
+ type ThreeLib = Pick<typeof THREE_NS, 'AdditiveBlending' | 'Box3' | 'BoxGeometry' | 'BufferAttribute' | 'BufferGeometry' | 'Color' | 'ConeGeometry' | 'DoubleSide' | 'Fog' | 'FogExp2' | 'Group' | 'HemisphereLight' | 'Mesh' | 'Matrix4' | 'MeshBasicMaterial' | 'Object3D' | 'PlaneGeometry' | 'Points' | 'ShaderMaterial' | 'SpotLight' | 'Vector3'>;
7
7
  /** What the renderer reads of a weather world: its sample. A `createWeather` world fits. */
8
8
  type WeatherSource = {
9
9
  sample(): WeatherSample;
@@ -320,6 +320,11 @@ type WeatherVisualOptions = Pick<WeatherRendererOptions, 'profile' | 'random' |
320
320
  readonly vehicles?: (THREE: ThreeLib) => RigVehicles;
321
321
  /** Fleet id of the rig's car (default `midsize_sedan`). */
322
322
  readonly rigVehicle?: string;
323
+ /**
324
+ * The rig's car as an item of the host's catalogue (`sedan` of the vehicles test module): the host puts it in its world (`ModuleHost.addObject`), so shots, blows, blasts and fire damage it, it
325
+ * blocks the player and its doors open, and the lamps go on its nose. Wins over `vehicles` when the host can place it; until a loaded module has the item the rig has only the lamps.
326
+ */
327
+ readonly rigItem?: string;
323
328
  /** Which layers start on (all do by default); the room that is not about weather starts the sky and the fog off to keep its own look. */
324
329
  readonly layers?: Partial<Record<WeatherLayerId, boolean>>;
325
330
  };
@@ -2415,7 +2415,8 @@ var GameWeatherThree = (() => {
2415
2415
  ...options.headlights ? { headlights: options.headlights } : {},
2416
2416
  ...options.layers ? { layers: options.layers } : {}
2417
2417
  });
2418
- return attachRig(THREE, host, renderer, options.rig === void 0 ? RIG_DEFAULT : options.rig, options.vehicles?.(THREE), options.rigVehicle ?? RIG_VEHICLE);
2418
+ const item = options.rigItem && host.addObject ? options.rigItem : void 0;
2419
+ return attachRig(THREE, host, renderer, options.rig === void 0 ? RIG_DEFAULT : options.rig, item ? void 0 : options.vehicles?.(THREE), options.rigVehicle ?? RIG_VEHICLE, item);
2419
2420
  }
2420
2421
  };
2421
2422
  }
@@ -2424,7 +2425,22 @@ var GameWeatherThree = (() => {
2424
2425
  const z = car.group.position.z;
2425
2426
  return { minX: -car.width / 2, maxX: car.width / 2, minZ: z - car.length / 2, maxZ: z + car.length / 2 };
2426
2427
  }
2427
- function attachRig(THREE, host, renderer, place, vehicles, key) {
2428
+ function lengthOf(THREE, root) {
2429
+ root.updateMatrixWorld(true);
2430
+ const inverse = new THREE.Matrix4().copy(root.matrixWorld).invert();
2431
+ const box = new THREE.Box3();
2432
+ const piece = new THREE.Box3();
2433
+ const matrix = new THREE.Matrix4();
2434
+ root.traverse((mesh2) => {
2435
+ const m = mesh2;
2436
+ if (!m.isMesh || !m.geometry) return;
2437
+ if (!m.geometry.boundingBox) m.geometry.computeBoundingBox();
2438
+ piece.copy(m.geometry.boundingBox).applyMatrix4(matrix.multiplyMatrices(inverse, m.matrixWorld));
2439
+ box.union(piece);
2440
+ });
2441
+ return box.isEmpty() ? 0 : box.max.z - box.min.z;
2442
+ }
2443
+ function attachRig(THREE, host, renderer, place, vehicles, key, item) {
2428
2444
  const scene = host.scene;
2429
2445
  const rig = place === false ? void 0 : buildRig(THREE, vehicles, key);
2430
2446
  let detach;
@@ -2436,6 +2452,22 @@ var GameWeatherThree = (() => {
2436
2452
  detach = renderer.headlights.attach(rig.group);
2437
2453
  if (rig.footprint) unblock = host.addBlocker?.(footprintBlocker(rig.group, rig.footprint));
2438
2454
  }
2455
+ let car = null;
2456
+ let stopWaiting;
2457
+ if (item && place) {
2458
+ const tryPlace = () => {
2459
+ if (car || !host.addObject) return;
2460
+ const placed = host.addObject(item, { position: [place[0], 0, place[1]], rotationY: place[2] });
2461
+ if (!placed) return;
2462
+ car = placed;
2463
+ stopWaiting?.();
2464
+ stopWaiting = void 0;
2465
+ const ahead = lengthOf(THREE, placed.root) / 2 - HEADLIGHTS.lamp[2] - 0.1;
2466
+ rig.group.position.set(place[0] + Math.sin(place[2]) * ahead, 0, place[1] + Math.cos(place[2]) * ahead);
2467
+ };
2468
+ tryPlace();
2469
+ if (!car) stopWaiting = host.onFrame(tryPlace);
2470
+ }
2439
2471
  return {
2440
2472
  update: (dt) => renderer.update(dt),
2441
2473
  setLayer(id, on) {
@@ -2444,6 +2476,8 @@ var GameWeatherThree = (() => {
2444
2476
  dispose() {
2445
2477
  detach?.();
2446
2478
  unblock?.();
2479
+ stopWaiting?.();
2480
+ car?.remove();
2447
2481
  if (rig) {
2448
2482
  scene.remove(rig.group);
2449
2483
  rig.dispose();