create-dreamer-app 1.0.6 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -273,6 +273,8 @@ Rectangle bounds use their top-left corner; circle bounds use their center. `pos
|
|
|
273
273
|
|
|
274
274
|
Call these each frame with the body's public `key`. The handler receives the other body's public key once per reported body pair per frame.
|
|
275
275
|
|
|
276
|
+
- `gameCollision` calls `handler(otherKey: string, otherLayer: string)`, passing the other body's public key and layer. The layer is always defined for a physical collision.
|
|
277
|
+
- `gameHit` calls `handler(otherKey: string)`, passing only the other body's public key.
|
|
276
278
|
- Physical collisions require both bodies to include each other's `layer` in `collidesWith`.
|
|
277
279
|
- Hits are directional: only the observing body's `hitsWith` needs to include the other body's layer.
|
|
278
280
|
- Hits alone do not separate bodies or change their velocities.
|
|
@@ -280,8 +282,8 @@ Call these each frame with the body's public `key`. The handler receives the oth
|
|
|
280
282
|
|
|
281
283
|
```tsx
|
|
282
284
|
function PhysicsExample() {
|
|
283
|
-
gameCollision("ball", (otherKey) => {
|
|
284
|
-
if (
|
|
285
|
+
gameCollision("ball", (otherKey, otherLayer) => {
|
|
286
|
+
if (otherLayer === "world") console.log("Ball contacted world body:", otherKey);
|
|
285
287
|
});
|
|
286
288
|
|
|
287
289
|
return (
|
|
@@ -301,7 +303,7 @@ function PhysicsExample() {
|
|
|
301
303
|
|
|
302
304
|
### `gameRef<RigidBody2DSnapshot>(key)`
|
|
303
305
|
|
|
304
|
-
Reads a body's live stored state. Pass the returned `key` to the body's `ref`, and check `ready` before calling `snapshot()`:
|
|
306
|
+
Reads and updates a body's live stored state. Pass the returned `key` to the body's `ref`, and check `ready` before calling `snapshot()`:
|
|
305
307
|
|
|
306
308
|
```tsx
|
|
307
309
|
function TrackedBody() {
|
|
@@ -319,7 +321,31 @@ function TrackedBody() {
|
|
|
319
321
|
}
|
|
320
322
|
```
|
|
321
323
|
|
|
322
|
-
Import `RigidBody2DSnapshot` with `import type`. The ref normally becomes ready on a later frame after the body attaches. A snapshot contains local position
|
|
324
|
+
Import `RigidBody2DSnapshot` with `import type`. The ref normally becomes ready on a later frame after the body attaches. A snapshot contains local `position`, `velocity`, `mass`, and an `update` method:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
update: (handler: (prev: BodyState) => BodyState) => void;
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
The handler receives a deep copy of the latest stored body state and must return the complete next state. `BodyState` contains `{ x, y, velocity, mass }`: use local `x` and `y` when updating position, rather than the snapshot's `position` field. Spread `prev` to preserve fields you are not changing.
|
|
331
|
+
|
|
332
|
+
For example, when game logic needs to reposition a body and change its velocity:
|
|
333
|
+
|
|
334
|
+
```ts
|
|
335
|
+
if (body.ready) {
|
|
336
|
+
const snapshot = body.snapshot();
|
|
337
|
+
|
|
338
|
+
snapshot.update(prev => ({ ...prev, x: 100, y: 200 }));
|
|
339
|
+
snapshot.update(prev => ({
|
|
340
|
+
...prev,
|
|
341
|
+
velocity: { ...prev.velocity, y: -300 },
|
|
342
|
+
}));
|
|
343
|
+
}
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
Updates write to storage synchronously. Each handler receives the latest state, including changes from earlier updates, so the second update above preserves the first update's position. The deep copy includes nested objects such as `velocity`; mutating the handler's `prev` does not mutate live state before the handler returns. Return the next state even if you modify that copy directly.
|
|
347
|
+
|
|
348
|
+
The snapshot's data fields remain the values captured when `snapshot()` was called. Changing those fields does not update the body; use `update`, then call `body.snapshot()` again to read fresh values. Normal physics continues from the updated state, and changing the body's `x`, `y`, `velocity`, or `mass` props can still reset it as described above.
|
|
323
349
|
|
|
324
350
|
## Update, effect, and input hooks
|
|
325
351
|
|