@zylem/behaviors 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/dist/behavior-system-y8SGrGWy.d.ts +580 -0
  2. package/dist/{chunk-ARJWUNRE.js → chunk-27UPCP5B.js} +10 -10
  3. package/dist/{chunk-ARJWUNRE.js.map → chunk-27UPCP5B.js.map} +1 -1
  4. package/dist/{chunk-YUO6SKJD.js → chunk-4L36OPY2.js} +33 -23
  5. package/dist/chunk-4L36OPY2.js.map +1 -0
  6. package/dist/{chunk-DYLH6K7B.js → chunk-4WH2ZN7P.js} +1 -1
  7. package/dist/{chunk-DYLH6K7B.js.map → chunk-4WH2ZN7P.js.map} +1 -1
  8. package/dist/{chunk-ZPSAXX73.js → chunk-5SM5SH5O.js} +26 -26
  9. package/dist/chunk-5SM5SH5O.js.map +1 -0
  10. package/dist/{chunk-STCCNLPC.js → chunk-5XZWTFGF.js} +5 -3
  11. package/dist/chunk-5XZWTFGF.js.map +1 -0
  12. package/dist/{chunk-JAN34G6S.js → chunk-67XLJ26X.js} +152 -168
  13. package/dist/chunk-67XLJ26X.js.map +1 -0
  14. package/dist/{chunk-2CLT36VB.js → chunk-75FJIUIG.js} +22 -23
  15. package/dist/chunk-75FJIUIG.js.map +1 -0
  16. package/dist/{chunk-OYWFPIHF.js → chunk-A4CA35DQ.js} +20 -17
  17. package/dist/chunk-A4CA35DQ.js.map +1 -0
  18. package/dist/{chunk-SS7ELMJ7.js → chunk-F72WX4MH.js} +49 -33
  19. package/dist/chunk-F72WX4MH.js.map +1 -0
  20. package/dist/{chunk-RTRVBEIB.js → chunk-FTDZAKZW.js} +5 -18
  21. package/dist/chunk-FTDZAKZW.js.map +1 -0
  22. package/dist/{chunk-XIJNE67Y.js → chunk-GFARAJV2.js} +3 -5
  23. package/dist/{chunk-XIJNE67Y.js.map → chunk-GFARAJV2.js.map} +1 -1
  24. package/dist/{chunk-VAN4WW3F.js → chunk-H4TJ7X4O.js} +4 -7
  25. package/dist/chunk-H4TJ7X4O.js.map +1 -0
  26. package/dist/{chunk-KS72RZHX.js → chunk-IZJFUAXB.js} +1 -1
  27. package/dist/{chunk-KS72RZHX.js.map → chunk-IZJFUAXB.js.map} +1 -1
  28. package/dist/{chunk-SVLNF434.js → chunk-JXMF4NUI.js} +51 -3
  29. package/dist/chunk-JXMF4NUI.js.map +1 -0
  30. package/dist/chunk-KFFETUAL.js +40 -0
  31. package/dist/chunk-KFFETUAL.js.map +1 -0
  32. package/dist/chunk-LL4FLHIY.js +12 -0
  33. package/dist/chunk-LL4FLHIY.js.map +1 -0
  34. package/dist/{chunk-RTVRUNYR.js → chunk-OTLSUYQQ.js} +2 -2
  35. package/dist/chunk-OTLSUYQQ.js.map +1 -0
  36. package/dist/{chunk-Y6UJOUE5.js → chunk-Q75LKHDF.js} +14 -2
  37. package/dist/chunk-Q75LKHDF.js.map +1 -0
  38. package/dist/{chunk-KOZUNIIE.js → chunk-QDCJLJUV.js} +10 -10
  39. package/dist/{chunk-KOZUNIIE.js.map → chunk-QDCJLJUV.js.map} +1 -1
  40. package/dist/{chunk-LD5SMIB3.js → chunk-QVCMJ4Q2.js} +1 -1
  41. package/dist/{chunk-LD5SMIB3.js.map → chunk-QVCMJ4Q2.js.map} +1 -1
  42. package/dist/{chunk-F3KRX2V4.js → chunk-RDHSHEZM.js} +46 -24
  43. package/dist/chunk-RDHSHEZM.js.map +1 -0
  44. package/dist/chunk-UIG7MWS2.js +518 -0
  45. package/dist/chunk-UIG7MWS2.js.map +1 -0
  46. package/dist/{chunk-6C6NXNEW.js → chunk-UL6Q5HDP.js} +25 -3
  47. package/dist/chunk-UL6Q5HDP.js.map +1 -0
  48. package/dist/{chunk-XG2YMCJW.js → chunk-VMQHNJJI.js} +6 -8
  49. package/dist/{chunk-XG2YMCJW.js.map → chunk-VMQHNJJI.js.map} +1 -1
  50. package/dist/{chunk-GCNRXAKH.js → chunk-XP3ASVGJ.js} +13 -10
  51. package/dist/chunk-XP3ASVGJ.js.map +1 -0
  52. package/dist/{chunk-ID5QJYF5.js → chunk-Y7YIAWPW.js} +11 -1
  53. package/dist/chunk-Y7YIAWPW.js.map +1 -0
  54. package/dist/{chunk-4I6E6DGV.js → chunk-Z72F2ENM.js} +6 -8
  55. package/dist/{chunk-4I6E6DGV.js.map → chunk-Z72F2ENM.js.map} +1 -1
  56. package/dist/{chunk-FPTWVCPN.js → chunk-ZA43LPRM.js} +4 -24
  57. package/dist/chunk-ZA43LPRM.js.map +1 -0
  58. package/dist/{chunk-K3UNLQ7W.js → chunk-ZRQWSURL.js} +2 -2
  59. package/dist/chunk-ZRQWSURL.js.map +1 -0
  60. package/dist/cooldown.d.ts +2 -1
  61. package/dist/cooldown.js +1 -1
  62. package/dist/coordinators/boundary-ricochet-3d.d.ts +12 -5
  63. package/dist/coordinators/boundary-ricochet-3d.js +2 -1
  64. package/dist/coordinators/boundary-ricochet.d.ts +9 -7
  65. package/dist/coordinators/boundary-ricochet.js +2 -1
  66. package/dist/coordinators/first-person-shooter.d.ts +9 -8
  67. package/dist/coordinators/first-person-shooter.js +3 -3
  68. package/dist/coordinators/multidirectional-space-shooter.d.ts +12 -9
  69. package/dist/coordinators/multidirectional-space-shooter.js +4 -4
  70. package/dist/coordinators/top-down-shooter.d.ts +9 -8
  71. package/dist/coordinators/top-down-shooter.js +3 -3
  72. package/dist/core.d.ts +4 -4
  73. package/dist/core.js +17 -39
  74. package/dist/core.js.map +1 -1
  75. package/dist/destructible-3d.d.ts +2 -1
  76. package/dist/first-person.d.ts +12 -12
  77. package/dist/first-person.js +9 -8
  78. package/dist/index.d.ts +37 -14
  79. package/dist/index.js +73 -44
  80. package/dist/jumper-2d.d.ts +7 -6
  81. package/dist/jumper-2d.js +6 -5
  82. package/dist/jumper-3d.d.ts +12 -11
  83. package/dist/jumper-3d.js +7 -6
  84. package/dist/{moveable-BCRpjAQb.d.ts → moveable-Cl9h-_w7.d.ts} +2 -78
  85. package/dist/particle-emitter.d.ts +1 -1
  86. package/dist/particle-emitter.js +1 -1
  87. package/dist/platformer-2d.d.ts +306 -0
  88. package/dist/platformer-2d.js +24 -0
  89. package/dist/platformer-3d.d.ts +74 -87
  90. package/dist/platformer-3d.js +5 -4
  91. package/dist/ricochet-2d.d.ts +1 -1
  92. package/dist/ricochet-2d.js +2 -2
  93. package/dist/ricochet-3d.d.ts +1 -1
  94. package/dist/ricochet-3d.js +2 -2
  95. package/dist/runtime-2d.d.ts +93 -2
  96. package/dist/runtime-2d.js +145 -22
  97. package/dist/runtime-2d.js.map +1 -1
  98. package/dist/screen-visibility.d.ts +2 -1
  99. package/dist/screen-wrap.d.ts +2 -1
  100. package/dist/screen-wrap.js +1 -1
  101. package/dist/shooter-2d.d.ts +5 -3
  102. package/dist/shooter-2d.js +2 -2
  103. package/dist/shooter-fire-DGripl0z.d.ts +55 -0
  104. package/dist/thruster.d.ts +8 -18
  105. package/dist/thruster.js +6 -6
  106. package/dist/top-down-movement.d.ts +7 -4
  107. package/dist/top-down-movement.js +5 -5
  108. package/dist/{use-behavior-D_82jAL3.d.ts → use-behavior-B85d5u7-.d.ts} +1 -1
  109. package/dist/world-boundary-2d.d.ts +2 -1
  110. package/dist/world-boundary-2d.js +2 -2
  111. package/dist/world-boundary-3d.d.ts +2 -1
  112. package/dist/world-boundary-3d.js +2 -2
  113. package/package.json +7 -5
  114. package/dist/behavior-system-DYbx-x_-.d.ts +0 -307
  115. package/dist/chunk-2C3Z5GE7.js +0 -176
  116. package/dist/chunk-2C3Z5GE7.js.map +0 -1
  117. package/dist/chunk-2CLT36VB.js.map +0 -1
  118. package/dist/chunk-3PYGRHAL.js +0 -35
  119. package/dist/chunk-3PYGRHAL.js.map +0 -1
  120. package/dist/chunk-6C6NXNEW.js.map +0 -1
  121. package/dist/chunk-F3KRX2V4.js.map +0 -1
  122. package/dist/chunk-FPTWVCPN.js.map +0 -1
  123. package/dist/chunk-GCNRXAKH.js.map +0 -1
  124. package/dist/chunk-ID5QJYF5.js.map +0 -1
  125. package/dist/chunk-JAN34G6S.js.map +0 -1
  126. package/dist/chunk-K3UNLQ7W.js.map +0 -1
  127. package/dist/chunk-OYWFPIHF.js.map +0 -1
  128. package/dist/chunk-RTRVBEIB.js.map +0 -1
  129. package/dist/chunk-RTVRUNYR.js.map +0 -1
  130. package/dist/chunk-SS7ELMJ7.js.map +0 -1
  131. package/dist/chunk-STCCNLPC.js.map +0 -1
  132. package/dist/chunk-SVLNF434.js.map +0 -1
  133. package/dist/chunk-VAN4WW3F.js.map +0 -1
  134. package/dist/chunk-Y6UJOUE5.js.map +0 -1
  135. package/dist/chunk-YUO6SKJD.js.map +0 -1
  136. package/dist/chunk-ZPSAXX73.js.map +0 -1
  137. package/dist/runtime-pong.d.ts +0 -92
  138. package/dist/runtime-pong.js +0 -38
  139. /package/dist/{runtime-pong.js.map → platformer-2d.js.map} +0 -0
