@naanouff/w3dts-character 0.0.0-stage → 0.0.21

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/README.md CHANGED
@@ -1,3 +1,54 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @naanouff/w3dts-character
2
+
3
+ First-person style **character motor** for W3DTS: ECS `CharacterMotorComponent` + `createCharacterMotorSystem` integrate horizontal motion against static `ColliderComponent` geometry using `@naanouff/w3dts-physics` queries (`raycast`, `overlapSphere` ×2 for a vertical **capsule**). Optional `IHeightService` keeps terrain-style ground when no physics hit is found.
4
+
5
+ ## Limitations (read before shipping gameplay)
6
+
7
+ - **No continuous sweep**: Wall motion uses capsule overlap (two spheres) plus **axis-separated sliding** (Quake-style). Corners and fast movement can clip slightly; tune `radius`, `capsuleHeight`, `skinWidth`, and frame rate.
8
+ - **Capsule = two sphere queries**: Not a single analytic capsule-vs-OBB test nor a robust Minkowski sweep; step-up uses downward rays and overlap tests.
9
+ - **No character scale handling**: Assumes unit-scale transforms on obstacles (same as current `PhysicsQueries` OBB tests).
10
+ - **Volumes**: Add `CharacterVolumeStateComponent` (this package) and `createGameplayVolumesSystem` from `@naanouff/w3dts-gameplay-volumes` **before** the motor system. See **Locomotion modes** below.
11
+ - **Self-query**: `PhysicsQueries` do not ignore the motor entity. Do **not** attach a `ColliderComponent` to the same entity as the motor (or to a child used as the motor transform) unless you add a dedicated collision layer the motor mask excludes.
12
+
13
+ ## Quick start
14
+
15
+ 1. **Dependencies** (workspace):
16
+
17
+ ```bash
18
+ pnpm add @naanouff/w3dts-character @naanouff/w3dts-physics @naanouff/w3dts-gameplay-volumes --filter your-app
19
+ ```
20
+
21
+ 2. **Scene**: Add static blocking geometry with `TransformComponent` + `ColliderComponent` (`AABB` or `SPHERE`).
22
+
23
+ 3. **Motor entity**: On the controlled entity, add `TransformComponent` + `CharacterMotorComponent`. For volume tagging, also add `CharacterVolumeStateComponent` (exported from this package). Treat **`TransformComponent.position` as the feet anchor** on the floor (capsule from feet to feet + `capsuleHeight`, radius `radius`; if `capsuleHeight <= 2*radius`, this is a single sphere at feet + `(0, radius, 0)`). This differs from the legacy `FPSCameraController` path that snaps the camera transform using `playerHeight`; align your camera (child entity offset or separate eye height) when mixing motor + FPS.
24
+
25
+ 4. **Scheduler** (same frame as core `Engine`):
26
+
27
+ - `inputManager.resolveFromHardware()` runs first.
28
+ - `cameraControllerManager.update(dt)` runs next — here, for FPS, set `CharacterMotorComponent.wishVelocity` (and camera rotation) from `@naanouff/w3dts-input` semantics (`readLocomotionAxes`, or `computeWishVelocityXZFromLocomotion` with your yaw).
29
+ - `scheduler.run(...)` runs **after** — register `createGameplayVolumesSystem(logger)` **before** `createCharacterMotorSystem(logger)` when using water/ladder volumes.
30
+
31
+ 5. **Optional terrain**: Assign `motor.heightService` (e.g. `TerrainHeightService`) for ground height when downward raycasts miss triangle colliders.
32
+
33
+ ## Input contract
34
+
35
+ Locomotion uses semantic ids from `@naanouff/w3dts-input` (`LOCOMOTION_AXIS_FORWARD`, `LOCOMOTION_AXIS_STRAFE`, `LOCOMOTION_ACTION_SPRINT`) matching `defaultInputAsset`. Core `InputManager` already resolves them; use `readLocomotionAxes` or `computeWishVelocityXZFromLocomotion` so overrides merged via `mergeInputOverrides` stay consistent.
36
+
37
+ ## Locomotion modes (`CharacterVolumeStateComponent.locomotionMode`)
38
+
39
+ | Mode | Wish velocity axes | Speeds |
40
+ |------|-------------------|--------|
41
+ | **ground** (default) | Horizontal **XZ** from yaw-flattened forward/right; **Y ignored** for horizontal resolution; feet snapped down with raycasts. | `walkSpeed` / `sprintSpeed` passed into `computeWishVelocityXZFromLocomotion`. |
42
+ | **swim** | Same **XZ** basis as ground; **Y** is vertical swim (sink/rise), clamped by `swimVerticalMaxSpeed` or volume override. Horizontal blocking still uses physics overlap. Buoyancy: `CharacterVolumeStateComponent.buoyancyAcceleration` (m/s²). | Use `motor.swimSpeedWalk` / `motor.swimSpeedSprint` as the `walkSpeed` / `sprintSpeed` arguments to `computeWishVelocityXZFromLocomotion` when underwater, unless the water volume sets positive overrides on state. |
43
+ | **ladder** | Movement is **only** the projection `dot(wishVelocity, ladderWorldUp) * ladderWorldUp` per frame. Align `wishVelocity` so that dot matches desired climb speed (e.g. `wishVelocity = scale(ladderWorldUp, forwardAxis * climbSpeed)` using the same forward sign as gameplay). | Volume state exposes `ladderClimbSpeed` / `ladderSlideSpeed` for gameplay to scale input; the motor does not read axes directly. |
44
+
45
+ ## Integrating with core `FPSCameraController` (optional)
46
+
47
+ `@naanouff/w3dts-core` does **not** depend on this package. To delegate FPS motion to the motor without pulling character into core:
48
+
49
+ - From an app package that already depends on **core + character + input**, in the FPS / gameplay layer: when the camera entity has `CharacterMotorComponent`, write `wishVelocity` each frame (via `computeWishVelocityXZFromLocomotion` + yaw) and **skip** horizontal movement / height snap inside a forked controller, or wrap the stock controller behind a small adapter.
50
+ - `OrbitalCameraController` is unaffected.
51
+
52
+ ## Public API
53
+
54
+ See `src/index.ts` exports: `CharacterMotorComponent`, `CharacterVolumeStateComponent`, `resolveCharacterMotorStep`, `createCharacterMotorSystem`, `computeWishVelocityXZFromLocomotion`.
@@ -0,0 +1,106 @@
1
+ import { vec3 } from 'gl-matrix';
2
+ import { Entity, IComponent, IHeightService } from '@naanouff/w3dts-core';
3
+ /**
4
+ * ECS component that configures a first-person style motor using a **vertical capsule**
5
+ * (two sphere queries along Y) against static physics colliders (`ColliderComponent` /
6
+ * `PhysicsQueries`) in the world.
7
+ *
8
+ * The entity's {@link TransformComponent}.position is treated as the **feet** anchor
9
+ * (bottom of the capsule). Capsule spans Y ∈ [feet.y, feet.y + capsuleHeight] with
10
+ * hemispherical caps of {@link radius}. If `capsuleHeight <= 2 * radius`, behavior matches
11
+ * a single sphere at feet + (0, radius, 0).
12
+ */
13
+ export declare class CharacterMotorComponent implements IComponent {
14
+ static readonly componentName = "CharacterMotorComponent";
15
+ /**
16
+ * When false, the motor does not change the transform (wish velocity is still readable).
17
+ */
18
+ enabled: boolean;
19
+ /**
20
+ * Capsule radius (world units): hemispheres at bottom and top, and cylinder between them.
21
+ */
22
+ radius: number;
23
+ /**
24
+ * Total capsule height from feet (bottom of lower hemisphere) to top of upper hemisphere.
25
+ * Clamped to at least `2 * radius` internally. Typical humanoid FPS: ~1.7–1.9.
26
+ */
27
+ capsuleHeight: number;
28
+ /**
29
+ * Maximum ledge height the motor attempts to step onto (world units).
30
+ * Step-up is allowed only when the measured rise `groundY - feetY` is at most this (plus slop).
31
+ */
32
+ stepHeight: number;
33
+ /**
34
+ * Maximum walkable inclination from the horizontal plane, in **radians**.
35
+ * A ray hit is a valid floor if `hit.normal[1] >= cos(maxSlopeRadians)` (up-facing surfaces only).
36
+ * Steeper slopes are skipped for step-up, ground snap, and grounded probe (like a wall for locomotion).
37
+ */
38
+ maxSlopeRadians: number;
39
+ /**
40
+ * Shrinks the query sphere slightly so sliding does not jitter on contact.
41
+ */
42
+ skinWidth: number;
43
+ /**
44
+ * Desired world velocity in m/s. The Y component is ignored for horizontal resolution.
45
+ */
46
+ readonly wishVelocity: vec3;
47
+ /**
48
+ * When true, horizontal displacement this frame is {@link rootMotionDelta}
49
+ * and {@link wishVelocity} is not added on XZ.
50
+ */
51
+ rootMotionConsumed: boolean;
52
+ /** World displacement in meters for the frame when root motion is consumed. */
53
+ readonly rootMotionDelta: vec3;
54
+ /**
55
+ * Optional terrain-style ground when no downward physics ray hits.
56
+ */
57
+ heightService: IHeightService | null;
58
+ /**
59
+ * Bitmask passed to overlap queries / {@link raycast} as `collisionMask`.
60
+ */
61
+ collisionQueryMask: number;
62
+ /**
63
+ * Colliders on these entities are skipped in motor overlap and ground raycasts.
64
+ * Use for kinematic “helper” volumes when vertical support comes from {@link heightService}
65
+ * (e.g. analytic ramp) but you still want hitscan / other systems to hit the solid mesh.
66
+ */
67
+ collisionQueryIgnoreEntities: ReadonlySet<Entity> | null;
68
+ /**
69
+ * Updated each integration step when a supporting surface is found within a short probe.
70
+ */
71
+ isGrounded: boolean;
72
+ /**
73
+ * Vertical offset above current feet used as the start of the primary downward ground ray.
74
+ */
75
+ groundProbeAboveFeet: number;
76
+ /**
77
+ * Maximum length of the primary downward ray for snapping feet to the floor.
78
+ */
79
+ maxGroundRay: number;
80
+ /** When true for a frame, applies {@link jumpImpulseSpeed} if grounded at integration start. */
81
+ jumpRequested: boolean;
82
+ /** Initial upward speed (m/s) when a jump starts. */
83
+ jumpImpulseSpeed: number;
84
+ /** Gravity magnitude (m/s²) while airborne in ground mode. */
85
+ gravity: number;
86
+ /** Vertical speed (m/s) while airborne; reset when grounded without a jump. */
87
+ verticalVelocity: number;
88
+ /**
89
+ * True from a successful jump until landing. Avoids snapping feet to ground when vertical
90
+ * speed briefly crosses zero at the jump apex while the short grounded probe misses the floor.
91
+ */
92
+ jumpArcActive: boolean;
93
+ /**
94
+ * Maximum upward Y snap allowed from {@link heightService} relative to current feet.y.
95
+ * Prevents teleporting on top of a floor/bridge the player is walking under.
96
+ * Must be large enough for step-ups and ramp following per frame.
97
+ */
98
+ maxHeightServiceSnapUp: number;
99
+ /** Horizontal swim speed when sprint is off (m/s). Wish is still built in XZ world from yaw. */
100
+ swimSpeedWalk: number;
101
+ /** Horizontal swim speed when sprint is on (m/s). */
102
+ swimSpeedSprint: number;
103
+ /** Clamp for vertical swim velocity from `wishVelocity[1]` (m/s). */
104
+ swimVerticalMaxSpeed: number;
105
+ }
106
+ //# sourceMappingURL=CharacterMotorComponent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CharacterMotorComponent.d.ts","sourceRoot":"","sources":["../src/CharacterMotorComponent.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE/E;;;;;;;;;GASG;AACH,qBAAa,uBAAwB,YAAW,UAAU;IACxD,gBAAuB,aAAa,6BAA6B;IAEjE;;OAEG;IACI,OAAO,UAAQ;IAEtB;;OAEG;IACI,MAAM,SAAQ;IAErB;;;OAGG;IACI,aAAa,SAAO;IAE3B;;;OAGG;IACI,UAAU,SAAQ;IAEzB;;;;OAIG;IACI,eAAe,SAAwB;IAE9C;;OAEG;IACI,SAAS,SAAQ;IAExB;;OAEG;IACH,SAAgB,YAAY,OAAiB;IAE7C;;;OAGG;IACI,kBAAkB,UAAS;IAElC,+EAA+E;IAC/E,SAAgB,eAAe,OAAiB;IAEhD;;OAEG;IACI,aAAa,EAAE,cAAc,GAAG,IAAI,CAAQ;IAEnD;;OAEG;IACI,kBAAkB,SAAc;IAEvC;;;;OAIG;IACI,4BAA4B,EAAE,WAAW,CAAC,MAAM,CAAC,GAAG,IAAI,CAAQ;IAEvE;;OAEG;IACI,UAAU,UAAS;IAE1B;;OAEG;IACI,oBAAoB,SAAK;IAEhC;;OAEG;IACI,YAAY,SAAO;IAI1B,gGAAgG;IACzF,aAAa,UAAS;IAE7B,qDAAqD;IAC9C,gBAAgB,SAAK;IAE5B,8DAA8D;IACvD,OAAO,SAAM;IAEpB,+EAA+E;IACxE,gBAAgB,SAAK;IAE5B;;;OAGG;IACI,aAAa,UAAS;IAE7B;;;;OAIG;IACI,sBAAsB,SAAO;IAIpC,gGAAgG;IACzF,aAAa,SAAQ;IAE5B,qDAAqD;IAC9C,eAAe,SAAO;IAE7B,qEAAqE;IAC9D,oBAAoB,SAAO;CACnC"}
@@ -0,0 +1,28 @@
1
+ import { vec3 } from 'gl-matrix';
2
+ import { Entity, IComponent } from '@naanouff/w3dts-core';
3
+ export type VolumeLocomotionMode = 'ground' | 'swim' | 'ladder';
4
+ /**
5
+ * Per-frame volume contact summary for entities using {@link CharacterMotorComponent}.
6
+ * Updated by `createGameplayVolumesSystem` in `@naanouff/w3dts-gameplay-volumes` before the motor system.
7
+ */
8
+ export declare class CharacterVolumeStateComponent implements IComponent {
9
+ static readonly componentName = "CharacterVolumeStateComponent";
10
+ /** Use motor swim speed fields when volume does not override. */
11
+ static readonly SWIM_SPEED_USE_MOTOR = -1;
12
+ locomotionMode: VolumeLocomotionMode;
13
+ activeWaterEntity: Entity | null;
14
+ activeLadderEntity: Entity | null;
15
+ enteredWater: boolean;
16
+ exitedWater: boolean;
17
+ enteredLadder: boolean;
18
+ exitedLadder: boolean;
19
+ readonly ladderWorldUp: vec3;
20
+ effectiveSwimSpeedH: number;
21
+ effectiveSwimSpeedV: number;
22
+ buoyancyAcceleration: number;
23
+ ladderClimbSpeed: number;
24
+ ladderSlideSpeed: number;
25
+ _prevInWater: boolean;
26
+ _prevOnLadder: boolean;
27
+ }
28
+ //# sourceMappingURL=CharacterVolumeStateComponent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CharacterVolumeStateComponent.d.ts","sourceRoot":"","sources":["../src/CharacterVolumeStateComponent.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAE/D,MAAM,MAAM,oBAAoB,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEhE;;;GAGG;AACH,qBAAa,6BAA8B,YAAW,UAAU;IAC9D,gBAAuB,aAAa,mCAAmC;IAEvE,iEAAiE;IACjE,gBAAuB,oBAAoB,MAAM;IAE1C,cAAc,EAAE,oBAAoB,CAAY;IAEhD,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAQ;IACxC,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAAQ;IAEzC,YAAY,UAAS;IACrB,WAAW,UAAS;IACpB,aAAa,UAAS;IACtB,YAAY,UAAS;IAE5B,SAAgB,aAAa,OAA4B;IAElD,mBAAmB,SAAsD;IACzE,mBAAmB,SAAsD;IAEzE,oBAAoB,SAAK;IAEzB,gBAAgB,SAAO;IACvB,gBAAgB,SAAO;IAEvB,YAAY,UAAS;IACrB,aAAa,UAAS;CAC9B"}
@@ -0,0 +1,11 @@
1
+ import { System } from '@naanouff/w3dts-core';
2
+ import { ILogger } from '@naanouff/w3dts-logger';
3
+ /**
4
+ * Creates an ECS system that integrates all entities with both
5
+ * {@link TransformComponent} and {@link CharacterMotorComponent}.
6
+ *
7
+ * Register this on the scheduler **after** gameplay code (e.g. `FPSCameraController.update`)
8
+ * has written {@link CharacterMotorComponent.wishVelocity} for the current frame.
9
+ */
10
+ export declare function createCharacterMotorSystem(logger: ILogger): System;
11
+ //# sourceMappingURL=character-motor.system.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"character-motor.system.d.ts","sourceRoot":"","sources":["../src/character-motor.system.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAGnD,OAAO,EAAc,KAAK,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAUlE;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,CA+DlE"}
@@ -0,0 +1,7 @@
1
+ export { CharacterMotorComponent } from './CharacterMotorComponent';
2
+ export { CharacterVolumeStateComponent } from './CharacterVolumeStateComponent';
3
+ export type { VolumeLocomotionMode } from './CharacterVolumeStateComponent';
4
+ export { planarDeltaForMotor, resolveCharacterMotorStep } from './resolveCharacterMotor';
5
+ export { computeWishVelocityXZFromLocomotion } from './locomotionWish';
6
+ export { createCharacterMotorSystem } from './character-motor.system';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAChF,YAAY,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AACzF,OAAO,EAAE,mCAAmC,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { vec3 } from 'gl-matrix';
2
+ import { ResolveOutput } from '@naanouff/w3dts-input';
3
+ /**
4
+ * Builds horizontal wish velocity (m/s) from semantic gameplay axes and yaw-only facing,
5
+ * using the same basis as core `FPSCameraController` (flattened forward on XZ).
6
+ *
7
+ * For **swim** mode, pass `motor.swimSpeedWalk` / `motor.swimSpeedSprint` as `walkSpeed` /
8
+ * `sprintSpeed`. Set `wishVelocity[1]` separately for vertical swim when
9
+ * `CharacterVolumeStateComponent.locomotionMode === 'swim'`.
10
+ *
11
+ * @param resolved Output maps from `InputActionRuntime.resolve` (e.g. via core `InputManager`).
12
+ * @param yawRadians Camera yaw around world Y (radians).
13
+ * @param walkSpeed Base speed when sprint is not active.
14
+ * @param sprintSpeed Speed when sprint is active.
15
+ * @param out Written XZ velocity; Y set to 0.
16
+ */
17
+ export declare function computeWishVelocityXZFromLocomotion(resolved: ResolveOutput, yawRadians: number, walkSpeed: number, sprintSpeed: number, out: vec3): void;
18
+ //# sourceMappingURL=locomotionWish.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"locomotionWish.d.ts","sourceRoot":"","sources":["../src/locomotionWish.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAQ,MAAM,WAAW,CAAC;AACvC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAG3D;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mCAAmC,CACjD,QAAQ,EAAE,aAAa,EACvB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,MAAM,EACnB,GAAG,EAAE,IAAI,GACR,IAAI,CAyBN"}
@@ -0,0 +1,29 @@
1
+ import { Entity, World } from '@naanouff/w3dts-core';
2
+ import { vec3 } from 'gl-matrix';
3
+ import { CharacterMotorComponent } from './CharacterMotorComponent';
4
+ /**
5
+ * Horizontal step. Root motion replaces wish velocity so the two do not stack.
6
+ * The delta is consumed (zeroed) so the next frame does not repeat it.
7
+ */
8
+ export declare function planarDeltaForMotor(motor: CharacterMotorComponent, deltaTime: number): {
9
+ dx: number;
10
+ dz: number;
11
+ };
12
+ /**
13
+ * Integrates one frame of character motor motion: horizontal wish velocity, blocking against
14
+ * static colliders via vertical capsule overlap (two spheres), axis-separated wall sliding,
15
+ * optional step-up, jump + gravity while airborne, then vertical snap to ground when supported —
16
+ * unless volume state selects swim or ladder
17
+ * (see gameplay-volumes).
18
+ *
19
+ * Mutates `feetPosition` in place (treated as feet on the floor).
20
+ *
21
+ * @param world ECS world containing obstacle colliders.
22
+ * @param entity Owning entity (for optional `CharacterVolumeStateComponent`).
23
+ * @param feetPosition Entity transform position (feet anchor); updated by this call.
24
+ * @param motor Configuration and per-frame wish velocity.
25
+ * @param deltaTime Seconds elapsed.
26
+ * @returns Whether the character is grounded after integration.
27
+ */
28
+ export declare function resolveCharacterMotorStep(world: World, entity: Entity, feetPosition: vec3, motor: CharacterMotorComponent, deltaTime: number): boolean;
29
+ //# sourceMappingURL=resolveCharacterMotor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveCharacterMotor.d.ts","sourceRoot":"","sources":["../src/resolveCharacterMotor.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAG1D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AASjC,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AA+TzE;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,uBAAuB,EAC9B,SAAS,EAAE,MAAM,GAChB;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAa5B;AAsJD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,IAAI,EAClB,KAAK,EAAE,uBAAuB,EAC9B,SAAS,EAAE,MAAM,GAChB,OAAO,CA2BT"}
@@ -0,0 +1,394 @@
1
+ var w = Object.defineProperty;
2
+ var T = (i, e, n) => e in i ? w(i, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : i[e] = n;
3
+ var r = (i, e, n) => T(i, typeof e != "symbol" ? e + "" : e, n);
4
+ import { vec3 as f, quat as Y } from "gl-matrix";
5
+ import { TransformComponent as W } from "@naanouff/w3dts-core";
6
+ import { raycast as m, ColliderComponent as q, overlapSphere as G } from "@naanouff/w3dts-physics";
7
+ import { readLocomotionAxes as J } from "@naanouff/w3dts-input";
8
+ import { LogChannel as V } from "@naanouff/w3dts-logger";
9
+ class P {
10
+ constructor() {
11
+ /**
12
+ * When false, the motor does not change the transform (wish velocity is still readable).
13
+ */
14
+ r(this, "enabled", !0);
15
+ /**
16
+ * Capsule radius (world units): hemispheres at bottom and top, and cylinder between them.
17
+ */
18
+ r(this, "radius", 0.35);
19
+ /**
20
+ * Total capsule height from feet (bottom of lower hemisphere) to top of upper hemisphere.
21
+ * Clamped to at least `2 * radius` internally. Typical humanoid FPS: ~1.7–1.9.
22
+ */
23
+ r(this, "capsuleHeight", 1.8);
24
+ /**
25
+ * Maximum ledge height the motor attempts to step onto (world units).
26
+ * Step-up is allowed only when the measured rise `groundY - feetY` is at most this (plus slop).
27
+ */
28
+ r(this, "stepHeight", 0.35);
29
+ /**
30
+ * Maximum walkable inclination from the horizontal plane, in **radians**.
31
+ * A ray hit is a valid floor if `hit.normal[1] >= cos(maxSlopeRadians)` (up-facing surfaces only).
32
+ * Steeper slopes are skipped for step-up, ground snap, and grounded probe (like a wall for locomotion).
33
+ */
34
+ r(this, "maxSlopeRadians", 50 * Math.PI / 180);
35
+ /**
36
+ * Shrinks the query sphere slightly so sliding does not jitter on contact.
37
+ */
38
+ r(this, "skinWidth", 0.02);
39
+ /**
40
+ * Desired world velocity in m/s. The Y component is ignored for horizontal resolution.
41
+ */
42
+ r(this, "wishVelocity", f.create());
43
+ /**
44
+ * When true, horizontal displacement this frame is {@link rootMotionDelta}
45
+ * and {@link wishVelocity} is not added on XZ.
46
+ */
47
+ r(this, "rootMotionConsumed", !1);
48
+ /** World displacement in meters for the frame when root motion is consumed. */
49
+ r(this, "rootMotionDelta", f.create());
50
+ /**
51
+ * Optional terrain-style ground when no downward physics ray hits.
52
+ */
53
+ r(this, "heightService", null);
54
+ /**
55
+ * Bitmask passed to overlap queries / {@link raycast} as `collisionMask`.
56
+ */
57
+ r(this, "collisionQueryMask", 4294967295);
58
+ /**
59
+ * Colliders on these entities are skipped in motor overlap and ground raycasts.
60
+ * Use for kinematic “helper” volumes when vertical support comes from {@link heightService}
61
+ * (e.g. analytic ramp) but you still want hitscan / other systems to hit the solid mesh.
62
+ */
63
+ r(this, "collisionQueryIgnoreEntities", null);
64
+ /**
65
+ * Updated each integration step when a supporting surface is found within a short probe.
66
+ */
67
+ r(this, "isGrounded", !1);
68
+ /**
69
+ * Vertical offset above current feet used as the start of the primary downward ground ray.
70
+ */
71
+ r(this, "groundProbeAboveFeet", 2);
72
+ /**
73
+ * Maximum length of the primary downward ray for snapping feet to the floor.
74
+ */
75
+ r(this, "maxGroundRay", 256);
76
+ // --- Jump (ground mode only; set {@link jumpRequested} from controller on jump input) ---
77
+ /** When true for a frame, applies {@link jumpImpulseSpeed} if grounded at integration start. */
78
+ r(this, "jumpRequested", !1);
79
+ /** Initial upward speed (m/s) when a jump starts. */
80
+ r(this, "jumpImpulseSpeed", 7);
81
+ /** Gravity magnitude (m/s²) while airborne in ground mode. */
82
+ r(this, "gravity", 22);
83
+ /** Vertical speed (m/s) while airborne; reset when grounded without a jump. */
84
+ r(this, "verticalVelocity", 0);
85
+ /**
86
+ * True from a successful jump until landing. Avoids snapping feet to ground when vertical
87
+ * speed briefly crosses zero at the jump apex while the short grounded probe misses the floor.
88
+ */
89
+ r(this, "jumpArcActive", !1);
90
+ /**
91
+ * Maximum upward Y snap allowed from {@link heightService} relative to current feet.y.
92
+ * Prevents teleporting on top of a floor/bridge the player is walking under.
93
+ * Must be large enough for step-ups and ramp following per frame.
94
+ */
95
+ r(this, "maxHeightServiceSnapUp", 1);
96
+ // --- Swim (when volume state locomotionMode === 'swim') ---
97
+ /** Horizontal swim speed when sprint is off (m/s). Wish is still built in XZ world from yaw. */
98
+ r(this, "swimSpeedWalk", 1.25);
99
+ /** Horizontal swim speed when sprint is on (m/s). */
100
+ r(this, "swimSpeedSprint", 2.5);
101
+ /** Clamp for vertical swim velocity from `wishVelocity[1]` (m/s). */
102
+ r(this, "swimVerticalMaxSpeed", 1.2);
103
+ }
104
+ }
105
+ r(P, "componentName", "CharacterMotorComponent");
106
+ const g = class g {
107
+ constructor() {
108
+ r(this, "locomotionMode", "ground");
109
+ r(this, "activeWaterEntity", null);
110
+ r(this, "activeLadderEntity", null);
111
+ r(this, "enteredWater", !1);
112
+ r(this, "exitedWater", !1);
113
+ r(this, "enteredLadder", !1);
114
+ r(this, "exitedLadder", !1);
115
+ r(this, "ladderWorldUp", f.fromValues(0, 1, 0));
116
+ r(this, "effectiveSwimSpeedH", g.SWIM_SPEED_USE_MOTOR);
117
+ r(this, "effectiveSwimSpeedV", g.SWIM_SPEED_USE_MOTOR);
118
+ r(this, "buoyancyAcceleration", 0);
119
+ r(this, "ladderClimbSpeed", 1.6);
120
+ r(this, "ladderSlideSpeed", 0.4);
121
+ r(this, "_prevInWater", !1);
122
+ r(this, "_prevOnLadder", !1);
123
+ }
124
+ };
125
+ r(g, "componentName", "CharacterVolumeStateComponent"), /** Use motor swim speed fields when volume does not override. */
126
+ r(g, "SWIM_SPEED_USE_MOTOR", -1);
127
+ let C = g;
128
+ const M = 0.08, K = 0.34;
129
+ function j(i) {
130
+ const e = i.collisionQueryMask, n = i.collisionQueryIgnoreEntities;
131
+ return !n || n.size === 0 ? { collisionMask: e } : {
132
+ collisionMask: e,
133
+ includeCollider: (t) => !n.has(t)
134
+ };
135
+ }
136
+ function U(i) {
137
+ return Math.max(1e-4, i.radius - i.skinWidth);
138
+ }
139
+ function O(i) {
140
+ const e = 2 * i.radius;
141
+ return i.capsuleHeight < e - 1e-6 ? e : i.capsuleHeight;
142
+ }
143
+ function N(i, e, n, t, s, c, a) {
144
+ const l = s.radius, d = O(s);
145
+ return G(i, [e, n + l, t], c, a).length > 0 ? !0 : d <= 2 * l + 1e-6 ? !1 : G(i, [e, n + d - l, t], c, a).length > 0;
146
+ }
147
+ function X(i, e, n, t, s, c, a) {
148
+ const l = s.radius, d = O(s), o = /* @__PURE__ */ new Set(), p = [];
149
+ for (const u of G(i, [e, n + l, t], c, a))
150
+ o.has(u) || (o.add(u), p.push(u));
151
+ if (d > 2 * l + 1e-6)
152
+ for (const u of G(i, [e, n + d - l, t], c, a))
153
+ o.has(u) || (o.add(u), p.push(u));
154
+ return p;
155
+ }
156
+ const Z = q;
157
+ function ee(i, e) {
158
+ const n = i.position[1] + e.offset[1];
159
+ return e.type === "SPHERE" ? n - e.size[0] : n - e.size[1];
160
+ }
161
+ function ne(i, e, n, t, s) {
162
+ if (ee(i, e) > s + M)
163
+ return !1;
164
+ const a = i.position[1] + e.offset[1];
165
+ return !((e.type === "SPHERE" ? a + e.size[0] : a + e.size[1]) <= n + t + M);
166
+ }
167
+ function ie(i) {
168
+ return Math.cos(Math.max(0, Math.min(Math.PI * 0.5 - 1e-6, i.maxSlopeRadians)));
169
+ }
170
+ function $(i, e) {
171
+ const n = i.includeCollider;
172
+ return {
173
+ ...i,
174
+ includeCollider: (t, s, c) => e(t) ? n ? n(t, s, c) : !0 : !1
175
+ };
176
+ }
177
+ function R(i, e, n, t, s, c, a, l = 12) {
178
+ const d = ie(c), o = /* @__PURE__ */ new Set();
179
+ for (let p = 0; p < l; p++) {
180
+ const u = $(a, (y) => !o.has(y)), h = m(i, [e, n, t], [0, -1, 0], s, u);
181
+ if (!h) return null;
182
+ if (h.normal[1] >= d - 1e-5) return h;
183
+ o.add(h.entity);
184
+ }
185
+ return null;
186
+ }
187
+ function x(i, e, n, t, s, c, a) {
188
+ const l = n[0] + t, d = n[2] + s, o = n[1];
189
+ if (!N(i, l, o, d, e, c, a))
190
+ return f.fromValues(l, o, d);
191
+ const u = o + e.stepHeight, h = u + O(e), y = X(i, l, u, d, e, c, a);
192
+ let b = !1;
193
+ for (const E of y) {
194
+ const D = i.getComponent(E, W), F = i.getComponent(E, Z);
195
+ if (!(!D || !F) && ne(D, F, o, e.stepHeight, h)) {
196
+ b = !0;
197
+ break;
198
+ }
199
+ }
200
+ if (b)
201
+ return null;
202
+ const H = u + e.radius + 0.05, S = e.stepHeight + e.radius + 2, v = R(i, l, H, d, S, e, a);
203
+ if (!v)
204
+ return null;
205
+ const A = v.point[1];
206
+ if (A - o > e.stepHeight + M)
207
+ return null;
208
+ const _ = f.fromValues(l, A, d), Q = $(a, (E) => E !== v.entity);
209
+ return N(i, l, A, d, e, c, Q) ? null : _;
210
+ }
211
+ function k(i, e, n, t, s, c, a) {
212
+ const l = x(i, e, n, t, s, c, a);
213
+ if (l) {
214
+ f.copy(n, l);
215
+ return;
216
+ }
217
+ const d = x(i, e, n, t, 0, c, a);
218
+ if (d) {
219
+ f.copy(n, d);
220
+ const p = x(i, e, n, 0, s, c, a);
221
+ p && f.copy(n, p);
222
+ return;
223
+ }
224
+ const o = x(i, e, n, 0, s, c, a);
225
+ if (o) {
226
+ f.copy(n, o);
227
+ const p = x(i, e, n, t, 0, c, a);
228
+ p && f.copy(n, p);
229
+ }
230
+ }
231
+ function z(i, e, n) {
232
+ let t = n.point[1];
233
+ if (i.heightService) {
234
+ const s = i.heightService.getHeightAt(e[0], e[2]);
235
+ s - e[1] <= i.maxHeightServiceSnapUp && (t = Math.max(t, s));
236
+ }
237
+ return t;
238
+ }
239
+ function te(i, e, n, t) {
240
+ const s = n[1] + e.groundProbeAboveFeet, c = R(
241
+ i,
242
+ n[0],
243
+ s,
244
+ n[2],
245
+ e.maxGroundRay,
246
+ e,
247
+ t
248
+ );
249
+ if (c) {
250
+ const a = z(e, n, c);
251
+ if (a - n[1] <= e.stepHeight + M) {
252
+ n[1] = a;
253
+ return;
254
+ }
255
+ }
256
+ if (e.heightService) {
257
+ const a = e.heightService.getHeightAt(n[0], n[2]);
258
+ a - n[1] <= e.maxHeightServiceSnapUp && (n[1] = a);
259
+ }
260
+ }
261
+ const L = 0.14, se = 1e-3;
262
+ function ce(i, e, n) {
263
+ return n - e <= i.stepHeight + M;
264
+ }
265
+ function B(i, e) {
266
+ if (i.rootMotionConsumed) {
267
+ const n = i.rootMotionDelta[0], t = i.rootMotionDelta[2];
268
+ return i.rootMotionDelta[0] = 0, i.rootMotionDelta[1] = 0, i.rootMotionDelta[2] = 0, { dx: n, dz: t };
269
+ }
270
+ return {
271
+ dx: i.wishVelocity[0] * e,
272
+ dz: i.wishVelocity[2] * e
273
+ };
274
+ }
275
+ function ae(i, e, n, t) {
276
+ const s = j(e), c = U(e), a = e.jumpRequested;
277
+ e.jumpRequested = !1;
278
+ const l = I(i, n, e, s);
279
+ l && !a && (e.verticalVelocity = 0), a && l && (e.verticalVelocity = e.jumpImpulseSpeed, e.jumpArcActive = !0);
280
+ const { dx: d, dz: o } = B(e, t);
281
+ if ((Math.abs(d) > 1e-8 || Math.abs(o) > 1e-8) && k(i, e, n, d, o, c, s), e.jumpArcActive || Math.abs(e.verticalVelocity) > se) {
282
+ e.verticalVelocity -= e.gravity * t, n[1] += e.verticalVelocity * t;
283
+ let u = !1;
284
+ const h = R(
285
+ i,
286
+ n[0],
287
+ n[1] + 0.08,
288
+ n[2],
289
+ e.maxGroundRay,
290
+ e,
291
+ s
292
+ );
293
+ if (h && e.verticalVelocity <= 0) {
294
+ const y = z(e, n, h);
295
+ ce(e, n[1], y) && n[1] <= y + L && (n[1] = y, u = !0);
296
+ }
297
+ if (!u && e.verticalVelocity <= 0 && e.heightService) {
298
+ const y = e.heightService.getHeightAt(n[0], n[2]);
299
+ n[1] <= y + L && y - n[1] <= e.maxHeightServiceSnapUp && (n[1] = y, u = !0);
300
+ }
301
+ u && (e.verticalVelocity = 0, e.jumpArcActive = !1);
302
+ } else
303
+ te(i, e, n, s), e.jumpArcActive = !1;
304
+ return e.isGrounded = I(i, n, e, s), e.isGrounded;
305
+ }
306
+ const re = 0.12;
307
+ function I(i, e, n, t) {
308
+ if (n.jumpArcActive && n.verticalVelocity > 0)
309
+ return !1;
310
+ if (R(
311
+ i,
312
+ e[0],
313
+ e[1] + 0.05,
314
+ e[2],
315
+ K,
316
+ n,
317
+ t
318
+ )) return !0;
319
+ if (n.heightService) {
320
+ const c = n.heightService.getHeightAt(e[0], e[2]), a = e[1] - c;
321
+ return a < re && a > -(n.stepHeight + M);
322
+ }
323
+ return !1;
324
+ }
325
+ function oe(i, e, n, t, s) {
326
+ const c = j(e), a = U(e), { dx: l, dz: d } = B(e, s);
327
+ (Math.abs(l) > 1e-8 || Math.abs(d) > 1e-8) && k(i, e, n, l, d, a, c);
328
+ const o = t.effectiveSwimSpeedV > 0 ? t.effectiveSwimSpeedV : e.swimVerticalMaxSpeed;
329
+ let p = e.wishVelocity[1];
330
+ return p = Math.min(o, Math.max(-o, p)), n[1] += p * s + t.buoyancyAcceleration * s * s, e.isGrounded = !1, !1;
331
+ }
332
+ function ue(i, e, n, t) {
333
+ const s = n.ladderWorldUp, c = f.dot(i.wishVelocity, s);
334
+ return e[0] += s[0] * c * t, e[1] += s[1] * c * t, e[2] += s[2] * c * t, i.isGrounded = !1, !1;
335
+ }
336
+ function le(i, e, n, t, s) {
337
+ const c = i.getComponent(e, C), a = (c == null ? void 0 : c.locomotionMode) ?? "ground";
338
+ return t.enabled ? a === "swim" && c ? (t.verticalVelocity = 0, t.jumpArcActive = !1, oe(i, t, n, c, s)) : a === "ladder" && c ? (t.verticalVelocity = 0, t.jumpArcActive = !1, ue(t, n, c, s)) : ae(i, t, n, s) : (t.verticalVelocity = 0, a === "ground" ? (t.isGrounded = I(i, n, t, j(t)), t.isGrounded) : (t.isGrounded = !1, !1));
339
+ }
340
+ function Ae(i, e, n, t, s) {
341
+ const { forward: c, strafe: a, sprint: l } = J(i), d = l ? t : n, o = Y.create();
342
+ Y.fromEuler(o, 0, e * 180 / Math.PI, 0);
343
+ const p = f.fromValues(0, 0, -1), u = f.fromValues(1, 0, 0);
344
+ f.transformQuat(p, p, o), f.transformQuat(u, u, o), p[1] = 0, u[1] = 0, f.normalize(p, p), f.normalize(u, u), f.zero(s), f.scaleAndAdd(s, s, p, c), f.scaleAndAdd(s, s, u, a);
345
+ const h = f.length(s);
346
+ h > 1e-6 && f.scale(s, s, 1 / h), f.scale(s, s, d), s[1] = 0;
347
+ }
348
+ function pe() {
349
+ const i = globalThis.__W3DTS_SCRIPT_PLAY_STATE__;
350
+ return i === void 0 ? !0 : i === "playing";
351
+ }
352
+ function ge(i) {
353
+ const e = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), t = /* @__PURE__ */ new Map(), s = 0.75;
354
+ return (c, a, l) => {
355
+ if (!pe()) return;
356
+ const d = c.query([
357
+ W,
358
+ P
359
+ ]);
360
+ for (const [o, [p, u]] of d.iter()) {
361
+ if (!u.enabled) continue;
362
+ const h = e.get(o) ?? !1, y = n.get(o) ?? !1, b = p.position[1], H = u.jumpRequested;
363
+ le(c, o, p.position, u, a);
364
+ const S = p.position[1], v = S - b;
365
+ H && i.info(
366
+ V.Gameplay,
367
+ `[Motor:${o}] jumpRequested consumed — grounded=${u.isGrounded} vy=${u.verticalVelocity.toFixed(2)} arc=${u.jumpArcActive}`
368
+ ), !h && u.isGrounded && i.info(V.Gameplay, `[Motor:${o}] LANDED y=${S.toFixed(2)}`), h && !u.isGrounded && !u.jumpArcActive && i.info(
369
+ V.Gameplay,
370
+ `[Motor:${o}] left ground (no jump) y=${S.toFixed(2)}`
371
+ );
372
+ const A = u.jumpArcActive || Math.abs(u.verticalVelocity) > 0.01;
373
+ if (A && !y && i.info(
374
+ V.Gameplay,
375
+ `[Motor:${o}] AIRBORNE vy=${u.verticalVelocity.toFixed(2)} y=${S.toFixed(2)}`
376
+ ), Math.abs(v) > 1) {
377
+ const _ = t.get(o) ?? Number.NEGATIVE_INFINITY;
378
+ l - _ >= s && (i.warn(
379
+ V.Gameplay,
380
+ `[Motor:${o}] BIG Y SNAP dy=${v.toFixed(2)} y=${b.toFixed(2)}->${S.toFixed(2)}`
381
+ ), t.set(o, l));
382
+ }
383
+ e.set(o, u.isGrounded), n.set(o, A), f.zero(u.wishVelocity), p.updateLocalTransform();
384
+ }
385
+ };
386
+ }
387
+ export {
388
+ P as CharacterMotorComponent,
389
+ C as CharacterVolumeStateComponent,
390
+ Ae as computeWishVelocityXZFromLocomotion,
391
+ ge as createCharacterMotorSystem,
392
+ B as planarDeltaForMotor,
393
+ le as resolveCharacterMotorStep
394
+ };
@@ -0,0 +1 @@
1
+ (function(h,p){typeof exports=="object"&&typeof module<"u"?p(exports,require("gl-matrix"),require("@naanouff/w3dts-core"),require("@naanouff/w3dts-physics"),require("@naanouff/w3dts-input"),require("@naanouff/w3dts-logger")):typeof define=="function"&&define.amd?define(["exports","gl-matrix","@naanouff/w3dts-core","@naanouff/w3dts-physics","@naanouff/w3dts-input","@naanouff/w3dts-logger"],p):(h=typeof globalThis<"u"?globalThis:h||self,p(h.W3dtsCharacter={},h.glMatrix,h.W3dtsCore,h.W3dtsPhysics,h.W3dtsInput,h.W3dtsLogger))})(this,function(h,p,S,V,m,M){"use strict";var pe=Object.defineProperty;var le=(h,p,S)=>p in h?pe(h,p,{enumerable:!0,configurable:!0,writable:!0,value:S}):h[p]=S;var r=(h,p,S)=>le(h,typeof p!="symbol"?p+"":p,S);class W{constructor(){r(this,"enabled",!0);r(this,"radius",.35);r(this,"capsuleHeight",1.8);r(this,"stepHeight",.35);r(this,"maxSlopeRadians",50*Math.PI/180);r(this,"skinWidth",.02);r(this,"wishVelocity",p.vec3.create());r(this,"rootMotionConsumed",!1);r(this,"rootMotionDelta",p.vec3.create());r(this,"heightService",null);r(this,"collisionQueryMask",4294967295);r(this,"collisionQueryIgnoreEntities",null);r(this,"isGrounded",!1);r(this,"groundProbeAboveFeet",2);r(this,"maxGroundRay",256);r(this,"jumpRequested",!1);r(this,"jumpImpulseSpeed",7);r(this,"gravity",22);r(this,"verticalVelocity",0);r(this,"jumpArcActive",!1);r(this,"maxHeightServiceSnapUp",1);r(this,"swimSpeedWalk",1.25);r(this,"swimSpeedSprint",2.5);r(this,"swimVerticalMaxSpeed",1.2)}}r(W,"componentName","CharacterMotorComponent");const b=class b{constructor(){r(this,"locomotionMode","ground");r(this,"activeWaterEntity",null);r(this,"activeLadderEntity",null);r(this,"enteredWater",!1);r(this,"exitedWater",!1);r(this,"enteredLadder",!1);r(this,"exitedLadder",!1);r(this,"ladderWorldUp",p.vec3.fromValues(0,1,0));r(this,"effectiveSwimSpeedH",b.SWIM_SPEED_USE_MOTOR);r(this,"effectiveSwimSpeedV",b.SWIM_SPEED_USE_MOTOR);r(this,"buoyancyAcceleration",0);r(this,"ladderClimbSpeed",1.6);r(this,"ladderSlideSpeed",.4);r(this,"_prevInWater",!1);r(this,"_prevOnLadder",!1)}};r(b,"componentName","CharacterVolumeStateComponent"),r(b,"SWIM_SPEED_USE_MOTOR",-1);let H=b;const C=.08,Q=.34;function j(t){const e=t.collisionQueryMask,n=t.collisionQueryIgnoreEntities;return!n||n.size===0?{collisionMask:e}:{collisionMask:e,includeCollider:i=>!n.has(i)}}function N(t){return Math.max(1e-4,t.radius-t.skinWidth)}function F(t){const e=2*t.radius;return t.capsuleHeight<e-1e-6?e:t.capsuleHeight}function U(t,e,n,i,c,s,a){const l=c.radius,f=F(c);return V.overlapSphere(t,[e,n+l,i],s,a).length>0?!0:f<=2*l+1e-6?!1:V.overlapSphere(t,[e,n+f-l,i],s,a).length>0}function x(t,e,n,i,c,s,a){const l=c.radius,f=F(c),o=new Set,d=[];for(const u of V.overlapSphere(t,[e,n+l,i],s,a))o.has(u)||(o.add(u),d.push(u));if(f>2*l+1e-6)for(const u of V.overlapSphere(t,[e,n+f-l,i],s,a))o.has(u)||(o.add(u),d.push(u));return d}const w=V.ColliderComponent;function J(t,e){const n=t.position[1]+e.offset[1];return e.type==="SPHERE"?n-e.size[0]:n-e.size[1]}function K(t,e,n,i,c){if(J(t,e)>c+C)return!1;const a=t.position[1]+e.offset[1];return!((e.type==="SPHERE"?a+e.size[0]:a+e.size[1])<=n+i+C)}function X(t){return Math.cos(Math.max(0,Math.min(Math.PI*.5-1e-6,t.maxSlopeRadians)))}function $(t,e){const n=t.includeCollider;return{...t,includeCollider:(i,c,s)=>e(i)?n?n(i,c,s):!0:!1}}function _(t,e,n,i,c,s,a,l=12){const f=X(s),o=new Set;for(let d=0;d<l;d++){const u=$(a,y=>!o.has(y)),v=V.raycast(t,[e,n,i],[0,-1,0],c,u);if(!v)return null;if(v.normal[1]>=f-1e-5)return v;o.add(v.entity)}return null}function G(t,e,n,i,c,s,a){const l=n[0]+i,f=n[2]+c,o=n[1];if(!U(t,l,o,f,e,s,a))return p.vec3.fromValues(l,o,f);const u=o+e.stepHeight,v=u+F(e),y=x(t,l,u,f,e,s,a);let R=!1;for(const I of y){const q=t.getComponent(I,S.TransformComponent),T=t.getComponent(I,w);if(!(!q||!T)&&K(q,T,o,e.stepHeight,v)){R=!0;break}}if(R)return null;const L=u+e.radius+.05,A=e.stepHeight+e.radius+2,g=_(t,l,L,f,A,e,a);if(!g)return null;const E=g.point[1];if(E-o>e.stepHeight+C)return null;const Y=p.vec3.fromValues(l,E,f),ue=$(a,I=>I!==g.entity);return U(t,l,E,f,e,s,ue)?null:Y}function k(t,e,n,i,c,s,a){const l=G(t,e,n,i,c,s,a);if(l){p.vec3.copy(n,l);return}const f=G(t,e,n,i,0,s,a);if(f){p.vec3.copy(n,f);const d=G(t,e,n,0,c,s,a);d&&p.vec3.copy(n,d);return}const o=G(t,e,n,0,c,s,a);if(o){p.vec3.copy(n,o);const d=G(t,e,n,i,0,s,a);d&&p.vec3.copy(n,d)}}function P(t,e,n){let i=n.point[1];if(t.heightService){const c=t.heightService.getHeightAt(e[0],e[2]);c-e[1]<=t.maxHeightServiceSnapUp&&(i=Math.max(i,c))}return i}function Z(t,e,n,i){const c=n[1]+e.groundProbeAboveFeet,s=_(t,n[0],c,n[2],e.maxGroundRay,e,i);if(s){const a=P(e,n,s);if(a-n[1]<=e.stepHeight+C){n[1]=a;return}}if(e.heightService){const a=e.heightService.getHeightAt(n[0],n[2]);a-n[1]<=e.maxHeightServiceSnapUp&&(n[1]=a)}}const z=.14,ee=.001;function ne(t,e,n){return n-e<=t.stepHeight+C}function O(t,e){if(t.rootMotionConsumed){const n=t.rootMotionDelta[0],i=t.rootMotionDelta[2];return t.rootMotionDelta[0]=0,t.rootMotionDelta[1]=0,t.rootMotionDelta[2]=0,{dx:n,dz:i}}return{dx:t.wishVelocity[0]*e,dz:t.wishVelocity[2]*e}}function te(t,e,n,i){const c=j(e),s=N(e),a=e.jumpRequested;e.jumpRequested=!1;const l=D(t,n,e,c);l&&!a&&(e.verticalVelocity=0),a&&l&&(e.verticalVelocity=e.jumpImpulseSpeed,e.jumpArcActive=!0);const{dx:f,dz:o}=O(e,i);if((Math.abs(f)>1e-8||Math.abs(o)>1e-8)&&k(t,e,n,f,o,s,c),e.jumpArcActive||Math.abs(e.verticalVelocity)>ee){e.verticalVelocity-=e.gravity*i,n[1]+=e.verticalVelocity*i;let u=!1;const v=_(t,n[0],n[1]+.08,n[2],e.maxGroundRay,e,c);if(v&&e.verticalVelocity<=0){const y=P(e,n,v);ne(e,n[1],y)&&n[1]<=y+z&&(n[1]=y,u=!0)}if(!u&&e.verticalVelocity<=0&&e.heightService){const y=e.heightService.getHeightAt(n[0],n[2]);n[1]<=y+z&&y-n[1]<=e.maxHeightServiceSnapUp&&(n[1]=y,u=!0)}u&&(e.verticalVelocity=0,e.jumpArcActive=!1)}else Z(t,e,n,c),e.jumpArcActive=!1;return e.isGrounded=D(t,n,e,c),e.isGrounded}const ie=.12;function D(t,e,n,i){if(n.jumpArcActive&&n.verticalVelocity>0)return!1;if(_(t,e[0],e[1]+.05,e[2],Q,n,i))return!0;if(n.heightService){const s=n.heightService.getHeightAt(e[0],e[2]),a=e[1]-s;return a<ie&&a>-(n.stepHeight+C)}return!1}function ce(t,e,n,i,c){const s=j(e),a=N(e),{dx:l,dz:f}=O(e,c);(Math.abs(l)>1e-8||Math.abs(f)>1e-8)&&k(t,e,n,l,f,a,s);const o=i.effectiveSwimSpeedV>0?i.effectiveSwimSpeedV:e.swimVerticalMaxSpeed;let d=e.wishVelocity[1];return d=Math.min(o,Math.max(-o,d)),n[1]+=d*c+i.buoyancyAcceleration*c*c,e.isGrounded=!1,!1}function se(t,e,n,i){const c=n.ladderWorldUp,s=p.vec3.dot(t.wishVelocity,c);return e[0]+=c[0]*s*i,e[1]+=c[1]*s*i,e[2]+=c[2]*s*i,t.isGrounded=!1,!1}function B(t,e,n,i,c){const s=t.getComponent(e,H),a=(s==null?void 0:s.locomotionMode)??"ground";return i.enabled?a==="swim"&&s?(i.verticalVelocity=0,i.jumpArcActive=!1,ce(t,i,n,s,c)):a==="ladder"&&s?(i.verticalVelocity=0,i.jumpArcActive=!1,se(i,n,s,c)):te(t,i,n,c):(i.verticalVelocity=0,a==="ground"?(i.isGrounded=D(t,n,i,j(i)),i.isGrounded):(i.isGrounded=!1,!1))}function ae(t,e,n,i,c){const{forward:s,strafe:a,sprint:l}=m.readLocomotionAxes(t),f=l?i:n,o=p.quat.create();p.quat.fromEuler(o,0,e*180/Math.PI,0);const d=p.vec3.fromValues(0,0,-1),u=p.vec3.fromValues(1,0,0);p.vec3.transformQuat(d,d,o),p.vec3.transformQuat(u,u,o),d[1]=0,u[1]=0,p.vec3.normalize(d,d),p.vec3.normalize(u,u),p.vec3.zero(c),p.vec3.scaleAndAdd(c,c,d,s),p.vec3.scaleAndAdd(c,c,u,a);const v=p.vec3.length(c);v>1e-6&&p.vec3.scale(c,c,1/v),p.vec3.scale(c,c,f),c[1]=0}function re(){const t=globalThis.__W3DTS_SCRIPT_PLAY_STATE__;return t===void 0?!0:t==="playing"}function oe(t){const e=new Map,n=new Map,i=new Map,c=.75;return(s,a,l)=>{if(!re())return;const f=s.query([S.TransformComponent,W]);for(const[o,[d,u]]of f.iter()){if(!u.enabled)continue;const v=e.get(o)??!1,y=n.get(o)??!1,R=d.position[1],L=u.jumpRequested;B(s,o,d.position,u,a);const A=d.position[1],g=A-R;L&&t.info(M.LogChannel.Gameplay,`[Motor:${o}] jumpRequested consumed — grounded=${u.isGrounded} vy=${u.verticalVelocity.toFixed(2)} arc=${u.jumpArcActive}`),!v&&u.isGrounded&&t.info(M.LogChannel.Gameplay,`[Motor:${o}] LANDED y=${A.toFixed(2)}`),v&&!u.isGrounded&&!u.jumpArcActive&&t.info(M.LogChannel.Gameplay,`[Motor:${o}] left ground (no jump) y=${A.toFixed(2)}`);const E=u.jumpArcActive||Math.abs(u.verticalVelocity)>.01;if(E&&!y&&t.info(M.LogChannel.Gameplay,`[Motor:${o}] AIRBORNE vy=${u.verticalVelocity.toFixed(2)} y=${A.toFixed(2)}`),Math.abs(g)>1){const Y=i.get(o)??Number.NEGATIVE_INFINITY;l-Y>=c&&(t.warn(M.LogChannel.Gameplay,`[Motor:${o}] BIG Y SNAP dy=${g.toFixed(2)} y=${R.toFixed(2)}->${A.toFixed(2)}`),i.set(o,l))}e.set(o,u.isGrounded),n.set(o,E),p.vec3.zero(u.wishVelocity),d.updateLocalTransform()}}}h.CharacterMotorComponent=W,h.CharacterVolumeStateComponent=H,h.computeWishVelocityXZFromLocomotion=ae,h.createCharacterMotorSystem=oe,h.planarDeltaForMotor=O,h.resolveCharacterMotorStep=B,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "@naanouff/w3dts-character",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.21",
4
+ "description": "First-person character motor (ECS) with static collider blocking for W3DTS",
5
+ "main": "dist/w3dts-character.umd.js",
6
+ "module": "dist/w3dts-character.es.js",
7
+ "types": "dist/index.d.ts",
8
+ "type": "module",
9
+ "publishConfig": {
10
+ "access": "public",
11
+ "registry": "https://registry.npmjs.org"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/naanouff/w3dts.git"
19
+ },
20
+ "dependencies": {
21
+ "gl-matrix": "^3.4.3",
22
+ "@naanouff/w3dts-core": "0.0.38",
23
+ "@naanouff/w3dts-physics": "0.0.29",
24
+ "@naanouff/w3dts-input": "0.0.22",
25
+ "@naanouff/w3dts-logger": "0.0.23"
26
+ },
27
+ "devDependencies": {
28
+ "typescript": "^5.5.4",
29
+ "vite": "^5.4.21",
30
+ "vite-plugin-dts": "^4.0.3",
31
+ "vitest": "^3.2.7"
32
+ },
33
+ "license": "UNLICENSED",
34
+ "exports": {
35
+ ".": {
36
+ "types": "./dist/index.d.ts",
37
+ "import": "./dist/w3dts-character.es.js",
38
+ "require": "./dist/w3dts-character.umd.js"
39
+ }
40
+ },
41
+ "scripts": {
42
+ "dev": "vite build --watch",
43
+ "build": "vite build",
44
+ "test": "vitest --run --config ../../vitest.config.ts",
45
+ "check": "tsc --noEmit -p ./tsconfig.json"
46
+ }
6
47
  }