@n3rdw1z4rd/core 0.20.0 → 0.21.0

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 (44) hide show
  1. package/dist/graph/doc.d.ts +141 -0
  2. package/dist/graph/doc.js +173 -0
  3. package/dist/graph/evaluate.d.ts +114 -0
  4. package/dist/graph/evaluate.js +319 -0
  5. package/dist/graph/index.d.ts +9 -0
  6. package/dist/graph/index.js +9 -0
  7. package/dist/graph/node.d.ts +153 -0
  8. package/dist/graph/node.js +94 -0
  9. package/dist/graph/nodes.d.ts +11 -0
  10. package/dist/graph/nodes.js +399 -0
  11. package/dist/graph/script.d.ts +56 -0
  12. package/dist/graph/script.js +98 -0
  13. package/dist/graph/sources.d.ts +11 -0
  14. package/dist/graph/sources.js +263 -0
  15. package/dist/graph/subgraph.d.ts +36 -0
  16. package/dist/graph/subgraph.js +280 -0
  17. package/dist/graph/types.d.ts +56 -0
  18. package/dist/graph/types.js +67 -0
  19. package/dist/graph.cjs +1709 -0
  20. package/dist/graph.d.cts +568 -0
  21. package/dist/index.cjs +53 -52
  22. package/dist/rng-Dhqq7owV.cjs +642 -0
  23. package/dist/{spatial-partition-3d-Ba-VBveM.cjs → spatial-partition-3d-CmSCL87Z.cjs} +19 -660
  24. package/dist/three/adaptive-grid.d.ts +43 -0
  25. package/dist/three/adaptive-grid.js +127 -0
  26. package/dist/three/ecs.d.ts +5 -3
  27. package/dist/three/ecs.js +5 -3
  28. package/dist/three/editor-camera.d.ts +93 -0
  29. package/dist/three/editor-camera.js +174 -0
  30. package/dist/three/index.d.ts +6 -1
  31. package/dist/three/index.js +6 -1
  32. package/dist/three/picking.d.ts +34 -0
  33. package/dist/three/picking.js +59 -0
  34. package/dist/three/schema.d.ts +139 -0
  35. package/dist/three/schema.js +214 -0
  36. package/dist/three/world.d.ts +192 -0
  37. package/dist/three/world.js +363 -0
  38. package/dist/three.cjs +970 -23
  39. package/dist/three.d.cts +497 -5
  40. package/dist/vite.cjs +40 -0
  41. package/dist/vite.d.cts +35 -0
  42. package/dist/vite.d.ts +35 -0
  43. package/dist/vite.js +39 -0
  44. package/package.json +21 -1
