castle-web-cli 0.4.231 → 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.
Files changed (55) hide show
  1. package/dist/serverBundle.js +5 -3
  2. package/kits/hub-game/CLAUDE.md +26 -4
  3. package/kits/hub-game/code/client/avatar.js +39 -0
  4. package/kits/hub-game/code/client/chat.js +12 -0
  5. package/kits/hub-game/code/client/chrome.js +194 -4
  6. package/kits/hub-game/code/client/grab.js +31 -7
  7. package/kits/hub-game/code/client/hats.js +26 -0
  8. package/kits/hub-game/code/client/hookLayer.js +30 -3
  9. package/kits/hub-game/code/client/hudClear.js +126 -0
  10. package/kits/hub-game/code/client/state.js +22 -0
  11. package/kits/hub-game/code/client/tiers.js +31 -6
  12. package/kits/hub-game/code/client/together.js +66 -1
  13. package/kits/hub-game/code/client/wear.js +96 -14
  14. package/kits/hub-game/code/server/actors.js +5 -0
  15. package/kits/hub-game/code/server/grab.js +296 -11
  16. package/kits/hub-game/code/server/hub.js +63 -2
  17. package/kits/hub-game/code/server/manifest.js +48 -3
  18. package/kits/hub-game/code/server/party.js +11 -0
  19. package/kits/hub-game/code/server/say.js +8 -2
  20. package/kits/hub-game/code/server/wear.js +116 -11
  21. package/kits/hub-game/code/systems/hub.js +253 -5
  22. package/kits/hub-game/docs/contract.md +204 -8
  23. package/kits/hub-game/package.json +1 -1
  24. package/kits/hub-game/scripts/test-chat.mjs +32 -0
  25. package/kits/hub-game/scripts/test-grab-client.mjs +186 -3
  26. package/kits/hub-game/scripts/test-grab.mjs +857 -3
  27. package/kits/hub-game/scripts/test-hats.mjs +229 -0
  28. package/kits/hub-game/scripts/test-hub-trip.mjs +513 -0
  29. package/kits/hub-game/scripts/test-hud-clear.mjs +676 -0
  30. package/kits/hub-game/scripts/test-manifest.mjs +75 -0
  31. package/kits/hub-game/scripts/test-party.mjs +22 -1
  32. package/kits/hub-game/scripts/test-say.mjs +59 -2
  33. package/kits/hub-game/scripts/test-tiers.mjs +190 -2
  34. package/kits/hub-game/scripts/test-try-on.mjs +940 -0
  35. package/kits/physics-2d/castle.json +1 -1
  36. package/kits/physics-2d/engine/scene.js +35 -0
  37. package/kits/physics-2d/scripts/test-skip-unchanged.mjs +44 -0
  38. package/kits/physics-3d/CLAUDE.md +252 -0
  39. package/kits/physics-3d/behaviors/Player.jsx +363 -2
  40. package/kits/physics-3d/castle.json +2 -2
  41. package/kits/physics-3d/docs/pxmodel-format.md +4 -0
  42. package/kits/physics-3d/engine3d/Scene3DPlayer.jsx +32 -0
  43. package/kits/physics-3d/engine3d/optimize.js +49 -0
  44. package/kits/physics-3d/engine3d/orbitinput.js +277 -0
  45. package/kits/physics-3d/engine3d/world3d.js +81 -25
  46. package/kits/physics-3d/scripts/Player.before-orbit.mjs +310 -0
  47. package/kits/physics-3d/scripts/Scene3DPlayer.before-orbit.jsx +337 -0
  48. package/kits/physics-3d/scripts/test-camera-follow.mjs +12 -2
  49. package/kits/physics-3d/scripts/test-camera-orbit.mjs +2105 -0
  50. package/kits/physics-3d/scripts/test-drop-empty-parts.mjs +787 -0
  51. package/kits/physics-3d/scripts/test-static-actors.mjs +6 -1
  52. package/kits/physics-3d/scripts/test-static-passes.mjs +1 -1
  53. package/kits/physics-3d/scripts/test-steady-hover.mjs +633 -0
  54. package/kits/physics-3d/systems/physics3d.js +91 -6
  55. package/package.json +1 -1
