@daneren2005/shared-memory-ecs 1.1.1 → 1.2.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/LICENSE +21 -0
- package/README.md +151 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +220 -83
- package/dist/index.js.map +1 -1
- package/dist/performance-timing.d.ts +44 -0
- package/dist/systems/component-system.d.ts +3 -1
- package/dist/systems/entity-system.d.ts +1 -1
- package/dist/systems/system.d.ts +2 -1
- package/dist/systems/workers/component-worker-message.d.ts +10 -5
- package/dist/world.d.ts +2 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -130,14 +130,14 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
130
130
|
clear() {
|
|
131
131
|
this.pool.clear();
|
|
132
132
|
}
|
|
133
|
-
}, p = class {
|
|
133
|
+
}, p = class extends d.default {
|
|
134
134
|
world;
|
|
135
135
|
name;
|
|
136
136
|
currentDelta = 0;
|
|
137
137
|
deltaBetweenRuns;
|
|
138
138
|
firstRun;
|
|
139
139
|
constructor(e, t = { name: "System" }) {
|
|
140
|
-
this.name = t.name, this.world = e, this.deltaBetweenRuns = t.deltaBetweenRuns ?? 0, this.firstRun = t.firstRun !== void 0 && t.firstRun;
|
|
140
|
+
super(), this.name = t.name, this.world = e, this.deltaBetweenRuns = t.deltaBetweenRuns ?? 0, this.firstRun = t.firstRun !== void 0 && t.firstRun;
|
|
141
141
|
}
|
|
142
142
|
init() {}
|
|
143
143
|
clear() {
|
|
@@ -153,7 +153,9 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
153
153
|
shouldRun() {
|
|
154
154
|
return !0;
|
|
155
155
|
}
|
|
156
|
-
destroy() {
|
|
156
|
+
destroy() {
|
|
157
|
+
this.removeAllListeners();
|
|
158
|
+
}
|
|
157
159
|
}, m = class extends p {
|
|
158
160
|
remainingInstancesToRun = [];
|
|
159
161
|
remainingInstancesStartTime = null;
|
|
@@ -183,7 +185,7 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
183
185
|
}
|
|
184
186
|
beforeRunIterables() {}
|
|
185
187
|
}, h = class extends m {
|
|
186
|
-
entities =
|
|
188
|
+
entities = /* @__PURE__ */ new Map();
|
|
187
189
|
options;
|
|
188
190
|
constructor(e, t = { name: "EntitySystem" }) {
|
|
189
191
|
t.iterationsPerCheck ||= 10, t.maxMsPerFrame ||= 4, super(e, t), this.options = t, e.on("entity-added", (e) => {
|
|
@@ -195,7 +197,10 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
195
197
|
});
|
|
196
198
|
}
|
|
197
199
|
getIterables() {
|
|
198
|
-
|
|
200
|
+
let e = [];
|
|
201
|
+
return this.entities.forEach((t) => {
|
|
202
|
+
t.components.entity.dead || e.push(t);
|
|
203
|
+
}), e;
|
|
199
204
|
}
|
|
200
205
|
updateIterable(e, t) {
|
|
201
206
|
e.components.entity.dead || this.updateEntity(e, t);
|
|
@@ -204,17 +209,16 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
204
209
|
return !e.components.entity.isStatic;
|
|
205
210
|
}
|
|
206
211
|
isEntityInSystem(e) {
|
|
207
|
-
return this.entities.
|
|
212
|
+
return this.entities.has(e.eid);
|
|
208
213
|
}
|
|
209
214
|
checkAddEntity(e) {
|
|
210
|
-
return this.options.components && this.options.components.filter((t) => !!e.components[t]).length !== this.options.components.length ? !1 : this.filterEntity(e) ? (this.entities.
|
|
215
|
+
return this.options.components && this.options.components.filter((t) => !!e.components[t]).length !== this.options.components.length ? !1 : this.filterEntity(e) ? (this.entities.set(e.eid, e), !0) : !1;
|
|
211
216
|
}
|
|
212
217
|
removeEntity(e) {
|
|
213
|
-
|
|
214
|
-
t !== -1 && this.entities.splice(t, 1);
|
|
218
|
+
this.entities.delete(e.eid);
|
|
215
219
|
}
|
|
216
220
|
shouldRun() {
|
|
217
|
-
return this.entities.
|
|
221
|
+
return this.entities.size > 0;
|
|
218
222
|
}
|
|
219
223
|
}, g = class {
|
|
220
224
|
onmessage(e, t = []) {}
|
|
@@ -248,44 +252,56 @@ var v = class extends g {
|
|
|
248
252
|
postMessage(e) {
|
|
249
253
|
if (e.type === "init") this.onMessageTyped({ type: "loaded" });
|
|
250
254
|
else if (e.type === "run") {
|
|
251
|
-
let t = [],
|
|
255
|
+
let t = performance.now(), n = [], r = {}, i = [];
|
|
252
256
|
this.entities = _(this.entities, e.entities);
|
|
253
|
-
let
|
|
257
|
+
let a = {};
|
|
254
258
|
Object.entries(e.queries).forEach(([e, t]) => {
|
|
255
259
|
let n = _(this.queryEntities[e] ?? [], t);
|
|
256
|
-
this.queryEntities[e] = n,
|
|
260
|
+
this.queryEntities[e] = n, a[e] = n;
|
|
257
261
|
});
|
|
258
|
-
let
|
|
259
|
-
entityComponentChanged(e,
|
|
260
|
-
|
|
262
|
+
let o = {
|
|
263
|
+
entityComponentChanged(e, t, r, i) {
|
|
264
|
+
n.push({
|
|
261
265
|
entityId: e,
|
|
262
266
|
event: "component-property-updated",
|
|
263
267
|
args: [
|
|
264
|
-
|
|
268
|
+
t,
|
|
265
269
|
r,
|
|
266
270
|
i
|
|
267
271
|
]
|
|
268
272
|
});
|
|
269
273
|
},
|
|
274
|
+
emitEntityEvent(e, t, ...r) {
|
|
275
|
+
n.push({
|
|
276
|
+
entityId: e,
|
|
277
|
+
event: t,
|
|
278
|
+
args: r
|
|
279
|
+
});
|
|
280
|
+
},
|
|
281
|
+
emitSystemEvent(e, t) {
|
|
282
|
+
(r[e] ?? (r[e] = [])).push(t);
|
|
283
|
+
},
|
|
270
284
|
entityDied(e) {
|
|
271
|
-
|
|
285
|
+
n.push({
|
|
272
286
|
entityId: e,
|
|
273
287
|
event: "death",
|
|
274
288
|
args: []
|
|
275
289
|
});
|
|
276
290
|
},
|
|
277
291
|
createEntity(e) {
|
|
278
|
-
|
|
292
|
+
i.push(e);
|
|
279
293
|
}
|
|
280
294
|
};
|
|
281
|
-
if (this.updateFunction.preRun && this.updateFunction.preRun(e.world, this.entities,
|
|
282
|
-
this.updateFunction(e.world, t.entityId, t.components,
|
|
283
|
-
}), this.updateFunction.entityRemoved) for (let t of e.entities.removed) this.updateFunction.entityRemoved(e.world, t,
|
|
295
|
+
if (this.updateFunction.preRun && this.updateFunction.preRun(e.world, this.entities, a, o), this.entities.forEach((t) => {
|
|
296
|
+
this.updateFunction(e.world, t.entityId, t.components, a, o);
|
|
297
|
+
}), this.updateFunction.entityRemoved) for (let t of e.entities.removed) this.updateFunction.entityRemoved(e.world, t, o);
|
|
298
|
+
let s = performance.now() - t;
|
|
284
299
|
this.onMessageTyped({
|
|
285
300
|
type: "run-complete",
|
|
286
|
-
runTime:
|
|
287
|
-
events:
|
|
288
|
-
|
|
301
|
+
runTime: s,
|
|
302
|
+
events: n,
|
|
303
|
+
systemEvents: r,
|
|
304
|
+
created: i
|
|
289
305
|
});
|
|
290
306
|
}
|
|
291
307
|
}
|
|
@@ -293,7 +309,7 @@ var v = class extends g {
|
|
|
293
309
|
this.onmessage({ data: e });
|
|
294
310
|
}
|
|
295
311
|
}, y = "___main", b = class extends p {
|
|
296
|
-
entities =
|
|
312
|
+
entities = /* @__PURE__ */ new Map();
|
|
297
313
|
options;
|
|
298
314
|
worker;
|
|
299
315
|
isWorkerThread;
|
|
@@ -304,7 +320,7 @@ var v = class extends g {
|
|
|
304
320
|
queryDeltas = {};
|
|
305
321
|
constructor(e, t) {
|
|
306
322
|
super(e, t), this.options = t, Object.keys(this.options.queries ?? {}).forEach((e) => {
|
|
307
|
-
this.queryEntities[e] =
|
|
323
|
+
this.queryEntities[e] = /* @__PURE__ */ new Map();
|
|
308
324
|
}), e.on("entity-added", (e) => {
|
|
309
325
|
this.checkAddEntity(e);
|
|
310
326
|
}), e.on("entity-removed", (e) => {
|
|
@@ -316,16 +332,21 @@ var v = class extends g {
|
|
|
316
332
|
initWorker() {
|
|
317
333
|
this.worker.onmessage = (e) => {
|
|
318
334
|
let t = e.data;
|
|
319
|
-
t.type === "loaded"
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
335
|
+
if (t.type === "loaded") this.loaded = !0, this.loadingPromise &&= (this.loadingPromise.resolve(), null);
|
|
336
|
+
else if (t.type === "run-complete") {
|
|
337
|
+
this.isRunning = !1, this.world.emit(`system-${this.name}-worker-finished`, t.runTime);
|
|
338
|
+
for (let e of Object.keys(t.systemEvents)) this.emit(e, t.systemEvents[e]);
|
|
339
|
+
t.events.forEach((e) => {
|
|
340
|
+
let t = this.world.getEntityByEid(e.entityId);
|
|
341
|
+
if (!t) {
|
|
342
|
+
console.warn(`Could not find entity with id ${e.entityId}`);
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
t.emit(e.event, ...e.args);
|
|
346
|
+
}), t.created.forEach((e) => {
|
|
347
|
+
this.world.loadEntity(e);
|
|
348
|
+
}), this.world.emit(`system-${this.name}-worker-events-finished`, t.runTime);
|
|
349
|
+
}
|
|
329
350
|
}, this.worker.postMessage({ type: "init" });
|
|
330
351
|
}
|
|
331
352
|
init() {
|
|
@@ -359,13 +380,17 @@ var v = class extends g {
|
|
|
359
380
|
this.worker.postMessage(i);
|
|
360
381
|
}
|
|
361
382
|
buildQueryDelta(e, t) {
|
|
362
|
-
let n = this.getQueryDelta(e), r =
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
383
|
+
let n = this.getQueryDelta(e), r = [];
|
|
384
|
+
n.added.forEach((e) => {
|
|
385
|
+
r.push({
|
|
386
|
+
entityId: e.eid,
|
|
387
|
+
components: this.buildComponents(e, t)
|
|
388
|
+
});
|
|
389
|
+
});
|
|
390
|
+
let i = Array.from(n.removed);
|
|
366
391
|
return this.queryDeltas[e] = {
|
|
367
|
-
added:
|
|
368
|
-
removed:
|
|
392
|
+
added: /* @__PURE__ */ new Set(),
|
|
393
|
+
removed: /* @__PURE__ */ new Set()
|
|
369
394
|
}, {
|
|
370
395
|
added: r,
|
|
371
396
|
removed: i
|
|
@@ -379,7 +404,7 @@ var v = class extends g {
|
|
|
379
404
|
}), n;
|
|
380
405
|
}
|
|
381
406
|
isEntityInSystem(e) {
|
|
382
|
-
return this.entities.
|
|
407
|
+
return this.entities.has(e.eid);
|
|
383
408
|
}
|
|
384
409
|
matchesQuery(e, t) {
|
|
385
410
|
return !(e.components.entity.dead || t.required.find((t) => !e.components[t]) || t.not?.find((t) => !!e.components[t]) || t.filter && !t.filter(e));
|
|
@@ -387,30 +412,24 @@ var v = class extends g {
|
|
|
387
412
|
getQueryDelta(e) {
|
|
388
413
|
let t = this.queryDeltas[e];
|
|
389
414
|
return t ||= this.queryDeltas[e] = {
|
|
390
|
-
added:
|
|
391
|
-
removed:
|
|
415
|
+
added: /* @__PURE__ */ new Set(),
|
|
416
|
+
removed: /* @__PURE__ */ new Set()
|
|
392
417
|
}, t;
|
|
393
418
|
}
|
|
394
419
|
markAdded(e, t) {
|
|
395
|
-
|
|
396
|
-
n !== -1 && e.removed.splice(n, 1), e.added.indexOf(t) === -1 && e.added.push(t);
|
|
420
|
+
e.removed.delete(t.eid), e.added.add(t);
|
|
397
421
|
}
|
|
398
422
|
markRemoved(e, t) {
|
|
399
|
-
|
|
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);
|
|
423
|
+
e.added.delete(t) || e.removed.add(t.eid);
|
|
405
424
|
}
|
|
406
425
|
updateEntityList(e, t, n, r) {
|
|
407
|
-
let i =
|
|
408
|
-
r ? (
|
|
426
|
+
let i = this.getQueryDelta(e);
|
|
427
|
+
r ? (t.set(n.eid, n), this.markAdded(i, n)) : t.delete(n.eid) && this.markRemoved(i, n);
|
|
409
428
|
}
|
|
410
429
|
checkAddEntity(e) {
|
|
411
430
|
let t = this.matchesQuery(e, this.options);
|
|
412
431
|
return this.updateEntityList(y, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
|
|
413
|
-
let r = this.queryEntities[t] ?? (this.queryEntities[t] =
|
|
432
|
+
let r = this.queryEntities[t] ?? (this.queryEntities[t] = /* @__PURE__ */ new Map());
|
|
414
433
|
this.updateEntityList(t, r, e, this.matchesQuery(e, n));
|
|
415
434
|
}), t;
|
|
416
435
|
}
|
|
@@ -420,10 +439,10 @@ var v = class extends g {
|
|
|
420
439
|
});
|
|
421
440
|
}
|
|
422
441
|
shouldRun() {
|
|
423
|
-
return this.entities.
|
|
442
|
+
return this.entities.size > 0;
|
|
424
443
|
}
|
|
425
444
|
destroy() {
|
|
426
|
-
"terminate" in this.worker && this.worker.terminate();
|
|
445
|
+
super.destroy(), "terminate" in this.worker && this.worker.terminate();
|
|
427
446
|
}
|
|
428
447
|
}, x = 0, S = 1, C = {
|
|
429
448
|
type: Uint32Array,
|
|
@@ -542,8 +561,7 @@ var v = class extends g {
|
|
|
542
561
|
heap;
|
|
543
562
|
registry;
|
|
544
563
|
factory;
|
|
545
|
-
entities =
|
|
546
|
-
entitiesByEid = {};
|
|
564
|
+
entities = /* @__PURE__ */ new Map();
|
|
547
565
|
systems = [];
|
|
548
566
|
gameTime = 0;
|
|
549
567
|
playerTime = 0;
|
|
@@ -569,7 +587,7 @@ var v = class extends g {
|
|
|
569
587
|
await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
|
|
570
588
|
}
|
|
571
589
|
addEntity(e, t = !0) {
|
|
572
|
-
return this.entities.
|
|
590
|
+
return this.entities.set(e.eid, e), e.world = this, e.on("component-added", (t) => {
|
|
573
591
|
this.addEntityToComponentSystem(e, t);
|
|
574
592
|
}), e.on("component-removed", (t) => {
|
|
575
593
|
this.removeEntityFromComponentSystem(e, t);
|
|
@@ -581,7 +599,7 @@ var v = class extends g {
|
|
|
581
599
|
return this.factory.loadEntity(e, t);
|
|
582
600
|
}
|
|
583
601
|
load(e) {
|
|
584
|
-
for (let e of this.entities.
|
|
602
|
+
for (let e of Array.from(this.entities.values())) this.removeEntity(e);
|
|
585
603
|
this.systems.forEach((e) => e.clear());
|
|
586
604
|
let t = e.entities.map((e) => this.loadEntity(e, !1));
|
|
587
605
|
for (let e of t) e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
|
|
@@ -590,26 +608,33 @@ var v = class extends g {
|
|
|
590
608
|
this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1;
|
|
591
609
|
}
|
|
592
610
|
removeEntity(e) {
|
|
593
|
-
|
|
594
|
-
t !== -1 && (this.entities.splice(t, 1), delete this.entitiesByEid[e.eid], this.emit("entity-removed", e)), e.deleteAllComponentMemory();
|
|
611
|
+
this.entities.delete(e.eid) && this.emit("entity-removed", e), e.deleteAllComponentMemory();
|
|
595
612
|
}
|
|
596
613
|
onEntityDied(e) {
|
|
597
614
|
this.removeEntity(e);
|
|
598
615
|
}
|
|
599
616
|
getEntityByEid(e) {
|
|
600
|
-
return this.
|
|
617
|
+
return this.entities.get(e);
|
|
601
618
|
}
|
|
602
619
|
addSystem(e) {
|
|
603
|
-
return this.systems.push(e), e;
|
|
620
|
+
return this.systems.push(e), this.emit("system-added", e), e;
|
|
604
621
|
}
|
|
605
622
|
addSystemIfNotExists(e) {
|
|
606
|
-
this.systems.findIndex((t) => e.name === t.name) === -1 && this.systems.push(e);
|
|
623
|
+
this.systems.findIndex((t) => e.name === t.name) === -1 && (this.systems.push(e), this.emit("system-added", e));
|
|
607
624
|
}
|
|
608
625
|
removeSystem(e) {
|
|
609
626
|
let t = this.systems.findIndex((t) => t.name === e);
|
|
610
|
-
t !== -1
|
|
627
|
+
if (t !== -1) {
|
|
628
|
+
let [e] = this.systems.splice(t, 1);
|
|
629
|
+
this.emit("system-removed", e);
|
|
630
|
+
}
|
|
611
631
|
}
|
|
612
632
|
update(e) {
|
|
633
|
+
this.emit("update-started", e);
|
|
634
|
+
let t = this.runUpdate(e);
|
|
635
|
+
return this.emit("update-finished", e), t;
|
|
636
|
+
}
|
|
637
|
+
runUpdate(e) {
|
|
613
638
|
if (this.playerTime += e, this.paused) return {};
|
|
614
639
|
e = this.timeScale * e, this.gameTime += e;
|
|
615
640
|
let t = null;
|
|
@@ -672,21 +697,122 @@ function A(e, t) {
|
|
|
672
697
|
t.createEntity(e);
|
|
673
698
|
}
|
|
674
699
|
//#endregion
|
|
700
|
+
//#region src/performance-timing.ts
|
|
701
|
+
var j = 1e3, M = {
|
|
702
|
+
avg: 0,
|
|
703
|
+
min: 0,
|
|
704
|
+
max: 0,
|
|
705
|
+
samples: 0
|
|
706
|
+
};
|
|
707
|
+
function N(e) {
|
|
708
|
+
if (!e.length) return { ...M };
|
|
709
|
+
let t = 0, n = Infinity, r = 0;
|
|
710
|
+
for (let i of e) t += i, i < n && (n = i), i > r && (r = i);
|
|
711
|
+
return {
|
|
712
|
+
avg: t / e.length,
|
|
713
|
+
min: n,
|
|
714
|
+
max: r,
|
|
715
|
+
samples: e.length
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
var P = class extends d.default {
|
|
719
|
+
world;
|
|
720
|
+
ticksBetweenUpdates;
|
|
721
|
+
stats = {
|
|
722
|
+
update: { ...M },
|
|
723
|
+
systems: [],
|
|
724
|
+
events: { ...M }
|
|
725
|
+
};
|
|
726
|
+
ticks = 0;
|
|
727
|
+
updateStart = 0;
|
|
728
|
+
updateTimes = [];
|
|
729
|
+
systemTimings = /* @__PURE__ */ new Map();
|
|
730
|
+
destroyed = !1;
|
|
731
|
+
constructor(e, t = {}) {
|
|
732
|
+
super(), this.world = e, this.ticksBetweenUpdates = t.ticksBetweenUpdates ?? 1e3, e.on("update-started", this.onUpdateStarted), e.on("update-finished", this.onUpdateFinished), e.on("system-added", this.onSystemAdded), e.on("system-removed", this.onSystemRemoved), e.systems.forEach((e) => this.trackSystem(e));
|
|
733
|
+
}
|
|
734
|
+
getSystemStats(e) {
|
|
735
|
+
return this.stats.systems.find((t) => t.name === e);
|
|
736
|
+
}
|
|
737
|
+
reset() {
|
|
738
|
+
this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
|
|
739
|
+
e.run = [], e.events = [], e.eventStart = -1;
|
|
740
|
+
}), this.stats = {
|
|
741
|
+
update: { ...M },
|
|
742
|
+
systems: [],
|
|
743
|
+
events: { ...M }
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
destroy() {
|
|
747
|
+
this.destroyed || (this.destroyed = !0, this.world.off("update-started", this.onUpdateStarted), this.world.off("update-finished", this.onUpdateFinished), this.world.off("system-added", this.onSystemAdded), this.world.off("system-removed", this.onSystemRemoved), Array.from(this.systemTimings.keys()).forEach((e) => this.untrackSystem(e)), this.removeAllListeners());
|
|
748
|
+
}
|
|
749
|
+
onUpdateStarted = () => {
|
|
750
|
+
this.updateStart = performance.now();
|
|
751
|
+
};
|
|
752
|
+
onUpdateFinished = (e) => {
|
|
753
|
+
this.world.paused || (this.updateTimes.push(performance.now() - this.updateStart), this.ticks += e, this.ticks >= this.ticksBetweenUpdates && this.recalculate());
|
|
754
|
+
};
|
|
755
|
+
onSystemAdded = (e) => {
|
|
756
|
+
this.trackSystem(e);
|
|
757
|
+
};
|
|
758
|
+
onSystemRemoved = (e) => {
|
|
759
|
+
this.untrackSystem(e.name);
|
|
760
|
+
};
|
|
761
|
+
trackSystem(e) {
|
|
762
|
+
if (this.systemTimings.has(e.name)) return;
|
|
763
|
+
let t = {
|
|
764
|
+
run: [],
|
|
765
|
+
events: [],
|
|
766
|
+
eventStart: -1,
|
|
767
|
+
onRunFinished: (e) => {
|
|
768
|
+
t.run.push(e), t.eventStart = performance.now();
|
|
769
|
+
},
|
|
770
|
+
onEventsFinished: () => {
|
|
771
|
+
t.eventStart < 0 || (t.events.push(performance.now() - t.eventStart), t.eventStart = -1);
|
|
772
|
+
}
|
|
773
|
+
};
|
|
774
|
+
this.world.on(`system-${e.name}-worker-finished`, t.onRunFinished), this.world.on(`system-${e.name}-worker-events-finished`, t.onEventsFinished), this.systemTimings.set(e.name, t);
|
|
775
|
+
}
|
|
776
|
+
untrackSystem(e) {
|
|
777
|
+
let t = this.systemTimings.get(e);
|
|
778
|
+
t && (this.world.off(`system-${e}-worker-finished`, t.onRunFinished), this.world.off(`system-${e}-worker-events-finished`, t.onEventsFinished), this.systemTimings.delete(e));
|
|
779
|
+
}
|
|
780
|
+
recalculate() {
|
|
781
|
+
let e = this.world.systems.map((e) => {
|
|
782
|
+
let t = this.systemTimings.get(e.name);
|
|
783
|
+
return {
|
|
784
|
+
name: e.name,
|
|
785
|
+
run: N(t?.run ?? []),
|
|
786
|
+
events: N(t?.events ?? [])
|
|
787
|
+
};
|
|
788
|
+
}), t = { ...M };
|
|
789
|
+
e.forEach((e) => {
|
|
790
|
+
t.avg += e.events.avg, t.min += e.events.min, t.max += e.events.max, t.samples += e.events.samples;
|
|
791
|
+
}), this.stats = {
|
|
792
|
+
update: N(this.updateTimes),
|
|
793
|
+
systems: e,
|
|
794
|
+
events: t
|
|
795
|
+
}, this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
|
|
796
|
+
e.run = [], e.events = [];
|
|
797
|
+
}), this.emit("stats-updated", this.stats);
|
|
798
|
+
}
|
|
799
|
+
};
|
|
800
|
+
//#endregion
|
|
675
801
|
//#region src/systems/workers/create-component-worker.ts
|
|
676
|
-
function
|
|
802
|
+
function F(e, t) {
|
|
677
803
|
let n = [], r = {};
|
|
678
804
|
e.onmessage = function(i) {
|
|
679
805
|
let a = i.data;
|
|
680
|
-
if (a.type === "init")
|
|
806
|
+
if (a.type === "init") I(e, { type: "loaded" });
|
|
681
807
|
else if (a.type === "run") {
|
|
682
|
-
let i = performance.now(), o = [], s = [];
|
|
808
|
+
let i = performance.now(), o = [], s = {}, c = [];
|
|
683
809
|
n = _(n, a.entities);
|
|
684
|
-
let
|
|
810
|
+
let l = {};
|
|
685
811
|
Object.entries(a.queries).forEach(([e, t]) => {
|
|
686
812
|
let n = _(r[e] ?? [], t);
|
|
687
|
-
r[e] = n,
|
|
813
|
+
r[e] = n, l[e] = n;
|
|
688
814
|
});
|
|
689
|
-
let
|
|
815
|
+
let u = {
|
|
690
816
|
entityComponentChanged(e, t, n, r) {
|
|
691
817
|
o.push({
|
|
692
818
|
entityId: e,
|
|
@@ -698,6 +824,16 @@ function j(e, t) {
|
|
|
698
824
|
]
|
|
699
825
|
});
|
|
700
826
|
},
|
|
827
|
+
emitEntityEvent(e, t, ...n) {
|
|
828
|
+
o.push({
|
|
829
|
+
entityId: e,
|
|
830
|
+
event: t,
|
|
831
|
+
args: n
|
|
832
|
+
});
|
|
833
|
+
},
|
|
834
|
+
emitSystemEvent(e, t) {
|
|
835
|
+
(s[e] ?? (s[e] = [])).push(t);
|
|
836
|
+
},
|
|
701
837
|
entityDied(e) {
|
|
702
838
|
o.push({
|
|
703
839
|
entityId: e,
|
|
@@ -706,26 +842,27 @@ function j(e, t) {
|
|
|
706
842
|
});
|
|
707
843
|
},
|
|
708
844
|
createEntity(e) {
|
|
709
|
-
|
|
845
|
+
c.push(e);
|
|
710
846
|
}
|
|
711
847
|
};
|
|
712
|
-
t.preRun && t.preRun(a.world, n,
|
|
713
|
-
t(a.world, e.entityId, e.components,
|
|
848
|
+
t.preRun && t.preRun(a.world, n, l, u), n.forEach((e) => {
|
|
849
|
+
t(a.world, e.entityId, e.components, l, u);
|
|
714
850
|
}), t.entityRemoved && a.entities.removed.forEach((e) => {
|
|
715
|
-
t.entityRemoved(a.world, e,
|
|
716
|
-
}),
|
|
851
|
+
t.entityRemoved(a.world, e, u);
|
|
852
|
+
}), I(e, {
|
|
717
853
|
type: "run-complete",
|
|
718
854
|
runTime: performance.now() - i,
|
|
719
855
|
events: o,
|
|
720
|
-
|
|
856
|
+
systemEvents: s,
|
|
857
|
+
created: c
|
|
721
858
|
});
|
|
722
859
|
}
|
|
723
860
|
};
|
|
724
861
|
}
|
|
725
|
-
function
|
|
862
|
+
function I(e, t) {
|
|
726
863
|
e.postMessage(t);
|
|
727
864
|
}
|
|
728
865
|
//#endregion
|
|
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,
|
|
866
|
+
export { w as BaseEntity, D as BaseWorld, b as ComponentSystem, v as ComponentWebWorker, x as DEAD_INDEX, j as DEFAULT_TICKS_BETWEEN_UPDATES, T as EntityFactory, h as EntitySystem, m as IterableSystem, f as MemoryComponent, P as PerformanceTiming, S as STATIC_INDEX, p as System, g as WebWorker, F as createComponentWorker, A as createEntityWorker, C as entityDefinition, O as killEntity, k as killEntityWorker };
|
|
730
867
|
|
|
731
868
|
//# sourceMappingURL=index.js.map
|