castle-web-cli 0.4.114 → 0.4.116

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 (95) hide show
  1. package/dist/agent-prompts.js +2 -2
  2. package/dist/agent.d.ts +9 -9
  3. package/dist/agent.js +590 -589
  4. package/dist/castleJson.d.ts +7 -0
  5. package/dist/castleJson.js +10 -0
  6. package/dist/editorConfig.d.ts +34 -0
  7. package/dist/editorConfig.js +125 -0
  8. package/dist/ide.js +245 -120
  9. package/dist/imports.d.ts +10 -0
  10. package/dist/imports.js +149 -17
  11. package/dist/init.d.ts +3 -0
  12. package/dist/init.js +113 -88
  13. package/dist/install.d.ts +1 -1
  14. package/dist/install.js +26 -24
  15. package/dist/serve.js +16 -0
  16. package/dist/shell/assets/index-DiPlPGyg.js +144 -0
  17. package/dist/shell/assets/index-Y6cJRRCX.css +1 -0
  18. package/dist/shell/index.html +2 -2
  19. package/dist/unsupportedMedia.d.ts +1 -0
  20. package/dist/unsupportedMedia.js +54 -0
  21. package/dist/vitePlugins.js +13 -20
  22. package/kits/basic-2d/castle.json +1 -1
  23. package/kits/basic-2d/editors/behaviorRegistry.js +5 -7
  24. package/kits/physics-2d/CLAUDE.md +65 -34
  25. package/kits/physics-2d/{physics/behaviors → behaviors}/AnalogStick.jsx +3 -3
  26. package/kits/physics-2d/behaviors/Collider.jsx +1 -1
  27. package/kits/physics-2d/{physics/behaviors → behaviors}/Draggable.jsx +3 -3
  28. package/kits/physics-2d/{physics/behaviors → behaviors}/Joints.jsx +5 -5
  29. package/kits/physics-2d/{physics/behaviors → behaviors}/RigidBody.jsx +2 -2
  30. package/kits/physics-2d/{physics/behaviors → behaviors}/Slingshot.jsx +3 -3
  31. package/kits/physics-2d/behaviors/Sound.jsx +152 -0
  32. package/kits/physics-2d/behaviors/Sprite.jsx +112 -42
  33. package/kits/physics-2d/behaviors/Tone.jsx +83 -0
  34. package/kits/physics-2d/behaviors/Video.jsx +111 -0
  35. package/kits/physics-2d/behaviors/tint.js +24 -9
  36. package/kits/physics-2d/blueprints/ball.scene +1 -1
  37. package/kits/physics-2d/blueprints/block.scene +1 -1
  38. package/kits/physics-2d/blueprints/cauldron.scene +1 -1
  39. package/kits/physics-2d/blueprints/crate.scene +1 -1
  40. package/kits/physics-2d/castle.json +74 -4
  41. package/kits/physics-2d/docs/pxart-format.md +537 -51
  42. package/kits/physics-2d/editors/BlueprintLibrary.jsx +7 -3
  43. package/kits/physics-2d/editors/ImageViewer.jsx +206 -0
  44. package/kits/physics-2d/editors/MediaPlayer.jsx +57 -0
  45. package/kits/physics-2d/editors/PxArtEditor.jsx +1794 -65
  46. package/kits/physics-2d/editors/SceneEditor.jsx +7 -3
  47. package/kits/physics-2d/editors/SingleEditor.jsx +8 -0
  48. package/kits/physics-2d/editors/behaviorRegistry.js +5 -7
  49. package/kits/physics-2d/editors/brushFit.js +535 -0
  50. package/kits/physics-2d/editors/brushShapes.js +140 -0
  51. package/kits/physics-2d/editors/mediaFile.js +31 -0
  52. package/kits/physics-2d/editors/mediaViewer.module.css +128 -0
  53. package/kits/physics-2d/editors/pathOverlay.js +340 -0
  54. package/kits/physics-2d/editors/pathTools.js +1906 -0
  55. package/kits/physics-2d/editors/pixelCanvas.js +13 -0
  56. package/kits/physics-2d/editors/pixelEditorChrome.jsx +2 -2
  57. package/kits/physics-2d/editors/pixelGeometry.js +4 -2
  58. package/kits/physics-2d/editors/pixelInspector.jsx +410 -37
  59. package/kits/physics-2d/editors/pxArtEditorModel.js +172 -16
  60. package/kits/physics-2d/editors/pxArtTimeline.jsx +163 -43
  61. package/kits/physics-2d/editors/pxArtTimeline.module.css +31 -5
  62. package/kits/physics-2d/engine/ScenePlayer.jsx +1 -0
  63. package/kits/physics-2d/engine/art.js +105 -0
  64. package/kits/physics-2d/engine/assets.js +12 -4
  65. package/kits/physics-2d/engine/audioContext.js +34 -0
  66. package/kits/physics-2d/engine/blueprint.js +3 -3
  67. package/kits/physics-2d/engine/collider.js +28 -17
  68. package/kits/physics-2d/engine/files.js +77 -1
  69. package/kits/physics-2d/engine/liveReload.js +1 -1
  70. package/kits/physics-2d/engine/media.js +212 -0
  71. package/kits/physics-2d/{physics → engine/physics}/PhysicsSystem.js +1 -1
  72. package/kits/physics-2d/{physics → engine/physics}/jointArt.js +3 -4
  73. package/kits/physics-2d/{physics → engine/physics}/matterBridge.js +2 -9
  74. package/kits/physics-2d/engine/pxart.js +153 -35
  75. package/kits/physics-2d/engine/pxartPath.js +1356 -0
  76. package/kits/physics-2d/engine/pxartSmooth.js +276 -125
  77. package/kits/physics-2d/engine/scene.js +12 -0
  78. package/kits/physics-2d/engine/tone.js +112 -0
  79. package/kits/physics-2d/engine/ui.jsx +22 -1
  80. package/kits/physics-2d/engine/ui.module.css +36 -12
  81. package/kits/physics-2d/package-lock.json +1 -1
  82. package/kits/physics-2d/scripts/draw.mjs +7 -7
  83. package/kits/physics-2d/scripts/import-svg.mjs +1231 -0
  84. package/kits/physics-2d/scripts/svg-emission-guide.md +92 -0
  85. package/kits/physics-2d/systems/media.js +34 -0
  86. package/kits/physics-2d/systems/physics.js +12 -3
  87. package/package.json +1 -1
  88. package/dist/shell/assets/index-CqpY1xZR.js +0 -144
  89. package/dist/shell/assets/index-DCwVFL5u.css +0 -1
  90. package/kits/physics-2d/physics/index.js +0 -26
  91. /package/kits/physics-2d/drawings/{block.pxart → block.sprite} +0 -0
  92. /package/kits/physics-2d/drawings/{cauldron.pxart → cauldron.sprite} +0 -0
  93. /package/kits/physics-2d/drawings/{joint-rope.pxart → joint-rope.sprite} +0 -0
  94. /package/kits/physics-2d/{physics → engine/physics}/controls.js +0 -0
  95. /package/kits/physics-2d/{physics → engine/physics}/joints.js +0 -0
