@adatechnology/game-vfx 0.11.0 → 0.12.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
@@ -5164,9 +5164,17 @@ function createEffectPlayer(options) {
5164
5164
  });
5165
5165
  }
5166
5166
  function placementOf(entry) {
5167
+ const carrier = entry.carrier;
5168
+ if (carrier) return { origin: carrier.origin, anchor: entry.anchor, yaw: entry.yaw, carrier, ...carrier.orientation ? { orientation: carrier.orientation } : {} };
5167
5169
  const origin = entry.follow ? [entry.origin[0] + positionOf2(entry.follow)[0], entry.origin[1] + positionOf2(entry.follow)[1], entry.origin[2] + positionOf2(entry.follow)[2]] : entry.origin;
5168
5170
  return { origin, anchor: entry.anchor, yaw: entry.yaw, ...entry.orientation ? { orientation: entry.orientation } : {} };
5169
5171
  }
5172
+ function moveCarrier(entry) {
5173
+ const pose = entry.track?.();
5174
+ if (!pose || !entry.carrier) return;
5175
+ entry.carrier.origin = [pose.point[0], pose.point[1], pose.point[2]];
5176
+ if (pose.orientation) entry.carrier.orientation = pose.orientation;
5177
+ }
5170
5178
  function drawing(entry, action) {
5171
5179
  const context = engine.context;
5172
5180
  const shared = context.random;
@@ -5192,7 +5200,9 @@ function createEffectPlayer(options) {
5192
5200
  anchor: def.anchor ?? ORIGIN5,
5193
5201
  yaw: yawOf(heading, playOptions.direction),
5194
5202
  orientation: playOptions.orientation,
5195
- seconds: playOptions.seconds ?? (def.loop && def.loop > 0 ? def.loop : ENDLESS_SECONDS)
5203
+ seconds: playOptions.seconds ?? (def.loop && def.loop > 0 ? def.loop : ENDLESS_SECONDS),
5204
+ track: playOptions.track,
5205
+ carrier: playOptions.track ? { origin: point, orientation: playOptions.orientation } : void 0
5196
5206
  };
5197
5207
  playing.push(entry);
5198
5208
  drawing(
@@ -5214,11 +5224,21 @@ function createEffectPlayer(options) {
5214
5224
  for (let index = playing.length - 1; index >= 0; index--) {
5215
5225
  const entry = playing[index];
5216
5226
  const instance = entry.instance;
5227
+ moveCarrier(entry);
5217
5228
  instance.setup = { ...instance.setup, placement: placementOf(entry) };
5218
5229
  drawing(entry, () => instance.step(dt));
5219
5230
  if (instance.time >= entry.seconds) playing.splice(index, 1);
5220
5231
  }
5221
5232
  },
5233
+ retrack() {
5234
+ let tracked = false;
5235
+ for (const entry of playing) {
5236
+ if (!entry.carrier) continue;
5237
+ moveCarrier(entry);
5238
+ tracked = true;
5239
+ }
5240
+ return tracked;
5241
+ },
5222
5242
  clear() {
5223
5243
  playing.length = 0;
5224
5244
  },
@@ -6871,6 +6891,21 @@ var DecalPool = class {
6871
6891
  }
6872
6892
  };
6873
6893
 
6894
+ // src/workshop/carrier.ts
6895
+ function rotate(q, v) {
6896
+ const [qx, qy, qz, qw] = q;
6897
+ const ix = qw * v[0] + qy * v[2] - qz * v[1];
6898
+ const iy = qw * v[1] + qz * v[0] - qx * v[2];
6899
+ const iz = qw * v[2] + qx * v[1] - qy * v[0];
6900
+ const iw = -qx * v[0] - qy * v[1] - qz * v[2];
6901
+ return [ix * qw + iw * -qx + iy * -qz - iz * -qy, iy * qw + iw * -qy + iz * -qx - ix * -qz, iz * qw + iw * -qz + ix * -qy - iy * -qx];
6902
+ }
6903
+ function carriedPoint(carried) {
6904
+ const { carrier, local: local2 } = carried;
6905
+ const turned = carrier.orientation ? rotate(carrier.orientation, local2) : local2;
6906
+ return [carrier.origin[0] + turned[0], carrier.origin[1] + turned[1], carrier.origin[2] + turned[2]];
6907
+ }
6908
+
6874
6909
  // src/workshop/meshes.ts
6875
6910
  var CAPACITY3 = 400;
6876
6911
  var IDENTITY = [0, 0, 0, 1];
@@ -6889,7 +6924,7 @@ var MeshPool = class {
6889
6924
  get count() {
6890
6925
  return this.particles.length;
6891
6926
  }
6892
- spawn(options, frame = IDENTITY) {
6927
+ spawn(options, frame = IDENTITY, carried) {
6893
6928
  const velocity = options.v ?? [0, 0, 0];
6894
6929
  const world = options.wv ?? [0, 0, 0];
6895
6930
  const particle = {
@@ -6906,6 +6941,9 @@ var MeshPool = class {
6906
6941
  y: options.p[1],
6907
6942
  z: options.p[2],
6908
6943
  frame: options.roll ? multiply(frame, aboutX(options.roll)) : frame,
6944
+ roll: options.roll ? aboutX(options.roll) : void 0,
6945
+ carried,
6946
+ drift: [0, 0, 0],
6909
6947
  at: options.at ?? [0, 0, 0],
6910
6948
  rx: options.rot?.[0] ?? 0,
6911
6949
  ry: options.rot?.[1] ?? 0,
@@ -6946,9 +6984,12 @@ var MeshPool = class {
6946
6984
  particle.vy *= keep;
6947
6985
  particle.vz *= keep;
6948
6986
  }
6949
- particle.x += particle.vx * dt;
6950
- particle.y += particle.vy * dt;
6951
- particle.z += particle.vz * dt;
6987
+ if (particle.carried) this.ride(particle, dt);
6988
+ else {
6989
+ particle.x += particle.vx * dt;
6990
+ particle.y += particle.vy * dt;
6991
+ particle.z += particle.vz * dt;
6992
+ }
6952
6993
  particle.rx += particle.spin[0] * dt;
6953
6994
  particle.ry += particle.spin[1] * dt;
6954
6995
  particle.rz += particle.spin[2] * dt;
@@ -6959,6 +7000,30 @@ var MeshPool = class {
6959
7000
  clear() {
6960
7001
  this.particles.length = 0;
6961
7002
  }
7003
+ /** Puts every carried piece where its carrier is now (after the frame moved the thing it belongs to). */
7004
+ follow() {
7005
+ for (const particle of this.particles) if (particle.carried) this.place(particle);
7006
+ }
7007
+ /** A carried piece: how far its own velocity took it, then its place on the carrier. */
7008
+ ride(particle, dt) {
7009
+ const drift = particle.drift;
7010
+ drift[0] += particle.vx * dt;
7011
+ drift[1] += particle.vy * dt;
7012
+ drift[2] += particle.vz * dt;
7013
+ this.place(particle);
7014
+ }
7015
+ /** A carried piece's place on the carrier now plus its drift, turned with the carrier. */
7016
+ place(particle) {
7017
+ const carried = particle.carried;
7018
+ if (!carried) return;
7019
+ const drift = particle.drift;
7020
+ const [x, y, z] = carriedPoint(carried);
7021
+ particle.x = x + drift[0];
7022
+ particle.y = y + drift[1];
7023
+ particle.z = z + drift[2];
7024
+ const turn = carried.carrier.orientation;
7025
+ if (turn) particle.frame = particle.roll ? multiply(turn, particle.roll) : turn;
7026
+ }
6962
7027
  };
6963
7028
 
6964
7029
  // src/workshop/sprites.ts
@@ -7327,8 +7392,8 @@ var FlashLightPool = class {
7327
7392
  }
7328
7393
  flash(point, color, intensity, duration, distance, force, steady) {
7329
7394
  const { level } = this.context.quality;
7330
- if (level >= 1 && !force) return;
7331
- if (level >= 2 && force !== "boom") return;
7395
+ if (level >= 1 && !force) return void 0;
7396
+ if (level >= 2 && force !== "boom") return void 0;
7332
7397
  const light = this.lights[this.next++ % LIGHT_CAPACITY];
7333
7398
  light.x = point[0];
7334
7399
  light.y = point[1];
@@ -7340,17 +7405,24 @@ var FlashLightPool = class {
7340
7405
  light.t = 0;
7341
7406
  light.steady = steady === true;
7342
7407
  light.intensity = intensity;
7408
+ light.carried = void 0;
7409
+ return light;
7343
7410
  }
7344
7411
  clear() {
7345
7412
  for (const light of this.lights) light.intensity = 0;
7346
7413
  for (const light of this.fire) light.intensity = 0;
7347
7414
  }
7415
+ /** Puts every carried light that burns where its carrier is now. */
7416
+ follow() {
7417
+ for (const light of this.lights) if (light.intensity > 0 && light.carried) [light.x, light.y, light.z] = carriedPoint(light.carried);
7418
+ }
7348
7419
  update(dt) {
7349
7420
  for (const light of this.lights) {
7350
7421
  if (light.intensity <= 0) continue;
7351
7422
  light.t += dt;
7352
7423
  const age = light.t / light.duration;
7353
7424
  light.intensity = age >= 1 ? 0 : light.steady ? light.peak : light.peak * (1 - age) * (1 - age);
7425
+ if (light.carried) [light.x, light.y, light.z] = carriedPoint(light.carried);
7354
7426
  }
7355
7427
  }
7356
7428
  };
@@ -7360,7 +7432,7 @@ var QUALITY_MULTIPLIERS = [1, 0.5, 0.2];
7360
7432
  var FIRE_FLICKER_SWAY = 0.25;
7361
7433
  var FIRE_FLICKER_JITTER = 0.08;
7362
7434
  var LENS_FADE = 0.16;
7363
- function rotate(q, v) {
7435
+ function rotate2(q, v) {
7364
7436
  const [qx, qy, qz, qw] = q;
7365
7437
  const ix = qw * v[0] + qy * v[2] - qz * v[1];
7366
7438
  const iy = qw * v[1] + qz * v[0] - qx * v[2];
@@ -7418,7 +7490,7 @@ var WorkshopEngine = class {
7418
7490
  if (!placement) return point;
7419
7491
  const { anchor, origin, yaw, orientation } = placement;
7420
7492
  if (orientation) {
7421
- const turned = rotate(orientation, [point[0] - anchor[0], point[1] - anchor[1], point[2] - anchor[2]]);
7493
+ const turned = rotate2(orientation, [point[0] - anchor[0], point[1] - anchor[1], point[2] - anchor[2]]);
7422
7494
  return [origin[0] + turned[0], origin[1] + turned[1], origin[2] + turned[2]];
7423
7495
  }
7424
7496
  const x = point[0] - anchor[0];
@@ -7427,11 +7499,20 @@ var WorkshopEngine = class {
7427
7499
  const sin2 = Math.sin(yaw);
7428
7500
  return [origin[0] + cos2 * x + sin2 * z, origin[1] + point[1] - anchor[1], origin[2] - sin2 * x + cos2 * z];
7429
7501
  }
7502
+ /** A point of the recipe's space from the anchor, turned by the placement's yaw when it has no orientation (the carrier turns the rest). */
7503
+ local(point) {
7504
+ const placement = this.placement;
7505
+ if (!placement) return point;
7506
+ const offset = [point[0] - placement.anchor[0], point[1] - placement.anchor[1], point[2] - placement.anchor[2]];
7507
+ if (placement.carrier?.orientation) return offset;
7508
+ const turned = this.place(point);
7509
+ return [turned[0] - placement.origin[0], turned[1] - placement.origin[1], turned[2] - placement.origin[2]];
7510
+ }
7430
7511
  /** A direction or velocity of the recipe's space in the world. */
7431
7512
  turn(vector2) {
7432
7513
  const placement = this.placement;
7433
7514
  if (!placement) return vector2;
7434
- if (placement.orientation) return rotate(placement.orientation, vector2);
7515
+ if (placement.orientation) return rotate2(placement.orientation, vector2);
7435
7516
  const cos2 = Math.cos(placement.yaw);
7436
7517
  const sin2 = Math.sin(placement.yaw);
7437
7518
  return [cos2 * vector2[0] + sin2 * vector2[2], vector2[1], -sin2 * vector2[0] + cos2 * vector2[2]];
@@ -7466,7 +7547,8 @@ var WorkshopEngine = class {
7466
7547
  const options = placement ? { ...recipe, p: this.place(recipe.p), ...recipe.v ? { v: this.turn(recipe.v) } : {} } : recipe;
7467
7548
  this.context.trace?.("mesh", [options]);
7468
7549
  const grouped = recipe.at !== void 0 || recipe.roll !== void 0;
7469
- return this.meshes.spawn(options, placement && grouped ? placement.orientation ?? [0, Math.sin(placement.yaw / 2), 0, Math.cos(placement.yaw / 2)] : void 0);
7550
+ const carried = placement?.carrier && grouped ? { carrier: placement.carrier, local: this.local(recipe.p) } : void 0;
7551
+ return this.meshes.spawn(options, placement && grouped ? placement.orientation ?? [0, Math.sin(placement.yaw / 2), 0, Math.cos(placement.yaw / 2)] : void 0, carried);
7470
7552
  }
7471
7553
  debrisPiece(recipe) {
7472
7554
  const options = this.placed(recipe);
@@ -7490,9 +7572,11 @@ var WorkshopEngine = class {
7490
7572
  }
7491
7573
  /** `steady` (a seventh number): the light holds its intensity for its duration and goes out, where the Workshop's fades with the square of its age. */
7492
7574
  flash(recipe) {
7493
- const args = this.placement ? [this.place(recipe[0]), recipe[1], recipe[2], recipe[3], recipe[4], recipe[5], recipe[6]] : recipe;
7575
+ const placement = this.placement;
7576
+ const args = placement ? [this.place(recipe[0]), recipe[1], recipe[2], recipe[3], recipe[4], recipe[5], recipe[6]] : recipe;
7494
7577
  this.context.trace?.("flash", args.slice(0, 6));
7495
- this.lights.flash(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
7578
+ const light = this.lights.flash(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
7579
+ if (light && placement?.carrier) light.carried = { carrier: placement.carrier, local: this.local(recipe[0]) };
7496
7580
  }
7497
7581
  /** `fireLight` (W400): sways with the clock and flickers with one draw. */
7498
7582
  fireLight(slot, recipe, intensity, time) {
@@ -7655,6 +7739,12 @@ function createVfx(options = {}) {
7655
7739
  const seed = playOptions.seed ?? (randomMode === "shared" ? void 0 : Math.imul(++handles.events, AUTO_SEED) + (options.seed ?? 0));
7656
7740
  return effectPlayer().play(id, [point[0], point[1], point[2]], { ...playOptions, ...seed !== void 0 ? { seed } : {} });
7657
7741
  },
7742
+ follow() {
7743
+ if (!engine || !player?.retrack()) return false;
7744
+ engine.meshes.follow();
7745
+ engine.lights.follow();
7746
+ return true;
7747
+ },
7658
7748
  burst: (kind, point, normal, count, speed, spriteOptions) => spriteSystem.burst(kind, point, normal, count, speed, spriteOptions),
7659
7749
  spawn: (kind, point, velocity, spriteOptions) => spriteSystem.spawn(kind, point, velocity, spriteOptions),
7660
7750
  onLand: (owner, listener) => spriteSystem.onLand(owner, listener),
@@ -8661,7 +8751,7 @@ function ease3(value) {
8661
8751
  function mix(from, to, amount) {
8662
8752
  return [from[0] + (to[0] - from[0]) * amount, from[1] + (to[1] - from[1]) * amount, from[2] + (to[2] - from[2]) * amount];
8663
8753
  }
8664
- function rotate2(local2, x, y, z, w) {
8754
+ function rotate3(local2, x, y, z, w) {
8665
8755
  const [vx2, vy2, vz2] = local2;
8666
8756
  const ix = w * vx2 + y * vz2 - z * vy2;
8667
8757
  const iy = w * vy2 + z * vx2 - x * vz2;
@@ -8719,7 +8809,7 @@ var EffectCamera = class {
8719
8809
  /** A direction given in the camera's own axes, in world axes. */
8720
8810
  forward(local2) {
8721
8811
  const [x, y, z, w] = lookAtQuaternion(this.position, this.target);
8722
- return rotate2(local2, x, y, z, w);
8812
+ return rotate3(local2, x, y, z, w);
8723
8813
  }
8724
8814
  };
8725
8815