@daneren2005/shared-memory-ecs 1.5.0 → 1.6.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/README.md +104 -28
- package/dist/actions/build-worker-entity.d.ts +1 -1
- package/dist/actions/create-entity-worker.d.ts +2 -2
- package/dist/actions/kill-entity-worker.d.ts +2 -2
- package/dist/component-definition.d.ts +2 -0
- package/dist/{create-component-worker-CaB32Dgn.js → create-system-worker-BM5E-S92.js} +53 -19
- package/dist/create-system-worker-BM5E-S92.js.map +1 -0
- package/dist/entity.d.ts +1 -0
- package/dist/index.d.ts +10 -7
- package/dist/index.js +199 -94
- package/dist/index.js.map +1 -1
- package/dist/systems/entity-system.d.ts +1 -0
- package/dist/systems/{component-system.d.ts → entity-worker-system.d.ts} +22 -18
- package/dist/systems/iterable-system.d.ts +1 -0
- package/dist/systems/system.d.ts +11 -0
- package/dist/systems/worker-system.d.ts +22 -0
- package/dist/systems/workers/apply-query-delta.d.ts +1 -1
- package/dist/systems/workers/create-entity-system-worker.d.ts +8 -0
- package/dist/systems/workers/create-system-worker.d.ts +9 -0
- package/dist/systems/workers/entity-system-web-worker.d.ts +17 -0
- package/dist/systems/workers/{component-worker-message.d.ts → entity-system-worker-message.d.ts} +10 -4
- package/dist/worker.d.ts +7 -5
- package/dist/worker.js +2 -2
- package/dist/world.d.ts +5 -4
- package/package.json +3 -2
- package/dist/create-component-worker-CaB32Dgn.js.map +0 -1
- package/dist/systems/workers/component-web-worker.d.ts +0 -17
- package/dist/systems/workers/create-component-worker.d.ts +0 -8
package/dist/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { a as e, c as t, d as n, f as r, i, l as a,
|
|
2
|
-
import
|
|
3
|
-
import { MAX_BYTE_OFFSET_LENGTH as
|
|
1
|
+
import { a as e, c as t, d as n, f as r, i, l as a, m as o, n as s, o as c, p as l, r as u, s as d, t as f, u as p } from "./create-system-worker-BM5E-S92.js";
|
|
2
|
+
import m from "@daneren2005/shared-memory-objects/memory-heap";
|
|
3
|
+
import { MAX_BYTE_OFFSET_LENGTH as h } 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 g = Object.create, _ = Object.defineProperty, v = Object.getOwnPropertyDescriptor, y = Object.getOwnPropertyNames, b = Object.getPrototypeOf, x = Object.prototype.hasOwnProperty, S = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), C = (e, t, n, r) => {
|
|
6
|
+
if (t && typeof t == "object" || typeof t == "function") for (var i = y(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !x.call(e, s) && s !== n && _(e, s, {
|
|
7
7
|
get: ((e) => t[e]).bind(null, s),
|
|
8
|
-
enumerable: !(r =
|
|
8
|
+
enumerable: !(r = v(t, s)) || r.enumerable
|
|
9
9
|
});
|
|
10
10
|
return e;
|
|
11
|
-
},
|
|
11
|
+
}, w = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : g(b(e)), C(t || !e || !e.__esModule ? _(n, "default", {
|
|
12
12
|
value: e,
|
|
13
13
|
enumerable: !0
|
|
14
|
-
}) : n, e)))((/* @__PURE__ */
|
|
14
|
+
}) : n, e)))((/* @__PURE__ */ S(((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,10 +98,11 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
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), T = class extends w.default {
|
|
102
102
|
eid;
|
|
103
103
|
config;
|
|
104
104
|
world;
|
|
105
|
+
componentMemoryDeletionScheduled = !1;
|
|
105
106
|
components = {};
|
|
106
107
|
constructor(e, t, n) {
|
|
107
108
|
super(), this.world = e, this.eid = n ?? e.allocateEid(), n === void 0 && (this.loadComponent("entity", t ?? {}, !1), t && this.load(t));
|
|
@@ -115,6 +116,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
115
116
|
return r.block ??= n.memoryComponent.getBlock(t), this.components[e] = r, r;
|
|
116
117
|
}
|
|
117
118
|
removeComponent(e) {
|
|
119
|
+
if (this.componentMemoryDeletionScheduled) return;
|
|
118
120
|
let t = this.components[e];
|
|
119
121
|
if (t) {
|
|
120
122
|
let n = this.world.registry[e];
|
|
@@ -134,6 +136,8 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
134
136
|
n && (delete n[t], this.emit("component-property-deleted", e, t));
|
|
135
137
|
}
|
|
136
138
|
deleteAllComponentMemory() {
|
|
139
|
+
if (this.componentMemoryDeletionScheduled) return;
|
|
140
|
+
this.componentMemoryDeletionScheduled = !0;
|
|
137
141
|
let e = this.world.registry, t = this.components;
|
|
138
142
|
for (let n of Object.keys(t)) {
|
|
139
143
|
let r = e[n], i = t[n];
|
|
@@ -167,7 +171,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
167
171
|
i.loadInFinishLoading && i.loadProperties.some((e) => e in t) && this.loadComponent(r, e, !1);
|
|
168
172
|
}
|
|
169
173
|
}
|
|
170
|
-
},
|
|
174
|
+
}, E = class extends w.default {
|
|
171
175
|
world;
|
|
172
176
|
name;
|
|
173
177
|
currentDelta = 0;
|
|
@@ -187,6 +191,17 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
187
191
|
return this.deltaBetweenRuns > 0 && (e = this.currentDelta % this.deltaBetweenRuns), this.run(this.currentDelta - e), this.currentDelta = e, this.firstRun = !1, this.onRunFinished(), !0;
|
|
188
192
|
} else return !1;
|
|
189
193
|
}
|
|
194
|
+
onError(e, t = {}) {
|
|
195
|
+
let n = t.entityId === void 0 ? "" : ` (entity ${t.entityId})`;
|
|
196
|
+
console.error(`Error in system ${this.name}${n}: ${e.message}`, e);
|
|
197
|
+
let r = {
|
|
198
|
+
system: this.name,
|
|
199
|
+
error: e,
|
|
200
|
+
entityId: t.entityId,
|
|
201
|
+
phase: t.phase
|
|
202
|
+
};
|
|
203
|
+
this.world.emit("system-error", r);
|
|
204
|
+
}
|
|
190
205
|
onRunFinished() {
|
|
191
206
|
this.world.notifySystemRunCompleted(this);
|
|
192
207
|
}
|
|
@@ -200,7 +215,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
200
215
|
destroy() {
|
|
201
216
|
this.removeAllListeners();
|
|
202
217
|
}
|
|
203
|
-
},
|
|
218
|
+
}, D = class extends E {
|
|
204
219
|
remainingInstancesToRun = [];
|
|
205
220
|
remainingInstancesStartTime = null;
|
|
206
221
|
iterationsPerCheck;
|
|
@@ -226,15 +241,31 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
226
241
|
}
|
|
227
242
|
runIterables(e, t) {
|
|
228
243
|
let n = performance.now();
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
244
|
+
try {
|
|
245
|
+
this.beforeRunIterables();
|
|
246
|
+
} catch (e) {
|
|
247
|
+
this.onError(e, { phase: "preRun" }), this.remainingInstancesToRun = [], this.remainingInstancesStartTime = null;
|
|
232
248
|
return;
|
|
233
249
|
}
|
|
250
|
+
for (let r = 0; r < e.length; r++) {
|
|
251
|
+
try {
|
|
252
|
+
this.updateIterable(e[r], t);
|
|
253
|
+
} catch (t) {
|
|
254
|
+
this.onError(t, {
|
|
255
|
+
phase: "update",
|
|
256
|
+
entityId: this.getIterableEntityId(e[r])
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (r % this.iterationsPerCheck === 0 && performance.now() - n >= this.maxMsPerFrame) {
|
|
260
|
+
this.remainingInstancesToRun = e.slice(r + 1), this.remainingInstancesStartTime = t;
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
234
264
|
this.remainingInstancesToRun = [], this.remainingInstancesStartTime = null;
|
|
235
265
|
}
|
|
236
266
|
beforeRunIterables() {}
|
|
237
|
-
|
|
267
|
+
getIterableEntityId(e) {}
|
|
268
|
+
}, O = class extends D {
|
|
238
269
|
entities = /* @__PURE__ */ new Map();
|
|
239
270
|
options;
|
|
240
271
|
constructor(e, t = { name: "EntitySystem" }) {
|
|
@@ -253,11 +284,14 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
253
284
|
}), e;
|
|
254
285
|
}
|
|
255
286
|
updateIterable(e, t) {
|
|
256
|
-
e.components.entity.dead || this.updateEntity(e, t);
|
|
287
|
+
e.components.entity.dead || this.entities.get(e.eid) !== e || this.updateEntity(e, t);
|
|
257
288
|
}
|
|
258
289
|
filterEntity(e) {
|
|
259
290
|
return !e.components.entity.isStatic;
|
|
260
291
|
}
|
|
292
|
+
getIterableEntityId(e) {
|
|
293
|
+
return e.eid;
|
|
294
|
+
}
|
|
261
295
|
isEntityInSystem(e) {
|
|
262
296
|
return this.entities.has(e.eid);
|
|
263
297
|
}
|
|
@@ -268,11 +302,11 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
268
302
|
this.entities.delete(e.eid);
|
|
269
303
|
}
|
|
270
304
|
shouldRun() {
|
|
271
|
-
return this.entities.size > 0;
|
|
305
|
+
return this.isCurrentlyRunning() || this.entities.size > 0;
|
|
272
306
|
}
|
|
273
|
-
},
|
|
307
|
+
}, k = class {
|
|
274
308
|
onmessage(e, t = []) {}
|
|
275
|
-
},
|
|
309
|
+
}, A = class extends k {
|
|
276
310
|
updateFunction;
|
|
277
311
|
entities = [];
|
|
278
312
|
queryEntities = {};
|
|
@@ -293,68 +327,93 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
293
327
|
else if (e.type === "load") this.worldExtension = this.updateFunction.init?.(e.data) ?? void 0, this.onMessageTyped({ type: "loaded" });
|
|
294
328
|
else if (e.type === "reset") this.entities = [], this.queryEntities = {};
|
|
295
329
|
else if (e.type === "run") {
|
|
296
|
-
if (this.worldExtension && Object.assign(e.world, this.worldExtension), e.world.getString = (e) => this.world.constantStrings.getString(e) ?? "", e.world.allocate = this.allocator, this.createsEntities) {
|
|
330
|
+
if (this.worldExtension && Object.assign(e.world, this.worldExtension), e.world.getString = (e) => this.world.constantStrings.getString(e) ?? "", e.world.heap = this.world.heap, e.world.allocate = this.allocator, this.createsEntities) {
|
|
297
331
|
let t = this.world.registry;
|
|
298
|
-
e.world.buildEntityDescriptor = (e) =>
|
|
332
|
+
e.world.buildEntityDescriptor = (e) => n(e, this.world.factory.configs, t, this.allocator);
|
|
299
333
|
}
|
|
300
|
-
let t = performance.now(),
|
|
301
|
-
this.entities =
|
|
302
|
-
let
|
|
334
|
+
let t = performance.now(), i = [], a = {}, o = [], s = [];
|
|
335
|
+
this.entities = r(this.entities, e.entities);
|
|
336
|
+
let c = {};
|
|
303
337
|
Object.entries(e.queries).forEach(([e, t]) => {
|
|
304
|
-
let n =
|
|
305
|
-
this.queryEntities[e] = n,
|
|
338
|
+
let n = r(this.queryEntities[e] ?? [], t);
|
|
339
|
+
this.queryEntities[e] = n, c[e] = n;
|
|
306
340
|
});
|
|
307
|
-
let
|
|
308
|
-
entityComponentChanged(e, t,
|
|
309
|
-
|
|
341
|
+
let l = {
|
|
342
|
+
entityComponentChanged(e, t, n, r) {
|
|
343
|
+
i.push({
|
|
310
344
|
entityId: e,
|
|
311
345
|
event: "component-property-updated",
|
|
312
346
|
args: [
|
|
313
347
|
t,
|
|
314
|
-
|
|
315
|
-
|
|
348
|
+
n,
|
|
349
|
+
r
|
|
316
350
|
]
|
|
317
351
|
});
|
|
318
352
|
},
|
|
319
|
-
emitEntityEvent(e, t, ...
|
|
320
|
-
|
|
353
|
+
emitEntityEvent(e, t, ...n) {
|
|
354
|
+
i.push({
|
|
321
355
|
entityId: e,
|
|
322
356
|
event: t,
|
|
323
|
-
args:
|
|
357
|
+
args: n
|
|
324
358
|
});
|
|
325
359
|
},
|
|
326
360
|
emitSystemEvent(e, t) {
|
|
327
|
-
(
|
|
361
|
+
(a[e] ?? (a[e] = [])).push(t);
|
|
328
362
|
},
|
|
329
363
|
entityDied(e) {
|
|
330
|
-
|
|
364
|
+
i.push({
|
|
331
365
|
entityId: e,
|
|
332
366
|
event: "death",
|
|
333
367
|
args: []
|
|
334
368
|
});
|
|
335
369
|
},
|
|
336
370
|
createEntity(e) {
|
|
337
|
-
|
|
371
|
+
o.push(e);
|
|
338
372
|
}
|
|
339
|
-
};
|
|
340
|
-
if (this.updateFunction.preRun
|
|
341
|
-
this.updateFunction(e.world,
|
|
342
|
-
}
|
|
343
|
-
|
|
373
|
+
}, u = !1;
|
|
374
|
+
if (this.updateFunction.preRun) try {
|
|
375
|
+
this.updateFunction.preRun(e.world, this.entities, c, l);
|
|
376
|
+
} catch (e) {
|
|
377
|
+
u = !0, s.push({
|
|
378
|
+
error: e,
|
|
379
|
+
phase: "preRun"
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
if (u || this.entities.forEach((t) => {
|
|
383
|
+
try {
|
|
384
|
+
this.updateFunction(e.world, t.entityId, t.components, c, l);
|
|
385
|
+
} catch (e) {
|
|
386
|
+
s.push({
|
|
387
|
+
error: e,
|
|
388
|
+
phase: "update",
|
|
389
|
+
entityId: t.entityId
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}), this.updateFunction.entityRemoved) for (let t of e.entities.removed) try {
|
|
393
|
+
this.updateFunction.entityRemoved(e.world, t, l);
|
|
394
|
+
} catch (e) {
|
|
395
|
+
s.push({
|
|
396
|
+
error: e,
|
|
397
|
+
phase: "entityRemoved",
|
|
398
|
+
entityId: t
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
let d = performance.now() - t;
|
|
344
402
|
this.onMessageTyped({
|
|
345
403
|
type: "run-complete",
|
|
346
404
|
generation: e.generation,
|
|
347
|
-
runTime:
|
|
348
|
-
events:
|
|
349
|
-
systemEvents:
|
|
350
|
-
created:
|
|
405
|
+
runTime: d,
|
|
406
|
+
events: i,
|
|
407
|
+
systemEvents: a,
|
|
408
|
+
created: o,
|
|
409
|
+
errors: s
|
|
351
410
|
});
|
|
352
411
|
}
|
|
353
412
|
}
|
|
354
413
|
onMessageTyped(e) {
|
|
355
414
|
this.onmessage({ data: e });
|
|
356
415
|
}
|
|
357
|
-
},
|
|
416
|
+
}, j = "___main", M = class extends E {
|
|
358
417
|
entities = /* @__PURE__ */ new Map();
|
|
359
418
|
options;
|
|
360
419
|
worker;
|
|
@@ -381,7 +440,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
381
440
|
type: "grow-buffer",
|
|
382
441
|
buffer: e
|
|
383
442
|
});
|
|
384
|
-
})) : (this.worker = new
|
|
443
|
+
})) : (this.worker = new A(t.updateFunction, e, !!t.createsEntities), this.isWorkerThread = !1), this.initWorker();
|
|
385
444
|
}
|
|
386
445
|
initWorker() {
|
|
387
446
|
this.worker.onmessage = (e) => {
|
|
@@ -391,7 +450,12 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
391
450
|
else if (t.type === "grow-buffer-from-worker") this.world.addGrownBuffer(t.buffer);
|
|
392
451
|
else if (t.type === "run-complete") {
|
|
393
452
|
if (t.generation !== this.generation) return;
|
|
394
|
-
this.isRunning = !1, this.world.emit(`system-${this.name}-worker-finished`, t.runTime)
|
|
453
|
+
this.isRunning = !1, this.world.emit(`system-${this.name}-worker-finished`, t.runTime), t.errors.forEach(({ error: e, entityId: t, phase: n }) => {
|
|
454
|
+
this.onError(e, {
|
|
455
|
+
entityId: t,
|
|
456
|
+
phase: n
|
|
457
|
+
});
|
|
458
|
+
});
|
|
395
459
|
for (let e of Object.keys(t.systemEvents)) this.emit(e, t.systemEvents[e]);
|
|
396
460
|
t.events.forEach((e) => {
|
|
397
461
|
let t = this.world.getEntityByEid(e.entityId);
|
|
@@ -465,7 +529,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
465
529
|
elapsedTime: e
|
|
466
530
|
};
|
|
467
531
|
this.addDataToWorld?.(t), this.isRunning = !0;
|
|
468
|
-
let n = this.buildQueryDelta(
|
|
532
|
+
let n = this.buildQueryDelta(j, this.options), r = {};
|
|
469
533
|
Object.entries(this.options.queries ?? {}).forEach(([e, t]) => {
|
|
470
534
|
r[e] = this.buildQueryDelta(e, t);
|
|
471
535
|
});
|
|
@@ -527,13 +591,13 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
527
591
|
}
|
|
528
592
|
checkAddEntity(e) {
|
|
529
593
|
let t = this.matchesQuery(e, this.options);
|
|
530
|
-
return this.updateEntityList(
|
|
594
|
+
return this.updateEntityList(j, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
|
|
531
595
|
let r = this.queryEntities[t] ?? (this.queryEntities[t] = /* @__PURE__ */ new Map());
|
|
532
596
|
this.updateEntityList(t, r, e, this.matchesQuery(e, n));
|
|
533
597
|
}), t;
|
|
534
598
|
}
|
|
535
599
|
removeEntity(e) {
|
|
536
|
-
this.updateEntityList(
|
|
600
|
+
this.updateEntityList(j, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
|
|
537
601
|
this.updateEntityList(t, n, e, !1);
|
|
538
602
|
});
|
|
539
603
|
}
|
|
@@ -548,7 +612,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
548
612
|
destroy() {
|
|
549
613
|
super.destroy(), "terminate" in this.worker && this.worker.terminate();
|
|
550
614
|
}
|
|
551
|
-
},
|
|
615
|
+
}, N = class {
|
|
552
616
|
world;
|
|
553
617
|
configs;
|
|
554
618
|
constructor(e = {}) {
|
|
@@ -569,9 +633,9 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
569
633
|
return this.world.addEntity(n, t);
|
|
570
634
|
}
|
|
571
635
|
createEntity(e) {
|
|
572
|
-
return new
|
|
636
|
+
return new T(this.world, e);
|
|
573
637
|
}
|
|
574
|
-
},
|
|
638
|
+
}, P = h, F = 1e4, I = class extends w.default {
|
|
575
639
|
heap;
|
|
576
640
|
constantStrings;
|
|
577
641
|
eidCounter;
|
|
@@ -596,19 +660,19 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
596
660
|
waitElapsed: 0
|
|
597
661
|
};
|
|
598
662
|
constructor(e, t = {}) {
|
|
599
|
-
super(), this.heap = new
|
|
600
|
-
let
|
|
663
|
+
super(), this.heap = new m({ bufferSize: t.heapSize ?? P }), this.constantStrings = new l(this.heap), this.eidCounter = this.heap.allocUI32(1), this.heap.addOnGrowBufferHandlers((e) => this.emit("grow-buffer", e));
|
|
664
|
+
let n = {
|
|
601
665
|
...e,
|
|
602
|
-
entity:
|
|
603
|
-
},
|
|
604
|
-
for (let e of Object.keys(
|
|
605
|
-
let t =
|
|
606
|
-
|
|
666
|
+
entity: a
|
|
667
|
+
}, r = {};
|
|
668
|
+
for (let e of Object.keys(n)) {
|
|
669
|
+
let t = n[e];
|
|
670
|
+
r[e] = {
|
|
607
671
|
...t,
|
|
608
|
-
memoryComponent: new
|
|
672
|
+
memoryComponent: new o(this.heap, t.type, t.size)
|
|
609
673
|
};
|
|
610
674
|
}
|
|
611
|
-
this.registry =
|
|
675
|
+
this.registry = r, this.factory = t.factory ?? new N(), this.factory.world = this;
|
|
612
676
|
}
|
|
613
677
|
allocateEid() {
|
|
614
678
|
return Atomics.add(this.eidCounter.data, 0, 1) + 1;
|
|
@@ -624,6 +688,9 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
624
688
|
addGrownBuffer(e) {
|
|
625
689
|
this.heap.buffers[e.bufferPosition] === void 0 && this.heap.addSharedBuffer(e), this.emit("grow-buffer", e);
|
|
626
690
|
}
|
|
691
|
+
get needsSpareBuffer() {
|
|
692
|
+
return this.systems.some((e) => e instanceof M && e.isWorkerThread && !!e.options.createsEntities);
|
|
693
|
+
}
|
|
627
694
|
async init() {
|
|
628
695
|
await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
|
|
629
696
|
}
|
|
@@ -632,9 +699,9 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
632
699
|
}
|
|
633
700
|
addEntity(e, t = !0) {
|
|
634
701
|
return this.entities.set(e.eid, e), e.world = this, this.pristine = !1, e.on("component-added", (t) => {
|
|
635
|
-
this.
|
|
702
|
+
this.addEntityToEntityWorkerSystem(e, t);
|
|
636
703
|
}), e.on("component-removed", (t) => {
|
|
637
|
-
this.
|
|
704
|
+
this.removeEntityFromEntityWorkerSystem(e, t);
|
|
638
705
|
}), t && (e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
|
|
639
706
|
this.onEntityDied(e);
|
|
640
707
|
})), e;
|
|
@@ -643,7 +710,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
643
710
|
return this.factory.loadEntity(e, t);
|
|
644
711
|
}
|
|
645
712
|
adoptEntity(e) {
|
|
646
|
-
let t = new
|
|
713
|
+
let t = new T(this, void 0, e.eid);
|
|
647
714
|
t.loadComponent("entity", {
|
|
648
715
|
type: e.type,
|
|
649
716
|
isStatic: e.isStatic
|
|
@@ -665,15 +732,32 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
665
732
|
}
|
|
666
733
|
async clear() {
|
|
667
734
|
if (!this.pristine) {
|
|
735
|
+
await Promise.all(this.systems.map((e) => e.waitForRunToComplete()).filter((e) => e instanceof Promise));
|
|
668
736
|
for (let e of Array.from(this.entities.values())) this.removeEntity(e);
|
|
669
|
-
this.systems.forEach((e) => e.clear()),
|
|
737
|
+
this.systems.forEach((e) => e.clear()), this.freeAllPendingBuffers(), this.pristine = !0;
|
|
670
738
|
}
|
|
671
739
|
}
|
|
672
740
|
removeEntity(e) {
|
|
673
|
-
this.entities.
|
|
741
|
+
this.entities.get(e.eid) === e && (this.entities.delete(e.eid), this.emit("entity-removed", e), e.deleteAllComponentMemory());
|
|
674
742
|
}
|
|
675
743
|
onEntityDied(e) {
|
|
676
|
-
this.
|
|
744
|
+
if (this.entities.get(e.eid) !== e) return;
|
|
745
|
+
let t = this.registry, n = e.components;
|
|
746
|
+
for (let r of Object.keys(n)) {
|
|
747
|
+
let i = t[r], a = n[r];
|
|
748
|
+
if (i?.died && a) try {
|
|
749
|
+
i.died(a, e, this);
|
|
750
|
+
} catch (t) {
|
|
751
|
+
let n = t;
|
|
752
|
+
console.error(`Error in died hook for component ${String(r)} (entity ${e.eid}): ${n.message}`, n), this.emit("system-error", {
|
|
753
|
+
system: String(r),
|
|
754
|
+
error: n,
|
|
755
|
+
entityId: e.eid,
|
|
756
|
+
phase: "died"
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
this.emit("entity-died", e), this.removeEntity(e);
|
|
677
761
|
}
|
|
678
762
|
getEntityByEid(e) {
|
|
679
763
|
return this.entities.get(e);
|
|
@@ -710,9 +794,9 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
710
794
|
}
|
|
711
795
|
checkFreeBufferTimeout(e) {
|
|
712
796
|
let t = this.activeFreeBuffer;
|
|
713
|
-
if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >=
|
|
797
|
+
if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >= F)) {
|
|
714
798
|
let e = Array.from(t.waitingSystems).map((e) => e.name).join(", ");
|
|
715
|
-
console.warn(`shared-memory-ecs: deferred component free waited over ${
|
|
799
|
+
console.warn(`shared-memory-ecs: deferred component free waited over ${F}ms on system(s): ${e}; freeing anyway`), t.waitingSystems.clear(), this.processFreeBuffers();
|
|
716
800
|
}
|
|
717
801
|
}
|
|
718
802
|
addSystem(e) {
|
|
@@ -736,7 +820,7 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
736
820
|
runUpdate(e) {
|
|
737
821
|
if (this.playerTime += e, this.paused) return {};
|
|
738
822
|
let t = e;
|
|
739
|
-
e = this.timeScale * e, this.gameTime += e, this.processFreeBuffers();
|
|
823
|
+
e = this.timeScale * e, this.gameTime += e, this.processFreeBuffers(), this.needsSpareBuffer && this.heap.ensureSpareBuffer();
|
|
740
824
|
let n = null;
|
|
741
825
|
return this.systems.forEach((t) => {
|
|
742
826
|
let r = !0, i = !1, a = !1;
|
|
@@ -744,8 +828,12 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
744
828
|
try {
|
|
745
829
|
r = t.shouldRun(), r && (i = t.update(e));
|
|
746
830
|
} catch (e) {
|
|
747
|
-
let
|
|
748
|
-
console.error(
|
|
831
|
+
let r = e;
|
|
832
|
+
console.error(r.message, r), a = !0, n = r, this.emit("system-error", {
|
|
833
|
+
system: t.name,
|
|
834
|
+
error: r,
|
|
835
|
+
phase: "run"
|
|
836
|
+
});
|
|
749
837
|
}
|
|
750
838
|
this.emit(`system-${t.name}-finished`, {
|
|
751
839
|
ran: i,
|
|
@@ -760,17 +848,17 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
760
848
|
resume() {
|
|
761
849
|
this.paused = !1;
|
|
762
850
|
}
|
|
763
|
-
|
|
851
|
+
addEntityToEntityWorkerSystem(e, t) {
|
|
764
852
|
this.systems.forEach((n) => {
|
|
765
|
-
(n instanceof
|
|
853
|
+
(n instanceof O && n.options.components?.includes(t) || n instanceof M && this.componentAffectsEntityWorkerSystem(n, t)) && n.checkAddEntity(e);
|
|
766
854
|
});
|
|
767
855
|
}
|
|
768
|
-
|
|
856
|
+
removeEntityFromEntityWorkerSystem(e, t) {
|
|
769
857
|
this.systems.forEach((n) => {
|
|
770
|
-
n instanceof
|
|
858
|
+
n instanceof O && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof M && this.componentAffectsEntityWorkerSystem(n, t) && n.checkAddEntity(e);
|
|
771
859
|
});
|
|
772
860
|
}
|
|
773
|
-
|
|
861
|
+
componentAffectsEntityWorkerSystem(e, t) {
|
|
774
862
|
let n = e.options.required.includes(t) || !!e.options.not?.includes(t) || !!e.options.optional?.includes(t), r = Object.values(e.options.queries ?? {}).some((e) => e.required.includes(t) || !!e.not?.includes(t) || !!e.optional?.includes(t));
|
|
775
863
|
return n || r;
|
|
776
864
|
}
|
|
@@ -782,19 +870,19 @@ var m = Object.create, h = Object.defineProperty, g = Object.getOwnPropertyDescr
|
|
|
782
870
|
};
|
|
783
871
|
//#endregion
|
|
784
872
|
//#region src/actions/kill-entity.ts
|
|
785
|
-
function
|
|
873
|
+
function L(e) {
|
|
786
874
|
e.components.entity.dead = !0, e.emit("death");
|
|
787
875
|
}
|
|
788
876
|
//#endregion
|
|
789
877
|
//#region src/performance-timing.ts
|
|
790
|
-
var
|
|
878
|
+
var R = 1e3, z = {
|
|
791
879
|
avg: 0,
|
|
792
880
|
min: 0,
|
|
793
881
|
max: 0,
|
|
794
882
|
samples: 0
|
|
795
883
|
};
|
|
796
|
-
function
|
|
797
|
-
if (!e.length) return { ...
|
|
884
|
+
function B(e) {
|
|
885
|
+
if (!e.length) return { ...z };
|
|
798
886
|
let t = 0, n = Infinity, r = 0;
|
|
799
887
|
for (let i of e) t += i, i < n && (n = i), i > r && (r = i);
|
|
800
888
|
return {
|
|
@@ -804,13 +892,13 @@ function R(e) {
|
|
|
804
892
|
samples: e.length
|
|
805
893
|
};
|
|
806
894
|
}
|
|
807
|
-
var
|
|
895
|
+
var V = class extends w.default {
|
|
808
896
|
world;
|
|
809
897
|
ticksBetweenUpdates;
|
|
810
898
|
stats = {
|
|
811
|
-
update: { ...
|
|
899
|
+
update: { ...z },
|
|
812
900
|
systems: [],
|
|
813
|
-
events: { ...
|
|
901
|
+
events: { ...z }
|
|
814
902
|
};
|
|
815
903
|
ticks = 0;
|
|
816
904
|
updateStart = 0;
|
|
@@ -827,9 +915,9 @@ var z = class extends S.default {
|
|
|
827
915
|
this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
|
|
828
916
|
e.run = [], e.events = [], e.eventStart = -1;
|
|
829
917
|
}), this.stats = {
|
|
830
|
-
update: { ...
|
|
918
|
+
update: { ...z },
|
|
831
919
|
systems: [],
|
|
832
|
-
events: { ...
|
|
920
|
+
events: { ...z }
|
|
833
921
|
};
|
|
834
922
|
}
|
|
835
923
|
destroy() {
|
|
@@ -871,22 +959,39 @@ var z = class extends S.default {
|
|
|
871
959
|
let t = this.systemTimings.get(e.name);
|
|
872
960
|
return {
|
|
873
961
|
name: e.name,
|
|
874
|
-
run:
|
|
875
|
-
events:
|
|
962
|
+
run: B(t?.run ?? []),
|
|
963
|
+
events: B(t?.events ?? [])
|
|
876
964
|
};
|
|
877
|
-
}), t = { ...
|
|
965
|
+
}), t = { ...z };
|
|
878
966
|
e.forEach((e) => {
|
|
879
967
|
t.avg += e.events.avg, t.min += e.events.min, t.max += e.events.max, t.samples += e.events.samples;
|
|
880
968
|
}), this.stats = {
|
|
881
|
-
update:
|
|
969
|
+
update: B(this.updateTimes),
|
|
882
970
|
systems: e,
|
|
883
971
|
events: t
|
|
884
972
|
}, this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
|
|
885
973
|
e.run = [], e.events = [];
|
|
886
974
|
}), this.emit("stats-updated", this.stats);
|
|
887
975
|
}
|
|
976
|
+
}, H = class extends M {
|
|
977
|
+
constructor(e, t) {
|
|
978
|
+
super(e, {
|
|
979
|
+
...t,
|
|
980
|
+
required: [],
|
|
981
|
+
updateFunction: s(t.updateFunction)
|
|
982
|
+
});
|
|
983
|
+
}
|
|
984
|
+
checkAddEntity(e) {
|
|
985
|
+
return Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
|
|
986
|
+
let r = this.queryEntities[t] ?? (this.queryEntities[t] = /* @__PURE__ */ new Map());
|
|
987
|
+
this.updateEntityList(t, r, e, this.matchesQuery(e, n));
|
|
988
|
+
}), !1;
|
|
989
|
+
}
|
|
990
|
+
shouldRun() {
|
|
991
|
+
return !0;
|
|
992
|
+
}
|
|
888
993
|
};
|
|
889
994
|
//#endregion
|
|
890
|
-
export {
|
|
995
|
+
export { T as BaseEntity, I as BaseWorld, p as Component, l as ConstantStringCache, c as DEAD_INDEX, R as DEFAULT_TICKS_BETWEEN_UPDATES, N as EntityFactory, O as EntitySystem, A as EntitySystemWebWorker, M as EntityWorkerSystem, D as IterableSystem, o as MemoryComponent, V as PerformanceTiming, d as STATIC_INDEX, E as System, t as TYPE_INDEX, k as WebWorker, H as WorkerSystem, u as createEntitySystemWorker, i as createEntityWorker, f as createSystemWorker, a as entityDefinition, L as killEntity, e as killEntityWorker };
|
|
891
996
|
|
|
892
997
|
//# sourceMappingURL=index.js.map
|