@@ -0,0 +1,518 @@
1
+ import {
2
+ SyncStateMachine,
3
+ t
4
+ } from "./chunk-4URF65BR.js";
5
+ import {
6
+ STAGE_INPUT_BUTTON_JUMP,
7
+ STAGE_INPUT_BUTTON_RUN,
8
+ readBehaviorSnapshot
9
+ } from "./chunk-Q75LKHDF.js";
10
+ import {
11
+ defineBehavior
12
+ } from "./chunk-EZNCCO4L.js";
13
+
14
+ // src/lib/behaviors/platformer-2d/components.ts
15
+ function createPlatformer2DMovementComponent(options = {}) {
16
+ return {
17
+ walkSpeed: options.walkSpeed ?? 8,
18
+ runSpeed: options.runSpeed ?? 14,
19
+ jumpForce: options.jumpForce ?? 12,
20
+ maxJumps: options.maxJumps ?? 1,
21
+ gravity: options.gravity ?? 20,
22
+ coyoteTime: options.coyoteTime ?? 0.1,
23
+ jumpBufferTime: options.jumpBufferTime ?? 0.1,
24
+ jumpCutMultiplier: options.jumpCutMultiplier ?? 0.5,
25
+ multiJumpWindowTime: options.multiJumpWindowTime ?? 0.15,
26
+ slopeClimbAngle: options.slopeClimbAngle ?? 50,
27
+ slopeSlideAngle: options.slopeSlideAngle ?? 40,
28
+ autostep: options.autostep ?? { maxHeight: 0.25, minWidth: 0.05 },
29
+ snapToGroundDistance: options.snapToGroundDistance ?? 0.2,
30
+ characterOffset: options.characterOffset ?? 0.05
31
+ };
32
+ }
33
+ function createPlatformer2DInputComponent() {
34
+ return {
35
+ moveX: 0,
36
+ jump: false,
37
+ run: false
38
+ };
39
+ }
40
+ function createPlatformer2DStateComponent() {
41
+ return {
42
+ grounded: false,
43
+ jumping: false,
44
+ falling: false,
45
+ jumpCount: 0,
46
+ jumpStartHeight: 0,
47
+ currentSpeed: 0,
48
+ lastGroundedY: 0,
49
+ jumpPressedLastFrame: false,
50
+ timeSinceGrounded: 0,
51
+ jumpBuffered: false,
52
+ jumpBufferTimer: 0,
53
+ jumpHeld: false,
54
+ jumpCutApplied: false,
55
+ jumpReleasedSinceLastJump: true,
56
+ timeSinceJump: 0,
57
+ jumpedThisFrame: false,
58
+ jumpCutRampTimer: 0,
59
+ verticalVelocity: 0
60
+ };
61
+ }
62
+
63
+ // src/lib/behaviors/platformer-2d/platformer-2d-fsm.ts
64
+ var Platformer2DState = /* @__PURE__ */ ((Platformer2DState2) => {
65
+ Platformer2DState2["Idle"] = "idle";
66
+ Platformer2DState2["Walking"] = "walking";
67
+ Platformer2DState2["Running"] = "running";
68
+ Platformer2DState2["Jumping"] = "jumping";
69
+ Platformer2DState2["Falling"] = "falling";
70
+ Platformer2DState2["Landing"] = "landing";
71
+ return Platformer2DState2;
72
+ })(Platformer2DState || {});
73
+ var Platformer2DEvent = /* @__PURE__ */ ((Platformer2DEvent2) => {
74
+ Platformer2DEvent2["Walk"] = "walk";
75
+ Platformer2DEvent2["Run"] = "run";
76
+ Platformer2DEvent2["Jump"] = "jump";
77
+ Platformer2DEvent2["Fall"] = "fall";
78
+ Platformer2DEvent2["Land"] = "land";
79
+ Platformer2DEvent2["Stop"] = "stop";
80
+ return Platformer2DEvent2;
81
+ })(Platformer2DEvent || {});
82
+ var Platformer2DFSM = class {
83
+ constructor(ctx) {
84
+ this.ctx = ctx;
85
+ this.machine = new SyncStateMachine(
86
+ "idle" /* Idle */,
87
+ [
88
+ // Idle transitions
89
+ t("idle" /* Idle */, "walk" /* Walk */, "walking" /* Walking */),
90
+ t("idle" /* Idle */, "run" /* Run */, "running" /* Running */),
91
+ t("idle" /* Idle */, "jump" /* Jump */, "jumping" /* Jumping */),
92
+ t("idle" /* Idle */, "fall" /* Fall */, "falling" /* Falling */),
93
+ // Walking transitions
94
+ t("walking" /* Walking */, "run" /* Run */, "running" /* Running */),
95
+ t("walking" /* Walking */, "jump" /* Jump */, "jumping" /* Jumping */),
96
+ t("walking" /* Walking */, "stop" /* Stop */, "idle" /* Idle */),
97
+ t("walking" /* Walking */, "fall" /* Fall */, "falling" /* Falling */),
98
+ // Running transitions
99
+ t("running" /* Running */, "walk" /* Walk */, "walking" /* Walking */),
100
+ t("running" /* Running */, "jump" /* Jump */, "jumping" /* Jumping */),
101
+ t("running" /* Running */, "stop" /* Stop */, "idle" /* Idle */),
102
+ t("running" /* Running */, "fall" /* Fall */, "falling" /* Falling */),
103
+ // Jumping transitions
104
+ t("jumping" /* Jumping */, "fall" /* Fall */, "falling" /* Falling */),
105
+ t("jumping" /* Jumping */, "land" /* Land */, "landing" /* Landing */),
106
+ t("jumping" /* Jumping */, "jump" /* Jump */, "jumping" /* Jumping */),
107
+ // Multi-jump
108
+ // Falling transitions
109
+ t("falling" /* Falling */, "land" /* Land */, "landing" /* Landing */),
110
+ t("falling" /* Falling */, "jump" /* Jump */, "jumping" /* Jumping */),
111
+ // Air jump
112
+ // Landing transitions
113
+ t("landing" /* Landing */, "walk" /* Walk */, "walking" /* Walking */),
114
+ t("landing" /* Landing */, "run" /* Run */, "running" /* Running */),
115
+ t("landing" /* Landing */, "stop" /* Stop */, "idle" /* Idle */),
116
+ // Self-transitions (no-ops)
117
+ t("idle" /* Idle */, "stop" /* Stop */, "idle" /* Idle */)
118
+ ]
119
+ );
120
+ }
121
+ ctx;
122
+ machine;
123
+ getState() {
124
+ return this.machine.getState();
125
+ }
126
+ dispatch(event) {
127
+ if (this.machine.can(event)) {
128
+ this.machine.syncDispatch(event);
129
+ }
130
+ }
131
+ isGrounded() {
132
+ const state = this.getState();
133
+ return state === "idle" /* Idle */ || state === "walking" /* Walking */ || state === "running" /* Running */ || state === "landing" /* Landing */;
134
+ }
135
+ getJumpCount() {
136
+ return this.ctx.state.jumpCount;
137
+ }
138
+ /**
139
+ * Update FSM based on current component state.
140
+ */
141
+ update(input, state) {
142
+ this.ctx.input = input;
143
+ this.ctx.state = state;
144
+ const currentState = this.getState();
145
+ const hasInput = Math.abs(input.moveX) > 0.1;
146
+ const isRunning = input.run;
147
+ if (currentState === "falling" /* Falling */ && state.grounded) {
148
+ this.dispatch("land" /* Land */);
149
+ }
150
+ if (state.grounded) {
151
+ if (hasInput) {
152
+ if (isRunning) {
153
+ this.dispatch("run" /* Run */);
154
+ } else {
155
+ this.dispatch("walk" /* Walk */);
156
+ }
157
+ } else {
158
+ this.dispatch("stop" /* Stop */);
159
+ }
160
+ } else if (state.falling) {
161
+ this.dispatch("fall" /* Fall */);
162
+ }
163
+ if (input.jump && !state.jumpPressedLastFrame) {
164
+ this.dispatch("jump" /* Jump */);
165
+ }
166
+ }
167
+ };
168
+
169
+ // src/lib/behaviors/platformer-2d/platformer-2d.system.ts
170
+ import { RigidBodyType, QueryFilterFlags } from "@dimforge/rapier3d-compat";
171
+ var JUMP_CUT_RAMP_DURATION = 0.05;
172
+ var GROUND_STICK_SPEED = 2;
173
+ var FALL_VELOCITY_THRESHOLD = -0.1;
174
+ var DEG_TO_RAD = Math.PI / 180;
175
+ var _desired = { x: 0, y: 0, z: 0 };
176
+ var Platformer2DSystem = class {
177
+ world;
178
+ /** Per-entity character controllers, keyed by entity uuid. */
179
+ controllers = /* @__PURE__ */ new Map();
180
+ /** Spawn-time z per entity; re-pinned every tick to hold the 2D plane. */
181
+ planeZ = /* @__PURE__ */ new Map();
182
+ constructor(world) {
183
+ this.world = world;
184
+ }
185
+ /**
186
+ * One-time per-entity physics setup: convert the body to a
187
+ * position-based kinematic body, record the plane z, and create the
188
+ * entity's character controller. Done lazily on the first update because
189
+ * the body isn't always available at construction time.
190
+ */
191
+ ensurePhysicsConfigured(entity) {
192
+ const existing = this.controllers.get(entity.uuid);
193
+ if (existing) return existing;
194
+ const physicsWorld = this.world?.world;
195
+ if (!physicsWorld || !entity.body) return null;
196
+ if (entity.body.bodyType() !== RigidBodyType.KinematicPositionBased) {
197
+ entity.body.setBodyType(RigidBodyType.KinematicPositionBased, true);
198
+ }
199
+ this.planeZ.set(entity.uuid, entity.body.translation().z);
200
+ const movement = entity.platformer2D;
201
+ const controller = physicsWorld.createCharacterController(movement.characterOffset);
202
+ controller.setUp({ x: 0, y: 1, z: 0 });
203
+ controller.setMaxSlopeClimbAngle(movement.slopeClimbAngle * DEG_TO_RAD);
204
+ controller.setMinSlopeSlideAngle(movement.slopeSlideAngle * DEG_TO_RAD);
205
+ if (movement.autostep) {
206
+ controller.enableAutostep(movement.autostep.maxHeight, movement.autostep.minWidth, true);
207
+ } else {
208
+ controller.disableAutostep();
209
+ }
210
+ controller.enableSnapToGround(movement.snapToGroundDistance);
211
+ controller.setApplyImpulsesToDynamicBodies(true);
212
+ this.controllers.set(entity.uuid, controller);
213
+ return controller;
214
+ }
215
+ /**
216
+ * Handle jump logic with multi-jump, coyote time, buffering, and jump-cut
217
+ * ramp. All vertical motion is written to `state.verticalVelocity`.
218
+ */
219
+ handleJump(entity, delta) {
220
+ const input = entity.$platformer2D;
221
+ const movement = entity.platformer2D;
222
+ const state = entity.platformer2DState;
223
+ if (state.jumping || state.falling) {
224
+ state.timeSinceJump += delta;
225
+ }
226
+ if (!input.jump && state.jumpHeld) {
227
+ state.jumpReleasedSinceLastJump = true;
228
+ }
229
+ if (input.jump && !state.jumpPressedLastFrame) {
230
+ state.jumpBuffered = true;
231
+ state.jumpBufferTimer = movement.jumpBufferTime;
232
+ }
233
+ state.jumpPressedLastFrame = input.jump;
234
+ state.jumpHeld = input.jump;
235
+ if (state.jumpBuffered) {
236
+ state.jumpBufferTimer -= delta;
237
+ if (state.jumpBufferTimer <= 0) {
238
+ state.jumpBuffered = false;
239
+ }
240
+ }
241
+ const minTimeBeforeCut = 0.1;
242
+ const canApplyCut = state.timeSinceJump >= minTimeBeforeCut;
243
+ if (!input.jump && state.jumping && !state.jumpCutApplied && canApplyCut) {
244
+ state.jumpCutApplied = true;
245
+ state.jumpCutRampTimer = JUMP_CUT_RAMP_DURATION;
246
+ }
247
+ if (state.jumpCutRampTimer > 0) {
248
+ if (state.verticalVelocity > 0) {
249
+ const stepT = Math.min(delta / state.jumpCutRampTimer, 1);
250
+ const targetY = state.verticalVelocity * movement.jumpCutMultiplier;
251
+ state.verticalVelocity += (targetY - state.verticalVelocity) * stepT;
252
+ }
253
+ state.jumpCutRampTimer = Math.max(0, state.jumpCutRampTimer - delta);
254
+ }
255
+ if (!state.jumpBuffered) return;
256
+ const inCoyoteWindow = !state.grounded && state.timeSinceGrounded <= movement.coyoteTime;
257
+ const isFirstJump = state.grounded || inCoyoteWindow && state.jumpCount === 0;
258
+ const hasJumpsRemaining = state.jumpCount < movement.maxJumps;
259
+ const buttonReleased = state.jumpReleasedSinceLastJump;
260
+ const inMultiJumpWindow = state.timeSinceJump >= movement.multiJumpWindowTime;
261
+ const canMultiJump = !state.grounded && hasJumpsRemaining && buttonReleased && inMultiJumpWindow;
262
+ if (isFirstJump || canMultiJump) {
263
+ state.jumpBuffered = false;
264
+ state.jumpCount++;
265
+ state.jumpReleasedSinceLastJump = false;
266
+ state.timeSinceJump = 0;
267
+ state.jumpStartHeight = entity.body.translation().y;
268
+ state.jumping = true;
269
+ state.falling = false;
270
+ state.jumpCutApplied = false;
271
+ state.jumpCutRampTimer = 0;
272
+ state.verticalVelocity = movement.jumpForce;
273
+ state.jumpedThisFrame = true;
274
+ }
275
+ }
276
+ /**
277
+ * Integrate gravity into `state.verticalVelocity`.
278
+ *
279
+ * While grounded (and not on the jump frame) vertical velocity is held at
280
+ * a small constant downward speed so the controller stays pressed into
281
+ * the ground. Skipped entirely on the frame a jump was applied — the
282
+ * grounded flag is still stale-true then and would clobber the jump.
283
+ */
284
+ applyGravity(entity, delta) {
285
+ const movement = entity.platformer2D;
286
+ const state = entity.platformer2DState;
287
+ if (state.jumpedThisFrame) {
288
+ state.jumpedThisFrame = false;
289
+ return;
290
+ }
291
+ if (state.grounded) {
292
+ state.verticalVelocity = -GROUND_STICK_SPEED;
293
+ return;
294
+ }
295
+ state.verticalVelocity -= movement.gravity * delta;
296
+ }
297
+ /**
298
+ * Update entity state from the controller's grounded result.
299
+ */
300
+ updateState(entity, delta, computedGrounded) {
301
+ const state = entity.platformer2DState;
302
+ const wasGrounded = state.grounded;
303
+ const isAirborne = state.jumping || state.falling;
304
+ let isGrounded;
305
+ if (isAirborne) {
306
+ const canLand = state.falling && !state.jumping;
307
+ isGrounded = computedGrounded && canLand;
308
+ } else {
309
+ isGrounded = computedGrounded;
310
+ }
311
+ state.grounded = isGrounded;
312
+ if (state.grounded) {
313
+ state.timeSinceGrounded = 0;
314
+ state.lastGroundedY = entity.body.translation().y;
315
+ } else {
316
+ state.timeSinceGrounded += delta;
317
+ }
318
+ if (!wasGrounded && state.grounded) {
319
+ state.jumpCount = 0;
320
+ state.jumping = false;
321
+ state.falling = false;
322
+ state.jumpCutApplied = false;
323
+ state.jumpCutRampTimer = 0;
324
+ if (state.verticalVelocity < 0) {
325
+ state.verticalVelocity = 0;
326
+ }
327
+ }
328
+ if (state.verticalVelocity < FALL_VELOCITY_THRESHOLD && !state.grounded) {
329
+ if (state.jumping) {
330
+ state.jumping = false;
331
+ state.falling = true;
332
+ } else if (!state.jumping) {
333
+ state.falling = true;
334
+ }
335
+ }
336
+ }
337
+ /**
338
+ * Update one platformer entity. Invoked per linked entity by the
339
+ * Platformer2DBehaviorSystem adapter.
340
+ */
341
+ updateEntity(entity, delta) {
342
+ if (!entity.platformer2D || !entity.$platformer2D || !entity.platformer2DState) {
343
+ return;
344
+ }
345
+ const controller = this.ensurePhysicsConfigured(entity);
346
+ const collider = entity.collider;
347
+ if (!controller || !collider) return;
348
+ const input = entity.$platformer2D;
349
+ const movement = entity.platformer2D;
350
+ const state = entity.platformer2DState;
351
+ this.handleJump(entity, delta);
352
+ this.applyGravity(entity, delta);
353
+ const speed = input.run ? movement.runSpeed : movement.walkSpeed;
354
+ state.currentSpeed = speed;
355
+ _desired.x = input.moveX * speed * delta;
356
+ _desired.y = state.verticalVelocity * delta;
357
+ _desired.z = 0;
358
+ controller.computeColliderMovement(collider, _desired, QueryFilterFlags.EXCLUDE_SENSORS);
359
+ const corrected = controller.computedMovement();
360
+ if (_desired.y > 1e-9 && corrected.y < _desired.y * 0.5) {
361
+ state.verticalVelocity = 0;
362
+ }
363
+ const translation = entity.body.translation();
364
+ entity.body.setNextKinematicTranslation({
365
+ x: translation.x + corrected.x,
366
+ y: translation.y + corrected.y,
367
+ z: this.planeZ.get(entity.uuid) ?? translation.z
368
+ });
369
+ this.updateState(entity, delta, controller.computedGrounded());
370
+ }
371
+ destroy() {
372
+ const physicsWorld = this.world?.world;
373
+ if (physicsWorld) {
374
+ for (const controller of this.controllers.values()) {
375
+ physicsWorld.removeCharacterController(controller);
376
+ }
377
+ }
378
+ this.controllers.clear();
379
+ this.planeZ.clear();
380
+ }
381
+ };
382
+
383
+ // src/lib/behaviors/platformer-2d/platformer-2d.descriptor.ts
384
+ var FSM_STATE_BY_INDEX = [
385
+ "idle" /* Idle */,
386
+ "walking" /* Walking */,
387
+ "running" /* Running */,
388
+ "jumping" /* Jumping */,
389
+ "falling" /* Falling */,
390
+ "landing" /* Landing */
391
+ ];
392
+ var defaultOptions = {
393
+ walkSpeed: 8,
394
+ runSpeed: 14,
395
+ jumpForce: 12,
396
+ maxJumps: 1,
397
+ gravity: 20,
398
+ coyoteTime: 0.1,
399
+ jumpBufferTime: 0.1,
400
+ jumpCutMultiplier: 0.5,
401
+ slopeClimbAngle: 50,
402
+ slopeSlideAngle: 40,
403
+ autostep: { maxHeight: 0.25, minWidth: 0.05 },
404
+ snapToGroundDistance: 0.2,
405
+ characterOffset: 0.05
406
+ };
407
+ var PLATFORMER_2D_BEHAVIOR_KEY = /* @__PURE__ */ Symbol.for("zylem:behavior:platformer-2d");
408
+ var Platformer2DBehaviorSystem = class {
409
+ constructor(world, wasmStage, getBehaviorLinks) {
410
+ this.wasmStage = wasmStage;
411
+ this.getBehaviorLinks = getBehaviorLinks;
412
+ this.movementSystem = new Platformer2DSystem(world);
413
+ }
414
+ wasmStage;
415
+ getBehaviorLinks;
416
+ movementSystem;
417
+ attachedRuntimeSlots = /* @__PURE__ */ new Set();
418
+ update(_ecs, delta) {
419
+ const links = this.getBehaviorLinks?.(PLATFORMER_2D_BEHAVIOR_KEY);
420
+ if (!links) return;
421
+ for (const link of links) {
422
+ const gameEntity = link.entity;
423
+ const platformerRef = link.ref;
424
+ const options = platformerRef.options;
425
+ if (!gameEntity.platformer2D) {
426
+ gameEntity.platformer2D = createPlatformer2DMovementComponent(options);
427
+ }
428
+ if (!gameEntity.$platformer2D) {
429
+ gameEntity.$platformer2D = createPlatformer2DInputComponent();
430
+ }
431
+ if (!gameEntity.platformer2DState) {
432
+ gameEntity.platformer2DState = createPlatformer2DStateComponent();
433
+ }
434
+ const handle = gameEntity.runtimeHandle ?? -1;
435
+ if (this.wasmStage && handle >= 0) {
436
+ this.ensureWasmAttached(handle, options);
437
+ platformerRef.__wasmStage = this.wasmStage;
438
+ platformerRef.__entity = gameEntity;
439
+ const input = gameEntity.$platformer2D;
440
+ if (input) {
441
+ this.wasmStage.writeInputAxes(handle, input.moveX ?? 0, 0);
442
+ this.wasmStage.writeInputButton(handle, STAGE_INPUT_BUTTON_JUMP, !!input.jump);
443
+ this.wasmStage.writeInputButton(handle, STAGE_INPUT_BUTTON_RUN, !!input.run);
444
+ }
445
+ continue;
446
+ }
447
+ if (!platformerRef.fsm) {
448
+ platformerRef.fsm = new Platformer2DFSM({
449
+ input: gameEntity.$platformer2D,
450
+ state: gameEntity.platformer2DState
451
+ });
452
+ }
453
+ platformerRef.fsm.update(gameEntity.$platformer2D, gameEntity.platformer2DState);
454
+ if (!gameEntity.body) continue;
455
+ this.movementSystem.updateEntity(gameEntity, delta);
456
+ }
457
+ }
458
+ ensureWasmAttached(handle, options) {
459
+ if (!this.wasmStage || this.attachedRuntimeSlots.has(handle)) return;
460
+ const autostep = options.autostep ?? defaultOptions.autostep;
461
+ this.wasmStage.attachPlatformer2D(handle, {
462
+ halfHeight: 0.9,
463
+ radius: 0.4,
464
+ walkSpeed: options.walkSpeed ?? defaultOptions.walkSpeed,
465
+ runSpeed: options.runSpeed ?? defaultOptions.runSpeed,
466
+ jumpForce: options.jumpForce ?? defaultOptions.jumpForce,
467
+ maxJumps: options.maxJumps ?? defaultOptions.maxJumps,
468
+ gravity: options.gravity ?? defaultOptions.gravity,
469
+ coyoteTime: options.coyoteTime ?? defaultOptions.coyoteTime,
470
+ jumpBufferTime: options.jumpBufferTime ?? defaultOptions.jumpBufferTime,
471
+ jumpCutMultiplier: options.jumpCutMultiplier ?? defaultOptions.jumpCutMultiplier,
472
+ slopeClimbAngle: options.slopeClimbAngle ?? defaultOptions.slopeClimbAngle,
473
+ slopeSlideAngle: options.slopeSlideAngle ?? defaultOptions.slopeSlideAngle,
474
+ autostepHeight: autostep ? autostep.maxHeight : 0,
475
+ autostepMinWidth: autostep ? autostep.minWidth : 0.05,
476
+ snapToGroundDistance: options.snapToGroundDistance ?? defaultOptions.snapToGroundDistance,
477
+ characterOffset: options.characterOffset ?? defaultOptions.characterOffset
478
+ });
479
+ this.attachedRuntimeSlots.add(handle);
480
+ }
481
+ destroy(_ecs) {
482
+ this.movementSystem.destroy();
483
+ this.attachedRuntimeSlots.clear();
484
+ }
485
+ };
486
+ var Platformer2DBehavior = defineBehavior({
487
+ name: "platformer-2d",
488
+ defaultOptions,
489
+ systemFactory: (ctx) => new Platformer2DBehaviorSystem(
490
+ ctx.world ?? null,
491
+ ctx.wasmStage ?? null,
492
+ ctx.getBehaviorLinks
493
+ ),
494
+ // On the wasm fast path the handle reads the runtime snapshot buffer
495
+ // (values are from the end of the previous step — one-frame lag); the TS
496
+ // FSM only backs the no-wasm fallback path.
497
+ createHandle: (ref) => ({
498
+ getState: () => {
499
+ const snapshot = readBehaviorSnapshot(ref);
500
+ if (snapshot) return FSM_STATE_BY_INDEX[snapshot.fsmState] ?? "idle" /* Idle */;
501
+ return ref.fsm?.getState() ?? "idle" /* Idle */;
502
+ },
503
+ isGrounded: () => readBehaviorSnapshot(ref)?.grounded ?? ref.fsm?.isGrounded() ?? false,
504
+ getJumpCount: () => readBehaviorSnapshot(ref)?.jumpCount ?? ref.fsm?.getJumpCount() ?? 0
505
+ })
506
+ });
507
+
508
+ export {
509
+ createPlatformer2DMovementComponent,
510
+ createPlatformer2DInputComponent,
511
+ createPlatformer2DStateComponent,
512
+ Platformer2DState,
513
+ Platformer2DEvent,
514
+ Platformer2DFSM,
515
+ Platformer2DSystem,
516
+ Platformer2DBehavior
517
+ };
518
+ //# sourceMappingURL=chunk-UIG7MWS2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lib/behaviors/platformer-2d/components.ts","../src/lib/behaviors/platformer-2d/platformer-2d-fsm.ts","../src/lib/behaviors/platformer-2d/platformer-2d.system.ts","../src/lib/behaviors/platformer-2d/platformer-2d.descriptor.ts"],"sourcesContent":["/**\n * Platformer 2D Components\n *\n * Components for 2D platformer movement on the X/Y plane: walking/running on\n * X, jumping with multi-jump support, and falling/landing states.\n */\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Platformer2DMovementComponent (capability / configuration)\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface Platformer2DMovementComponent {\n\t/** Base walking speed */\n\twalkSpeed: number;\n\t/** Sprint/run speed */\n\trunSpeed: number;\n\t/** Initial jump force/velocity */\n\tjumpForce: number;\n\t/** Maximum number of jumps (1 = single jump, 2 = double jump, etc.) */\n\tmaxJumps: number;\n\t/** Gravity force */\n\tgravity: number;\n\t/** Coyote time in seconds (grace period after leaving ground) */\n\tcoyoteTime: number;\n\t/** Jump buffer time in seconds (queue jump input before landing) */\n\tjumpBufferTime: number;\n\t/** Velocity multiplier when releasing jump button early (0-1) */\n\tjumpCutMultiplier: number;\n\t/** Time in seconds before multi-jump becomes available (0 = after button release) */\n\tmultiJumpWindowTime: number;\n\t/** Maximum slope angle (degrees) the character can walk up */\n\tslopeClimbAngle: number;\n\t/** Slope angle (degrees) past which the character slides down */\n\tslopeSlideAngle: number;\n\t/** Autostep configuration for SNES-style step-ups, or false to disable */\n\tautostep: { maxHeight: number; minWidth: number } | false;\n\t/** Snap-to-ground distance (keeps the character glued going downhill) */\n\tsnapToGroundDistance: number;\n\t/** Character controller offset (small gap kept between collider and environment) */\n\tcharacterOffset: number;\n}\n\nexport function createPlatformer2DMovementComponent(\n\toptions: Partial<Platformer2DMovementComponent> = {},\n): Platformer2DMovementComponent {\n\treturn {\n\t\twalkSpeed: options.walkSpeed ?? 8,\n\t\trunSpeed: options.runSpeed ?? 14,\n\t\tjumpForce: options.jumpForce ?? 12,\n\t\tmaxJumps: options.maxJumps ?? 1,\n\t\tgravity: options.gravity ?? 20,\n\t\tcoyoteTime: options.coyoteTime ?? 0.1,\n\t\tjumpBufferTime: options.jumpBufferTime ?? 0.1,\n\t\tjumpCutMultiplier: options.jumpCutMultiplier ?? 0.5,\n\t\tmultiJumpWindowTime: options.multiJumpWindowTime ?? 0.15,\n\t\tslopeClimbAngle: options.slopeClimbAngle ?? 50,\n\t\tslopeSlideAngle: options.slopeSlideAngle ?? 40,\n\t\tautostep: options.autostep ?? { maxHeight: 0.25, minWidth: 0.05 },\n\t\tsnapToGroundDistance: options.snapToGroundDistance ?? 0.2,\n\t\tcharacterOffset: options.characterOffset ?? 0.05,\n\t};\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Platformer2DInputComponent (intent)\n// Written by: Player controller, AI controller\n// Read by: Platformer2DBehavior\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface Platformer2DInputComponent {\n\t/** Horizontal movement input (-1 to 1) */\n\tmoveX: number;\n\t/** Jump button pressed */\n\tjump: boolean;\n\t/** Run/sprint button held */\n\trun: boolean;\n}\n\nexport function createPlatformer2DInputComponent(): Platformer2DInputComponent {\n\treturn {\n\t\tmoveX: 0,\n\t\tjump: false,\n\t\trun: false,\n\t};\n}\n\n// ─────────────────────────────────────────────────────────────────────────────\n// Platformer2DStateComponent (runtime state)\n// ─────────────────────────────────────────────────────────────────────────────\n\nexport interface Platformer2DStateComponent {\n\t/** Is the entity on the ground */\n\tgrounded: boolean;\n\t/** Is currently in a jump arc */\n\tjumping: boolean;\n\t/** Is falling (not jumping) */\n\tfalling: boolean;\n\t/** Current jump count (for multi-jump) */\n\tjumpCount: number;\n\t/** Y position when jump started */\n\tjumpStartHeight: number;\n\t/** Current movement speed being applied */\n\tcurrentSpeed: number;\n\t/** Last known ground Y position */\n\tlastGroundedY: number;\n\t/** Was jump button pressed last frame (for edge detection) */\n\tjumpPressedLastFrame: boolean;\n\t/** Time in seconds since last grounded */\n\ttimeSinceGrounded: number;\n\t/** Is a jump allowed due to buffer? */\n\tjumpBuffered: boolean;\n\t/** Time remaining in jump buffer */\n\tjumpBufferTimer: number;\n\t/** Is jump button currently held */\n\tjumpHeld: boolean;\n\t/** Has the jump cut been applied for this jump? */\n\tjumpCutApplied: boolean;\n\t/** Has jump button been released since last jump (required for multi-jump) */\n\tjumpReleasedSinceLastJump: boolean;\n\t/** Time since current jump started (for multi-jump window) */\n\ttimeSinceJump: number;\n\t/** True for the single frame on which a jump impulse was applied; prevents gravity from clobbering jump velocity. */\n\tjumpedThisFrame: boolean;\n\t/** Remaining seconds of jump-cut velocity ramp; while > 0, verticalVelocity lerps toward verticalVelocity * jumpCutMultiplier. */\n\tjumpCutRampTimer: number;\n\t/**\n\t * Controller-owned vertical velocity (units/second).\n\t * The body is kinematic, so `linvel()` is meaningless — this field is the\n\t * single source of truth for jump/gravity motion on the Y axis.\n\t */\n\tverticalVelocity: number;\n}\n\nexport function createPlatformer2DStateComponent(): Platformer2DStateComponent {\n\treturn {\n\t\tgrounded: false,\n\t\tjumping: false,\n\t\tfalling: false,\n\t\tjumpCount: 0,\n\t\tjumpStartHeight: 0,\n\t\tcurrentSpeed: 0,\n\t\tlastGroundedY: 0,\n\t\tjumpPressedLastFrame: false,\n\t\ttimeSinceGrounded: 0,\n\t\tjumpBuffered: false,\n\t\tjumpBufferTimer: 0,\n\t\tjumpHeld: false,\n\t\tjumpCutApplied: false,\n\t\tjumpReleasedSinceLastJump: true,\n\t\ttimeSinceJump: 0,\n\t\tjumpedThisFrame: false,\n\t\tjumpCutRampTimer: 0,\n\t\tverticalVelocity: 0,\n\t};\n}\n","/**\n * Platformer 2D Finite State Machine\n *\n * Manages state transitions for 2D platformer movement:\n * - Idle: Standing still on ground\n * - Walking: Moving on ground (slow)\n * - Running: Moving on ground (fast)\n * - Jumping: Ascending from jump\n * - Falling: Descending (gravity)\n * - Landing: Just touched ground\n */\n\nimport { SyncStateMachine, t } from '../../core/utility/sync-state-machine';\nimport type { Platformer2DInputComponent, Platformer2DStateComponent } from './components';\n\n/**\n * Simplified collision context (normal in the X/Y plane).\n */\nexport interface Platformer2DCollisionContext {\n\tcontact: {\n\t\tnormal: { x: number; y: number };\n\t};\n}\n\nexport enum Platformer2DState {\n\tIdle = 'idle',\n\tWalking = 'walking',\n\tRunning = 'running',\n\tJumping = 'jumping',\n\tFalling = 'falling',\n\tLanding = 'landing',\n}\n\nexport enum Platformer2DEvent {\n\tWalk = 'walk',\n\tRun = 'run',\n\tJump = 'jump',\n\tFall = 'fall',\n\tLand = 'land',\n\tStop = 'stop',\n}\n\nexport interface Platformer2DContext {\n\tinput: Platformer2DInputComponent;\n\tstate: Platformer2DStateComponent;\n}\n\nexport class Platformer2DFSM {\n\tmachine: SyncStateMachine<Platformer2DState, Platformer2DEvent, never>;\n\n\tconstructor(private ctx: Platformer2DContext) {\n\t\tthis.machine = new SyncStateMachine<Platformer2DState, Platformer2DEvent, never>(\n\t\t\tPlatformer2DState.Idle,\n\t\t\t[\n\t\t\t\t// Idle transitions\n\t\t\t\tt(Platformer2DState.Idle, Platformer2DEvent.Walk, Platformer2DState.Walking),\n\t\t\t\tt(Platformer2DState.Idle, Platformer2DEvent.Run, Platformer2DState.Running),\n\t\t\t\tt(Platformer2DState.Idle, Platformer2DEvent.Jump, Platformer2DState.Jumping),\n\t\t\t\tt(Platformer2DState.Idle, Platformer2DEvent.Fall, Platformer2DState.Falling),\n\n\t\t\t\t// Walking transitions\n\t\t\t\tt(Platformer2DState.Walking, Platformer2DEvent.Run, Platformer2DState.Running),\n\t\t\t\tt(Platformer2DState.Walking, Platformer2DEvent.Jump, Platformer2DState.Jumping),\n\t\t\t\tt(Platformer2DState.Walking, Platformer2DEvent.Stop, Platformer2DState.Idle),\n\t\t\t\tt(Platformer2DState.Walking, Platformer2DEvent.Fall, Platformer2DState.Falling),\n\n\t\t\t\t// Running transitions\n\t\t\t\tt(Platformer2DState.Running, Platformer2DEvent.Walk, Platformer2DState.Walking),\n\t\t\t\tt(Platformer2DState.Running, Platformer2DEvent.Jump, Platformer2DState.Jumping),\n\t\t\t\tt(Platformer2DState.Running, Platformer2DEvent.Stop, Platformer2DState.Idle),\n\t\t\t\tt(Platformer2DState.Running, Platformer2DEvent.Fall, Platformer2DState.Falling),\n\n\t\t\t\t// Jumping transitions\n\t\t\t\tt(Platformer2DState.Jumping, Platformer2DEvent.Fall, Platformer2DState.Falling),\n\t\t\t\tt(Platformer2DState.Jumping, Platformer2DEvent.Land, Platformer2DState.Landing),\n\t\t\t\tt(Platformer2DState.Jumping, Platformer2DEvent.Jump, Platformer2DState.Jumping), // Multi-jump\n\n\t\t\t\t// Falling transitions\n\t\t\t\tt(Platformer2DState.Falling, Platformer2DEvent.Land, Platformer2DState.Landing),\n\t\t\t\tt(Platformer2DState.Falling, Platformer2DEvent.Jump, Platformer2DState.Jumping), // Air jump\n\n\t\t\t\t// Landing transitions\n\t\t\t\tt(Platformer2DState.Landing, Platformer2DEvent.Walk, Platformer2DState.Walking),\n\t\t\t\tt(Platformer2DState.Landing, Platformer2DEvent.Run, Platformer2DState.Running),\n\t\t\t\tt(Platformer2DState.Landing, Platformer2DEvent.Stop, Platformer2DState.Idle),\n\n\t\t\t\t// Self-transitions (no-ops)\n\t\t\t\tt(Platformer2DState.Idle, Platformer2DEvent.Stop, Platformer2DState.Idle),\n\t\t\t],\n\t\t);\n\t}\n\n\tgetState(): Platformer2DState {\n\t\treturn this.machine.getState();\n\t}\n\n\tdispatch(event: Platformer2DEvent): void {\n\t\tif (this.machine.can(event)) {\n\t\t\tthis.machine.syncDispatch(event);\n\t\t}\n\t}\n\n\tisGrounded(): boolean {\n\t\tconst state = this.getState();\n\t\treturn (\n\t\t\tstate === Platformer2DState.Idle ||\n\t\t\tstate === Platformer2DState.Walking ||\n\t\t\tstate === Platformer2DState.Running ||\n\t\t\tstate === Platformer2DState.Landing\n\t\t);\n\t}\n\n\tgetJumpCount(): number {\n\t\treturn this.ctx.state.jumpCount;\n\t}\n\n\t/**\n\t * Update FSM based on current component state.\n\t */\n\tupdate(input: Platformer2DInputComponent, state: Platformer2DStateComponent): void {\n\t\tthis.ctx.input = input;\n\t\tthis.ctx.state = state;\n\n\t\tconst currentState = this.getState();\n\t\tconst hasInput = Math.abs(input.moveX) > 0.1;\n\t\tconst isRunning = input.run;\n\n\t\t// Handle landing: if we're in falling state and now grounded, land\n\t\tif (currentState === Platformer2DState.Falling && state.grounded) {\n\t\t\tthis.dispatch(Platformer2DEvent.Land);\n\t\t\t// Don't return - allow immediate transition to movement state\n\t\t}\n\n\t\tif (state.grounded) {\n\t\t\tif (hasInput) {\n\t\t\t\tif (isRunning) {\n\t\t\t\t\tthis.dispatch(Platformer2DEvent.Run);\n\t\t\t\t} else {\n\t\t\t\t\tthis.dispatch(Platformer2DEvent.Walk);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthis.dispatch(Platformer2DEvent.Stop);\n\t\t\t}\n\t\t} else if (state.falling) {\n\t\t\tthis.dispatch(Platformer2DEvent.Fall);\n\t\t}\n\n\t\t// Handle jump input (edge detection handled in the system)\n\t\tif (input.jump && !state.jumpPressedLastFrame) {\n\t\t\tthis.dispatch(Platformer2DEvent.Jump);\n\t\t}\n\t}\n}\n","/**\n * Platformer 2D System\n *\n * Kinematic character controller (SNES-style) on the X/Y plane: the\n * controller owns all motion and physics is only a collision database.\n *\n * - The entity's body is converted to `KinematicPositionBased` on first\n * update; Rapier never applies forces to it.\n * - Each tick, desired displacement (X input + integrated vertical velocity,\n * z locked to 0) is run through Rapier's `KinematicCharacterController`\n * (collide-and-slide with autostep, slope limits, and snap-to-ground) and\n * the corrected movement is applied via `setNextKinematicTranslation`. The\n * z coordinate is re-pinned to its first-seen value every tick so slide\n * resolution can never drift the entity off the 2D plane.\n * - Grounded comes from `controller.computedGrounded()` — no contact-pair or\n * raycast probing.\n * - Vertical velocity lives in `state.verticalVelocity` (`linvel()` is\n * meaningless on kinematic bodies). Jump buffering, coyote time,\n * multi-jump, and the jump-cut ramp all operate on that field.\n */\n\nimport { RigidBodyType, QueryFilterFlags } from '@dimforge/rapier3d-compat';\nimport type { Collider, KinematicCharacterController, RigidBody } from '@dimforge/rapier3d-compat';\nimport type { ZylemWorld } from '../../collision/world';\nimport type {\n\tPlatformer2DMovementComponent,\n\tPlatformer2DInputComponent,\n\tPlatformer2DStateComponent,\n} from './components';\n\n/**\n * Duration of the jump-cut velocity ramp in seconds.\n * Instead of cutting vertical velocity in a single frame (which produces a\n * visible pop), we lerp toward the cut velocity across roughly 3 physics\n * steps at 60 Hz.\n */\nconst JUMP_CUT_RAMP_DURATION = 0.05;\n\n/**\n * Constant downward speed applied while grounded (units/second). Keeps the\n * controller pressed into the ground so `computedGrounded()` stays engaged on\n * flat ground and going downhill (snap-to-ground handles larger drops).\n */\nconst GROUND_STICK_SPEED = 2;\n\n/** Vertical speed below which the airborne state flips from jumping to falling. */\nconst FALL_VELOCITY_THRESHOLD = -0.1;\n\nconst DEG_TO_RAD = Math.PI / 180;\n\n/**\n * Entity with 2D platformer components\n */\nexport interface Platformer2DEntity {\n\tuuid: string;\n\tbody: RigidBody;\n\tcollider?: Collider | null;\n\tplatformer2D: Platformer2DMovementComponent;\n\t$platformer2D: Platformer2DInputComponent;\n\tplatformer2DState: Platformer2DStateComponent;\n}\n\n/** Scratch vector reused for the desired per-tick displacement. */\nconst _desired = { x: 0, y: 0, z: 0 };\n\n/**\n * Platformer 2D movement system.\n *\n * Core kinematic controller for 2D platformer movement. Requires a live\n * `ZylemWorld` with the raw Rapier `World` exposed on `.world` — without it\n * the system is a no-op.\n */\nexport class Platformer2DSystem {\n\tprivate world: ZylemWorld | null;\n\t/** Per-entity character controllers, keyed by entity uuid. */\n\tprivate controllers = new Map<string, KinematicCharacterController>();\n\t/** Spawn-time z per entity; re-pinned every tick to hold the 2D plane. */\n\tprivate planeZ = new Map<string, number>();\n\n\tconstructor(world: ZylemWorld | null) {\n\t\tthis.world = world;\n\t}\n\n\t/**\n\t * One-time per-entity physics setup: convert the body to a\n\t * position-based kinematic body, record the plane z, and create the\n\t * entity's character controller. Done lazily on the first update because\n\t * the body isn't always available at construction time.\n\t */\n\tprivate ensurePhysicsConfigured(entity: Platformer2DEntity): KinematicCharacterController | null {\n\t\tconst existing = this.controllers.get(entity.uuid);\n\t\tif (existing) return existing;\n\n\t\tconst physicsWorld = this.world?.world;\n\t\tif (!physicsWorld || !entity.body) return null;\n\n\t\tif (entity.body.bodyType() !== RigidBodyType.KinematicPositionBased) {\n\t\t\tentity.body.setBodyType(RigidBodyType.KinematicPositionBased, true);\n\t\t}\n\t\tthis.planeZ.set(entity.uuid, entity.body.translation().z);\n\n\t\tconst movement = entity.platformer2D;\n\t\tconst controller = physicsWorld.createCharacterController(movement.characterOffset);\n\t\tcontroller.setUp({ x: 0, y: 1, z: 0 });\n\t\tcontroller.setMaxSlopeClimbAngle(movement.slopeClimbAngle * DEG_TO_RAD);\n\t\tcontroller.setMinSlopeSlideAngle(movement.slopeSlideAngle * DEG_TO_RAD);\n\t\tif (movement.autostep) {\n\t\t\tcontroller.enableAutostep(movement.autostep.maxHeight, movement.autostep.minWidth, true);\n\t\t} else {\n\t\t\tcontroller.disableAutostep();\n\t\t}\n\t\tcontroller.enableSnapToGround(movement.snapToGroundDistance);\n\t\tcontroller.setApplyImpulsesToDynamicBodies(true);\n\n\t\tthis.controllers.set(entity.uuid, controller);\n\t\treturn controller;\n\t}\n\n\t/**\n\t * Handle jump logic with multi-jump, coyote time, buffering, and jump-cut\n\t * ramp. All vertical motion is written to `state.verticalVelocity`.\n\t */\n\tprivate handleJump(entity: Platformer2DEntity, delta: number): void {\n\t\tconst input = entity.$platformer2D;\n\t\tconst movement = entity.platformer2D;\n\t\tconst state = entity.platformer2DState;\n\n\t\t// Track time since last jump (for multi-jump window)\n\t\tif (state.jumping || state.falling) {\n\t\t\tstate.timeSinceJump += delta;\n\t\t}\n\n\t\t// Track jump button release (required for multi-jump)\n\t\tif (!input.jump && state.jumpHeld) {\n\t\t\tstate.jumpReleasedSinceLastJump = true;\n\t\t}\n\n\t\t// 1. Jump Buffering - queue on new press\n\t\tif (input.jump && !state.jumpPressedLastFrame) {\n\t\t\tstate.jumpBuffered = true;\n\t\t\tstate.jumpBufferTimer = movement.jumpBufferTime;\n\t\t}\n\n\t\tstate.jumpPressedLastFrame = input.jump;\n\t\tstate.jumpHeld = input.jump;\n\n\t\t// Decrement buffer timer\n\t\tif (state.jumpBuffered) {\n\t\t\tstate.jumpBufferTimer -= delta;\n\t\t\tif (state.jumpBufferTimer <= 0) {\n\t\t\t\tstate.jumpBuffered = false;\n\t\t\t}\n\t\t}\n\n\t\t// 2. Variable Jump Height (Jump Cut)\n\t\t// Only trigger a cut if we've been jumping for a bit (prevents cutting multi-jump immediately).\n\t\tconst minTimeBeforeCut = 0.1;\n\t\tconst canApplyCut = state.timeSinceJump >= minTimeBeforeCut;\n\t\tif (!input.jump && state.jumping && !state.jumpCutApplied && canApplyCut) {\n\t\t\tstate.jumpCutApplied = true;\n\t\t\tstate.jumpCutRampTimer = JUMP_CUT_RAMP_DURATION;\n\t\t}\n\n\t\t// 2a. Ramp verticalVelocity toward verticalVelocity * jumpCutMultiplier\n\t\t// across the ramp window so the change isn't a single-frame discontinuity.\n\t\tif (state.jumpCutRampTimer > 0) {\n\t\t\tif (state.verticalVelocity > 0) {\n\t\t\t\tconst stepT = Math.min(delta / state.jumpCutRampTimer, 1);\n\t\t\t\tconst targetY = state.verticalVelocity * movement.jumpCutMultiplier;\n\t\t\t\tstate.verticalVelocity += (targetY - state.verticalVelocity) * stepT;\n\t\t\t}\n\t\t\tstate.jumpCutRampTimer = Math.max(0, state.jumpCutRampTimer - delta);\n\t\t}\n\n\t\t// Execute Jump (if buffered input exists)\n\t\tif (!state.jumpBuffered) return;\n\n\t\t// 3. Jump eligibility check\n\t\tconst inCoyoteWindow = !state.grounded && state.timeSinceGrounded <= movement.coyoteTime;\n\t\tconst isFirstJump = state.grounded || (inCoyoteWindow && state.jumpCount === 0);\n\n\t\t// Multi-jump requirements:\n\t\t// 1. Not grounded\n\t\t// 2. Have jumps remaining\n\t\t// 3. Button was released since last jump (no holding for double jump)\n\t\t// 4. Within the jump window (after multiJumpWindowTime has passed)\n\t\tconst hasJumpsRemaining = state.jumpCount < movement.maxJumps;\n\t\tconst buttonReleased = state.jumpReleasedSinceLastJump;\n\t\tconst inMultiJumpWindow = state.timeSinceJump >= movement.multiJumpWindowTime;\n\t\tconst canMultiJump = !state.grounded && hasJumpsRemaining && buttonReleased && inMultiJumpWindow;\n\n\t\tif (isFirstJump || canMultiJump) {\n\t\t\t// Consume buffered input\n\t\t\tstate.jumpBuffered = false;\n\n\t\t\t// Increment jump count and reset tracking\n\t\t\tstate.jumpCount++;\n\t\t\tstate.jumpReleasedSinceLastJump = false; // Must release again for next jump\n\t\t\tstate.timeSinceJump = 0; // Reset window timer\n\n\t\t\t// Record jump start height\n\t\t\tstate.jumpStartHeight = entity.body.translation().y;\n\n\t\t\t// Apply jump force\n\t\t\tstate.jumping = true;\n\t\t\tstate.falling = false;\n\t\t\tstate.jumpCutApplied = false;\n\t\t\tstate.jumpCutRampTimer = 0;\n\n\t\t\tstate.verticalVelocity = movement.jumpForce;\n\n\t\t\t// Flag this frame so applyGravity skips exactly one grounded\n\t\t\t// clamp/gravity step (the jump velocity is authoritative).\n\t\t\tstate.jumpedThisFrame = true;\n\t\t}\n\t}\n\n\t/**\n\t * Integrate gravity into `state.verticalVelocity`.\n\t *\n\t * While grounded (and not on the jump frame) vertical velocity is held at\n\t * a small constant downward speed so the controller stays pressed into\n\t * the ground. Skipped entirely on the frame a jump was applied — the\n\t * grounded flag is still stale-true then and would clobber the jump.\n\t */\n\tprivate applyGravity(entity: Platformer2DEntity, delta: number): void {\n\t\tconst movement = entity.platformer2D;\n\t\tconst state = entity.platformer2DState;\n\n\t\tif (state.jumpedThisFrame) {\n\t\t\tstate.jumpedThisFrame = false;\n\t\t\treturn;\n\t\t}\n\n\t\tif (state.grounded) {\n\t\t\tstate.verticalVelocity = -GROUND_STICK_SPEED;\n\t\t\treturn;\n\t\t}\n\n\t\tstate.verticalVelocity -= movement.gravity * delta;\n\t}\n\n\t/**\n\t * Update entity state from the controller's grounded result.\n\t */\n\tprivate updateState(entity: Platformer2DEntity, delta: number, computedGrounded: boolean): void {\n\t\tconst state = entity.platformer2DState;\n\t\tconst wasGrounded = state.grounded;\n\n\t\tconst isAirborne = state.jumping || state.falling;\n\t\tlet isGrounded: boolean;\n\t\tif (isAirborne) {\n\t\t\t// Never land while still moving upward in a jump arc.\n\t\t\tconst canLand = state.falling && !state.jumping;\n\t\t\tisGrounded = computedGrounded && canLand;\n\t\t} else {\n\t\t\tisGrounded = computedGrounded;\n\t\t}\n\n\t\tstate.grounded = isGrounded;\n\n\t\tif (state.grounded) {\n\t\t\tstate.timeSinceGrounded = 0;\n\t\t\tstate.lastGroundedY = entity.body.translation().y;\n\t\t} else {\n\t\t\tstate.timeSinceGrounded += delta;\n\t\t}\n\n\t\tif (!wasGrounded && state.grounded) {\n\t\t\tstate.jumpCount = 0;\n\t\t\tstate.jumping = false;\n\t\t\tstate.falling = false;\n\t\t\tstate.jumpCutApplied = false;\n\t\t\tstate.jumpCutRampTimer = 0;\n\t\t\tif (state.verticalVelocity < 0) {\n\t\t\t\tstate.verticalVelocity = 0;\n\t\t\t}\n\t\t}\n\n\t\tif (state.verticalVelocity < FALL_VELOCITY_THRESHOLD && !state.grounded) {\n\t\t\tif (state.jumping) {\n\t\t\t\tstate.jumping = false;\n\t\t\t\tstate.falling = true;\n\t\t\t} else if (!state.jumping) {\n\t\t\t\tstate.falling = true;\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Update one platformer entity. Invoked per linked entity by the\n\t * Platformer2DBehaviorSystem adapter.\n\t */\n\tupdateEntity(entity: Platformer2DEntity, delta: number): void {\n\t\tif (!entity.platformer2D || !entity.$platformer2D || !entity.platformer2DState) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst controller = this.ensurePhysicsConfigured(entity);\n\t\tconst collider = entity.collider;\n\t\tif (!controller || !collider) return;\n\n\t\tconst input = entity.$platformer2D;\n\t\tconst movement = entity.platformer2D;\n\t\tconst state = entity.platformer2DState;\n\n\t\t// 1. Jump logic (may set verticalVelocity = jumpForce this frame)\n\t\tthis.handleJump(entity, delta);\n\n\t\t// 2. Gravity / grounded stick on verticalVelocity\n\t\tthis.applyGravity(entity, delta);\n\n\t\t// 3. Desired displacement for this tick (z locked to the plane)\n\t\tconst speed = input.run ? movement.runSpeed : movement.walkSpeed;\n\t\tstate.currentSpeed = speed;\n\t\t_desired.x = input.moveX * speed * delta;\n\t\t_desired.y = state.verticalVelocity * delta;\n\t\t_desired.z = 0;\n\n\t\t// 4. Collide-and-slide\n\t\tcontroller.computeColliderMovement(collider, _desired, QueryFilterFlags.EXCLUDE_SENSORS);\n\t\tconst corrected = controller.computedMovement();\n\n\t\t// Head bump: upward motion was blocked, so kill the remaining ascent\n\t\t// instead of hanging under the ceiling for the rest of the arc.\n\t\tif (_desired.y > 1e-9 && corrected.y < _desired.y * 0.5) {\n\t\t\tstate.verticalVelocity = 0;\n\t\t}\n\n\t\tconst translation = entity.body.translation();\n\t\tentity.body.setNextKinematicTranslation({\n\t\t\tx: translation.x + corrected.x,\n\t\t\ty: translation.y + corrected.y,\n\t\t\tz: this.planeZ.get(entity.uuid) ?? translation.z,\n\t\t});\n\n\t\t// 5. State transitions from the controller's grounded result\n\t\tthis.updateState(entity, delta, controller.computedGrounded());\n\t}\n\n\tdestroy(): void {\n\t\tconst physicsWorld = this.world?.world;\n\t\tif (physicsWorld) {\n\t\t\tfor (const controller of this.controllers.values()) {\n\t\t\t\tphysicsWorld.removeCharacterController(controller);\n\t\t\t}\n\t\t}\n\t\tthis.controllers.clear();\n\t\tthis.planeZ.clear();\n\t}\n}\n","/**\n * Platformer 2D Behavior Descriptor\n *\n * Type-safe descriptor for the platformer 2D behavior system.\n * Provides entity.use() API for 2D platformer movement on the X/Y plane.\n */\n\nimport { defineBehavior } from '../behavior-descriptor';\nimport type { BehaviorEntityLink, BehaviorSystem } from '../behavior-system';\nimport { Platformer2DSystem, Platformer2DEntity } from './platformer-2d.system';\nimport type { ZylemWorld } from '../../collision/world';\nimport { Platformer2DFSM, Platformer2DState } from './platformer-2d-fsm';\nimport {\n\tcreatePlatformer2DMovementComponent,\n\tcreatePlatformer2DInputComponent,\n\tcreatePlatformer2DStateComponent,\n} from './components';\nimport {\n\tSTAGE_INPUT_BUTTON_JUMP,\n\tSTAGE_INPUT_BUTTON_RUN,\n\treadBehaviorSnapshot,\n\ttype WasmStageRuntime,\n} from '../../runtime/wasm-stage-runtime';\n\n/** Rust FSM index → TS state (shared `platformer_3d/fsm.rs` ordering). */\nconst FSM_STATE_BY_INDEX: readonly Platformer2DState[] = [\n\tPlatformer2DState.Idle,\n\tPlatformer2DState.Walking,\n\tPlatformer2DState.Running,\n\tPlatformer2DState.Jumping,\n\tPlatformer2DState.Falling,\n\tPlatformer2DState.Landing,\n];\n\n/**\n * Platformer 2D behavior options (typed for entity.use() autocomplete)\n */\nexport interface Platformer2DBehaviorOptions {\n\t/** Base walking speed (default: 8) */\n\twalkSpeed?: number;\n\t/** Sprint/run speed (default: 14) */\n\trunSpeed?: number;\n\t/** Initial jump force (default: 12) */\n\tjumpForce?: number;\n\t/** Maximum number of jumps (default: 1) */\n\tmaxJumps?: number;\n\t/** Gravity force (default: 20) */\n\tgravity?: number;\n\t/** Coyote time in seconds (default: 0.1) */\n\tcoyoteTime?: number;\n\t/** Jump buffer time in seconds (default: 0.1) */\n\tjumpBufferTime?: number;\n\t/** Velocity multiplier when releasing jump early (default: 0.5) */\n\tjumpCutMultiplier?: number;\n\t/** Maximum slope angle in degrees the character can walk up (default: 50) */\n\tslopeClimbAngle?: number;\n\t/** Slope angle in degrees past which the character slides (default: 40) */\n\tslopeSlideAngle?: number;\n\t/** Autostep over small step-ups, or false to disable (default: { maxHeight: 0.25, minWidth: 0.05 }) */\n\tautostep?: { maxHeight: number; minWidth: number } | false;\n\t/** Snap-to-ground distance for downhill/step-down stability (default: 0.2) */\n\tsnapToGroundDistance?: number;\n\t/** Character controller collision offset (default: 0.05) */\n\tcharacterOffset?: number;\n}\n\nconst defaultOptions: Platformer2DBehaviorOptions = {\n\twalkSpeed: 8,\n\trunSpeed: 14,\n\tjumpForce: 12,\n\tmaxJumps: 1,\n\tgravity: 20,\n\tcoyoteTime: 0.1,\n\tjumpBufferTime: 0.1,\n\tjumpCutMultiplier: 0.5,\n\tslopeClimbAngle: 50,\n\tslopeSlideAngle: 40,\n\tautostep: { maxHeight: 0.25, minWidth: 0.05 },\n\tsnapToGroundDistance: 0.2,\n\tcharacterOffset: 0.05,\n};\n\nconst PLATFORMER_2D_BEHAVIOR_KEY = Symbol.for('zylem:behavior:platformer-2d');\n\n/**\n * Adapter that wraps Platformer2DSystem as a BehaviorSystem.\n *\n * Mirrors the platformer-3d pattern for the wasm fast path: entities with a\n * `runtimeHandle` are attached once to the Stage runtime, receive input via\n * the stage setters each frame, and skip the TS `updateEntity` path.\n */\nclass Platformer2DBehaviorSystem implements BehaviorSystem {\n\tprivate movementSystem: Platformer2DSystem;\n\tprivate attachedRuntimeSlots = new Set<number>();\n\n\tconstructor(\n\t\tworld: ZylemWorld | null,\n\t\tprivate wasmStage: WasmStageRuntime | null,\n\t\tprivate getBehaviorLinks?: (key: symbol) => Iterable<BehaviorEntityLink>,\n\t) {\n\t\tthis.movementSystem = new Platformer2DSystem(world);\n\t}\n\n\tupdate(_ecs: unknown, delta: number): void {\n\t\tconst links = this.getBehaviorLinks?.(PLATFORMER_2D_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 platformerRef = link.ref as any;\n\n\t\t\tconst options = platformerRef.options as Platformer2DBehaviorOptions;\n\n\t\t\tif (!gameEntity.platformer2D) {\n\t\t\t\tgameEntity.platformer2D = createPlatformer2DMovementComponent(options);\n\t\t\t}\n\t\t\tif (!gameEntity.$platformer2D) {\n\t\t\t\tgameEntity.$platformer2D = createPlatformer2DInputComponent();\n\t\t\t}\n\t\t\tif (!gameEntity.platformer2DState) {\n\t\t\t\tgameEntity.platformer2DState = createPlatformer2DStateComponent();\n\t\t\t}\n\n\t\t\t// Wasm fast path: forward input into the batched buffer and let\n\t\t\t// handles read the Rust FSM via the snapshot buffer — the TS FSM\n\t\t\t// is never created (single source of truth in the runtime).\n\t\t\tconst handle = (gameEntity.runtimeHandle ?? -1) as number;\n\t\t\tif (this.wasmStage && handle >= 0) {\n\t\t\t\tthis.ensureWasmAttached(handle, options);\n\t\t\t\tplatformerRef.__wasmStage = this.wasmStage;\n\t\t\t\tplatformerRef.__entity = gameEntity;\n\t\t\t\tconst input = gameEntity.$platformer2D;\n\t\t\t\tif (input) {\n\t\t\t\t\tthis.wasmStage.writeInputAxes(handle, input.moveX ?? 0, 0);\n\t\t\t\t\tthis.wasmStage.writeInputButton(handle, STAGE_INPUT_BUTTON_JUMP, !!input.jump);\n\t\t\t\t\tthis.wasmStage.writeInputButton(handle, STAGE_INPUT_BUTTON_RUN, !!input.run);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (!platformerRef.fsm) {\n\t\t\t\tplatformerRef.fsm = new Platformer2DFSM({\n\t\t\t\t\tinput: gameEntity.$platformer2D,\n\t\t\t\t\tstate: gameEntity.platformer2DState,\n\t\t\t\t});\n\t\t\t}\n\t\t\tplatformerRef.fsm.update(gameEntity.$platformer2D, gameEntity.platformer2DState);\n\n\t\t\tif (!gameEntity.body) continue;\n\t\t\tthis.movementSystem.updateEntity(gameEntity, delta);\n\t\t}\n\t}\n\n\tprivate ensureWasmAttached(handle: number, options: Platformer2DBehaviorOptions): void {\n\t\tif (!this.wasmStage || this.attachedRuntimeSlots.has(handle)) return;\n\t\tconst autostep = options.autostep ?? defaultOptions.autostep!;\n\t\tthis.wasmStage.attachPlatformer2D(handle, {\n\t\t\thalfHeight: 0.9,\n\t\t\tradius: 0.4,\n\t\t\twalkSpeed: options.walkSpeed ?? defaultOptions.walkSpeed!,\n\t\t\trunSpeed: options.runSpeed ?? defaultOptions.runSpeed!,\n\t\t\tjumpForce: options.jumpForce ?? defaultOptions.jumpForce!,\n\t\t\tmaxJumps: options.maxJumps ?? defaultOptions.maxJumps!,\n\t\t\tgravity: options.gravity ?? defaultOptions.gravity!,\n\t\t\tcoyoteTime: options.coyoteTime ?? defaultOptions.coyoteTime!,\n\t\t\tjumpBufferTime: options.jumpBufferTime ?? defaultOptions.jumpBufferTime!,\n\t\t\tjumpCutMultiplier: options.jumpCutMultiplier ?? defaultOptions.jumpCutMultiplier!,\n\t\t\tslopeClimbAngle: options.slopeClimbAngle ?? defaultOptions.slopeClimbAngle!,\n\t\t\tslopeSlideAngle: options.slopeSlideAngle ?? defaultOptions.slopeSlideAngle!,\n\t\t\tautostepHeight: autostep ? autostep.maxHeight : 0,\n\t\t\tautostepMinWidth: autostep ? autostep.minWidth : 0.05,\n\t\t\tsnapToGroundDistance: options.snapToGroundDistance ?? defaultOptions.snapToGroundDistance!,\n\t\t\tcharacterOffset: options.characterOffset ?? defaultOptions.characterOffset!,\n\t\t});\n\t\tthis.attachedRuntimeSlots.add(handle);\n\t}\n\n\tdestroy(_ecs: unknown): void {\n\t\tthis.movementSystem.destroy();\n\t\tthis.attachedRuntimeSlots.clear();\n\t}\n}\n\n/**\n * Platformer2DBehavior - typed descriptor for 2D platformer movement\n *\n * Provides complete 2D platformer physics on the X/Y plane including:\n * - Walking and running on X\n * - Jumping with buffer, coyote time, multi-jump, and jump-cut\n * - Gravity ownership and falling\n * - Kinematic character controller (collide-and-slide, autostep, snap-to-ground)\n *\n * @example\n * ```typescript\n * import { Platformer2DBehavior } from \"@zylem/behaviors/platformer-2d\";\n *\n * const platformer = player.use(Platformer2DBehavior, {\n * walkSpeed: 8,\n * runSpeed: 14,\n * jumpForce: 12,\n * maxJumps: 2, // Double jump!\n * });\n *\n * // In update loop\n * player.onUpdate(({ inputs }) => {\n * player.$platformer2D.moveX = inputs.p1.axes.Horizontal.value;\n * player.$platformer2D.jump = inputs.p1.buttons.A.held > 0;\n * player.$platformer2D.run = inputs.p1.shoulders.LTrigger.held > 0;\n *\n * const state = platformer.getState();\n * const grounded = platformer.isGrounded();\n * });\n * ```\n */\nexport const Platformer2DBehavior = defineBehavior<\n\tPlatformer2DBehaviorOptions,\n\t{\n\t\tgetState: () => Platformer2DState;\n\t\tisGrounded: () => boolean;\n\t\tgetJumpCount: () => number;\n\t},\n\tPlatformer2DEntity\n>({\n\tname: 'platformer-2d',\n\tdefaultOptions,\n\tsystemFactory: (ctx) =>\n\t\tnew Platformer2DBehaviorSystem(\n\t\t\tctx.world ?? null,\n\t\t\tctx.wasmStage ?? null,\n\t\t\tctx.getBehaviorLinks,\n\t\t),\n\t// On the wasm fast path the handle reads the runtime snapshot buffer\n\t// (values are from the end of the previous step — one-frame lag); the TS\n\t// FSM only backs the no-wasm fallback path.\n\tcreateHandle: (ref) => ({\n\t\tgetState: () => {\n\t\t\tconst snapshot = readBehaviorSnapshot(ref);\n\t\t\tif (snapshot) return FSM_STATE_BY_INDEX[snapshot.fsmState] ?? Platformer2DState.Idle;\n\t\t\treturn ref.fsm?.getState() ?? Platformer2DState.Idle;\n\t\t},\n\t\tisGrounded: () =>\n\t\t\treadBehaviorSnapshot(ref)?.grounded ?? ref.fsm?.isGrounded() ?? false,\n\t\tgetJumpCount: () =>\n\t\t\treadBehaviorSnapshot(ref)?.jumpCount ?? ref.fsm?.getJumpCount() ?? 0,\n\t}),\n});\n"],"mappings":";;;;;;;;;;;;;;AA0CO,SAAS,oCACf,UAAkD,CAAC,GACnB;AAChC,SAAO;AAAA,IACN,WAAW,QAAQ,aAAa;AAAA,IAChC,UAAU,QAAQ,YAAY;AAAA,IAC9B,WAAW,QAAQ,aAAa;AAAA,IAChC,UAAU,QAAQ,YAAY;AAAA,IAC9B,SAAS,QAAQ,WAAW;AAAA,IAC5B,YAAY,QAAQ,cAAc;AAAA,IAClC,gBAAgB,QAAQ,kBAAkB;AAAA,IAC1C,mBAAmB,QAAQ,qBAAqB;AAAA,IAChD,qBAAqB,QAAQ,uBAAuB;AAAA,IACpD,iBAAiB,QAAQ,mBAAmB;AAAA,IAC5C,iBAAiB,QAAQ,mBAAmB;AAAA,IAC5C,UAAU,QAAQ,YAAY,EAAE,WAAW,MAAM,UAAU,KAAK;AAAA,IAChE,sBAAsB,QAAQ,wBAAwB;AAAA,IACtD,iBAAiB,QAAQ,mBAAmB;AAAA,EAC7C;AACD;AAiBO,SAAS,mCAA+D;AAC9E,SAAO;AAAA,IACN,OAAO;AAAA,IACP,MAAM;AAAA,IACN,KAAK;AAAA,EACN;AACD;AAiDO,SAAS,mCAA+D;AAC9E,SAAO;AAAA,IACN,UAAU;AAAA,IACV,SAAS;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U,QAAO,mBAAmB,SAAS,QAAQ;AACzD,aAAO,IAAI,KAAK,SAAS;AAAA,IAC1B;AAAA,IACA,YAAY,MACX,qBAAqB,GAAG,GAAG,YAAY,IAAI,KAAK,WAAW,KAAK;AAAA,IACjE,cAAc,MACb,qBAAqB,GAAG,GAAG,aAAa,IAAI,KAAK,aAAa,KAAK;AAAA,EACrE;AACD,CAAC;","names":["Platformer2DState","Platformer2DEvent"]}
@@ -3,7 +3,7 @@ import {
3
3
  directionFromAngle2D,
4
4
  getRotationAngle2D,
5
5
  normalizeDirection2D
6
- } from "./chunk-ID5QJYF5.js";
6
+ } from "./chunk-Y7YIAWPW.js";
7
7
 
