castle-web-cli 0.4.90 → 0.4.92
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/dist/agent.js +23 -4
- package/dist/metering.d.ts +21 -0
- package/dist/metering.js +75 -0
- package/dist/native/loop.js +1 -0
- package/dist/native/openrouter.d.ts +1 -0
- package/dist/native/openrouter.js +1 -0
- package/dist/native/types.d.ts +1 -0
- package/kits/basic-2d/editors/SceneEditor.jsx +39 -6
- package/kits/basic-2d/engine/scene.js +10 -2
- package/kits/physics-2d/behaviors/Collider.jsx +190 -153
- package/kits/physics-2d/editors/SceneEditor.jsx +42 -6
- package/kits/physics-2d/editors/SelectionOverlay.jsx +57 -28
- package/kits/physics-2d/engine/collider.js +123 -105
- package/kits/physics-2d/engine/scene.js +10 -2
- package/kits/physics-2d/physics/behaviors/RigidBody.jsx +3 -0
- package/kits/physics-2d/physics/matterBridge.js +73 -24
- package/package.json +1 -1
- package/kits/physics-2d/engine/behaviorExtensions.js +0 -28
- package/kits/physics-2d/physics/extensions/collider.js +0 -15
|
@@ -1,21 +1,30 @@
|
|
|
1
|
-
// Single source of truth for
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// Single source of truth for Collider geometry. Lives in engine/ so SceneRuntime
|
|
2
|
+
// can use it directly; behaviors/Collider.jsx delegates here (its draw + the
|
|
3
|
+
// getColliderRect/intersects re-exports).
|
|
4
|
+
//
|
|
5
|
+
// A Collider is a LIST of shapes (`Collider.shapes`), each stored as a FRACTION
|
|
6
|
+
// of the actor's Layout box -- so a collider keeps its relative size and position
|
|
7
|
+
// when the box is resized (blueprint default OR per-instance override). Shapes:
|
|
8
|
+
// box: { type:'box', x, y, w, h } center + size, fractions of box
|
|
9
|
+
// circle: { type:'circle', x, y, r } center frac; r = frac of min(box side)
|
|
10
|
+
// triangle: { type:'triangle', points:[{x,y}×3] } fractions of box
|
|
11
|
+
// polygon: { type:'polygon', points:[{x,y}×N] }
|
|
12
|
+
// (x,y) are fractions of the box where (0,0)=top-left, (1,1)=bottom-right, so the
|
|
13
|
+
// box center is (0.5, 0.5); a circle radius `r` is a fraction of the box's SHORTER
|
|
14
|
+
// side (keeps circles round under non-uniform box scaling). Legacy single-shape
|
|
15
|
+
// colliders (shape/width/height/radius/offset[/mode]) normalize to one box shape.
|
|
16
|
+
//
|
|
17
|
+
// Shapes are in the UNROTATED box frame; the draw pipeline (scene.js) and the
|
|
18
|
+
// matter body (matterBridge) apply Layout.rotation, so nothing here rotates.
|
|
5
19
|
import { frameCount, renderSpriteFrame } from './pxart';
|
|
6
20
|
import { spriteDestRect } from './spriteGeometry';
|
|
7
21
|
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
// sprite object -- a sprite edit produces a fresh object (same invalidation
|
|
13
|
-
// lifecycle as Sprite.jsx's rendered-canvas cache), so a stale entry never
|
|
14
|
-
// outlives the art it was computed from.
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Sprite opaque-bounds -- only used now for the "auto-fit to sprite" action and
|
|
24
|
+
// the empty-actor placeholder. Colliders no longer live-track the sprite.
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
15
26
|
const opaqueBoundsCache = new WeakMap();
|
|
16
27
|
|
|
17
|
-
// Expand `acc` (in pixel units) to cover every opaque pixel of the canvas's
|
|
18
|
-
// current contents.
|
|
19
28
|
function accumulateOpaquePixels(acc, ctx, canvas) {
|
|
20
29
|
if (!ctx || canvas.width === 0 || canvas.height === 0) return;
|
|
21
30
|
const { data } = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
@@ -32,7 +41,6 @@ function accumulateOpaquePixels(acc, ctx, canvas) {
|
|
|
32
41
|
|
|
33
42
|
function opaqueBoundsFraction(sprite) {
|
|
34
43
|
if (opaqueBoundsCache.has(sprite)) return opaqueBoundsCache.get(sprite);
|
|
35
|
-
|
|
36
44
|
const { width, height } = sprite.resolution;
|
|
37
45
|
const canvas = document.createElement('canvas');
|
|
38
46
|
const ctx = canvas.getContext('2d');
|
|
@@ -42,7 +50,6 @@ function opaqueBoundsFraction(sprite) {
|
|
|
42
50
|
renderSpriteFrame(sprite, frame, canvas);
|
|
43
51
|
accumulateOpaquePixels(acc, ctx, canvas);
|
|
44
52
|
}
|
|
45
|
-
|
|
46
53
|
const bounds =
|
|
47
54
|
acc.minX <= acc.maxX && acc.minY <= acc.maxY && width > 0 && height > 0
|
|
48
55
|
? {
|
|
@@ -56,21 +63,12 @@ function opaqueBoundsFraction(sprite) {
|
|
|
56
63
|
return bounds;
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
// True when a resolved sprite has no opaque pixels across any frame
|
|
60
|
-
// transparent art (e.g. a brand-new blank drawing). The editor draws an
|
|
61
|
-
// empty-actor placeholder for these so a blank actor isn't invisible. Shares
|
|
62
|
-
// `opaqueBoundsFraction`'s per-sprite cache (a sprite edit yields a fresh
|
|
63
|
-
// object, which invalidates it).
|
|
66
|
+
// True when a resolved sprite has no opaque pixels across any frame.
|
|
64
67
|
export function spriteIsEmpty(sprite) {
|
|
65
68
|
return !sprite || opaqueBoundsFraction(sprite) === null;
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
// The
|
|
69
|
-
// falls back to the full Layout box: no Sprite, an unresolved sprite file,
|
|
70
|
-
// `Sprite.mode === 'tile'` (tiled art fills its cell by definition, so a
|
|
71
|
-
// per-cell bbox doesn't compose), or fully transparent art. Deliberately does
|
|
72
|
-
// NOT follow Sprite.jsx's FALLBACK_FILE placeholder-art fallback -- an
|
|
73
|
-
// unresolvable file means the Layout box, period.
|
|
71
|
+
// The sprite's opaque-pixel rect in world units through the draw mode, or null.
|
|
74
72
|
function autoRect(layout, spriteProps, sprite) {
|
|
75
73
|
if (!sprite || spriteProps.mode === 'tile') return null;
|
|
76
74
|
const bounds = opaqueBoundsFraction(sprite);
|
|
@@ -82,14 +80,10 @@ function autoRect(layout, spriteProps, sprite) {
|
|
|
82
80
|
width: bounds.width * dest.width,
|
|
83
81
|
height: bounds.height * dest.height,
|
|
84
82
|
};
|
|
85
|
-
// `cover`'s dest overflows the Layout box and the draw clips to it, so the
|
|
86
|
-
// opaque-bounds rect can spill outside the actor -- clamp to the visible box.
|
|
87
|
-
// A no-op for `stretch`/`fit`, whose dest already sits inside the box.
|
|
88
83
|
if (spriteProps.mode === 'cover') return intersectRect(rect, layout);
|
|
89
84
|
return rect;
|
|
90
85
|
}
|
|
91
86
|
|
|
92
|
-
// Rect intersection in world units; zero-size (never negative) when disjoint.
|
|
93
87
|
function intersectRect(rect, box) {
|
|
94
88
|
const x0 = Math.max(rect.x, box.x);
|
|
95
89
|
const y0 = Math.max(rect.y, box.y);
|
|
@@ -98,101 +92,125 @@ function intersectRect(rect, box) {
|
|
|
98
92
|
return { x: x0, y: y0, width: Math.max(0, x1 - x0), height: Math.max(0, y1 - y0) };
|
|
99
93
|
}
|
|
100
94
|
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
};
|
|
95
|
+
// ---------------------------------------------------------------------------
|
|
96
|
+
// Shape model
|
|
97
|
+
// ---------------------------------------------------------------------------
|
|
98
|
+
export const FULL_BOX_SHAPE = { type: 'box', x: 0.5, y: 0.5, w: 1, h: 1 };
|
|
99
|
+
|
|
100
|
+
// A collider's shapes as box-fraction shapes: the stored `shapes` list, or a
|
|
101
|
+
// single shape migrated from the retired single-shape fields.
|
|
102
|
+
export function colliderShapeFractions(collider, layout) {
|
|
103
|
+
if (Array.isArray(collider.shapes) && collider.shapes.length > 0) return collider.shapes;
|
|
104
|
+
return [legacyShapeFraction(collider, layout)];
|
|
112
105
|
}
|
|
113
106
|
|
|
114
|
-
|
|
107
|
+
// Migrate retired single-shape fields (shape/width/height/radius/offset) to one
|
|
108
|
+
// box-fraction shape. `mode: 'auto'` decks lose the live sprite-fit (a one-time
|
|
109
|
+
// snapshot isn't available here) and fall back to the full box.
|
|
110
|
+
function legacyShapeFraction(collider, layout) {
|
|
111
|
+
const bw = layout?.width || 1;
|
|
112
|
+
const bh = layout?.height || 1;
|
|
113
|
+
const cx = 0.5 + (collider.offsetX ?? 0) / bw;
|
|
114
|
+
const cy = 0.5 + (collider.offsetY ?? 0) / bh;
|
|
115
|
+
if (collider.shape === 'circle') {
|
|
116
|
+
const r =
|
|
117
|
+
collider.radius > 0
|
|
118
|
+
? collider.radius / Math.min(bw, bh)
|
|
119
|
+
: Math.min((collider.width ?? bw) / bw, (collider.height ?? bh) / bh) / 2;
|
|
120
|
+
return { type: 'circle', x: cx, y: cy, r };
|
|
121
|
+
}
|
|
122
|
+
return { type: 'box', x: cx, y: cy, w: (collider.width ?? bw) / bw, h: (collider.height ?? bh) / bh };
|
|
123
|
+
}
|
|
115
124
|
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
125
|
+
// Resolve one box-fraction shape to world coordinates (unrotated box frame).
|
|
126
|
+
function resolveShape(shape, layout) {
|
|
127
|
+
const bx = layout.x;
|
|
128
|
+
const by = layout.y;
|
|
129
|
+
const bw = layout.width;
|
|
130
|
+
const bh = layout.height;
|
|
131
|
+
const wx = (fx) => bx + fx * bw;
|
|
132
|
+
const wy = (fy) => by + fy * bh;
|
|
133
|
+
if (shape.type === 'circle') {
|
|
134
|
+
return { type: 'circle', cx: wx(shape.x ?? 0.5), cy: wy(shape.y ?? 0.5), radius: (shape.r ?? 0.5) * Math.min(bw, bh) };
|
|
124
135
|
}
|
|
125
|
-
|
|
136
|
+
if (shape.type === 'triangle' || shape.type === 'polygon') {
|
|
137
|
+
return { type: shape.type, points: (shape.points ?? []).map((p) => ({ x: wx(p.x), y: wy(p.y) })) };
|
|
138
|
+
}
|
|
139
|
+
const w = (shape.w ?? 1) * bw;
|
|
140
|
+
const h = (shape.h ?? 1) * bh;
|
|
141
|
+
const cx = wx(shape.x ?? 0.5);
|
|
142
|
+
const cy = wy(shape.y ?? 0.5);
|
|
143
|
+
return { type: 'box', cx, cy, width: w, height: h, x: cx - w / 2, y: cy - h / 2 };
|
|
126
144
|
}
|
|
127
145
|
|
|
128
|
-
//
|
|
129
|
-
|
|
130
|
-
// scene's sprite map (file path -> parsed sprite); omit it (or pass a map
|
|
131
|
-
// missing the actor's file) to force the Layout-box fallback, e.g. from a
|
|
132
|
-
// caller that only has raw scene data and no loaded sprites.
|
|
133
|
-
export function getColliderRect(actor, sprites) {
|
|
146
|
+
// World-space shapes for an actor's collider (list), or null if no Layout/Collider.
|
|
147
|
+
export function getColliderShapes(actor) {
|
|
134
148
|
const layout = actor?.components?.Layout;
|
|
135
149
|
const collider = actor?.components?.Collider;
|
|
136
150
|
if (!layout || !collider) return null;
|
|
151
|
+
return colliderShapeFractions(collider, layout).map((s) => resolveShape(s, layout));
|
|
152
|
+
}
|
|
137
153
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
width: rect.width,
|
|
143
|
-
height: rect.height,
|
|
144
|
-
};
|
|
154
|
+
// Back-compat single-shape accessor (the first shape).
|
|
155
|
+
export function getColliderShape(actor) {
|
|
156
|
+
const shapes = getColliderShapes(actor);
|
|
157
|
+
return shapes && shapes.length ? shapes[0] : null;
|
|
145
158
|
}
|
|
146
159
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
y: rect.y,
|
|
164
|
-
width: rect.width,
|
|
165
|
-
height: rect.height,
|
|
166
|
-
cx: rect.x + rect.width / 2,
|
|
167
|
-
cy: rect.y + rect.height / 2,
|
|
168
|
-
radius,
|
|
169
|
-
};
|
|
160
|
+
function shapeAabb(s) {
|
|
161
|
+
if (s.type === 'circle') return [s.cx - s.radius, s.cy - s.radius, s.cx + s.radius, s.cy + s.radius];
|
|
162
|
+
if (s.type === 'triangle' || s.type === 'polygon') {
|
|
163
|
+
let x0 = Infinity;
|
|
164
|
+
let y0 = Infinity;
|
|
165
|
+
let x1 = -Infinity;
|
|
166
|
+
let y1 = -Infinity;
|
|
167
|
+
for (const p of s.points) {
|
|
168
|
+
if (p.x < x0) x0 = p.x;
|
|
169
|
+
if (p.y < y0) y0 = p.y;
|
|
170
|
+
if (p.x > x1) x1 = p.x;
|
|
171
|
+
if (p.y > y1) y1 = p.y;
|
|
172
|
+
}
|
|
173
|
+
return [x0, y0, x1, y1];
|
|
174
|
+
}
|
|
175
|
+
return [s.x, s.y, s.x + s.width, s.y + s.height];
|
|
170
176
|
}
|
|
171
177
|
|
|
172
|
-
//
|
|
173
|
-
//
|
|
174
|
-
//
|
|
175
|
-
|
|
176
|
-
|
|
178
|
+
// AABB of the whole collider (union of all shapes), world units -- for
|
|
179
|
+
// scene.overlaps / camera / coarse queries. Second arg accepted for call-site
|
|
180
|
+
// compat (colliders no longer read sprites at resolve time).
|
|
181
|
+
export function getColliderRect(actor) {
|
|
182
|
+
const shapes = getColliderShapes(actor);
|
|
183
|
+
if (!shapes || shapes.length === 0) return null;
|
|
184
|
+
let minX = Infinity;
|
|
185
|
+
let minY = Infinity;
|
|
186
|
+
let maxX = -Infinity;
|
|
187
|
+
let maxY = -Infinity;
|
|
188
|
+
for (const s of shapes) {
|
|
189
|
+
const [x0, y0, x1, y1] = shapeAabb(s);
|
|
190
|
+
if (x0 < minX) minX = x0;
|
|
191
|
+
if (y0 < minY) minY = y0;
|
|
192
|
+
if (x1 > maxX) maxX = x1;
|
|
193
|
+
if (y1 > maxY) maxY = y1;
|
|
194
|
+
}
|
|
195
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// A single box-fraction shape matching the sprite's opaque bounds through the
|
|
199
|
+
// draw mode, or null when there's nothing to fit to. `asCircle` returns a circle
|
|
200
|
+
// that encloses those bounds. Powers the inspector's "Auto-fit to sprite".
|
|
201
|
+
export function computeAutoFit(actor, sprites, asCircle = false) {
|
|
177
202
|
const rawLayout = actor?.components?.Layout;
|
|
178
203
|
const spriteProps = actor?.components?.Sprite;
|
|
179
204
|
if (!rawLayout || !spriteProps) return null;
|
|
180
|
-
// Blueprint templates omit x/y (position is instance-local). x/y cancel out of
|
|
181
|
-
// the offset delta below, so default them to 0 -- otherwise an undefined x/y
|
|
182
|
-
// (auto-fitting a template, e.g. on add) turns the offsets into NaN.
|
|
183
205
|
const layout = { ...rawLayout, x: rawLayout.x ?? 0, y: rawLayout.y ?? 0 };
|
|
184
206
|
const rect = autoRect(layout, spriteProps, sprites?.[spriteProps.file]);
|
|
185
207
|
if (!rect || rect.width <= 0 || rect.height <= 0) return null;
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
const
|
|
190
|
-
return {
|
|
191
|
-
|
|
192
|
-
height: Math.round(rect.height),
|
|
193
|
-
offsetX: Math.round(rect.x - centeredX),
|
|
194
|
-
offsetY: Math.round(rect.y - centeredY),
|
|
195
|
-
};
|
|
208
|
+
const x = (rect.x + rect.width / 2 - layout.x) / layout.width;
|
|
209
|
+
const y = (rect.y + rect.height / 2 - layout.y) / layout.height;
|
|
210
|
+
const w = rect.width / layout.width;
|
|
211
|
+
const h = rect.height / layout.height;
|
|
212
|
+
if (asCircle) return { type: 'circle', x, y, r: Math.max(w, h) / 2 };
|
|
213
|
+
return { type: 'box', x, y, w, h };
|
|
196
214
|
}
|
|
197
215
|
|
|
198
216
|
export function intersects(a, b) {
|
|
@@ -281,7 +281,15 @@ export class SceneRuntime {
|
|
|
281
281
|
}
|
|
282
282
|
|
|
283
283
|
actorAt(x, y) {
|
|
284
|
+
return this.actorsAt(x, y)[0] ?? null;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// All actors whose Layout box contains the point, ordered topmost-first (high
|
|
288
|
+
// z -> low z). Used by the editor's click-to-cycle so repeated clicks in the
|
|
289
|
+
// same spot can walk down through overlapping actors.
|
|
290
|
+
actorsAt(x, y) {
|
|
284
291
|
const actors = this.getActors().slice().reverse();
|
|
292
|
+
const hits = [];
|
|
285
293
|
for (const actor of actors) {
|
|
286
294
|
const layout = getLayout(actor);
|
|
287
295
|
if (!layout) continue;
|
|
@@ -291,10 +299,10 @@ export class SceneRuntime {
|
|
|
291
299
|
y >= layout.y &&
|
|
292
300
|
y <= layout.y + layout.height
|
|
293
301
|
) {
|
|
294
|
-
|
|
302
|
+
hits.push(actor);
|
|
295
303
|
}
|
|
296
304
|
}
|
|
297
|
-
return
|
|
305
|
+
return hits;
|
|
298
306
|
}
|
|
299
307
|
|
|
300
308
|
actorIdsInRect(rect) {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// emulated here (applyWorldGravity, and kinematic == static-moved-from-Layout).
|
|
17
17
|
|
|
18
18
|
import Matter from 'matter-js';
|
|
19
|
+
import { getColliderShapes } from '../engine/collider';
|
|
19
20
|
|
|
20
21
|
export const DEG_TO_RAD = Math.PI / 180;
|
|
21
22
|
export const RAD_TO_DEG = 180 / Math.PI;
|
|
@@ -46,39 +47,78 @@ export function rectCenter(rect) {
|
|
|
46
47
|
return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 };
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
// One matter part (a body) for a resolved, world-space collider shape. Triangles
|
|
51
|
+
// and convex polygons go through `fromVertices`; a concave polygon (which would
|
|
52
|
+
// need the poly-decomp lib we don't ship) or a degenerate shape falls back to its
|
|
53
|
+
// AABB box so it still collides.
|
|
54
|
+
function shapeToPart(s) {
|
|
55
|
+
if (!s) return null;
|
|
56
|
+
if (s.type === 'circle') return Matter.Bodies.circle(s.cx, s.cy, s.radius > 0 ? s.radius : 0.5);
|
|
57
|
+
if ((s.type === 'triangle' || s.type === 'polygon') && s.points && s.points.length >= 3) {
|
|
58
|
+
let cx = 0;
|
|
59
|
+
let cy = 0;
|
|
60
|
+
let x0 = Infinity;
|
|
61
|
+
let y0 = Infinity;
|
|
62
|
+
let x1 = -Infinity;
|
|
63
|
+
let y1 = -Infinity;
|
|
64
|
+
for (const p of s.points) {
|
|
65
|
+
cx += p.x;
|
|
66
|
+
cy += p.y;
|
|
67
|
+
if (p.x < x0) x0 = p.x;
|
|
68
|
+
if (p.y < y0) y0 = p.y;
|
|
69
|
+
if (p.x > x1) x1 = p.x;
|
|
70
|
+
if (p.y > y1) y1 = p.y;
|
|
71
|
+
}
|
|
72
|
+
const part = Matter.Bodies.fromVertices(cx / s.points.length, cy / s.points.length, [s.points]);
|
|
73
|
+
if (part) return part;
|
|
74
|
+
return Matter.Bodies.rectangle((x0 + x1) / 2, (y0 + y1) / 2, Math.max(1, x1 - x0), Math.max(1, y1 - y0));
|
|
75
|
+
}
|
|
76
|
+
return Matter.Bodies.rectangle(s.cx, s.cy, Math.max(1, s.width), Math.max(1, s.height));
|
|
55
77
|
}
|
|
56
78
|
|
|
57
|
-
// Structural signature: when this changes we rebuild the body (shape/
|
|
58
|
-
// rather than live-patching material props in place.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
79
|
+
// Structural signature: when this changes we rebuild the body (shape set / sizes
|
|
80
|
+
// / type) rather than live-patching material props in place. Resolved sizes are
|
|
81
|
+
// included, so resizing the Layout box (which rescales every fraction shape)
|
|
82
|
+
// triggers a rebuild.
|
|
83
|
+
export function bodySignature(actor) {
|
|
84
|
+
const shapes = getColliderShapes(actor) ?? [];
|
|
85
|
+
const sig = shapes
|
|
86
|
+
.map((s) =>
|
|
87
|
+
s.type === 'circle'
|
|
88
|
+
? `c${Math.round(s.radius)}`
|
|
89
|
+
: s.type === 'box'
|
|
90
|
+
? `b${Math.round(s.width)}x${Math.round(s.height)}`
|
|
91
|
+
: `${s.type[0]}${(s.points ?? []).length}`
|
|
92
|
+
)
|
|
93
|
+
.join(',');
|
|
94
|
+
return `${sig}|${rigidBodyType(actor)}`;
|
|
66
95
|
}
|
|
67
96
|
|
|
68
|
-
// Build a fresh matter body
|
|
97
|
+
// Build a fresh matter body -- a single part, or a compound of all collider
|
|
98
|
+
// shapes -- anchored so `body.position` is the collider's AABB center (matching
|
|
99
|
+
// the center-offset PhysicsSystem caches), oriented by Layout.
|
|
69
100
|
export function createBody(actor, rect) {
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
101
|
+
const parts = (getColliderShapes(actor) ?? []).map(shapeToPart).filter(Boolean);
|
|
102
|
+
let body;
|
|
103
|
+
if (parts.length === 0) {
|
|
104
|
+
const c = rectCenter(rect);
|
|
105
|
+
body = Matter.Bodies.rectangle(c.x, c.y, Math.max(1, rect.width), Math.max(1, rect.height));
|
|
106
|
+
} else if (parts.length === 1) {
|
|
107
|
+
body = parts[0];
|
|
108
|
+
} else {
|
|
109
|
+
body = Matter.Body.create({ parts });
|
|
110
|
+
}
|
|
111
|
+
Matter.Body.setPosition(body, rectCenter(rect));
|
|
112
|
+
Matter.Body.setAngle(body, (actor.components.Layout.rotation ?? 0) * DEG_TO_RAD);
|
|
76
113
|
body.plugin.actorId = actor.id;
|
|
77
114
|
Matter.Body.setStatic(body, rigidBodyType(actor) !== 'dynamic');
|
|
78
115
|
patchBody(body, actor);
|
|
79
116
|
const rb = actor.components.RigidBody;
|
|
80
|
-
if (rb && rigidBodyType(actor) === 'dynamic'
|
|
81
|
-
|
|
117
|
+
if (rb && rigidBodyType(actor) === 'dynamic') {
|
|
118
|
+
if (rb.velocityX || rb.velocityY) {
|
|
119
|
+
Matter.Body.setVelocity(body, { x: rb.velocityX ?? 0, y: rb.velocityY ?? 0 });
|
|
120
|
+
}
|
|
121
|
+
if (rb.angularVelocity) Matter.Body.setAngularVelocity(body, rb.angularVelocity * DEG_TO_RAD);
|
|
82
122
|
}
|
|
83
123
|
return body;
|
|
84
124
|
}
|
|
@@ -89,12 +129,21 @@ export function patchBody(body, actor) {
|
|
|
89
129
|
const rb = actor.components.RigidBody;
|
|
90
130
|
body.restitution = collider.bounciness ?? 0;
|
|
91
131
|
body.friction = collider.friction ?? 0.1;
|
|
132
|
+
body.frictionStatic = collider.frictionStatic ?? 0.5;
|
|
92
133
|
// `isTrigger` is the source of truth for solid-vs-sensor; legacy decks that
|
|
93
134
|
// used the retired `kind: 'pickup'` label are still honored as sensors.
|
|
94
135
|
body.isSensor = Boolean(collider.isTrigger || collider.kind === 'pickup');
|
|
95
136
|
body.frictionAir = rb?.drag ?? 0.01;
|
|
96
137
|
body.plugin.gravityScale = rb?.gravityScale ?? 1;
|
|
97
138
|
body.plugin.angularDrag = rb?.angularDrag ?? 0;
|
|
139
|
+
// Density sets mass (= density x area) -- DYNAMIC bodies only. On a static or
|
|
140
|
+
// kinematic body, setDensity would replace the infinite mass that setStatic
|
|
141
|
+
// gave it with a finite inverse mass, corrupting it as an immovable obstacle
|
|
142
|
+
// (things fall through it / the sim NaNs). It recomputes inertia, so it runs
|
|
143
|
+
// before the freezeRotation override below.
|
|
144
|
+
if (rigidBodyType(actor) === 'dynamic') {
|
|
145
|
+
Matter.Body.setDensity(body, collider.density > 0 ? collider.density : 0.001);
|
|
146
|
+
}
|
|
98
147
|
if (rb?.freezeRotation) Matter.Body.setInertia(body, Infinity);
|
|
99
148
|
}
|
|
100
149
|
|
package/package.json
CHANGED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
// Behavior field extensions. A self-contained kit module (like `physics/`) can
|
|
2
|
-
// add fields to a behavior defined in the shared core WITHOUT editing that
|
|
3
|
-
// behavior's file -- so the core behavior stays byte-identical across kits and
|
|
4
|
-
// the module owns its own additions. Each extension module under any
|
|
5
|
-
// `<module>/extensions/*.js` exports:
|
|
6
|
-
//
|
|
7
|
-
// export const behaviorExtension = {
|
|
8
|
-
// behaviorName: 'Collider',
|
|
9
|
-
// defaultProps: { bounciness: 0, friction: 0.1 },
|
|
10
|
-
// };
|
|
11
|
-
//
|
|
12
|
-
// A behavior folds its registered extensions into its own `defaultProps` (see
|
|
13
|
-
// behaviors/Collider.jsx `...extensionDefaultProps('Collider')`); the inspector
|
|
14
|
-
// already renders leftover defaultProps generically, so registered fields show
|
|
15
|
-
// up with no inspector change. Empty in a kit with no `*/extensions/` dir (e.g.
|
|
16
|
-
// basic-2d). Symmetric with engine/systemRegistry.js and editors/behaviorRegistry.js.
|
|
17
|
-
const modules = import.meta.glob('../*/extensions/*.js', { eager: true });
|
|
18
|
-
const extensions = Object.values(modules)
|
|
19
|
-
.map((mod) => mod.behaviorExtension)
|
|
20
|
-
.filter(Boolean);
|
|
21
|
-
|
|
22
|
-
// Merged defaultProps that registered extensions contribute to `behaviorName`
|
|
23
|
-
// (empty object when none). Later extensions win on a key collision.
|
|
24
|
-
export function extensionDefaultProps(behaviorName) {
|
|
25
|
-
return extensions
|
|
26
|
-
.filter((ext) => ext.behaviorName === behaviorName)
|
|
27
|
-
.reduce((acc, ext) => ({ ...acc, ...ext.defaultProps }), {});
|
|
28
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
// Physics contributes material fields to the shared Collider behavior WITHOUT
|
|
2
|
-
// editing behaviors/Collider.jsx -- that file stays identical to basic-2d's, so
|
|
3
|
-
// the drift check treats it as a converged shared file. These fields are read
|
|
4
|
-
// by the physics simulation (see physics/matterBridge.js); a kit without the
|
|
5
|
-
// physics module never registers them, so its Collider has no material fields.
|
|
6
|
-
// See engine/behaviorExtensions.js for how this is discovered.
|
|
7
|
-
export const behaviorExtension = {
|
|
8
|
-
behaviorName: 'Collider',
|
|
9
|
-
defaultProps: {
|
|
10
|
-
// `bounciness` = restitution: 0 (dead) to ~1 (very bouncy); can exceed 1.
|
|
11
|
-
bounciness: 0,
|
|
12
|
-
// `friction` = surface friction.
|
|
13
|
-
friction: 0.1,
|
|
14
|
-
},
|
|
15
|
-
};
|