castle-web-cli 0.4.195 → 0.4.197
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-prompts.js +7 -6
- package/dist/agent.d.ts +17 -0
- package/dist/agent.js +158 -6
- package/dist/shell/assets/{index-D6leLNUq.css → index-MSHtKPXt.css} +1 -1
- package/dist/shell/assets/index-yHGXRGlX.js +449 -0
- package/dist/shell/index.html +2 -2
- package/kits/base/castle.json +1 -1
- package/kits/base/package-lock.json +1 -1
- package/kits/physics-2d/CLAUDE.md +96 -11
- package/kits/physics-2d/behaviors/AnalogStick.jsx +28 -26
- package/kits/physics-2d/behaviors/Draggable.jsx +10 -8
- package/kits/physics-2d/behaviors/Goal.jsx +16 -3
- package/kits/physics-2d/behaviors/Slingshot.jsx +24 -22
- package/kits/physics-2d/behaviors/SoundPlayer.jsx +42 -5
- package/kits/physics-2d/behaviors/Sprite.jsx +21 -16
- package/kits/physics-2d/castle.json +2 -2
- package/kits/physics-2d/editors/SceneEditor.jsx +15 -1
- package/kits/physics-2d/editors/behaviorRegistry.js +14 -3
- package/kits/physics-2d/engine/SceneUI.jsx +11 -11
- package/kits/physics-2d/engine/scene.js +143 -20
- package/kits/physics-2d/engine/tap.js +9 -11
- package/package.json +1 -1
- package/dist/shell/assets/index-kMH1qj6D.js +0 -449
|
@@ -21,10 +21,10 @@ import { paintActorStyle, styleContentBox, styleOpacity } from './Style';
|
|
|
21
21
|
// asset — not a Sprite prop), in which case the cached canvas holds a
|
|
22
22
|
// supersampled render and gets blitted with smoothing on.
|
|
23
23
|
//
|
|
24
|
-
// Animation plays in `update` and is stored on `
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
// frame 0.
|
|
24
|
+
// Animation plays in `update` and is stored on `this.state.anim` (per-actor,
|
|
25
|
+
// seeded from `static initialState`, kept for the cached instance's life), and
|
|
26
|
+
// mirrored to `actor.runtime.spriteAnim`, the older spelling. In edit-mode
|
|
27
|
+
// previews `update` never runs, so the actor holds frame 0.
|
|
28
28
|
const FALLBACK_FILE = 'drawings/cauldron.sprite';
|
|
29
29
|
|
|
30
30
|
export class Sprite {
|
|
@@ -52,6 +52,8 @@ export class Sprite {
|
|
|
52
52
|
tileSize: { min: 1, step: 1 },
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
+
static initialState = { anim: null };
|
|
56
|
+
|
|
55
57
|
constructor(props) {
|
|
56
58
|
this.props = props;
|
|
57
59
|
}
|
|
@@ -65,21 +67,24 @@ export class Sprite {
|
|
|
65
67
|
}
|
|
66
68
|
|
|
67
69
|
update(actor, scene, dt) {
|
|
68
|
-
if (
|
|
70
|
+
if (actor.runtime.collected) return;
|
|
69
71
|
const sprite = this.resolveSprite(scene);
|
|
70
72
|
if (!sprite) return;
|
|
71
73
|
const tag = resolveTag(sprite, this.props.tag);
|
|
72
74
|
const sequence = frameSequence(sprite, tag);
|
|
73
|
-
const
|
|
75
|
+
const anim = ensureAnimState(this.state, sprite, this.props, sequence);
|
|
76
|
+
// Kept as the older spelling -- `actor.runtime.spriteAnim` is where this
|
|
77
|
+
// lived before `this.state`, and a deck may still read the frame from it.
|
|
78
|
+
actor.runtime.spriteAnim = anim;
|
|
74
79
|
if (!this.props.playing) {
|
|
75
|
-
if (
|
|
80
|
+
if (anim.done) resetAnimState(anim);
|
|
76
81
|
return;
|
|
77
82
|
}
|
|
78
|
-
advanceAnim(
|
|
83
|
+
advanceAnim(anim, sprite, sequence, tag, dt);
|
|
79
84
|
}
|
|
80
85
|
|
|
81
86
|
draw(actor, scene, ctx) {
|
|
82
|
-
if (actor.runtime
|
|
87
|
+
if (actor.runtime.collected) return;
|
|
83
88
|
const layout = actor.components.Layout;
|
|
84
89
|
if (!layout) return;
|
|
85
90
|
// Style chrome first so background/border sit behind the blit even when
|
|
@@ -88,7 +93,7 @@ export class Sprite {
|
|
|
88
93
|
const sprite = this.resolveSprite(scene);
|
|
89
94
|
if (!sprite) return;
|
|
90
95
|
const total = artFrameCount(sprite);
|
|
91
|
-
const frameIndex = Math.min(Math.max(
|
|
96
|
+
const frameIndex = Math.min(Math.max(this.state.anim?.frameIndex ?? 0, 0), total - 1);
|
|
92
97
|
const canvas = getSpriteCanvas(sprite, frameIndex, this.props.tint);
|
|
93
98
|
if (!canvas) return;
|
|
94
99
|
// `canvas` is a <canvas> for pixel art and may be the <img> itself for an
|
|
@@ -385,14 +390,14 @@ function animKey(props) {
|
|
|
385
390
|
}
|
|
386
391
|
|
|
387
392
|
// Reset playback state when the sprite identity or active tag changes.
|
|
388
|
-
function ensureAnimState(
|
|
393
|
+
function ensureAnimState(state, sprite, props, sequence) {
|
|
389
394
|
const key = animKey(props);
|
|
390
|
-
let
|
|
391
|
-
if (!
|
|
392
|
-
|
|
393
|
-
|
|
395
|
+
let anim = state.anim;
|
|
396
|
+
if (!anim || anim.sprite !== sprite || anim.key !== key) {
|
|
397
|
+
anim = { sprite, key, pos: 0, elapsedMs: 0, loops: 0, done: false, frameIndex: sequence[0] };
|
|
398
|
+
state.anim = anim;
|
|
394
399
|
}
|
|
395
|
-
return
|
|
400
|
+
return anim;
|
|
396
401
|
}
|
|
397
402
|
|
|
398
403
|
function resetAnimState(state) {
|
|
@@ -67,11 +67,11 @@
|
|
|
67
67
|
"autoUpdateWhenImported": true,
|
|
68
68
|
"onUpdate": "scripts/onUpdate.mjs",
|
|
69
69
|
"title": "physics-2d",
|
|
70
|
-
"publishedVersion": "2026-09-
|
|
70
|
+
"publishedVersion": "2026-09-18T20:10:58.457Z",
|
|
71
71
|
"imports": {
|
|
72
72
|
"castle.base": {
|
|
73
73
|
"deckId": "yRcmH4_aYllE",
|
|
74
|
-
"version": "2026-09-
|
|
74
|
+
"version": "2026-09-18T20:02:20.225Z"
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
}
|
|
@@ -78,7 +78,12 @@ import { PanelSortContext, usePanelSortable } from '../engine/panelSorting';
|
|
|
78
78
|
import { SceneUI } from '../engine/SceneUI';
|
|
79
79
|
import { SelectionOverlay } from './SelectionOverlay';
|
|
80
80
|
import { BlueprintLibrary, BlueprintThumbnail } from './BlueprintLibrary';
|
|
81
|
-
import {
|
|
81
|
+
import {
|
|
82
|
+
behaviorClasses,
|
|
83
|
+
findBehaviorClass,
|
|
84
|
+
subscribeBehaviors,
|
|
85
|
+
useBehaviorReload,
|
|
86
|
+
} from './behaviorRegistry';
|
|
82
87
|
import { useEditHistory, useUndoRedoShortcuts, useHostUndoRedo } from './editorHistory';
|
|
83
88
|
import { useTwoFingerZoomPan } from '../engine/gestureZoomPan';
|
|
84
89
|
// A blueprint file's `actors[0]` has no `id` (it's the template, not an
|
|
@@ -1753,6 +1758,15 @@ function useScenePlayLoop({
|
|
|
1753
1758
|
// the fresh loop back the one that just threw.
|
|
1754
1759
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1755
1760
|
}, [sprites, isPlaying, text, runtimeRef, loopRunId]);
|
|
1761
|
+
// Behavior instances are cached per (actor, behavior) on the runtime, so a
|
|
1762
|
+
// hot-swapped behavior module never reaches a play-mode runtime already
|
|
1763
|
+
// running unless it's told: tell it. Harmless when `runtimeRef.current` is
|
|
1764
|
+
// null (not playing) -- `?.` short-circuits -- so this needs no `isPlaying`
|
|
1765
|
+
// dependency and just needs to be subscribed for the runtime's lifetime.
|
|
1766
|
+
useEffect(
|
|
1767
|
+
() => subscribeBehaviors(() => runtimeRef.current?.setBehaviors(behaviorClasses)),
|
|
1768
|
+
[runtimeRef]
|
|
1769
|
+
);
|
|
1756
1770
|
// Animation loop -- draws edit-mode previews and ticks the play-mode runtime.
|
|
1757
1771
|
useEffect(() => {
|
|
1758
1772
|
if (!sceneData || !canvasRef.current) return undefined;
|
|
@@ -73,15 +73,26 @@ export function subscribeBehaviors(listener) {
|
|
|
73
73
|
|
|
74
74
|
// Only `behaviors/**`: `engine/` and `editors/` are imported statically all over
|
|
75
75
|
// the runtime and still need a reload.
|
|
76
|
+
//
|
|
77
|
+
// Recursive, to match the seed glob above. A stricter test here than the glob is
|
|
78
|
+
// the worst of both: a behavior in a subfolder LOADS (so it is in the list, and
|
|
79
|
+
// its props are in the inspector) but never swaps, so every later edit to it is
|
|
80
|
+
// invisible until the scene is closed and reopened -- which reads as the editor
|
|
81
|
+
// caching behavior data, with no hint that the folder is what decides.
|
|
76
82
|
function isBehaviorModulePath(path) {
|
|
77
|
-
return /(^|\/)behaviors
|
|
83
|
+
return /(^|\/)behaviors\/.+\.jsx$/.test(path) && !path.startsWith('editors/');
|
|
78
84
|
}
|
|
79
85
|
|
|
80
86
|
// The serve runs with `hmr: false`, so a cache-busting query is what gets a
|
|
81
87
|
// fresh module rather than the one already in the browser's map.
|
|
82
88
|
//
|
|
83
|
-
// Behavior
|
|
84
|
-
//
|
|
89
|
+
// Behavior instances are now CACHED per (actor, behavior) on the runtime
|
|
90
|
+
// (`SceneRuntime#instanceFor`), not rebuilt per frame -- so a running play-mode
|
|
91
|
+
// runtime must be told explicitly that the registry changed. `SceneEditor.jsx`
|
|
92
|
+
// subscribes here and calls `runtime.setBehaviors(behaviorClasses)`, which is
|
|
93
|
+
// what makes a hot-swapped class visible to instances already cached; without
|
|
94
|
+
// that call a runtime created before the swap would keep dispatching to the
|
|
95
|
+
// old class forever.
|
|
85
96
|
async function swapBehaviorModule(deckPath) {
|
|
86
97
|
const key = `/${deckPath}`;
|
|
87
98
|
try {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React, { useEffect, useRef, useState } from 'react';
|
|
2
2
|
import styles from './ui.module.css';
|
|
3
3
|
import { useElementSize } from './ui';
|
|
4
|
-
import { cardSize
|
|
4
|
+
import { cardSize } from './scene';
|
|
5
5
|
import { deckFontFamily } from './fonts';
|
|
6
6
|
// Game-time React UI overlay -- deprecated.
|
|
7
7
|
//
|
|
@@ -41,21 +41,21 @@ export function SceneUI({ getRuntime }) {
|
|
|
41
41
|
</div>
|
|
42
42
|
);
|
|
43
43
|
}
|
|
44
|
-
// Build one keyed React node per behavior that defines a `ui` handler.
|
|
44
|
+
// Build one keyed React node per behavior that defines a `ui` handler. Goes
|
|
45
|
+
// through `forEachBehavior` -- the shared cached-instance path -- rather than
|
|
46
|
+
// constructing its own instance, so a `ui()`-implementing behavior sees the
|
|
47
|
+
// SAME instance (and `this.state`) that `update`/`draw` use this frame.
|
|
45
48
|
function collectUiNodes(runtime) {
|
|
46
49
|
if (!runtime) return [];
|
|
47
50
|
const nodes = [];
|
|
48
51
|
for (const actor of runtime.getActors()) {
|
|
49
|
-
|
|
50
|
-
if (!
|
|
51
|
-
const Behavior = runtime.behaviors.get(behaviorName);
|
|
52
|
-
if (!Behavior) continue;
|
|
53
|
-
const instance = new Behavior(props);
|
|
54
|
-
if (!instance.ui) continue;
|
|
52
|
+
runtime.forEachBehavior(actor, (instance) => {
|
|
53
|
+
if (!instance.ui) return;
|
|
55
54
|
const node = instance.ui(actor, runtime);
|
|
56
|
-
if (node == null)
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
if (node == null) return;
|
|
56
|
+
const key = `${actor.id}:${instance.constructor.behaviorName}`;
|
|
57
|
+
nodes.push(<React.Fragment key={key}>{node}</React.Fragment>);
|
|
58
|
+
});
|
|
59
59
|
}
|
|
60
60
|
return nodes;
|
|
61
61
|
}
|
|
@@ -34,15 +34,6 @@ function resolveSceneFileKey(name) {
|
|
|
34
34
|
return candidates.find((candidate) => initialFiles[candidate] !== undefined) ?? key;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
// A behavior instance for one actor. `this.props` is what the constructor
|
|
38
|
-
// assigns; a behavior written without one would read `this.props` as undefined
|
|
39
|
-
// and throw on its first frame, so it is filled in here when missing.
|
|
40
|
-
function makeBehavior(Behavior, props) {
|
|
41
|
-
const instance = new Behavior(props);
|
|
42
|
-
if (instance.props === undefined) instance.props = props;
|
|
43
|
-
return instance;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
37
|
// `$enabled: false` turns a behavior off while keeping its props. Absent means
|
|
47
38
|
// on, so it is only ever stored when false, and every existing scene reads the
|
|
48
39
|
// same. `$` marks a prop the ENGINE owns -- behaviors must not define one.
|
|
@@ -50,6 +41,72 @@ export function isBehaviorEnabled(props) {
|
|
|
50
41
|
return Boolean(props) && props.$enabled !== false;
|
|
51
42
|
}
|
|
52
43
|
|
|
44
|
+
// Bind one behavior instance to the actor/scene/name it was built for. Split
|
|
45
|
+
// out of the class so `#instanceFor` stays small (eslint complexity budget):
|
|
46
|
+
// `this.actor`/`this.scene` are fixed for the instance's life; `this.props` is
|
|
47
|
+
// a LIVE getter onto `actor.components[name]` (the same object the inspector
|
|
48
|
+
// and deck code mutate) rather than a snapshot; `this.state` lazily seeds a
|
|
49
|
+
// per-actor scratch object from `static initialState`, stored under the
|
|
50
|
+
// engine-owned `$state` key so it survives a hot reload of the behavior's own
|
|
51
|
+
// code; `this.enabled` mirrors `$enabled` and never writes `$enabled: true`
|
|
52
|
+
// (absent means on, matching `isBehaviorEnabled`).
|
|
53
|
+
//
|
|
54
|
+
// Ordering matters at the call site: the constructor's own `this.props = props`
|
|
55
|
+
// (or `#instanceFor`'s fallback assignment) must run BEFORE this function,
|
|
56
|
+
// since it replaces `props` with a getter-only accessor -- assigning
|
|
57
|
+
// `inst.props` afterward would throw in strict mode.
|
|
58
|
+
function bindBehaviorInstance(inst, actor, scene, name) {
|
|
59
|
+
const Behavior = inst.constructor;
|
|
60
|
+
Object.defineProperties(inst, {
|
|
61
|
+
actor: { value: actor, configurable: true },
|
|
62
|
+
scene: { value: scene, configurable: true },
|
|
63
|
+
props: { get: () => actor.components[name], enumerable: true, configurable: true },
|
|
64
|
+
state: {
|
|
65
|
+
get() {
|
|
66
|
+
const all = (actor.runtime.$state ??= {});
|
|
67
|
+
return (all[name] ??= structuredClone(Behavior.initialState ?? {}));
|
|
68
|
+
},
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
},
|
|
72
|
+
enabled: {
|
|
73
|
+
get: () => isBehaviorEnabled(actor.components[name]),
|
|
74
|
+
set(on) {
|
|
75
|
+
const p = actor.components[name];
|
|
76
|
+
if (!p) return;
|
|
77
|
+
if (on) delete p.$enabled;
|
|
78
|
+
else p.$enabled = false;
|
|
79
|
+
},
|
|
80
|
+
configurable: true,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// `actor.behaviors.Jump?.onLand()` -- a per-actor Proxy handing back the same
|
|
86
|
+
// cached instance `scene.behavior(actor, name)` would. Built once per actor
|
|
87
|
+
// (see `#initActor`); `scene`/`actor` are stable closures so there's no reason
|
|
88
|
+
// to rebuild it on every read.
|
|
89
|
+
function makeBehaviorsProxy(scene, actor) {
|
|
90
|
+
const names = () => Object.keys(actor.components ?? {}).filter((n) => scene.behaviors.has(n));
|
|
91
|
+
return new Proxy(Object.create(null), {
|
|
92
|
+
get: (_, name) => (typeof name === 'string' ? scene.behavior(actor, name) : undefined),
|
|
93
|
+
has: (_, name) => names().includes(name),
|
|
94
|
+
ownKeys: names,
|
|
95
|
+
getOwnPropertyDescriptor: (_, name) => {
|
|
96
|
+
const value = scene.behavior(actor, name);
|
|
97
|
+
return value ? { value, enumerable: true, configurable: true } : undefined;
|
|
98
|
+
},
|
|
99
|
+
set: () => false, // add a component via actor.components.X = {...} instead
|
|
100
|
+
deleteProperty: () => false,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Shared by the constructor and `setBehaviors` -- both build the same
|
|
105
|
+
// behaviorName -> class Map from a list of classes.
|
|
106
|
+
function behaviorMapFrom(classes) {
|
|
107
|
+
return new Map(classes.map((Behavior) => [Behavior.behaviorName, Behavior]));
|
|
108
|
+
}
|
|
109
|
+
|
|
53
110
|
export class SceneRuntime {
|
|
54
111
|
// `files` is the live deck files map (path -> text), used to resolve every
|
|
55
112
|
// actor's `blueprint` ref against the CURRENT blueprint file content -- not
|
|
@@ -61,7 +118,7 @@ export class SceneRuntime {
|
|
|
61
118
|
// data with no file behind it; refs then resolve against the deck root,
|
|
62
119
|
// which is what they meant before this existed.
|
|
63
120
|
this.scenePath = scenePath;
|
|
64
|
-
this.behaviors =
|
|
121
|
+
this.behaviors = behaviorMapFrom(behaviors);
|
|
65
122
|
this.sprites = sprites ?? {};
|
|
66
123
|
this.files = files ?? {};
|
|
67
124
|
this.time = 0;
|
|
@@ -107,6 +164,75 @@ export class SceneRuntime {
|
|
|
107
164
|
for (const system of this.systems) system.dispose?.(this);
|
|
108
165
|
}
|
|
109
166
|
|
|
167
|
+
// Give an actor its `runtime` scratchpad and `behaviors` accessor. Both are
|
|
168
|
+
// NON-enumerable: `serialize()`/`clone()` are `structuredClone(this.data)`
|
|
169
|
+
// and actors ARE `this.data.actors` entries, so an enumerable `runtime`
|
|
170
|
+
// (holding a Map of live instances, possibly a DOM handle via SoundPlayer)
|
|
171
|
+
// or a Proxy-valued `behaviors` would throw `DataCloneError`. Non-enumerable
|
|
172
|
+
// own properties are simply skipped by structuredClone.
|
|
173
|
+
#initActor(actor) {
|
|
174
|
+
Object.defineProperties(actor, {
|
|
175
|
+
runtime: { value: {}, writable: true, configurable: true },
|
|
176
|
+
behaviors: { value: makeBehaviorsProxy(this, actor), configurable: true },
|
|
177
|
+
});
|
|
178
|
+
return actor;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// The cached instance for `name` on `actor`, or undefined when the actor has
|
|
182
|
+
// no such component or the class is unregistered. Returned whether or not
|
|
183
|
+
// the behavior is `$enabled` -- component present = instance exists;
|
|
184
|
+
// `inst.enabled` is how a caller flips it (mirrors the OG engine's
|
|
185
|
+
// `my.body.enabled`).
|
|
186
|
+
behavior(actorOrId, name) {
|
|
187
|
+
const actor = typeof actorOrId === 'string' ? this.getActor(actorOrId) : actorOrId;
|
|
188
|
+
const props = actor?.components?.[name];
|
|
189
|
+
if (!props) return undefined;
|
|
190
|
+
const Behavior = this.behaviors.get(name);
|
|
191
|
+
if (!Behavior) return undefined;
|
|
192
|
+
return this.#instanceFor(actor, name, Behavior, props);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// One instance per (actor, behavior), cached on `actor.runtime.$behaviors`
|
|
196
|
+
// for the actor's life. Rebuilt when the registry hands back a different
|
|
197
|
+
// class for the same name -- this is the whole hot-swap detection
|
|
198
|
+
// (`setBehaviors` doesn't touch the cache directly; a stale entry is simply
|
|
199
|
+
// caught here on next access).
|
|
200
|
+
#instanceFor(actor, name, Behavior, props) {
|
|
201
|
+
const cache = (actor.runtime.$behaviors ??= new Map());
|
|
202
|
+
const cached = cache.get(name);
|
|
203
|
+
if (cached && cached.constructor === Behavior) return cached;
|
|
204
|
+
const inst = new Behavior(props);
|
|
205
|
+
// The constructor's own `this.props = props` runs above, before binding
|
|
206
|
+
// replaces `props` with a live getter -- a behavior that skipped it (in
|
|
207
|
+
// violation of the documented contract) still gets `.props` filled here,
|
|
208
|
+
// as a plain own-property write, which is legal before binding but would
|
|
209
|
+
// throw against the getter-only accessor afterward.
|
|
210
|
+
if (inst.props === undefined) inst.props = props;
|
|
211
|
+
bindBehaviorInstance(inst, actor, this, name);
|
|
212
|
+
cache.set(name, inst);
|
|
213
|
+
return inst;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Swap in a new set of behavior classes (a hot code reload in the editor).
|
|
217
|
+
// Fills newly-added `defaultProps` keys into existing props objects IN
|
|
218
|
+
// PLACE (props identity is kept, so a live `this.props` getter elsewhere
|
|
219
|
+
// doesn't suddenly point at a stale object) -- same merge style `load` uses.
|
|
220
|
+
// Does not rebuild any actor's instance cache: a stale entry's
|
|
221
|
+
// `.constructor !== Behavior` is caught lazily by `#instanceFor` on next
|
|
222
|
+
// access, so `this.state` survives a hot swap.
|
|
223
|
+
setBehaviors(classes) {
|
|
224
|
+
this.behaviors = behaviorMapFrom(classes);
|
|
225
|
+
for (const actor of this.actors.values()) {
|
|
226
|
+
for (const [name, Behavior] of this.behaviors) {
|
|
227
|
+
const props = actor.components[name];
|
|
228
|
+
if (!props) continue;
|
|
229
|
+
for (const [key, value] of Object.entries(Behavior.defaultProps ?? {})) {
|
|
230
|
+
if (!(key in props)) props[key] = value;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
110
236
|
load(sceneData) {
|
|
111
237
|
// Reset registered systems (e.g. physics) so a reload/restart/scene
|
|
112
238
|
// transition starts from the authored layout -- otherwise a system holding
|
|
@@ -116,7 +242,7 @@ export class SceneRuntime {
|
|
|
116
242
|
this.data = structuredClone(sceneData);
|
|
117
243
|
this.actors = new Map();
|
|
118
244
|
for (const actor of this.data.actors ?? []) {
|
|
119
|
-
actor
|
|
245
|
+
this.#initActor(actor);
|
|
120
246
|
if (actor.blueprint) {
|
|
121
247
|
const template = getBlueprintTemplate(this.files, actor.blueprint, this.scenePath);
|
|
122
248
|
actor.components = mergeComponents(template?.components, actor.components);
|
|
@@ -242,7 +368,8 @@ export class SceneRuntime {
|
|
|
242
368
|
const Behavior = this.behaviors.get(name);
|
|
243
369
|
components[name] = Behavior ? { ...Behavior.defaultProps, ...props } : { ...props };
|
|
244
370
|
}
|
|
245
|
-
const next = { ...actor, id, components
|
|
371
|
+
const next = { ...actor, id, components };
|
|
372
|
+
this.#initActor(next);
|
|
246
373
|
this.data.actors.push(next);
|
|
247
374
|
this.actors.set(id, next);
|
|
248
375
|
return next;
|
|
@@ -386,29 +513,25 @@ export class SceneRuntime {
|
|
|
386
513
|
#drawDisabledSprite(ctx, actor, options) {
|
|
387
514
|
const props = actor.components?.Sprite;
|
|
388
515
|
if (!props || isBehaviorEnabled(props)) return;
|
|
389
|
-
const Behavior = this.behaviors.get('Sprite');
|
|
390
|
-
if (!Behavior) return;
|
|
391
516
|
ctx.save();
|
|
392
517
|
ctx.globalAlpha *= 0.5;
|
|
393
|
-
|
|
518
|
+
this.behavior(actor, 'Sprite')?.draw?.(actor, this, ctx, options);
|
|
394
519
|
ctx.restore();
|
|
395
520
|
drawDisabledSpriteLabel(ctx, actor);
|
|
396
521
|
}
|
|
397
522
|
|
|
398
523
|
#syncEnabled(actor) {
|
|
399
|
-
const was = (actor.runtime
|
|
524
|
+
const was = (actor.runtime.$enabled ??= new Map());
|
|
400
525
|
for (const [behaviorName, props] of Object.entries(actor.components)) {
|
|
401
526
|
const on = isBehaviorEnabled(props);
|
|
402
527
|
if (was.get(behaviorName) === on) continue;
|
|
403
528
|
const first = !was.has(behaviorName);
|
|
404
529
|
was.set(behaviorName, on);
|
|
405
530
|
if (first) continue; // first sight is not a transition
|
|
406
|
-
const Behavior = this.behaviors.get(behaviorName);
|
|
407
|
-
if (!Behavior) continue;
|
|
408
531
|
const hook = on ? 'onEnable' : 'onDisable';
|
|
409
532
|
// Constructed from the props it still has -- the whole point of
|
|
410
533
|
// disabling is that they survive.
|
|
411
|
-
|
|
534
|
+
this.behavior(actor, behaviorName)?.[hook]?.(actor, this);
|
|
412
535
|
}
|
|
413
536
|
}
|
|
414
537
|
|
|
@@ -441,7 +564,7 @@ export class SceneRuntime {
|
|
|
441
564
|
if (!isBehaviorEnabled(props)) continue;
|
|
442
565
|
const Behavior = this.behaviors.get(behaviorName);
|
|
443
566
|
if (!Behavior) continue;
|
|
444
|
-
callback(
|
|
567
|
+
callback(this.#instanceFor(actor, behaviorName, Behavior, props));
|
|
445
568
|
}
|
|
446
569
|
}
|
|
447
570
|
|
|
@@ -12,16 +12,16 @@ import { TAP_CLAIM } from './physics/controls';
|
|
|
12
12
|
|
|
13
13
|
export function updateTaps(scene) {
|
|
14
14
|
for (const actor of scene.actors.values()) {
|
|
15
|
-
const
|
|
16
|
-
if (
|
|
17
|
-
const held = scene.pointers.get(
|
|
15
|
+
const tap = actor.runtime?.$tap;
|
|
16
|
+
if (tap?.pointerId == null) continue;
|
|
17
|
+
const held = scene.pointers.get(tap.pointerId);
|
|
18
18
|
if (held?.down) {
|
|
19
|
-
if (!scene.ownsPointer(
|
|
20
|
-
else actor.runtime
|
|
19
|
+
if (!scene.ownsPointer(tap.pointerId, actor.id)) clearTap(actor);
|
|
20
|
+
else actor.runtime.$tap = { pointerId: tap.pointerId, inside: hits(actor, scene, held) };
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
|
-
const stillOwned = held ? scene.ownsPointer(
|
|
24
|
-
const inside = held ? hits(actor, scene, held) : Boolean(
|
|
23
|
+
const stillOwned = held ? scene.ownsPointer(tap.pointerId, actor.id) : true;
|
|
24
|
+
const inside = held ? hits(actor, scene, held) : Boolean(tap.inside);
|
|
25
25
|
if (stillOwned && inside && hasOnTap(scene, actor)) fireTap(scene, actor);
|
|
26
26
|
clearTap(actor);
|
|
27
27
|
}
|
|
@@ -31,8 +31,7 @@ export function updateTaps(scene) {
|
|
|
31
31
|
const actor = topTapActor(scene, pointer);
|
|
32
32
|
if (!actor) continue;
|
|
33
33
|
if (!scene.claimPointer(pointer.id, actor.id, TAP_CLAIM)) continue;
|
|
34
|
-
actor.runtime
|
|
35
|
-
actor.runtime._tapInside = true;
|
|
34
|
+
actor.runtime.$tap = { pointerId: pointer.id, inside: true };
|
|
36
35
|
// A click that ends before the next frame is justPressed with down:false.
|
|
37
36
|
if (!pointer.down) {
|
|
38
37
|
if (scene.ownsPointer(pointer.id, actor.id)) fireTap(scene, actor);
|
|
@@ -46,8 +45,7 @@ function fireTap(scene, actor) {
|
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
function clearTap(actor) {
|
|
49
|
-
actor.runtime
|
|
50
|
-
actor.runtime._tapInside = false;
|
|
48
|
+
actor.runtime.$tap = null;
|
|
51
49
|
}
|
|
52
50
|
|
|
53
51
|
function hasOnTap(scene, actor) {
|