castle-web-cli 0.4.230 → 0.4.232
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/castle-host/host.js +81 -0
- package/dist/devSaveStorage.d.ts +36 -0
- package/dist/devSaveStorage.js +469 -0
- package/dist/devSessionChild.js +22 -12
- package/dist/devSessionServer.js +8 -2
- package/dist/devSessionStorage.d.ts +9 -0
- package/dist/devSessionStorage.js +66 -1
- package/dist/init.js +1 -1
- package/dist/serve.js +5 -1
- package/dist/serverBundle.js +5 -3
- package/kits/base/castle.json +1 -1
- package/kits/base/sdk/README.md +148 -1
- package/kits/base/sdk/castle.d.ts +4 -0
- package/kits/base/sdk/castle.js +2 -0
- package/kits/base/sdk/commands.d.ts +15 -0
- package/kits/base/sdk/multiplayer.js +52 -4
- package/kits/base/sdk/save.d.ts +197 -0
- package/kits/base/sdk/save.js +525 -0
- package/kits/base/sdk/saveCoordinator.d.ts +39 -0
- package/kits/base/sdk/saveCoordinator.js +252 -0
- package/kits/base/sdk/server/storage.d.ts +3 -2
- package/kits/base/sdk/server/storage.js +25 -5
- package/kits/base/sdk/server/wrapper.d.ts +4 -0
- package/kits/base/sdk/server/wrapper.js +2 -0
- package/kits/base/sdk/store.d.ts +3 -2
- package/kits/base/sdk/store.js +5 -0
- package/kits/hub-game/CLAUDE.md +29 -4
- package/kits/hub-game/code/client/avatar.js +39 -0
- package/kits/hub-game/code/client/chat.js +12 -0
- package/kits/hub-game/code/client/chrome.js +237 -13
- package/kits/hub-game/code/client/grab.js +166 -7
- package/kits/hub-game/code/client/hats.js +26 -0
- package/kits/hub-game/code/client/hookLayer.js +36 -3
- package/kits/hub-game/code/client/hudClear.js +126 -0
- package/kits/hub-game/code/client/state.js +35 -1
- package/kits/hub-game/code/client/talk.js +10 -36
- package/kits/hub-game/code/client/tiers.js +505 -0
- package/kits/hub-game/code/client/together.js +144 -4
- package/kits/hub-game/code/client/wear.js +96 -14
- package/kits/hub-game/code/server/actors.js +5 -0
- package/kits/hub-game/code/server/grab.js +299 -13
- package/kits/hub-game/code/server/hub.js +162 -7
- package/kits/hub-game/code/server/manifest.js +80 -8
- package/kits/hub-game/code/server/npcs.js +21 -33
- package/kits/hub-game/code/server/party.js +75 -6
- package/kits/hub-game/code/server/say.js +8 -2
- package/kits/hub-game/code/server/wear.js +116 -11
- package/kits/hub-game/code/systems/hub.js +386 -21
- package/kits/hub-game/docs/contract.md +285 -19
- package/kits/hub-game/package.json +1 -1
- package/kits/hub-game/scripts/test-chat.mjs +32 -0
- package/kits/hub-game/scripts/test-grab-client.mjs +511 -0
- package/kits/hub-game/scripts/test-grab.mjs +857 -3
- package/kits/hub-game/scripts/test-hats.mjs +229 -0
- package/kits/hub-game/scripts/test-hub-trip.mjs +721 -0
- package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
- package/kits/hub-game/scripts/test-manifest.mjs +122 -1
- package/kits/hub-game/scripts/test-npcs.mjs +34 -24
- package/kits/hub-game/scripts/test-party.mjs +108 -4
- package/kits/hub-game/scripts/test-say.mjs +59 -2
- package/kits/hub-game/scripts/test-tiers.mjs +854 -0
- package/kits/hub-game/scripts/test-together.mjs +33 -1
- package/kits/hub-game/scripts/test-try-on.mjs +940 -0
- package/kits/physics-2d/CLAUDE.md +2 -0
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/engine/scene.js +127 -0
- package/kits/physics-2d/package.json +1 -1
- package/kits/physics-2d/scripts/test-skip-unchanged.mjs +298 -0
- package/kits/physics-3d/CLAUDE.md +531 -0
- package/kits/physics-3d/behaviors/Player.jsx +437 -3
- package/kits/physics-3d/castle.json +3 -3
- package/kits/physics-3d/docs/pxmodel-format.md +4 -0
- package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +33 -1
- package/kits/physics-3d/engine3d/actornode.js +253 -0
- package/kits/physics-3d/engine3d/nearshadows.js +600 -0
- package/kits/physics-3d/engine3d/optimize.js +49 -0
- package/kits/physics-3d/engine3d/orbitinput.js +277 -0
- package/kits/physics-3d/engine3d/unchanged.js +153 -0
- package/kits/physics-3d/engine3d/world3d.js +525 -11
- package/kits/physics-3d/package.json +1 -1
- package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
- package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
- package/kits/physics-3d/scripts/test-camera-follow.mjs +543 -0
- package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
- package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
- package/kits/physics-3d/scripts/test-near-shadows.mjs +1073 -0
- package/kits/physics-3d/scripts/test-static-actors.mjs +667 -0
- package/kits/physics-3d/scripts/test-static-passes.mjs +459 -0
- package/kits/physics-3d/scripts/test-static-physics.mjs +128 -0
- package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
- package/kits/physics-3d/systems/physics3d.js +273 -9
- package/package.json +1 -1
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
// simulating on the first frame after init resolves.
|
|
12
12
|
import * as THREE from 'three';
|
|
13
13
|
import * as RAPIER from '@dimforge/rapier3d-compat';
|
|
14
|
+
import { remember, sameAs } from '../engine3d/unchanged.js';
|
|
15
|
+
import { isOrbitLook } from '../engine3d/orbitinput.js';
|
|
14
16
|
|
|
15
17
|
const FIXED_STEP = 1 / 60;
|
|
16
18
|
const MAX_STEPS_PER_FRAME = 4;
|
|
@@ -28,6 +30,24 @@ function ensureRapierInit() {
|
|
|
28
30
|
return rapierInitPromise;
|
|
29
31
|
}
|
|
30
32
|
|
|
33
|
+
// A scene that set `"engine": { "camera": { "follow": "rigid" } }`: the
|
|
34
|
+
// Player behavior asks for its camera (`scene.followCamera`: the player's id,
|
|
35
|
+
// the scene data it was asked for, and the function that places it) and it is
|
|
36
|
+
// placed here, last in the frame, once this frame's positions are written back
|
|
37
|
+
// and after the collision callbacks, so on the position the frame is drawn
|
|
38
|
+
// with (behaviors run before physics, and see last frame's). It is placed on a
|
|
39
|
+
// frame physics could not step too (rapier still loading). A request is used
|
|
40
|
+
// once, and only while the scene it was made for is loaded; a load and a
|
|
41
|
+
// dispose drop it (`reset`). Without the key nothing is asked for, and this
|
|
42
|
+
// reads one property and returns.
|
|
43
|
+
export function placeFollowCamera(scene) {
|
|
44
|
+
const request = scene.followCamera;
|
|
45
|
+
if (!request) return;
|
|
46
|
+
scene.followCamera = null;
|
|
47
|
+
if (scene.cameraFollowsRigidly !== true || request.data !== scene.data) return;
|
|
48
|
+
request.place(scene, request.id, request.dt);
|
|
49
|
+
}
|
|
50
|
+
|
|
31
51
|
export function installSystem(runtime) {
|
|
32
52
|
const system = new Physics3DSystem();
|
|
33
53
|
runtime.registerSystem(system);
|
|
@@ -45,14 +65,20 @@ export class Physics3DSystem {
|
|
|
45
65
|
this.touching = new Set(); // "aId|bId" sorted pair keys
|
|
46
66
|
this.enter = [];
|
|
47
67
|
this.exit = [];
|
|
68
|
+
// Forces asked for through `scene.physics3d.addForce` this frame
|
|
69
|
+
// (actorId -> { x, y, z }, and the scene time they were asked at); null
|
|
70
|
+
// when there are none. They are on their bodies only while this frame's
|
|
71
|
+
// physics pass steps (`stepForced`).
|
|
72
|
+
this.forces = null;
|
|
73
|
+
this.forcesAt = undefined;
|
|
48
74
|
}
|
|
49
75
|
|
|
50
76
|
attach(runtime) {
|
|
51
|
-
runtime.physics3d = makeApi(this);
|
|
77
|
+
runtime.physics3d = makeApi(this, runtime);
|
|
52
78
|
void ensureRapierInit();
|
|
53
79
|
}
|
|
54
80
|
|
|
55
|
-
reset() {
|
|
81
|
+
reset(runtime) {
|
|
56
82
|
// Rapier objects live in wasm -- dropping the JS reference leaks them, so
|
|
57
83
|
// both the world and its event queue are freed explicitly. Runs on every
|
|
58
84
|
// scene load (the runtime resets registered systems) and on dispose.
|
|
@@ -66,15 +92,47 @@ export class Physics3DSystem {
|
|
|
66
92
|
this.enter.length = 0;
|
|
67
93
|
this.exit.length = 0;
|
|
68
94
|
this.acc = 0;
|
|
95
|
+
// What a skipping pass wrote down goes with the world it was written for.
|
|
96
|
+
this.remembered = null;
|
|
97
|
+
this.rememberedIn = null;
|
|
98
|
+
// A force asked for and not yet used goes with it.
|
|
99
|
+
this.forces = null;
|
|
100
|
+
// A follow camera asked for before the load goes with the scene it was
|
|
101
|
+
// asked for.
|
|
102
|
+
if (runtime?.followCamera) runtime.followCamera = null;
|
|
103
|
+
// So does the orbit camera's pointer input (engine3d/orbitinput.js):
|
|
104
|
+
// every pointer, what was asked of the camera, and the wheel and
|
|
105
|
+
// context-menu listeners. A runtime that never met the key has none.
|
|
106
|
+
if (isOrbitLook(runtime?.lookInput)) runtime.lookInput.reset();
|
|
107
|
+
// And a facing asked for under the key and not yet taken, whether or not
|
|
108
|
+
// the orbit input was ever made (only a keyed scene asks for one).
|
|
109
|
+
if (runtime?.faceRequest !== undefined) runtime.faceRequest = undefined;
|
|
69
110
|
}
|
|
70
111
|
|
|
71
|
-
dispose() {
|
|
72
|
-
this.reset();
|
|
112
|
+
dispose(runtime) {
|
|
113
|
+
this.reset(runtime);
|
|
73
114
|
}
|
|
74
115
|
|
|
75
116
|
afterBehaviors(scene, dt) {
|
|
117
|
+
this.simulate(scene, dt);
|
|
118
|
+
placeFollowCamera(scene);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
simulate(scene, dt) {
|
|
76
122
|
if (!rapierReady) return;
|
|
77
123
|
this.ensureWorld(scene);
|
|
124
|
+
// A force is for the frame it was asked in: one left from a frame whose
|
|
125
|
+
// pass did not run, or asked after that frame's pass (from a collision
|
|
126
|
+
// callback), is dropped.
|
|
127
|
+
if (this.forces && this.forcesAt !== scene.time) this.forces = null;
|
|
128
|
+
if (scene.skipsUnchangedActors === true) {
|
|
129
|
+
this.afterBehaviorsSkipping(scene, dt);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (this.remembered) {
|
|
133
|
+
this.remembered = null;
|
|
134
|
+
this.rememberedIn = null;
|
|
135
|
+
}
|
|
78
136
|
this.reconcileBodies(scene);
|
|
79
137
|
this.pushTransforms(scene);
|
|
80
138
|
this.step(dt);
|
|
@@ -131,6 +189,7 @@ export class Physics3DSystem {
|
|
|
131
189
|
if (entry.collider) this.byCollider.delete(entry.collider.handle);
|
|
132
190
|
this.world.removeRigidBody(entry.body);
|
|
133
191
|
this.bodies.delete(actorId);
|
|
192
|
+
if (this.remembered) this.remembered.delete(entry);
|
|
134
193
|
for (const key of [...this.touching]) {
|
|
135
194
|
if (key.startsWith(`${actorId}|`) || key.endsWith(`|${actorId}`)) this.touching.delete(key);
|
|
136
195
|
}
|
|
@@ -158,7 +217,38 @@ export class Physics3DSystem {
|
|
|
158
217
|
}
|
|
159
218
|
}
|
|
160
219
|
|
|
220
|
+
// A force lasts one frame's physics pass. rapier keeps a force on a body
|
|
221
|
+
// until it is reset, so the forces asked for this frame are put on their
|
|
222
|
+
// bodies here, just before the frame's fixed steps, and taken off straight
|
|
223
|
+
// after them -- however many steps the frame takes (none, one or several:
|
|
224
|
+
// a force acts once per step, as gravity does), and if a step throws. An
|
|
225
|
+
// actor that has no dynamic body by now (despawned, rebuilt as fixed), or
|
|
226
|
+
// whose body is not the one the force was asked for, gets nothing. So no force outlives the frame it was asked in: whoever wants
|
|
227
|
+
// one every frame asks every frame, and stops by not asking.
|
|
228
|
+
stepForced(dt) {
|
|
229
|
+
const forces = this.forces;
|
|
230
|
+
this.forces = null;
|
|
231
|
+
const forced = [];
|
|
232
|
+
for (const [id, force] of forces) {
|
|
233
|
+
const body = this.bodies.get(id)?.body;
|
|
234
|
+
// The body it was asked for: one made since under the same id (the
|
|
235
|
+
// actor despawned and spawned again, or rebuilt) was asked nothing.
|
|
236
|
+
if (!body || body !== force.body || body.bodyType() !== RAPIER.RigidBodyType.Dynamic) continue;
|
|
237
|
+
body.addForce({ x: force.x, y: force.y, z: force.z }, true);
|
|
238
|
+
forced.push(body);
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
this.step(dt);
|
|
242
|
+
} finally {
|
|
243
|
+
for (const body of forced) body.resetForces(false);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
161
247
|
step(dt) {
|
|
248
|
+
if (this.forces) {
|
|
249
|
+
this.stepForced(dt);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
162
252
|
this.enter.length = 0;
|
|
163
253
|
this.exit.length = 0;
|
|
164
254
|
this.acc += dt;
|
|
@@ -233,6 +323,102 @@ export class Physics3DSystem {
|
|
|
233
323
|
}
|
|
234
324
|
}
|
|
235
325
|
|
|
326
|
+
// `afterBehaviors` for a scene that set `"engine": { "skipUnchangedActors":
|
|
327
|
+
// true }`: the same bodies, pushes, steps and write-back, without building a
|
|
328
|
+
// body's signature or a fixed body's Transform key while what they are built
|
|
329
|
+
// from reads the same, and with the scene's actors listed once (nothing
|
|
330
|
+
// between here and the write-back runs deck code, so the list is the same
|
|
331
|
+
// for each pass). A body's type is still read from the body every frame.
|
|
332
|
+
//
|
|
333
|
+
// By the rule world3d.js states for its passes: nothing this method relies
|
|
334
|
+
// on from one statement to the next is trusted across code outside the
|
|
335
|
+
// engine. Between listing the actors and the write-back it calls only
|
|
336
|
+
// rapier (wasm; no hooks are handed to `step`), `scene.getActors`, and
|
|
337
|
+
// `JSON.stringify` on plain components. The one place it leaves the engine
|
|
338
|
+
// is `dispatchCallbacks`, last, after which nothing here is used again; a
|
|
339
|
+
// behavior that updates the scene from a collision callback starts a pass of
|
|
340
|
+
// its own from the top. What is kept between frames is read against the
|
|
341
|
+
// live components on every use. It is kept in `this.remembered`, a WeakMap
|
|
342
|
+
// from a body's entry to what the body was made from and where it was last
|
|
343
|
+
// pushed, started afresh with each rapier world and dropped on `reset`, on
|
|
344
|
+
// a frame without the key and, for one body, when the body is removed;
|
|
345
|
+
// nothing is kept on the entries, so an entry that is removed, or held by
|
|
346
|
+
// someone else, holds none of it.
|
|
347
|
+
afterBehaviorsSkipping(scene, dt) {
|
|
348
|
+
if (!this.remembered || this.rememberedIn !== this.world) {
|
|
349
|
+
this.remembered = new WeakMap();
|
|
350
|
+
this.rememberedIn = this.world;
|
|
351
|
+
}
|
|
352
|
+
const actors = scene.getActors();
|
|
353
|
+
this.reconcileUnchanged(actors);
|
|
354
|
+
const dynamic = this.pushUnchanged(actors);
|
|
355
|
+
this.step(dt);
|
|
356
|
+
// `writeBack` asks its scene for the actors; it is handed the dynamic
|
|
357
|
+
// ones, in the scene's order, which are the only ones it writes.
|
|
358
|
+
this.writeBack({ getActors: () => dynamic });
|
|
359
|
+
this.dispatchCallbacks(scene);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
reconcileUnchanged(actors) {
|
|
363
|
+
// Each body whose actor is still here is marked with this pass's count; a
|
|
364
|
+
// body without the mark afterwards has lost its actor.
|
|
365
|
+
this.pass = (this.pass ?? 0) + 1;
|
|
366
|
+
const pass = this.pass;
|
|
367
|
+
let marked = 0;
|
|
368
|
+
for (const actor of actors) {
|
|
369
|
+
if (!actor.components.Transform) continue;
|
|
370
|
+
const entry = this.bodyFor(actor);
|
|
371
|
+
if (entry.pass !== pass) {
|
|
372
|
+
entry.pass = pass;
|
|
373
|
+
marked += 1;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (marked === this.bodies.size) return;
|
|
377
|
+
for (const [id, entry] of [...this.bodies]) {
|
|
378
|
+
if (entry.pass !== pass) this.removeBody(id);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// The actor's body, made again when its signature differs. The signature is
|
|
383
|
+
// built and compared, as it is every frame elsewhere, only when something
|
|
384
|
+
// the body is made from reads differently; what it was made from is written
|
|
385
|
+
// down after the body is made.
|
|
386
|
+
bodyFor(actor) {
|
|
387
|
+
let entry = this.bodies.get(actor.id);
|
|
388
|
+
const made = entry && this.remembered.get(entry);
|
|
389
|
+
if (made && madeTheSame(made.madeFrom, actor)) return entry;
|
|
390
|
+
const sig = bodySignature(actor);
|
|
391
|
+
if (!entry || entry.sig !== sig) {
|
|
392
|
+
if (entry) this.removeBody(actor.id);
|
|
393
|
+
this.createBody(actor, sig);
|
|
394
|
+
entry = this.bodies.get(actor.id);
|
|
395
|
+
}
|
|
396
|
+
this.remembered.set(entry, { madeFrom: rememberMaking(actor), pushedAt: null });
|
|
397
|
+
return entry;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// `pushTransforms`, returning the actors whose body is dynamic.
|
|
401
|
+
pushUnchanged(actors) {
|
|
402
|
+
const dynamic = [];
|
|
403
|
+
for (const actor of actors) {
|
|
404
|
+
const entry = this.bodies.get(actor.id);
|
|
405
|
+
const t = actor.components.Transform;
|
|
406
|
+
if (!entry || !t) continue;
|
|
407
|
+
const { body } = entry;
|
|
408
|
+
const type = body.bodyType();
|
|
409
|
+
if (type === RAPIER.RigidBodyType.Dynamic) {
|
|
410
|
+
dynamic.push(actor);
|
|
411
|
+
} else if (type === RAPIER.RigidBodyType.KinematicPositionBased) {
|
|
412
|
+
body.setNextKinematicTranslation(placeOf(t));
|
|
413
|
+
body.setNextKinematicRotation(quatFromEuler(t));
|
|
414
|
+
} else if (type === RAPIER.RigidBodyType.Fixed && !alreadyPushed(entry, this.remembered.get(entry), t)) {
|
|
415
|
+
body.setTranslation(placeOf(t), false);
|
|
416
|
+
body.setRotation(quatFromEuler(t), false);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return dynamic;
|
|
420
|
+
}
|
|
421
|
+
|
|
236
422
|
dispatchCallbacks(scene) {
|
|
237
423
|
for (const [a, b] of this.enter) emitPair(scene, a, b, 'onCollisionEnter');
|
|
238
424
|
for (const [a, b] of this.exit) emitPair(scene, a, b, 'onCollisionExit');
|
|
@@ -263,6 +449,50 @@ function transformKey(t) {
|
|
|
263
449
|
return `${t.x},${t.y},${t.z},${t.rotationX},${t.rotationY},${t.rotationZ}`;
|
|
264
450
|
}
|
|
265
451
|
|
|
452
|
+
function placeOf(t) {
|
|
453
|
+
return { x: t.x, y: t.y, z: t.z };
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// What a body is made from, kept so the next frame can tell without building
|
|
457
|
+
// the signature whether it would come out the same (`unchanged.js`).
|
|
458
|
+
function rememberMaking(actor) {
|
|
459
|
+
const components = actor.components;
|
|
460
|
+
return { body: remember(components.Body), shape: remember(components.Shape), solid: isSolid(actor) };
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function madeTheSame(madeFrom, actor) {
|
|
464
|
+
const components = actor.components;
|
|
465
|
+
return (
|
|
466
|
+
sameAs(components.Body, madeFrom.body) &&
|
|
467
|
+
sameAs(components.Shape, madeFrom.shape) &&
|
|
468
|
+
isSolid(actor) === madeFrom.solid
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// Whether a fixed body is already where its Transform says. The six numbers it
|
|
473
|
+
// was last compared at are kept and compared as they are; anything that is not
|
|
474
|
+
// plainly the same six is settled by the key `pushTransforms` compares, and
|
|
475
|
+
// the entry's `pushedKey` is moved on when that differs.
|
|
476
|
+
function alreadyPushed(entry, made, t) {
|
|
477
|
+
const at = made.pushedAt;
|
|
478
|
+
if (
|
|
479
|
+
at &&
|
|
480
|
+
at.x === t.x &&
|
|
481
|
+
at.y === t.y &&
|
|
482
|
+
at.z === t.z &&
|
|
483
|
+
at.rotationX === t.rotationX &&
|
|
484
|
+
at.rotationY === t.rotationY &&
|
|
485
|
+
at.rotationZ === t.rotationZ
|
|
486
|
+
) {
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
const key = transformKey(t);
|
|
490
|
+
const same = key === entry.pushedKey;
|
|
491
|
+
entry.pushedKey = key;
|
|
492
|
+
made.pushedAt = { x: t.x, y: t.y, z: t.z, rotationX: t.rotationX, rotationY: t.rotationY, rotationZ: t.rotationZ };
|
|
493
|
+
return same;
|
|
494
|
+
}
|
|
495
|
+
|
|
266
496
|
function bodyDescFor(actor, t) {
|
|
267
497
|
const opts = actor.components.Body;
|
|
268
498
|
let desc;
|
|
@@ -317,14 +547,18 @@ function setEulerFromQuat(t, q) {
|
|
|
317
547
|
t.rotationZ = tmpEuler.z / DEG;
|
|
318
548
|
}
|
|
319
549
|
|
|
550
|
+
// rapier's query filter for `castRayFixed`: fixed bodies' solid colliders only.
|
|
551
|
+
function fixedSolids() {
|
|
552
|
+
const flags = RAPIER.QueryFilterFlags;
|
|
553
|
+
return flags.EXCLUDE_DYNAMIC | flags.EXCLUDE_KINEMATIC | flags.EXCLUDE_SENSORS;
|
|
554
|
+
}
|
|
555
|
+
|
|
320
556
|
// The author-facing API behaviors reach through `scene.physics3d`. Motion goes
|
|
321
557
|
// through the body (never by writing a dynamic actor's Transform); rays
|
|
322
558
|
// exclude sensors so ground checks don't land on pickups.
|
|
323
|
-
function makeApi(system) {
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
return system.bodies.get(id)?.body ?? null;
|
|
327
|
-
};
|
|
559
|
+
function makeApi(system, runtime) {
|
|
560
|
+
const idOf = (actorOrId) => (typeof actorOrId === 'string' ? actorOrId : actorOrId?.id);
|
|
561
|
+
const bodyOf = (actorOrId) => system.bodies.get(idOf(actorOrId))?.body ?? null;
|
|
328
562
|
return {
|
|
329
563
|
ready: () => rapierReady && Boolean(system.world),
|
|
330
564
|
debugRender: () => (system.world ? system.world.debugRender() : null),
|
|
@@ -336,6 +570,28 @@ function makeApi(system) {
|
|
|
336
570
|
},
|
|
337
571
|
setVelocity: (actor, v) => bodyOf(actor)?.setLinvel(v, true),
|
|
338
572
|
applyImpulse: (actor, v) => bodyOf(actor)?.applyImpulse(v, true),
|
|
573
|
+
// A force on the actor's body through the fixed steps of this frame's
|
|
574
|
+
// physics pass and no longer, asked from a behavior's `update`: it acts
|
|
575
|
+
// once per step, as gravity does, where an impulse acts once per frame.
|
|
576
|
+
// Asking again in the frame adds to it; `resetForces` drops what was
|
|
577
|
+
// asked. Nothing is kept past the frame, a scene load or a dispose, so a
|
|
578
|
+
// steady push is asked for every frame.
|
|
579
|
+
addForce: (actor, v) => {
|
|
580
|
+
const id = idOf(actor);
|
|
581
|
+
const body = system.world && system.bodies.get(id)?.body;
|
|
582
|
+
if (!body) return;
|
|
583
|
+
if (system.forces && system.forcesAt !== runtime.time) system.forces = null;
|
|
584
|
+
system.forcesAt = runtime.time;
|
|
585
|
+
const forces = (system.forces ??= new Map());
|
|
586
|
+
// A sum only with what was asked of this same body.
|
|
587
|
+
const sum = forces.get(id);
|
|
588
|
+
const same = sum && sum.body === body;
|
|
589
|
+
forces.set(id, { x: (same ? sum.x : 0) + v.x, y: (same ? sum.y : 0) + v.y, z: (same ? sum.z : 0) + v.z, body });
|
|
590
|
+
},
|
|
591
|
+
resetForces: (actor) => {
|
|
592
|
+
system.forces?.delete(idOf(actor));
|
|
593
|
+
if (system.forces?.size === 0) system.forces = null;
|
|
594
|
+
},
|
|
339
595
|
castRay: (origin, dir, maxToi, excludeActor) => {
|
|
340
596
|
if (!system.world) return null;
|
|
341
597
|
const ray = new RAPIER.Ray(origin, dir);
|
|
@@ -355,5 +611,13 @@ function makeApi(system) {
|
|
|
355
611
|
actorId: system.byCollider.get(hit.collider.handle) ?? null,
|
|
356
612
|
};
|
|
357
613
|
},
|
|
614
|
+
// The nearest fixed body's solid collider along a ray, or null: what
|
|
615
|
+
// stands still and blocks (walls, the ground). Sensors and dynamic and
|
|
616
|
+
// kinematic bodies are passed through. The orbit camera asks it what is
|
|
617
|
+
// between it and the player.
|
|
618
|
+
castRayFixed: (origin, dir, maxToi) => {
|
|
619
|
+
const hit = system.world?.castRay(new RAPIER.Ray(origin, dir), maxToi, true, fixedSolids());
|
|
620
|
+
return hit ? { distance: hit.timeOfImpact, actorId: system.byCollider.get(hit.collider.handle) ?? null } : null;
|
|
621
|
+
},
|
|
358
622
|
};
|
|
359
623
|
}
|