@margoengine/ecs 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +486 -2
- package/dist/changes.d.ts +39 -0
- package/dist/changes.js +63 -0
- package/dist/coroutines.d.ts +82 -0
- package/dist/coroutines.js +97 -0
- package/dist/engine.d.ts +80 -0
- package/dist/engine.js +164 -0
- package/dist/growable.d.ts +23 -0
- package/dist/growable.js +28 -0
- package/dist/index.d.ts +145 -0
- package/dist/index.js +166 -0
- package/dist/objects.d.ts +43 -0
- package/dist/objects.js +78 -0
- package/dist/parallel-runtime.d.ts +43 -0
- package/dist/parallel.d.ts +102 -0
- package/dist/parallel.js +280 -0
- package/dist/parallel.worker.d.ts +1 -0
- package/dist/query.d.ts +42 -0
- package/dist/query.js +61 -0
- package/dist/reactive.d.ts +59 -0
- package/dist/reactive.js +106 -0
- package/dist/relation-query.d.ts +77 -0
- package/dist/relation-query.js +123 -0
- package/dist/relations.d.ts +72 -0
- package/dist/relations.js +99 -0
- package/dist/schedule.d.ts +82 -0
- package/dist/schedule.js +109 -0
- package/dist/scripts.d.ts +182 -0
- package/dist/scripts.js +278 -0
- package/dist/serialize-codec.d.ts +34 -0
- package/dist/serialize.d.ts +34 -0
- package/dist/serialize.js +355 -0
- package/dist/workers/parallel.js +1 -0
- package/package.json +105 -4
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Schedule } from './schedule.js';
|
|
3
|
+
/**
|
|
4
|
+
* What a coroutine yields to wait: nothing (the next update), a number of seconds, a promise (its rejection is thrown
|
|
5
|
+
* into the generator at the `yield`), or another {@link Coroutine} (until it ends).
|
|
6
|
+
*/
|
|
7
|
+
export type CoroutineWait = undefined | number | PromiseLike<unknown> | Coroutine;
|
|
8
|
+
/** A generator that spans frames: `function* () { yield; yield 0.5; yield somePromise; }`. */
|
|
9
|
+
export type CoroutineGenerator = Generator<CoroutineWait, void, unknown>;
|
|
10
|
+
/** Options of {@link Coroutines.start}. */
|
|
11
|
+
export interface CoroutineOptions {
|
|
12
|
+
/** Cancels the coroutine when it aborts; an already aborted signal cancels it before it starts. */
|
|
13
|
+
signal?: AbortSignal;
|
|
14
|
+
}
|
|
15
|
+
/** Settings of {@link Coroutines}. */
|
|
16
|
+
export interface CoroutinesOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Called for an error a coroutine throws, so one failing coroutine never stops the others. Default: errors are
|
|
19
|
+
* collected and rethrown after the update as one MargoError `'failed'` whose `cause` is the first.
|
|
20
|
+
*/
|
|
21
|
+
onError?: (error: unknown, coroutine: Coroutine) => void;
|
|
22
|
+
}
|
|
23
|
+
/** A running coroutine: `done` settles when it ends, `cancel()` stops it. */
|
|
24
|
+
export declare class Coroutine {
|
|
25
|
+
readonly generator: CoroutineGenerator;
|
|
26
|
+
private readonly owner;
|
|
27
|
+
/** Resolves when the generator returns; rejects with an AbortError on cancel, or with the error it threw. */
|
|
28
|
+
readonly done: Promise<void>;
|
|
29
|
+
/** @internal */ resolve: () => void;
|
|
30
|
+
/** @internal */ reject: (error: unknown) => void;
|
|
31
|
+
/** @internal */ waitSeconds: number;
|
|
32
|
+
/** @internal */ waitFor: Coroutine | null;
|
|
33
|
+
/** @internal */ thrown: {
|
|
34
|
+
error: unknown;
|
|
35
|
+
} | null;
|
|
36
|
+
/** @internal */ finished: boolean;
|
|
37
|
+
/** @internal */
|
|
38
|
+
constructor(generator: CoroutineGenerator, owner: Coroutines);
|
|
39
|
+
/** True until it returns, throws or is cancelled. */
|
|
40
|
+
get active(): boolean;
|
|
41
|
+
/** Stops it at the current `yield` (its `finally` blocks run) and rejects `done` with an AbortError. Does nothing once ended. */
|
|
42
|
+
cancel(): void;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Runs multi-frame logic written as generators, stepped once per `update(dt)` (or by a {@link Schedule} after
|
|
46
|
+
* `install`). A bare `yield` waits one update, `yield seconds` waits that long, `yield promise` waits for a promise and
|
|
47
|
+
* `yield coroutine` waits for another coroutine, so a sequence reads top to bottom instead of as a state machine.
|
|
48
|
+
*/
|
|
49
|
+
export declare class Coroutines {
|
|
50
|
+
private readonly options;
|
|
51
|
+
private readonly running;
|
|
52
|
+
private disposed;
|
|
53
|
+
constructor(options?: CoroutinesOptions);
|
|
54
|
+
/** Number of coroutines still running. */
|
|
55
|
+
get size(): number;
|
|
56
|
+
/**
|
|
57
|
+
* Starts a coroutine; its first step runs on the next `update`.
|
|
58
|
+
* @throws MargoError `'disposed'` after `dispose`.
|
|
59
|
+
*/
|
|
60
|
+
start(generator: CoroutineGenerator, options?: CoroutineOptions): Coroutine;
|
|
61
|
+
/** Starts a coroutine and returns its `done` promise: the async form, `await coroutines.run(gen)`. */
|
|
62
|
+
run(generator: CoroutineGenerator, options?: CoroutineOptions): Promise<void>;
|
|
63
|
+
/** @internal Ends `co`: no `error` resolves it, anything else rejects it. */
|
|
64
|
+
stop(co: Coroutine, error?: unknown): void;
|
|
65
|
+
/**
|
|
66
|
+
* Steps every running coroutine by `dt` seconds. Returns how many are still running.
|
|
67
|
+
* @throws RangeError when `dt` is negative or not finite.
|
|
68
|
+
* @throws MargoError `'failed'` when a coroutine threw and no `onError` was given.
|
|
69
|
+
*/
|
|
70
|
+
update(dt: number): number;
|
|
71
|
+
private step;
|
|
72
|
+
/** Adds a frame system `coroutines` that calls `update(delta)` to a {@link Schedule}. */
|
|
73
|
+
install(schedule: Schedule, options?: {
|
|
74
|
+
before?: readonly string[];
|
|
75
|
+
after?: readonly string[];
|
|
76
|
+
}): this;
|
|
77
|
+
/** Cancels every running coroutine; each `done` rejects with an AbortError. */
|
|
78
|
+
cancelAll(): void;
|
|
79
|
+
/** Cancels everything and refuses new coroutines. */
|
|
80
|
+
dispose(): void;
|
|
81
|
+
[Symbol.dispose]: () => void;
|
|
82
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/coroutines.ts
|
|
3
|
+
var t = class {
|
|
4
|
+
constructor(e, t) {
|
|
5
|
+
this.generator = e, this.owner = t, this.waitSeconds = 0, this.waitFor = null, this.thrown = null, this.finished = !1, this.done = new Promise((e, t) => {
|
|
6
|
+
this.resolve = e, this.reject = t;
|
|
7
|
+
}), this.done.catch(() => {});
|
|
8
|
+
}
|
|
9
|
+
get active() {
|
|
10
|
+
return !this.finished;
|
|
11
|
+
}
|
|
12
|
+
cancel() {
|
|
13
|
+
this.owner.stop(this, new DOMException("Coroutine cancelled; catch the AbortError from done to handle it", "AbortError"));
|
|
14
|
+
}
|
|
15
|
+
}, n = class {
|
|
16
|
+
constructor(e = {}) {
|
|
17
|
+
this.options = e, this.running = /* @__PURE__ */ new Set(), this.disposed = !1;
|
|
18
|
+
}
|
|
19
|
+
get size() {
|
|
20
|
+
return this.running.size;
|
|
21
|
+
}
|
|
22
|
+
start(n, r = {}) {
|
|
23
|
+
if (this.disposed) throw new e("disposed", "Coroutines is disposed; create a new Coroutines");
|
|
24
|
+
let i = new t(n, this);
|
|
25
|
+
this.running.add(i);
|
|
26
|
+
let { signal: a } = r;
|
|
27
|
+
if (a) {
|
|
28
|
+
let e = () => i.cancel();
|
|
29
|
+
if (a.aborted) e();
|
|
30
|
+
else {
|
|
31
|
+
a.addEventListener("abort", e, { once: !0 });
|
|
32
|
+
let t = () => a.removeEventListener("abort", e);
|
|
33
|
+
i.done.then(t, t);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return i;
|
|
37
|
+
}
|
|
38
|
+
run(e, t = {}) {
|
|
39
|
+
return this.start(e, t).done;
|
|
40
|
+
}
|
|
41
|
+
stop(e, t) {
|
|
42
|
+
if (!e.finished) {
|
|
43
|
+
if (e.finished = !0, this.running.delete(e), t === void 0) return e.resolve();
|
|
44
|
+
try {
|
|
45
|
+
e.generator.return();
|
|
46
|
+
} catch (e) {
|
|
47
|
+
t = e;
|
|
48
|
+
}
|
|
49
|
+
e.reject(t);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
update(t) {
|
|
53
|
+
if (!(t >= 0) || !Number.isFinite(t)) throw RangeError("Coroutines.update needs a finite dt of 0 or more; pass seconds");
|
|
54
|
+
let n = [];
|
|
55
|
+
for (let e of [...this.running]) if (!e.finished) {
|
|
56
|
+
if (e.waitFor) {
|
|
57
|
+
if (e.waitFor.active) continue;
|
|
58
|
+
e.waitFor = null;
|
|
59
|
+
}
|
|
60
|
+
if (e.waitSeconds > 0) {
|
|
61
|
+
if (e.waitSeconds -= t, e.waitSeconds > 0) continue;
|
|
62
|
+
e.waitSeconds = 0;
|
|
63
|
+
}
|
|
64
|
+
this.step(e, n);
|
|
65
|
+
}
|
|
66
|
+
if (n.length) {
|
|
67
|
+
let t = new e("failed", `${n.length} coroutine${n.length > 1 ? "s" : ""} threw; the first error is in cause, or pass onError to handle each`);
|
|
68
|
+
throw Object.defineProperty(t, "cause", { value: n[0] }), t;
|
|
69
|
+
}
|
|
70
|
+
return this.running.size;
|
|
71
|
+
}
|
|
72
|
+
step(e, n) {
|
|
73
|
+
let r;
|
|
74
|
+
try {
|
|
75
|
+
let t = e.thrown;
|
|
76
|
+
e.thrown = null, r = t ? e.generator.throw(t.error) : e.generator.next();
|
|
77
|
+
} catch (t) {
|
|
78
|
+
e.finished = !0, this.running.delete(e), e.reject(t), this.options.onError ? this.options.onError(t, e) : n.push(t);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (r.done) return this.stop(e);
|
|
82
|
+
let i = r.value;
|
|
83
|
+
typeof i == "number" ? i >= 0 && Number.isFinite(i) ? e.waitSeconds = i : e.thrown = { error: /* @__PURE__ */ RangeError("A coroutine can yield seconds of 0 or more; yield a finite number") } : i instanceof t ? e.waitFor = i : i && typeof i.then == "function" && (e.waitSeconds = Infinity, i.then(() => e.waitSeconds = 0, (t) => (e.thrown = { error: t }, e.waitSeconds = 0)));
|
|
84
|
+
}
|
|
85
|
+
install(e, t = {}) {
|
|
86
|
+
return e.add("coroutines", (e) => void this.update(e.delta), t), this;
|
|
87
|
+
}
|
|
88
|
+
cancelAll() {
|
|
89
|
+
for (let e of [...this.running]) e.cancel();
|
|
90
|
+
}
|
|
91
|
+
dispose() {
|
|
92
|
+
this.disposed ||= (this.cancelAll(), !0);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
n.prototype[Symbol.dispose] = n.prototype.dispose;
|
|
96
|
+
//#endregion
|
|
97
|
+
export { t as Coroutine, n as Coroutines };
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Mesh } from '@margoengine/core';
|
|
2
|
+
import type { Component, Entity } from './index.js';
|
|
3
|
+
/** Schema of a full transform component: position `x y z`, unit quaternion `qx qy qz qw`, scale `sx sy sz`. */
|
|
4
|
+
export declare const transformSchema: {
|
|
5
|
+
x: Float32ArrayConstructor;
|
|
6
|
+
y: Float32ArrayConstructor;
|
|
7
|
+
z: Float32ArrayConstructor;
|
|
8
|
+
qx: Float32ArrayConstructor;
|
|
9
|
+
qy: Float32ArrayConstructor;
|
|
10
|
+
qz: Float32ArrayConstructor;
|
|
11
|
+
qw: Float32ArrayConstructor;
|
|
12
|
+
sx: Float32ArrayConstructor;
|
|
13
|
+
sy: Float32ArrayConstructor;
|
|
14
|
+
sz: Float32ArrayConstructor;
|
|
15
|
+
};
|
|
16
|
+
/** Settings of a {@link MeshBridge}. */
|
|
17
|
+
export interface MeshBridgeOptions {
|
|
18
|
+
/** Dispose a linked mesh when its entity loses the component or is destroyed. Default false: the link just ends. */
|
|
19
|
+
disposeMeshes?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Copies a transform component to core meshes in one bulk step a frame, through `TransformWriter` from
|
|
23
|
+
* `@margoengine/scene/bulk`. The component may hold any of the three groups of {@link transformSchema}: position,
|
|
24
|
+
* quaternion, scale; a mesh keeps its own values for a group the component lacks.
|
|
25
|
+
* A mesh's transform is relative to its parent, so hierarchies stay on the meshes (`mesh.setParent`).
|
|
26
|
+
*/
|
|
27
|
+
export declare class MeshBridge {
|
|
28
|
+
readonly component: Component;
|
|
29
|
+
private readonly entities;
|
|
30
|
+
private readonly meshes;
|
|
31
|
+
private readonly places;
|
|
32
|
+
private readonly linked;
|
|
33
|
+
private readonly observer;
|
|
34
|
+
private readonly position;
|
|
35
|
+
private readonly rotation;
|
|
36
|
+
private readonly scale;
|
|
37
|
+
/** Undefined when the links changed; `sync` builds it again. */
|
|
38
|
+
private batches;
|
|
39
|
+
/** Rows gathered for one write; float64, so a value reaches the writer's checks exactly as the component holds it. */
|
|
40
|
+
private scratch;
|
|
41
|
+
/** @throws TypeError when the component has none of the three groups, or only part of one. */
|
|
42
|
+
constructor(component: Component, { disposeMeshes }?: MeshBridgeOptions);
|
|
43
|
+
/**
|
|
44
|
+
* Tie `entity` to `mesh`. An entity without the component gets it, filled from the mesh's current transform, so the
|
|
45
|
+
* mesh does not jump; one that has it keeps its values and the mesh takes them at the next `sync`. Linking an entity
|
|
46
|
+
* again replaces its mesh.
|
|
47
|
+
* @throws MargoError `'state'` when the entity is stale, or `'disposed'` when the mesh is disposed.
|
|
48
|
+
* @throws TypeError when the mesh is linked to another entity.
|
|
49
|
+
*/
|
|
50
|
+
link(entity: Entity, mesh: Mesh): this;
|
|
51
|
+
/** End a link and leave the mesh as it is; false when `entity` has none. */
|
|
52
|
+
unlink(entity: Entity): boolean;
|
|
53
|
+
/** The mesh linked to `entity`, if any. */
|
|
54
|
+
meshOf(entity: Entity): Mesh | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Copy every linked entity's transform to its mesh; returns how many links were copied. Call it once a frame, after
|
|
57
|
+
* the systems that move things. One pass over the links finds which groups changed, so a world with nothing moving
|
|
58
|
+
* costs that pass and no write. A changed group goes to `TransformWriter` in one write per run of rows that changed,
|
|
59
|
+
* so a world that moves everything costs one write a group. The result is what the mesh's setters give,
|
|
60
|
+
* `mesh.rotation` included, and a group equal to what the mesh already holds is left alone. The first sync after a
|
|
61
|
+
* link changes builds the writer again, about one pass over the links.
|
|
62
|
+
* @throws MargoError `'disposed'` when a linked mesh was disposed, before anything is written; `unlink` it when you dispose it yourself.
|
|
63
|
+
* @throws RangeError for a value the mesh refuses: not finite, a zero scale or a zero quaternion. Writes before it stay copied.
|
|
64
|
+
*/
|
|
65
|
+
sync(): number;
|
|
66
|
+
/** End every link and stop watching the component. Safe to call twice. */
|
|
67
|
+
stop(): void;
|
|
68
|
+
[Symbol.dispose]: () => void;
|
|
69
|
+
/** The links, one batch per engine: an app may draw one world on two canvases. */
|
|
70
|
+
private group;
|
|
71
|
+
/**
|
|
72
|
+
* The one pass of a sync: checks each mesh, reads its slot and component row, and returns a mask of the groups where
|
|
73
|
+
* some row differs from what the mesh holds. A group already known to differ is not compared again, so a world that
|
|
74
|
+
* moves everything pays the compare for its first row only.
|
|
75
|
+
*/
|
|
76
|
+
private scan;
|
|
77
|
+
/** Writes one group of `batch`: one `TransformWriter` call per run of rows that differ from what the meshes hold. */
|
|
78
|
+
private write;
|
|
79
|
+
private fill;
|
|
80
|
+
}
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { TransformWriter as e } from "@margoengine/scene/bulk";
|
|
2
|
+
//#region src/engine.ts
|
|
3
|
+
var t = {
|
|
4
|
+
x: Float32Array,
|
|
5
|
+
y: Float32Array,
|
|
6
|
+
z: Float32Array,
|
|
7
|
+
qx: Float32Array,
|
|
8
|
+
qy: Float32Array,
|
|
9
|
+
qz: Float32Array,
|
|
10
|
+
qw: Float32Array,
|
|
11
|
+
sx: Float32Array,
|
|
12
|
+
sy: Float32Array,
|
|
13
|
+
sz: Float32Array
|
|
14
|
+
}, n = (e) => e, r = 1, i = 2, a = 4, o = class {
|
|
15
|
+
constructor(e, { disposeMeshes: t = !1 } = {}) {
|
|
16
|
+
this.component = e, this.entities = [], this.meshes = [], this.places = /* @__PURE__ */ new Map(), this.linked = /* @__PURE__ */ new Set(), this.scratch = /* @__PURE__ */ new Float64Array();
|
|
17
|
+
let n = e.fields, r = (e) => {
|
|
18
|
+
let t = e.filter((e) => Object.hasOwn(n, e));
|
|
19
|
+
if (t.length && t.length < e.length) throw TypeError(`Partial transform component; give all of ${e.join(", ")} or none of them`);
|
|
20
|
+
return t.length ? e.map((e) => n[e]) : void 0;
|
|
21
|
+
};
|
|
22
|
+
if (this.position = r([
|
|
23
|
+
"x",
|
|
24
|
+
"y",
|
|
25
|
+
"z"
|
|
26
|
+
]), this.rotation = r([
|
|
27
|
+
"qx",
|
|
28
|
+
"qy",
|
|
29
|
+
"qz",
|
|
30
|
+
"qw"
|
|
31
|
+
]), this.scale = r([
|
|
32
|
+
"sx",
|
|
33
|
+
"sy",
|
|
34
|
+
"sz"
|
|
35
|
+
]), !this.position && !this.rotation && !this.scale) throw TypeError("Component has no transform fields; use transformSchema or its x, y, z fields");
|
|
36
|
+
this.observer = {
|
|
37
|
+
onAdd() {},
|
|
38
|
+
onChange() {},
|
|
39
|
+
onRemove: (e) => {
|
|
40
|
+
let n = this.meshOf(e);
|
|
41
|
+
n && (this.unlink(e), t && n.dispose());
|
|
42
|
+
}
|
|
43
|
+
}, e.observers.push(this.observer);
|
|
44
|
+
}
|
|
45
|
+
link(e, t) {
|
|
46
|
+
if (this.component.world.check(e), t.check(), this.linked.has(t) && this.meshOf(e) !== t) throw TypeError("Mesh is already linked; unlink it first, or give each entity its own mesh");
|
|
47
|
+
return this.unlink(e), this.component.index(e) < 0 && this.fill(e, t), this.places.set(e, this.entities.length), this.entities.push(e), this.meshes.push(t), this.linked.add(t), this.batches = void 0, this;
|
|
48
|
+
}
|
|
49
|
+
unlink(e) {
|
|
50
|
+
let t = this.places.get(e);
|
|
51
|
+
if (t === void 0) return !1;
|
|
52
|
+
let { entities: n, meshes: r } = this, i = n.pop(), a = r.pop();
|
|
53
|
+
return this.linked.delete(t === n.length ? a : r[t]), this.places.delete(e), this.batches = void 0, t < n.length && (n[t] = i, r[t] = a, this.places.set(i, t)), !0;
|
|
54
|
+
}
|
|
55
|
+
meshOf(e) {
|
|
56
|
+
let t = this.places.get(e);
|
|
57
|
+
return t === void 0 ? void 0 : this.meshes[t];
|
|
58
|
+
}
|
|
59
|
+
sync() {
|
|
60
|
+
let { entities: e, meshes: t, position: o, rotation: s, scale: c } = this, l = this.batches ??= this.group(), u = 0, d = l.map((e) => {
|
|
61
|
+
let t = this.scan(e);
|
|
62
|
+
return u |= t, t;
|
|
63
|
+
});
|
|
64
|
+
return u && l.forEach((e, l) => {
|
|
65
|
+
let u = n(t[e.links[0]]).engine.store.transforms, f = d[l];
|
|
66
|
+
f & r && this.write(e, "positions", o, [
|
|
67
|
+
u.x,
|
|
68
|
+
u.y,
|
|
69
|
+
u.z
|
|
70
|
+
]), f & i && this.write(e, "quaternions", s, [
|
|
71
|
+
u.qx,
|
|
72
|
+
u.qy,
|
|
73
|
+
u.qz,
|
|
74
|
+
u.qw
|
|
75
|
+
]), f & a && this.write(e, "scales", c, [
|
|
76
|
+
u.sx,
|
|
77
|
+
u.sy,
|
|
78
|
+
u.sz
|
|
79
|
+
]);
|
|
80
|
+
}), e.length;
|
|
81
|
+
}
|
|
82
|
+
stop() {
|
|
83
|
+
let { observers: e } = this.component, t = e.indexOf(this.observer);
|
|
84
|
+
t >= 0 && e.splice(t, 1), this.entities.length = this.meshes.length = 0, this.places.clear(), this.linked.clear(), this.batches = void 0;
|
|
85
|
+
}
|
|
86
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
87
|
+
this.stop();
|
|
88
|
+
}
|
|
89
|
+
group() {
|
|
90
|
+
let { meshes: t } = this, r = /* @__PURE__ */ new Map();
|
|
91
|
+
for (let e = 0; e < t.length; e++) {
|
|
92
|
+
let { engine: i } = n(t[e]), a = r.get(i);
|
|
93
|
+
a ? a.push(e) : r.set(i, [e]);
|
|
94
|
+
}
|
|
95
|
+
return [...r.values()].map((n) => ({
|
|
96
|
+
writer: new e(n.map((e) => t[e])),
|
|
97
|
+
links: Int32Array.from(n),
|
|
98
|
+
ids: new Int32Array(n.length),
|
|
99
|
+
rows: new Int32Array(n.length)
|
|
100
|
+
}));
|
|
101
|
+
}
|
|
102
|
+
scan({ links: e, ids: t, rows: o }) {
|
|
103
|
+
let { entities: s, meshes: c, component: l, position: u, rotation: d, scale: f } = this, { x: p, y: m, z: h, qx: g, qy: _, qz: v, qw: y, sx: b, sy: x, sz: S } = n(c[e[0]]).engine.store.transforms, [C, w, T] = u ?? [
|
|
104
|
+
p,
|
|
105
|
+
m,
|
|
106
|
+
h
|
|
107
|
+
], [E, D, O, k] = d ?? [
|
|
108
|
+
g,
|
|
109
|
+
_,
|
|
110
|
+
v,
|
|
111
|
+
y
|
|
112
|
+
], [A, j, M] = f ?? [
|
|
113
|
+
b,
|
|
114
|
+
x,
|
|
115
|
+
S
|
|
116
|
+
], N = (u ? r : 0) | (d ? i : 0) | (f ? a : 0), P = 0;
|
|
117
|
+
for (let u = 0, d = e.length; u < d; u++) {
|
|
118
|
+
let d = e[u], f = c[d];
|
|
119
|
+
f.check();
|
|
120
|
+
let F = n(f).id, I = l.index(s[d]);
|
|
121
|
+
t[u] = F, o[u] = I, N && (N & r && (p[F] !== C[I] || m[F] !== w[I] || h[F] !== T[I]) && (N &= -2, P |= r), N & i && (g[F] !== E[I] || _[F] !== D[I] || v[F] !== O[I] || y[F] !== k[I]) && (N &= -3, P |= i), N & a && (b[F] !== A[I] || x[F] !== j[I] || S[F] !== M[I]) && (N &= -5, P |= a));
|
|
122
|
+
}
|
|
123
|
+
return P;
|
|
124
|
+
}
|
|
125
|
+
write({ writer: e, ids: t, rows: n }, r, i, a) {
|
|
126
|
+
let o = i.length, s = t.length, [c, l, u, d = u] = i, [f, p, m, h = m] = a;
|
|
127
|
+
this.scratch.length < s * 4 && (this.scratch = new Float64Array(s * 4));
|
|
128
|
+
let g = this.scratch, _ = -1;
|
|
129
|
+
for (let i = 0; i < s; i++) {
|
|
130
|
+
let a = n[i], s = t[i];
|
|
131
|
+
if (f[s] !== c[a] || p[s] !== l[a] || m[s] !== u[a] || h[s] !== d[a]) {
|
|
132
|
+
_ < 0 && (_ = i);
|
|
133
|
+
let e = (i - _) * o;
|
|
134
|
+
g[e] = c[a], g[e + 1] = l[a], g[e + 2] = u[a], o === 4 && (g[e + 3] = d[a]);
|
|
135
|
+
} else _ >= 0 && (e.write({ [r]: g }, {
|
|
136
|
+
from: _,
|
|
137
|
+
count: i - _
|
|
138
|
+
}), _ = -1);
|
|
139
|
+
}
|
|
140
|
+
_ >= 0 && e.write({ [r]: g }, {
|
|
141
|
+
from: _,
|
|
142
|
+
count: s - _
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
fill(e, t) {
|
|
146
|
+
let { id: r, engine: i } = n(t), a = i.store.transforms, o = {};
|
|
147
|
+
this.position && Object.assign(o, {
|
|
148
|
+
x: a.x[r],
|
|
149
|
+
y: a.y[r],
|
|
150
|
+
z: a.z[r]
|
|
151
|
+
}), this.rotation && Object.assign(o, {
|
|
152
|
+
qx: a.qx[r],
|
|
153
|
+
qy: a.qy[r],
|
|
154
|
+
qz: a.qz[r],
|
|
155
|
+
qw: a.qw[r]
|
|
156
|
+
}), this.scale && Object.assign(o, {
|
|
157
|
+
sx: a.sx[r],
|
|
158
|
+
sy: a.sy[r],
|
|
159
|
+
sz: a.sz[r]
|
|
160
|
+
}), this.component.add(e, o);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
//#endregion
|
|
164
|
+
export { o as MeshBridge, t as transformSchema };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { World } from './index.js';
|
|
2
|
+
import type { NumericArray } from './index.js';
|
|
3
|
+
/** Settings of a {@link GrowableWorld}. */
|
|
4
|
+
export interface GrowableWorldOptions {
|
|
5
|
+
/** Slots at the start, an integer in [1, `limit`]. Default 1024. */
|
|
6
|
+
capacity?: number;
|
|
7
|
+
/** Most slots it may grow to, an integer up to 1,000,000. Default 1,000,000. Entity ids are spaced by it. */
|
|
8
|
+
limit?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A {@link World} that doubles its slots when `create` runs out, up to `limit`. Every array sized by slot sits on a
|
|
12
|
+
* resizable buffer, so growing moves nothing: `component.fields`, arrays taken from them, and arrays from
|
|
13
|
+
* {@link World.allocate} stay valid and only get longer. Ids do not change either.
|
|
14
|
+
*/
|
|
15
|
+
export declare class GrowableWorld extends World {
|
|
16
|
+
private slotted;
|
|
17
|
+
/** @throws RangeError for a capacity or limit that is not an integer, or a capacity above the limit. */
|
|
18
|
+
constructor({ capacity, limit }?: GrowableWorldOptions);
|
|
19
|
+
/** Most slots this world may grow to. */
|
|
20
|
+
get limit(): number;
|
|
21
|
+
allocate<T extends NumericArray>(Type: new (length: number) => T, fill?: number): T;
|
|
22
|
+
protected grow(): void;
|
|
23
|
+
}
|
package/dist/growable.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { World as e } from "./index.js";
|
|
2
|
+
//#region src/growable.ts
|
|
3
|
+
var t = class extends e {
|
|
4
|
+
constructor({ capacity: e = 1024, limit: t = 1e6 } = {}) {
|
|
5
|
+
super(e, t);
|
|
6
|
+
}
|
|
7
|
+
get limit() {
|
|
8
|
+
return this.stride;
|
|
9
|
+
}
|
|
10
|
+
allocate(e, t = 0) {
|
|
11
|
+
let { BYTES_PER_ELEMENT: n } = e, r = new e(new ArrayBuffer(this.capacity * n, { maxByteLength: this.stride * n }));
|
|
12
|
+
return t && r.fill(t), (this.slotted ??= []).push({
|
|
13
|
+
array: new WeakRef(r),
|
|
14
|
+
fill: t
|
|
15
|
+
}), r;
|
|
16
|
+
}
|
|
17
|
+
grow() {
|
|
18
|
+
let e = this.capacity;
|
|
19
|
+
if (e >= this.stride) throw RangeError("Entity limit reached; destroy entities or create the GrowableWorld with a higher limit");
|
|
20
|
+
let t = Math.min(e * 2, this.stride);
|
|
21
|
+
this.slotted = this.slotted.filter(({ array: n, fill: r }) => {
|
|
22
|
+
let i = n.deref();
|
|
23
|
+
return i ? (i.buffer.resize(t * i.BYTES_PER_ELEMENT), r && i.fill(r, e), !0) : !1;
|
|
24
|
+
}), this.capacity = t, this.release(e, t);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
export { t as GrowableWorld };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/** Typed arrays allowed as component field storage. */
|
|
2
|
+
export type NumericArray = Float32Array | Float64Array | Int32Array | Uint32Array | Uint8Array;
|
|
3
|
+
/** Constructor of a {@link NumericArray}, e.g. `Float32Array`; used as a schema field type. */
|
|
4
|
+
export type ArrayConstructor = {
|
|
5
|
+
new (length: number): NumericArray;
|
|
6
|
+
};
|
|
7
|
+
/** Component layout: field name to storage constructor. Must have at least one field. */
|
|
8
|
+
export type Schema = Record<string, ArrayConstructor>;
|
|
9
|
+
/** Entity id: `generation * stride + slot`, a non-negative integer up to `0xffffffff`. Stale ids fail `World.alive`. */
|
|
10
|
+
export type Entity = number;
|
|
11
|
+
/** Membership and lifetime common to numeric, tag and mixed-reference stores. */
|
|
12
|
+
export type ComponentStore = Component<Record<never, never>>;
|
|
13
|
+
/** Told about every row change of a component, after it happened. Push one onto {@link Component.observers}. */
|
|
14
|
+
export interface ComponentObserver {
|
|
15
|
+
/** `entity` gained a row. */
|
|
16
|
+
onAdd(entity: Entity): void;
|
|
17
|
+
/** `add` wrote values into the row `entity` already had, or `touch` reported a direct write. */
|
|
18
|
+
onChange(entity: Entity): void;
|
|
19
|
+
/** `entity` lost its row; a destroyed entity is reported here before its id goes stale. */
|
|
20
|
+
onRemove(entity: Entity): void;
|
|
21
|
+
}
|
|
22
|
+
/** Packed sparse-set component store. Data rows move when entities are removed. */
|
|
23
|
+
export declare class Component<S extends Schema = Schema> {
|
|
24
|
+
readonly world: World;
|
|
25
|
+
readonly fields: {
|
|
26
|
+
[K in keyof S]: InstanceType<S[K]>;
|
|
27
|
+
};
|
|
28
|
+
readonly entities: Uint32Array;
|
|
29
|
+
private readonly sparse;
|
|
30
|
+
/** Field arrays in schema order, so row moves need no per-call Object.values. */
|
|
31
|
+
private readonly columns;
|
|
32
|
+
size: number;
|
|
33
|
+
/** Called on every row change; `ChangeTracker` from `@margoengine/ecs/changes` is the usual user. */
|
|
34
|
+
readonly observers: ComponentObserver[];
|
|
35
|
+
/**
|
|
36
|
+
* Prefer {@link World.component}, which also registers the store for cleanup on `destroy`.
|
|
37
|
+
* @throws TypeError for an empty schema or an unsupported storage constructor.
|
|
38
|
+
* @param tag True only from {@link World.tag}, the one place a component may have no fields.
|
|
39
|
+
*/
|
|
40
|
+
constructor(world: World, schema: S, tag?: boolean);
|
|
41
|
+
/** Row of `entity` in the field arrays, or -1 when it is dead or has no such component. */
|
|
42
|
+
index(entity: Entity): number;
|
|
43
|
+
/** True when `entity` is alive and has this component. */
|
|
44
|
+
has(entity: Entity): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Add the component to `entity` (new rows start zeroed) or overwrite fields of an existing row; returns the row.
|
|
47
|
+
* @throws MargoError `'state'` when the entity is stale or destroyed.
|
|
48
|
+
* @throws RangeError for an unknown field or a value that is not finite or does not fit the field's storage type.
|
|
49
|
+
*/
|
|
50
|
+
add(entity: Entity, values?: Partial<Record<keyof S, number>>): number;
|
|
51
|
+
/** Validate all values before changing a row; optional stores may extend field storage. */
|
|
52
|
+
protected validateValues(values: Partial<Record<keyof S, number>>): void;
|
|
53
|
+
/** Initialize storage before the add notification. */
|
|
54
|
+
protected initializeRow(row: number, _values?: Partial<Record<keyof S, number>>): void;
|
|
55
|
+
/** Move storage before the remove notification. Optional stores also release reference fields here. */
|
|
56
|
+
protected moveRow(row: number, last: number): void;
|
|
57
|
+
/** Report a write made straight into `fields`, which observers cannot see; false when `entity` has no row. */
|
|
58
|
+
touch(entity: Entity): boolean;
|
|
59
|
+
/** Remove the component from `entity`, moving the last row into its place; false when it had none. */
|
|
60
|
+
remove(entity: Entity): boolean;
|
|
61
|
+
}
|
|
62
|
+
/** Fixed-capacity entity allocator with generational ids and the component stores created from it. */
|
|
63
|
+
export declare class World {
|
|
64
|
+
readonly capacity: number;
|
|
65
|
+
readonly stride: number;
|
|
66
|
+
private readonly live;
|
|
67
|
+
private readonly generations;
|
|
68
|
+
private readonly free;
|
|
69
|
+
private freeCount;
|
|
70
|
+
private readonly components;
|
|
71
|
+
/**
|
|
72
|
+
* @param capacity Maximum live entities, an integer in [1, 1_000_000]. Default 4096.
|
|
73
|
+
* @param stride Spacing of entity ids, at least `capacity`; slots can never pass it. Default `capacity`.
|
|
74
|
+
* `GrowableWorld` sets it to its limit, so ids survive growth.
|
|
75
|
+
* @throws RangeError for an invalid capacity or stride.
|
|
76
|
+
*/
|
|
77
|
+
constructor(capacity?: number, stride?: number);
|
|
78
|
+
/**
|
|
79
|
+
* A zeroed array with one element per slot, filled with `fill` when given. Component stores come from here; in a
|
|
80
|
+
* `GrowableWorld` it keeps its length in step with `capacity`, which makes it the right query output there.
|
|
81
|
+
*/
|
|
82
|
+
allocate<T extends NumericArray>(Type: new (length: number) => T, fill?: number): T;
|
|
83
|
+
/** Number of free, nonretired entity slots. */
|
|
84
|
+
get available(): number;
|
|
85
|
+
/** Ensure at least count free slots, growing only buffers; throws RangeError before entity mutation if unavailable. */
|
|
86
|
+
reserve(count: number): void;
|
|
87
|
+
/** Write current entity ids, excluding teardown, into output; throws RangeError if the output is too small. */
|
|
88
|
+
entities(output: Uint32Array): number;
|
|
89
|
+
/** Make slots `from` up to `to` free, lowest first out. */
|
|
90
|
+
protected release(from: number, to: number): void;
|
|
91
|
+
/**
|
|
92
|
+
* Called by `create` with no free slot; must free at least one or throw.
|
|
93
|
+
* @throws RangeError in a fixed world.
|
|
94
|
+
*/
|
|
95
|
+
protected grow(): void;
|
|
96
|
+
/** True when `entity` is a current id of this world (created and not destroyed since). */
|
|
97
|
+
alive(entity: Entity): boolean;
|
|
98
|
+
/** True only while the entity is being destroyed; avoids transient teardown matches. */
|
|
99
|
+
destroying(entity: Entity): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Assert that `entity` is alive.
|
|
102
|
+
* @throws MargoError `'state'` when the entity is stale or destroyed.
|
|
103
|
+
*/
|
|
104
|
+
check(entity: Entity): void;
|
|
105
|
+
/**
|
|
106
|
+
* Allocate a new entity id.
|
|
107
|
+
* @throws RangeError when all `capacity` slots are in use and the world cannot grow.
|
|
108
|
+
*/
|
|
109
|
+
create(): Entity;
|
|
110
|
+
/**
|
|
111
|
+
* Remove `entity` from every component of this world and free its slot (retired instead once
|
|
112
|
+
* its next id would exceed `0xffffffff`); the old id becomes stale.
|
|
113
|
+
* A call made while the same entity is being destroyed, from a store's `remove`, does nothing.
|
|
114
|
+
* @throws MargoError `'state'` when the entity is already stale or destroyed.
|
|
115
|
+
*/
|
|
116
|
+
destroy(entity: Entity): void;
|
|
117
|
+
/** Have `store.remove(entity)` called for every entity destroyed, while it is still alive; `Relation` uses this. */
|
|
118
|
+
attach(store: {
|
|
119
|
+
remove(entity: Entity): unknown;
|
|
120
|
+
}): void;
|
|
121
|
+
/** Undo {@link World.attach}. */
|
|
122
|
+
detach(store: {
|
|
123
|
+
remove(entity: Entity): unknown;
|
|
124
|
+
}): void;
|
|
125
|
+
/**
|
|
126
|
+
* Create a component store sized to `capacity` and register it with this world.
|
|
127
|
+
* @throws TypeError for an empty schema or an unsupported storage constructor.
|
|
128
|
+
*/
|
|
129
|
+
component<S extends Schema>(schema: S): Component<S>;
|
|
130
|
+
/** Create a tag: a component with no fields, for marks like `frozen`. Use it like any component: add, remove, has, query. */
|
|
131
|
+
tag(): Component<Record<never, never>>;
|
|
132
|
+
/**
|
|
133
|
+
* Query output is caller-owned; returns the number of matching entity IDs.
|
|
134
|
+
* @throws TypeError when `components` is empty or holds a component of another world.
|
|
135
|
+
* @throws RangeError when `output` is too small for the matches.
|
|
136
|
+
*/
|
|
137
|
+
query(components: readonly ComponentStore[], output: Uint32Array): number;
|
|
138
|
+
}
|
|
139
|
+
export { Schedule } from './schedule.js';
|
|
140
|
+
export type { ScheduleOptions, ScheduleTime, SystemOptions, SystemRun } from './schedule.js';
|
|
141
|
+
export { ChangeTracker } from './changes.js';
|
|
142
|
+
export { Query } from './query.js';
|
|
143
|
+
export type { QueryTerms } from './query.js';
|
|
144
|
+
export { Relation } from './relations.js';
|
|
145
|
+
export type { RelationOptions } from './relations.js';
|