@daneren2005/shared-memory-ecs 1.0.1 → 1.1.1
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 +61 -13
- package/dist/actions/create-entity-worker.d.ts +2 -0
- package/dist/actions/kill-entity-worker.d.ts +2 -0
- package/dist/actions/kill-entity.d.ts +2 -0
- package/dist/component-definition.d.ts +38 -0
- package/dist/entity-component.d.ts +18 -0
- package/dist/entity-factory.d.ts +16 -0
- package/dist/entity.d.ts +26 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +443 -184
- package/dist/index.js.map +1 -1
- package/dist/memory-component.d.ts +15 -0
- package/dist/systems/component-system.d.ts +85 -0
- package/dist/systems/entity-system.d.ts +21 -0
- package/dist/systems/iterable-system.d.ts +21 -0
- package/dist/systems/system.d.ts +22 -0
- package/dist/systems/workers/apply-query-delta.d.ts +2 -0
- package/dist/systems/workers/component-web-worker.d.ts +12 -0
- package/dist/systems/workers/component-worker-message.d.ts +27 -0
- package/dist/systems/workers/create-component-worker.d.ts +8 -0
- package/dist/systems/workers/web-worker.d.ts +4 -0
- package/dist/world.d.ts +54 -0
- package/package.json +7 -4
package/dist/index.js
CHANGED
|
@@ -1,11 +1,106 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var
|
|
1
|
+
import { LocalPool as e, MAX_BYTE_OFFSET_LENGTH as t, MemoryHeap as n } from "@daneren2005/shared-memory-objects";
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescriptor, o = Object.getOwnPropertyNames, s = Object.getPrototypeOf, c = Object.prototype.hasOwnProperty, l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), u = (e, t, n, r) => {
|
|
4
|
+
if (t && typeof t == "object" || typeof t == "function") for (var s = o(t), l = 0, u = s.length, d; l < u; l++) d = s[l], !c.call(e, d) && d !== n && i(e, d, {
|
|
5
|
+
get: ((e) => t[e]).bind(null, d),
|
|
6
|
+
enumerable: !(r = a(t, d)) || r.enumerable
|
|
7
|
+
});
|
|
8
|
+
return e;
|
|
9
|
+
}, d = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : r(s(e)), u(t || !e || !e.__esModule ? i(n, "default", {
|
|
10
|
+
value: e,
|
|
11
|
+
enumerable: !0
|
|
12
|
+
}) : n, e)))((/* @__PURE__ */ l(((e, t) => {
|
|
13
|
+
var n = Object.prototype.hasOwnProperty, r = "~";
|
|
14
|
+
function i() {}
|
|
15
|
+
Object.create && (i.prototype = Object.create(null), new i().__proto__ || (r = !1));
|
|
16
|
+
function a(e, t, n) {
|
|
17
|
+
this.fn = e, this.context = t, this.once = n || !1;
|
|
18
|
+
}
|
|
19
|
+
function o(e, t, n, i, o) {
|
|
20
|
+
if (typeof n != "function") throw TypeError("The listener must be a function");
|
|
21
|
+
var s = new a(n, i || e, o), c = r ? r + t : t;
|
|
22
|
+
return e._events[c] ? e._events[c].fn ? e._events[c] = [e._events[c], s] : e._events[c].push(s) : (e._events[c] = s, e._eventsCount++), e;
|
|
23
|
+
}
|
|
24
|
+
function s(e, t) {
|
|
25
|
+
--e._eventsCount === 0 ? e._events = new i() : delete e._events[t];
|
|
26
|
+
}
|
|
27
|
+
function c() {
|
|
28
|
+
this._events = new i(), this._eventsCount = 0;
|
|
29
|
+
}
|
|
30
|
+
c.prototype.eventNames = function() {
|
|
31
|
+
var e = [], t, i;
|
|
32
|
+
if (this._eventsCount === 0) return e;
|
|
33
|
+
for (i in t = this._events) n.call(t, i) && e.push(r ? i.slice(1) : i);
|
|
34
|
+
return Object.getOwnPropertySymbols ? e.concat(Object.getOwnPropertySymbols(t)) : e;
|
|
35
|
+
}, c.prototype.listeners = function(e) {
|
|
36
|
+
var t = r ? r + e : e, n = this._events[t];
|
|
37
|
+
if (!n) return [];
|
|
38
|
+
if (n.fn) return [n.fn];
|
|
39
|
+
for (var i = 0, a = n.length, o = Array(a); i < a; i++) o[i] = n[i].fn;
|
|
40
|
+
return o;
|
|
41
|
+
}, c.prototype.listenerCount = function(e) {
|
|
42
|
+
var t = r ? r + e : e, n = this._events[t];
|
|
43
|
+
return n ? n.fn ? 1 : n.length : 0;
|
|
44
|
+
}, c.prototype.emit = function(e, t, n, i, a, o) {
|
|
45
|
+
var s = r ? r + e : e;
|
|
46
|
+
if (!this._events[s]) return !1;
|
|
47
|
+
var c = this._events[s], l = arguments.length, u, d;
|
|
48
|
+
if (c.fn) {
|
|
49
|
+
switch (c.once && this.removeListener(e, c.fn, void 0, !0), l) {
|
|
50
|
+
case 1: return c.fn.call(c.context), !0;
|
|
51
|
+
case 2: return c.fn.call(c.context, t), !0;
|
|
52
|
+
case 3: return c.fn.call(c.context, t, n), !0;
|
|
53
|
+
case 4: return c.fn.call(c.context, t, n, i), !0;
|
|
54
|
+
case 5: return c.fn.call(c.context, t, n, i, a), !0;
|
|
55
|
+
case 6: return c.fn.call(c.context, t, n, i, a, o), !0;
|
|
56
|
+
}
|
|
57
|
+
for (d = 1, u = Array(l - 1); d < l; d++) u[d - 1] = arguments[d];
|
|
58
|
+
c.fn.apply(c.context, u);
|
|
59
|
+
} else {
|
|
60
|
+
var f = c.length, p;
|
|
61
|
+
for (d = 0; d < f; d++) switch (c[d].once && this.removeListener(e, c[d].fn, void 0, !0), l) {
|
|
62
|
+
case 1:
|
|
63
|
+
c[d].fn.call(c[d].context);
|
|
64
|
+
break;
|
|
65
|
+
case 2:
|
|
66
|
+
c[d].fn.call(c[d].context, t);
|
|
67
|
+
break;
|
|
68
|
+
case 3:
|
|
69
|
+
c[d].fn.call(c[d].context, t, n);
|
|
70
|
+
break;
|
|
71
|
+
case 4:
|
|
72
|
+
c[d].fn.call(c[d].context, t, n, i);
|
|
73
|
+
break;
|
|
74
|
+
default:
|
|
75
|
+
if (!u) for (p = 1, u = Array(l - 1); p < l; p++) u[p - 1] = arguments[p];
|
|
76
|
+
c[d].fn.apply(c[d].context, u);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return !0;
|
|
80
|
+
}, c.prototype.on = function(e, t, n) {
|
|
81
|
+
return o(this, e, t, n, !1);
|
|
82
|
+
}, c.prototype.once = function(e, t, n) {
|
|
83
|
+
return o(this, e, t, n, !0);
|
|
84
|
+
}, c.prototype.removeListener = function(e, t, n, i) {
|
|
85
|
+
var a = r ? r + e : e;
|
|
86
|
+
if (!this._events[a]) return this;
|
|
87
|
+
if (!t) return s(this, a), this;
|
|
88
|
+
var o = this._events[a];
|
|
89
|
+
if (o.fn) o.fn === t && (!i || o.once) && (!n || o.context === n) && s(this, a);
|
|
90
|
+
else {
|
|
91
|
+
for (var c = 0, l = [], u = o.length; c < u; c++) (o[c].fn !== t || i && !o[c].once || n && o[c].context !== n) && l.push(o[c]);
|
|
92
|
+
l.length ? this._events[a] = l.length === 1 ? l[0] : l : s(this, a);
|
|
93
|
+
}
|
|
94
|
+
return this;
|
|
95
|
+
}, c.prototype.removeAllListeners = function(e) {
|
|
96
|
+
var t;
|
|
97
|
+
return e ? (t = r ? r + e : e, this._events[t] && s(this, t)) : (this._events = new i(), this._eventsCount = 0), this;
|
|
98
|
+
}, c.prototype.off = c.prototype.removeListener, c.prototype.addListener = c.prototype.on, c.prefixed = r, c.EventEmitter = c, t !== void 0 && (t.exports = c);
|
|
99
|
+
})))(), 1), f = class {
|
|
5
100
|
heap;
|
|
6
101
|
pool;
|
|
7
|
-
constructor(
|
|
8
|
-
this.heap =
|
|
102
|
+
constructor(t, n, r) {
|
|
103
|
+
this.heap = t, this.pool = new e(t, {
|
|
9
104
|
type: n,
|
|
10
105
|
dataLength: r
|
|
11
106
|
});
|
|
@@ -35,58 +130,7 @@ var i = class {
|
|
|
35
130
|
clear() {
|
|
36
131
|
this.pool.clear();
|
|
37
132
|
}
|
|
38
|
-
},
|
|
39
|
-
static eidCounter = 1;
|
|
40
|
-
eid;
|
|
41
|
-
id;
|
|
42
|
-
config;
|
|
43
|
-
world;
|
|
44
|
-
components = {};
|
|
45
|
-
dead = !1;
|
|
46
|
-
isStatic = !1;
|
|
47
|
-
constructor(e, n) {
|
|
48
|
-
super(), this.world = e, this.eid = t.eidCounter++, n && this.load(n);
|
|
49
|
-
}
|
|
50
|
-
loadComponent(e, t) {
|
|
51
|
-
let n = this.world.registry[e], r = this.world.components[e], i = n.load(this, r, t);
|
|
52
|
-
return this.components[e] = i, this.emit("component-added", e, i), i;
|
|
53
|
-
}
|
|
54
|
-
removeComponent(e) {
|
|
55
|
-
let t = this.components[e];
|
|
56
|
-
t && (this.world.components[e].delete(t.index), delete this.components[e], this.emit("component-removed", e));
|
|
57
|
-
}
|
|
58
|
-
setComponent(e, t, n) {
|
|
59
|
-
let r = this.components[e];
|
|
60
|
-
r && (r[t] = n, this.emit("component-property-updated", e, t, n));
|
|
61
|
-
}
|
|
62
|
-
setComponentBulk(e, t) {
|
|
63
|
-
let n = this.components[e];
|
|
64
|
-
n && Object.assign(n, t);
|
|
65
|
-
}
|
|
66
|
-
deleteComponent(e, t) {
|
|
67
|
-
let n = this.components[e];
|
|
68
|
-
n && (delete n[t], this.emit("component-property-deleted", e, t));
|
|
69
|
-
}
|
|
70
|
-
deleteAllComponentMemory() {
|
|
71
|
-
for (let e of Object.keys(this.components)) {
|
|
72
|
-
let t = this.components[e];
|
|
73
|
-
t && this.world.components[e].delete(t.index);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
load(e) {
|
|
77
|
-
for (let t in e) t in this.world.registry && this.loadComponent(t, e[t]);
|
|
78
|
-
this.config = e;
|
|
79
|
-
}
|
|
80
|
-
save() {
|
|
81
|
-
let e = {};
|
|
82
|
-
for (let t of Object.keys(this.components)) {
|
|
83
|
-
let n = this.world.registry[t], r = this.components[t];
|
|
84
|
-
n.save && r && (e[t] = n.save(r));
|
|
85
|
-
}
|
|
86
|
-
return e;
|
|
87
|
-
}
|
|
88
|
-
finishLoading() {}
|
|
89
|
-
}, o = class {
|
|
133
|
+
}, p = class {
|
|
90
134
|
world;
|
|
91
135
|
name;
|
|
92
136
|
currentDelta = 0;
|
|
@@ -110,7 +154,7 @@ var i = class {
|
|
|
110
154
|
return !0;
|
|
111
155
|
}
|
|
112
156
|
destroy() {}
|
|
113
|
-
},
|
|
157
|
+
}, m = class extends p {
|
|
114
158
|
remainingInstancesToRun = [];
|
|
115
159
|
remainingInstancesStartTime = null;
|
|
116
160
|
iterationsPerCheck;
|
|
@@ -138,7 +182,7 @@ var i = class {
|
|
|
138
182
|
this.remainingInstancesToRun = [], this.remainingInstancesStartTime = null;
|
|
139
183
|
}
|
|
140
184
|
beforeRunIterables() {}
|
|
141
|
-
},
|
|
185
|
+
}, h = class extends m {
|
|
142
186
|
entities = [];
|
|
143
187
|
options;
|
|
144
188
|
constructor(e, t = { name: "EntitySystem" }) {
|
|
@@ -151,13 +195,13 @@ var i = class {
|
|
|
151
195
|
});
|
|
152
196
|
}
|
|
153
197
|
getIterables() {
|
|
154
|
-
return this.entities.filter((e) => !e.dead);
|
|
198
|
+
return this.entities.filter((e) => !e.components.entity.dead);
|
|
155
199
|
}
|
|
156
200
|
updateIterable(e, t) {
|
|
157
|
-
e.dead || this.updateEntity(e, t);
|
|
201
|
+
e.components.entity.dead || this.updateEntity(e, t);
|
|
158
202
|
}
|
|
159
203
|
filterEntity(e) {
|
|
160
|
-
return !e.isStatic;
|
|
204
|
+
return !e.components.entity.isStatic;
|
|
161
205
|
}
|
|
162
206
|
isEntityInSystem(e) {
|
|
163
207
|
return this.entities.indexOf(e) !== -1;
|
|
@@ -172,41 +216,83 @@ var i = class {
|
|
|
172
216
|
shouldRun() {
|
|
173
217
|
return this.entities.length > 0;
|
|
174
218
|
}
|
|
175
|
-
},
|
|
219
|
+
}, g = class {
|
|
176
220
|
onmessage(e, t = []) {}
|
|
177
|
-
}
|
|
221
|
+
};
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/systems/workers/apply-query-delta.ts
|
|
224
|
+
function _(e, t) {
|
|
225
|
+
if (t.removed.length) {
|
|
226
|
+
let n = new Set(t.removed);
|
|
227
|
+
e = e.filter((e) => !n.has(e.entityId));
|
|
228
|
+
}
|
|
229
|
+
if (t.added.length) {
|
|
230
|
+
let n = /* @__PURE__ */ new Map();
|
|
231
|
+
e.forEach((e, t) => n.set(e.entityId, t));
|
|
232
|
+
for (let r of t.added) {
|
|
233
|
+
let t = n.get(r.entityId);
|
|
234
|
+
t === void 0 ? (n.set(r.entityId, e.length), e.push(r)) : e[t] = r;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return e;
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/systems/workers/component-web-worker.ts
|
|
241
|
+
var v = class extends g {
|
|
178
242
|
updateFunction;
|
|
243
|
+
entities = [];
|
|
244
|
+
queryEntities = {};
|
|
179
245
|
constructor(e) {
|
|
180
246
|
super(), this.updateFunction = e;
|
|
181
247
|
}
|
|
182
248
|
postMessage(e) {
|
|
183
249
|
if (e.type === "init") this.onMessageTyped({ type: "loaded" });
|
|
184
250
|
else if (e.type === "run") {
|
|
185
|
-
let t = [], n =
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
251
|
+
let t = [], n = [];
|
|
252
|
+
this.entities = _(this.entities, e.entities);
|
|
253
|
+
let r = {};
|
|
254
|
+
Object.entries(e.queries).forEach(([e, t]) => {
|
|
255
|
+
let n = _(this.queryEntities[e] ?? [], t);
|
|
256
|
+
this.queryEntities[e] = n, r[e] = n;
|
|
257
|
+
});
|
|
258
|
+
let i = {
|
|
259
|
+
entityComponentChanged(e, n, r, i) {
|
|
260
|
+
t.push({
|
|
261
|
+
entityId: e,
|
|
262
|
+
event: "component-property-updated",
|
|
263
|
+
args: [
|
|
264
|
+
n,
|
|
265
|
+
r,
|
|
266
|
+
i
|
|
267
|
+
]
|
|
268
|
+
});
|
|
269
|
+
},
|
|
270
|
+
entityDied(e) {
|
|
271
|
+
t.push({
|
|
272
|
+
entityId: e,
|
|
273
|
+
event: "death",
|
|
274
|
+
args: []
|
|
275
|
+
});
|
|
276
|
+
},
|
|
277
|
+
createEntity(e) {
|
|
278
|
+
n.push(e);
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
if (this.updateFunction.preRun && this.updateFunction.preRun(e.world, this.entities, r, i), this.entities.forEach((t) => {
|
|
282
|
+
this.updateFunction(e.world, t.entityId, t.components, r, i);
|
|
283
|
+
}), this.updateFunction.entityRemoved) for (let t of e.entities.removed) this.updateFunction.entityRemoved(e.world, t, i);
|
|
199
284
|
this.onMessageTyped({
|
|
200
285
|
type: "run-complete",
|
|
201
286
|
runTime: 0,
|
|
202
|
-
events: t
|
|
287
|
+
events: t,
|
|
288
|
+
created: n
|
|
203
289
|
});
|
|
204
290
|
}
|
|
205
291
|
}
|
|
206
292
|
onMessageTyped(e) {
|
|
207
293
|
this.onmessage({ data: e });
|
|
208
294
|
}
|
|
209
|
-
},
|
|
295
|
+
}, y = "___main", b = class extends p {
|
|
210
296
|
entities = [];
|
|
211
297
|
options;
|
|
212
298
|
worker;
|
|
@@ -214,9 +300,8 @@ var i = class {
|
|
|
214
300
|
loaded = !1;
|
|
215
301
|
loadingPromise = null;
|
|
216
302
|
isRunning = !1;
|
|
217
|
-
queryEntityComponentCache = {};
|
|
218
303
|
queryEntities = {};
|
|
219
|
-
|
|
304
|
+
queryDeltas = {};
|
|
220
305
|
constructor(e, t) {
|
|
221
306
|
super(e, t), this.options = t, Object.keys(this.options.queries ?? {}).forEach((e) => {
|
|
222
307
|
this.queryEntities[e] = [];
|
|
@@ -226,19 +311,21 @@ var i = class {
|
|
|
226
311
|
this.removeEntity(e);
|
|
227
312
|
}), e.entities.forEach((e) => {
|
|
228
313
|
this.checkAddEntity(e);
|
|
229
|
-
}), !t.forceMainThread && globalThis.Worker !== void 0 && globalThis.SharedArrayBuffer !== void 0 ? (this.worker = t.getWorker(), this.isWorkerThread = !0) : (this.worker = new
|
|
314
|
+
}), !t.forceMainThread && globalThis.Worker !== void 0 && globalThis.SharedArrayBuffer !== void 0 ? (this.worker = t.getWorker(), this.isWorkerThread = !0) : (this.worker = new v(t.updateFunction), this.isWorkerThread = !1), this.initWorker();
|
|
230
315
|
}
|
|
231
316
|
initWorker() {
|
|
232
317
|
this.worker.onmessage = (e) => {
|
|
233
318
|
let t = e.data;
|
|
234
319
|
t.type === "loaded" ? (this.loaded = !0, this.loadingPromise &&= (this.loadingPromise.resolve(), null)) : t.type === "run-complete" && (this.isRunning = !1, this.isWorkerThread && this.world.emit(`system-${this.name}-worker-finished`, t.runTime), t.events.forEach((e) => {
|
|
235
|
-
let t = this.
|
|
320
|
+
let t = this.world.getEntityByEid(e.entityId);
|
|
236
321
|
if (!t) {
|
|
237
322
|
console.warn(`Could not find entity with id ${e.entityId}`);
|
|
238
323
|
return;
|
|
239
324
|
}
|
|
240
325
|
t.emit(e.event, ...e.args);
|
|
241
|
-
}))
|
|
326
|
+
}), t.created.forEach((e) => {
|
|
327
|
+
this.world.loadEntity(e);
|
|
328
|
+
}), this.isWorkerThread && this.world.emit(`system-${this.name}-worker-events-finished`, t.runTime));
|
|
242
329
|
}, this.worker.postMessage({ type: "init" });
|
|
243
330
|
}
|
|
244
331
|
init() {
|
|
@@ -259,64 +346,78 @@ var i = class {
|
|
|
259
346
|
elapsedTime: e
|
|
260
347
|
};
|
|
261
348
|
this.addDataToWorld?.(t), this.isRunning = !0;
|
|
262
|
-
let n = this.
|
|
349
|
+
let n = this.buildQueryDelta(y, this.options), r = {};
|
|
263
350
|
Object.entries(this.options.queries ?? {}).forEach(([e, t]) => {
|
|
264
|
-
r[e] = this.
|
|
351
|
+
r[e] = this.buildQueryDelta(e, t);
|
|
265
352
|
});
|
|
266
353
|
let i = {
|
|
267
354
|
type: "run",
|
|
268
355
|
world: t,
|
|
269
356
|
entities: n,
|
|
270
|
-
queries: r
|
|
271
|
-
|
|
357
|
+
queries: r
|
|
358
|
+
};
|
|
359
|
+
this.worker.postMessage(i);
|
|
360
|
+
}
|
|
361
|
+
buildQueryDelta(e, t) {
|
|
362
|
+
let n = this.getQueryDelta(e), r = n.added.map((e) => ({
|
|
363
|
+
entityId: e.eid,
|
|
364
|
+
components: this.buildComponents(e, t)
|
|
365
|
+
})), i = n.removed;
|
|
366
|
+
return this.queryDeltas[e] = {
|
|
367
|
+
added: [],
|
|
368
|
+
removed: []
|
|
369
|
+
}, {
|
|
370
|
+
added: r,
|
|
371
|
+
removed: i
|
|
272
372
|
};
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
t ? this.isWorkerThread ? r.push(e.eid) : r.push({
|
|
281
|
-
entityId: e.eid,
|
|
282
|
-
components: t
|
|
283
|
-
}) : (t = {}, [...n.required, ...n.optional ?? []].forEach((n) => {
|
|
284
|
-
let r = e.components[n], i = this.world.components[n];
|
|
285
|
-
r && i && (t[n] = i.getBlock(r.index));
|
|
286
|
-
}), i[e.eid] = {
|
|
287
|
-
entity: e,
|
|
288
|
-
components: t
|
|
289
|
-
}, r.push({
|
|
290
|
-
entityId: e.eid,
|
|
291
|
-
components: t
|
|
292
|
-
}));
|
|
293
|
-
}), r;
|
|
373
|
+
}
|
|
374
|
+
buildComponents(e, t) {
|
|
375
|
+
let n = {}, r = this.world.registry;
|
|
376
|
+
return [...t.required, ...t.optional ?? []].forEach((t) => {
|
|
377
|
+
let i = e.components[t], a = r[t].memoryComponent;
|
|
378
|
+
i && a && (n[t] = a.getBlock(i.index));
|
|
379
|
+
}), n;
|
|
294
380
|
}
|
|
295
381
|
isEntityInSystem(e) {
|
|
296
382
|
return this.entities.indexOf(e) !== -1;
|
|
297
383
|
}
|
|
298
384
|
matchesQuery(e, t) {
|
|
299
|
-
return !(e.dead || t.required.find((t) => !e.components[t]) || t.not?.find((t) => !!e.components[t]) || t.filter && !t.filter(e));
|
|
385
|
+
return !(e.components.entity.dead || t.required.find((t) => !e.components[t]) || t.not?.find((t) => !!e.components[t]) || t.filter && !t.filter(e));
|
|
386
|
+
}
|
|
387
|
+
getQueryDelta(e) {
|
|
388
|
+
let t = this.queryDeltas[e];
|
|
389
|
+
return t ||= this.queryDeltas[e] = {
|
|
390
|
+
added: [],
|
|
391
|
+
removed: []
|
|
392
|
+
}, t;
|
|
393
|
+
}
|
|
394
|
+
markAdded(e, t) {
|
|
395
|
+
let n = e.removed.indexOf(t.eid);
|
|
396
|
+
n !== -1 && e.removed.splice(n, 1), e.added.indexOf(t) === -1 && e.added.push(t);
|
|
397
|
+
}
|
|
398
|
+
markRemoved(e, t) {
|
|
399
|
+
let n = e.added.indexOf(t);
|
|
400
|
+
if (n !== -1) {
|
|
401
|
+
e.added.splice(n, 1);
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
e.removed.indexOf(t.eid) === -1 && e.removed.push(t.eid);
|
|
300
405
|
}
|
|
301
|
-
updateEntityList(e, t, n) {
|
|
302
|
-
let
|
|
303
|
-
|
|
406
|
+
updateEntityList(e, t, n, r) {
|
|
407
|
+
let i = t.indexOf(n), a = this.getQueryDelta(e);
|
|
408
|
+
r ? (i === -1 && t.push(n), this.markAdded(a, n)) : i !== -1 && (t.splice(i, 1), this.markRemoved(a, n));
|
|
304
409
|
}
|
|
305
410
|
checkAddEntity(e) {
|
|
306
411
|
let t = this.matchesQuery(e, this.options);
|
|
307
|
-
return this.updateEntityList(this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
|
|
412
|
+
return this.updateEntityList(y, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
|
|
308
413
|
let r = this.queryEntities[t] ?? (this.queryEntities[t] = []);
|
|
309
|
-
this.updateEntityList(r, e, this.matchesQuery(e, n));
|
|
310
|
-
}), Object.values(this.queryEntityComponentCache).forEach((t) => {
|
|
311
|
-
t[e.eid] && delete t[e.eid];
|
|
414
|
+
this.updateEntityList(t, r, e, this.matchesQuery(e, n));
|
|
312
415
|
}), t;
|
|
313
416
|
}
|
|
314
417
|
removeEntity(e) {
|
|
315
|
-
this.updateEntityList(this.entities, e, !1), Object.
|
|
316
|
-
this.updateEntityList(t, e, !1);
|
|
317
|
-
})
|
|
318
|
-
t[e.eid] && delete t[e.eid];
|
|
319
|
-
}), this.removedEntityBuffer.push(e.eid);
|
|
418
|
+
this.updateEntityList(y, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
|
|
419
|
+
this.updateEntityList(t, n, e, !1);
|
|
420
|
+
});
|
|
320
421
|
}
|
|
321
422
|
shouldRun() {
|
|
322
423
|
return this.entities.length > 0;
|
|
@@ -324,23 +425,145 @@ var i = class {
|
|
|
324
425
|
destroy() {
|
|
325
426
|
"terminate" in this.worker && this.worker.terminate();
|
|
326
427
|
}
|
|
327
|
-
},
|
|
428
|
+
}, x = 0, S = 1, C = {
|
|
429
|
+
type: Uint32Array,
|
|
430
|
+
size: 2,
|
|
431
|
+
loadProperties: ["type", "isStatic"],
|
|
432
|
+
load(e, t, n) {
|
|
433
|
+
let r = t.create([+!!n.dead, +!!n.isStatic]), i = t.getBlock(r);
|
|
434
|
+
return {
|
|
435
|
+
index: r,
|
|
436
|
+
type: n.type,
|
|
437
|
+
get dead() {
|
|
438
|
+
return i[0] === 1;
|
|
439
|
+
},
|
|
440
|
+
set dead(e) {
|
|
441
|
+
i[0] = +!!e;
|
|
442
|
+
},
|
|
443
|
+
get isStatic() {
|
|
444
|
+
return i[1] === 1;
|
|
445
|
+
},
|
|
446
|
+
set isStatic(e) {
|
|
447
|
+
i[1] = +!!e;
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
},
|
|
451
|
+
save(e) {
|
|
452
|
+
let t = {};
|
|
453
|
+
return e.type && (t.type = e.type), e.dead && (t.dead = !0), t;
|
|
454
|
+
}
|
|
455
|
+
}, w = class e extends d.default {
|
|
456
|
+
static eidCounter = 1;
|
|
457
|
+
eid;
|
|
458
|
+
config;
|
|
459
|
+
world;
|
|
460
|
+
components = {};
|
|
461
|
+
constructor(t, n) {
|
|
462
|
+
super(), this.world = t, this.eid = e.eidCounter++, this.loadComponent("entity", n ?? {}, !1), n && this.load(n);
|
|
463
|
+
}
|
|
464
|
+
loadComponent(e, t, n = !0) {
|
|
465
|
+
let r = this.world.registry[e], i = r.memoryComponent, a = r.load(this, i, t);
|
|
466
|
+
return this.components[e] = a, n && this.emit("component-added", e, a), a;
|
|
467
|
+
}
|
|
468
|
+
removeComponent(e) {
|
|
469
|
+
let t = this.components[e];
|
|
470
|
+
t && (this.world.registry[e].memoryComponent.delete(t.index), delete this.components[e], this.emit("component-removed", e));
|
|
471
|
+
}
|
|
472
|
+
setComponent(e, t, n) {
|
|
473
|
+
let r = this.components[e];
|
|
474
|
+
r && (r[t] = n, this.emit("component-property-updated", e, t, n));
|
|
475
|
+
}
|
|
476
|
+
setComponentBulk(e, t) {
|
|
477
|
+
let n = this.components[e];
|
|
478
|
+
n && Object.assign(n, t);
|
|
479
|
+
}
|
|
480
|
+
deleteComponent(e, t) {
|
|
481
|
+
let n = this.components[e];
|
|
482
|
+
n && (delete n[t], this.emit("component-property-deleted", e, t));
|
|
483
|
+
}
|
|
484
|
+
deleteAllComponentMemory() {
|
|
485
|
+
let e = this.world.registry;
|
|
486
|
+
for (let t of Object.keys(this.components)) {
|
|
487
|
+
let n = this.components[t];
|
|
488
|
+
n && e[t].memoryComponent.delete(n.index);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
load(e) {
|
|
492
|
+
let t = e, n = this.world.registry;
|
|
493
|
+
for (let r of Object.keys(n)) {
|
|
494
|
+
if (r === "entity") continue;
|
|
495
|
+
let i = n[r];
|
|
496
|
+
i.loadInFinishLoading || i.loadProperties.some((e) => e in t) && this.loadComponent(r, e, !1);
|
|
497
|
+
}
|
|
498
|
+
this.config = e;
|
|
499
|
+
}
|
|
500
|
+
save() {
|
|
501
|
+
let e = {}, t = this.world.registry, n = this.components;
|
|
502
|
+
for (let r of Object.keys(n)) {
|
|
503
|
+
let i = t[r], a = n[r];
|
|
504
|
+
i.save && a && Object.assign(e, i.save(a));
|
|
505
|
+
}
|
|
506
|
+
return e;
|
|
507
|
+
}
|
|
508
|
+
finishLoading() {
|
|
509
|
+
let e = this.config;
|
|
510
|
+
if (!e) return;
|
|
511
|
+
let t = e, n = this.world.registry;
|
|
512
|
+
for (let r of Object.keys(n)) {
|
|
513
|
+
if (r === "entity") continue;
|
|
514
|
+
let i = n[r];
|
|
515
|
+
i.loadInFinishLoading && i.loadProperties.some((e) => e in t) && this.loadComponent(r, e, !1);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}, T = class {
|
|
519
|
+
world;
|
|
520
|
+
configs;
|
|
521
|
+
constructor(e = {}) {
|
|
522
|
+
this.configs = e;
|
|
523
|
+
}
|
|
524
|
+
register(e, t) {
|
|
525
|
+
this.configs[e] = t;
|
|
526
|
+
}
|
|
527
|
+
getConfig(e) {
|
|
528
|
+
let t = e?.type, n = t ? this.configs[t] : void 0;
|
|
529
|
+
return n ? {
|
|
530
|
+
...n,
|
|
531
|
+
...e
|
|
532
|
+
} : e;
|
|
533
|
+
}
|
|
534
|
+
loadEntity(e, t = !0) {
|
|
535
|
+
let n = this.createEntity(this.getConfig(e));
|
|
536
|
+
return this.world.addEntity(n, t);
|
|
537
|
+
}
|
|
538
|
+
createEntity(e) {
|
|
539
|
+
return new w(this.world, e);
|
|
540
|
+
}
|
|
541
|
+
}, E = t, D = class extends d.default {
|
|
328
542
|
heap;
|
|
329
543
|
registry;
|
|
330
|
-
|
|
544
|
+
factory;
|
|
331
545
|
entities = [];
|
|
332
546
|
entitiesByEid = {};
|
|
333
547
|
systems = [];
|
|
334
548
|
gameTime = 0;
|
|
549
|
+
playerTime = 0;
|
|
550
|
+
timeScale = 1;
|
|
551
|
+
paused = !1;
|
|
335
552
|
destroyed = !1;
|
|
336
553
|
constructor(e, t = {}) {
|
|
337
|
-
super(), this.
|
|
338
|
-
let
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
554
|
+
super(), this.heap = new n({ bufferSize: t.heapSize ?? E });
|
|
555
|
+
let r = {
|
|
556
|
+
...e,
|
|
557
|
+
entity: C
|
|
558
|
+
}, i = {};
|
|
559
|
+
for (let e of Object.keys(r)) {
|
|
560
|
+
let t = r[e];
|
|
561
|
+
i[e] = {
|
|
562
|
+
...t,
|
|
563
|
+
memoryComponent: new f(this.heap, t.type, t.size)
|
|
564
|
+
};
|
|
342
565
|
}
|
|
343
|
-
this.
|
|
566
|
+
this.registry = i, this.factory = t.factory ?? new T(), this.factory.world = this;
|
|
344
567
|
}
|
|
345
568
|
async init() {
|
|
346
569
|
await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
|
|
@@ -350,15 +573,29 @@ var i = class {
|
|
|
350
573
|
this.addEntityToComponentSystem(e, t);
|
|
351
574
|
}), e.on("component-removed", (t) => {
|
|
352
575
|
this.removeEntityFromComponentSystem(e, t);
|
|
353
|
-
}), t && e.finishLoading(), this.emit("entity-added", e), e
|
|
576
|
+
}), t && (e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
|
|
577
|
+
this.onEntityDied(e);
|
|
578
|
+
})), e;
|
|
354
579
|
}
|
|
355
580
|
loadEntity(e, t = !0) {
|
|
356
|
-
return this.
|
|
581
|
+
return this.factory.loadEntity(e, t);
|
|
582
|
+
}
|
|
583
|
+
load(e) {
|
|
584
|
+
for (let e of this.entities.slice()) this.removeEntity(e);
|
|
585
|
+
this.systems.forEach((e) => e.clear());
|
|
586
|
+
let t = e.entities.map((e) => this.loadEntity(e, !1));
|
|
587
|
+
for (let e of t) e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
|
|
588
|
+
this.onEntityDied(e);
|
|
589
|
+
});
|
|
590
|
+
this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1;
|
|
357
591
|
}
|
|
358
592
|
removeEntity(e) {
|
|
359
593
|
let t = this.entities.indexOf(e);
|
|
360
594
|
t !== -1 && (this.entities.splice(t, 1), delete this.entitiesByEid[e.eid], this.emit("entity-removed", e)), e.deleteAllComponentMemory();
|
|
361
595
|
}
|
|
596
|
+
onEntityDied(e) {
|
|
597
|
+
this.removeEntity(e);
|
|
598
|
+
}
|
|
362
599
|
getEntityByEid(e) {
|
|
363
600
|
return this.entitiesByEid[e];
|
|
364
601
|
}
|
|
@@ -373,7 +610,8 @@ var i = class {
|
|
|
373
610
|
t !== -1 && this.systems.splice(t, 1);
|
|
374
611
|
}
|
|
375
612
|
update(e) {
|
|
376
|
-
this.
|
|
613
|
+
if (this.playerTime += e, this.paused) return {};
|
|
614
|
+
e = this.timeScale * e, this.gameTime += e;
|
|
377
615
|
let t = null;
|
|
378
616
|
return this.systems.forEach((n) => {
|
|
379
617
|
let r = !0, i = !1, a = !1;
|
|
@@ -391,14 +629,20 @@ var i = class {
|
|
|
391
629
|
});
|
|
392
630
|
}), { lastSystemError: t };
|
|
393
631
|
}
|
|
632
|
+
pause() {
|
|
633
|
+
this.paused = !0;
|
|
634
|
+
}
|
|
635
|
+
resume() {
|
|
636
|
+
this.paused = !1;
|
|
637
|
+
}
|
|
394
638
|
addEntityToComponentSystem(e, t) {
|
|
395
639
|
this.systems.forEach((n) => {
|
|
396
|
-
(n instanceof
|
|
640
|
+
(n instanceof h && n.options.components?.includes(t) || n instanceof b && this.componentAffectsComponentSystem(n, t)) && n.checkAddEntity(e);
|
|
397
641
|
});
|
|
398
642
|
}
|
|
399
643
|
removeEntityFromComponentSystem(e, t) {
|
|
400
644
|
this.systems.forEach((n) => {
|
|
401
|
-
n instanceof
|
|
645
|
+
n instanceof h && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof b && this.componentAffectsComponentSystem(n, t) && n.checkAddEntity(e);
|
|
402
646
|
});
|
|
403
647
|
}
|
|
404
648
|
componentAffectsComponentSystem(e, t) {
|
|
@@ -412,61 +656,76 @@ var i = class {
|
|
|
412
656
|
}
|
|
413
657
|
};
|
|
414
658
|
//#endregion
|
|
659
|
+
//#region src/actions/kill-entity.ts
|
|
660
|
+
function O(e) {
|
|
661
|
+
e.components.entity.dead = !0, e.emit("death");
|
|
662
|
+
}
|
|
663
|
+
//#endregion
|
|
664
|
+
//#region src/actions/kill-entity-worker.ts
|
|
665
|
+
function k(e, t, n) {
|
|
666
|
+
let r = t.entity;
|
|
667
|
+
r && (r[0] = 1), n.entityDied(e);
|
|
668
|
+
}
|
|
669
|
+
//#endregion
|
|
670
|
+
//#region src/actions/create-entity-worker.ts
|
|
671
|
+
function A(e, t) {
|
|
672
|
+
t.createEntity(e);
|
|
673
|
+
}
|
|
674
|
+
//#endregion
|
|
415
675
|
//#region src/systems/workers/create-component-worker.ts
|
|
416
|
-
function
|
|
417
|
-
let
|
|
418
|
-
|
|
419
|
-
let
|
|
420
|
-
if (
|
|
421
|
-
else if (
|
|
422
|
-
let
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
entityId: n,
|
|
429
|
-
components: t
|
|
430
|
-
};
|
|
431
|
-
});
|
|
676
|
+
function j(e, t) {
|
|
677
|
+
let n = [], r = {};
|
|
678
|
+
e.onmessage = function(i) {
|
|
679
|
+
let a = i.data;
|
|
680
|
+
if (a.type === "init") M(e, { type: "loaded" });
|
|
681
|
+
else if (a.type === "run") {
|
|
682
|
+
let i = performance.now(), o = [], s = [];
|
|
683
|
+
n = _(n, a.entities);
|
|
684
|
+
let c = {};
|
|
685
|
+
Object.entries(a.queries).forEach(([e, t]) => {
|
|
686
|
+
let n = _(r[e] ?? [], t);
|
|
687
|
+
r[e] = n, c[e] = n;
|
|
432
688
|
});
|
|
433
|
-
let
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
}),
|
|
689
|
+
let l = {
|
|
690
|
+
entityComponentChanged(e, t, n, r) {
|
|
691
|
+
o.push({
|
|
692
|
+
entityId: e,
|
|
693
|
+
event: "component-property-updated",
|
|
694
|
+
args: [
|
|
695
|
+
t,
|
|
696
|
+
n,
|
|
697
|
+
r
|
|
698
|
+
]
|
|
699
|
+
});
|
|
700
|
+
},
|
|
701
|
+
entityDied(e) {
|
|
702
|
+
o.push({
|
|
703
|
+
entityId: e,
|
|
704
|
+
event: "death",
|
|
705
|
+
args: []
|
|
706
|
+
});
|
|
707
|
+
},
|
|
708
|
+
createEntity(e) {
|
|
709
|
+
s.push(e);
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
t.preRun && t.preRun(a.world, n, c, l), n.forEach((e) => {
|
|
713
|
+
t(a.world, e.entityId, e.components, c, l);
|
|
714
|
+
}), t.entityRemoved && a.entities.removed.forEach((e) => {
|
|
715
|
+
t.entityRemoved(a.world, e, l);
|
|
716
|
+
}), M(e, {
|
|
459
717
|
type: "run-complete",
|
|
460
|
-
runTime: performance.now() -
|
|
461
|
-
events:
|
|
718
|
+
runTime: performance.now() - i,
|
|
719
|
+
events: o,
|
|
720
|
+
created: s
|
|
462
721
|
});
|
|
463
722
|
}
|
|
464
723
|
};
|
|
465
724
|
}
|
|
466
|
-
function
|
|
467
|
-
|
|
725
|
+
function M(e, t) {
|
|
726
|
+
e.postMessage(t);
|
|
468
727
|
}
|
|
469
728
|
//#endregion
|
|
470
|
-
export {
|
|
729
|
+
export { w as BaseEntity, D as BaseWorld, b as ComponentSystem, v as ComponentWebWorker, x as DEAD_INDEX, T as EntityFactory, h as EntitySystem, m as IterableSystem, f as MemoryComponent, S as STATIC_INDEX, p as System, g as WebWorker, j as createComponentWorker, A as createEntityWorker, C as entityDefinition, O as killEntity, k as killEntityWorker };
|
|
471
730
|
|
|
472
731
|
//# sourceMappingURL=index.js.map
|