@@ -7,71 +7,38 @@
7
7
  // the normal 1px/cell `renderSpriteFrame`.
8
8
  //
9
9
  // This is a LOCAL, per-pixel kernel filter — the same shape of algorithm as
10
- // Animal Crossing's actual smoothing (xBRZ-style template matching), not the
11
- // global boundary-trace-and-round approach this file used to implement. That
12
- // approach traced each region's FULL pixel-boundary loop and could detect
13
- // long straight/staircase runs across the whole loop; it looked fine on
14
- // blocky shapes but flattened organic curves it decided were "too regular",
15
- // which isn't fixable by tuning since the run detection itself is the
16
- // problem. This version never looks past a pixel's immediate 3x3
17
- // neighborhood, so there is no run/regularity detection to mis-fire — it
18
- // physically cannot flatten a curve, because it has no notion of a curve at
19
- // all, only of each corner in isolation.
20
- //
21
- // Approach, per source pixel P:
22
- // 1. Composite the frame normally (`renderSpriteFrame` — this already
23
- // handles layer visibility/opacity/blend), then read back the native
24
- // resolution raster. Regions are exact-RGBA-equality color runs, same as
25
- // before.
26
- // 2. Supersample: P gets its own `scale` x `scale` block of the output
27
- // canvas (plain grid subdivision — every output pixel belongs to
28
- // exactly one source pixel's block, so there is no possibility of a
29
- // gap or overlap between neighboring pixels' rendering).
30
- // 3. Fill P's whole block with its own color, then independently classify
31
- // each of P's 4 corners against ONLY the 3 pixels touching that corner
32
- // (2 edge-adjacent neighbors + 1 diagonal) and, for genuine convex
33
- // corners, paint a `cornerRadius`-sized quarter-circle "cut" over that
34
- // corner revealing the relevant neighbor's color (see `cornerFill`).
35
- // Because every pixel only ever paints within its OWN block, this recoloring
36
- // can never create a gap: it's the same guarantee a supersampled nearest-
37
- // neighbor render already has, just with a softened corner instead of a hard
38
- // one.
39
- //
40
- // Kept as a sibling of pxart.js (rather than inside it) so the format
41
- // parser/serializer stays focused on the on-disk shape.
10
+ // Animal Crossing's actual smoothing (xBRZ-style template matching), not a
11
+ // global vectorization pass. Under `renderer: "vector"`,
12
+ // `renderSmoothCompositeFrame` paints each path layer's shape stack
13
+ // analytically into the supersampled canvas (even-odd fills + 1-cell-wide
14
+ // round-cap strokes) in painter's order.
42
15
  // ============================================================================