8
8
  // src/lib/coordinators/shared/top-down-aim.ts
9
9
  function getEntityFacingAngle2D(entity) {
@@ -31,8 +31,30 @@ function resolveAim2D(aimX, aimY, fallbackAngle) {
31
31
  };
32
32
  }
33
33
 
34
+ // src/lib/coordinators/shared/shooter-fire.ts
35
+ function fireShooter2DIfRequested(entity, shooter, stage, input, aim) {
36
+ if (!input.shootPressed && !input.shootHeld) {
37
+ return false;
38
+ }
39
+ const sourcePosition = entity.body?.translation();
40
+ if (!sourcePosition) {
41
+ return false;
42
+ }
43
+ const resolved = aim ?? resolveAim2D(input.aimX, input.aimY, getEntityFacingAngle2D(entity));
44
+ void shooter.fire({
45
+ source: entity,
46
+ stage,
47
+ target: {
48
+ x: sourcePosition.x + resolved.direction.x,
49
+ y: sourcePosition.y + resolved.direction.y
50
+ }
51
+ });
52
+ return true;
53
+ }
54
+
34
55
  export {
35
56
  getEntityFacingAngle2D,
36
- resolveAim2D
57
+ resolveAim2D,
58
+ fireShooter2DIfRequested
37
59
  };
38
- //# sourceMappingURL=chunk-6C6NXNEW.js.map
60
+ //# sourceMappingURL=chunk-UL6Q5HDP.js.map