@@ -0,0 +1,363 @@
1
+ import { CreateThreeEntity, IsThreeEntity } from "./ecs.js";
2
+ import { componentToJson, components } from "./schema.js";
3
+ import { MathUtils, Scene } from "three";
4
+ //#region src/three/world.ts
5
+ /**
6
+ * A scene of entities: three.js objects with components (see {@link CreateThreeEntity}),
7
+ * tracked by their `Object3D.uuid`.
8
+ *
9
+ * The world keeps an index of which entities have which components (for {@link World.query}),
10
+ * runs systems, tells listeners what changed (so editor panels stay in sync), and saves and
11
+ * loads the whole scene as plain JSON ({@link SceneDoc}).
12
+ *
13
+ * The three.js scene graph is the hierarchy: parent an entity under another and it moves
14
+ * with it. Transforms live on the objects themselves (`position`, `rotation`, `scale`), so
15
+ * there is no Transform component.
16
+ *
17
+ * ```ts
18
+ * const world = new World();
19
+ * const ball = world.create('ball');
20
+ * world.addComponent(ball, RigidBody, { mass: 2 });
21
+ *
22
+ * world.addSystem({ query: ['RigidBody'], update(entities, dt) { ... } });
23
+ * renderer.setAnimationLoop(() => { world.update(clock.tick()); renderer.render(world.root, camera); });
24
+ *
25
+ * const saved = world.serialize(); // JSON-safe
26
+ * world.load(saved); // same entities, same uuids
27
+ * ```
28
+ *
29
+ * Add and remove entities through the world (not `Object3D.add`/`remove` directly), and call
30
+ * {@link World.changed} after editing a component in place, so the index and listeners stay
31
+ * current.
32
+ *
33
+ * @category ECS
34
+ */
35
+ var World = class {
36
+ root;
37
+ registry;
38
+ entities = /* @__PURE__ */ new Map();
39
+ index = /* @__PURE__ */ new Map();
40
+ systems = [];
41
+ listeners = /* @__PURE__ */ new Map();
42
+ constructor(options = {}) {
43
+ this.root = options.root ?? new Scene();
44
+ this.registry = options.registry ?? components;
45
+ }
46
+ /** number of entities */
47
+ get size() {
48
+ return this.entities.size;
49
+ }
50
+ [Symbol.iterator]() {
51
+ return this.entities.values();
52
+ }
53
+ /** The entity with this uuid. */
54
+ get(uuid) {
55
+ return this.entities.get(uuid);
56
+ }
57
+ has(obj) {
58
+ return this.entities.get(obj.uuid) === obj;
59
+ }
60
+ /** Makes a new entity (an empty `Group`, or `obj`) and adds it under `parent` (default: the root). */
61
+ create(name = "", parent = this.root, obj) {
62
+ const entity = CreateThreeEntity(obj);
63
+ if (name) entity.name = name;
64
+ return this.add(entity, parent);
65
+ }
66
+ /**
67
+ * Puts `obj` under `parent` (default: the root) and starts tracking it — and every entity
68
+ * among its descendants. `obj` is made an entity if it isn't one.
69
+ */
70
+ add(obj, parent = this.root) {
71
+ if (!IsThreeEntity(obj)) CreateThreeEntity(obj);
72
+ parent.add(obj);
73
+ obj.traverse((o) => {
74
+ if (IsThreeEntity(o)) this.track(o);
75
+ });
76
+ return obj;
77
+ }
78
+ /** Takes the entity (and its descendants) out of the scene and stops tracking them. */
79
+ remove(entity) {
80
+ const gone = [];
81
+ entity.traverse((o) => {
82
+ if (this.entities.get(o.uuid) === o) gone.push(o);
83
+ });
84
+ entity.removeFromParent();
85
+ for (const e of gone.reverse()) this.untrack(e);
86
+ }
87
+ /** Removes every entity. */
88
+ clear() {
89
+ for (const child of [...this.root.children]) {
90
+ let hasEntity = false;
91
+ child.traverse((o) => hasEntity ||= IsThreeEntity(o));
92
+ if (hasEntity) this.remove(child);
93
+ }
94
+ }
95
+ /**
96
+ * Moves `entity` under `parent` (default: the root). By default its world transform is
97
+ * kept (it doesn't jump); pass `keepWorldTransform = false` to keep its local one instead.
98
+ */
99
+ reparent(entity, parent = this.root, keepWorldTransform = true) {
100
+ let p = parent;
101
+ while (p) {
102
+ if (p === entity) throw new Error("World.reparent: cannot parent an entity under itself");
103
+ p = p.parent;
104
+ }
105
+ if (keepWorldTransform) parent.attach(entity);
106
+ else parent.add(entity);
107
+ this.emit("reparented", entity, parent);
108
+ }
109
+ /** The closest ancestor that is an entity of this world, or null. */
110
+ parentOf(entity) {
111
+ let p = entity.parent;
112
+ while (p && p !== this.root) {
113
+ if (this.entities.get(p.uuid) === p) return p;
114
+ p = p.parent;
115
+ }
116
+ return null;
117
+ }
118
+ /** The entities directly under `parent` (default: the root), looking through non-entity objects. */
119
+ childrenOf(parent = this.root) {
120
+ const out = [];
121
+ const visit = (o) => {
122
+ for (const c of o.children) if (this.entities.get(c.uuid) === c) out.push(c);
123
+ else visit(c);
124
+ };
125
+ visit(parent);
126
+ return out;
127
+ }
128
+ /** A deep copy of the entity and its descendant entities (new uuids), added next to it. */
129
+ clone(entity, parent = entity.parent ?? this.root) {
130
+ const docs = this.serializeEntities([entity]);
131
+ const [copy] = this.instantiate(docs, {
132
+ parent,
133
+ newIds: true
134
+ });
135
+ return copy;
136
+ }
137
+ track(entity) {
138
+ entity.userData.entityId = entity.uuid;
139
+ const existing = this.entities.get(entity.uuid);
140
+ if (existing === entity) return;
141
+ if (existing) throw new Error(`World: another entity already has uuid ${entity.uuid}`);
142
+ this.entities.set(entity.uuid, entity);
143
+ for (const name of Object.keys(entity.userData.components)) this.indexSet(name).add(entity);
144
+ this.emit("entityAdded", entity);
145
+ }
146
+ untrack(entity) {
147
+ if (!this.entities.delete(entity.uuid)) return;
148
+ for (const name of Object.keys(entity.userData.components)) this.index.get(name)?.delete(entity);
149
+ this.emit("entityRemoved", entity);
150
+ }
151
+ indexSet(name) {
152
+ let set = this.index.get(name);
153
+ if (!set) this.index.set(name, set = /* @__PURE__ */ new Set());
154
+ return set;
155
+ }
156
+ /** The registered type for a name, class or type; throws if unknown. */
157
+ typeOf(ref) {
158
+ const type = this.registry.resolve(ref);
159
+ if (!type) throw new Error(`World: unknown component type ${typeof ref === "string" ? `"${ref}"` : String(ref?.name ?? ref)}`);
160
+ return type;
161
+ }
162
+ /**
163
+ * Adds a component made from its type's defaults plus `data`, replacing one of the same
164
+ * type. Returns the component.
165
+ */
166
+ addComponent(entity, ref, data) {
167
+ const type = this.typeOf(ref);
168
+ const name = type.schema.name;
169
+ const previous = entity.userData.components[name];
170
+ if (previous) this.emit("componentRemoved", entity, name, previous);
171
+ const component = type.create(data);
172
+ entity.userData.components[name] = component;
173
+ if (this.has(entity)) this.indexSet(name).add(entity);
174
+ this.emit("componentAdded", entity, name, component);
175
+ return component;
176
+ }
177
+ getComponent(entity, ref) {
178
+ return entity.userData.components[this.nameOf(ref)];
179
+ }
180
+ hasComponent(entity, ref) {
181
+ return this.nameOf(ref) in entity.userData.components;
182
+ }
183
+ removeComponent(entity, ref) {
184
+ const name = this.nameOf(ref);
185
+ const component = entity.userData.components[name];
186
+ if (component === void 0) return;
187
+ delete entity.userData.components[name];
188
+ this.index.get(name)?.delete(entity);
189
+ this.emit("componentRemoved", entity, name, component);
190
+ }
191
+ /** The names of the entity's components, in the order they were added. */
192
+ componentNames(entity) {
193
+ return Object.keys(entity.userData.components);
194
+ }
195
+ /**
196
+ * Tells listeners a component (or the entity's transform/name, with `component` omitted)
197
+ * was edited in place. `field` narrows it to one field.
198
+ */
199
+ changed(entity, component, field) {
200
+ this.emit("changed", entity, component === void 0 ? void 0 : this.nameOf(component), field);
201
+ }
202
+ /** Entities that have every one of the given components. */
203
+ query(...refs) {
204
+ if (refs.length === 0) return [...this.entities.values()];
205
+ const sets = refs.map((r) => this.nameOf(r)).map((n) => this.index.get(n) ?? /* @__PURE__ */ new Set());
206
+ sets.sort((a, b) => a.size - b.size);
207
+ const [smallest, ...rest] = sets;
208
+ const out = [];
209
+ for (const e of smallest) if (rest.every((s) => s.has(e))) out.push(e);
210
+ return out;
211
+ }
212
+ /** Entities whose name is `name`. */
213
+ findByName(name) {
214
+ return [...this.entities.values()].filter((e) => e.name === name);
215
+ }
216
+ nameOf(ref) {
217
+ if (typeof ref === "string") return ref;
218
+ return this.typeOf(ref).schema.name;
219
+ }
220
+ /**
221
+ * Adds a system, run by {@link World.update} in the order added. With a `query`, it gets
222
+ * the entities that have all of those components; without, every entity.
223
+ */
224
+ addSystem(system, query) {
225
+ const s = typeof system === "function" ? {
226
+ update: system,
227
+ query
228
+ } : system;
229
+ this.systems.push(s);
230
+ return s;
231
+ }
232
+ removeSystem(system) {
233
+ const i = this.systems.indexOf(system);
234
+ if (i >= 0) this.systems.splice(i, 1);
235
+ }
236
+ /** Runs every enabled system once. */
237
+ update(delta) {
238
+ for (const s of [...this.systems]) {
239
+ if (s.enabled === false) continue;
240
+ s.update(this.query(...s.query ?? []), delta, this);
241
+ }
242
+ }
243
+ /** Listens for an event; returns a function that stops listening. */
244
+ on(event, listener) {
245
+ let set = this.listeners.get(event);
246
+ if (!set) this.listeners.set(event, set = /* @__PURE__ */ new Set());
247
+ set.add(listener);
248
+ return () => set.delete(listener);
249
+ }
250
+ emit(event, ...args) {
251
+ const set = this.listeners.get(event);
252
+ if (set) for (const l of [...set]) l(...args);
253
+ }
254
+ /** The whole scene as plain JSON. */
255
+ serialize(meta) {
256
+ return {
257
+ format: SCENE_FORMAT,
258
+ version: 1,
259
+ entities: this.serializeEntities(this.childrenOf()),
260
+ ...meta ? { meta } : {}
261
+ };
262
+ }
263
+ /**
264
+ * Replaces the scene with a saved one (or, with `replace: false`, adds to it). Entities
265
+ * keep their saved uuids.
266
+ */
267
+ load(doc, options = {}) {
268
+ if (doc?.format !== "n3rdw1z4rd.scene") throw new Error("World.load: not a scene document");
269
+ if (doc.version > 1) throw new Error(`World.load: scene version ${doc.version} is newer than supported (1)`);
270
+ if (options.replace !== false) this.clear();
271
+ return this.instantiate(doc.entities);
272
+ }
273
+ /**
274
+ * `entities` and their descendant entities, depth-first (parents before children), as JSON:
275
+ * e.g. a prefab or the clipboard.
276
+ */
277
+ serializeEntities(entities) {
278
+ const out = [];
279
+ const visit = (e, parent) => {
280
+ out.push(this.serializeEntity(e, parent));
281
+ for (const c of this.childrenOf(e)) visit(c, e.uuid);
282
+ };
283
+ for (const e of entities) visit(e, null);
284
+ return out;
285
+ }
286
+ serializeEntity(e, parent) {
287
+ const doc = {
288
+ uuid: e.uuid,
289
+ parent,
290
+ position: e.position.toArray(),
291
+ rotation: [
292
+ e.rotation.x,
293
+ e.rotation.y,
294
+ e.rotation.z
295
+ ],
296
+ scale: e.scale.toArray(),
297
+ components: {}
298
+ };
299
+ if (e.name) doc.name = e.name;
300
+ if (e.rotation.order !== "XYZ") doc.rotationOrder = e.rotation.order;
301
+ if (!e.visible) doc.visible = false;
302
+ for (const [name, component] of Object.entries(e.userData.components)) {
303
+ const type = this.registry.get(name);
304
+ doc.components[name] = type ? componentToJson(type, component) : structuredClone(component);
305
+ }
306
+ return doc;
307
+ }
308
+ /**
309
+ * Makes entities from saved docs. A doc whose `parent` isn't among `docs` goes under
310
+ * `options.parent` (default: the root). With `newIds`, every entity gets a fresh uuid and
311
+ * `entity` fields pointing inside the set are remapped (pasting, prefabs, clone).
312
+ */
313
+ instantiate(docs, options = {}) {
314
+ const parent = options.parent ?? this.root;
315
+ const ids = /* @__PURE__ */ new Map();
316
+ for (const d of docs) ids.set(d.uuid, options.newIds ? MathUtils.generateUUID() : d.uuid);
317
+ const made = /* @__PURE__ */ new Map();
318
+ const roots = [];
319
+ for (const d of docs) {
320
+ const entity = CreateThreeEntity();
321
+ entity.uuid = ids.get(d.uuid);
322
+ entity.name = d.name ?? "";
323
+ entity.position.fromArray(d.position ?? [
324
+ 0,
325
+ 0,
326
+ 0
327
+ ]);
328
+ entity.rotation.set(...d.rotation ?? [
329
+ 0,
330
+ 0,
331
+ 0
332
+ ], d.rotationOrder ?? "XYZ");
333
+ entity.scale.fromArray(d.scale ?? [
334
+ 1,
335
+ 1,
336
+ 1
337
+ ]);
338
+ entity.visible = d.visible !== false;
339
+ for (const [name, data] of Object.entries(d.components ?? {})) {
340
+ const type = this.registry.get(name);
341
+ if (!type) {
342
+ entity.userData.components[name] = structuredClone(data);
343
+ continue;
344
+ }
345
+ const component = type.create(data);
346
+ if (options.newIds) {
347
+ for (const [key, def] of Object.entries(type.schema.fields)) if (def.type === "entity" && ids.has(component[key])) component[key] = ids.get(component[key]);
348
+ }
349
+ entity.userData.components[name] = component;
350
+ }
351
+ made.set(d.uuid, entity);
352
+ const p = d.parent !== null && d.parent !== void 0 ? made.get(d.parent) : void 0;
353
+ if (p) p.add(entity);
354
+ else roots.push(entity);
355
+ }
356
+ for (const r of roots) this.add(r, parent);
357
+ return roots;
358
+ }
359
+ };
360
+ const SCENE_FORMAT = "n3rdw1z4rd.scene";
361
+ const SCENE_VERSION = 1;
362
+ //#endregion
363
+ export { SCENE_FORMAT, SCENE_VERSION, World };