@n3rdw1z4rd/core 0.20.0 → 0.22.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.
- package/dist/graph/doc.d.ts +141 -0
- package/dist/graph/doc.js +173 -0
- package/dist/graph/evaluate.d.ts +114 -0
- package/dist/graph/evaluate.js +319 -0
- package/dist/graph/index.d.ts +9 -0
- package/dist/graph/index.js +9 -0
- package/dist/graph/node.d.ts +153 -0
- package/dist/graph/node.js +94 -0
- package/dist/graph/nodes.d.ts +11 -0
- package/dist/graph/nodes.js +399 -0
- package/dist/graph/script.d.ts +64 -0
- package/dist/graph/script.js +111 -0
- package/dist/graph/sources.d.ts +11 -0
- package/dist/graph/sources.js +263 -0
- package/dist/graph/subgraph.d.ts +36 -0
- package/dist/graph/subgraph.js +280 -0
- package/dist/graph/types.d.ts +56 -0
- package/dist/graph/types.js +67 -0
- package/dist/graph.cjs +1723 -0
- package/dist/graph.d.cts +576 -0
- package/dist/index.cjs +53 -52
- package/dist/rng-Dhqq7owV.cjs +642 -0
- package/dist/{spatial-partition-3d-Ba-VBveM.cjs → spatial-partition-3d-CmSCL87Z.cjs} +19 -660
- package/dist/three/adaptive-grid.d.ts +43 -0
- package/dist/three/adaptive-grid.js +127 -0
- package/dist/three/ecs.d.ts +5 -3
- package/dist/three/ecs.js +5 -3
- package/dist/three/editor-camera.d.ts +93 -0
- package/dist/three/editor-camera.js +174 -0
- package/dist/three/index.d.ts +6 -1
- package/dist/three/index.js +6 -1
- package/dist/three/picking.d.ts +34 -0
- package/dist/three/picking.js +59 -0
- package/dist/three/schema.d.ts +139 -0
- package/dist/three/schema.js +214 -0
- package/dist/three/world.d.ts +192 -0
- package/dist/three/world.js +363 -0
- package/dist/three.cjs +970 -23
- package/dist/three.d.cts +497 -5
- package/dist/vite.cjs +40 -0
- package/dist/vite.d.cts +35 -0
- package/dist/vite.d.ts +35 -0
- package/dist/vite.js +39 -0
- package/package.json +21 -1
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
//#region src/three/schema.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Component schemas: what fields a component type has, their types, defaults and ranges.
|
|
4
|
+
*
|
|
5
|
+
* A schema is what lets an editor work with components it has never seen: the inspector,
|
|
6
|
+
* the scene serializer and (later) graph nodes are all generated from it.
|
|
7
|
+
*
|
|
8
|
+
* Two ways to declare a component type, producing the same {@link ComponentType}:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* // a class with standard (TC39) decorators — needs the `decorators()` Vite plugin
|
|
12
|
+
* // from `@n3rdw1z4rd/core/vite` until browsers run decorators natively
|
|
13
|
+
* @component('RigidBody')
|
|
14
|
+
* class RigidBody {
|
|
15
|
+
* @field({ type: 'enum', options: ['dynamic', 'fixed', 'kinematic'] }) kind = 'dynamic';
|
|
16
|
+
* @field({ min: 0, step: 0.1 }) mass = 1;
|
|
17
|
+
* @field() velocity = new Vector3();
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
* // plain data — no decorators, works anywhere (e.g. in a worker or a user script)
|
|
21
|
+
* const Health = defineComponent('Health', {
|
|
22
|
+
* value: { type: 'number', default: 100, min: 0 },
|
|
23
|
+
* regen: { type: 'boolean', default: false },
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @module
|
|
28
|
+
*/
|
|
29
|
+
/** @category ECS */
|
|
30
|
+
export type FieldType = 'number' | 'boolean' | 'string' | 'color' | 'vec2' | 'vec3' | 'enum' | 'entity' | 'asset';
|
|
31
|
+
/**
|
|
32
|
+
* One field of a component schema.
|
|
33
|
+
*
|
|
34
|
+
* @category ECS
|
|
35
|
+
*/
|
|
36
|
+
export interface FieldDef {
|
|
37
|
+
type: FieldType;
|
|
38
|
+
/** the value a new component starts with */
|
|
39
|
+
default?: unknown;
|
|
40
|
+
/** shown instead of the field name */
|
|
41
|
+
label?: string;
|
|
42
|
+
min?: number;
|
|
43
|
+
max?: number;
|
|
44
|
+
step?: number;
|
|
45
|
+
/** the choices of an `enum` field */
|
|
46
|
+
options?: readonly string[];
|
|
47
|
+
/** kept and saved, but not shown in an inspector */
|
|
48
|
+
hidden?: boolean;
|
|
49
|
+
/** saved, but not editable in an inspector */
|
|
50
|
+
readonly?: boolean;
|
|
51
|
+
/** not saved with the scene (runtime-only state) */
|
|
52
|
+
transient?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Editor hint: show the field only while this returns true for the component, e.g.
|
|
55
|
+
* `showIf: (shape) => shape.kind === 'box'`. Not saved (schemas aren't).
|
|
56
|
+
*/
|
|
57
|
+
showIf?: (component: any) => boolean;
|
|
58
|
+
}
|
|
59
|
+
/** @category ECS */
|
|
60
|
+
export interface ComponentSchema {
|
|
61
|
+
/** unique among registered types; the key the component is stored and saved under */
|
|
62
|
+
name: string;
|
|
63
|
+
fields: Record<string, FieldDef>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A component type: its schema, and how to make an instance.
|
|
67
|
+
*
|
|
68
|
+
* @category ECS
|
|
69
|
+
*/
|
|
70
|
+
export interface ComponentType<T extends object = any> {
|
|
71
|
+
readonly schema: ComponentSchema;
|
|
72
|
+
/** the class, for class-based types */
|
|
73
|
+
readonly ctor?: new () => T;
|
|
74
|
+
/** a new instance with the defaults, then `data` (raw JSON or live values) applied */
|
|
75
|
+
create(data?: Partial<Record<keyof T, unknown>> | Record<string, unknown>): T;
|
|
76
|
+
}
|
|
77
|
+
/** Works out a field's type from a value, e.g. a class field's initial value. */
|
|
78
|
+
export declare function inferFieldType(value: unknown): FieldType;
|
|
79
|
+
/** A field value as plain JSON: vectors become arrays, colors `#rrggbb` strings. */
|
|
80
|
+
export declare function fieldToJson(def: FieldDef, value: unknown): unknown;
|
|
81
|
+
/**
|
|
82
|
+
* Applies a JSON (or live) value to a field. When the field currently holds a three.js
|
|
83
|
+
* object (a `Vector3`, a `Color`…) it is updated in place, so references to it stay valid.
|
|
84
|
+
*/
|
|
85
|
+
export declare function applyFieldValue(target: Record<string, any>, key: string, def: FieldDef, json: unknown): void;
|
|
86
|
+
/** The saved form of a component: every non-transient field, as JSON. */
|
|
87
|
+
export declare function componentToJson(type: ComponentType, component: Record<string, any>): Record<string, unknown>;
|
|
88
|
+
/**
|
|
89
|
+
* Declares a component type from plain field definitions; instances are plain objects.
|
|
90
|
+
*
|
|
91
|
+
* @category ECS
|
|
92
|
+
*/
|
|
93
|
+
export declare function defineComponent<F extends Record<string, FieldDef>>(name: string, fields: F, registry?: ComponentRegistry | null): ComponentType<{ [K in keyof F]: any; }>;
|
|
94
|
+
type FieldOptions = Partial<FieldDef>;
|
|
95
|
+
/**
|
|
96
|
+
* Field decorator: marks a class field as part of the component's schema. The type is
|
|
97
|
+
* inferred from the initial value when not given.
|
|
98
|
+
*
|
|
99
|
+
* @category ECS
|
|
100
|
+
*/
|
|
101
|
+
export declare function field(options?: FieldOptions): (_value: undefined, context: ClassFieldDecoratorContext) => void;
|
|
102
|
+
/**
|
|
103
|
+
* Class decorator: registers the class as a component type (in the default registry, or
|
|
104
|
+
* the one given). Give it a name: class names don't survive minification.
|
|
105
|
+
*
|
|
106
|
+
* @category ECS
|
|
107
|
+
*/
|
|
108
|
+
export declare function component(name?: string, registry?: ComponentRegistry): <C extends new () => object>(ctor: C, context: ClassDecoratorContext<C>) => void;
|
|
109
|
+
/**
|
|
110
|
+
* The {@link ComponentType} for a class with `@field`s (made once, then cached). Field types
|
|
111
|
+
* and defaults come from a fresh instance.
|
|
112
|
+
*
|
|
113
|
+
* @category ECS
|
|
114
|
+
*/
|
|
115
|
+
export declare function componentType<T extends object>(ctor: new () => T, name?: string): ComponentType<T>;
|
|
116
|
+
/**
|
|
117
|
+
* Component types by name (and by class). Scenes are saved with type names and loaded
|
|
118
|
+
* through a registry.
|
|
119
|
+
*
|
|
120
|
+
* @category ECS
|
|
121
|
+
*/
|
|
122
|
+
export declare class ComponentRegistry {
|
|
123
|
+
private readonly byName;
|
|
124
|
+
register(type: ComponentType): ComponentType;
|
|
125
|
+
get(name: string): ComponentType | undefined;
|
|
126
|
+
has(name: string): boolean;
|
|
127
|
+
/** The type of a name, class, type or instance (via its constructor); undefined if unknown. */
|
|
128
|
+
resolve(ref: string | ComponentType | (new () => object) | object): ComponentType | undefined;
|
|
129
|
+
/** every registered type, e.g. for an "add component" menu */
|
|
130
|
+
all(): ComponentType[];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The default registry, used by `@component` and {@link defineComponent} unless given another.
|
|
134
|
+
*
|
|
135
|
+
* @category ECS
|
|
136
|
+
*/
|
|
137
|
+
export declare const components: ComponentRegistry;
|
|
138
|
+
//#endregion
|
|
139
|
+
export {};
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
//#region src/three/schema.ts
|
|
2
|
+
const isObj = (v) => typeof v === "object" && v !== null;
|
|
3
|
+
/** Works out a field's type from a value, e.g. a class field's initial value. */
|
|
4
|
+
function inferFieldType(value) {
|
|
5
|
+
if (typeof value === "number") return "number";
|
|
6
|
+
if (typeof value === "boolean") return "boolean";
|
|
7
|
+
if (isObj(value)) {
|
|
8
|
+
if (value.isColor) return "color";
|
|
9
|
+
if (value.isVector3 || "z" in value && "x" in value && "y" in value) return "vec3";
|
|
10
|
+
if (value.isVector2 || "x" in value && "y" in value) return "vec2";
|
|
11
|
+
}
|
|
12
|
+
return "string";
|
|
13
|
+
}
|
|
14
|
+
/** A field value as plain JSON: vectors become arrays, colors `#rrggbb` strings. */
|
|
15
|
+
function fieldToJson(def, value) {
|
|
16
|
+
switch (def.type) {
|
|
17
|
+
case "vec2": return isObj(value) ? [value.x ?? 0, value.y ?? 0] : value;
|
|
18
|
+
case "vec3": return isObj(value) ? [
|
|
19
|
+
value.x ?? 0,
|
|
20
|
+
value.y ?? 0,
|
|
21
|
+
value.z ?? 0
|
|
22
|
+
] : value;
|
|
23
|
+
case "color": return isObj(value) && value.isColor ? `#${value.getHexString()}` : value;
|
|
24
|
+
default: return isObj(value) ? structuredClone(value) : value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Applies a JSON (or live) value to a field. When the field currently holds a three.js
|
|
29
|
+
* object (a `Vector3`, a `Color`…) it is updated in place, so references to it stay valid.
|
|
30
|
+
*/
|
|
31
|
+
function applyFieldValue(target, key, def, json) {
|
|
32
|
+
const current = target[key];
|
|
33
|
+
if (def.type === "vec2" || def.type === "vec3") {
|
|
34
|
+
const [x, y, z] = Array.isArray(json) ? json : isObj(json) ? [
|
|
35
|
+
json.x,
|
|
36
|
+
json.y,
|
|
37
|
+
json.z
|
|
38
|
+
] : [];
|
|
39
|
+
if (isObj(current) && typeof current.set === "function") def.type === "vec3" ? current.set(x ?? 0, y ?? 0, z ?? 0) : current.set(x ?? 0, y ?? 0);
|
|
40
|
+
else target[key] = def.type === "vec3" ? {
|
|
41
|
+
x: x ?? 0,
|
|
42
|
+
y: y ?? 0,
|
|
43
|
+
z: z ?? 0
|
|
44
|
+
} : {
|
|
45
|
+
x: x ?? 0,
|
|
46
|
+
y: y ?? 0
|
|
47
|
+
};
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (def.type === "color" && isObj(current) && current.isColor) {
|
|
51
|
+
if (isObj(json) && json.isColor) current.copy(json);
|
|
52
|
+
else current.set(json);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
target[key] = isObj(json) ? structuredClone(json) : json;
|
|
56
|
+
}
|
|
57
|
+
/** The saved form of a component: every non-transient field, as JSON. */
|
|
58
|
+
function componentToJson(type, component) {
|
|
59
|
+
const out = {};
|
|
60
|
+
for (const [key, def] of Object.entries(type.schema.fields)) if (!def.transient) out[key] = fieldToJson(def, component[key]);
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
function applyData(type, instance, data) {
|
|
64
|
+
if (!data) return;
|
|
65
|
+
for (const [key, def] of Object.entries(type.schema.fields)) if (key in data) applyFieldValue(instance, key, def, data[key]);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Declares a component type from plain field definitions; instances are plain objects.
|
|
69
|
+
*
|
|
70
|
+
* @category ECS
|
|
71
|
+
*/
|
|
72
|
+
function defineComponent(name, fields, registry = components) {
|
|
73
|
+
const type = {
|
|
74
|
+
schema: {
|
|
75
|
+
name,
|
|
76
|
+
fields: { ...fields }
|
|
77
|
+
},
|
|
78
|
+
create(data) {
|
|
79
|
+
const instance = {};
|
|
80
|
+
for (const [key, def] of Object.entries(fields)) instance[key] = isObj(def.default) ? structuredClone(def.default) : def.default ?? defaultFor(def);
|
|
81
|
+
applyData(type, instance, data);
|
|
82
|
+
return instance;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
registry?.register(type);
|
|
86
|
+
return type;
|
|
87
|
+
}
|
|
88
|
+
function defaultFor(def) {
|
|
89
|
+
switch (def.type) {
|
|
90
|
+
case "number": return def.min !== void 0 && def.min > 0 ? def.min : 0;
|
|
91
|
+
case "boolean": return false;
|
|
92
|
+
case "color": return "#ffffff";
|
|
93
|
+
case "vec2": return {
|
|
94
|
+
x: 0,
|
|
95
|
+
y: 0
|
|
96
|
+
};
|
|
97
|
+
case "vec3": return {
|
|
98
|
+
x: 0,
|
|
99
|
+
y: 0,
|
|
100
|
+
z: 0
|
|
101
|
+
};
|
|
102
|
+
case "enum": return def.options?.[0] ?? "";
|
|
103
|
+
case "entity": return null;
|
|
104
|
+
default: return "";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const METADATA = Symbol.metadata ?? Symbol.for("Symbol.metadata");
|
|
108
|
+
const FIELDS = Symbol("n3rdw1z4rd.fields");
|
|
109
|
+
/**
|
|
110
|
+
* Field decorator: marks a class field as part of the component's schema. The type is
|
|
111
|
+
* inferred from the initial value when not given.
|
|
112
|
+
*
|
|
113
|
+
* @category ECS
|
|
114
|
+
*/
|
|
115
|
+
function field(options = {}) {
|
|
116
|
+
return function(_value, context) {
|
|
117
|
+
if (context.static || context.private) throw new Error("@field: only public instance fields can be fields");
|
|
118
|
+
const meta = context.metadata;
|
|
119
|
+
if (!Object.hasOwn(meta, FIELDS)) meta[FIELDS] = { ...meta[FIELDS] ?? {} };
|
|
120
|
+
meta[FIELDS][String(context.name)] = options;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Class decorator: registers the class as a component type (in the default registry, or
|
|
125
|
+
* the one given). Give it a name: class names don't survive minification.
|
|
126
|
+
*
|
|
127
|
+
* @category ECS
|
|
128
|
+
*/
|
|
129
|
+
function component(name, registry = components) {
|
|
130
|
+
return function(ctor, context) {
|
|
131
|
+
context.addInitializer(() => {
|
|
132
|
+
registry.register(componentType(ctor, name ?? String(context.name)));
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const classTypes = /* @__PURE__ */ new WeakMap();
|
|
137
|
+
/**
|
|
138
|
+
* The {@link ComponentType} for a class with `@field`s (made once, then cached). Field types
|
|
139
|
+
* and defaults come from a fresh instance.
|
|
140
|
+
*
|
|
141
|
+
* @category ECS
|
|
142
|
+
*/
|
|
143
|
+
function componentType(ctor, name = ctor.name) {
|
|
144
|
+
const cached = classTypes.get(ctor);
|
|
145
|
+
if (cached) return cached;
|
|
146
|
+
const declared = ctor[METADATA]?.[FIELDS] ?? {};
|
|
147
|
+
const sample = new ctor();
|
|
148
|
+
const fields = {};
|
|
149
|
+
for (const [key, opts] of Object.entries(declared)) {
|
|
150
|
+
const type = opts.type ?? (opts.options ? "enum" : inferFieldType(sample[key]));
|
|
151
|
+
fields[key] = {
|
|
152
|
+
...opts,
|
|
153
|
+
type,
|
|
154
|
+
default: fieldToJson({ type }, sample[key])
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const type = {
|
|
158
|
+
schema: {
|
|
159
|
+
name,
|
|
160
|
+
fields
|
|
161
|
+
},
|
|
162
|
+
ctor,
|
|
163
|
+
create(data) {
|
|
164
|
+
const instance = new ctor();
|
|
165
|
+
applyData(type, instance, data);
|
|
166
|
+
return instance;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
classTypes.set(ctor, type);
|
|
170
|
+
return type;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Component types by name (and by class). Scenes are saved with type names and loaded
|
|
174
|
+
* through a registry.
|
|
175
|
+
*
|
|
176
|
+
* @category ECS
|
|
177
|
+
*/
|
|
178
|
+
var ComponentRegistry = class {
|
|
179
|
+
byName = /* @__PURE__ */ new Map();
|
|
180
|
+
register(type) {
|
|
181
|
+
const existing = this.byName.get(type.schema.name);
|
|
182
|
+
if (existing && existing !== type) console.warn(`ComponentRegistry: replacing component type "${type.schema.name}"`);
|
|
183
|
+
this.byName.set(type.schema.name, type);
|
|
184
|
+
return type;
|
|
185
|
+
}
|
|
186
|
+
get(name) {
|
|
187
|
+
return this.byName.get(name);
|
|
188
|
+
}
|
|
189
|
+
has(name) {
|
|
190
|
+
return this.byName.has(name);
|
|
191
|
+
}
|
|
192
|
+
/** The type of a name, class, type or instance (via its constructor); undefined if unknown. */
|
|
193
|
+
resolve(ref) {
|
|
194
|
+
if (typeof ref === "string") return this.get(ref);
|
|
195
|
+
if (typeof ref === "function") {
|
|
196
|
+
const t = classTypes.get(ref);
|
|
197
|
+
return t && this.get(t.schema.name) === t ? t : void 0;
|
|
198
|
+
}
|
|
199
|
+
if ("schema" in ref && "create" in ref) return ref;
|
|
200
|
+
return this.resolve(ref.constructor);
|
|
201
|
+
}
|
|
202
|
+
/** every registered type, e.g. for an "add component" menu */
|
|
203
|
+
all() {
|
|
204
|
+
return [...this.byName.values()];
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* The default registry, used by `@component` and {@link defineComponent} unless given another.
|
|
209
|
+
*
|
|
210
|
+
* @category ECS
|
|
211
|
+
*/
|
|
212
|
+
const components = new ComponentRegistry();
|
|
213
|
+
//#endregion
|
|
214
|
+
export { ComponentRegistry, applyFieldValue, component, componentToJson, componentType, components, defineComponent, field, fieldToJson, inferFieldType };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { ThreeEntity } from "./ecs.js";
|
|
2
|
+
import { ComponentRegistry, ComponentType } from "./schema.js";
|
|
3
|
+
import { Object3D } from "three";
|
|
4
|
+
//#region src/three/world.d.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
|
+
export declare class World {
|
|
36
|
+
readonly root: Object3D;
|
|
37
|
+
readonly registry: ComponentRegistry;
|
|
38
|
+
private readonly entities;
|
|
39
|
+
private readonly index;
|
|
40
|
+
private readonly systems;
|
|
41
|
+
private readonly listeners;
|
|
42
|
+
constructor(options?: {
|
|
43
|
+
root?: Object3D;
|
|
44
|
+
registry?: ComponentRegistry;
|
|
45
|
+
});
|
|
46
|
+
/** number of entities */
|
|
47
|
+
get size(): number;
|
|
48
|
+
[Symbol.iterator](): IterableIterator<ThreeEntity>;
|
|
49
|
+
/** The entity with this uuid. */
|
|
50
|
+
get(uuid: string): ThreeEntity | undefined;
|
|
51
|
+
has(obj: Object3D): boolean;
|
|
52
|
+
/** Makes a new entity (an empty `Group`, or `obj`) and adds it under `parent` (default: the root). */
|
|
53
|
+
create(name?: string, parent?: Object3D, obj?: Object3D): ThreeEntity;
|
|
54
|
+
/**
|
|
55
|
+
* Puts `obj` under `parent` (default: the root) and starts tracking it — and every entity
|
|
56
|
+
* among its descendants. `obj` is made an entity if it isn't one.
|
|
57
|
+
*/
|
|
58
|
+
add(obj: Object3D, parent?: Object3D): ThreeEntity;
|
|
59
|
+
/** Takes the entity (and its descendants) out of the scene and stops tracking them. */
|
|
60
|
+
remove(entity: Object3D): void;
|
|
61
|
+
/** Removes every entity. */
|
|
62
|
+
clear(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Moves `entity` under `parent` (default: the root). By default its world transform is
|
|
65
|
+
* kept (it doesn't jump); pass `keepWorldTransform = false` to keep its local one instead.
|
|
66
|
+
*/
|
|
67
|
+
reparent(entity: ThreeEntity, parent?: Object3D, keepWorldTransform?: boolean): void;
|
|
68
|
+
/** The closest ancestor that is an entity of this world, or null. */
|
|
69
|
+
parentOf(entity: Object3D): ThreeEntity | null;
|
|
70
|
+
/** The entities directly under `parent` (default: the root), looking through non-entity objects. */
|
|
71
|
+
childrenOf(parent?: Object3D): ThreeEntity[];
|
|
72
|
+
/** A deep copy of the entity and its descendant entities (new uuids), added next to it. */
|
|
73
|
+
clone(entity: ThreeEntity, parent?: Object3D): ThreeEntity;
|
|
74
|
+
private track;
|
|
75
|
+
private untrack;
|
|
76
|
+
private indexSet;
|
|
77
|
+
/** The registered type for a name, class or type; throws if unknown. */
|
|
78
|
+
typeOf(ref: ComponentRef): ComponentType;
|
|
79
|
+
/**
|
|
80
|
+
* Adds a component made from its type's defaults plus `data`, replacing one of the same
|
|
81
|
+
* type. Returns the component.
|
|
82
|
+
*/
|
|
83
|
+
addComponent<T extends object>(entity: ThreeEntity, ref: ComponentRef<T>, data?: Record<string, unknown>): T;
|
|
84
|
+
getComponent<T extends object>(entity: ThreeEntity, ref: ComponentRef<T>): T | undefined;
|
|
85
|
+
hasComponent(entity: ThreeEntity, ref: ComponentRef): boolean;
|
|
86
|
+
removeComponent(entity: ThreeEntity, ref: ComponentRef): void;
|
|
87
|
+
/** The names of the entity's components, in the order they were added. */
|
|
88
|
+
componentNames(entity: ThreeEntity): string[];
|
|
89
|
+
/**
|
|
90
|
+
* Tells listeners a component (or the entity's transform/name, with `component` omitted)
|
|
91
|
+
* was edited in place. `field` narrows it to one field.
|
|
92
|
+
*/
|
|
93
|
+
changed(entity: ThreeEntity, component?: ComponentRef, field?: string): void;
|
|
94
|
+
/** Entities that have every one of the given components. */
|
|
95
|
+
query(...refs: ComponentRef[]): ThreeEntity[];
|
|
96
|
+
/** Entities whose name is `name`. */
|
|
97
|
+
findByName(name: string): ThreeEntity[];
|
|
98
|
+
private nameOf;
|
|
99
|
+
/**
|
|
100
|
+
* Adds a system, run by {@link World.update} in the order added. With a `query`, it gets
|
|
101
|
+
* the entities that have all of those components; without, every entity.
|
|
102
|
+
*/
|
|
103
|
+
addSystem(system: WorldSystem | WorldSystem['update'], query?: ComponentRef[]): WorldSystem;
|
|
104
|
+
removeSystem(system: WorldSystem): void;
|
|
105
|
+
/** Runs every enabled system once. */
|
|
106
|
+
update(delta: number): void;
|
|
107
|
+
/** Listens for an event; returns a function that stops listening. */
|
|
108
|
+
on<K extends keyof WorldEvents>(event: K, listener: (...args: WorldEvents[K]) => void): () => void;
|
|
109
|
+
private emit;
|
|
110
|
+
/** The whole scene as plain JSON. */
|
|
111
|
+
serialize(meta?: Record<string, unknown>): SceneDoc;
|
|
112
|
+
/**
|
|
113
|
+
* Replaces the scene with a saved one (or, with `replace: false`, adds to it). Entities
|
|
114
|
+
* keep their saved uuids.
|
|
115
|
+
*/
|
|
116
|
+
load(doc: SceneDoc, options?: {
|
|
117
|
+
replace?: boolean;
|
|
118
|
+
}): ThreeEntity[];
|
|
119
|
+
/**
|
|
120
|
+
* `entities` and their descendant entities, depth-first (parents before children), as JSON:
|
|
121
|
+
* e.g. a prefab or the clipboard.
|
|
122
|
+
*/
|
|
123
|
+
serializeEntities(entities: ThreeEntity[]): EntityDoc[];
|
|
124
|
+
private serializeEntity;
|
|
125
|
+
/**
|
|
126
|
+
* Makes entities from saved docs. A doc whose `parent` isn't among `docs` goes under
|
|
127
|
+
* `options.parent` (default: the root). With `newIds`, every entity gets a fresh uuid and
|
|
128
|
+
* `entity` fields pointing inside the set are remapped (pasting, prefabs, clone).
|
|
129
|
+
*/
|
|
130
|
+
instantiate(docs: EntityDoc[], options?: {
|
|
131
|
+
parent?: Object3D;
|
|
132
|
+
newIds?: boolean;
|
|
133
|
+
}): ThreeEntity[];
|
|
134
|
+
}
|
|
135
|
+
/** A component reference: its name, type, or class. */
|
|
136
|
+
export type ComponentRef<T extends object = any> = string | ComponentType<T> | (new () => T);
|
|
137
|
+
/** @category ECS */
|
|
138
|
+
export interface WorldSystem {
|
|
139
|
+
/** components an entity needs to be passed to `update` */
|
|
140
|
+
query?: ComponentRef[];
|
|
141
|
+
/** false skips it in {@link World.update} */
|
|
142
|
+
enabled?: boolean;
|
|
143
|
+
update(entities: ThreeEntity[], delta: number, world: World): void;
|
|
144
|
+
}
|
|
145
|
+
/** @category ECS */
|
|
146
|
+
export interface WorldEvents {
|
|
147
|
+
entityAdded: [entity: ThreeEntity];
|
|
148
|
+
entityRemoved: [entity: ThreeEntity];
|
|
149
|
+
componentAdded: [entity: ThreeEntity, name: string, component: object];
|
|
150
|
+
componentRemoved: [entity: ThreeEntity, name: string, component: object];
|
|
151
|
+
/** `component` undefined: the entity itself (transform, name, visibility) */
|
|
152
|
+
changed: [entity: ThreeEntity, component: string | undefined, field: string | undefined];
|
|
153
|
+
reparented: [entity: ThreeEntity, parent: Object3D];
|
|
154
|
+
}
|
|
155
|
+
type Vec3 = [number, number, number];
|
|
156
|
+
export declare const SCENE_FORMAT = "n3rdw1z4rd.scene";
|
|
157
|
+
export declare const SCENE_VERSION = 1;
|
|
158
|
+
/**
|
|
159
|
+
* A saved scene: plain JSON.
|
|
160
|
+
*
|
|
161
|
+
* @category ECS
|
|
162
|
+
*/
|
|
163
|
+
export interface SceneDoc {
|
|
164
|
+
format: typeof SCENE_FORMAT;
|
|
165
|
+
version: number;
|
|
166
|
+
/** depth-first: parents before their children */
|
|
167
|
+
entities: EntityDoc[];
|
|
168
|
+
/** anything else the app wants saved with the scene (camera, settings, scripts…) */
|
|
169
|
+
meta?: Record<string, unknown>;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* One saved entity. Only entities are saved: non-entity objects (e.g. meshes a render
|
|
173
|
+
* system made) are rebuilt from components.
|
|
174
|
+
*
|
|
175
|
+
* @category ECS
|
|
176
|
+
*/
|
|
177
|
+
export interface EntityDoc {
|
|
178
|
+
uuid: string;
|
|
179
|
+
name?: string;
|
|
180
|
+
/** the parent entity's uuid; null for a top-level entity */
|
|
181
|
+
parent: string | null;
|
|
182
|
+
position: Vec3;
|
|
183
|
+
/** Euler angles, radians */
|
|
184
|
+
rotation: Vec3;
|
|
185
|
+
rotationOrder?: string;
|
|
186
|
+
scale: Vec3;
|
|
187
|
+
visible?: false;
|
|
188
|
+
/** component JSON by type name */
|
|
189
|
+
components: Record<string, Record<string, unknown>>;
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
export {};
|