43
16
 
44
- import { MAX_CORNER_RADIUS, renderSpriteFrame } from './pxart';
17
+ import {
18
+ MAX_CORNER_RADIUS,
19
+ colorForKeyV2,
20
+ isVectorRenderer,
21
+ paletteLookup,
22
+ renderSpriteFrame,
23
+ resolveCell,
24
+ } from './pxart';
25
+ import { absoluteSegment, normalizeSubpath } from './pxartPath.js';
45
26
 
46
27
  export const DEFAULT_SMOOTH_SCALE = 8;
47
28
 
48
- // Fallback only for a caller that omits `cornerRadius` entirely; every real
49
- // caller passes the sprite's own file-level `cornerRadius` value (see
50
- // docs/pxart-format.md and Sprite.jsx/PxArtEditor.jsx), which is how the
51
- // corner radius ends up being a portable, per-sprite part of the format
52
- // rather than a fixed constant every smooth sprite is stuck with.
53
29
  const FALLBACK_CORNER_RADIUS = 0.25;
54
30
 
55
- /** Render one frame of a Sprite with locally corner-rounded fills,
56
- * supersampled into `canvas` (sized to width*scale x height*scale). Reuses
57
- * `renderSpriteFrame` for compositing (layers, opacity, visibility, blend),
58
- * so those keep working unchanged. `cornerRadius` is in native-pixel units,
59
- * clamped to `MAX_CORNER_RADIUS` (two cuts on the same edge must not
60
- * overlap). */
31
+ /** Render one frame with locally corner-rounded fills. */
61
32
  export function renderSmoothSpriteFrame(
62
33
  sprite,
63
34
  frameIndex,
64
35
  canvas,
65
- { scale = DEFAULT_SMOOTH_SCALE, cornerRadius = FALLBACK_CORNER_RADIUS } = {}
36
+ { scale = DEFAULT_SMOOTH_SCALE, cornerRadius = FALLBACK_CORNER_RADIUS } = {},
66
37
  ) {
67
38
  const clampedRadius = Math.min(MAX_CORNER_RADIUS, Math.max(0, cornerRadius));
68
- const { width, height } = sprite.resolution;
69
- canvas.width = Math.max(1, Math.round(width * scale));
70
- canvas.height = Math.max(1, Math.round(height * scale));
71
- const ctx = canvas.getContext('2d');
72
- if (!ctx) return;
73
- ctx.clearRect(0, 0, canvas.width, canvas.height);
74
- if (width <= 0 || height <= 0) return;
39
+ const target = setupScaledCanvas(canvas, sprite.resolution, scale);
40
+ if (!target) return;
41
+ const { ctx, width, height } = target;
75
42
 
76
43
  const native = document.createElement('canvas');
77
44
  renderSpriteFrame(sprite, frameIndex, native);
@@ -79,9 +46,6 @@ export function renderSmoothSpriteFrame(
79
46
  const data = nctx?.getImageData(0, 0, width, height).data;
80
47
  if (!data) return;
81
48
 
82
- // null = out of canvas OR fully transparent; both read as "not this pixel's
83
- // color" to every classification below, same as the old mask's treatment
84
- // of out-of-canvas/transparent as background.
85
49
  const colorAt = (x, y) => {
86
50
  if (x < 0 || x >= width || y < 0 || y >= height) return null;
87
51
  const i = (y * width + x) * 4;
@@ -90,31 +54,215 @@ export function renderSmoothSpriteFrame(
90
54
  };
91
55
 
92
56
  ctx.save();
93
- ctx.scale(scale, scale);
94
- // Every pixel is processed, including transparent ones: a fully-enclosed
95
- // transparent "hole" is, from its own corners' point of view, exactly the
96
- // same kind of convex corner as an opaque pixel poking out of a
97
- // background — it needs to cut (and round) its OWN corners the same way,
98
- // or an enclosed 1px hole would stay a hard square forever (its opaque
99
- // neighbors never round toward it, by the same-neighbor rule below).
57
+ ctx.imageSmoothingEnabled = false;
100
58
  for (let y = 0; y < height; y++) {
101
- for (let x = 0; x < width; x++) drawPixelBlock(ctx, colorAt, x, y, colorAt(x, y), clampedRadius);
59
+ for (let x = 0; x < width; x++) {
60
+ drawPixelBlock(ctx, colorAt, x, y, scale, colorAt(x, y), clampedRadius);
61
+ }
102
62
  }
103
63
  ctx.restore();
104
64
  }
105
65
 
66
+ // Render the "smooth" finish one layer at a time. Path layers paint their
67
+ // shape stack analytically at supersample; pixel layers use maximum corner
68
+ // smoothing. Layer visibility and opacity remain authoritative.
69
+ export function renderSmoothCompositeFrame(
70
+ sprite,
71
+ frameIndex,
72
+ canvas,
73
+ { scale = DEFAULT_SMOOTH_SCALE } = {},
74
+ ) {
75
+ const target = setupScaledCanvas(canvas, sprite.resolution, scale);
76
+ if (!target) return;
77
+ const { ctx, width, height } = target;
78
+
79
+ const lookup = paletteLookup(sprite.palette);
80
+ const prevAlpha = ctx.globalAlpha;
81
+ for (const layer of sprite.layers) {
82
+ if (!layer.visible || layer.opacity <= 0) continue;
83
+ const resolved = resolveCell(layer, frameIndex);
84
+ if (!resolved) continue;
85
+ ctx.globalAlpha = Math.min(1, Math.max(0, layer.opacity));
86
+ drawScaledLayer(ctx, sprite, layer, resolved, frameIndex, {
87
+ scale,
88
+ width,
89
+ height,
90
+ lookup,
91
+ analyticPath: true,
92
+ cornerRadius: MAX_CORNER_RADIUS,
93
+ });
94
+ }
95
+ ctx.globalAlpha = prevAlpha;
96
+ }
97
+
98
+ /**
99
+ * Draw one resolved layer into `ctx` at `scale`. A path layer renders
100
+ * analytically from its shape stack when `analyticPath`; everything else goes
101
+ * through the corner kernel at `cornerRadius`.
102
+ */
103
+ function drawScaledLayer(
104
+ ctx,
105
+ sprite,
106
+ layer,
107
+ resolved,
108
+ frameIndex,
109
+ { scale, width, height, lookup, analyticPath, cornerRadius },
110
+ ) {
111
+ if (analyticPath && layer.kind === 'path' && resolved.path) {
112
+ const layerCanvas = renderScaledPathCell(resolved.path, width, height, scale, lookup);
113
+ ctx.drawImage(layerCanvas, Math.round(resolved.x * scale), Math.round(resolved.y * scale));
114
+ return;
115
+ }
116
+ const layerCanvas = document.createElement('canvas');
117
+ const single = { ...sprite, layers: [{ ...layer, visible: true, opacity: 1 }] };
118
+ renderSmoothSpriteFrame(single, frameIndex, layerCanvas, { scale, cornerRadius });
119
+ ctx.drawImage(layerCanvas, 0, 0);
120
+ }
121
+
122
+ /** Finish render of one (layer, frame) cell — path layers analytic under the
123
+ * vector renderer, else baked grid + corner kernel. */
124
+ export function renderSmoothLayerCell(
125
+ sprite,
126
+ layerIndex,
127
+ frameIndex,
128
+ canvas,
129
+ { scale = DEFAULT_SMOOTH_SCALE } = {},
130
+ ) {
131
+ const layer = sprite.layers[layerIndex];
132
+ if (!layer) return;
133
+ const resolved = resolveCell(layer, frameIndex);
134
+ if (!resolved) return;
135
+ const vector = isVectorRenderer(sprite.renderer);
136
+ const radius = sprite.cornerRadius ?? 0;
137
+ // Nothing to add over the plain nearest-neighbor blit.
138
+ if (!vector && radius === 0) return;
139
+
140
+ const { width, height } = sprite.resolution;
141
+ const lookup = paletteLookup(sprite.palette);
142
+ const target = setupScaledCanvas(canvas, sprite.resolution, scale);
143
+ if (!target) return;
144
+ const { ctx } = target;
145
+
146
+ const prevAlpha = ctx.globalAlpha;
147
+ ctx.globalAlpha = Math.min(1, Math.max(0, layer.opacity ?? 1));
148
+ drawScaledLayer(ctx, sprite, layer, resolved, frameIndex, {
149
+ scale,
150
+ width,
151
+ height,
152
+ lookup,
153
+ analyticPath: vector,
154
+ cornerRadius: vector ? MAX_CORNER_RADIUS : radius,
155
+ });
156
+ ctx.globalAlpha = prevAlpha;
157
+ }
158
+
159
+ function setupScaledCanvas(canvas, resolution, scale) {
160
+ const { width, height } = resolution;
161
+ canvas.width = Math.max(1, Math.round(width * scale));
162
+ canvas.height = Math.max(1, Math.round(height * scale));
163
+ const ctx = canvas.getContext('2d');
164
+ if (!ctx) return null;
165
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
166
+ return width > 0 && height > 0 ? { ctx, width, height } : null;
167
+ }
168
+
169
+ /**
170
+ * Analytic smooth render of a stacked path: fills as even-odd canvas paths,
171
+ * strokes as 1-cell-wide analytic strokes with round caps/joins along the
172
+ * true curves, shapes in stack order.
173
+ */
174
+ function renderScaledPathCell(path, width, height, scale, lookup) {
175
+ const w = Math.max(1, Math.round(width * scale));
176
+ const h = Math.max(1, Math.round(height * scale));
177
+ const off = document.createElement('canvas');
178
+ off.width = w;
179
+ off.height = h;
180
+ const octx = off.getContext('2d');
181
+ if (!octx) return off;
182
+
183
+ for (const shape of path?.shapes ?? []) {
184
+ if (shape.fill) {
185
+ const color = colorForKeyV2(lookup, shape.fill);
186
+ if (color) {
187
+ const geometry = buildFillPath2D(shape, scale);
188
+ if (geometry) {
189
+ octx.fillStyle = color;
190
+ octx.fill(geometry, 'evenodd');
191
+ }
192
+ }
193
+ }
194
+ if (shape.stroke) {
195
+ const color = colorForKeyV2(lookup, shape.stroke);
196
+ if (color) {
197
+ octx.save();
198
+ octx.strokeStyle = color;
199
+ octx.lineWidth = scale; // 1 native cell
200
+ octx.lineCap = 'round';
201
+ octx.lineJoin = 'round';
202
+ for (const sub of shape.subpaths ?? []) {
203
+ const strokePath = buildSubpathPath2D(sub, scale);
204
+ if (strokePath) octx.stroke(strokePath);
205
+ }
206
+ octx.restore();
207
+ }
208
+ }
209
+ }
210
+ return off;
211
+ }
212
+
213
+ function buildFillPath2D(shape, scale) {
214
+ const geometry = new Path2D();
215
+ let any = false;
216
+ for (const sub of shape.subpaths ?? []) {
217
+ if (!sub.closed) continue;
218
+ if (appendSubpath(geometry, sub, scale)) any = true;
219
+ }
220
+ return any ? geometry : null;
221
+ }
222
+
223
+ function buildSubpathPath2D(sub, scale) {
224
+ const geometry = new Path2D();
225
+ if (!appendSubpath(geometry, sub, scale)) return null;
226
+ return geometry;
227
+ }
228
+
229
+ function appendSubpath(geometry, raw, scale) {
230
+ // Read through the same funnel as the bake does. A subpath still stored in
231
+ // the pre-segment form has no `start`, so reading it raw drew nothing at all
232
+ // — art that baked correctly at Pixel vanished at the smooth finishes.
233
+ const sub = normalizeSubpath(raw);
234
+ if (!sub?.start) return false;
235
+ geometry.moveTo(sub.start[0] * scale, sub.start[1] * scale);
236
+ let from = sub.start;
237
+ for (const seg of sub.segs ?? []) {
238
+ if (!seg.c1 && !seg.c2) {
239
+ geometry.lineTo(seg.to[0] * scale, seg.to[1] * scale);
240
+ } else {
241
+ const { c1, c2, to } = absoluteSegment(from, seg);
242
+ geometry.bezierCurveTo(
243
+ c1[0] * scale,
244
+ c1[1] * scale,
245
+ c2[0] * scale,
246
+ c2[1] * scale,
247
+ to[0] * scale,
248
+ to[1] * scale
249
+ );
250
+ }
251
+ from = seg.to;
252
+ }
253
+ if (sub.closed) geometry.closePath();
254
+ return true;
255
+ }
256
+
106
257
  // ---------------------------------------------------------------------------
107
258
  // per-pixel local corner kernel
108
259
  // ---------------------------------------------------------------------------
109
260
 
110
261
  const NO_CUT = undefined;
111
262
 
112
- // Fill pixel (x, y)'s own unit square [x, x+1] x [y, y+1] with `color` (a
113
- // no-op when `color` is null — transparent, nothing to fill), then paint a
114
- // small rounded cut over each of its 4 corners that qualifies (see
115
- // `cornerFill`). Uses ONLY the 3x3 neighborhood of (x, y) — genuinely local,
116
- // unlike the old global loop trace.
117
- function drawPixelBlock(ctx, colorAt, x, y, color, cornerRadius) {
263
+ function drawPixelBlock(ctx, colorAt, x, y, scale, color, cornerRadius) {
264
+ const px = x * scale;
265
+ const py = y * scale;
118
266
  const north = colorAt(x, y - 1);
119
267
  const south = colorAt(x, y + 1);
120
268
  const west = colorAt(x - 1, y);
@@ -129,20 +277,19 @@ function drawPixelBlock(ctx, colorAt, x, y, color, cornerRadius) {
129
277
 
130
278
  if (color) {
131
279
  ctx.fillStyle = rgbaFillStyle(color);
132
- ctx.fillRect(x, y, 1, 1);
280
+ ctx.fillRect(px, py, scale, scale);
133
281
  }
134
282
 
135
283
  if (cornerRadius <= 0) return;
284
+ const r = cornerRadius * scale;
136
285
  for (const [corner, reveal] of cuts) {
137
286
  if (reveal === NO_CUT) continue;
138
287
  ctx.beginPath();
139
- tracePixelWedge(ctx, corner, x, y, cornerRadius);
288
+ tracePixelWedge(ctx, corner, px, py, scale, r);
140
289
  if (reveal === null) {
141
- // Revealing transparency: clip to the wedge and clear it, rather than
142
- // fill it, since there's no color to paint.
143
290
  ctx.save();
144
291
  ctx.clip();
145
- ctx.clearRect(x, y, 1, 1);
292
+ ctx.clearRect(px, py, scale, scale);
146
293
  ctx.restore();
147
294
  } else {
148
295
  ctx.fillStyle = rgbaFillStyle(reveal);
@@ -151,72 +298,76 @@ function drawPixelBlock(ctx, colorAt, x, y, color, cornerRadius) {
151
298
  }
152
299
  }
153
300
 
154
- // Classify one corner of pixel `color`, given its two edge-adjacent
155
- // neighbors (`a`, `b`) and its diagonal neighbor (`g`). Returns the neighbor
156
- // color to reveal at that corner's rounded cut, or `NO_CUT` to leave the
157
- // corner sharp:
158
- // - `a` or `b` matches `color`: either a flat/interior corner, or (when
159
- // they don't BOTH match) a straight edge passing by rather than a real
160
- // corner. Either way, nothing to round from P's side — a matching
161
- // neighbor's OWN corner classification (evaluated independently, when
162
- // IT is P) is what rounds the opposite case; the physical wedge that
163
- // gets cut always lives entirely inside whichever pixel's corner is
164
- // convex, so there's no double-handling.
165
- // - neither `a` nor `b` matches, but `g` DOES: a diagonal touch between
166
- // two same-colored pixels (the "checkerboard" case). Left uncut, so a
167
- // smoothed diagonal stroke doesn't get visually pinched off at every
168
- // step.
169
- // - neither `a`, `b`, nor `g` matches, and `a` and `b` are THE SAME color:
170
- // a genuine, unambiguous convex corner of P's own region touching one
171
- // other region. Cut it, revealing that color.
172
- // - neither `a`, `b`, nor `g` matches, and `a` and `b` DIFFER: three (or
173
- // four, counting `g`) distinct colors meet at this exact point — e.g. a
174
- // "T" where one region's straight edge is crossed by the boundary
175
- // between two others. Left uncut. Rounding here would have to guess
176
- // which of `a`/`b` "wins", and since the pixel on the OTHER side of
177
- // that guess is classifying this same point independently — and would
178
- // guess differently — a pair of pixels each revealing the OTHER's
179
- // color produces a little criss-crossed notch instead of one clean
180
- // curve. Leaving every pixel at a 3+-way point sharp keeps it a single
181
- // consistent (if unrounded) vertex.
182
301
  function cornerFill(color, a, b, g) {
183
302
  if (a === color || b === color) return NO_CUT;
184
303
  if (g === color) return NO_CUT;
185
304
  return a === b ? a : NO_CUT;
186
305
  }
187
306
 
188
- // `color` is always a `"r,g,b,a"` string (0-255 channels, alpha 0-255) —
189
- // see `colorAt` — never the transparent sentinel (only opaque colors ever
190
- // reach this function as `color`, only ever as `reveal`).
191
307
  function rgbaFillStyle(color) {
192
308
  const [r, g, b, a] = color.split(',').map(Number);
193
309
  return `rgba(${r}, ${g}, ${b}, ${a / 255})`;
194
310
  }
195
311
 
196
- // Trace the small corner wedge that gets cut from pixel (x, y)'s unit square
197
- // at `corner` (one of 'TL' | 'TR' | 'BL' | 'BR'): the sliver between the
198
- // exact corner point and the quarter-circle of radius `r` tangent to both
199
- // adjacent edges at distance `r` from the corner — i.e. exactly the piece a
200
- // standard rounded-rect corner removes from a sharp one. `ctx.arcTo`'s
201
- // corner-point-as-control-point form draws that same tangent arc without
202
- // hand-computed sweep angles.
203
- function tracePixelWedge(ctx, corner, x, y, r) {
312
+ /** Display-resolution ghost of native cells using the same corner kernel as placed pixels. */
313
+ export function renderSmoothCellsPreview(
314
+ ctx,
315
+ cells,
316
+ color,
317
+ { scale, cornerRadius, alpha = 1 } = {},
318
+ ) {
319
+ if (!cells?.length || !scale || cornerRadius <= 0) return;
320
+ const clampedRadius = Math.min(MAX_CORNER_RADIUS, Math.max(0, cornerRadius));
321
+ const cellSet = new Set(cells.map((c) => `${c.x},${c.y}`));
322
+ const rgba = cssColorToRgbaString(color, alpha);
323
+ const colorAt = (x, y) => (cellSet.has(`${x},${y}`) ? rgba : null);
324
+
325
+ ctx.save();
326
+ ctx.imageSmoothingEnabled = false;
327
+ ctx.globalAlpha = 1;
328
+ for (const { x, y } of cells) {
329
+ drawPixelBlock(ctx, colorAt, x, y, scale, rgba, clampedRadius);
330
+ }
331
+ ctx.restore();
332
+ }
333
+
334
+ function cssColorToRgbaString(color, alpha = 1) {
335
+ let r = 255;
336
+ let g = 255;
337
+ let b = 255;
338
+ if (typeof color === 'string' && color.startsWith('#')) {
339
+ const h = color.slice(1);
340
+ if (h.length === 3) {
341
+ r = parseInt(h[0] + h[0], 16);
342
+ g = parseInt(h[1] + h[1], 16);
343
+ b = parseInt(h[2] + h[2], 16);
344
+ } else if (h.length >= 6) {
345
+ r = parseInt(h.slice(0, 2), 16);
346
+ g = parseInt(h.slice(2, 4), 16);
347
+ b = parseInt(h.slice(4, 6), 16);
348
+ }
349
+ }
350
+ const a = Math.round(Math.min(1, Math.max(0, alpha)) * 255);
351
+ return `${r},${g},${b},${a}`;
352
+ }
353
+
354
+ function tracePixelWedge(ctx, corner, px, py, cell, r) {
204
355
  if (corner === 'TL') {
205
- ctx.moveTo(x, y);
206
- ctx.lineTo(x + r, y);
207
- ctx.arcTo(x, y, x, y + r, r);
356
+ ctx.moveTo(px, py);
357
+ ctx.lineTo(px + r, py);
358
+ ctx.arcTo(px, py, px, py + r, r);
208
359
  } else if (corner === 'TR') {
209
- ctx.moveTo(x + 1, y);
210
- ctx.lineTo(x + 1, y + r);
211
- ctx.arcTo(x + 1, y, x + 1 - r, y, r);
360
+ ctx.moveTo(px + cell, py);
361
+ ctx.lineTo(px + cell, py + r);
362
+ ctx.arcTo(px + cell, py, px + cell - r, py, r);
212
363
  } else if (corner === 'BL') {
213
- ctx.moveTo(x, y + 1);
214
- ctx.lineTo(x, y + 1 - r);
215
- ctx.arcTo(x, y + 1, x + r, y + 1, r);
364
+ ctx.moveTo(px, py + cell);
365
+ ctx.lineTo(px, py + cell - r);
366
+ ctx.arcTo(px, py + cell, px + r, py + cell, r);
216
367
  } else {
217
- ctx.moveTo(x + 1, y + 1);
218
- ctx.lineTo(x + 1 - r, y + 1);
219
- ctx.arcTo(x + 1, y + 1, x + 1, y + 1 - r, r);
368
+ ctx.moveTo(px + cell, py + cell);
369
+ ctx.lineTo(px + cell - r, py + cell);
370
+ ctx.arcTo(px + cell, py + cell, px + cell, py + cell - r, r);
220
371
  }
221
372
  ctx.closePath();
222
373
  }
@@ -61,6 +61,15 @@ export class SceneRuntime {
61
61
  return system;
62
62
  }
63
63
 
64
+ // Tear this runtime down for good: the player is unmounting, or the editor
65
+ // left play mode. `reset` is for "start this scene over" and runs on every
66
+ // load; this is "nothing of this runtime should still be happening", which is
67
+ // the difference that matters to a system holding something the browser keeps
68
+ // doing on its own -- a playing sound outlives its scene otherwise.
69
+ dispose() {
70
+ for (const system of this.systems) system.dispose?.(this);
71
+ }
72
+
64
73
  load(sceneData) {
65
74
  // Reset registered systems (e.g. physics) so a reload/restart/scene
66
75
  // transition starts from the authored layout -- otherwise a system holding
@@ -611,6 +620,9 @@ function drawDotGrid(ctx, gridSize = 25, viewport = DEFAULT_VIEWPORT, camera = {
611
620
  // represented separately by the DOM SelectionOverlay's solid white box, so a
612
621
  // Sprite-backed actor never shows this box, selected or not.
613
622
  function shouldDrawEditPlaceholder(actor, sprites) {
623
+ // A Video draws its own picture into the box, so the actor is not sprite-less
624
+ // in the sense this box is for.
625
+ if (actor?.components?.Video?.file) return false;
614
626
  const sprite = actor?.components?.Sprite;
615
627
  if (!sprite || !sprite.file) return true;
616
628
  const resolved = sprites?.[sprite.file];
@@ -0,0 +1,112 @@
1
+ // A synthesized note: one oscillator through an attack/release envelope. This
2
+ // is the sound a deck can make with no audio file at all -- a blip on a pickup,
3
+ // a beep on a wrong answer -- and it costs nothing to ship.
4
+ //
5
+ // Deliberately one voice and two envelope stages, matching what the classic
6
+ // Castle editor's "tone" sample offers. Anything richer is an instrument, and an
7
+ // instrument belongs in a music system, not here.
8
+
9
+ import { getAudioContext, resumeAudioContext } from './audioContext';
10
+
11
+ export const WAVEFORMS = ['square', 'sawtooth', 'sine', 'triangle', 'noise'];
12
+
13
+ // Voices currently sounding, so play mode can be silenced on Stop.
14
+ const voices = new Set();
15
+
16
+ // MIDI note number to Hz. 69 is A440, 12 notes to the octave.
17
+ export function noteToFrequency(note) {
18
+ const midi = Number.isFinite(note) ? note : 60;
19
+ return 440 * Math.pow(2, (midi - 69) / 12);
20
+ }
21
+
22
+ // A short burst of white noise, made once and reused. Noise is the one waveform
23
+ // an oscillator can't produce, and it's what a percussive hit wants.
24
+ let noiseBuffer = null;
25
+ function getNoiseBuffer(ctx) {
26
+ if (noiseBuffer && noiseBuffer.sampleRate === ctx.sampleRate) return noiseBuffer;
27
+ const frames = Math.floor(ctx.sampleRate * 2);
28
+ const buffer = ctx.createBuffer(1, frames, ctx.sampleRate);
29
+ const data = buffer.getChannelData(0);
30
+ for (let i = 0; i < frames; i++) data[i] = Math.random() * 2 - 1;
31
+ noiseBuffer = buffer;
32
+ return buffer;
33
+ }
34
+
35
+ function makeSource(ctx, waveform, frequency) {
36
+ if (waveform === 'noise') {
37
+ const source = ctx.createBufferSource();
38
+ source.buffer = getNoiseBuffer(ctx);
39
+ source.loop = true;
40
+ return source;
41
+ }
42
+ const osc = ctx.createOscillator();
43
+ osc.type = WAVEFORMS.includes(waveform) ? waveform : 'square';
44
+ osc.frequency.value = frequency;
45
+ return osc;
46
+ }
47
+
48
+ // Play one note. Returns `{ stop, finished }` -- `finished` resolves when the
49
+ // release has run out, so a caller can wait for it the way a Sound can.
50
+ export function playTone({ note = 60, waveform = 'square', attack = 0, release = 0.4, volume = 1, pan = 0 } = {}) {
51
+ const ctx = getAudioContext();
52
+ if (!ctx) return { stop() {}, finished: Promise.resolve() };
53
+ resumeAudioContext();
54
+
55
+ const level = Number.isFinite(volume) ? Math.min(1, Math.max(0, volume)) : 1;
56
+ const rise = Math.max(0, Number.isFinite(attack) ? attack : 0);
57
+ // A note with no release at all clicks; give it a floor short enough to still
58
+ // read as instant.
59
+ const fall = Math.max(0.01, Number.isFinite(release) ? release : 0.4);
60
+ const start = ctx.currentTime;
61
+ const end = start + rise + fall;
62
+
63
+ const source = makeSource(ctx, waveform, noteToFrequency(note));
64
+ const gain = ctx.createGain();
65
+ gain.gain.setValueAtTime(0, start);
66
+ gain.gain.linearRampToValueAtTime(level, start + rise);
67
+ gain.gain.linearRampToValueAtTime(0, end);
68
+
69
+ let tail = gain;
70
+ if (pan && ctx.createStereoPanner) {
71
+ const panner = ctx.createStereoPanner();
72
+ panner.pan.value = Math.min(1, Math.max(-1, pan));
73
+ gain.connect(panner);
74
+ tail = panner;
75
+ }
76
+ source.connect(gain);
77
+ tail.connect(ctx.destination);
78
+ source.start(start);
79
+ source.stop(end);
80
+
81
+ const voice = { source, gain };
82
+ voices.add(voice);
83
+ const finished = new Promise((resolve) => {
84
+ source.addEventListener('ended', () => {
85
+ voices.delete(voice);
86
+ resolve();
87
+ });
88
+ });
89
+ return {
90
+ stop() {
91
+ voices.delete(voice);
92
+ try {
93
+ source.stop();
94
+ } catch {
95
+ // Already stopped -- nothing to do.
96
+ }
97
+ },
98
+ finished,
99
+ };
100
+ }
101
+
102
+ // Silence every sounding note. Play mode ending must not leave one ringing.
103
+ export function stopAllTones() {
104
+ for (const voice of voices) {
105
+ try {
106
+ voice.source.stop();
107
+ } catch {
108
+ // Already stopped.
109
+ }
110
+ }
111
+ voices.clear();
112
+ }