@adatechnology/game-weather-three 0.3.2 → 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
  };
@@ -330,6 +335,7 @@ interface RigVehicles {
330
335
  }): {
331
336
  readonly group: THREE_NS.Object3D;
332
337
  readonly length: number;
338
+ readonly width?: number;
333
339
  dispose(): void;
334
340
  };
335
341
  }
@@ -2348,6 +2348,24 @@ var GameWeatherThree = (() => {
2348
2348
  };
2349
2349
  }
2350
2350
 
2351
+ // ../game-contracts/dist/index.js
2352
+ function footprintBlocker(object, footprint) {
2353
+ return (x, z, radius) => {
2354
+ const o = typeof object === "function" ? object() : object;
2355
+ const fp = typeof footprint === "function" ? footprint() : footprint;
2356
+ if (!o || !fp || !o.parent) return false;
2357
+ o.updateWorldMatrix?.(true, false);
2358
+ const e = o.matrixWorld.elements;
2359
+ const dx = x - e[12];
2360
+ const dz = z - e[14];
2361
+ const ax = e[0] ** 2 + e[1] ** 2 + e[2] ** 2;
2362
+ const az = e[8] ** 2 + e[9] ** 2 + e[10] ** 2;
2363
+ const lx = (dx * e[0] + dz * e[2]) / (ax || 1);
2364
+ const lz = (dx * e[8] + dz * e[10]) / (az || 1);
2365
+ return lx > fp.minX - radius && lx < fp.maxX + radius && lz > fp.minZ - radius && lz < fp.maxZ + radius;
2366
+ };
2367
+ }
2368
+
2351
2369
  // src/bench/text.ts
2352
2370
  var TEXTS = {
2353
2371
  en: { layers: { far_rain: "Rain curtain", near_rain: "Near rain and splashes", mist: "Mist and ash", sky: "Sky", lightning: "Lightning", headlights: "Headlights", haze: "Heat haze" } },
@@ -2367,8 +2385,10 @@ var GameWeatherThree = (() => {
2367
2385
  car.group.position.z = HEADLIGHTS.lamp[2] + 0.1 - car.length / 2;
2368
2386
  group.add(car.group);
2369
2387
  }
2388
+ const footprint = car ? footprintOf(car) : void 0;
2370
2389
  return {
2371
2390
  group,
2391
+ ...footprint ? { footprint } : {},
2372
2392
  dispose() {
2373
2393
  car?.dispose();
2374
2394
  }
@@ -2395,19 +2415,58 @@ var GameWeatherThree = (() => {
2395
2415
  ...options.headlights ? { headlights: options.headlights } : {},
2396
2416
  ...options.layers ? { layers: options.layers } : {}
2397
2417
  });
2398
- 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);
2399
2420
  }
2400
2421
  };
2401
2422
  }
2402
- function attachRig(THREE, host, renderer, place, vehicles, key) {
2423
+ function footprintOf(car) {
2424
+ if (!car.width) return void 0;
2425
+ const z = car.group.position.z;
2426
+ return { minX: -car.width / 2, maxX: car.width / 2, minZ: z - car.length / 2, maxZ: z + car.length / 2 };
2427
+ }
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) {
2403
2444
  const scene = host.scene;
2404
2445
  const rig = place === false ? void 0 : buildRig(THREE, vehicles, key);
2405
2446
  let detach;
2447
+ let unblock;
2406
2448
  if (rig && place) {
2407
2449
  rig.group.position.set(place[0], 0, place[1]);
2408
2450
  rig.group.rotation.y = place[2];
2409
2451
  scene.add(rig.group);
2410
2452
  detach = renderer.headlights.attach(rig.group);
2453
+ if (rig.footprint) unblock = host.addBlocker?.(footprintBlocker(rig.group, rig.footprint));
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);
2411
2470
  }
2412
2471
  return {
2413
2472
  update: (dt) => renderer.update(dt),
@@ -2416,6 +2475,9 @@ var GameWeatherThree = (() => {
2416
2475
  },
2417
2476
  dispose() {
2418
2477
  detach?.();
2478
+ unblock?.();
2479
+ stopWaiting?.();
2480
+ car?.remove();
2419
2481
  if (rig) {
2420
2482
  scene.remove(rig.group);
2421
2483
  rig.dispose();