@margoengine/ecs 0.0.0-stage → 0.1.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 +294 -3
- 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 +30 -0
- package/dist/objects.js +50 -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 +44 -0
- package/dist/relation-query.js +79 -0
- package/dist/relations.d.ts +70 -0
- package/dist/relations.js +93 -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 +107 -6
package/dist/query.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ComponentStore as Component, Entity, World } from './index.js';
|
|
2
|
+
/** Terms of a {@link Query}. Tags from `world.tag()` work in all three lists. */
|
|
3
|
+
export interface QueryTerms {
|
|
4
|
+
/** An entity must have every one of these. At least one. */
|
|
5
|
+
with: readonly Component[];
|
|
6
|
+
/** An entity must have none of these. Default none. */
|
|
7
|
+
without?: readonly Component[];
|
|
8
|
+
/** An entity may have these or not; they never change the match, only the rows given to `forEach`. Default none. */
|
|
9
|
+
optional?: readonly Component[];
|
|
10
|
+
}
|
|
11
|
+
/** A reusable set of terms: required components, excluded components and optional ones. */
|
|
12
|
+
export declare class Query {
|
|
13
|
+
readonly world: World;
|
|
14
|
+
private readonly required;
|
|
15
|
+
private readonly excluded;
|
|
16
|
+
private readonly reads;
|
|
17
|
+
private readonly rows;
|
|
18
|
+
private matches;
|
|
19
|
+
private visiting;
|
|
20
|
+
/** @throws TypeError when `with` is empty or a term belongs to another world. */
|
|
21
|
+
constructor(world: World, terms: QueryTerms);
|
|
22
|
+
/** Fill the private match list; `world.query` does the intersection and the rows that fail `without` drop out. */
|
|
23
|
+
private collect;
|
|
24
|
+
private allowed;
|
|
25
|
+
/**
|
|
26
|
+
* Write the matching entities into caller-owned `output`; returns how many. Optional terms have no effect here.
|
|
27
|
+
* @throws RangeError when `output` is too small (`world.capacity` entries always fit).
|
|
28
|
+
* @throws MargoError `'state'` when called from this query's own `forEach`.
|
|
29
|
+
*/
|
|
30
|
+
run(output: Uint32Array): number;
|
|
31
|
+
/**
|
|
32
|
+
* Call `visit` for each match with its rows: `rows[i]` is the row in the i-th `with` term, then the `optional` terms
|
|
33
|
+
* in order, -1 for an optional term the entity lacks. `rows` is reused, so read it during the call.
|
|
34
|
+
*
|
|
35
|
+
* The match list is taken first, so `visit` may add, remove and destroy freely: rows are looked up just before each
|
|
36
|
+
* call, and an entity that stopped matching meanwhile is passed over. Entities that start matching are not visited.
|
|
37
|
+
* Returns the number of calls made.
|
|
38
|
+
* @throws MargoError `'state'` when called from this query's own `forEach`; a second Query object may nest.
|
|
39
|
+
*/
|
|
40
|
+
forEach(visit: (entity: Entity, rows: Int32Array) => void): number;
|
|
41
|
+
private enter;
|
|
42
|
+
}
|
package/dist/query.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/query.ts
|
|
3
|
+
var t = class {
|
|
4
|
+
constructor(e, t) {
|
|
5
|
+
this.world = e, this.matches = /* @__PURE__ */ new Uint32Array(), this.visiting = !1;
|
|
6
|
+
let n = [...t.with ?? []], r = [...t.without ?? []], i = [...t.optional ?? []];
|
|
7
|
+
if (!n.length || [
|
|
8
|
+
...n,
|
|
9
|
+
...r,
|
|
10
|
+
...i
|
|
11
|
+
].some((t) => t.world !== e)) throw TypeError("Query needs components of this world; pass at least one in with");
|
|
12
|
+
this.required = n, this.excluded = r, this.reads = [...n, ...i], this.rows = new Int32Array(this.reads.length);
|
|
13
|
+
}
|
|
14
|
+
collect() {
|
|
15
|
+
let { world: e, excluded: t } = this;
|
|
16
|
+
this.matches.length < e.capacity && (this.matches = new Uint32Array(e.capacity));
|
|
17
|
+
let n = this.matches, r = e.query(this.required, n);
|
|
18
|
+
if (!t.length) return r;
|
|
19
|
+
let i = 0;
|
|
20
|
+
for (let e = 0; e < r; e++) {
|
|
21
|
+
let t = n[e];
|
|
22
|
+
this.allowed(t) && (n[i++] = t);
|
|
23
|
+
}
|
|
24
|
+
return i;
|
|
25
|
+
}
|
|
26
|
+
allowed(e) {
|
|
27
|
+
let { excluded: t } = this;
|
|
28
|
+
for (let n = 0; n < t.length; n++) if (t[n].index(e) >= 0) return !1;
|
|
29
|
+
return !0;
|
|
30
|
+
}
|
|
31
|
+
run(e) {
|
|
32
|
+
this.enter();
|
|
33
|
+
let t = this.collect();
|
|
34
|
+
if (e.length < t) throw RangeError("Query output is too small; pass a Uint32Array at least as long as the entity count (world.capacity always fits)");
|
|
35
|
+
return e.set(this.matches.subarray(0, t)), t;
|
|
36
|
+
}
|
|
37
|
+
forEach(e) {
|
|
38
|
+
this.enter();
|
|
39
|
+
let { reads: t, rows: n, required: r } = this, i = r.length, a = this.collect(), o = this.matches, s = 0;
|
|
40
|
+
this.visiting = !0;
|
|
41
|
+
try {
|
|
42
|
+
next: for (let r = 0; r < a; r++) {
|
|
43
|
+
let a = o[r];
|
|
44
|
+
for (let e = 0; e < t.length; e++) {
|
|
45
|
+
let r = t[e].index(a);
|
|
46
|
+
if (r < 0 && e < i) continue next;
|
|
47
|
+
n[e] = r;
|
|
48
|
+
}
|
|
49
|
+
this.allowed(a) && (e(a, n), s++);
|
|
50
|
+
}
|
|
51
|
+
} finally {
|
|
52
|
+
this.visiting = !1;
|
|
53
|
+
}
|
|
54
|
+
return s;
|
|
55
|
+
}
|
|
56
|
+
enter() {
|
|
57
|
+
if (this.visiting) throw new e("state", "Query is inside its own forEach; use a second Query for a nested loop");
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
export { t as Query };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Entity, World } from './index.js';
|
|
2
|
+
import type { QueryTerms } from './query.js';
|
|
3
|
+
/** Per-query structural transitions delivered by flush(). */
|
|
4
|
+
export interface QueryListener {
|
|
5
|
+
/** Entity entered the query. Default no callback. */
|
|
6
|
+
onEnter?: (entity: Entity) => void;
|
|
7
|
+
/** Entity exited the query. Its id can be stale after destruction. Default no callback. */
|
|
8
|
+
onExit?: (entity: Entity) => void;
|
|
9
|
+
}
|
|
10
|
+
/** An incrementally maintained component query with independent enter/exit events. */
|
|
11
|
+
export declare class ReactiveQuery {
|
|
12
|
+
readonly world: World;
|
|
13
|
+
private readonly required;
|
|
14
|
+
private readonly excluded;
|
|
15
|
+
private readonly reads;
|
|
16
|
+
private readonly watched;
|
|
17
|
+
private readonly dense;
|
|
18
|
+
private readonly positions;
|
|
19
|
+
private readonly listeners;
|
|
20
|
+
private events;
|
|
21
|
+
private stopped;
|
|
22
|
+
private visiting;
|
|
23
|
+
private flushing;
|
|
24
|
+
private readonly observer;
|
|
25
|
+
/** @throws TypeError when with is empty or a component belongs to another world. */
|
|
26
|
+
constructor(world: World, terms: QueryTerms);
|
|
27
|
+
/** Number of cached matches. No world scan. */
|
|
28
|
+
get size(): number;
|
|
29
|
+
/** True when the entity is in the current cache. */
|
|
30
|
+
has(entity: Entity): boolean;
|
|
31
|
+
private matches;
|
|
32
|
+
private refresh;
|
|
33
|
+
/**
|
|
34
|
+
* Subscribe to future transitions; initial:true immediately visits current entries for this listener alone.
|
|
35
|
+
* Returns an idempotent unsubscribe function.
|
|
36
|
+
* @throws MargoError 'disposed' after stop.
|
|
37
|
+
*/
|
|
38
|
+
subscribe(listener: QueryListener, { initial }?: {
|
|
39
|
+
initial?: boolean;
|
|
40
|
+
}): () => void;
|
|
41
|
+
/**
|
|
42
|
+
* Deliver queued transitions in order. Listener mutations queue transitions for the next flush.
|
|
43
|
+
* An error propagates; already delivered transitions are not replayed, remaining transitions stay pending.
|
|
44
|
+
* @throws MargoError 'state' for nested flush, or 'disposed' after stop.
|
|
45
|
+
*/
|
|
46
|
+
flush(): number;
|
|
47
|
+
/** Write cached ids without scanning components; throws RangeError for an undersized output. */
|
|
48
|
+
run(output: Uint32Array): number;
|
|
49
|
+
/**
|
|
50
|
+
* Snapshot cached ids, then resolve current rows. Newly entered matches wait until the next iteration.
|
|
51
|
+
* Rows are reused and ordered with then optional (-1 when absent).
|
|
52
|
+
* @throws MargoError 'state' for a nested iteration, or 'disposed' after stop.
|
|
53
|
+
*/
|
|
54
|
+
forEach(visit: (entity: Entity, rows: Int32Array) => void): number;
|
|
55
|
+
/** Detach observers and release cached ids, transitions and listeners. Idempotent. */
|
|
56
|
+
stop(): void;
|
|
57
|
+
[Symbol.dispose]: () => void;
|
|
58
|
+
private check;
|
|
59
|
+
}
|
package/dist/reactive.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/reactive.ts
|
|
3
|
+
var t = class {
|
|
4
|
+
constructor(e, t) {
|
|
5
|
+
if (this.world = e, this.dense = [], this.positions = /* @__PURE__ */ new Map(), this.listeners = /* @__PURE__ */ new Set(), this.events = [], this.stopped = !1, this.visiting = !1, this.flushing = !1, this.observer = {
|
|
6
|
+
onAdd: (e) => this.refresh(e),
|
|
7
|
+
onRemove: (e) => this.refresh(e),
|
|
8
|
+
onChange: () => {}
|
|
9
|
+
}, this.required = [...t.with], this.excluded = [...t.without ?? []], this.reads = [...this.required, ...t.optional ?? []], this.watched = [.../* @__PURE__ */ new Set([...this.required, ...this.excluded])], !this.required.length || [...this.watched, ...this.reads].some((t) => t.world !== e)) throw TypeError("Invalid reactive query; pass at least one required component and only components of this world");
|
|
10
|
+
let n = this.required[0];
|
|
11
|
+
for (let e of this.required) e.size < n.size && (n = e);
|
|
12
|
+
for (let e = 0; e < n.size; e++) this.refresh(n.entities[e]);
|
|
13
|
+
this.events.length = 0;
|
|
14
|
+
for (let e of this.watched) e.observers.push(this.observer);
|
|
15
|
+
}
|
|
16
|
+
get size() {
|
|
17
|
+
return this.dense.length;
|
|
18
|
+
}
|
|
19
|
+
has(e) {
|
|
20
|
+
return this.positions.has(e);
|
|
21
|
+
}
|
|
22
|
+
matches(e) {
|
|
23
|
+
return this.world.alive(e) && !this.world.destroying(e) && this.required.every((t) => t.has(e)) && this.excluded.every((t) => !t.has(e));
|
|
24
|
+
}
|
|
25
|
+
refresh(e) {
|
|
26
|
+
if (this.stopped) return;
|
|
27
|
+
let t = this.positions.get(e), n = this.matches(e);
|
|
28
|
+
if (n && t === void 0) this.positions.set(e, this.dense.length), this.dense.push(e), this.listeners.size && this.events.push([
|
|
29
|
+
!0,
|
|
30
|
+
e,
|
|
31
|
+
[...this.listeners]
|
|
32
|
+
]);
|
|
33
|
+
else if (!n && t !== void 0) {
|
|
34
|
+
let n = this.dense.pop();
|
|
35
|
+
t < this.dense.length && (this.dense[t] = n, this.positions.set(n, t)), this.positions.delete(e), this.listeners.size && this.events.push([
|
|
36
|
+
!1,
|
|
37
|
+
e,
|
|
38
|
+
[...this.listeners]
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
subscribe(e, { initial: t = !1 } = {}) {
|
|
43
|
+
if (this.check(), this.listeners.add(e), t) for (let t of [...this.dense]) {
|
|
44
|
+
if (!this.listeners.has(e) || this.stopped) break;
|
|
45
|
+
e.onEnter?.(t);
|
|
46
|
+
}
|
|
47
|
+
return () => {
|
|
48
|
+
this.listeners.delete(e);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
flush() {
|
|
52
|
+
if (this.check(), this.flushing) throw new e("state", "Reactive query is flushing; flush again after callbacks return");
|
|
53
|
+
this.flushing = !0;
|
|
54
|
+
let t = this.events;
|
|
55
|
+
this.events = [];
|
|
56
|
+
let n = 0;
|
|
57
|
+
try {
|
|
58
|
+
for (let e = 0; e < t.length && !this.stopped; e++) {
|
|
59
|
+
let [r, i, a] = t[e];
|
|
60
|
+
n++;
|
|
61
|
+
for (let e of a) {
|
|
62
|
+
if (this.stopped) break;
|
|
63
|
+
this.listeners.has(e) && (r ? e.onEnter : e.onExit)?.(i);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} finally {
|
|
67
|
+
!this.stopped && n < t.length && (this.events = [...t.slice(n), ...this.events]), this.flushing = !1;
|
|
68
|
+
}
|
|
69
|
+
return n;
|
|
70
|
+
}
|
|
71
|
+
run(e) {
|
|
72
|
+
if (this.check(), e.length < this.size) throw RangeError("Reactive query output is too small; allocate at least query.size entries");
|
|
73
|
+
return e.set(this.dense), this.size;
|
|
74
|
+
}
|
|
75
|
+
forEach(t) {
|
|
76
|
+
if (this.check(), this.visiting) throw new e("state", "Reactive query is visiting; use another query to nest iteration");
|
|
77
|
+
let n = [...this.dense], r = new Int32Array(this.reads.length), i = 0;
|
|
78
|
+
this.visiting = !0;
|
|
79
|
+
try {
|
|
80
|
+
for (let e of n) !this.stopped && this.positions.has(e) && (this.reads.forEach((t, n) => {
|
|
81
|
+
r[n] = t.index(e);
|
|
82
|
+
}), t(e, r), i++);
|
|
83
|
+
} finally {
|
|
84
|
+
this.visiting = !1;
|
|
85
|
+
}
|
|
86
|
+
return i;
|
|
87
|
+
}
|
|
88
|
+
stop() {
|
|
89
|
+
if (!this.stopped) {
|
|
90
|
+
this.stopped = !0;
|
|
91
|
+
for (let e of this.watched) {
|
|
92
|
+
let t = e.observers.indexOf(this.observer);
|
|
93
|
+
t >= 0 && e.observers.splice(t, 1);
|
|
94
|
+
}
|
|
95
|
+
this.positions.clear(), this.dense.length = 0, this.events.length = 0, this.listeners.clear();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
99
|
+
this.stop();
|
|
100
|
+
}
|
|
101
|
+
check() {
|
|
102
|
+
if (this.stopped) throw new e("disposed", "Reactive query stopped; create a new query before reading or subscribing");
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
//#endregion
|
|
106
|
+
export { t as ReactiveQuery };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ComponentStore as Component, Entity, World } from './index.js';
|
|
2
|
+
import type { Relation } from './relations.js';
|
|
3
|
+
/** A required pair and optional required components on its target. */
|
|
4
|
+
export interface RelationTerm {
|
|
5
|
+
/** Relationship owned by this world. */
|
|
6
|
+
relation: Relation;
|
|
7
|
+
/** Exact target id, or '*' for every target. Default '*'. */
|
|
8
|
+
target?: Entity | '*';
|
|
9
|
+
/** Components required on the target, with rows returned in term order. Default none. */
|
|
10
|
+
with?: readonly Component[];
|
|
11
|
+
}
|
|
12
|
+
/** Source component filters and required relationship pairs. */
|
|
13
|
+
export interface RelationQueryTerms {
|
|
14
|
+
/** Required pair terms; at least one. Wildcards enumerate every combination. */
|
|
15
|
+
relations: readonly RelationTerm[];
|
|
16
|
+
/** Required source components. Default none. */
|
|
17
|
+
with?: readonly Component[];
|
|
18
|
+
/** Excluded source components. Default none. */
|
|
19
|
+
without?: readonly Component[];
|
|
20
|
+
/** Optional source components; rows use -1 when absent. Default none. */
|
|
21
|
+
optional?: readonly Component[];
|
|
22
|
+
}
|
|
23
|
+
/** A relation join query returning source rows, matched targets and target component rows. */
|
|
24
|
+
export declare class RelationQuery {
|
|
25
|
+
readonly world: World;
|
|
26
|
+
private readonly terms;
|
|
27
|
+
private visiting;
|
|
28
|
+
/** @throws TypeError for empty relation terms, invalid targets or foreign-world terms. */
|
|
29
|
+
constructor(world: World, terms: RelationQueryTerms);
|
|
30
|
+
private allowed;
|
|
31
|
+
private collect;
|
|
32
|
+
/**
|
|
33
|
+
* Write sources (repeated for each matching pair combination) and optional target columns.
|
|
34
|
+
* @throws RangeError when any output is too small, or target column count differs from the term count.
|
|
35
|
+
*/
|
|
36
|
+
run(output: Uint32Array, targets?: readonly Uint32Array[]): number;
|
|
37
|
+
/**
|
|
38
|
+
* Visit snapshot matches, rechecking membership and resolving current rows before each call.
|
|
39
|
+
* Source rows are with then optional. Target rows flatten each relation term's with list.
|
|
40
|
+
* Callback arrays are reused. Added pairs wait for the next call; removed pairs are passed over.
|
|
41
|
+
* @throws MargoError 'state' for a nested call on this query.
|
|
42
|
+
*/
|
|
43
|
+
forEach(visit: (source: Entity, rows: Int32Array, targets: Uint32Array, targetRows: Int32Array) => void): number;
|
|
44
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { MargoError as e } from "@margoengine/math/errors";
|
|
2
|
+
//#region src/relation-query.ts
|
|
3
|
+
var t = class {
|
|
4
|
+
constructor(e, t) {
|
|
5
|
+
this.world = e, this.visiting = !1;
|
|
6
|
+
let n = t.relations.map((e) => ({
|
|
7
|
+
...e,
|
|
8
|
+
with: [...e.with ?? []]
|
|
9
|
+
})), r = [...t.with ?? []], i = [...t.without ?? []], a = [...t.optional ?? []];
|
|
10
|
+
if (!n.length || n.some((t) => t.relation.world !== e || t.target !== void 0 && t.target !== "*" && (!Number.isInteger(t.target) || t.target < 0 || t.target > 4294967295)) || [
|
|
11
|
+
...r,
|
|
12
|
+
...i,
|
|
13
|
+
...a,
|
|
14
|
+
...n.flatMap((e) => e.with)
|
|
15
|
+
].some((t) => t.world !== e)) throw TypeError("Invalid relation query; use at least one relation and components of this world with Uint32 targets or *");
|
|
16
|
+
this.terms = {
|
|
17
|
+
relations: n,
|
|
18
|
+
with: r,
|
|
19
|
+
without: i,
|
|
20
|
+
optional: a
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
allowed(e, t) {
|
|
24
|
+
let n = this.terms;
|
|
25
|
+
return this.world.alive(e) && n.with.every((t) => t.has(e)) && n.without.every((t) => !t.has(e)) && (!t || n.relations.every((n, r) => this.world.alive(t[r]) && n.relation.has(e, t[r]) && n.with.every((e) => e.has(t[r]))));
|
|
26
|
+
}
|
|
27
|
+
collect() {
|
|
28
|
+
if (this.visiting) throw new e("state", "Relation query is visiting; use a second query for a nested call");
|
|
29
|
+
let t = this.terms.relations.map((e) => {
|
|
30
|
+
let t = /* @__PURE__ */ new Map();
|
|
31
|
+
return e.relation.forEach((n, r) => {
|
|
32
|
+
if ((e.target === void 0 || e.target === "*" || e.target === r) && e.with.every((e) => e.has(r))) {
|
|
33
|
+
let e = t.get(n);
|
|
34
|
+
e ? e.push(r) : t.set(n, [r]);
|
|
35
|
+
}
|
|
36
|
+
}), t;
|
|
37
|
+
}), n = [];
|
|
38
|
+
for (let e of t[0].keys()) {
|
|
39
|
+
if (!this.allowed(e) || t.some((t) => !t.has(e))) continue;
|
|
40
|
+
let r = [[]];
|
|
41
|
+
for (let n of t) r = r.flatMap((t) => n.get(e).map((e) => [...t, e]));
|
|
42
|
+
for (let t of r) n.push({
|
|
43
|
+
source: e,
|
|
44
|
+
targets: t
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return n;
|
|
48
|
+
}
|
|
49
|
+
run(e, t) {
|
|
50
|
+
let n = this.collect();
|
|
51
|
+
if (e.length < n.length || t && (t.length !== this.terms.relations.length || t.some((e) => e.length < n.length))) throw RangeError("Relation query output is too small; allocate one row per pair combination and one target column per term");
|
|
52
|
+
return n.forEach((n, r) => {
|
|
53
|
+
e[r] = n.source, t?.forEach((e, t) => {
|
|
54
|
+
e[r] = n.targets[t];
|
|
55
|
+
});
|
|
56
|
+
}), n.length;
|
|
57
|
+
}
|
|
58
|
+
forEach(e) {
|
|
59
|
+
let t = this.collect(), n = [...this.terms.with, ...this.terms.optional], r = new Int32Array(n.length), i = new Uint32Array(this.terms.relations.length), a = new Int32Array(this.terms.relations.reduce((e, t) => e + t.with.length, 0)), o = 0;
|
|
60
|
+
this.visiting = !0;
|
|
61
|
+
try {
|
|
62
|
+
for (let s of t) {
|
|
63
|
+
if (!this.allowed(s.source, s.targets)) continue;
|
|
64
|
+
n.forEach((e, t) => {
|
|
65
|
+
r[t] = e.index(s.source);
|
|
66
|
+
}), i.set(s.targets);
|
|
67
|
+
let t = 0;
|
|
68
|
+
this.terms.relations.forEach((e, n) => {
|
|
69
|
+
for (let r of e.with) a[t++] = r.index(i[n]);
|
|
70
|
+
}), e(s.source, r, i, a), o++;
|
|
71
|
+
}
|
|
72
|
+
} finally {
|
|
73
|
+
this.visiting = !1;
|
|
74
|
+
}
|
|
75
|
+
return o;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
//#endregion
|
|
79
|
+
export { t as RelationQuery };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Entity, World } from './index.js';
|
|
2
|
+
/** Rules of a {@link Relation}. For parent and child use all three: `{exclusive: true, cascade: true, acyclic: true}`. */
|
|
3
|
+
export interface RelationOptions {
|
|
4
|
+
/** A source has at most one target: a new pair replaces the old one. Default false. */
|
|
5
|
+
exclusive?: boolean;
|
|
6
|
+
/** Destroying a target destroys its sources, and theirs, all the way down. Default false: only the pairs end. */
|
|
7
|
+
cascade?: boolean;
|
|
8
|
+
/** Refuse a pair that would close a loop. Default false. */
|
|
9
|
+
acyclic?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Pairs of entities with a meaning you name: `childOf`, `owns`, `targets`. A pair goes from a source to a target, and
|
|
13
|
+
* both directions can be read directly. Pairs of a destroyed entity end with it.
|
|
14
|
+
*/
|
|
15
|
+
export declare class Relation {
|
|
16
|
+
readonly world: World;
|
|
17
|
+
/** Pairs held now. */
|
|
18
|
+
size: number;
|
|
19
|
+
/** True when a source has at most one target. */
|
|
20
|
+
readonly exclusive: boolean;
|
|
21
|
+
/** True when destroying a target destroys its sources. */
|
|
22
|
+
readonly cascade: boolean;
|
|
23
|
+
/** True when pairs that close a loop are refused. */
|
|
24
|
+
readonly acyclic: boolean;
|
|
25
|
+
private readonly forward;
|
|
26
|
+
private readonly backward;
|
|
27
|
+
private readonly pending;
|
|
28
|
+
private draining;
|
|
29
|
+
private readonly store;
|
|
30
|
+
constructor(world: World, { exclusive, cascade, acyclic }?: RelationOptions);
|
|
31
|
+
/**
|
|
32
|
+
* Add the pair; false when it already exists. On an exclusive relation it replaces the source's old pair.
|
|
33
|
+
* @throws MargoError `'state'` when either entity is stale or destroyed.
|
|
34
|
+
* @throws TypeError on an acyclic relation when the pair would close a loop; nothing changes.
|
|
35
|
+
*/
|
|
36
|
+
add(source: Entity, target: Entity): boolean;
|
|
37
|
+
/** End the pair; false when there is none. Neither entity is destroyed. */
|
|
38
|
+
remove(source: Entity, target: Entity): boolean;
|
|
39
|
+
/** True when the pair exists. */
|
|
40
|
+
has(source: Entity, target: Entity): boolean;
|
|
41
|
+
/** The first target of `source`, the only one on an exclusive relation (a child's parent); undefined when it has none. */
|
|
42
|
+
target(source: Entity): Entity | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Write what `source` points at into `output`, oldest pair first; returns how many.
|
|
45
|
+
* @throws RangeError when `output` is too small (`world.capacity` entries always fit).
|
|
46
|
+
*/
|
|
47
|
+
targets(source: Entity, output: Uint32Array): number;
|
|
48
|
+
/**
|
|
49
|
+
* Write what points at `target` into `output`, oldest pair first (a parent's children); returns how many.
|
|
50
|
+
* @throws RangeError when `output` is too small (`world.capacity` entries always fit).
|
|
51
|
+
*/
|
|
52
|
+
sources(target: Entity, output: Uint32Array): number;
|
|
53
|
+
/**
|
|
54
|
+
* Write everything below `target`, breadth first (nearest first): its sources, their sources and so on, without
|
|
55
|
+
* `target` itself and each entity once, even in a loop; returns how many.
|
|
56
|
+
* @throws RangeError when `output` is too small (`world.capacity` entries always fit).
|
|
57
|
+
*/
|
|
58
|
+
descendants(target: Entity, output: Uint32Array): number;
|
|
59
|
+
/** Call `visit` for every pair. Pairs added during the loop may or may not be visited. */
|
|
60
|
+
forEach(visit: (source: Entity, target: Entity) => void): void;
|
|
61
|
+
/** End every pair and stop following the world. Safe to call twice. */
|
|
62
|
+
stop(): void;
|
|
63
|
+
[Symbol.dispose]: () => void;
|
|
64
|
+
private link;
|
|
65
|
+
private unlink;
|
|
66
|
+
/** True when walking targets from `from` arrives at `goal`, `from` itself included. */
|
|
67
|
+
private reaches;
|
|
68
|
+
/** The world is destroying `entity`. Cascades run from a list, so a deep hierarchy cannot overflow the call stack. */
|
|
69
|
+
private forget;
|
|
70
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
//#region src/relations.ts
|
|
2
|
+
var e = (e, t) => {
|
|
3
|
+
if (!e) return 0;
|
|
4
|
+
if (t.length < e.size) throw RangeError("Relation output is too small; pass a Uint32Array at least as long as the entity count (world.capacity always fits)");
|
|
5
|
+
let n = 0;
|
|
6
|
+
for (let r of e) t[n++] = r;
|
|
7
|
+
return n;
|
|
8
|
+
}, t = class {
|
|
9
|
+
constructor(e, { exclusive: t = !1, cascade: n = !1, acyclic: r = !1 } = {}) {
|
|
10
|
+
this.world = e, this.size = 0, this.forward = /* @__PURE__ */ new Map(), this.backward = /* @__PURE__ */ new Map(), this.pending = [], this.draining = !1, this.store = { remove: (e) => this.forget(e) }, this.exclusive = t, this.cascade = n, this.acyclic = r, e.attach(this.store);
|
|
11
|
+
}
|
|
12
|
+
add(e, t) {
|
|
13
|
+
if (this.world.check(e), this.world.check(t), this.has(e, t)) return !1;
|
|
14
|
+
if (this.acyclic && this.reaches(t, e)) throw TypeError("Relation pair would close a loop; remove a pair first, or drop the acyclic option");
|
|
15
|
+
if (this.exclusive) {
|
|
16
|
+
let t = this.forward.get(e);
|
|
17
|
+
if (t) for (let n of t) this.remove(e, n);
|
|
18
|
+
}
|
|
19
|
+
return this.link(this.forward, e, t), this.link(this.backward, t, e), this.size++, !0;
|
|
20
|
+
}
|
|
21
|
+
remove(e, t) {
|
|
22
|
+
return this.has(e, t) ? (this.unlink(this.forward, e, t), this.unlink(this.backward, t, e), this.size--, !0) : !1;
|
|
23
|
+
}
|
|
24
|
+
has(e, t) {
|
|
25
|
+
return this.forward.get(e)?.has(t) ?? !1;
|
|
26
|
+
}
|
|
27
|
+
target(e) {
|
|
28
|
+
let t = this.forward.get(e);
|
|
29
|
+
if (t) for (let e of t) return e;
|
|
30
|
+
}
|
|
31
|
+
targets(t, n) {
|
|
32
|
+
return e(this.forward.get(t), n);
|
|
33
|
+
}
|
|
34
|
+
sources(t, n) {
|
|
35
|
+
return e(this.backward.get(t), n);
|
|
36
|
+
}
|
|
37
|
+
descendants(e, t) {
|
|
38
|
+
let n = /* @__PURE__ */ new Set([e]), r = 0;
|
|
39
|
+
for (let i = -1; i < r; i++) {
|
|
40
|
+
let a = this.backward.get(i < 0 ? e : t[i]);
|
|
41
|
+
if (a) {
|
|
42
|
+
for (let e of a) if (!n.has(e)) {
|
|
43
|
+
if (n.add(e), r === t.length) throw RangeError("Relation output is too small; pass a Uint32Array at least as long as the entity count (world.capacity always fits)");
|
|
44
|
+
t[r++] = e;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return r;
|
|
49
|
+
}
|
|
50
|
+
forEach(e) {
|
|
51
|
+
for (let [t, n] of this.forward) for (let r of n) e(t, r);
|
|
52
|
+
}
|
|
53
|
+
stop() {
|
|
54
|
+
this.world.detach(this.store), this.forward.clear(), this.backward.clear(), this.size = 0;
|
|
55
|
+
}
|
|
56
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
57
|
+
this.stop();
|
|
58
|
+
}
|
|
59
|
+
link(e, t, n) {
|
|
60
|
+
let r = e.get(t);
|
|
61
|
+
r ? r.add(n) : e.set(t, /* @__PURE__ */ new Set([n]));
|
|
62
|
+
}
|
|
63
|
+
unlink(e, t, n) {
|
|
64
|
+
let r = e.get(t);
|
|
65
|
+
r.delete(n), r.size || e.delete(t);
|
|
66
|
+
}
|
|
67
|
+
reaches(e, t) {
|
|
68
|
+
let n = /* @__PURE__ */ new Set([e]), r = [e];
|
|
69
|
+
for (; r.length;) {
|
|
70
|
+
let e = r.pop();
|
|
71
|
+
if (e === t) return !0;
|
|
72
|
+
for (let t of this.forward.get(e) ?? []) n.has(t) || (n.add(t), r.push(t));
|
|
73
|
+
}
|
|
74
|
+
return !1;
|
|
75
|
+
}
|
|
76
|
+
forget(e) {
|
|
77
|
+
for (let t of [...this.forward.get(e) ?? []]) this.remove(e, t);
|
|
78
|
+
for (let t of [...this.backward.get(e) ?? []]) this.remove(t, e), this.cascade && this.pending.push(t);
|
|
79
|
+
if (!this.draining) {
|
|
80
|
+
this.draining = !0;
|
|
81
|
+
try {
|
|
82
|
+
for (; this.pending.length;) {
|
|
83
|
+
let e = this.pending.pop();
|
|
84
|
+
this.world.alive(e) && this.world.destroy(e);
|
|
85
|
+
}
|
|
86
|
+
} finally {
|
|
87
|
+
this.draining = !1, this.pending.length = 0;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
//#endregion
|
|
93
|
+
export { t as Relation };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** What a system sees when it runs. One object is reused, so read it during the call and do not keep it. */
|
|
2
|
+
export interface ScheduleTime {
|
|
3
|
+
/** Seconds this run covers: the fixed step in a fixed system, the frame's time otherwise. */
|
|
4
|
+
delta: number;
|
|
5
|
+
/** Seconds on this phase's clock: steps times the step for fixed systems, the sum of frame times otherwise. */
|
|
6
|
+
elapsed: number;
|
|
7
|
+
/** Runs of this phase so far, starting at 1: the step number in a fixed system, the frame number otherwise. */
|
|
8
|
+
tick: number;
|
|
9
|
+
/** Share of a step still waiting in the accumulator, in [0, 1): blend two fixed states with it. 0 in fixed systems. */
|
|
10
|
+
alpha: number;
|
|
11
|
+
/** True in a fixed system. */
|
|
12
|
+
fixed: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** A system: plain code that reads and writes components, usually through a closure over them. */
|
|
15
|
+
export type SystemRun = (time: Readonly<ScheduleTime>) => void;
|
|
16
|
+
/** Where and when a system runs. */
|
|
17
|
+
export interface SystemOptions {
|
|
18
|
+
/** Names of systems this one runs before. Default none. */
|
|
19
|
+
before?: readonly string[];
|
|
20
|
+
/** Names of systems this one runs after. Default none. */
|
|
21
|
+
after?: readonly string[];
|
|
22
|
+
/** Run condition, asked before every run; the system is passed over when it returns false. Default: always run. */
|
|
23
|
+
when?: (time: Readonly<ScheduleTime>) => boolean;
|
|
24
|
+
/** Run in the fixed phase, zero or more times a frame at the schedule's step. Default false. */
|
|
25
|
+
fixed?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** Settings of a {@link Schedule}. */
|
|
28
|
+
export interface ScheduleOptions {
|
|
29
|
+
/** Seconds per fixed step, above 0. Default 1/60. */
|
|
30
|
+
step?: number;
|
|
31
|
+
/** Most fixed steps in one update; time past that is dropped, so a long stall cannot snowball. Default 8. */
|
|
32
|
+
maxSteps?: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Named systems in a dependable order. Each {@link Schedule.update} runs the fixed phase (every `fixed` system, once per
|
|
36
|
+
* step the elapsed time pays for), then the frame phase (every other system, once). Order inside a phase comes from
|
|
37
|
+
* `before` and `after`, then from the order of `add`.
|
|
38
|
+
*/
|
|
39
|
+
export declare class Schedule {
|
|
40
|
+
/** Seconds per fixed step. */
|
|
41
|
+
readonly step: number;
|
|
42
|
+
/** Most fixed steps in one update. */
|
|
43
|
+
readonly maxSteps: number;
|
|
44
|
+
/** Seconds of frame time thrown away by the last update because of `maxSteps`. */
|
|
45
|
+
dropped: number;
|
|
46
|
+
private readonly entries;
|
|
47
|
+
private fixedPhase;
|
|
48
|
+
private framePhase;
|
|
49
|
+
private accumulator;
|
|
50
|
+
private steps;
|
|
51
|
+
private frames;
|
|
52
|
+
private clock;
|
|
53
|
+
private running;
|
|
54
|
+
private readonly time;
|
|
55
|
+
/** @throws RangeError for a step that is not a finite number above 0, or a maxSteps that is not a positive integer. */
|
|
56
|
+
constructor({ step, maxSteps }?: ScheduleOptions);
|
|
57
|
+
/**
|
|
58
|
+
* Add a system. Added during an update, it first runs in the next one. Names in `before` and `after` are resolved by
|
|
59
|
+
* the next update, so systems can be added in any order.
|
|
60
|
+
* @throws TypeError for an empty or repeated name, or a `run`, `when`, `before` or `after` of the wrong type.
|
|
61
|
+
*/
|
|
62
|
+
add(name: string, run: SystemRun, options?: SystemOptions): this;
|
|
63
|
+
/** Remove a system; false when there is none. Removed during an update, it does not run again, in that update either. */
|
|
64
|
+
remove(name: string): boolean;
|
|
65
|
+
/** True when a system has this name. */
|
|
66
|
+
has(name: string): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* System names in run order, fixed phase first.
|
|
69
|
+
* @throws MargoError `'state'` when `before` or `after` name a missing system, form a cycle, or ask a frame system to
|
|
70
|
+
* run before a fixed one (the fixed phase always comes first).
|
|
71
|
+
*/
|
|
72
|
+
order(): string[];
|
|
73
|
+
/**
|
|
74
|
+
* Advance by `delta` seconds and run the systems; returns the number of fixed steps run. An error from a system
|
|
75
|
+
* reaches the caller and ends the update; time not yet stepped stays in the accumulator.
|
|
76
|
+
* @throws RangeError for a `delta` that is negative or not finite.
|
|
77
|
+
* @throws MargoError `'state'` when called from inside a system, and for the order errors of {@link Schedule.order}.
|
|
78
|
+
*/
|
|
79
|
+
update(delta: number): number;
|
|
80
|
+
private pass;
|
|
81
|
+
private resolve;
|
|
82
|
+
}
|