@@ -12,6 +12,7 @@
12
12
  import * as THREE from 'three';
13
13
  import * as RAPIER from '@dimforge/rapier3d-compat';
14
14
  import { remember, sameAs } from '../engine3d/unchanged.js';
15
+ import { isOrbitLook } from '../engine3d/orbitinput.js';
15
16
 
16
17
  const FIXED_STEP = 1 / 60;
17
18
  const MAX_STEPS_PER_FRAME = 4;
@@ -64,10 +65,16 @@ export class Physics3DSystem {
64
65
  this.touching = new Set(); // "aId|bId" sorted pair keys
65
66
  this.enter = [];
66
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;
67
74
  }
68
75
 
69
76
  attach(runtime) {
70
- runtime.physics3d = makeApi(this);
77
+ runtime.physics3d = makeApi(this, runtime);
71
78
  void ensureRapierInit();
72
79
  }
73
80
 
@@ -88,9 +95,18 @@ export class Physics3DSystem {
88
95
  // What a skipping pass wrote down goes with the world it was written for.
89
96
  this.remembered = null;
90
97
  this.rememberedIn = null;
98
+ // A force asked for and not yet used goes with it.
99
+ this.forces = null;
91
100
  // A follow camera asked for before the load goes with the scene it was
92
101
  // asked for.
93
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;
94
110
  }
95
111
 
96
112
  dispose(runtime) {
@@ -105,6 +121,10 @@ export class Physics3DSystem {
105
121
  simulate(scene, dt) {
106
122
  if (!rapierReady) return;
107
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;
108
128
  if (scene.skipsUnchangedActors === true) {
109
129
  this.afterBehaviorsSkipping(scene, dt);
110
130
  return;
@@ -197,7 +217,38 @@ export class Physics3DSystem {
197
217
  }
198
218
  }
199
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
+
200
247
  step(dt) {
248
+ if (this.forces) {
249
+ this.stepForced(dt);
250
+ return;
251
+ }
201
252
  this.enter.length = 0;
202
253
  this.exit.length = 0;
203
254
  this.acc += dt;
@@ -496,14 +547,18 @@ function setEulerFromQuat(t, q) {
496
547
  t.rotationZ = tmpEuler.z / DEG;
497
548
  }
498
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
+
499
556
  // The author-facing API behaviors reach through `scene.physics3d`. Motion goes
500
557
  // through the body (never by writing a dynamic actor's Transform); rays
501
558
  // exclude sensors so ground checks don't land on pickups.
502
- function makeApi(system) {
503
- const bodyOf = (actorOrId) => {
504
- const id = typeof actorOrId === 'string' ? actorOrId : actorOrId?.id;
505
- return system.bodies.get(id)?.body ?? null;
506
- };
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;
507
562
  return {
508
563
  ready: () => rapierReady && Boolean(system.world),
509
564
  debugRender: () => (system.world ? system.world.debugRender() : null),
@@ -515,6 +570,28 @@ function makeApi(system) {
515
570
  },
516
571
  setVelocity: (actor, v) => bodyOf(actor)?.setLinvel(v, true),
517
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
+ },
518
595
  castRay: (origin, dir, maxToi, excludeActor) => {
519
596
  if (!system.world) return null;
520
597
  const ray = new RAPIER.Ray(origin, dir);
@@ -534,5 +611,13 @@ function makeApi(system) {
534
611
  actorId: system.byCollider.get(hit.collider.handle) ?? null,
535
612
  };
536
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
+ },
537
622
  };
538
623
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-cli",
3
- "version": "0.4.231",
3
+ "version": "0.4.232",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/castle-xyz/castle-experimental-web.git"