@zylem/game-lib 0.6.2 → 0.6.4
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/LICENSE +21 -0
- package/README.md +9 -16
- package/dist/actions.d.ts +30 -21
- package/dist/actions.js +793 -146
- package/dist/actions.js.map +1 -1
- package/dist/behavior/jumper-2d.d.ts +114 -0
- package/dist/behavior/jumper-2d.js +711 -0
- package/dist/behavior/jumper-2d.js.map +1 -0
- package/dist/behavior/platformer-3d.d.ts +296 -0
- package/dist/behavior/platformer-3d.js +761 -0
- package/dist/behavior/platformer-3d.js.map +1 -0
- package/dist/behavior/ricochet-2d.d.ts +275 -0
- package/dist/behavior/ricochet-2d.js +425 -0
- package/dist/behavior/ricochet-2d.js.map +1 -0
- package/dist/behavior/ricochet-3d.d.ts +117 -0
- package/dist/behavior/ricochet-3d.js +443 -0
- package/dist/behavior/ricochet-3d.js.map +1 -0
- package/dist/behavior/screen-visibility.d.ts +79 -0
- package/dist/behavior/screen-visibility.js +358 -0
- package/dist/behavior/screen-visibility.js.map +1 -0
- package/dist/behavior/screen-wrap.d.ts +87 -0
- package/dist/behavior/screen-wrap.js +246 -0
- package/dist/behavior/screen-wrap.js.map +1 -0
- package/dist/behavior/shooter-2d.d.ts +79 -0
- package/dist/behavior/shooter-2d.js +180 -0
- package/dist/behavior/shooter-2d.js.map +1 -0
- package/dist/behavior/thruster.d.ts +11 -0
- package/dist/behavior/thruster.js +292 -0
- package/dist/behavior/thruster.js.map +1 -0
- package/dist/behavior/top-down-movement.d.ts +56 -0
- package/dist/behavior/top-down-movement.js +125 -0
- package/dist/behavior/top-down-movement.js.map +1 -0
- package/dist/behavior/world-boundary-2d.d.ts +142 -0
- package/dist/behavior/world-boundary-2d.js +235 -0
- package/dist/behavior/world-boundary-2d.js.map +1 -0
- package/dist/behavior/world-boundary-3d.d.ts +76 -0
- package/dist/behavior/world-boundary-3d.js +274 -0
- package/dist/behavior/world-boundary-3d.js.map +1 -0
- package/dist/behavior-descriptor-BXnVR8Ki.d.ts +159 -0
- package/dist/{blueprints-Cq3Ko6_G.d.ts → blueprints-DmbK2dki.d.ts} +2 -2
- package/dist/camera-4XO5gbQH.d.ts +905 -0
- package/dist/camera.d.ts +3 -2
- package/dist/camera.js +1653 -377
- package/dist/camera.js.map +1 -1
- package/dist/composition-BASvMKrW.d.ts +218 -0
- package/dist/{core-bO8TzV7u.d.ts → core-CARRaS55.d.ts} +110 -69
- package/dist/core.d.ts +11 -6
- package/dist/core.js +10766 -5626
- package/dist/core.js.map +1 -1
- package/dist/{entities-DvByhMGU.d.ts → entities-ChFirVL9.d.ts} +133 -29
- package/dist/entities.d.ts +5 -3
- package/dist/entities.js +4679 -3202
- package/dist/entities.js.map +1 -1
- package/dist/entity-vj-HTjzU.d.ts +1169 -0
- package/dist/global-change-2JvMaz44.d.ts +25 -0
- package/dist/main.d.ts +1118 -16
- package/dist/main.js +17538 -8499
- package/dist/main.js.map +1 -1
- package/dist/physics-pose-DCc4oE44.d.ts +25 -0
- package/dist/physics-protocol-BDD3P5W2.d.ts +200 -0
- package/dist/physics-worker.d.ts +21 -0
- package/dist/physics-worker.js +306 -0
- package/dist/physics-worker.js.map +1 -0
- package/dist/physics.d.ts +205 -0
- package/dist/physics.js +577 -0
- package/dist/physics.js.map +1 -0
- package/dist/stage-types-C19IhuzA.d.ts +731 -0
- package/dist/stage.d.ts +11 -7
- package/dist/stage.js +8024 -3852
- package/dist/stage.js.map +1 -1
- package/dist/sync-state-machine-CZyspBpj.d.ts +16 -0
- package/dist/thruster-23lzoPZd.d.ts +180 -0
- package/dist/world-DfgxoNMt.d.ts +105 -0
- package/package.json +53 -13
- package/dist/behaviors.d.ts +0 -854
- package/dist/behaviors.js +0 -1209
- package/dist/behaviors.js.map +0 -1
- package/dist/camera-CeJPAgGg.d.ts +0 -116
- package/dist/moveable-B_vyA6cw.d.ts +0 -67
- package/dist/stage-types-Bd-KtcYT.d.ts +0 -375
- package/dist/transformable-CUhvyuYO.d.ts +0 -67
- package/dist/world-C8tQ7Plj.d.ts +0 -774
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
// src/lib/core/utility/sync-state-machine.ts
|
|
2
|
+
import {
|
|
3
|
+
StateMachine as BaseStateMachine
|
|
4
|
+
} from "typescript-fsm";
|
|
5
|
+
import { t } from "typescript-fsm";
|
|
6
|
+
var SyncStateMachine = class extends BaseStateMachine {
|
|
7
|
+
constructor(init, transitions = [], logger = console) {
|
|
8
|
+
super(init, transitions, logger);
|
|
9
|
+
}
|
|
10
|
+
dispatch(_event, ..._args) {
|
|
11
|
+
throw new Error("SyncStateMachine does not support async dispatch.");
|
|
12
|
+
}
|
|
13
|
+
syncDispatch(event, ...args) {
|
|
14
|
+
const found = this.transitions.some((transition) => {
|
|
15
|
+
if (transition.fromState !== this._current || transition.event !== event) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const current = this._current;
|
|
19
|
+
this._current = transition.toState;
|
|
20
|
+
if (!transition.cb) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
transition.cb(...args);
|
|
25
|
+
return true;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
this._current = current;
|
|
28
|
+
this.logger.error("Exception in callback", error);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
if (!found) {
|
|
33
|
+
const errorMessage = this.formatErr(this._current, event);
|
|
34
|
+
this.logger.error(errorMessage);
|
|
35
|
+
}
|
|
36
|
+
return found;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/lib/behaviors/ricochet-2d/ricochet-2d-fsm.ts
|
|
41
|
+
var Ricochet2DState = /* @__PURE__ */ ((Ricochet2DState2) => {
|
|
42
|
+
Ricochet2DState2["Idle"] = "idle";
|
|
43
|
+
Ricochet2DState2["Ricocheting"] = "ricocheting";
|
|
44
|
+
return Ricochet2DState2;
|
|
45
|
+
})(Ricochet2DState || {});
|
|
46
|
+
var Ricochet2DEvent = /* @__PURE__ */ ((Ricochet2DEvent2) => {
|
|
47
|
+
Ricochet2DEvent2["StartRicochet"] = "start-ricochet";
|
|
48
|
+
Ricochet2DEvent2["EndRicochet"] = "end-ricochet";
|
|
49
|
+
return Ricochet2DEvent2;
|
|
50
|
+
})(Ricochet2DEvent || {});
|
|
51
|
+
function clamp(value, min, max) {
|
|
52
|
+
return Math.max(min, Math.min(max, value));
|
|
53
|
+
}
|
|
54
|
+
var Ricochet2DFSM = class {
|
|
55
|
+
machine;
|
|
56
|
+
lastResult = null;
|
|
57
|
+
lastUpdatedAtMs = null;
|
|
58
|
+
currentTimeMs = 0;
|
|
59
|
+
listeners = /* @__PURE__ */ new Set();
|
|
60
|
+
constructor() {
|
|
61
|
+
this.machine = new SyncStateMachine(
|
|
62
|
+
"idle" /* Idle */,
|
|
63
|
+
[
|
|
64
|
+
t("idle" /* Idle */, "start-ricochet" /* StartRicochet */, "ricocheting" /* Ricocheting */),
|
|
65
|
+
t("ricocheting" /* Ricocheting */, "end-ricochet" /* EndRicochet */, "idle" /* Idle */),
|
|
66
|
+
// Self transitions (no-ops)
|
|
67
|
+
t("idle" /* Idle */, "end-ricochet" /* EndRicochet */, "idle" /* Idle */),
|
|
68
|
+
t("ricocheting" /* Ricocheting */, "start-ricochet" /* StartRicochet */, "ricocheting" /* Ricocheting */)
|
|
69
|
+
]
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Add a listener for ricochet events.
|
|
74
|
+
* @returns Unsubscribe function
|
|
75
|
+
*/
|
|
76
|
+
addListener(callback) {
|
|
77
|
+
this.listeners.add(callback);
|
|
78
|
+
return () => this.listeners.delete(callback);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Emit result to all listeners.
|
|
82
|
+
*/
|
|
83
|
+
emitToListeners(result) {
|
|
84
|
+
for (const callback of this.listeners) {
|
|
85
|
+
try {
|
|
86
|
+
callback(result);
|
|
87
|
+
} catch (e) {
|
|
88
|
+
console.error("[Ricochet2DFSM] Listener error:", e);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
getState() {
|
|
93
|
+
return this.machine.getState();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Returns the last computed ricochet result, or null if none.
|
|
97
|
+
*/
|
|
98
|
+
getLastResult() {
|
|
99
|
+
return this.lastResult;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Best-effort timestamp (ms) of the last computation.
|
|
103
|
+
*/
|
|
104
|
+
getLastUpdatedAtMs() {
|
|
105
|
+
return this.lastUpdatedAtMs;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Set current game time (called by system each frame).
|
|
109
|
+
* Used for cooldown calculations.
|
|
110
|
+
*/
|
|
111
|
+
setCurrentTimeMs(timeMs) {
|
|
112
|
+
this.currentTimeMs = timeMs;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Check if ricochet is on cooldown (to prevent rapid duplicate applications).
|
|
116
|
+
* @param cooldownMs Cooldown duration in milliseconds (default: 50ms)
|
|
117
|
+
*/
|
|
118
|
+
isOnCooldown(cooldownMs = 50) {
|
|
119
|
+
if (this.lastUpdatedAtMs === null) return false;
|
|
120
|
+
return this.currentTimeMs - this.lastUpdatedAtMs < cooldownMs;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Reset cooldown state (e.g., on entity respawn).
|
|
124
|
+
*/
|
|
125
|
+
resetCooldown() {
|
|
126
|
+
this.lastUpdatedAtMs = null;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Compute a ricochet result from collision context.
|
|
130
|
+
* Returns the result for the consumer to apply, or null if invalid input.
|
|
131
|
+
*/
|
|
132
|
+
computeRicochet(ctx, options = {}) {
|
|
133
|
+
const {
|
|
134
|
+
minSpeed = 2,
|
|
135
|
+
maxSpeed = 20,
|
|
136
|
+
speedMultiplier = 1.05,
|
|
137
|
+
reflectionMode = "angled",
|
|
138
|
+
maxAngleDeg = 60
|
|
139
|
+
} = options;
|
|
140
|
+
const { selfVelocity, selfPosition, otherPosition, otherSize } = this.extractDataFromEntities(ctx);
|
|
141
|
+
if (!selfVelocity) {
|
|
142
|
+
this.dispatch("end-ricochet" /* EndRicochet */);
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
const speed = Math.hypot(selfVelocity.x, selfVelocity.y);
|
|
146
|
+
if (speed === 0) {
|
|
147
|
+
this.dispatch("end-ricochet" /* EndRicochet */);
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const normal = ctx.contact.normal ?? this.computeNormalFromPositions(selfPosition, otherPosition);
|
|
151
|
+
if (!normal) {
|
|
152
|
+
this.dispatch("end-ricochet" /* EndRicochet */);
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
let reflected = this.computeBasicReflection(selfVelocity, normal);
|
|
156
|
+
if (reflectionMode === "angled") {
|
|
157
|
+
reflected = this.computeAngledDeflection(
|
|
158
|
+
selfVelocity,
|
|
159
|
+
normal,
|
|
160
|
+
speed,
|
|
161
|
+
maxAngleDeg,
|
|
162
|
+
speedMultiplier,
|
|
163
|
+
selfPosition,
|
|
164
|
+
otherPosition,
|
|
165
|
+
otherSize,
|
|
166
|
+
ctx.contact.position
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
reflected = this.applySpeedClamp(reflected, minSpeed, maxSpeed);
|
|
170
|
+
const result = {
|
|
171
|
+
velocity: { x: reflected.x, y: reflected.y, z: 0 },
|
|
172
|
+
speed: Math.hypot(reflected.x, reflected.y),
|
|
173
|
+
normal: { x: normal.x, y: normal.y, z: 0 }
|
|
174
|
+
};
|
|
175
|
+
this.lastResult = result;
|
|
176
|
+
this.lastUpdatedAtMs = this.currentTimeMs;
|
|
177
|
+
this.dispatch("start-ricochet" /* StartRicochet */);
|
|
178
|
+
this.emitToListeners(result);
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Extract velocity, position, and size data from entities or context.
|
|
183
|
+
*/
|
|
184
|
+
extractDataFromEntities(ctx) {
|
|
185
|
+
let selfVelocity = ctx.selfVelocity;
|
|
186
|
+
let selfPosition = ctx.selfPosition;
|
|
187
|
+
let otherPosition = ctx.otherPosition;
|
|
188
|
+
let otherSize = ctx.otherSize;
|
|
189
|
+
if (ctx.entity?.body) {
|
|
190
|
+
const vel = ctx.entity.body.linvel();
|
|
191
|
+
selfVelocity = selfVelocity ?? { x: vel.x, y: vel.y, z: vel.z };
|
|
192
|
+
const pos = ctx.entity.body.translation();
|
|
193
|
+
selfPosition = selfPosition ?? { x: pos.x, y: pos.y, z: pos.z };
|
|
194
|
+
}
|
|
195
|
+
if (ctx.otherEntity?.body) {
|
|
196
|
+
const pos = ctx.otherEntity.body.translation();
|
|
197
|
+
otherPosition = otherPosition ?? { x: pos.x, y: pos.y, z: pos.z };
|
|
198
|
+
}
|
|
199
|
+
if (ctx.otherEntity && "size" in ctx.otherEntity) {
|
|
200
|
+
const size = ctx.otherEntity.size;
|
|
201
|
+
if (size && typeof size.x === "number") {
|
|
202
|
+
otherSize = otherSize ?? { x: size.x, y: size.y, z: size.z };
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { selfVelocity, selfPosition, otherPosition, otherSize };
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Compute collision normal from entity positions using AABB heuristic.
|
|
209
|
+
*/
|
|
210
|
+
computeNormalFromPositions(selfPosition, otherPosition) {
|
|
211
|
+
if (!selfPosition || !otherPosition) return null;
|
|
212
|
+
const dx = selfPosition.x - otherPosition.x;
|
|
213
|
+
const dy = selfPosition.y - otherPosition.y;
|
|
214
|
+
if (Math.abs(dx) > Math.abs(dy)) {
|
|
215
|
+
return { x: dx > 0 ? 1 : -1, y: 0, z: 0 };
|
|
216
|
+
} else {
|
|
217
|
+
return { x: 0, y: dy > 0 ? 1 : -1, z: 0 };
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Compute basic reflection using the formula: v' = v - 2(v·n)n
|
|
222
|
+
*/
|
|
223
|
+
computeBasicReflection(velocity, normal) {
|
|
224
|
+
const vx = velocity.x;
|
|
225
|
+
const vy = velocity.y;
|
|
226
|
+
const dotProduct = vx * normal.x + vy * normal.y;
|
|
227
|
+
return {
|
|
228
|
+
x: vx - 2 * dotProduct * normal.x,
|
|
229
|
+
y: vy - 2 * dotProduct * normal.y
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Compute angled deflection for paddle-style reflections.
|
|
234
|
+
*/
|
|
235
|
+
computeAngledDeflection(velocity, normal, speed, maxAngleDeg, speedMultiplier, selfPosition, otherPosition, otherSize, contactPosition) {
|
|
236
|
+
const maxAngleRad = maxAngleDeg * Math.PI / 180;
|
|
237
|
+
let tx = -normal.y;
|
|
238
|
+
let ty = normal.x;
|
|
239
|
+
if (Math.abs(normal.x) > Math.abs(normal.y)) {
|
|
240
|
+
if (ty < 0) {
|
|
241
|
+
tx = -tx;
|
|
242
|
+
ty = -ty;
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
if (tx < 0) {
|
|
246
|
+
tx = -tx;
|
|
247
|
+
ty = -ty;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const offset = this.computeHitOffset(
|
|
251
|
+
velocity,
|
|
252
|
+
normal,
|
|
253
|
+
speed,
|
|
254
|
+
tx,
|
|
255
|
+
ty,
|
|
256
|
+
selfPosition,
|
|
257
|
+
otherPosition,
|
|
258
|
+
otherSize,
|
|
259
|
+
contactPosition
|
|
260
|
+
);
|
|
261
|
+
const angle = clamp(offset, -1, 1) * maxAngleRad;
|
|
262
|
+
const cosA = Math.cos(angle);
|
|
263
|
+
const sinA = Math.sin(angle);
|
|
264
|
+
const newSpeed = speed * speedMultiplier;
|
|
265
|
+
return {
|
|
266
|
+
x: newSpeed * (normal.x * cosA + tx * sinA),
|
|
267
|
+
y: newSpeed * (normal.y * cosA + ty * sinA)
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Compute hit offset for angled deflection (-1 to 1).
|
|
272
|
+
*/
|
|
273
|
+
computeHitOffset(velocity, normal, speed, tx, ty, selfPosition, otherPosition, otherSize, contactPosition) {
|
|
274
|
+
if (otherPosition && otherSize) {
|
|
275
|
+
const useY = Math.abs(normal.x) > Math.abs(normal.y);
|
|
276
|
+
const halfExtent = useY ? otherSize.y / 2 : otherSize.x / 2;
|
|
277
|
+
if (useY) {
|
|
278
|
+
const selfY = selfPosition?.y ?? contactPosition?.y ?? 0;
|
|
279
|
+
const paddleY = otherPosition.y;
|
|
280
|
+
return (selfY - paddleY) / halfExtent;
|
|
281
|
+
} else {
|
|
282
|
+
const selfX = selfPosition?.x ?? contactPosition?.x ?? 0;
|
|
283
|
+
const paddleX = otherPosition.x;
|
|
284
|
+
return (selfX - paddleX) / halfExtent;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return (velocity.x * tx + velocity.y * ty) / speed;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Apply speed constraints to the reflected velocity.
|
|
291
|
+
*/
|
|
292
|
+
applySpeedClamp(velocity, minSpeed, maxSpeed) {
|
|
293
|
+
const currentSpeed = Math.hypot(velocity.x, velocity.y);
|
|
294
|
+
if (currentSpeed === 0) return velocity;
|
|
295
|
+
const targetSpeed = clamp(currentSpeed, minSpeed, maxSpeed);
|
|
296
|
+
const scale = targetSpeed / currentSpeed;
|
|
297
|
+
return {
|
|
298
|
+
x: velocity.x * scale,
|
|
299
|
+
y: velocity.y * scale
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Clear the ricochet state (call after consumer has applied the result).
|
|
304
|
+
*/
|
|
305
|
+
clearRicochet() {
|
|
306
|
+
this.dispatch("end-ricochet" /* EndRicochet */);
|
|
307
|
+
}
|
|
308
|
+
dispatch(event) {
|
|
309
|
+
if (this.machine.can(event)) {
|
|
310
|
+
this.machine.syncDispatch(event);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
// src/lib/behaviors/behavior-descriptor.ts
|
|
316
|
+
function defineBehavior(config) {
|
|
317
|
+
return {
|
|
318
|
+
key: /* @__PURE__ */ Symbol.for(`zylem:behavior:${config.name}`),
|
|
319
|
+
defaultOptions: config.defaultOptions,
|
|
320
|
+
systemFactory: config.systemFactory,
|
|
321
|
+
createHandle: config.createHandle
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/lib/behaviors/ricochet-2d/ricochet-2d.descriptor.ts
|
|
326
|
+
var defaultOptions = {
|
|
327
|
+
minSpeed: 2,
|
|
328
|
+
maxSpeed: 20,
|
|
329
|
+
speedMultiplier: 1.05,
|
|
330
|
+
reflectionMode: "angled",
|
|
331
|
+
maxAngleDeg: 60
|
|
332
|
+
};
|
|
333
|
+
var RICOCHET_BEHAVIOR_KEY = /* @__PURE__ */ Symbol.for("zylem:behavior:ricochet-2d");
|
|
334
|
+
function createRicochet2DHandle(ref) {
|
|
335
|
+
return {
|
|
336
|
+
getRicochet: (ctx) => {
|
|
337
|
+
const fsm = ref.fsm;
|
|
338
|
+
if (!fsm) return null;
|
|
339
|
+
return fsm.computeRicochet(ctx, ref.options);
|
|
340
|
+
},
|
|
341
|
+
applyRicochet: (ctx) => {
|
|
342
|
+
const fsm = ref.fsm;
|
|
343
|
+
if (!fsm) return false;
|
|
344
|
+
if (fsm.isOnCooldown()) return false;
|
|
345
|
+
const result = fsm.computeRicochet(ctx, ref.options);
|
|
346
|
+
if (!result) return false;
|
|
347
|
+
const entity = ctx.entity;
|
|
348
|
+
if (entity?.transformStore) {
|
|
349
|
+
entity.transformStore.velocity.x = result.velocity.x;
|
|
350
|
+
entity.transformStore.velocity.y = result.velocity.y;
|
|
351
|
+
entity.transformStore.velocity.z = result.velocity.z ?? 0;
|
|
352
|
+
entity.transformStore.dirty.velocity = true;
|
|
353
|
+
}
|
|
354
|
+
return true;
|
|
355
|
+
},
|
|
356
|
+
getLastResult: () => {
|
|
357
|
+
const fsm = ref.fsm;
|
|
358
|
+
return fsm?.getLastResult() ?? null;
|
|
359
|
+
},
|
|
360
|
+
onRicochet: (callback) => {
|
|
361
|
+
const fsm = ref.fsm;
|
|
362
|
+
if (!fsm) {
|
|
363
|
+
if (!ref.pendingListeners) {
|
|
364
|
+
ref.pendingListeners = [];
|
|
365
|
+
}
|
|
366
|
+
ref.pendingListeners.push(callback);
|
|
367
|
+
return () => {
|
|
368
|
+
const pending = ref.pendingListeners;
|
|
369
|
+
const idx = pending.indexOf(callback);
|
|
370
|
+
if (idx >= 0) pending.splice(idx, 1);
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
return fsm.addListener(callback);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
var Ricochet2DSystem = class {
|
|
378
|
+
constructor(world, getBehaviorLinks) {
|
|
379
|
+
this.world = world;
|
|
380
|
+
this.getBehaviorLinks = getBehaviorLinks;
|
|
381
|
+
}
|
|
382
|
+
elapsedMs = 0;
|
|
383
|
+
update(_ecs, delta) {
|
|
384
|
+
this.elapsedMs += delta * 1e3;
|
|
385
|
+
const links = this.getBehaviorLinks?.(RICOCHET_BEHAVIOR_KEY);
|
|
386
|
+
if (!links) return;
|
|
387
|
+
for (const link of links) {
|
|
388
|
+
const ricochetRef = link.ref;
|
|
389
|
+
if (!ricochetRef.fsm) {
|
|
390
|
+
ricochetRef.fsm = new Ricochet2DFSM();
|
|
391
|
+
const pending = ricochetRef.pendingListeners;
|
|
392
|
+
if (pending) {
|
|
393
|
+
for (const cb of pending) {
|
|
394
|
+
ricochetRef.fsm.addListener(cb);
|
|
395
|
+
}
|
|
396
|
+
ricochetRef.pendingListeners = [];
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
ricochetRef.fsm.setCurrentTimeMs(this.elapsedMs);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
destroy(_ecs) {
|
|
403
|
+
const links = this.getBehaviorLinks?.(RICOCHET_BEHAVIOR_KEY);
|
|
404
|
+
if (!links) return;
|
|
405
|
+
for (const link of links) {
|
|
406
|
+
const ricochetRef = link.ref;
|
|
407
|
+
if (ricochetRef?.fsm) {
|
|
408
|
+
ricochetRef.fsm.resetCooldown();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
var Ricochet2DBehavior = defineBehavior({
|
|
414
|
+
name: "ricochet-2d",
|
|
415
|
+
defaultOptions,
|
|
416
|
+
systemFactory: (ctx) => new Ricochet2DSystem(ctx.world, ctx.getBehaviorLinks),
|
|
417
|
+
createHandle: createRicochet2DHandle
|
|
418
|
+
});
|
|
419
|
+
export {
|
|
420
|
+
Ricochet2DBehavior,
|
|
421
|
+
Ricochet2DEvent,
|
|
422
|
+
Ricochet2DFSM,
|
|
423
|
+
Ricochet2DState
|
|
424
|
+
};
|
|
425
|
+
//# sourceMappingURL=ricochet-2d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/lib/core/utility/sync-state-machine.ts","../../src/lib/behaviors/ricochet-2d/ricochet-2d-fsm.ts","../../src/lib/behaviors/behavior-descriptor.ts","../../src/lib/behaviors/ricochet-2d/ricochet-2d.descriptor.ts"],"sourcesContent":["import {\n\tStateMachine as BaseStateMachine,\n\ttype ILogger,\n\ttype ITransition,\n\ttype SyncCallback,\n} from 'typescript-fsm';\n\nexport { t } from 'typescript-fsm';\nexport type { ILogger, ITransition, SyncCallback };\n\n/**\n * Local wrapper around typescript-fsm's SyncStateMachine.\n *\n * typescript-fsm@1.6.0 incorrectly reports callback-less transitions as\n * unhandled even after moving to the next state, which causes noisy\n * `No transition...` console errors for valid FSM dispatches.\n */\nexport class SyncStateMachine<\n\tSTATE extends string | number | symbol,\n\tEVENT extends string | number | symbol,\n\tCALLBACK extends Record<EVENT, SyncCallback> = Record<EVENT, SyncCallback>,\n> extends BaseStateMachine<STATE, EVENT, CALLBACK> {\n\tconstructor(\n\t\tinit: STATE,\n\t\ttransitions: ITransition<STATE, EVENT, CALLBACK[EVENT]>[] = [],\n\t\tlogger: ILogger = console,\n\t) {\n\t\tsuper(init, transitions, logger);\n\t}\n\n\tdispatch<E extends EVENT>(_event: E, ..._args: unknown[]): Promise<void> {\n\t\tthrow new Error('SyncStateMachine does not support async dispatch.');\n\t}\n\n\tsyncDispatch<E extends EVENT>(event: E, ...args: unknown[]): boolean {\n\t\tconst found = this.transitions.some((transition) => {\n\t\t\tif (transition.fromState !== this._current || transition.event !== event) {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\tconst current = this._current;\n\t\t\tthis._current = transition.toState;\n\n\t\t\tif (!transition.cb) {\n\t\t\t\treturn true;\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\ttransition.cb(...args);\n\t\t\t\treturn true;\n\t\t\t} catch (error) {\n\t\t\t\tthis._current = current;\n\t\t\t\tthis.logger.error('Exception in callback', error);\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t});\n\n\t\tif (!found) {\n\t\t\tconst errorMessage = this.formatErr(this._current, event);\n\t\t\tthis.logger.error(errorMessage);\n\t\t}\n\n\t\treturn found;\n\t}\n}\n","/**\n * Ricochet2DFSM\n *\n * FSM + extended state to track ricochet events and results.\n * The FSM state tracks whether a ricochet is currently occurring.\n */\nimport { BaseEntityInterface } from \"../../types/entity-types\";\nimport { SyncStateMachine, t } from '../../core/utility/sync-state-machine';\n\nexport interface Ricochet2DResult {\n\t/** The reflected velocity vector */\n\tvelocity: { x: number; y: number; z?: number };\n\t/** The resulting speed after reflection */\n\tspeed: number;\n\t/** The collision normal used for reflection */\n\tnormal: { x: number; y: number; z?: number };\n}\n\nexport interface Ricochet2DCollisionContext {\n\tentity?: BaseEntityInterface;\n\totherEntity?: BaseEntityInterface;\n\t/** Current velocity of the entity (optional if entity is provided) */\n\tselfVelocity?: { x: number; y: number; z?: number };\n\t/** Contact information from the collision */\n\tcontact: {\n\t\t/** The collision normal */\n\t\tnormal?: { x: number; y: number; z?: number };\n\t\t/**\n\t\t * Optional position where the collision occurred.\n\t\t * If provided, used for precise offset calculation.\n\t\t */\n\t\tposition?: { x: number; y: number; z?: number };\n\t};\n\t/**\n\t * Optional position of the entity that owns this behavior.\n\t * Used with contact.position for offset calculations.\n\t */\n\tselfPosition?: { x: number; y: number; z?: number };\n\t/**\n\t * Optional position of the other entity in the collision.\n\t * Used for paddle-style deflection: offset = (contactY - otherY) / halfHeight.\n\t */\n\totherPosition?: { x: number; y: number; z?: number };\n\t/**\n\t * Optional size of the other entity (e.g., paddle size).\n\t * If provided, used to normalize the offset based on the collision face.\n\t */\n\totherSize?: { x: number; y: number; z?: number };\n}\n\nexport enum Ricochet2DState {\n\tIdle = 'idle',\n\tRicocheting = 'ricocheting',\n}\n\nexport enum Ricochet2DEvent {\n\tStartRicochet = 'start-ricochet',\n\tEndRicochet = 'end-ricochet',\n}\n\nfunction clamp(value: number, min: number, max: number): number {\n\treturn Math.max(min, Math.min(max, value));\n}\n\n/**\n * Callback type for ricochet event listeners.\n */\nexport type RicochetCallback = (result: Ricochet2DResult) => void;\n\n/**\n * FSM wrapper with extended state (lastResult).\n * Systems or consumers call `computeRicochet(...)` when a collision occurs.\n */\nexport class Ricochet2DFSM {\n\tpublic readonly machine: SyncStateMachine<Ricochet2DState, Ricochet2DEvent, never>;\n\n\tprivate lastResult: Ricochet2DResult | null = null;\n\tprivate lastUpdatedAtMs: number | null = null;\n\tprivate currentTimeMs: number = 0;\n\tprivate listeners: Set<RicochetCallback> = new Set();\n\n\tconstructor() {\n\t\tthis.machine = new SyncStateMachine<Ricochet2DState, Ricochet2DEvent, never>(\n\t\t\tRicochet2DState.Idle,\n\t\t\t[\n\t\t\t\tt(Ricochet2DState.Idle, Ricochet2DEvent.StartRicochet, Ricochet2DState.Ricocheting),\n\t\t\t\tt(Ricochet2DState.Ricocheting, Ricochet2DEvent.EndRicochet, Ricochet2DState.Idle),\n\n\t\t\t\t// Self transitions (no-ops)\n\t\t\t\tt(Ricochet2DState.Idle, Ricochet2DEvent.EndRicochet, Ricochet2DState.Idle),\n\t\t\t\tt(Ricochet2DState.Ricocheting, Ricochet2DEvent.StartRicochet, Ricochet2DState.Ricocheting),\n\t\t\t]\n\t\t);\n\t}\n\n\t/**\n\t * Add a listener for ricochet events.\n\t * @returns Unsubscribe function\n\t */\n\taddListener(callback: RicochetCallback): () => void {\n\t\tthis.listeners.add(callback);\n\t\treturn () => this.listeners.delete(callback);\n\t}\n\n\t/**\n\t * Emit result to all listeners.\n\t */\n\tprivate emitToListeners(result: Ricochet2DResult): void {\n\t\tfor (const callback of this.listeners) {\n\t\t\ttry {\n\t\t\t\tcallback(result);\n\t\t\t} catch (e) {\n\t\t\t\tconsole.error('[Ricochet2DFSM] Listener error:', e);\n\t\t\t}\n\t\t}\n\t}\n\n\tgetState(): Ricochet2DState {\n\t\treturn this.machine.getState();\n\t}\n\n\t/**\n\t * Returns the last computed ricochet result, or null if none.\n\t */\n\tgetLastResult(): Ricochet2DResult | null {\n\t\treturn this.lastResult;\n\t}\n\n\t/**\n\t * Best-effort timestamp (ms) of the last computation.\n\t */\n\tgetLastUpdatedAtMs(): number | null {\n\t\treturn this.lastUpdatedAtMs;\n\t}\n\n\t/**\n\t * Set current game time (called by system each frame).\n\t * Used for cooldown calculations.\n\t */\n\tsetCurrentTimeMs(timeMs: number): void {\n\t\tthis.currentTimeMs = timeMs;\n\t}\n\n\t/**\n\t * Check if ricochet is on cooldown (to prevent rapid duplicate applications).\n\t * @param cooldownMs Cooldown duration in milliseconds (default: 50ms)\n\t */\n\tisOnCooldown(cooldownMs: number = 50): boolean {\n\t\tif (this.lastUpdatedAtMs === null) return false;\n\t\treturn (this.currentTimeMs - this.lastUpdatedAtMs) < cooldownMs;\n\t}\n\n\t/**\n\t * Reset cooldown state (e.g., on entity respawn).\n\t */\n\tresetCooldown(): void {\n\t\tthis.lastUpdatedAtMs = null;\n\t}\n\n\t/**\n\t * Compute a ricochet result from collision context.\n\t * Returns the result for the consumer to apply, or null if invalid input.\n\t */\n\tcomputeRicochet(\n\t\tctx: Ricochet2DCollisionContext,\n\t\toptions: {\n\t\t\tminSpeed?: number;\n\t\t\tmaxSpeed?: number;\n\t\t\tspeedMultiplier?: number;\n\t\t\treflectionMode?: 'simple' | 'angled';\n\t\t\tmaxAngleDeg?: number;\n\t\t} = {}\n\t): Ricochet2DResult | null {\n\t\tconst {\n\t\t\tminSpeed = 2,\n\t\t\tmaxSpeed = 20,\n\t\t\tspeedMultiplier = 1.05,\n\t\t\treflectionMode = 'angled',\n\t\t\tmaxAngleDeg = 60,\n\t\t} = options;\n\n\t\t// Extract data from entities if provided\n\t\tconst { selfVelocity, selfPosition, otherPosition, otherSize } = this.extractDataFromEntities(ctx);\n\n\t\tif (!selfVelocity) {\n\t\t\tthis.dispatch(Ricochet2DEvent.EndRicochet);\n\t\t\treturn null;\n\t\t}\n\n\t\tconst speed = Math.hypot(selfVelocity.x, selfVelocity.y);\n\t\tif (speed === 0) {\n\t\t\tthis.dispatch(Ricochet2DEvent.EndRicochet);\n\t\t\treturn null;\n\t\t}\n\n\t\t// Compute or extract collision normal\n\t\tconst normal = ctx.contact.normal ?? this.computeNormalFromPositions(selfPosition, otherPosition);\n\t\tif (!normal) {\n\t\t\tthis.dispatch(Ricochet2DEvent.EndRicochet);\n\t\t\treturn null;\n\t\t}\n\n\t\t// Compute basic reflection\n\t\tlet reflected = this.computeBasicReflection(selfVelocity, normal);\n\n\t\t// Apply angled deflection if requested\n\t\tif (reflectionMode === 'angled') {\n\t\t\treflected = this.computeAngledDeflection(\n\t\t\t\tselfVelocity,\n\t\t\t\tnormal,\n\t\t\t\tspeed,\n\t\t\t\tmaxAngleDeg,\n\t\t\t\tspeedMultiplier,\n\t\t\t\tselfPosition,\n\t\t\t\totherPosition,\n\t\t\t\totherSize,\n\t\t\t\tctx.contact.position\n\t\t\t);\n\t\t}\n\n\t\t// Apply final speed constraints\n\t\treflected = this.applySpeedClamp(reflected, minSpeed, maxSpeed);\n\n\t\tconst result: Ricochet2DResult = {\n\t\t\tvelocity: { x: reflected.x, y: reflected.y, z: 0 },\n\t\t\tspeed: Math.hypot(reflected.x, reflected.y),\n\t\t\tnormal: { x: normal.x, y: normal.y, z: 0 },\n\t\t};\n\n\t\tthis.lastResult = result;\n\t\tthis.lastUpdatedAtMs = this.currentTimeMs;\n\t\tthis.dispatch(Ricochet2DEvent.StartRicochet);\n\t\tthis.emitToListeners(result);\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Extract velocity, position, and size data from entities or context.\n\t */\n\tprivate extractDataFromEntities(ctx: Ricochet2DCollisionContext) {\n\t\tlet selfVelocity = ctx.selfVelocity;\n\t\tlet selfPosition = ctx.selfPosition;\n\t\tlet otherPosition = ctx.otherPosition;\n\t\tlet otherSize = ctx.otherSize;\n\n\t\tif (ctx.entity?.body) {\n\t\t\tconst vel = ctx.entity.body.linvel();\n\t\t\tselfVelocity = selfVelocity ?? { x: vel.x, y: vel.y, z: vel.z };\n\t\t\tconst pos = ctx.entity.body.translation();\n\t\t\tselfPosition = selfPosition ?? { x: pos.x, y: pos.y, z: pos.z };\n\t\t}\n\n\t\tif (ctx.otherEntity?.body) {\n\t\t\tconst pos = ctx.otherEntity.body.translation();\n\t\t\totherPosition = otherPosition ?? { x: pos.x, y: pos.y, z: pos.z };\n\t\t}\n\n\t\tif (ctx.otherEntity && 'size' in ctx.otherEntity) {\n\t\t\tconst size = (ctx.otherEntity as any).size;\n\t\t\tif (size && typeof size.x === 'number') {\n\t\t\t\totherSize = otherSize ?? { x: size.x, y: size.y, z: size.z };\n\t\t\t}\n\t\t}\n\n\t\treturn { selfVelocity, selfPosition, otherPosition, otherSize };\n\t}\n\n\t/**\n\t * Compute collision normal from entity positions using AABB heuristic.\n\t */\n\tprivate computeNormalFromPositions(\n\t\tselfPosition?: { x: number; y: number; z?: number },\n\t\totherPosition?: { x: number; y: number; z?: number }\n\t): { x: number; y: number; z?: number } | null {\n\t\tif (!selfPosition || !otherPosition) return null;\n\n\t\tconst dx = selfPosition.x - otherPosition.x;\n\t\tconst dy = selfPosition.y - otherPosition.y;\n\n\t\t// Simple \"which face was hit\" logic for box collisions\n\t\tif (Math.abs(dx) > Math.abs(dy)) {\n\t\t\treturn { x: dx > 0 ? 1 : -1, y: 0, z: 0 };\n\t\t} else {\n\t\t\treturn { x: 0, y: dy > 0 ? 1 : -1, z: 0 };\n\t\t}\n\t}\n\n\t/**\n\t * Compute basic reflection using the formula: v' = v - 2(v·n)n\n\t */\n\tprivate computeBasicReflection(\n\t\tvelocity: { x: number; y: number },\n\t\tnormal: { x: number; y: number; z?: number }\n\t): { x: number; y: number } {\n\t\tconst vx = velocity.x;\n\t\tconst vy = velocity.y;\n\t\tconst dotProduct = vx * normal.x + vy * normal.y;\n\n\t\treturn {\n\t\t\tx: vx - 2 * dotProduct * normal.x,\n\t\t\ty: vy - 2 * dotProduct * normal.y,\n\t\t};\n\t}\n\n\t/**\n\t * Compute angled deflection for paddle-style reflections.\n\t */\n\tprivate computeAngledDeflection(\n\t\tvelocity: { x: number; y: number },\n\t\tnormal: { x: number; y: number; z?: number },\n\t\tspeed: number,\n\t\tmaxAngleDeg: number,\n\t\tspeedMultiplier: number,\n\t\tselfPosition?: { x: number; y: number; z?: number },\n\t\totherPosition?: { x: number; y: number; z?: number },\n\t\totherSize?: { x: number; y: number; z?: number },\n\t\tcontactPosition?: { x: number; y: number; z?: number }\n\t): { x: number; y: number } {\n\t\tconst maxAngleRad = (maxAngleDeg * Math.PI) / 180;\n\n\t\t// Tangent vector (perpendicular to normal)\n\t\tlet tx = -normal.y;\n\t\tlet ty = normal.x;\n\n\t\t// Ensure tangent points in positive direction of the deflection axis\n\t\t// so that positive offset (right/top) results in positive deflection\n\t\tif (Math.abs(normal.x) > Math.abs(normal.y)) {\n\t\t\t// Vertical face (Normal is X-aligned). Deflection axis is Y.\n\t\t\t// We want ty > 0.\n\t\t\tif (ty < 0) {\n\t\t\t\ttx = -tx;\n\t\t\t\tty = -ty;\n\t\t\t}\n\t\t} else {\n\t\t\t// Horizontal face (Normal is Y-aligned). Deflection axis is X.\n\t\t\t// We want tx > 0.\n\t\t\tif (tx < 0) {\n\t\t\t\ttx = -tx;\n\t\t\t\tty = -ty;\n\t\t\t}\n\t\t}\n\n\t\t// Compute offset based on hit position\n\t\tconst offset = this.computeHitOffset(\n\t\t\tvelocity,\n\t\t\tnormal,\n\t\t\tspeed,\n\t\t\ttx,\n\t\t\tty,\n\t\t\tselfPosition,\n\t\t\totherPosition,\n\t\t\totherSize,\n\t\t\tcontactPosition\n\t\t);\n\n\t\tconst angle = clamp(offset, -1, 1) * maxAngleRad;\n\n\t\tconst cosA = Math.cos(angle);\n\t\tconst sinA = Math.sin(angle);\n\n\t\tconst newSpeed = speed * speedMultiplier;\n\n\t\treturn {\n\t\t\tx: newSpeed * (normal.x * cosA + tx * sinA),\n\t\t\ty: newSpeed * (normal.y * cosA + ty * sinA),\n\t\t};\n\t}\n\n\t/**\n\t * Compute hit offset for angled deflection (-1 to 1).\n\t */\n\tprivate computeHitOffset(\n\t\tvelocity: { x: number; y: number },\n\t\tnormal: { x: number; y: number; z?: number },\n\t\tspeed: number,\n\t\ttx: number,\n\t\tty: number,\n\t\tselfPosition?: { x: number; y: number; z?: number },\n\t\totherPosition?: { x: number; y: number; z?: number },\n\t\totherSize?: { x: number; y: number; z?: number },\n\t\tcontactPosition?: { x: number; y: number; z?: number }\n\t): number {\n\t\t// Use position-based offset if available\n\t\tif (otherPosition && otherSize) {\n\t\t\tconst useY = Math.abs(normal.x) > Math.abs(normal.y);\n\t\t\tconst halfExtent = useY ? otherSize.y / 2 : otherSize.x / 2;\n\n\t\t\tif (useY) {\n\t\t\t\tconst selfY = selfPosition?.y ?? contactPosition?.y ?? 0;\n\t\t\t\tconst paddleY = otherPosition.y;\n\t\t\t\treturn (selfY - paddleY) / halfExtent;\n\t\t\t} else {\n\t\t\t\tconst selfX = selfPosition?.x ?? contactPosition?.x ?? 0;\n\t\t\t\tconst paddleX = otherPosition.x;\n\t\t\t\treturn (selfX - paddleX) / halfExtent;\n\t\t\t}\n\t\t}\n\n\t\t// Fallback: use velocity-based offset\n\t\treturn (velocity.x * tx + velocity.y * ty) / speed;\n\t}\n\n\t/**\n\t * Apply speed constraints to the reflected velocity.\n\t */\n\tprivate applySpeedClamp(\n\t\tvelocity: { x: number; y: number },\n\t\tminSpeed: number,\n\t\tmaxSpeed: number\n\t): { x: number; y: number } {\n\t\tconst currentSpeed = Math.hypot(velocity.x, velocity.y);\n\t\tif (currentSpeed === 0) return velocity;\n\n\t\tconst targetSpeed = clamp(currentSpeed, minSpeed, maxSpeed);\n\t\tconst scale = targetSpeed / currentSpeed;\n\n\t\treturn {\n\t\t\tx: velocity.x * scale,\n\t\t\ty: velocity.y * scale,\n\t\t};\n\t}\n\n\t/**\n\t * Clear the ricochet state (call after consumer has applied the result).\n\t */\n\tclearRicochet(): void {\n\t\tthis.dispatch(Ricochet2DEvent.EndRicochet);\n\t}\n\n\tprivate dispatch(event: Ricochet2DEvent): void {\n\t\tif (this.machine.can(event)) {\n\t\t\tthis.machine.syncDispatch(event);\n\t\t}\n\t}\n}\n","/**\n * BehaviorDescriptor\n *\n * Type-safe behavior descriptors that provide options inference.\n * Used with entity.use() to declaratively attach behaviors to entities.\n *\n * Each behavior can define its own handle type via `createHandle`,\n * providing behavior-specific methods with full type safety.\n */\n\nimport type { BehaviorSystemFactory } from './behavior-system';\n\n/**\n * Base handle returned by entity.use() for lazy access to behavior runtime.\n * FSM is null until entity is spawned and components are initialized.\n */\nexport interface BaseBehaviorHandle<\n O extends Record<string, any> = Record<string, any>,\n> {\n /** Get the FSM instance (null until entity is spawned) */\n getFSM(): any | null;\n /** Get the current options */\n getOptions(): O;\n /** Access the underlying behavior ref */\n readonly ref: BehaviorRef<O>;\n}\n\n/**\n * Reference to a behavior stored on an entity\n */\nexport interface BehaviorRef<\n O extends Record<string, any> = Record<string, any>,\n> {\n /** The behavior descriptor */\n descriptor: BehaviorDescriptor<O, any>;\n /** Merged options (defaults + overrides) */\n options: O;\n /** Optional FSM instance - set lazily when entity is spawned */\n fsm?: any;\n}\n\n/**\n * A typed behavior descriptor that associates a symbol key with:\n * - Default options (providing type inference)\n * - A system factory to create the behavior system\n * - An optional handle factory for behavior-specific methods\n */\nexport interface BehaviorDescriptor<\n O extends Record<string, any> = Record<string, any>,\n H extends Record<string, any> = Record<string, never>,\n I = unknown,\n> {\n /** Unique symbol identifying this behavior */\n readonly key: symbol;\n /** Default options (used for type inference) */\n readonly defaultOptions: O;\n /** Factory to create the behavior system */\n readonly systemFactory: BehaviorSystemFactory;\n /**\n * Optional factory to create behavior-specific handle methods.\n * These methods are merged into the handle returned by entity.use().\n */\n readonly createHandle?: (ref: BehaviorRef<O>) => H;\n}\n\n/**\n * The full handle type returned by entity.use().\n * Combines base handle with behavior-specific methods.\n */\nexport type BehaviorHandle<\n O extends Record<string, any> = Record<string, any>,\n H extends Record<string, any> = Record<string, never>,\n> = BaseBehaviorHandle<O> & H;\n\n/**\n * Configuration for defining a new behavior\n */\nexport interface DefineBehaviorConfig<\n O extends Record<string, any>,\n H extends Record<string, any> = Record<string, never>,\n I = unknown,\n> {\n /** Human-readable name for debugging */\n name: string;\n /** Default options - these define the type */\n defaultOptions: O;\n /** Factory function to create the system */\n systemFactory: BehaviorSystemFactory;\n /**\n * Optional factory to create behavior-specific handle methods.\n * The returned object is merged into the handle returned by entity.use().\n *\n * @example\n * ```typescript\n * createHandle: (ref) => ({\n * getLastHits: () => ref.fsm?.getLastHits() ?? null,\n * getMovement: (moveX, moveY) => ref.fsm?.getMovement(moveX, moveY) ?? { moveX, moveY },\n * }),\n * ```\n */\n createHandle?: (ref: BehaviorRef<O>) => H;\n}\n\n/**\n * Define a typed behavior descriptor.\n *\n * @example\n * ```typescript\n * export const WorldBoundary2DBehavior = defineBehavior({\n * name: 'world-boundary-2d',\n * defaultOptions: { boundaries: { top: 0, bottom: 0, left: 0, right: 0 } },\n * systemFactory: (ctx) => new WorldBoundary2DSystem(ctx.world),\n * createHandle: (ref) => ({\n * getLastHits: () => ref.fsm?.getLastHits() ?? null,\n * getMovement: (moveX: number, moveY: number) =>\n * ref.fsm?.getMovement(moveX, moveY) ?? { moveX, moveY },\n * }),\n * });\n *\n * // Usage - handle has getLastHits and getMovement with full types\n * const boundary = ship.use(WorldBoundary2DBehavior, { ... });\n * const hits = boundary.getLastHits(); // Fully typed!\n * ```\n */\nexport function defineBehavior<\n O extends Record<string, any>,\n H extends Record<string, any> = Record<string, never>,\n I = unknown,\n>(\n config: DefineBehaviorConfig<O, H, I>,\n): BehaviorDescriptor<O, H, I> {\n return {\n key: Symbol.for(`zylem:behavior:${config.name}`),\n defaultOptions: config.defaultOptions,\n systemFactory: config.systemFactory,\n createHandle: config.createHandle,\n };\n}\n","/**\n * Ricochet2DBehavior\n *\n * Computes 2D ricochet/reflection results for entities during collisions.\n * The behavior computes the result; the consumer decides how to apply it.\n *\n * Use `getRicochet(ctx)` on the behavior handle to compute reflection results.\n */\n\nimport type { IWorld } from 'bitecs';\nimport { defineBehavior, type BehaviorRef } from '../behavior-descriptor';\nimport type { BehaviorEntityLink, BehaviorSystem } from '../behavior-system';\nimport { Ricochet2DFSM, type Ricochet2DResult, type Ricochet2DCollisionContext, type RicochetCallback } from './ricochet-2d-fsm';\nexport type { Ricochet2DResult };\n\nexport interface Ricochet2DOptions {\n\t/**\n\t * Minimum speed after reflection.\n\t * Default: 2\n\t */\n\tminSpeed: number;\n\n\t/**\n\t * Maximum speed after reflection.\n\t * Default: 20\n\t */\n\tmaxSpeed: number;\n\n\t/**\n\t * Speed multiplier applied during angled reflection.\n\t * Default: 1.05\n\t */\n\tspeedMultiplier: number;\n\n\t/**\n\t * Reflection mode:\n\t * - 'simple': Basic axis inversion\n\t * - 'angled': Paddle-style deflection based on contact point\n\t * Default: 'angled'\n\t */\n\treflectionMode: 'simple' | 'angled';\n\n\t/**\n\t * Maximum deflection angle in degrees for angled mode.\n\t * Default: 60\n\t */\n\tmaxAngleDeg: number;\n}\n\n/**\n * Handle methods provided by Ricochet2DBehavior\n */\nexport interface Ricochet2DHandle {\n\t/**\n\t * Compute a ricochet/reflection result from collision context.\n\t * Returns the result for the consumer to apply, or null if invalid input.\n\t *\n\t * @param ctx - Collision context with selfVelocity and contact normal\n\t * @returns Ricochet result with velocity, speed, and normal, or null\n\t */\n\tgetRicochet(ctx: Ricochet2DCollisionContext): Ricochet2DResult | null;\n\n\t/**\n\t * Compute ricochet and apply velocity directly via transformStore.\n\t * Emits to onRicochet listeners if successful.\n\t *\n\t * @param ctx - Collision context with entity and contact normal\n\t * @returns true if ricochet was computed and applied, false otherwise\n\t */\n\tapplyRicochet(ctx: Ricochet2DCollisionContext): boolean;\n\n\t/**\n\t * Get the last computed ricochet result, or null if none.\n\t */\n\tgetLastResult(): Ricochet2DResult | null;\n\n\t/**\n\t * Register a listener for ricochet events.\n\t * Called whenever a ricochet is computed (from getRicochet or applyRicochet).\n\t *\n\t * @param callback - Function to call with ricochet result\n\t * @returns Unsubscribe function\n\t */\n\tonRicochet(callback: RicochetCallback): () => void;\n}\n\nconst defaultOptions: Ricochet2DOptions = {\n\tminSpeed: 2,\n\tmaxSpeed: 20,\n\tspeedMultiplier: 1.05,\n\treflectionMode: 'angled',\n\tmaxAngleDeg: 60,\n};\n\nconst RICOCHET_BEHAVIOR_KEY = Symbol.for('zylem:behavior:ricochet-2d');\n\n/**\n * Creates behavior-specific handle methods for Ricochet2DBehavior.\n */\nfunction createRicochet2DHandle(\n\tref: BehaviorRef<Ricochet2DOptions>\n): Ricochet2DHandle {\n\treturn {\n\t\tgetRicochet: (ctx: Ricochet2DCollisionContext) => {\n\t\t\tconst fsm = ref.fsm as Ricochet2DFSM | undefined;\n\t\t\tif (!fsm) return null;\n\t\t\treturn fsm.computeRicochet(ctx, ref.options);\n\t\t},\n\t\tapplyRicochet: (ctx: Ricochet2DCollisionContext): boolean => {\n\t\t\tconst fsm = ref.fsm as Ricochet2DFSM | undefined;\n\t\t\tif (!fsm) return false;\n\n\t\t\t// Skip if on cooldown (prevents rapid duplicate applications)\n\t\t\tif (fsm.isOnCooldown()) return false;\n\n\t\t\tconst result = fsm.computeRicochet(ctx, ref.options);\n\t\t\tif (!result) return false;\n\n\t\t\t// Apply velocity via transformStore\n\t\t\tconst entity = ctx.entity as any;\n\t\t\tif (entity?.transformStore) {\n\t\t\t\tentity.transformStore.velocity.x = result.velocity.x;\n\t\t\t\tentity.transformStore.velocity.y = result.velocity.y;\n\t\t\t\tentity.transformStore.velocity.z = result.velocity.z ?? 0;\n\t\t\t\tentity.transformStore.dirty.velocity = true;\n\t\t\t}\n\n\t\t\treturn true;\n\t\t},\n\t\tgetLastResult: () => {\n\t\t\tconst fsm = ref.fsm as Ricochet2DFSM | undefined;\n\t\t\treturn fsm?.getLastResult() ?? null;\n\t\t},\n\t\tonRicochet: (callback: RicochetCallback): (() => void) => {\n\t\t\tconst fsm = ref.fsm as Ricochet2DFSM | undefined;\n\t\t\tif (!fsm) {\n\t\t\t\t// FSM not ready yet - queue callback for later\n\t\t\t\t// System will apply pending callbacks when FSM is created\n\t\t\t\tif (!(ref as any).pendingListeners) {\n\t\t\t\t\t(ref as any).pendingListeners = [];\n\t\t\t\t}\n\t\t\t\t(ref as any).pendingListeners.push(callback);\n\t\t\t\t\n\t\t\t\t// Return unsubscribe that removes from pending queue\n\t\t\t\treturn () => {\n\t\t\t\t\tconst pending = (ref as any).pendingListeners as RicochetCallback[];\n\t\t\t\t\tconst idx = pending.indexOf(callback);\n\t\t\t\t\tif (idx >= 0) pending.splice(idx, 1);\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn fsm.addListener(callback);\n\t\t},\n\t};\n}\n\n/**\n * Ricochet2DSystem\n *\n * Stage-level system that:\n * - finds entities with this behavior attached\n * - lazily creates FSM instances for each entity\n *\n * Note: This behavior is consumer-driven. The system only manages FSM lifecycle.\n * Consumers call `getRicochet(ctx)` during collision callbacks to compute results.\n */\nclass Ricochet2DSystem implements BehaviorSystem {\n\tprivate elapsedMs: number = 0;\n\n\tconstructor(\n\t\tprivate world: any,\n\t\tprivate getBehaviorLinks?: (key: symbol) => Iterable<BehaviorEntityLink>,\n\t) {}\n\n\tupdate(_ecs: IWorld, delta: number): void {\n\t\t// Accumulate elapsed time (delta is in seconds)\n\t\tthis.elapsedMs += delta * 1000;\n\n\t\tconst links = this.getBehaviorLinks?.(RICOCHET_BEHAVIOR_KEY);\n\t\tif (!links) return;\n\n\t\tfor (const link of links) {\n\t\t\tconst ricochetRef = link.ref as any;\n\n\t\t\t// Create FSM lazily on first update after spawn\n\t\t\tif (!ricochetRef.fsm) {\n\t\t\t\tricochetRef.fsm = new Ricochet2DFSM();\n\t\t\t\t\n\t\t\t\t// Apply any pending listeners that were registered before FSM existed\n\t\t\t\tconst pending = (ricochetRef as any).pendingListeners as RicochetCallback[] | undefined;\n\t\t\t\tif (pending) {\n\t\t\t\t\tfor (const cb of pending) {\n\t\t\t\t\t\tricochetRef.fsm.addListener(cb);\n\t\t\t\t\t}\n\t\t\t\t\t(ricochetRef as any).pendingListeners = [];\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Sync current game time to FSM\n\t\t\tricochetRef.fsm.setCurrentTimeMs(this.elapsedMs);\n\t\t}\n\t}\n\n\tdestroy(_ecs: IWorld): void {\n\t\tconst links = this.getBehaviorLinks?.(RICOCHET_BEHAVIOR_KEY);\n\t\tif (!links) return;\n\t\tfor (const link of links) {\n\t\t\tconst ricochetRef = link.ref as any;\n\t\t\tif (ricochetRef?.fsm) {\n\t\t\t\tricochetRef.fsm.resetCooldown();\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Ricochet2DBehavior\n *\n * @example\n * ```ts\n * import { Ricochet2DBehavior } from \"@zylem/game-lib\";\n *\n * const ball = createSphere({ ... });\n * const ricochet = ball.use(Ricochet2DBehavior, {\n * minSpeed: 3,\n * maxSpeed: 15,\n * reflectionMode: 'angled',\n * });\n *\n * ball.onCollision(({ entity, other }) => {\n * const velocity = entity.body.linvel();\n * const result = ricochet.getRicochet({\n * selfVelocity: velocity,\n * contact: { normal: { x: 1, y: 0 } }, // from collision data\n * });\n *\n * if (result) {\n * entity.body.setLinvel(result.velocity, true);\n * }\n * });\n * ```\n */\nexport const Ricochet2DBehavior = defineBehavior({\n\tname: 'ricochet-2d',\n\tdefaultOptions,\n\tsystemFactory: (ctx) =>\n\t\tnew Ricochet2DSystem(ctx.world, ctx.getBehaviorLinks),\n\tcreateHandle: createRicochet2DHandle,\n});\n"],"mappings":";AAAA;AAAA,EACC,gBAAgB;AAAA,OAIV;AAEP,SAAS,SAAS;AAUX,IAAM,mBAAN,cAIG,iBAAyC;AAAA,EAClD,YACC,MACA,cAA4D,CAAC,GAC7D,SAAkB,SACjB;AACD,UAAM,MAAM,aAAa,MAAM;AAAA,EAChC;AAAA,EAEA,SAA0B,WAAc,OAAiC;AACxE,UAAM,IAAI,MAAM,mDAAmD;AAAA,EACpE;AAAA,EAEA,aAA8B,UAAa,MAA0B;AACpE,UAAM,QAAQ,KAAK,YAAY,KAAK,CAAC,eAAe;AACnD,UAAI,WAAW,cAAc,KAAK,YAAY,WAAW,UAAU,OAAO;AACzE,eAAO;AAAA,MACR;AAEA,YAAM,UAAU,KAAK;AACrB,WAAK,WAAW,WAAW;AAE3B,UAAI,CAAC,WAAW,IAAI;AACnB,eAAO;AAAA,MACR;AAEA,UAAI;AACH,mBAAW,GAAG,GAAG,IAAI;AACrB,eAAO;AAAA,MACR,SAAS,OAAO;AACf,aAAK,WAAW;AAChB,aAAK,OAAO,MAAM,yBAAyB,KAAK;AAChD,cAAM;AAAA,MACP;AAAA,IACD,CAAC;AAED,QAAI,CAAC,OAAO;AACX,YAAM,eAAe,KAAK,UAAU,KAAK,UAAU,KAAK;A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AAAA,IACD;AAAA,EACD;AACD;AA6BO,IAAM,qBAAqB,eAAe;AAAA,EAChD,MAAM;AAAA,EACN;AAAA,EACA,eAAe,CAAC,QACf,IAAI,iBAAiB,IAAI,OAAO,IAAI,gBAAgB;AAAA,EACrD,cAAc;AACf,CAAC;","names":["Ricochet2DState","Ricochet2DEvent"]}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { S as SyncStateMachine } from '../sync-state-machine-CZyspBpj.js';
|
|
2
|
+
import { B as BaseEntityInterface } from '../entity-types-DAu8sGJH.js';
|
|
3
|
+
import { c as BehaviorDescriptor } from '../behavior-descriptor-BXnVR8Ki.js';
|
|
4
|
+
import 'typescript-fsm';
|
|
5
|
+
import 'three';
|
|
6
|
+
import '@dimforge/rapier3d-compat';
|
|
7
|
+
import 'bitecs';
|
|
8
|
+
|
|
9
|
+
interface Ricochet3DResult {
|
|
10
|
+
velocity: {
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
z: number;
|
|
14
|
+
};
|
|
15
|
+
speed: number;
|
|
16
|
+
normal: {
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
z: number;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
interface Ricochet3DCollisionContext {
|
|
23
|
+
entity?: BaseEntityInterface;
|
|
24
|
+
otherEntity?: BaseEntityInterface;
|
|
25
|
+
selfVelocity?: {
|
|
26
|
+
x: number;
|
|
27
|
+
y: number;
|
|
28
|
+
z: number;
|
|
29
|
+
};
|
|
30
|
+
contact: {
|
|
31
|
+
normal?: {
|
|
32
|
+
x: number;
|
|
33
|
+
y: number;
|
|
34
|
+
z: number;
|
|
35
|
+
};
|
|
36
|
+
position?: {
|
|
37
|
+
x: number;
|
|
38
|
+
y: number;
|
|
39
|
+
z: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
selfPosition?: {
|
|
43
|
+
x: number;
|
|
44
|
+
y: number;
|
|
45
|
+
z: number;
|
|
46
|
+
};
|
|
47
|
+
otherPosition?: {
|
|
48
|
+
x: number;
|
|
49
|
+
y: number;
|
|
50
|
+
z: number;
|
|
51
|
+
};
|
|
52
|
+
otherSize?: {
|
|
53
|
+
x: number;
|
|
54
|
+
y: number;
|
|
55
|
+
z: number;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
declare enum Ricochet3DState {
|
|
59
|
+
Idle = "idle",
|
|
60
|
+
Ricocheting = "ricocheting"
|
|
61
|
+
}
|
|
62
|
+
declare enum Ricochet3DEvent {
|
|
63
|
+
StartRicochet = "start-ricochet",
|
|
64
|
+
EndRicochet = "end-ricochet"
|
|
65
|
+
}
|
|
66
|
+
type Ricochet3DCallback = (result: Ricochet3DResult) => void;
|
|
67
|
+
declare class Ricochet3DFSM {
|
|
68
|
+
readonly machine: SyncStateMachine<Ricochet3DState, Ricochet3DEvent, never>;
|
|
69
|
+
private lastResult;
|
|
70
|
+
private lastUpdatedAtMs;
|
|
71
|
+
private currentTimeMs;
|
|
72
|
+
private listeners;
|
|
73
|
+
constructor();
|
|
74
|
+
addListener(callback: Ricochet3DCallback): () => void;
|
|
75
|
+
getState(): Ricochet3DState;
|
|
76
|
+
getLastResult(): Ricochet3DResult | null;
|
|
77
|
+
getLastUpdatedAtMs(): number | null;
|
|
78
|
+
setCurrentTimeMs(timeMs: number): void;
|
|
79
|
+
isOnCooldown(cooldownMs?: number): boolean;
|
|
80
|
+
resetCooldown(): void;
|
|
81
|
+
computeRicochet(ctx: Ricochet3DCollisionContext, options?: {
|
|
82
|
+
minSpeed?: number;
|
|
83
|
+
maxSpeed?: number;
|
|
84
|
+
speedMultiplier?: number;
|
|
85
|
+
reflectionMode?: 'simple' | 'angled';
|
|
86
|
+
maxAngleDeg?: number;
|
|
87
|
+
}): Ricochet3DResult | null;
|
|
88
|
+
clearRicochet(): void;
|
|
89
|
+
private emitToListeners;
|
|
90
|
+
private extractDataFromEntities;
|
|
91
|
+
private computeNormalFromPositions;
|
|
92
|
+
private normalizeVector;
|
|
93
|
+
private dot;
|
|
94
|
+
private computeBasicReflection;
|
|
95
|
+
private computeAngledDeflection;
|
|
96
|
+
private computeHitOffsetVector;
|
|
97
|
+
private getDominantAxis;
|
|
98
|
+
private applySpeedClamp;
|
|
99
|
+
private dispatch;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
interface Ricochet3DOptions {
|
|
103
|
+
minSpeed: number;
|
|
104
|
+
maxSpeed: number;
|
|
105
|
+
speedMultiplier: number;
|
|
106
|
+
reflectionMode: 'simple' | 'angled';
|
|
107
|
+
maxAngleDeg: number;
|
|
108
|
+
}
|
|
109
|
+
interface Ricochet3DHandle {
|
|
110
|
+
getRicochet(ctx: Ricochet3DCollisionContext): Ricochet3DResult | null;
|
|
111
|
+
applyRicochet(ctx: Ricochet3DCollisionContext): boolean;
|
|
112
|
+
getLastResult(): Ricochet3DResult | null;
|
|
113
|
+
onRicochet(callback: Ricochet3DCallback): () => void;
|
|
114
|
+
}
|
|
115
|
+
declare const Ricochet3DBehavior: BehaviorDescriptor<Ricochet3DOptions, Ricochet3DHandle, unknown>;
|
|
116
|
+
|
|
117
|
+
export { Ricochet3DBehavior, type Ricochet3DCallback, type Ricochet3DCollisionContext, Ricochet3DEvent, Ricochet3DFSM, type Ricochet3DHandle, type Ricochet3DOptions, type Ricochet3DResult, Ricochet3DState };
|