@adatechnology/game-destruction-three 1.2.0 → 1.3.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 +50 -1
- package/dist/index.global.js +136 -17
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +124 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -3452,6 +3452,97 @@ var createEngineImpacts = (loop, statics, destruction, options = {}, isDebris) =
|
|
|
3452
3452
|
// src/engine-pieces.ts
|
|
3453
3453
|
import { samePatch } from "@adatechnology/game-terrain";
|
|
3454
3454
|
|
|
3455
|
+
// src/kit-energy.ts
|
|
3456
|
+
var KIT_RULE = {
|
|
3457
|
+
/** A piece that lost a leg and is balanced on the line between the ones left is started down over it (`renderer.ts`). */
|
|
3458
|
+
GO_OVER_IF_BALANCED: "goOverIfBalanced",
|
|
3459
|
+
/** What stood on ground that went (a hole under a foot) is started down over the gap (`renderer.ts`). */
|
|
3460
|
+
CHECK_UNDERMINED: "checkUndermined",
|
|
3461
|
+
/** Furniture made a piece balanced on an edge of its feet is started over it (`EDGE_NUDGE`, `renderer.ts`). */
|
|
3462
|
+
LOOSEN_IF_FOOTLESS: "loosenIfFootless",
|
|
3463
|
+
/** A piece of the engine balanced on a narrow support is turned about its edge (`engine-pieces.ts`). */
|
|
3464
|
+
TOPPLE: "topple"
|
|
3465
|
+
};
|
|
3466
|
+
var RULES = Object.values(KIT_RULE);
|
|
3467
|
+
var GRAVITY = 9.81;
|
|
3468
|
+
var KIT_ENERGY_RESOLUTION = 1e-3;
|
|
3469
|
+
var REST_HEIGHT = KIT_ENERGY_RESOLUTION;
|
|
3470
|
+
var kineticEnergyOf = (body) => {
|
|
3471
|
+
const [vx, vy, vz] = body.velocity;
|
|
3472
|
+
const [wx, wy, wz] = body.angularVelocity;
|
|
3473
|
+
const { x, y, z, w } = body.root.quaternion;
|
|
3474
|
+
const lx = (1 - 2 * (y * y + z * z)) * wx + 2 * (x * y + z * w) * wy + 2 * (x * z - y * w) * wz;
|
|
3475
|
+
const ly = 2 * (x * y - z * w) * wx + (1 - 2 * (x * x + z * z)) * wy + 2 * (y * z + x * w) * wz;
|
|
3476
|
+
const lz = 2 * (x * z + y * w) * wx + 2 * (y * z - x * w) * wy + (1 - 2 * (x * x + y * y)) * wz;
|
|
3477
|
+
const [hx, hy, hz] = body.halfExtents;
|
|
3478
|
+
const m = body.mass;
|
|
3479
|
+
let ix = m * (hy * hy + hz * hz) / 3;
|
|
3480
|
+
let iy = m * (hx * hx + hz * hz) / 3;
|
|
3481
|
+
let iz = m * (hx * hx + hy * hy) / 3;
|
|
3482
|
+
const round = body.round;
|
|
3483
|
+
if (round) {
|
|
3484
|
+
const about = round.inertia * m * round.radius * round.radius;
|
|
3485
|
+
if (round.kind === "sphere") ix = iy = iz = about;
|
|
3486
|
+
else if (round.axis === 0) ix = about;
|
|
3487
|
+
else if (round.axis === 1) iy = about;
|
|
3488
|
+
else iz = about;
|
|
3489
|
+
}
|
|
3490
|
+
return 0.5 * m * (vx * vx + vy * vy + vz * vz) + 0.5 * (ix * lx * lx + iy * ly * ly + iz * lz * lz);
|
|
3491
|
+
};
|
|
3492
|
+
var fallEnergyOf = (body) => {
|
|
3493
|
+
const { x, y, z, w } = body.root.quaternion;
|
|
3494
|
+
const [hx, hy, hz] = body.halfExtents;
|
|
3495
|
+
const [cx, cy, cz] = body.massCentre ?? [0, 0, 0];
|
|
3496
|
+
const rx = 2 * (x * y + z * w);
|
|
3497
|
+
const ry = 1 - 2 * (x * x + z * z);
|
|
3498
|
+
const rz = 2 * (y * z - x * w);
|
|
3499
|
+
const over2 = hx * Math.abs(rx) + hy * Math.abs(ry) + hz * Math.abs(rz) + rx * cx + ry * cy + rz * cz;
|
|
3500
|
+
const lowest = Math.min(hx - Math.abs(cx), hy - Math.abs(cy), hz - Math.abs(cz));
|
|
3501
|
+
return body.mass * GRAVITY * Math.max(0, over2 - lowest);
|
|
3502
|
+
};
|
|
3503
|
+
var KitEnergyLedger = class {
|
|
3504
|
+
constructor(onOver) {
|
|
3505
|
+
__publicField(this, "onOver", onOver);
|
|
3506
|
+
__publicField(this, "before", 0);
|
|
3507
|
+
__publicField(this, "tallies", new Map(RULES.map((rule) => [rule, { pushes: 0, overs: 0, worstShare: 0, worst: null }])));
|
|
3508
|
+
__publicField(this, "episodes", /* @__PURE__ */ new WeakMap());
|
|
3509
|
+
}
|
|
3510
|
+
begin(body) {
|
|
3511
|
+
this.before = kineticEnergyOf(body);
|
|
3512
|
+
if (this.before > body.mass * GRAVITY * REST_HEIGHT) return;
|
|
3513
|
+
const episode = this.episodes.get(body);
|
|
3514
|
+
if (!episode) {
|
|
3515
|
+
this.episodes.set(body, { given: new Float64Array(RULES.length), over: new Uint8Array(RULES.length), fall: fallEnergyOf(body) });
|
|
3516
|
+
return;
|
|
3517
|
+
}
|
|
3518
|
+
episode.given.fill(0);
|
|
3519
|
+
episode.over.fill(0);
|
|
3520
|
+
episode.fall = fallEnergyOf(body);
|
|
3521
|
+
}
|
|
3522
|
+
end(rule, body) {
|
|
3523
|
+
const tally = this.tallies.get(rule);
|
|
3524
|
+
tally.pushes++;
|
|
3525
|
+
const episode = this.episodes.get(body);
|
|
3526
|
+
const open = episode ?? { given: new Float64Array(RULES.length), over: new Uint8Array(RULES.length), fall: fallEnergyOf(body) };
|
|
3527
|
+
if (!episode) this.episodes.set(body, open);
|
|
3528
|
+
const index = RULES.indexOf(rule);
|
|
3529
|
+
const given = open.given[index] + kineticEnergyOf(body) - this.before;
|
|
3530
|
+
open.given[index] = given;
|
|
3531
|
+
const share = given <= 0 ? 0 : given / (open.fall + body.mass * GRAVITY * KIT_ENERGY_RESOLUTION);
|
|
3532
|
+
if (share > tally.worstShare) {
|
|
3533
|
+
tally.worstShare = share;
|
|
3534
|
+
tally.worst = { rule, objectId: body.objectId, given, fall: open.fall };
|
|
3535
|
+
}
|
|
3536
|
+
if (share <= 1 || open.over[index]) return;
|
|
3537
|
+
open.over[index] = 1;
|
|
3538
|
+
tally.overs++;
|
|
3539
|
+
this.onOver?.({ rule, objectId: body.objectId, given, fall: open.fall });
|
|
3540
|
+
}
|
|
3541
|
+
tally() {
|
|
3542
|
+
return RULES.map((rule) => ({ rule, ...this.tallies.get(rule) }));
|
|
3543
|
+
}
|
|
3544
|
+
};
|
|
3545
|
+
|
|
3455
3546
|
// src/engine-ground-heights.ts
|
|
3456
3547
|
var changedHeights = (before, after, size) => {
|
|
3457
3548
|
let minX = size;
|
|
@@ -4776,20 +4867,25 @@ var EnginePieces = class {
|
|
|
4776
4867
|
if (wx <= wz) {
|
|
4777
4868
|
const edge = dirX > 0 ? x1 : x0;
|
|
4778
4869
|
const dw = edge * dirX > 0 ? -dirX * this.topplePush(edge, height, isColumn && Math.abs(w[2]) < PUSH_SPIN, this.turningOf(b, 2)) * dt : 0;
|
|
4870
|
+
if (dw === 0) return;
|
|
4779
4871
|
const rx = -edge;
|
|
4780
4872
|
const ry = -low;
|
|
4873
|
+
this.o.kitEnergy?.begin(b);
|
|
4781
4874
|
w[2] += dw;
|
|
4782
4875
|
v[0] += -dw * ry;
|
|
4783
4876
|
v[1] += dw * rx;
|
|
4784
4877
|
} else {
|
|
4785
4878
|
const edge = dirZ > 0 ? z1 : z0;
|
|
4786
4879
|
const dw = edge * dirZ > 0 ? dirZ * this.topplePush(edge, height, isColumn && Math.abs(w[0]) < PUSH_SPIN, this.turningOf(b, 0)) * dt : 0;
|
|
4880
|
+
if (dw === 0) return;
|
|
4787
4881
|
const rz = -edge;
|
|
4788
4882
|
const ry = -low;
|
|
4883
|
+
this.o.kitEnergy?.begin(b);
|
|
4789
4884
|
w[0] += dw;
|
|
4790
4885
|
v[1] += -dw * rz;
|
|
4791
4886
|
v[2] += dw * ry;
|
|
4792
4887
|
}
|
|
4888
|
+
this.o.kitEnergy?.end(KIT_RULE.TOPPLE, b);
|
|
4793
4889
|
}
|
|
4794
4890
|
/** Propped pieces lose most of their friction until they move or sleep (the push only presses them into the prop: an energy-free way to end a wedge). */
|
|
4795
4891
|
slip(b, link, isSlipping) {
|
|
@@ -5541,6 +5637,8 @@ var PieceSystem = class {
|
|
|
5541
5637
|
__publicField(this, "supportNormal", UP);
|
|
5542
5638
|
__publicField(this, "supportFriction", null);
|
|
5543
5639
|
__publicField(this, "supportBody", null);
|
|
5640
|
+
/** What each rule of the kit gave the pieces it pushed (`ruleImpulse`, and the engine's own `topple`). */
|
|
5641
|
+
__publicField(this, "kitEnergy");
|
|
5544
5642
|
/** A piece is asleep on both sides (the engine's too): nothing of it changes until it wakes. */
|
|
5545
5643
|
__publicField(this, "isSettled", (body) => this.engine.isSettled(body));
|
|
5546
5644
|
__publicField(this, "measureAwake", (body) => {
|
|
@@ -5574,7 +5672,9 @@ var PieceSystem = class {
|
|
|
5574
5672
|
this.loop = resolved.loop;
|
|
5575
5673
|
this.stepsItself = resolved.stepsItself;
|
|
5576
5674
|
this.trail = o.interpolate ? new PoseTrail() : null;
|
|
5675
|
+
this.kitEnergy = new KitEnergyLedger(o.onKitEnergy);
|
|
5577
5676
|
this.engine = new EnginePieces(THREE, resolved.loop.world, {
|
|
5677
|
+
kitEnergy: this.kitEnergy,
|
|
5578
5678
|
groundAt: o.terrainAt ?? o.groundAt,
|
|
5579
5679
|
...o.terrainRollingAt ? { groundRollingAt: o.terrainRollingAt } : {},
|
|
5580
5680
|
...o.terrainRollingDampingAt ? { groundRollingDampingAt: o.terrainRollingDampingAt } : {},
|
|
@@ -5916,6 +6016,20 @@ var PieceSystem = class {
|
|
|
5916
6016
|
b.still = 0;
|
|
5917
6017
|
b.calmAt = null;
|
|
5918
6018
|
}
|
|
6019
|
+
/** `impulse` given by a rule of the kit on its own, measured by its name (`kitEnergy`). */
|
|
6020
|
+
ruleImpulse(rule, b, impulse, point) {
|
|
6021
|
+
this.kitEnergy.begin(b);
|
|
6022
|
+
this.impulse(b, impulse, point);
|
|
6023
|
+
this.kitEnergy.end(rule, b);
|
|
6024
|
+
}
|
|
6025
|
+
/** Spin (rad/s, world axes x and z) added by a rule of the kit on its own, measured by its name (`kitEnergy`). */
|
|
6026
|
+
ruleSpin(rule, b, x, z) {
|
|
6027
|
+
this.kitEnergy.begin(b);
|
|
6028
|
+
b.angularVelocity[0] += x;
|
|
6029
|
+
b.angularVelocity[2] += z;
|
|
6030
|
+
b.sleeping = false;
|
|
6031
|
+
this.kitEnergy.end(rule, b);
|
|
6032
|
+
}
|
|
5919
6033
|
/** I^-1 * (x, y, z) in world space for a box with the body's half extents and mass (needs `this.m` to hold the body's rotation). */
|
|
5920
6034
|
mulInvInertia(b, x, y, z) {
|
|
5921
6035
|
this.m.makeRotationFromQuaternion(b.root.quaternion);
|
|
@@ -9212,6 +9326,7 @@ var Renderer = class {
|
|
|
9212
9326
|
...o.groundWorkerFrames !== void 0 ? { groundWorkerFrames: o.groundWorkerFrames } : {},
|
|
9213
9327
|
...this.blastDebris ? { keep: { maxLive: this.blastDebris.maxLivePieces, far: this.blastDebris.far, ...o.focus ? { focus: o.focus } : {}, onFade: (b) => this.fadePiece(b) } } : {},
|
|
9214
9328
|
onHardLanding: (p, point, speed) => this.hardLanding(p, point, speed),
|
|
9329
|
+
...o.onKitEnergy ? { onKitEnergy: o.onKitEnergy } : {},
|
|
9215
9330
|
sweep: (p, from, to) => this.sweepPiece(p, from, to),
|
|
9216
9331
|
onSlam: (p, hit, speed) => this.slam(p, hit, speed),
|
|
9217
9332
|
// two pieces knocked together: each sounds as its material does (a can on a can, a bottle on a table's leg)
|
|
@@ -9663,6 +9778,10 @@ var Renderer = class {
|
|
|
9663
9778
|
const piece = this.pieceSys.get(objectId);
|
|
9664
9779
|
return piece && !piece.debris ? piece : null;
|
|
9665
9780
|
}
|
|
9781
|
+
/** What each rule of the kit that moves a piece by itself (`KIT_RULE`) gave the pieces it pushed, against their own fall (see `RendererOptions.onKitEnergy`). */
|
|
9782
|
+
kitEnergy() {
|
|
9783
|
+
return this.pieceSys.kitEnergy.tally();
|
|
9784
|
+
}
|
|
9666
9785
|
impulse(objectId, impulse, point) {
|
|
9667
9786
|
const p = this.pieceSys.get(objectId);
|
|
9668
9787
|
if (p) this.pieceSys.impulse(p, impulse, point);
|
|
@@ -11340,9 +11459,7 @@ var Renderer = class {
|
|
|
11340
11459
|
this.setContacts(b, b.piece ?? piece, left);
|
|
11341
11460
|
const loose = b.piece;
|
|
11342
11461
|
if (away && loose) {
|
|
11343
|
-
|
|
11344
|
-
loose.angularVelocity[2] -= away.x * EDGE_NUDGE;
|
|
11345
|
-
loose.sleeping = false;
|
|
11462
|
+
this.pieceSys.ruleSpin(KIT_RULE.LOOSEN_IF_FOOTLESS, loose, away.z * EDGE_NUDGE, -away.x * EDGE_NUDGE);
|
|
11346
11463
|
} else this.tipIfTopHeavy(b);
|
|
11347
11464
|
this.o.onLoosened?.(objectId, piece, "apart");
|
|
11348
11465
|
}
|
|
@@ -11438,7 +11555,7 @@ var Renderer = class {
|
|
|
11438
11555
|
body.sleeping = false;
|
|
11439
11556
|
body.still = 0;
|
|
11440
11557
|
body.calmAt = null;
|
|
11441
|
-
if (held.length > 0 && length > 1e-6) this.pieceSys.
|
|
11558
|
+
if (held.length > 0 && length > 1e-6) this.pieceSys.ruleImpulse(KIT_RULE.CHECK_UNDERMINED, body, [0, -body.mass * UNDERMINED_PUSH, 0], [centre[0] + gx, floor, centre[2] + gz]);
|
|
11442
11559
|
if (!was) this.o.onLoosened?.(b.id, body, "ground");
|
|
11443
11560
|
}
|
|
11444
11561
|
}
|
|
@@ -11455,7 +11572,7 @@ var Renderer = class {
|
|
|
11455
11572
|
if (this.standsWith(held, null, centre, feet.floor)) return;
|
|
11456
11573
|
const inside = insideBy(convexHull(held), [centre[0], centre[2]]);
|
|
11457
11574
|
const reach = this.reachAlong(body, inside.out.x, inside.out.z);
|
|
11458
|
-
this.pieceSys.
|
|
11575
|
+
this.pieceSys.ruleImpulse(KIT_RULE.GO_OVER_IF_BALANCED, body, [0, -body.mass * UNDERMINED_PUSH, 0], [centre[0] + inside.out.x * reach, feet.floor, centre[2] + inside.out.z * reach]);
|
|
11459
11576
|
}
|
|
11460
11577
|
/** How far (m) the box of a piece reaches from its middle along the level direction (x, z), turned as it is: the support of its box that way. */
|
|
11461
11578
|
reachAlong(body, x, z) {
|
|
@@ -13527,6 +13644,8 @@ export {
|
|
|
13527
13644
|
IMPACT_FAMILY_TAGS,
|
|
13528
13645
|
IMPACT_SOUNDS,
|
|
13529
13646
|
IMPACT_SOUND_KEYS,
|
|
13647
|
+
KIT_ENERGY_RESOLUTION,
|
|
13648
|
+
KIT_RULE,
|
|
13530
13649
|
MIN_PIECE_MASS_KG,
|
|
13531
13650
|
PhysicsOptionError,
|
|
13532
13651
|
ROCKET_MOTOR_LOOP,
|