@daneren2005/shared-memory-ecs 1.10.3 → 1.10.4
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/README.md +23 -0
- package/dist/entity-compression.d.ts +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +201 -108
- package/dist/index.js.map +1 -1
- package/dist/world.d.ts +21 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { _ as e, a as t, c as n, d as r, f as i, g as a, h as o, i as s, l as c, m as l, n as u, o as d, p as f, r as p, s as
|
|
2
|
-
import
|
|
3
|
-
import { MAX_BYTE_OFFSET_LENGTH as
|
|
1
|
+
import { _ as e, a as t, c as n, d as r, f as i, g as a, h as o, i as s, l as c, m as l, n as u, o as d, p as f, r as p, s as ee, t as te, u as ne, v as m, y as h } from "./create-system-worker-BU5CnyVh.js";
|
|
2
|
+
import re from "@daneren2005/shared-memory-objects/memory-heap";
|
|
3
|
+
import { MAX_BYTE_OFFSET_LENGTH as ie } from "@daneren2005/shared-memory-objects/utils/pointer";
|
|
4
4
|
//#region \0rolldown/runtime.js
|
|
5
|
-
var
|
|
6
|
-
if (t && typeof t == "object" || typeof t == "function") for (var i =
|
|
5
|
+
var ae = Object.create, g = Object.defineProperty, _ = Object.getOwnPropertyDescriptor, oe = Object.getOwnPropertyNames, v = Object.getPrototypeOf, y = Object.prototype.hasOwnProperty, b = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), se = (e, t, n, r) => {
|
|
6
|
+
if (t && typeof t == "object" || typeof t == "function") for (var i = oe(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !y.call(e, s) && s !== n && g(e, s, {
|
|
7
7
|
get: ((e) => t[e]).bind(null, s),
|
|
8
|
-
enumerable: !(r =
|
|
8
|
+
enumerable: !(r = _(t, s)) || r.enumerable
|
|
9
9
|
});
|
|
10
10
|
return e;
|
|
11
|
-
},
|
|
11
|
+
}, x = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : ae(v(e)), se(t || !e || !e.__esModule || !y.call(e, "default") ? g(n, "default", {
|
|
12
12
|
value: e,
|
|
13
13
|
enumerable: !0
|
|
14
|
-
}) : n, e)))((/* @__PURE__ */
|
|
14
|
+
}) : n, e)))((/* @__PURE__ */ b(((e, t) => {
|
|
15
15
|
var n = Object.prototype.hasOwnProperty, r = "~";
|
|
16
16
|
function i() {}
|
|
17
17
|
Object.create && (i.prototype = Object.create(null), new i().__proto__ || (r = !1));
|
|
@@ -98,7 +98,7 @@ var y = Object.create, b = Object.defineProperty, ne = Object.getOwnPropertyDesc
|
|
|
98
98
|
var t;
|
|
99
99
|
return e ? (t = r ? r + e : e, this._events[t] && s(this, t)) : (this._events = new i(), this._eventsCount = 0), this;
|
|
100
100
|
}, c.prototype.off = c.prototype.removeListener, c.prototype.addListener = c.prototype.on, c.prefixed = r, c.EventEmitter = c, t !== void 0 && (t.exports = c);
|
|
101
|
-
})))(), 1),
|
|
101
|
+
})))(), 1), S = class extends x.default {
|
|
102
102
|
world;
|
|
103
103
|
name;
|
|
104
104
|
currentDelta = 0;
|
|
@@ -174,13 +174,13 @@ var y = Object.create, b = Object.defineProperty, ne = Object.getOwnPropertyDesc
|
|
|
174
174
|
this.removeAllListeners();
|
|
175
175
|
}
|
|
176
176
|
};
|
|
177
|
-
function
|
|
177
|
+
function C(e, t, n) {
|
|
178
178
|
let r = e.groups.map(t);
|
|
179
179
|
for (let t = 0; t < e.entities.length; t++) r[e.groupIndexes[t]] && n(e.entities[t]);
|
|
180
180
|
}
|
|
181
181
|
//#endregion
|
|
182
182
|
//#region src/systems/iterable-system.ts
|
|
183
|
-
var
|
|
183
|
+
var w = class extends S {
|
|
184
184
|
remainingInstancesToRun = [];
|
|
185
185
|
remainingInstancesStartTime = null;
|
|
186
186
|
iterationsPerCheck;
|
|
@@ -230,7 +230,7 @@ var T = class extends C {
|
|
|
230
230
|
}
|
|
231
231
|
beforeRunIterables() {}
|
|
232
232
|
getIterableEntityId(e) {}
|
|
233
|
-
},
|
|
233
|
+
}, T = class e extends w {
|
|
234
234
|
entities = /* @__PURE__ */ new Map();
|
|
235
235
|
options;
|
|
236
236
|
constructor(e, t = { name: "EntitySystem" }) {
|
|
@@ -268,7 +268,7 @@ var T = class extends C {
|
|
|
268
268
|
t.entities.forEach((e) => this.onEntityAdded(e));
|
|
269
269
|
return;
|
|
270
270
|
}
|
|
271
|
-
|
|
271
|
+
C(t, (e) => this.hasComponents(e), (e) => {
|
|
272
272
|
this.filterEntity(e) && (this.entities.set(e.eid, e), this.options.updateEntityOnAdd && this.updateEntity(e, 0));
|
|
273
273
|
});
|
|
274
274
|
}
|
|
@@ -300,9 +300,9 @@ var T = class extends C {
|
|
|
300
300
|
...this.reads ?? []
|
|
301
301
|
] : void 0;
|
|
302
302
|
}
|
|
303
|
-
},
|
|
303
|
+
}, E = class {
|
|
304
304
|
onmessage(e, t = []) {}
|
|
305
|
-
},
|
|
305
|
+
}, D = class extends E {
|
|
306
306
|
updateFunction;
|
|
307
307
|
entities = new e();
|
|
308
308
|
workState = new a();
|
|
@@ -461,7 +461,7 @@ var T = class extends C {
|
|
|
461
461
|
onMessageTyped(e) {
|
|
462
462
|
this.onmessage({ data: e });
|
|
463
463
|
}
|
|
464
|
-
},
|
|
464
|
+
}, O = "___main", k = class e extends S {
|
|
465
465
|
entities = /* @__PURE__ */ new Map();
|
|
466
466
|
options;
|
|
467
467
|
worker;
|
|
@@ -494,13 +494,13 @@ var T = class extends C {
|
|
|
494
494
|
type: "grow-buffer",
|
|
495
495
|
buffer: e
|
|
496
496
|
});
|
|
497
|
-
})) : (this.worker = new
|
|
497
|
+
})) : (this.worker = new D(t.updateFunction, e, !!t.createsEntities, !!t.addsComponents), this.isWorkerThread = !1), this.initWorker();
|
|
498
498
|
}
|
|
499
499
|
initWorker() {
|
|
500
500
|
let e = (e) => {
|
|
501
501
|
console.error(e, e.cause), this.initialized || (this.initError = e, this.initPromise?.reject(e));
|
|
502
502
|
};
|
|
503
|
-
if (!(this.worker instanceof
|
|
503
|
+
if (!(this.worker instanceof D)) {
|
|
504
504
|
let t = (t) => {
|
|
505
505
|
e(Error(`Worker ${this.name} failed to load`, { cause: t }));
|
|
506
506
|
};
|
|
@@ -605,7 +605,7 @@ var T = class extends C {
|
|
|
605
605
|
type: "load",
|
|
606
606
|
data: this.options.getInitData?.(),
|
|
607
607
|
gameTime: this.gameTime,
|
|
608
|
-
entities: this.buildQueryDelta(
|
|
608
|
+
entities: this.buildQueryDelta(O, this.options),
|
|
609
609
|
queries: n,
|
|
610
610
|
heap: this.isWorkerThread ? this.world.heap.getSharedMemory() : void 0,
|
|
611
611
|
sharedMemory: this.isWorkerThread ? this.world.getSharedComponentMemory() : void 0,
|
|
@@ -705,7 +705,7 @@ var T = class extends C {
|
|
|
705
705
|
first: t.first,
|
|
706
706
|
last: t.last
|
|
707
707
|
}, this.isRunning = !0, this.completeEffects = this.world.scheduler.prepareEffects?.(this.world, this);
|
|
708
|
-
let i = this.buildQueryDelta(
|
|
708
|
+
let i = this.buildQueryDelta(O, this.options), a = {};
|
|
709
709
|
Object.entries(this.options.queries ?? {}).forEach(([e, t]) => {
|
|
710
710
|
a[e] = this.buildQueryDelta(e, t);
|
|
711
711
|
});
|
|
@@ -774,7 +774,7 @@ var T = class extends C {
|
|
|
774
774
|
}
|
|
775
775
|
checkAddEntity(e) {
|
|
776
776
|
let t = this.tracksMainQuery && this.matchesQuery(e, this.options);
|
|
777
|
-
return this.updateEntityList(
|
|
777
|
+
return this.updateEntityList(O, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
|
|
778
778
|
let r = this.queryEntities[t] ?? (this.queryEntities[t] = /* @__PURE__ */ new Map());
|
|
779
779
|
this.updateEntityList(t, r, e, this.matchesQuery(e, n));
|
|
780
780
|
}), t;
|
|
@@ -784,19 +784,19 @@ var T = class extends C {
|
|
|
784
784
|
t.entities.forEach((e) => this.checkAddEntity(e));
|
|
785
785
|
return;
|
|
786
786
|
}
|
|
787
|
-
this.tracksMainQuery && this.addBatchToQuery(
|
|
787
|
+
this.tracksMainQuery && this.addBatchToQuery(O, this.entities, this.options, t), Object.entries(this.options.queries ?? {}).forEach(([e, n]) => {
|
|
788
788
|
let r = this.queryEntities[e] ?? (this.queryEntities[e] = /* @__PURE__ */ new Map());
|
|
789
789
|
this.addBatchToQuery(e, r, n, t);
|
|
790
790
|
});
|
|
791
791
|
}
|
|
792
792
|
addBatchToQuery(e, t, n, r) {
|
|
793
793
|
let i = this.getQueryDelta(e);
|
|
794
|
-
|
|
794
|
+
C(r, (e) => this.matchesComponents(e, n), (e) => {
|
|
795
795
|
e.components.entity.dead || n.filter && !n.filter(e) || (t.set(e.eid, e), this.markAdded(i, e));
|
|
796
796
|
});
|
|
797
797
|
}
|
|
798
798
|
removeEntity(e) {
|
|
799
|
-
this.updateEntityList(
|
|
799
|
+
this.updateEntityList(O, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
|
|
800
800
|
this.updateEntityList(t, n, e, !1);
|
|
801
801
|
});
|
|
802
802
|
}
|
|
@@ -811,7 +811,7 @@ var T = class extends C {
|
|
|
811
811
|
destroy() {
|
|
812
812
|
super.destroy(), this.pendingEffects?.discard(), "terminate" in this.worker && this.worker.terminate();
|
|
813
813
|
}
|
|
814
|
-
},
|
|
814
|
+
}, A = class extends x.default {
|
|
815
815
|
eid;
|
|
816
816
|
config;
|
|
817
817
|
world;
|
|
@@ -912,7 +912,7 @@ var T = class extends C {
|
|
|
912
912
|
assertComponentPropertyMutable(e, t) {
|
|
913
913
|
if (e === "entity" && (t === "type" || t === "class")) throw Error(`Entity ${t} is immutable`);
|
|
914
914
|
}
|
|
915
|
-
},
|
|
915
|
+
}, j = class e {
|
|
916
916
|
world;
|
|
917
917
|
configs;
|
|
918
918
|
classes;
|
|
@@ -929,7 +929,7 @@ var T = class extends C {
|
|
|
929
929
|
this.world = e, this.forbiddenProperties.clear();
|
|
930
930
|
for (let t of Object.keys(this.classes)) {
|
|
931
931
|
let n = this.classes[t].entity;
|
|
932
|
-
if (n !==
|
|
932
|
+
if (n !== A && !(n.prototype instanceof A)) throw Error(`Entity class ${t} must extend BaseEntity`);
|
|
933
933
|
for (let n of this.classes[t].components) if (n === "entity" || !(n in e.registry)) throw Error(`Entity class ${t} contains unknown or reserved component ${n}`);
|
|
934
934
|
}
|
|
935
935
|
for (let e of Object.keys(this.configs)) this.validateTemplate(e, this.configs[e]);
|
|
@@ -955,7 +955,7 @@ var T = class extends C {
|
|
|
955
955
|
createAdoptedEntity(e, t, n) {
|
|
956
956
|
if (!t) {
|
|
957
957
|
if (this.hasClasses) throw Error(`Entity type ${e} has no class`);
|
|
958
|
-
return new
|
|
958
|
+
return new A(this.world, void 0, n);
|
|
959
959
|
}
|
|
960
960
|
let r = this.classes[t];
|
|
961
961
|
if (!r) throw Error(`Unknown entity class: ${t}`);
|
|
@@ -981,7 +981,7 @@ var T = class extends C {
|
|
|
981
981
|
return e ? this.getAllowedComponents(e) : void 0;
|
|
982
982
|
}
|
|
983
983
|
createEntity(e) {
|
|
984
|
-
return new
|
|
984
|
+
return new A(this.world, e);
|
|
985
985
|
}
|
|
986
986
|
get hasClasses() {
|
|
987
987
|
return this.allowedComponents.size > 0;
|
|
@@ -1046,48 +1046,48 @@ var T = class extends C {
|
|
|
1046
1046
|
}
|
|
1047
1047
|
throw Error(`Entity type ${e} class ${t} loads a component outside its class`);
|
|
1048
1048
|
}
|
|
1049
|
-
},
|
|
1050
|
-
for (let e = 0; e <
|
|
1051
|
-
function
|
|
1049
|
+
}, M = 45, N = "!", P = "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO", F = "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}", I = (/* @__PURE__ */ new Int8Array(128)).fill(-1);
|
|
1050
|
+
for (let e = 0; e < M; e++) I[P.charCodeAt(e)] = e, I[F.charCodeAt(e)] = e + M;
|
|
1051
|
+
function L(e) {
|
|
1052
1052
|
let t = "";
|
|
1053
|
-
for (; e >=
|
|
1054
|
-
return t +
|
|
1053
|
+
for (; e >= M;) t += F[e % M], e = Math.floor(e / M);
|
|
1054
|
+
return t + P[e];
|
|
1055
1055
|
}
|
|
1056
|
-
var
|
|
1057
|
-
function
|
|
1056
|
+
var R = /* @__PURE__ */ new Float32Array(1), z = new Uint32Array(R.buffer);
|
|
1057
|
+
function ce(e, t) {
|
|
1058
1058
|
switch (e.type) {
|
|
1059
|
-
case "boolean": return typeof t == "boolean" ?
|
|
1060
|
-
case "string": return t === null ?
|
|
1061
|
-
case "float32": return typeof t != "number" || !Number.isFinite(t) || Math.fround(t) !== t ? void 0 : (
|
|
1059
|
+
case "boolean": return typeof t == "boolean" ? P[Number(t)] : void 0;
|
|
1060
|
+
case "string": return t === null ? P[0] : typeof t == "string" && t.length ? L(t.length) + t : void 0;
|
|
1061
|
+
case "float32": return typeof t != "number" || !Number.isFinite(t) || Math.fround(t) !== t ? void 0 : (R[0] = t, L(z[0]));
|
|
1062
1062
|
default: {
|
|
1063
|
-
let n =
|
|
1064
|
-
return n === void 0 ? void 0 :
|
|
1063
|
+
let n = le(e, t);
|
|
1064
|
+
return n === void 0 ? void 0 : L(n);
|
|
1065
1065
|
}
|
|
1066
1066
|
}
|
|
1067
1067
|
}
|
|
1068
|
-
function
|
|
1068
|
+
function le(e, t) {
|
|
1069
1069
|
if (typeof t != "number") return;
|
|
1070
1070
|
let n = Math.round((t - (e.offset ?? 0)) * 10 ** (e.decimals ?? 0) / (e.step ?? 1));
|
|
1071
|
-
if (!Number.isSafeInteger(n) ||
|
|
1071
|
+
if (!Number.isSafeInteger(n) || B(e, n) !== t) return;
|
|
1072
1072
|
if (e.type === "uint") return n >= 0 ? n : void 0;
|
|
1073
1073
|
let r = n >= 0 ? n * 2 : -n * 2 - 1;
|
|
1074
1074
|
return Number.isSafeInteger(r) ? r : void 0;
|
|
1075
1075
|
}
|
|
1076
|
-
function
|
|
1076
|
+
function B(e, t) {
|
|
1077
1077
|
return t * (e.step ?? 1) / 10 ** (e.decimals ?? 0) + (e.offset ?? 0);
|
|
1078
1078
|
}
|
|
1079
|
-
function
|
|
1079
|
+
function V(e, t) {
|
|
1080
1080
|
switch (e.type) {
|
|
1081
1081
|
case "boolean": return t !== 0;
|
|
1082
|
-
case "float32": return
|
|
1083
|
-
case "uint": return
|
|
1084
|
-
default: return
|
|
1082
|
+
case "float32": return z[0] = t, R[0];
|
|
1083
|
+
case "uint": return B(e, t);
|
|
1084
|
+
default: return B(e, t % 2 == 0 ? t / 2 : -(t + 1) / 2);
|
|
1085
1085
|
}
|
|
1086
1086
|
}
|
|
1087
|
-
function
|
|
1087
|
+
function H(e) {
|
|
1088
1088
|
return e.types === void 0 ? [e.type] : e.types;
|
|
1089
1089
|
}
|
|
1090
|
-
var
|
|
1090
|
+
var U = class {
|
|
1091
1091
|
encoders = /* @__PURE__ */ new Map();
|
|
1092
1092
|
used = /* @__PURE__ */ new Set();
|
|
1093
1093
|
constructor(e) {
|
|
@@ -1098,10 +1098,10 @@ var G = class {
|
|
|
1098
1098
|
definition: n,
|
|
1099
1099
|
indices: new Map(n.properties.map((e, t) => [e.name, t]))
|
|
1100
1100
|
};
|
|
1101
|
-
for (let r of
|
|
1101
|
+
for (let r of H(n)) {
|
|
1102
1102
|
if (this.encoders.has(r)) throw Error(`Duplicate compressed entity definition for ${r}`);
|
|
1103
1103
|
this.encoders.set(r, {
|
|
1104
|
-
prefix:
|
|
1104
|
+
prefix: L(t++),
|
|
1105
1105
|
group: e
|
|
1106
1106
|
});
|
|
1107
1107
|
}
|
|
@@ -1129,25 +1129,25 @@ var G = class {
|
|
|
1129
1129
|
if (r === "type" || i === void 0) continue;
|
|
1130
1130
|
let t = e.group.indices.get(r);
|
|
1131
1131
|
if (t === void 0) return;
|
|
1132
|
-
let a =
|
|
1132
|
+
let a = ce(e.group.definition.properties[t], i);
|
|
1133
1133
|
if (a === void 0) return;
|
|
1134
1134
|
n[t] = a;
|
|
1135
1135
|
}
|
|
1136
1136
|
let r = e.prefix;
|
|
1137
|
-
for (let e = 0; e < n.length; e++) r += n[e] ??
|
|
1137
|
+
for (let e = 0; e < n.length; e++) r += n[e] ?? N;
|
|
1138
1138
|
return r;
|
|
1139
1139
|
}
|
|
1140
1140
|
};
|
|
1141
|
-
function
|
|
1142
|
-
return new
|
|
1141
|
+
function ue(e, t) {
|
|
1142
|
+
return new U(t).compressAll(e);
|
|
1143
1143
|
}
|
|
1144
|
-
var
|
|
1145
|
-
function
|
|
1146
|
-
let t =
|
|
1144
|
+
var W = /* @__PURE__ */ new WeakMap();
|
|
1145
|
+
function de(e) {
|
|
1146
|
+
let t = W.get(e);
|
|
1147
1147
|
if (t) return t;
|
|
1148
1148
|
t = /* @__PURE__ */ new Map();
|
|
1149
1149
|
for (let [n, r] of Object.entries(e)) {
|
|
1150
|
-
let e =
|
|
1150
|
+
let e = H(r);
|
|
1151
1151
|
for (let i = 0; i < e.length; i++) {
|
|
1152
1152
|
if (t.has(Number(n) + i)) throw Error(`Compressed entity definitions overlap at key ${Number(n) + i}`);
|
|
1153
1153
|
t.set(Number(n) + i, {
|
|
@@ -1156,23 +1156,23 @@ function le(e) {
|
|
|
1156
1156
|
});
|
|
1157
1157
|
}
|
|
1158
1158
|
}
|
|
1159
|
-
return
|
|
1159
|
+
return W.set(e, t), t;
|
|
1160
1160
|
}
|
|
1161
|
-
function
|
|
1161
|
+
function G(e, t) {
|
|
1162
1162
|
let n = 0, r = () => {
|
|
1163
|
-
if (e[n] ===
|
|
1163
|
+
if (e[n] === N) {
|
|
1164
1164
|
n++;
|
|
1165
1165
|
return;
|
|
1166
1166
|
}
|
|
1167
1167
|
let t = 0, r = 1;
|
|
1168
1168
|
for (; n < e.length;) {
|
|
1169
|
-
let i = e.charCodeAt(n++), a = i < 128 ?
|
|
1169
|
+
let i = e.charCodeAt(n++), a = i < 128 ? I[i] : -1;
|
|
1170
1170
|
if (a < 0) throw Error(`Invalid compressed entity ${e}`);
|
|
1171
|
-
if (a <
|
|
1172
|
-
t += (a -
|
|
1171
|
+
if (a < M) return t + a * r;
|
|
1172
|
+
t += (a - M) * r, r *= M;
|
|
1173
1173
|
}
|
|
1174
1174
|
throw Error(`Truncated compressed entity ${e}`);
|
|
1175
|
-
}, i = r(), a = i === void 0 ? void 0 :
|
|
1175
|
+
}, i = r(), a = i === void 0 ? void 0 : de(t).get(i);
|
|
1176
1176
|
if (!a) throw Error(`No compressed entity definition for ${e}`);
|
|
1177
1177
|
let o = { type: a.type };
|
|
1178
1178
|
for (let t = 0; n < e.length; t++) {
|
|
@@ -1180,7 +1180,7 @@ function q(e, t) {
|
|
|
1180
1180
|
if (!i) throw Error(`Compressed entity ${e} has more values than ${a.type} defines`);
|
|
1181
1181
|
let s = r();
|
|
1182
1182
|
if (s !== void 0) {
|
|
1183
|
-
if (i.type !== "string") o[i.name] =
|
|
1183
|
+
if (i.type !== "string") o[i.name] = V(i, s);
|
|
1184
1184
|
else if (s === 0) o[i.name] = null;
|
|
1185
1185
|
else {
|
|
1186
1186
|
if (n + s > e.length) throw Error(`Truncated compressed entity ${e}`);
|
|
@@ -1190,16 +1190,17 @@ function q(e, t) {
|
|
|
1190
1190
|
}
|
|
1191
1191
|
return o;
|
|
1192
1192
|
}
|
|
1193
|
-
function
|
|
1194
|
-
return e.map((e) =>
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1193
|
+
function fe(e, t) {
|
|
1194
|
+
return e.map((e) => K(e, t));
|
|
1195
|
+
}
|
|
1196
|
+
function K(e, t) {
|
|
1197
|
+
if (typeof e != "string") return e;
|
|
1198
|
+
if (!t) throw Error("Compressed entities need entityDefinitions to load");
|
|
1199
|
+
return G(e, t);
|
|
1199
1200
|
}
|
|
1200
1201
|
//#endregion
|
|
1201
1202
|
//#region src/scheduling/default-scheduler.ts
|
|
1202
|
-
var
|
|
1203
|
+
var q = class {
|
|
1203
1204
|
update(e, t) {
|
|
1204
1205
|
let n = null;
|
|
1205
1206
|
return e.systems.forEach((r) => {
|
|
@@ -1222,7 +1223,24 @@ var Y = class {
|
|
|
1222
1223
|
});
|
|
1223
1224
|
}), { lastSystemError: n };
|
|
1224
1225
|
}
|
|
1225
|
-
},
|
|
1226
|
+
}, pe = ie, J = 1e4, me = {
|
|
1227
|
+
entities: {
|
|
1228
|
+
start: 0,
|
|
1229
|
+
weight: .6
|
|
1230
|
+
},
|
|
1231
|
+
linking: {
|
|
1232
|
+
start: .6,
|
|
1233
|
+
weight: .1
|
|
1234
|
+
},
|
|
1235
|
+
systems: {
|
|
1236
|
+
start: .7,
|
|
1237
|
+
weight: .1
|
|
1238
|
+
},
|
|
1239
|
+
workers: {
|
|
1240
|
+
start: .8,
|
|
1241
|
+
weight: .2
|
|
1242
|
+
}
|
|
1243
|
+
}, Y = 256, he = class extends x.default {
|
|
1226
1244
|
heap;
|
|
1227
1245
|
constantStrings;
|
|
1228
1246
|
eidCounter;
|
|
@@ -1238,6 +1256,8 @@ var Y = class {
|
|
|
1238
1256
|
destroyed = !1;
|
|
1239
1257
|
pristine = !0;
|
|
1240
1258
|
isBulkLoading = !1;
|
|
1259
|
+
isLoading = !1;
|
|
1260
|
+
activeLoad;
|
|
1241
1261
|
activeFreeBuffer = {
|
|
1242
1262
|
frees: [],
|
|
1243
1263
|
waitingSystems: /* @__PURE__ */ new Set(),
|
|
@@ -1250,7 +1270,7 @@ var Y = class {
|
|
|
1250
1270
|
};
|
|
1251
1271
|
clearing = !1;
|
|
1252
1272
|
constructor(e, t = {}) {
|
|
1253
|
-
super(), this.scheduler = t.scheduler ?? new
|
|
1273
|
+
super(), this.scheduler = t.scheduler ?? new q(), this.heap = new re({ bufferSize: t.heapSize ?? pe }), this.constantStrings = new m(this.heap), this.eidCounter = this.heap.allocUI32(1), this.heap.addOnGrowBufferHandlers((e) => this.emit("grow-buffer", e));
|
|
1254
1274
|
let n = {
|
|
1255
1275
|
...e,
|
|
1256
1276
|
entity: i
|
|
@@ -1259,10 +1279,10 @@ var Y = class {
|
|
|
1259
1279
|
let t = n[e];
|
|
1260
1280
|
r[e] = {
|
|
1261
1281
|
...t,
|
|
1262
|
-
memoryComponent: new
|
|
1282
|
+
memoryComponent: new h(this.heap, t.type, t.size)
|
|
1263
1283
|
};
|
|
1264
1284
|
}
|
|
1265
|
-
this.registry = r, this.factory = t.factory ?? new
|
|
1285
|
+
this.registry = r, this.factory = t.factory ?? new j(), this.factory.setWorld(this);
|
|
1266
1286
|
}
|
|
1267
1287
|
allocateEid() {
|
|
1268
1288
|
return Atomics.add(this.eidCounter.data, 0, 1) + 1;
|
|
@@ -1279,14 +1299,11 @@ var Y = class {
|
|
|
1279
1299
|
this.heap.buffers[e.bufferPosition] === void 0 && this.heap.addSharedBuffer(e), this.emit("grow-buffer", e);
|
|
1280
1300
|
}
|
|
1281
1301
|
get needsSpareBuffer() {
|
|
1282
|
-
return this.systems.some((e) => e instanceof
|
|
1302
|
+
return this.systems.some((e) => e instanceof k && e.isWorkerThread && (!!e.options.createsEntities || !!e.options.addsComponents));
|
|
1283
1303
|
}
|
|
1284
1304
|
async init() {
|
|
1285
1305
|
await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
|
|
1286
1306
|
}
|
|
1287
|
-
async finishLoadingSystems() {
|
|
1288
|
-
await Promise.all(this.systems.map((e) => e.finishLoading()).filter((e) => e instanceof Promise));
|
|
1289
|
-
}
|
|
1290
1307
|
addEntity(e, t = !0) {
|
|
1291
1308
|
return this.entities.set(e.eid, e), e.world = this, this.pristine = !1, e.on("component-added", (t) => {
|
|
1292
1309
|
this.addEntityToEntityWorkerSystem(e, t);
|
|
@@ -1319,28 +1336,81 @@ var Y = class {
|
|
|
1319
1336
|
throw r;
|
|
1320
1337
|
}
|
|
1321
1338
|
}
|
|
1322
|
-
load(e) {
|
|
1323
|
-
if (!this.pristine || this.scheduler.prepareEffects && this.systems.some((e) => e.isCurrentlyRunning())) {
|
|
1339
|
+
load(e, t = {}) {
|
|
1340
|
+
if (this.abortActiveLoad(), !this.pristine || this.scheduler.prepareEffects && this.systems.some((e) => e.isCurrentlyRunning())) {
|
|
1324
1341
|
let e = this.systems.filter((e) => e.isCurrentlyRunning());
|
|
1325
1342
|
for (let e of Array.from(this.entities.values())) this.removeEntity(e);
|
|
1326
1343
|
this.systems.forEach((e) => e.clear()), this.scheduler.reset?.(this), this.consolidateFreeBuffersForReload(e);
|
|
1327
1344
|
}
|
|
1328
1345
|
for (let e of Object.keys(this.factory.configs)) this.constantStrings.getOrCreate(e);
|
|
1329
1346
|
for (let e of Object.keys(this.factory.classes)) this.constantStrings.getOrCreate(e);
|
|
1347
|
+
this.isLoading = !0;
|
|
1348
|
+
let n = this.loadSteps(e);
|
|
1349
|
+
if (t.sliceMs === void 0) {
|
|
1350
|
+
let e;
|
|
1351
|
+
try {
|
|
1352
|
+
for (; !(e = n.next()).done;) t.onProgress?.(e.value);
|
|
1353
|
+
} finally {
|
|
1354
|
+
this.isLoading = !1;
|
|
1355
|
+
}
|
|
1356
|
+
return t.onEntitiesReady?.(), this.awaitWorkers(e.value, t.onProgress, t.signal);
|
|
1357
|
+
}
|
|
1358
|
+
let r = new AbortController();
|
|
1359
|
+
this.activeLoad = r;
|
|
1360
|
+
let i = t.signal ? AbortSignal.any([r.signal, t.signal]) : r.signal;
|
|
1361
|
+
return this.loadSliced(n, t.sliceMs, r, i, t);
|
|
1362
|
+
}
|
|
1363
|
+
async loadSliced(e, t, n, r, i) {
|
|
1364
|
+
let a = i.yieldTo ?? Z;
|
|
1365
|
+
try {
|
|
1366
|
+
r.throwIfAborted();
|
|
1367
|
+
let n = performance.now(), o = e.next();
|
|
1368
|
+
for (; !o.done;) i.onProgress?.(o.value), performance.now() - n >= t && (await a(), r.throwIfAborted(), n = performance.now()), o = e.next();
|
|
1369
|
+
this.isLoading = !1, i.onEntitiesReady?.(), await this.awaitWorkers(o.value, i.onProgress, r);
|
|
1370
|
+
} catch (e) {
|
|
1371
|
+
throw this.activeLoad === n && (this.activeLoad = void 0, this.isBulkLoading = !1, this.isLoading = !1), e;
|
|
1372
|
+
}
|
|
1373
|
+
this.activeLoad === n && (this.activeLoad = void 0);
|
|
1374
|
+
}
|
|
1375
|
+
*loadSteps(e) {
|
|
1376
|
+
let t = "entityDefinitions" in e ? e.entityDefinitions : void 0, n = e.entities, r = [];
|
|
1330
1377
|
this.isBulkLoading = !0;
|
|
1331
1378
|
try {
|
|
1332
|
-
let
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1379
|
+
for (let e = 0; e < n.length; e++) r.push(this.loadEntity(K(n[e], t), !1)), (e + 1) % Y === 0 && (yield X("entities", e + 1, n.length));
|
|
1380
|
+
yield X("entities", n.length, n.length);
|
|
1381
|
+
for (let e = 0; e < r.length; e++) {
|
|
1382
|
+
let t = r[e];
|
|
1383
|
+
t.finishLoading(), this.emit("entity-added", t), t.on("death", () => {
|
|
1384
|
+
this.onEntityDied(t);
|
|
1385
|
+
}), (e + 1) % Y === 0 && (yield X("linking", e + 1, r.length));
|
|
1386
|
+
}
|
|
1387
|
+
yield X("linking", r.length, r.length);
|
|
1336
1388
|
} finally {
|
|
1337
1389
|
this.isBulkLoading = !1;
|
|
1338
1390
|
}
|
|
1339
|
-
let
|
|
1340
|
-
|
|
1391
|
+
let i = _e(this.entities.values()), a = this.systems.slice(), o = a.length * 2;
|
|
1392
|
+
for (let e = 0; e < a.length; e++) a[e].addEntities(i), yield X("systems", e + 1, o);
|
|
1393
|
+
this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1;
|
|
1394
|
+
let s = [];
|
|
1395
|
+
for (let e = 0; e < a.length; e++) {
|
|
1396
|
+
let t = a[e].finishLoading();
|
|
1397
|
+
t instanceof Promise && s.push(t), yield X("systems", a.length + e + 1, o);
|
|
1398
|
+
}
|
|
1399
|
+
return s;
|
|
1400
|
+
}
|
|
1401
|
+
async awaitWorkers(e, t, n) {
|
|
1402
|
+
let r = 0;
|
|
1403
|
+
t?.(X("workers", 0, e.length));
|
|
1404
|
+
let i = Promise.all(e.map(async (n) => {
|
|
1405
|
+
await n, t?.(X("workers", ++r, e.length));
|
|
1406
|
+
}));
|
|
1407
|
+
n ? await Promise.race([i, ge(n)]) : await i;
|
|
1408
|
+
}
|
|
1409
|
+
abortActiveLoad() {
|
|
1410
|
+
this.activeLoad && (this.activeLoad.abort(), this.activeLoad = void 0, this.isBulkLoading = !1, this.isLoading = !1);
|
|
1341
1411
|
}
|
|
1342
1412
|
async clear() {
|
|
1343
|
-
if (!this.pristine || this.scheduler.beforeClear) {
|
|
1413
|
+
if (this.abortActiveLoad(), !this.pristine || this.scheduler.beforeClear) {
|
|
1344
1414
|
this.clearing = !0;
|
|
1345
1415
|
try {
|
|
1346
1416
|
this.scheduler.beforeClear?.(this), await Promise.all(this.systems.map((e) => e.waitForRunToComplete()).filter((e) => e instanceof Promise));
|
|
@@ -1412,9 +1482,9 @@ var Y = class {
|
|
|
1412
1482
|
}
|
|
1413
1483
|
checkFreeBufferTimeout(e) {
|
|
1414
1484
|
let t = this.activeFreeBuffer;
|
|
1415
|
-
if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >=
|
|
1485
|
+
if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >= J)) {
|
|
1416
1486
|
let e = Array.from(t.waitingSystems).map((e) => e.name).join(", ");
|
|
1417
|
-
console.warn(`shared-memory-ecs: deferred component free waited over ${
|
|
1487
|
+
console.warn(`shared-memory-ecs: deferred component free waited over ${J}ms on system(s): ${e}; freeing anyway`), t.waitingSystems.clear(), this.processFreeBuffers();
|
|
1418
1488
|
}
|
|
1419
1489
|
}
|
|
1420
1490
|
addSystem(e) {
|
|
@@ -1431,6 +1501,7 @@ var Y = class {
|
|
|
1431
1501
|
}
|
|
1432
1502
|
}
|
|
1433
1503
|
update(e) {
|
|
1504
|
+
if (this.isLoading) return {};
|
|
1434
1505
|
this.emit("update-started", e);
|
|
1435
1506
|
let t = this.runUpdate(e);
|
|
1436
1507
|
return this.emit("update-finished", e), t;
|
|
@@ -1462,12 +1533,12 @@ var Y = class {
|
|
|
1462
1533
|
}
|
|
1463
1534
|
addEntityToEntityWorkerSystem(e, t) {
|
|
1464
1535
|
this.isBulkLoading || this.systems.forEach((n) => {
|
|
1465
|
-
(n instanceof
|
|
1536
|
+
(n instanceof T && n.options.components?.includes(t) || n instanceof k && this.queriesReferenceComponent(n.options, t)) && n.checkAddEntity(e);
|
|
1466
1537
|
});
|
|
1467
1538
|
}
|
|
1468
1539
|
removeEntityFromEntityWorkerSystem(e, t) {
|
|
1469
1540
|
this.isBulkLoading || this.systems.forEach((n) => {
|
|
1470
|
-
n instanceof
|
|
1541
|
+
n instanceof T && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof k && this.queriesReferenceComponent(n.options, t) && n.checkAddEntity(e);
|
|
1471
1542
|
});
|
|
1472
1543
|
}
|
|
1473
1544
|
queriesReferenceComponent(e, t) {
|
|
@@ -1475,12 +1546,30 @@ var Y = class {
|
|
|
1475
1546
|
return n || r;
|
|
1476
1547
|
}
|
|
1477
1548
|
destroy() {
|
|
1478
|
-
this.destroyed ||= (this.systems.forEach((e) => {
|
|
1549
|
+
this.destroyed ||= (this.abortActiveLoad(), this.systems.forEach((e) => {
|
|
1479
1550
|
e.destroy();
|
|
1480
1551
|
}), this.scheduler.destroy?.(this), !0);
|
|
1481
1552
|
}
|
|
1482
1553
|
};
|
|
1483
|
-
function
|
|
1554
|
+
function X(e, t, n) {
|
|
1555
|
+
let { start: r, weight: i } = me[e];
|
|
1556
|
+
return {
|
|
1557
|
+
phase: e,
|
|
1558
|
+
done: t,
|
|
1559
|
+
total: n,
|
|
1560
|
+
fraction: r + i * (n === 0 ? 1 : t / n)
|
|
1561
|
+
};
|
|
1562
|
+
}
|
|
1563
|
+
function Z() {
|
|
1564
|
+
let e = globalThis.scheduler;
|
|
1565
|
+
return e?.yield ? e.yield() : new Promise((e) => setTimeout(e, 0));
|
|
1566
|
+
}
|
|
1567
|
+
function ge(e) {
|
|
1568
|
+
return new Promise((t, n) => {
|
|
1569
|
+
e.aborted ? n(e.reason) : e.addEventListener("abort", () => n(e.reason), { once: !0 });
|
|
1570
|
+
});
|
|
1571
|
+
}
|
|
1572
|
+
function _e(e) {
|
|
1484
1573
|
let t = {
|
|
1485
1574
|
entities: [],
|
|
1486
1575
|
groupIndexes: [],
|
|
@@ -1494,7 +1583,7 @@ function fe(e) {
|
|
|
1494
1583
|
}
|
|
1495
1584
|
//#endregion
|
|
1496
1585
|
//#region src/scheduling/conflict-scheduler.ts
|
|
1497
|
-
var
|
|
1586
|
+
var ve = class {
|
|
1498
1587
|
compiled = Object.freeze({
|
|
1499
1588
|
order: Object.freeze([]),
|
|
1500
1589
|
batches: Object.freeze([])
|
|
@@ -1577,7 +1666,7 @@ var pe = class {
|
|
|
1577
1666
|
}, 0);
|
|
1578
1667
|
}
|
|
1579
1668
|
isSliced(e) {
|
|
1580
|
-
return e.system instanceof
|
|
1669
|
+
return e.system instanceof w && e.system.remainingInstancesToRun.length > 0;
|
|
1581
1670
|
}
|
|
1582
1671
|
reset() {
|
|
1583
1672
|
this.continuationTimer !== void 0 && (clearTimeout(this.continuationTimer), this.continuationTimer = void 0);
|
|
@@ -1590,10 +1679,14 @@ var pe = class {
|
|
|
1590
1679
|
prepareEffects(e, t) {
|
|
1591
1680
|
let n = this.running.get(t), r = this.generation;
|
|
1592
1681
|
return (e) => {
|
|
1593
|
-
if (
|
|
1682
|
+
if (r !== this.generation || n && !this.nodes.has(n)) {
|
|
1594
1683
|
e.discard();
|
|
1595
1684
|
return;
|
|
1596
1685
|
}
|
|
1686
|
+
if (!n) {
|
|
1687
|
+
e.apply();
|
|
1688
|
+
return;
|
|
1689
|
+
}
|
|
1597
1690
|
n.effects = e, this.publications.add(n), n.publicationDependencies === 0 && this.publicationReady.push(n), this.publish();
|
|
1598
1691
|
};
|
|
1599
1692
|
}
|
|
@@ -1745,20 +1838,20 @@ var pe = class {
|
|
|
1745
1838
|
};
|
|
1746
1839
|
//#endregion
|
|
1747
1840
|
//#region src/entity-class.ts
|
|
1748
|
-
function
|
|
1841
|
+
function ye(e, t) {
|
|
1749
1842
|
return t;
|
|
1750
1843
|
}
|
|
1751
|
-
function
|
|
1844
|
+
function be(e, t) {
|
|
1752
1845
|
return t;
|
|
1753
1846
|
}
|
|
1754
1847
|
//#endregion
|
|
1755
1848
|
//#region src/actions/kill-entity.ts
|
|
1756
|
-
function
|
|
1849
|
+
function xe(e) {
|
|
1757
1850
|
e.components.entity.dead = !0, e.emit("death");
|
|
1758
1851
|
}
|
|
1759
1852
|
//#endregion
|
|
1760
1853
|
//#region src/performance-timing.ts
|
|
1761
|
-
var
|
|
1854
|
+
var Se = 1e3, Q = {
|
|
1762
1855
|
avg: 0,
|
|
1763
1856
|
min: 0,
|
|
1764
1857
|
max: 0,
|
|
@@ -1775,7 +1868,7 @@ function $(e) {
|
|
|
1775
1868
|
samples: e.length
|
|
1776
1869
|
};
|
|
1777
1870
|
}
|
|
1778
|
-
var
|
|
1871
|
+
var Ce = class extends x.default {
|
|
1779
1872
|
world;
|
|
1780
1873
|
ticksBetweenUpdates;
|
|
1781
1874
|
stats = {
|
|
@@ -1860,7 +1953,7 @@ var _e = class extends S.default {
|
|
|
1860
1953
|
e.run = [], e.events = [];
|
|
1861
1954
|
}), this.emit("stats-updated", this.stats), this.stats;
|
|
1862
1955
|
}
|
|
1863
|
-
},
|
|
1956
|
+
}, we = class extends k {
|
|
1864
1957
|
constructor(e, t) {
|
|
1865
1958
|
super(e, {
|
|
1866
1959
|
...t,
|
|
@@ -1876,6 +1969,6 @@ var _e = class extends S.default {
|
|
|
1876
1969
|
}
|
|
1877
1970
|
};
|
|
1878
1971
|
//#endregion
|
|
1879
|
-
export {
|
|
1972
|
+
export { A as BaseEntity, he as BaseWorld, n as CLASS_INDEX, f as Component, ve as ConflictScheduler, m as ConstantStringCache, c as DEAD_INDEX, Se as DEFAULT_TICKS_BETWEEN_UPDATES, q as DefaultScheduler, U as EntityCompressor, j as EntityFactory, T as EntitySystem, D as EntitySystemWebWorker, k as EntityWorkerSystem, w as IterableSystem, h as MemoryComponent, Ce as PerformanceTiming, ne as STATIC_INDEX, S as System, r as TYPE_INDEX, E as WebWorker, we as WorkerSystem, t as addComponentWorker, ue as compressEntities, p as createEntitySystemWorker, d as createEntityWorker, te as createSystemWorker, fe as decompressEntities, G as decompressEntity, ye as defineEntityClasses, be as defineEntityConfigs, i as entityDefinition, xe as killEntity, ee as killEntityWorker, s as removeComponentWorker };
|
|
1880
1973
|
|
|
1881
1974
|
//# sourceMappingURL=index.js.map
|