@zylem/behaviors 0.5.5 → 0.5.8

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.
@@ -146,28 +146,45 @@ var FirstPersonPresentation = class {
146
146
  );
147
147
  }
148
148
  const vm = this.viewmodels.get(entityUuid);
149
- if (vm && perspective?.initialPosition && state) {
149
+ if (vm && state && (vm.cameraObject || perspective?.initialPosition)) {
150
150
  this.positionViewmodel(vm, perspective, state, runtime);
151
151
  }
152
152
  }
153
153
  /**
154
154
  * Position a viewmodel entity relative to the camera's eye position and rotation.
155
+ * Prefer `cameraObject` parenting for jitter-free follow during movement.
155
156
  */
156
157
  positionViewmodel(vm, perspective, state, runtime) {
158
+ const entity = vm.entity;
159
+ const group = entity?.group;
160
+ if (vm.cameraObject && group) {
161
+ if (entity) {
162
+ entity.skipRenderPoseSync = true;
163
+ }
164
+ if (group.parent !== vm.cameraObject) {
165
+ vm.cameraObject.add(group);
166
+ }
167
+ group.position.copy(vm.offset);
168
+ if (vm.rotation) {
169
+ group.rotation.copy(vm.rotation);
170
+ } else {
171
+ group.rotation.set(0, 0, 0);
172
+ }
173
+ return;
174
+ }
157
175
  const eyePos = perspective.initialPosition;
158
176
  if (!eyePos) return;
159
177
  _viewmodelQuat.setFromEuler(
160
178
  _viewmodelEuler.set(state.pitch, state.yaw, 0, "YXZ")
161
179
  );
180
+ _offset.copy(vm.offset).applyQuaternion(_viewmodelQuat);
162
181
  if (vm.rotation) {
163
182
  _viewmodelLocalQuat.setFromEuler(vm.rotation);
164
183
  _viewmodelQuat.multiply(_viewmodelLocalQuat);
165
184
  }
166
- _offset.copy(vm.offset).applyQuaternion(_viewmodelQuat);
167
185
  const wx = eyePos.x + _offset.x;
168
186
  const wy = eyePos.y + _offset.y;
169
187
  const wz = eyePos.z + _offset.z;
170
- const entity = vm.entity;
171
188
  const handle = entity?.runtimeHandle ?? -1;
172
189
  if (handle >= 0) {
173
190
  runtime.setPosition(handle, wx, wy, wz);
@@ -179,9 +196,9 @@ var FirstPersonPresentation = class {
179
196
  _viewmodelQuat.w
180
197
  );
181
198
  }
182
- if (entity?.group) {
183
- entity.group.position.set(wx, wy, wz);
184
- entity.group.quaternion.copy(_viewmodelQuat);
199
+ if (group) {
200
+ group.position.set(wx, wy, wz);
201
+ group.quaternion.copy(_viewmodelQuat);
185
202
  }
186
203
  }
187
204
  /** Cleanup */
@@ -227,6 +244,8 @@ var FirstPersonBehaviorSystem = class {
227
244
  gameEntity.firstPersonState = createFirstPersonStateComponent();
228
245
  }
229
246
  if (options.perspective && !this.presentation.hasPerspective(gameEntity.uuid)) {
247
+ const eyeHeight = options.eyeHeight ?? defaultOptions.eyeHeight;
248
+ options.perspective.setEyeHeight?.(eyeHeight);
230
249
  this.presentation.setPerspective(gameEntity.uuid, options.perspective);
231
250
  }
232
251
  if (options.viewmodel && !this.presentation.hasViewmodel(gameEntity.uuid)) {
@@ -301,4 +320,4 @@ export {
301
320
  FirstPersonPresentation,
302
321
  FirstPersonBehavior
303
322
  };
304
- //# sourceMappingURL=chunk-B2ERQ6HD.js.map
323
+ //# sourceMappingURL=chunk-DGDNFX43.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lib/behaviors/first-person/first-person-fsm.ts","../src/lib/behaviors/first-person/first-person.system.ts","../src/lib/behaviors/first-person/first-person.descriptor.ts"],"sourcesContent":["/**\n * First Person Controller Finite State Machine\n *\n * Manages state transitions for first-person movement:\n * - Idle: Standing still\n * - Walking: Moving at walk speed\n * - Running: Moving at run/sprint speed\n */\n\nimport { SyncStateMachine, t } from '../../core/utility/sync-state-machine';\nimport type { FirstPersonInputComponent, FirstPersonStateComponent } from './components';\n\n/**\n * First-person movement states\n */\nexport enum FirstPersonState {\n\tIdle = 'idle',\n\tWalking = 'walking',\n\tRunning = 'running',\n}\n\n/**\n * Events that trigger state transitions\n */\nexport enum FirstPersonEvent {\n\tWalk = 'walk',\n\tRun = 'run',\n\tStop = 'stop',\n}\n\n/**\n * Context for the FSM\n */\nexport interface FirstPersonContext {\n\tinput: FirstPersonInputComponent;\n\tstate: FirstPersonStateComponent;\n}\n\n/**\n * First Person Controller FSM\n */\nexport class FirstPersonFSM {\n\tmachine: SyncStateMachine<FirstPersonState, FirstPersonEvent, never>;\n\n\tconstructor(private ctx: FirstPersonContext) {\n\t\tthis.machine = new SyncStateMachine<FirstPersonState, FirstPersonEvent, never>(\n\t\t\tFirstPersonState.Idle,\n\t\t\t[\n\t\t\t\t// Idle transitions\n\t\t\t\tt(FirstPersonState.Idle, FirstPersonEvent.Walk, FirstPersonState.Walking),\n\t\t\t\tt(FirstPersonState.Idle, FirstPersonEvent.Run, FirstPersonState.Running),\n\t\t\t\tt(FirstPersonState.Idle, FirstPersonEvent.Stop, FirstPersonState.Idle),\n\n\t\t\t\t// Walking transitions\n\t\t\t\tt(FirstPersonState.Walking, FirstPersonEvent.Run, FirstPersonState.Running),\n\t\t\t\tt(FirstPersonState.Walking, FirstPersonEvent.Stop, FirstPersonState.Idle),\n\t\t\t\tt(FirstPersonState.Walking, FirstPersonEvent.Walk, FirstPersonState.Walking),\n\n\t\t\t\t// Running transitions\n\t\t\t\tt(FirstPersonState.Running, FirstPersonEvent.Walk, FirstPersonState.Walking),\n\t\t\t\tt(FirstPersonState.Running, FirstPersonEvent.Stop, FirstPersonState.Idle),\n\t\t\t\tt(FirstPersonState.Running, FirstPersonEvent.Run, FirstPersonState.Running),\n\t\t\t],\n\t\t);\n\t}\n\n\t/** Get the current state */\n\tgetState(): FirstPersonState {\n\t\treturn this.machine.getState();\n\t}\n\n\t/** Dispatch an event to the FSM */\n\tdispatch(event: FirstPersonEvent): void {\n\t\tif (this.machine.can(event)) {\n\t\t\tthis.machine.syncDispatch(event);\n\t\t}\n\t}\n\n\t/** Get the current yaw from context */\n\tgetYaw(): number {\n\t\treturn this.ctx.state.yaw;\n\t}\n\n\t/** Get the current pitch from context */\n\tgetPitch(): number {\n\t\treturn this.ctx.state.pitch;\n\t}\n\n\t/**\n\t * Update FSM based on current input and state.\n\t */\n\tupdate(input: FirstPersonInputComponent, state: FirstPersonStateComponent): void {\n\t\tthis.ctx.input = input;\n\t\tthis.ctx.state = state;\n\n\t\tconst hasMovement = Math.abs(input.moveX) > 0.1 || Math.abs(input.moveZ) > 0.1;\n\n\t\tif (hasMovement) {\n\t\t\tif (input.sprint) {\n\t\t\t\tthis.dispatch(FirstPersonEvent.Run);\n\t\t\t} else {\n\t\t\t\tthis.dispatch(FirstPersonEvent.Walk);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.dispatch(FirstPersonEvent.Stop);\n\t\t}\n\t}\n}\n","/**\n * First Person Presentation System\n *\n * Presentation-only companion to the wasm `first_person` Stage behavior:\n * - Syncs the FirstPersonPerspective eye position from the runtime pose\n * - Applies the runtime's yaw/pitch (from the behavior snapshot) to the\n * perspective so the camera matches the simulated look state\n * - Positions an optional viewmodel entity relative to the camera\n *\n * All movement/look simulation happens in the wasm runtime; this system only\n * reads poses and snapshots.\n */\n\nimport { Vector3, Quaternion, Euler, type Object3D } from 'three';\nimport type { FirstPersonPerspective } from '../../camera/perspectives/first-person-perspective';\nimport type {\n\tBehaviorRuntime,\n\tStagePose,\n\tStageBehaviorSnapshot,\n} from '../../runtime/wasm-stage-runtime';\nimport type {\n\tFirstPersonMovementComponent,\n\tFirstPersonInputComponent,\n\tFirstPersonStateComponent,\n} from './components';\nimport type { TransformState } from '../../capabilities/transform-store';\n\n/**\n * Entity interface for the first-person controller.\n * Entities with this behavior have these components attached.\n */\nexport interface FirstPersonEntity {\n\tuuid: string;\n\ttransformStore: TransformState;\n\tfirstPerson: FirstPersonMovementComponent;\n\t$fps: FirstPersonInputComponent;\n\tfirstPersonState: FirstPersonStateComponent;\n}\n\n/**\n * Viewmodel configuration for an entity that follows the camera.\n */\nexport interface ViewmodelConfig {\n\t/** The entity whose visual group should be positioned relative to the camera. */\n\tentity: any;\n\t/** Offset from the eye position in camera-local space (right, down, forward). */\n\toffset: Vector3;\n\t/** Additional rotation applied to the viewmodel in local space (Euler angles in radians). */\n\trotation?: Euler;\n\t/**\n\t * When set, the viewmodel group is parented to this Object3D (typically the\n\t * Three.js camera) and kept at the local offset/rotation. This avoids\n\t * world-space teleport jitter from physics pose sync fighting the camera.\n\t */\n\tcameraObject?: Object3D;\n}\n// Reusable temporaries (avoid per-frame allocations)\nconst _viewmodelQuat = new Quaternion();\nconst _viewmodelLocalQuat = new Quaternion();\nconst _viewmodelEuler = new Euler();\nconst _offset = new Vector3();\nconst _pose: StagePose = { position: [0, 0, 0], rotation: [0, 0, 0, 1] };\nconst _snapshot = {} as StageBehaviorSnapshot;\n\n/**\n * Presentation system driven by runtime pose + snapshot reads.\n */\nexport class FirstPersonPresentation {\n\tprivate perspectives = new Map<string, FirstPersonPerspective>();\n\tprivate viewmodels = new Map<string, ViewmodelConfig>();\n\n\t/**\n\t * Associate a FirstPersonPerspective with an entity (by uuid).\n\t * Called once by the descriptor system when the behavior is initialized.\n\t */\n\tsetPerspective(entityUuid: string, perspective: FirstPersonPerspective): void {\n\t\tthis.perspectives.set(entityUuid, perspective);\n\t}\n\n\thasPerspective(entityUuid: string): boolean {\n\t\treturn this.perspectives.has(entityUuid);\n\t}\n\n\t/**\n\t * Attach a viewmodel (weapon/item) to an entity.\n\t * The viewmodel entity's group will be positioned relative to the camera each frame.\n\t */\n\tsetViewmodel(entityUuid: string, config: ViewmodelConfig): void {\n\t\tthis.viewmodels.set(entityUuid, config);\n\t}\n\n\thasViewmodel(entityUuid: string): boolean {\n\t\treturn this.viewmodels.has(entityUuid);\n\t}\n\n\t/**\n\t * Sync camera + viewmodel for one entity from the runtime state.\n\t */\n\tsyncEntity(\n\t\tentityUuid: string,\n\t\truntime: BehaviorRuntime,\n\t\thandle: number,\n\t\tmovement: FirstPersonMovementComponent,\n\t\tstate: FirstPersonStateComponent | undefined,\n\t): void {\n\t\tconst perspective = this.perspectives.get(entityUuid);\n\t\tconst pose = runtime.getPose(handle, _pose);\n\t\tconst snapshot = runtime.readSnapshot(handle, _snapshot);\n\n\t\tif (perspective && snapshot) {\n\t\t\tperspective.setLook?.(snapshot.yaw, snapshot.pitch);\n\t\t\tif (state) {\n\t\t\t\tstate.yaw = snapshot.yaw;\n\t\t\t\tstate.pitch = snapshot.pitch;\n\t\t\t\tstate.currentSpeed = snapshot.speed;\n\t\t\t}\n\t\t}\n\n\t\tif (perspective?.initialPosition && pose) {\n\t\t\tperspective.initialPosition.set(\n\t\t\t\tpose.position[0],\n\t\t\t\tpose.position[1] + movement.eyeHeight,\n\t\t\t\tpose.position[2],\n\t\t\t);\n\t\t}\n\n\t\tconst vm = this.viewmodels.get(entityUuid);\n\t\tif (vm && state && (vm.cameraObject || perspective?.initialPosition)) {\n\t\t\tthis.positionViewmodel(vm, perspective!, state, runtime);\n\t\t}\n\t}\n\n\t/**\n\t * Position a viewmodel entity relative to the camera's eye position and rotation.\n\t * Prefer `cameraObject` parenting for jitter-free follow during movement.\n\t */\n\tprivate positionViewmodel(\n\t\tvm: ViewmodelConfig,\n\t\tperspective: FirstPersonPerspective,\n\t\tstate: FirstPersonStateComponent,\n\t\truntime: BehaviorRuntime,\n\t): void {\n\t\tconst entity = vm.entity;\n\t\tconst group = entity?.group as Object3D | undefined;\n\n\t\t// Camera-parented path: local transform only — stays locked to the rendered view.\n\t\tif (vm.cameraObject && group) {\n\t\t\tif (entity) {\n\t\t\t\tentity.skipRenderPoseSync = true;\n\t\t\t}\n\t\t\tif (group.parent !== vm.cameraObject) {\n\t\t\t\tvm.cameraObject.add(group);\n\t\t\t}\n\t\t\tgroup.position.copy(vm.offset);\n\t\t\tif (vm.rotation) {\n\t\t\t\tgroup.rotation.copy(vm.rotation);\n\t\t\t} else {\n\t\t\t\tgroup.rotation.set(0, 0, 0);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tconst eyePos = perspective.initialPosition;\n\t\tif (!eyePos) return;\n\n\t\t// Look rotation only for the offset (matches camera-parented path).\n\t\t_viewmodelQuat.setFromEuler(\n\t\t\t_viewmodelEuler.set(state.pitch, state.yaw, 0, 'YXZ'),\n\t\t);\n\t\t_offset.copy(vm.offset).applyQuaternion(_viewmodelQuat);\n\n\t\t// Mesh orientation: look * viewmodel local fix\n\t\tif (vm.rotation) {\n\t\t\t_viewmodelLocalQuat.setFromEuler(vm.rotation);\n\t\t\t_viewmodelQuat.multiply(_viewmodelLocalQuat);\n\t\t}\n\n\t\tconst wx = eyePos.x + _offset.x;\n\t\tconst wy = eyePos.y + _offset.y;\n\t\tconst wz = eyePos.z + _offset.z;\n\n\t\t// Move the runtime body (if registered) so physics stays in sync\n\t\tconst handle = (entity?.runtimeHandle ?? -1) as number;\n\t\tif (handle >= 0) {\n\t\t\truntime.setPosition(handle, wx, wy, wz);\n\t\t\truntime.setRotation(\n\t\t\t\thandle,\n\t\t\t\t_viewmodelQuat.x, _viewmodelQuat.y, _viewmodelQuat.z, _viewmodelQuat.w,\n\t\t\t);\n\t\t}\n\n\t\t// Also set the group directly for immediate visual update\n\t\tif (group) {\n\t\t\tgroup.position.set(wx, wy, wz);\n\t\t\tgroup.quaternion.copy(_viewmodelQuat);\n\t\t}\n\t}\n\n\t/** Cleanup */\n\tdestroy(): void {\n\t\tthis.perspectives.clear();\n\t\tthis.viewmodels.clear();\n\t}\n}\n","/**\n * First Person Controller Behavior Descriptor\n *\n * Type-safe descriptor for the first-person controller behavior system.\n * Provides entity.use() API for FPS movement and camera control.\n */\n\nimport { Euler, type Object3D } from 'three';\nimport { defineBehavior } from '../behavior-descriptor';\nimport type { BehaviorEntityLink, BehaviorSystem } from '../behavior-system';\nimport type { FirstPersonPerspective } from '../../camera/perspectives/first-person-perspective';\nimport { Vec3Input, VEC3_ZERO, toThreeVector3 } from '../../core/vector';\nimport {\n\tFirstPersonPresentation,\n\ttype FirstPersonEntity,\n\ttype ViewmodelConfig,\n} from './first-person.system';\nimport { FirstPersonState } from './first-person-fsm';\nimport {\n\tcreateFirstPersonMovementComponent,\n\tcreateFirstPersonInputComponent,\n\tcreateFirstPersonStateComponent,\n} from './components';\nimport {\n\tSTAGE_INPUT_BUTTON_RUN,\n\treadBehaviorSnapshot,\n\ttype BehaviorRuntime,\n} from '../../runtime/wasm-stage-runtime';\n\n/**\n * First-person controller behavior options (typed for entity.use() autocomplete).\n */\nexport interface FirstPersonBehaviorOptions {\n\t/** Base walking speed (default: 8) */\n\twalkSpeed?: number;\n\t/** Sprint/run speed (default: 16) */\n\trunSpeed?: number;\n\t/** Look sensitivity multiplier (default: 2) */\n\tlookSensitivity?: number;\n\t/** Eye height offset above entity position (default: 1.7) */\n\teyeHeight?: number;\n\t/** The FirstPersonPerspective to drive (from camera.getPerspective()) */\n\tperspective?: FirstPersonPerspective;\n\t/** Optional viewmodel (weapon/item) to position relative to the camera */\n\tviewmodel?: {\n\t\tentity: any;\n\t\toffset: Vec3Input;\n\t\trotation?: Euler;\n\t\t/** Parent the viewmodel to this Object3D (e.g. the Three.js camera) for jitter-free follow. */\n\t\tcameraObject?: Object3D;\n\t};\n}\n\nconst defaultOptions: FirstPersonBehaviorOptions = {\n\twalkSpeed: 8,\n\trunSpeed: 16,\n\tlookSensitivity: 2,\n\teyeHeight: 1.7,\n};\n\nconst FIRST_PERSON_BEHAVIOR_KEY = Symbol.for(\n\t'zylem:behavior:first-person-controller',\n);\n\n/**\n * Forwards look/move input into the wasm runtime and syncs the camera\n * perspective + optional viewmodel from the runtime's pose/snapshot\n * (presentation only — the Rust `first_person` behavior owns simulation).\n */\nclass FirstPersonBehaviorSystem implements BehaviorSystem {\n\tprivate presentation = new FirstPersonPresentation();\n\tprivate attachedRuntimeSlots = new Set<number>();\n\n\tconstructor(\n\t\tprivate wasmStage: BehaviorRuntime | null,\n\t\tprivate getBehaviorLinks?: (key: symbol) => Iterable<BehaviorEntityLink>,\n\t) {}\n\n\tupdate(_ecs: unknown, _delta: number): void {\n\t\tconst links = this.getBehaviorLinks?.(FIRST_PERSON_BEHAVIOR_KEY);\n\t\tif (!links) return;\n\n\t\tfor (const link of links) {\n\t\t\tconst gameEntity = link.entity as any;\n\t\t\tconst fpsRef = link.ref as any;\n\n\t\t\tconst options = fpsRef.options as FirstPersonBehaviorOptions;\n\n\t\t\t// Initialize components (once)\n\t\t\tif (!gameEntity.firstPerson) {\n\t\t\t\tgameEntity.firstPerson = createFirstPersonMovementComponent(options);\n\t\t\t}\n\t\t\tif (!gameEntity.$fps) {\n\t\t\t\tgameEntity.$fps = createFirstPersonInputComponent();\n\t\t\t}\n\t\t\tif (!gameEntity.firstPersonState) {\n\t\t\t\tgameEntity.firstPersonState = createFirstPersonStateComponent();\n\t\t\t}\n\n\t\t\t// Wire up perspective (once)\n\t\t\tif (options.perspective && !this.presentation.hasPerspective(gameEntity.uuid)) {\n\t\t\t\tconst eyeHeight = options.eyeHeight ?? defaultOptions.eyeHeight!;\n\t\t\t\toptions.perspective.setEyeHeight?.(eyeHeight);\n\t\t\t\tthis.presentation.setPerspective(gameEntity.uuid, options.perspective);\n\t\t\t}\n\n\t\t\t// Wire up viewmodel (once)\n\t\t\tif (options.viewmodel && !this.presentation.hasViewmodel(gameEntity.uuid)) {\n\t\t\t\tthis.presentation.setViewmodel(gameEntity.uuid, {\n\t\t\t\t\t...options.viewmodel,\n\t\t\t\t\toffset: toThreeVector3(options.viewmodel.offset, VEC3_ZERO),\n\t\t\t\t} as ViewmodelConfig);\n\t\t\t}\n\n\t\t\tconst handle = (gameEntity.runtimeHandle ?? -1) as number;\n\t\t\tif (!this.wasmStage || handle < 0) continue;\n\n\t\t\tthis.ensureWasmAttached(handle, options);\n\t\t\tfpsRef.__wasmStage = this.wasmStage;\n\t\t\tfpsRef.__entity = gameEntity;\n\t\t\tconst input = gameEntity.$fps;\n\t\t\tif (input) {\n\t\t\t\tthis.wasmStage.writeInputAxes(handle, input.moveX ?? 0, input.moveZ ?? 0);\n\t\t\t\tthis.wasmStage.writeInputLook(handle, input.lookX ?? 0, input.lookY ?? 0);\n\t\t\t\tthis.wasmStage.writeInputButton(handle, STAGE_INPUT_BUTTON_RUN, !!input.sprint);\n\t\t\t}\n\n\t\t\tthis.presentation.syncEntity(\n\t\t\t\tgameEntity.uuid,\n\t\t\t\tthis.wasmStage,\n\t\t\t\thandle,\n\t\t\t\tgameEntity.firstPerson,\n\t\t\t\tgameEntity.firstPersonState,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate ensureWasmAttached(handle: number, options: FirstPersonBehaviorOptions): void {\n\t\tif (!this.wasmStage || this.attachedRuntimeSlots.has(handle)) return;\n\t\tthis.wasmStage.attachFirstPerson(handle, {\n\t\t\twalkSpeed: options.walkSpeed ?? defaultOptions.walkSpeed!,\n\t\t\trunSpeed: options.runSpeed ?? defaultOptions.runSpeed!,\n\t\t\teyeHeight: options.eyeHeight ?? defaultOptions.eyeHeight!,\n\t\t\tlookSensitivity: options.lookSensitivity ?? defaultOptions.lookSensitivity!,\n\t\t});\n\t\tthis.attachedRuntimeSlots.add(handle);\n\t}\n\n\tdestroy(_ecs: unknown): void {\n\t\tthis.presentation.destroy();\n\t\tthis.attachedRuntimeSlots.clear();\n\t}\n}\n\n/**\n * FirstPersonBehavior - typed descriptor for first-person movement and camera control.\n *\n * Provides complete first-person controller including:\n * - WASD movement relative to camera yaw\n * - Mouse look driving a FirstPersonPerspective\n * - Optional viewmodel positioning (weapon/item following camera)\n * - Walk/run state tracking via FSM\n *\n * @example\n * ```typescript\n * import { FirstPersonBehavior } from \"@zylem/game-lib/behavior\";\n * import { createCamera, Perspectives, FirstPersonPerspective } from \"@zylem/game-lib/core\";\n * import { createActor } from \"@zylem/game-lib/entity\";\n *\n * const fpsCamera = createCamera({ perspective: Perspectives.FirstPerson, ... });\n * const fps = fpsCamera.getPerspective<FirstPersonPerspective>();\n *\n * const player = createActor({ name: 'player', ... });\n * const controller = player.use(FirstPersonBehavior, {\n * perspective: fps,\n * walkSpeed: 8,\n * runSpeed: 16,\n * lookSensitivity: 2,\n * });\n *\n * // In update loop - write input, behavior handles the rest\n * player.onUpdate(({ inputs }) => {\n * player.$fps.moveX = inputs.p1.axes.Horizontal.value;\n * player.$fps.moveZ = inputs.p1.axes.Vertical.value;\n * player.$fps.lookX = inputs.p1.axes.SecondaryHorizontal.value;\n * player.$fps.lookY = inputs.p1.axes.SecondaryVertical.value;\n * player.$fps.sprint = inputs.p1.shoulders.LTrigger.held > 0;\n * });\n * ```\n */\nexport const FirstPersonBehavior = defineBehavior<\n\tFirstPersonBehaviorOptions,\n\t{\n\t\tgetState: () => FirstPersonState;\n\t\tgetYaw: () => number;\n\t\tgetPitch: () => number;\n\t\tattachViewmodel: (entity: any, offset: Vec3Input) => void;\n\t},\n\tFirstPersonEntity\n>({\n\tname: 'first-person-controller',\n\tdefaultOptions,\n\tsystemFactory: (ctx) =>\n\t\tnew FirstPersonBehaviorSystem(ctx.wasmStage ?? null, ctx.getBehaviorLinks),\n\t// Handles read the runtime snapshot buffer (previous-step values). The\n\t// Rust behavior is the single source of truth.\n\tcreateHandle: (ref) => ({\n\t\tgetState: () => {\n\t\t\tconst snapshot = readBehaviorSnapshot(ref);\n\t\t\tif (!snapshot) return FirstPersonState.Idle;\n\t\t\tif (snapshot.speed <= 0.01) return FirstPersonState.Idle;\n\t\t\tconst walkSpeed = ref.options.walkSpeed ?? defaultOptions.walkSpeed!;\n\t\t\treturn snapshot.speed > walkSpeed\n\t\t\t\t? FirstPersonState.Running\n\t\t\t\t: FirstPersonState.Walking;\n\t\t},\n\t\tgetYaw: () => readBehaviorSnapshot(ref)?.yaw ?? 0,\n\t\tgetPitch: () => readBehaviorSnapshot(ref)?.pitch ?? 0,\n\t\tattachViewmodel: (entity: any, offset: Vec3Input) => {\n\t\t\tif (ref.options) {\n\t\t\t\tref.options.viewmodel = { entity, offset };\n\t\t\t}\n\t\t},\n\t}),\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAeO,IAAK,mBAAL,kBAAKA,sBAAL;AACN,EAAAA,kBAAA,UAAO;AACP,EAAAA,kBAAA,aAAU;AACV,EAAAA,kBAAA,aAAU;A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Aa,eAAe;AAAA,MAC/C,iBAAiB,QAAQ,mBAAmB,eAAe;AAAA,IAC5D,CAAC;AACD,SAAK,qBAAqB,IAAI,MAAM;AAAA,EACrC;AAAA,EAEA,QAAQ,MAAqB;AAC5B,SAAK,aAAa,QAAQ;AAC1B,SAAK,qBAAqB,MAAM;AAAA,EACjC;AACD;AAsCO,IAAM,sBAAsB,eASjC;AAAA,EACD,MAAM;AAAA,EACN;AAAA,EACA,eAAe,CAAC,QACf,IAAI,0BAA0B,IAAI,aAAa,MAAM,IAAI,gBAAgB;AAAA;AAAA;AAAA,EAG1E,cAAc,CAAC,SAAS;AAAA,IACvB,UAAU,MAAM;AACf,YAAM,WAAW,qBAAqB,GAAG;AACzC,UAAI,CAAC,SAAU;AACf,UAAI,SAAS,SAAS,KAAM;AAC5B,YAAM,YAAY,IAAI,QAAQ,aAAa,eAAe;AAC1D,aAAO,SAAS,QAAQ;AAAA,IAGzB;AAAA,IACA,QAAQ,MAAM,qBAAqB,GAAG,GAAG,OAAO;AAAA,IAChD,UAAU,MAAM,qBAAqB,GAAG,GAAG,SAAS;AAAA,IACpD,iBAAiB,CAAC,QAAa,WAAsB;AACpD,UAAI,IAAI,SAAS;AAChB,YAAI,QAAQ,YAAY,EAAE,QAAQ,OAAO;AAAA,MAC1C;AAAA,IACD;AAAA,EACD;AACD,CAAC;","names":["FirstPersonState","FirstPersonEvent"]}
package/dist/core.d.ts CHANGED
@@ -4,7 +4,7 @@ export { P as PhysicsBodyComponent, T as TransformComponent, c as createPhysicsB
4
4
  export { E as EntityWithBody, M as MoveableBodyLike, a as MoveableEntity, V as Vector, g as getPosition, b as getVelocity, m as makeMoveable, c as move, d as moveForwardXY, e as moveX, f as moveXY, h as moveXZ, i as moveY, j as moveZ, r as resetVelocity, s as setPosition, k as setPositionX, l as setPositionY, n as setPositionZ, w as wrapAround3D, o as wrapAroundXY } from './moveable-BdFmI3s1.js';
5
5
  export { V as VEC2_ONE, a as VEC2_ZERO, b as VEC3_ONE, c as VEC3_ZERO, d as Vec2, e as Vec2Components, f as Vec2Input, g as Vec3, h as Vec3Components, i as Vec3Input, j as isVec2Input, k as isVec3Input, n as normalizeVec2, l as normalizeVec3, t as toThreeVector2, m as toThreeVector3 } from './vector-DjhLUrak.js';
6
6
  export { S as SyncStateMachine } from './sync-state-machine-CZyspBpj.js';
7
- export { F as FirstPersonPerspective } from './first-person-perspective-C9Mptl0-.js';
7
+ export { F as FirstPersonPerspective } from './first-person-perspective-BxFHgFln.js';
8
8
  export { ILogger, ITransition, SyncCallback, t } from 'typescript-fsm';
9
9
  export { STAGE_EVENT_STRIDE, STAGE_INPUT_STRIDE, STAGE_RENDER_STRIDE, STAGE_SNAPSHOT_STRIDE, StageWasmExports } from '@zylem/runtime';
10
10
  import 'three';
@@ -18,6 +18,8 @@ interface FirstPersonPerspective {
18
18
  look(deltaYaw: number, deltaPitch: number): void;
19
19
  /** Set absolute yaw and pitch. */
20
20
  setLook?(yaw: number, pitch: number): void;
21
+ /** Set eye height above the followed target. */
22
+ setEyeHeight?(height: number): void;
21
23
  }
22
24
 
23
25
  export type { FirstPersonPerspective as F };
@@ -1,8 +1,8 @@
1
1
  import { F as FirstPersonInputComponent, b as FirstPersonStateComponent, a as FirstPersonMovementComponent } from './components-qWwOsvwh.js';
2
2
  export { c as createFirstPersonInputComponent, d as createFirstPersonMovementComponent, e as createFirstPersonStateComponent } from './components-qWwOsvwh.js';
3
3
  import { S as SyncStateMachine } from './sync-state-machine-CZyspBpj.js';
4
- import { Vector3, Euler } from 'three';
5
- import { F as FirstPersonPerspective } from './first-person-perspective-C9Mptl0-.js';
4
+ import { Vector3, Euler, Object3D } from 'three';
5
+ import { F as FirstPersonPerspective } from './first-person-perspective-BxFHgFln.js';
6
6
  import { Z as TransformState, e as BehaviorRuntime, a as BehaviorDescriptor } from './behavior-system-HEt67el5.js';
7
7
  import { i as Vec3Input } from './vector-DjhLUrak.js';
8
8
  import 'typescript-fsm';
@@ -95,6 +95,12 @@ interface ViewmodelConfig {
95
95
  offset: Vector3;
96
96
  /** Additional rotation applied to the viewmodel in local space (Euler angles in radians). */
97
97
  rotation?: Euler;
98
+ /**
99
+ * When set, the viewmodel group is parented to this Object3D (typically the
100
+ * Three.js camera) and kept at the local offset/rotation. This avoids
101
+ * world-space teleport jitter from physics pose sync fighting the camera.
102
+ */
103
+ cameraObject?: Object3D;
98
104
  }
99
105
  /**
100
106
  * Presentation system driven by runtime pose + snapshot reads.
@@ -120,6 +126,7 @@ declare class FirstPersonPresentation {
120
126
  syncEntity(entityUuid: string, runtime: BehaviorRuntime, handle: number, movement: FirstPersonMovementComponent, state: FirstPersonStateComponent | undefined): void;
121
127
  /**
122
128
  * Position a viewmodel entity relative to the camera's eye position and rotation.
129
+ * Prefer `cameraObject` parenting for jitter-free follow during movement.
123
130
  */
124
131
  private positionViewmodel;
125
132
  /** Cleanup */
@@ -145,6 +152,8 @@ interface FirstPersonBehaviorOptions {
145
152
  entity: any;
146
153
  offset: Vec3Input;
147
154
  rotation?: Euler;
155
+ /** Parent the viewmodel to this Object3D (e.g. the Three.js camera) for jitter-free follow. */
156
+ cameraObject?: Object3D;
148
157
  };
149
158
  }
150
159
  /**
@@ -4,7 +4,7 @@ import {
4
4
  FirstPersonFSM,
5
5
  FirstPersonPresentation,
6
6
  FirstPersonState
7
- } from "./chunk-B2ERQ6HD.js";
7
+ } from "./chunk-DGDNFX43.js";
8
8
  import {
9
9
  createFirstPersonInputComponent,
10
10
  createFirstPersonMovementComponent,
package/dist/index.d.ts CHANGED
@@ -36,7 +36,7 @@ import './sync-state-machine-CZyspBpj.js';
36
36
  import 'typescript-fsm';
37
37
  import './vector-DjhLUrak.js';
38
38
  import './entity-types-DhfsZbSK.js';
39
- import './first-person-perspective-C9Mptl0-.js';
39
+ import './first-person-perspective-BxFHgFln.js';
40
40
  import './moveable-BdFmI3s1.js';
41
41
  import 'three/webgpu';
42
42
 
package/dist/index.js CHANGED
@@ -138,7 +138,7 @@ import {
138
138
  FirstPersonFSM,
139
139
  FirstPersonPresentation,
140
140
  FirstPersonState
141
- } from "./chunk-B2ERQ6HD.js";
141
+ } from "./chunk-DGDNFX43.js";
142
142
  import {
143
143
  createFirstPersonInputComponent,
144
144
  createFirstPersonMovementComponent,
package/package.json CHANGED
@@ -1,160 +1,162 @@
1
1
  {
2
- "name": "@zylem/behaviors",
3
- "version": "0.5.5",
4
- "type": "module",
5
- "description": "Tree-shakable Zylem behavior descriptors, systems, coordinators, and runtime-agnostic behavior helpers.",
6
- "license": "MIT",
7
- "repository": {
8
- "type": "git",
9
- "url": "git+ssh://git@github.com/zylem-game-lib/behaviors.git"
10
- },
11
- "engines": {
12
- "node": ">=22.12.0",
13
- "pnpm": ">=10.32.1"
14
- },
15
- "sideEffects": false,
16
- "files": [
17
- "dist"
18
- ],
19
- "exports": {
20
- ".": {
21
- "types": "./dist/index.d.ts",
22
- "import": "./dist/index.js"
23
- },
24
- "./core": {
25
- "types": "./dist/core.d.ts",
26
- "import": "./dist/core.js"
27
- },
28
- "./destructible-prebake": {
29
- "types": "./dist/destructible-prebake.d.ts",
30
- "import": "./dist/destructible-prebake.js"
31
- },
32
- "./cooldown": {
33
- "types": "./dist/cooldown.d.ts",
34
- "import": "./dist/cooldown.js"
35
- },
36
- "./destructible-3d": {
37
- "types": "./dist/destructible-3d.d.ts",
38
- "import": "./dist/destructible-3d.js"
39
- },
40
- "./first-person": {
41
- "types": "./dist/first-person.d.ts",
42
- "import": "./dist/first-person.js"
43
- },
44
- "./jumper-2d": {
45
- "types": "./dist/jumper-2d.d.ts",
46
- "import": "./dist/jumper-2d.js"
47
- },
48
- "./jumper-3d": {
49
- "types": "./dist/jumper-3d.d.ts",
50
- "import": "./dist/jumper-3d.js"
51
- },
52
- "./particle-emitter": {
53
- "types": "./dist/particle-emitter.d.ts",
54
- "import": "./dist/particle-emitter.js"
55
- },
56
- "./particle-emitter/presets": {
57
- "types": "./dist/particle-emitter/presets.d.ts",
58
- "import": "./dist/particle-emitter/presets.js"
59
- },
60
- "./platformer-2d": {
61
- "types": "./dist/platformer-2d.d.ts",
62
- "import": "./dist/platformer-2d.js"
63
- },
64
- "./platformer-3d": {
65
- "types": "./dist/platformer-3d.d.ts",
66
- "import": "./dist/platformer-3d.js"
67
- },
68
- "./ricochet-2d": {
69
- "types": "./dist/ricochet-2d.d.ts",
70
- "import": "./dist/ricochet-2d.js"
71
- },
72
- "./ricochet-3d": {
73
- "types": "./dist/ricochet-3d.d.ts",
74
- "import": "./dist/ricochet-3d.js"
75
- },
76
- "./runtime-2d": {
77
- "types": "./dist/runtime-2d.d.ts",
78
- "import": "./dist/runtime-2d.js"
79
- },
80
- "./screen-visibility": {
81
- "types": "./dist/screen-visibility.d.ts",
82
- "import": "./dist/screen-visibility.js"
83
- },
84
- "./screen-wrap": {
85
- "types": "./dist/screen-wrap.d.ts",
86
- "import": "./dist/screen-wrap.js"
87
- },
88
- "./shooter-2d": {
89
- "types": "./dist/shooter-2d.d.ts",
90
- "import": "./dist/shooter-2d.js"
91
- },
92
- "./thruster": {
93
- "types": "./dist/thruster.d.ts",
94
- "import": "./dist/thruster.js"
95
- },
96
- "./top-down-movement": {
97
- "types": "./dist/top-down-movement.d.ts",
98
- "import": "./dist/top-down-movement.js"
99
- },
100
- "./world-boundary-2d": {
101
- "types": "./dist/world-boundary-2d.d.ts",
102
- "import": "./dist/world-boundary-2d.js"
103
- },
104
- "./world-boundary-3d": {
105
- "types": "./dist/world-boundary-3d.d.ts",
106
- "import": "./dist/world-boundary-3d.js"
107
- },
108
- "./coordinators/boundary-ricochet": {
109
- "types": "./dist/coordinators/boundary-ricochet.d.ts",
110
- "import": "./dist/coordinators/boundary-ricochet.js"
111
- },
112
- "./coordinators/boundary-ricochet-3d": {
113
- "types": "./dist/coordinators/boundary-ricochet-3d.d.ts",
114
- "import": "./dist/coordinators/boundary-ricochet-3d.js"
115
- },
116
- "./coordinators/first-person-shooter": {
117
- "types": "./dist/coordinators/first-person-shooter.d.ts",
118
- "import": "./dist/coordinators/first-person-shooter.js"
119
- },
120
- "./coordinators/multidirectional-space-shooter": {
121
- "types": "./dist/coordinators/multidirectional-space-shooter.d.ts",
122
- "import": "./dist/coordinators/multidirectional-space-shooter.js"
123
- },
124
- "./coordinators/top-down-shooter": {
125
- "types": "./dist/coordinators/top-down-shooter.d.ts",
126
- "import": "./dist/coordinators/top-down-shooter.js"
127
- },
128
- "./package.json": "./package.json"
129
- },
130
- "dependencies": {
131
- "@dgreenheck/three-pinata": "^2.0.1",
132
- "@zylem/runtime": "link:../runtime",
133
- "comlink": "^4.4.2",
134
- "typescript-fsm": "^1.6.0",
135
- "valtio": "^2.1.7"
136
- },
137
- "peerDependencies": {
138
- "three": "^0.185.0"
139
- },
140
- "devDependencies": {
141
- "@clack/prompts": "^1.6.0",
142
- "@types/node": "^20.19.13",
143
- "@types/three": "^0.185.0",
144
- "happy-dom": "^18.0.1",
145
- "tsup": "^8.5.1",
146
- "typescript": "5.8.2",
147
- "vitest": "^4.0.9"
148
- },
149
- "publishConfig": {
150
- "access": "public",
151
- "registry": "https://registry.npmjs.org/"
152
- },
153
- "scripts": {
154
- "zylem": "node --experimental-strip-types ./scripts/zylem-runner.ts",
155
- "build": "tsup",
156
- "dev": "tsup --watch",
157
- "typecheck": "tsc --noEmit",
158
- "test": "vitest --reporter=verbose"
159
- }
160
- }
2
+ "name": "@zylem/behaviors",
3
+ "version": "0.5.8",
4
+ "type": "module",
5
+ "description": "Tree-shakable Zylem behavior descriptors, systems, coordinators, and runtime-agnostic behavior helpers.",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+ssh://git@github.com/zylem-game-lib/behaviors.git"
10
+ },
11
+ "engines": {
12
+ "node": ">=22.12.0",
13
+ "pnpm": ">=10.32.1"
14
+ },
15
+ "sideEffects": false,
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ },
24
+ "./core": {
25
+ "types": "./dist/core.d.ts",
26
+ "import": "./dist/core.js"
27
+ },
28
+ "./destructible-prebake": {
29
+ "types": "./dist/destructible-prebake.d.ts",
30
+ "import": "./dist/destructible-prebake.js"
31
+ },
32
+ "./cooldown": {
33
+ "types": "./dist/cooldown.d.ts",
34
+ "import": "./dist/cooldown.js"
35
+ },
36
+ "./destructible-3d": {
37
+ "types": "./dist/destructible-3d.d.ts",
38
+ "import": "./dist/destructible-3d.js"
39
+ },
40
+ "./first-person": {
41
+ "types": "./dist/first-person.d.ts",
42
+ "import": "./dist/first-person.js"
43
+ },
44
+ "./jumper-2d": {
45
+ "types": "./dist/jumper-2d.d.ts",
46
+ "import": "./dist/jumper-2d.js"
47
+ },
48
+ "./jumper-3d": {
49
+ "types": "./dist/jumper-3d.d.ts",
50
+ "import": "./dist/jumper-3d.js"
51
+ },
52
+ "./particle-emitter": {
53
+ "types": "./dist/particle-emitter.d.ts",
54
+ "import": "./dist/particle-emitter.js"
55
+ },
56
+ "./particle-emitter/presets": {
57
+ "types": "./dist/particle-emitter/presets.d.ts",
58
+ "import": "./dist/particle-emitter/presets.js"
59
+ },
60
+ "./platformer-2d": {
61
+ "types": "./dist/platformer-2d.d.ts",
62
+ "import": "./dist/platformer-2d.js"
63
+ },
64
+ "./platformer-3d": {
65
+ "types": "./dist/platformer-3d.d.ts",
66
+ "import": "./dist/platformer-3d.js"
67
+ },
68
+ "./ricochet-2d": {
69
+ "types": "./dist/ricochet-2d.d.ts",
70
+ "import": "./dist/ricochet-2d.js"
71
+ },
72
+ "./ricochet-3d": {
73
+ "types": "./dist/ricochet-3d.d.ts",
74
+ "import": "./dist/ricochet-3d.js"
75
+ },
76
+ "./runtime-2d": {
77
+ "types": "./dist/runtime-2d.d.ts",
78
+ "import": "./dist/runtime-2d.js"
79
+ },
80
+ "./screen-visibility": {
81
+ "types": "./dist/screen-visibility.d.ts",
82
+ "import": "./dist/screen-visibility.js"
83
+ },
84
+ "./screen-wrap": {
85
+ "types": "./dist/screen-wrap.d.ts",
86
+ "import": "./dist/screen-wrap.js"
87
+ },
88
+ "./shooter-2d": {
89
+ "types": "./dist/shooter-2d.d.ts",
90
+ "import": "./dist/shooter-2d.js"
91
+ },
92
+ "./thruster": {
93
+ "types": "./dist/thruster.d.ts",
94
+ "import": "./dist/thruster.js"
95
+ },
96
+ "./top-down-movement": {
97
+ "types": "./dist/top-down-movement.d.ts",
98
+ "import": "./dist/top-down-movement.js"
99
+ },
100
+ "./world-boundary-2d": {
101
+ "types": "./dist/world-boundary-2d.d.ts",
102
+ "import": "./dist/world-boundary-2d.js"
103
+ },
104
+ "./world-boundary-3d": {
105
+ "types": "./dist/world-boundary-3d.d.ts",
106
+ "import": "./dist/world-boundary-3d.js"
107
+ },
108
+ "./coordinators/boundary-ricochet": {
109
+ "types": "./dist/coordinators/boundary-ricochet.d.ts",
110
+ "import": "./dist/coordinators/boundary-ricochet.js"
111
+ },
112
+ "./coordinators/boundary-ricochet-3d": {
113
+ "types": "./dist/coordinators/boundary-ricochet-3d.d.ts",
114
+ "import": "./dist/coordinators/boundary-ricochet-3d.js"
115
+ },
116
+ "./coordinators/first-person-shooter": {
117
+ "types": "./dist/coordinators/first-person-shooter.d.ts",
118
+ "import": "./dist/coordinators/first-person-shooter.js"
119
+ },
120
+ "./coordinators/multidirectional-space-shooter": {
121
+ "types": "./dist/coordinators/multidirectional-space-shooter.d.ts",
122
+ "import": "./dist/coordinators/multidirectional-space-shooter.js"
123
+ },
124
+ "./coordinators/top-down-shooter": {
125
+ "types": "./dist/coordinators/top-down-shooter.d.ts",
126
+ "import": "./dist/coordinators/top-down-shooter.js"
127
+ },
128
+ "./package.json": "./package.json"
129
+ },
130
+ "scripts": {
131
+ "zylem": "node --experimental-strip-types ./scripts/zylem-runner.ts",
132
+ "build": "tsup",
133
+ "dev": "tsup --watch",
134
+ "typecheck": "tsc --noEmit",
135
+ "test": "vitest --reporter=verbose",
136
+ "prepublishOnly": "pnpm run build && vitest run"
137
+ },
138
+ "dependencies": {
139
+ "@dgreenheck/three-pinata": "^2.0.1",
140
+ "@zylem/runtime": "^0.2.5",
141
+ "comlink": "^4.4.2",
142
+ "typescript-fsm": "^1.6.0",
143
+ "valtio": "^2.1.7"
144
+ },
145
+ "peerDependencies": {
146
+ "three": "^0.185.0"
147
+ },
148
+ "devDependencies": {
149
+ "@clack/prompts": "^1.6.0",
150
+ "@types/node": "^20.19.13",
151
+ "@types/three": "^0.185.0",
152
+ "happy-dom": "^18.0.1",
153
+ "tsup": "^8.5.1",
154
+ "typescript": "5.8.2",
155
+ "vitest": "^4.0.9"
156
+ },
157
+ "packageManager": "pnpm@10.33.0",
158
+ "publishConfig": {
159
+ "access": "public",
160
+ "registry": "https://registry.npmjs.org/"
161
+ }
162
+ }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/lib/behaviors/first-person/first-person-fsm.ts","../src/lib/behaviors/first-person/first-person.system.ts","../src/lib/behaviors/first-person/first-person.descriptor.ts"],"sourcesContent":["/**\n * First Person Controller Finite State Machine\n *\n * Manages state transitions for first-person movement:\n * - Idle: Standing still\n * - Walking: Moving at walk speed\n * - Running: Moving at run/sprint speed\n */\n\nimport { SyncStateMachine, t } from '../../core/utility/sync-state-machine';\nimport type { FirstPersonInputComponent, FirstPersonStateComponent } from './components';\n\n/**\n * First-person movement states\n */\nexport enum FirstPersonState {\n\tIdle = 'idle',\n\tWalking = 'walking',\n\tRunning = 'running',\n}\n\n/**\n * Events that trigger state transitions\n */\nexport enum FirstPersonEvent {\n\tWalk = 'walk',\n\tRun = 'run',\n\tStop = 'stop',\n}\n\n/**\n * Context for the FSM\n */\nexport interface FirstPersonContext {\n\tinput: FirstPersonInputComponent;\n\tstate: FirstPersonStateComponent;\n}\n\n/**\n * First Person Controller FSM\n */\nexport class FirstPersonFSM {\n\tmachine: SyncStateMachine<FirstPersonState, FirstPersonEvent, never>;\n\n\tconstructor(private ctx: FirstPersonContext) {\n\t\tthis.machine = new SyncStateMachine<FirstPersonState, FirstPersonEvent, never>(\n\t\t\tFirstPersonState.Idle,\n\t\t\t[\n\t\t\t\t// Idle transitions\n\t\t\t\tt(FirstPersonState.Idle, FirstPersonEvent.Walk, FirstPersonState.Walking),\n\t\t\t\tt(FirstPersonState.Idle, FirstPersonEvent.Run, FirstPersonState.Running),\n\t\t\t\tt(FirstPersonState.Idle, FirstPersonEvent.Stop, FirstPersonState.Idle),\n\n\t\t\t\t// Walking transitions\n\t\t\t\tt(FirstPersonState.Walking, FirstPersonEvent.Run, FirstPersonState.Running),\n\t\t\t\tt(FirstPersonState.Walking, FirstPersonEvent.Stop, FirstPersonState.Idle),\n\t\t\t\tt(FirstPersonState.Walking, FirstPersonEvent.Walk, FirstPersonState.Walking),\n\n\t\t\t\t// Running transitions\n\t\t\t\tt(FirstPersonState.Running, FirstPersonEvent.Walk, FirstPersonState.Walking),\n\t\t\t\tt(FirstPersonState.Running, FirstPersonEvent.Stop, FirstPersonState.Idle),\n\t\t\t\tt(FirstPersonState.Running, FirstPersonEvent.Run, FirstPersonState.Running),\n\t\t\t],\n\t\t);\n\t}\n\n\t/** Get the current state */\n\tgetState(): FirstPersonState {\n\t\treturn this.machine.getState();\n\t}\n\n\t/** Dispatch an event to the FSM */\n\tdispatch(event: FirstPersonEvent): void {\n\t\tif (this.machine.can(event)) {\n\t\t\tthis.machine.syncDispatch(event);\n\t\t}\n\t}\n\n\t/** Get the current yaw from context */\n\tgetYaw(): number {\n\t\treturn this.ctx.state.yaw;\n\t}\n\n\t/** Get the current pitch from context */\n\tgetPitch(): number {\n\t\treturn this.ctx.state.pitch;\n\t}\n\n\t/**\n\t * Update FSM based on current input and state.\n\t */\n\tupdate(input: FirstPersonInputComponent, state: FirstPersonStateComponent): void {\n\t\tthis.ctx.input = input;\n\t\tthis.ctx.state = state;\n\n\t\tconst hasMovement = Math.abs(input.moveX) > 0.1 || Math.abs(input.moveZ) > 0.1;\n\n\t\tif (hasMovement) {\n\t\t\tif (input.sprint) {\n\t\t\t\tthis.dispatch(FirstPersonEvent.Run);\n\t\t\t} else {\n\t\t\t\tthis.dispatch(FirstPersonEvent.Walk);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.dispatch(FirstPersonEvent.Stop);\n\t\t}\n\t}\n}\n","/**\n * First Person Presentation System\n *\n * Presentation-only companion to the wasm `first_person` Stage behavior:\n * - Syncs the FirstPersonPerspective eye position from the runtime pose\n * - Applies the runtime's yaw/pitch (from the behavior snapshot) to the\n * perspective so the camera matches the simulated look state\n * - Positions an optional viewmodel entity relative to the camera\n *\n * All movement/look simulation happens in the wasm runtime; this system only\n * reads poses and snapshots.\n */\n\nimport { Vector3, Quaternion, Euler } from 'three';\nimport type { FirstPersonPerspective } from '../../camera/perspectives/first-person-perspective';\nimport type {\n\tBehaviorRuntime,\n\tStagePose,\n\tStageBehaviorSnapshot,\n} from '../../runtime/wasm-stage-runtime';\nimport type {\n\tFirstPersonMovementComponent,\n\tFirstPersonInputComponent,\n\tFirstPersonStateComponent,\n} from './components';\nimport type { TransformState } from '../../capabilities/transform-store';\n\n/**\n * Entity interface for the first-person controller.\n * Entities with this behavior have these components attached.\n */\nexport interface FirstPersonEntity {\n\tuuid: string;\n\ttransformStore: TransformState;\n\tfirstPerson: FirstPersonMovementComponent;\n\t$fps: FirstPersonInputComponent;\n\tfirstPersonState: FirstPersonStateComponent;\n}\n\n/**\n * Viewmodel configuration for an entity that follows the camera.\n */\nexport interface ViewmodelConfig {\n\t/** The entity whose visual group should be positioned relative to the camera. */\n\tentity: any;\n\t/** Offset from the eye position in camera-local space (right, down, forward). */\n\toffset: Vector3;\n\t/** Additional rotation applied to the viewmodel in local space (Euler angles in radians). */\n\trotation?: Euler;\n}\n\n// Reusable temporaries (avoid per-frame allocations)\nconst _viewmodelQuat = new Quaternion();\nconst _viewmodelLocalQuat = new Quaternion();\nconst _viewmodelEuler = new Euler();\nconst _offset = new Vector3();\nconst _pose: StagePose = { position: [0, 0, 0], rotation: [0, 0, 0, 1] };\nconst _snapshot = {} as StageBehaviorSnapshot;\n\n/**\n * Presentation system driven by runtime pose + snapshot reads.\n */\nexport class FirstPersonPresentation {\n\tprivate perspectives = new Map<string, FirstPersonPerspective>();\n\tprivate viewmodels = new Map<string, ViewmodelConfig>();\n\n\t/**\n\t * Associate a FirstPersonPerspective with an entity (by uuid).\n\t * Called once by the descriptor system when the behavior is initialized.\n\t */\n\tsetPerspective(entityUuid: string, perspective: FirstPersonPerspective): void {\n\t\tthis.perspectives.set(entityUuid, perspective);\n\t}\n\n\thasPerspective(entityUuid: string): boolean {\n\t\treturn this.perspectives.has(entityUuid);\n\t}\n\n\t/**\n\t * Attach a viewmodel (weapon/item) to an entity.\n\t * The viewmodel entity's group will be positioned relative to the camera each frame.\n\t */\n\tsetViewmodel(entityUuid: string, config: ViewmodelConfig): void {\n\t\tthis.viewmodels.set(entityUuid, config);\n\t}\n\n\thasViewmodel(entityUuid: string): boolean {\n\t\treturn this.viewmodels.has(entityUuid);\n\t}\n\n\t/**\n\t * Sync camera + viewmodel for one entity from the runtime state.\n\t */\n\tsyncEntity(\n\t\tentityUuid: string,\n\t\truntime: BehaviorRuntime,\n\t\thandle: number,\n\t\tmovement: FirstPersonMovementComponent,\n\t\tstate: FirstPersonStateComponent | undefined,\n\t): void {\n\t\tconst perspective = this.perspectives.get(entityUuid);\n\t\tconst pose = runtime.getPose(handle, _pose);\n\t\tconst snapshot = runtime.readSnapshot(handle, _snapshot);\n\n\t\tif (perspective && snapshot) {\n\t\t\tperspective.setLook?.(snapshot.yaw, snapshot.pitch);\n\t\t\tif (state) {\n\t\t\t\tstate.yaw = snapshot.yaw;\n\t\t\t\tstate.pitch = snapshot.pitch;\n\t\t\t\tstate.currentSpeed = snapshot.speed;\n\t\t\t}\n\t\t}\n\n\t\tif (perspective?.initialPosition && pose) {\n\t\t\tperspective.initialPosition.set(\n\t\t\t\tpose.position[0],\n\t\t\t\tpose.position[1] + movement.eyeHeight,\n\t\t\t\tpose.position[2],\n\t\t\t);\n\t\t}\n\n\t\tconst vm = this.viewmodels.get(entityUuid);\n\t\tif (vm && perspective?.initialPosition && state) {\n\t\t\tthis.positionViewmodel(vm, perspective, state, runtime);\n\t\t}\n\t}\n\n\t/**\n\t * Position a viewmodel entity relative to the camera's eye position and rotation.\n\t */\n\tprivate positionViewmodel(\n\t\tvm: ViewmodelConfig,\n\t\tperspective: FirstPersonPerspective,\n\t\tstate: FirstPersonStateComponent,\n\t\truntime: BehaviorRuntime,\n\t): void {\n\t\tconst eyePos = perspective.initialPosition;\n\t\tif (!eyePos) return;\n\n\t\t// Build camera rotation quaternion\n\t\t_viewmodelQuat.setFromEuler(\n\t\t\t_viewmodelEuler.set(state.pitch, state.yaw, 0, 'YXZ'),\n\t\t);\n\n\t\t// Compose with the viewmodel's local rotation (model orientation fix)\n\t\tif (vm.rotation) {\n\t\t\t_viewmodelLocalQuat.setFromEuler(vm.rotation);\n\t\t\t_viewmodelQuat.multiply(_viewmodelLocalQuat);\n\t\t}\n\n\t\t// Transform the local offset into world space\n\t\t_offset.copy(vm.offset).applyQuaternion(_viewmodelQuat);\n\n\t\tconst wx = eyePos.x + _offset.x;\n\t\tconst wy = eyePos.y + _offset.y;\n\t\tconst wz = eyePos.z + _offset.z;\n\n\t\tconst entity = vm.entity;\n\n\t\t// Move the runtime body (if registered) so physics stays in sync\n\t\tconst handle = (entity?.runtimeHandle ?? -1) as number;\n\t\tif (handle >= 0) {\n\t\t\truntime.setPosition(handle, wx, wy, wz);\n\t\t\truntime.setRotation(\n\t\t\t\thandle,\n\t\t\t\t_viewmodelQuat.x, _viewmodelQuat.y, _viewmodelQuat.z, _viewmodelQuat.w,\n\t\t\t);\n\t\t}\n\n\t\t// Also set the group directly for immediate visual update\n\t\tif (entity?.group) {\n\t\t\tentity.group.position.set(wx, wy, wz);\n\t\t\tentity.group.quaternion.copy(_viewmodelQuat);\n\t\t}\n\t}\n\n\t/** Cleanup */\n\tdestroy(): void {\n\t\tthis.perspectives.clear();\n\t\tthis.viewmodels.clear();\n\t}\n}\n","/**\n * First Person Controller Behavior Descriptor\n *\n * Type-safe descriptor for the first-person controller behavior system.\n * Provides entity.use() API for FPS movement and camera control.\n */\n\nimport { Euler } from 'three';\nimport { defineBehavior } from '../behavior-descriptor';\nimport type { BehaviorEntityLink, BehaviorSystem } from '../behavior-system';\nimport type { FirstPersonPerspective } from '../../camera/perspectives/first-person-perspective';\nimport { Vec3Input, VEC3_ZERO, toThreeVector3 } from '../../core/vector';\nimport {\n\tFirstPersonPresentation,\n\ttype FirstPersonEntity,\n\ttype ViewmodelConfig,\n} from './first-person.system';\nimport { FirstPersonState } from './first-person-fsm';\nimport {\n\tcreateFirstPersonMovementComponent,\n\tcreateFirstPersonInputComponent,\n\tcreateFirstPersonStateComponent,\n} from './components';\nimport {\n\tSTAGE_INPUT_BUTTON_RUN,\n\treadBehaviorSnapshot,\n\ttype BehaviorRuntime,\n} from '../../runtime/wasm-stage-runtime';\n\n/**\n * First-person controller behavior options (typed for entity.use() autocomplete).\n */\nexport interface FirstPersonBehaviorOptions {\n\t/** Base walking speed (default: 8) */\n\twalkSpeed?: number;\n\t/** Sprint/run speed (default: 16) */\n\trunSpeed?: number;\n\t/** Look sensitivity multiplier (default: 2) */\n\tlookSensitivity?: number;\n\t/** Eye height offset above entity position (default: 1.7) */\n\teyeHeight?: number;\n\t/** The FirstPersonPerspective to drive (from camera.getPerspective()) */\n\tperspective?: FirstPersonPerspective;\n\t/** Optional viewmodel (weapon/item) to position relative to the camera */\n\tviewmodel?: { entity: any; offset: Vec3Input; rotation?: Euler };\n}\n\nconst defaultOptions: FirstPersonBehaviorOptions = {\n\twalkSpeed: 8,\n\trunSpeed: 16,\n\tlookSensitivity: 2,\n\teyeHeight: 1.7,\n};\n\nconst FIRST_PERSON_BEHAVIOR_KEY = Symbol.for(\n\t'zylem:behavior:first-person-controller',\n);\n\n/**\n * Forwards look/move input into the wasm runtime and syncs the camera\n * perspective + optional viewmodel from the runtime's pose/snapshot\n * (presentation only — the Rust `first_person` behavior owns simulation).\n */\nclass FirstPersonBehaviorSystem implements BehaviorSystem {\n\tprivate presentation = new FirstPersonPresentation();\n\tprivate attachedRuntimeSlots = new Set<number>();\n\n\tconstructor(\n\t\tprivate wasmStage: BehaviorRuntime | null,\n\t\tprivate getBehaviorLinks?: (key: symbol) => Iterable<BehaviorEntityLink>,\n\t) {}\n\n\tupdate(_ecs: unknown, _delta: number): void {\n\t\tconst links = this.getBehaviorLinks?.(FIRST_PERSON_BEHAVIOR_KEY);\n\t\tif (!links) return;\n\n\t\tfor (const link of links) {\n\t\t\tconst gameEntity = link.entity as any;\n\t\t\tconst fpsRef = link.ref as any;\n\n\t\t\tconst options = fpsRef.options as FirstPersonBehaviorOptions;\n\n\t\t\t// Initialize components (once)\n\t\t\tif (!gameEntity.firstPerson) {\n\t\t\t\tgameEntity.firstPerson = createFirstPersonMovementComponent(options);\n\t\t\t}\n\t\t\tif (!gameEntity.$fps) {\n\t\t\t\tgameEntity.$fps = createFirstPersonInputComponent();\n\t\t\t}\n\t\t\tif (!gameEntity.firstPersonState) {\n\t\t\t\tgameEntity.firstPersonState = createFirstPersonStateComponent();\n\t\t\t}\n\n\t\t\t// Wire up perspective (once)\n\t\t\tif (options.perspective && !this.presentation.hasPerspective(gameEntity.uuid)) {\n\t\t\t\tthis.presentation.setPerspective(gameEntity.uuid, options.perspective);\n\t\t\t}\n\n\t\t\t// Wire up viewmodel (once)\n\t\t\tif (options.viewmodel && !this.presentation.hasViewmodel(gameEntity.uuid)) {\n\t\t\t\tthis.presentation.setViewmodel(gameEntity.uuid, {\n\t\t\t\t\t...options.viewmodel,\n\t\t\t\t\toffset: toThreeVector3(options.viewmodel.offset, VEC3_ZERO),\n\t\t\t\t} as ViewmodelConfig);\n\t\t\t}\n\n\t\t\tconst handle = (gameEntity.runtimeHandle ?? -1) as number;\n\t\t\tif (!this.wasmStage || handle < 0) continue;\n\n\t\t\tthis.ensureWasmAttached(handle, options);\n\t\t\tfpsRef.__wasmStage = this.wasmStage;\n\t\t\tfpsRef.__entity = gameEntity;\n\t\t\tconst input = gameEntity.$fps;\n\t\t\tif (input) {\n\t\t\t\tthis.wasmStage.writeInputAxes(handle, input.moveX ?? 0, input.moveZ ?? 0);\n\t\t\t\tthis.wasmStage.writeInputLook(handle, input.lookX ?? 0, input.lookY ?? 0);\n\t\t\t\tthis.wasmStage.writeInputButton(handle, STAGE_INPUT_BUTTON_RUN, !!input.sprint);\n\t\t\t}\n\n\t\t\tthis.presentation.syncEntity(\n\t\t\t\tgameEntity.uuid,\n\t\t\t\tthis.wasmStage,\n\t\t\t\thandle,\n\t\t\t\tgameEntity.firstPerson,\n\t\t\t\tgameEntity.firstPersonState,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate ensureWasmAttached(handle: number, options: FirstPersonBehaviorOptions): void {\n\t\tif (!this.wasmStage || this.attachedRuntimeSlots.has(handle)) return;\n\t\tthis.wasmStage.attachFirstPerson(handle, {\n\t\t\twalkSpeed: options.walkSpeed ?? defaultOptions.walkSpeed!,\n\t\t\trunSpeed: options.runSpeed ?? defaultOptions.runSpeed!,\n\t\t\teyeHeight: options.eyeHeight ?? defaultOptions.eyeHeight!,\n\t\t\tlookSensitivity: options.lookSensitivity ?? defaultOptions.lookSensitivity!,\n\t\t});\n\t\tthis.attachedRuntimeSlots.add(handle);\n\t}\n\n\tdestroy(_ecs: unknown): void {\n\t\tthis.presentation.destroy();\n\t\tthis.attachedRuntimeSlots.clear();\n\t}\n}\n\n/**\n * FirstPersonBehavior - typed descriptor for first-person movement and camera control.\n *\n * Provides complete first-person controller including:\n * - WASD movement relative to camera yaw\n * - Mouse look driving a FirstPersonPerspective\n * - Optional viewmodel positioning (weapon/item following camera)\n * - Walk/run state tracking via FSM\n *\n * @example\n * ```typescript\n * import { FirstPersonBehavior } from \"@zylem/game-lib/behavior\";\n * import { createCamera, Perspectives, FirstPersonPerspective } from \"@zylem/game-lib/core\";\n * import { createActor } from \"@zylem/game-lib/entity\";\n *\n * const fpsCamera = createCamera({ perspective: Perspectives.FirstPerson, ... });\n * const fps = fpsCamera.getPerspective<FirstPersonPerspective>();\n *\n * const player = createActor({ name: 'player', ... });\n * const controller = player.use(FirstPersonBehavior, {\n * perspective: fps,\n * walkSpeed: 8,\n * runSpeed: 16,\n * lookSensitivity: 2,\n * });\n *\n * // In update loop - write input, behavior handles the rest\n * player.onUpdate(({ inputs }) => {\n * player.$fps.moveX = inputs.p1.axes.Horizontal.value;\n * player.$fps.moveZ = inputs.p1.axes.Vertical.value;\n * player.$fps.lookX = inputs.p1.axes.SecondaryHorizontal.value;\n * player.$fps.lookY = inputs.p1.axes.SecondaryVertical.value;\n * player.$fps.sprint = inputs.p1.shoulders.LTrigger.held > 0;\n * });\n * ```\n */\nexport const FirstPersonBehavior = defineBehavior<\n\tFirstPersonBehaviorOptions,\n\t{\n\t\tgetState: () => FirstPersonState;\n\t\tgetYaw: () => number;\n\t\tgetPitch: () => number;\n\t\tattachViewmodel: (entity: any, offset: Vec3Input) => void;\n\t},\n\tFirstPersonEntity\n>({\n\tname: 'first-person-controller',\n\tdefaultOptions,\n\tsystemFactory: (ctx) =>\n\t\tnew FirstPersonBehaviorSystem(ctx.wasmStage ?? null, ctx.getBehaviorLinks),\n\t// Handles read the runtime snapshot buffer (previous-step values). The\n\t// Rust behavior is the single source of truth.\n\tcreateHandle: (ref) => ({\n\t\tgetState: () => {\n\t\t\tconst snapshot = readBehaviorSnapshot(ref);\n\t\t\tif (!snapshot) return FirstPersonState.Idle;\n\t\t\tif (snapshot.speed <= 0.01) return FirstPersonState.Idle;\n\t\t\tconst walkSpeed = ref.options.walkSpeed ?? defaultOptions.walkSpeed!;\n\t\t\treturn snapshot.speed > walkSpeed\n\t\t\t\t? FirstPersonState.Running\n\t\t\t\t: FirstPersonState.Walking;\n\t\t},\n\t\tgetYaw: () => readBehaviorSnapshot(ref)?.yaw ?? 0,\n\t\tgetPitch: () => readBehaviorSnapshot(ref)?.pitch ?? 0,\n\t\tattachViewmodel: (entity: any, offset: Vec3Input) => {\n\t\t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