@daneren2005/shared-memory-ecs 1.2.0 → 1.2.2
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 +119 -2
- package/dist/component-definition.d.ts +1 -1
- package/dist/create-component-worker-Bd1BTUkq.js +126 -0
- package/dist/create-component-worker-Bd1BTUkq.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +132 -238
- package/dist/index.js.map +1 -1
- package/dist/memory-component.d.ts +3 -1
- package/dist/systems/component-system.d.ts +2 -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 +4 -0
- package/dist/worker.d.ts +8 -0
- package/dist/worker.js +2 -0
- package/dist/world.d.ts +2 -5
- package/package.json +9 -2
package/dist/index.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as e, i as t, n, o as r, r as i, s as a, t as o } from "./create-component-worker-Bd1BTUkq.js";
|
|
2
|
+
import s from "@daneren2005/shared-memory-objects/memory-heap";
|
|
3
|
+
import { MAX_BYTE_OFFSET_LENGTH as c } from "@daneren2005/shared-memory-objects/utils/pointer";
|
|
4
|
+
import l from "@daneren2005/shared-memory-objects/local-pool";
|
|
2
5
|
//#region \0rolldown/runtime.js
|
|
3
|
-
var
|
|
4
|
-
if (t && typeof t == "object" || typeof t == "function") for (var
|
|
5
|
-
get: ((e) => t[e]).bind(null,
|
|
6
|
-
enumerable: !(r =
|
|
6
|
+
var u = Object.create, d = Object.defineProperty, f = Object.getOwnPropertyDescriptor, p = Object.getOwnPropertyNames, m = Object.getPrototypeOf, h = Object.prototype.hasOwnProperty, g = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), _ = (e, t, n, r) => {
|
|
7
|
+
if (t && typeof t == "object" || typeof t == "function") for (var i = p(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !h.call(e, s) && s !== n && d(e, s, {
|
|
8
|
+
get: ((e) => t[e]).bind(null, s),
|
|
9
|
+
enumerable: !(r = f(t, s)) || r.enumerable
|
|
7
10
|
});
|
|
8
11
|
return e;
|
|
9
|
-
},
|
|
12
|
+
}, v = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : u(m(e)), _(t || !e || !e.__esModule ? d(n, "default", {
|
|
10
13
|
value: e,
|
|
11
14
|
enumerable: !0
|
|
12
|
-
}) : n, e)))((/* @__PURE__ */
|
|
15
|
+
}) : n, e)))((/* @__PURE__ */ g(((e, t) => {
|
|
13
16
|
var n = Object.prototype.hasOwnProperty, r = "~";
|
|
14
17
|
function i() {}
|
|
15
18
|
Object.create && (i.prototype = Object.create(null), new i().__proto__ || (r = !1));
|
|
@@ -96,13 +99,13 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
96
99
|
var t;
|
|
97
100
|
return e ? (t = r ? r + e : e, this._events[t] && s(this, t)) : (this._events = new i(), this._eventsCount = 0), this;
|
|
98
101
|
}, 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),
|
|
102
|
+
})))(), 1), y = class {
|
|
100
103
|
heap;
|
|
101
104
|
pool;
|
|
102
|
-
constructor(t, n
|
|
103
|
-
this.heap =
|
|
104
|
-
type:
|
|
105
|
-
dataLength:
|
|
105
|
+
constructor(e, t, n) {
|
|
106
|
+
this.heap = e, this.pool = new l(e, {
|
|
107
|
+
type: t,
|
|
108
|
+
dataLength: n
|
|
106
109
|
});
|
|
107
110
|
}
|
|
108
111
|
get length() {
|
|
@@ -130,14 +133,14 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
130
133
|
clear() {
|
|
131
134
|
this.pool.clear();
|
|
132
135
|
}
|
|
133
|
-
},
|
|
136
|
+
}, b = class extends v.default {
|
|
134
137
|
world;
|
|
135
138
|
name;
|
|
136
139
|
currentDelta = 0;
|
|
137
140
|
deltaBetweenRuns;
|
|
138
141
|
firstRun;
|
|
139
142
|
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;
|
|
143
|
+
super(), this.name = t.name, this.world = e, this.deltaBetweenRuns = t.deltaBetweenRuns ?? 0, this.firstRun = t.firstRun !== void 0 && t.firstRun;
|
|
141
144
|
}
|
|
142
145
|
init() {}
|
|
143
146
|
clear() {
|
|
@@ -153,8 +156,10 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
153
156
|
shouldRun() {
|
|
154
157
|
return !0;
|
|
155
158
|
}
|
|
156
|
-
destroy() {
|
|
157
|
-
|
|
159
|
+
destroy() {
|
|
160
|
+
this.removeAllListeners();
|
|
161
|
+
}
|
|
162
|
+
}, x = class extends b {
|
|
158
163
|
remainingInstancesToRun = [];
|
|
159
164
|
remainingInstancesStartTime = null;
|
|
160
165
|
iterationsPerCheck;
|
|
@@ -182,8 +187,8 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
182
187
|
this.remainingInstancesToRun = [], this.remainingInstancesStartTime = null;
|
|
183
188
|
}
|
|
184
189
|
beforeRunIterables() {}
|
|
185
|
-
},
|
|
186
|
-
entities =
|
|
190
|
+
}, S = class extends x {
|
|
191
|
+
entities = /* @__PURE__ */ new Map();
|
|
187
192
|
options;
|
|
188
193
|
constructor(e, t = { name: "EntitySystem" }) {
|
|
189
194
|
t.iterationsPerCheck ||= 10, t.maxMsPerFrame ||= 4, super(e, t), this.options = t, e.on("entity-added", (e) => {
|
|
@@ -195,7 +200,10 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
195
200
|
});
|
|
196
201
|
}
|
|
197
202
|
getIterables() {
|
|
198
|
-
|
|
203
|
+
let e = [];
|
|
204
|
+
return this.entities.forEach((t) => {
|
|
205
|
+
t.components.entity.dead || e.push(t);
|
|
206
|
+
}), e;
|
|
199
207
|
}
|
|
200
208
|
updateIterable(e, t) {
|
|
201
209
|
e.components.entity.dead || this.updateEntity(e, t);
|
|
@@ -204,41 +212,20 @@ var r = Object.create, i = Object.defineProperty, a = Object.getOwnPropertyDescr
|
|
|
204
212
|
return !e.components.entity.isStatic;
|
|
205
213
|
}
|
|
206
214
|
isEntityInSystem(e) {
|
|
207
|
-
return this.entities.
|
|
215
|
+
return this.entities.has(e.eid);
|
|
208
216
|
}
|
|
209
217
|
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.
|
|
218
|
+
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
219
|
}
|
|
212
220
|
removeEntity(e) {
|
|
213
|
-
|
|
214
|
-
t !== -1 && this.entities.splice(t, 1);
|
|
221
|
+
this.entities.delete(e.eid);
|
|
215
222
|
}
|
|
216
223
|
shouldRun() {
|
|
217
|
-
return this.entities.
|
|
224
|
+
return this.entities.size > 0;
|
|
218
225
|
}
|
|
219
|
-
},
|
|
226
|
+
}, C = class {
|
|
220
227
|
onmessage(e, t = []) {}
|
|
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 {
|
|
228
|
+
}, w = class extends C {
|
|
242
229
|
updateFunction;
|
|
243
230
|
entities = [];
|
|
244
231
|
queryEntities = {};
|
|
@@ -248,59 +235,64 @@ var v = class extends g {
|
|
|
248
235
|
postMessage(e) {
|
|
249
236
|
if (e.type === "init") this.onMessageTyped({ type: "loaded" });
|
|
250
237
|
else if (e.type === "run") {
|
|
251
|
-
let t = [],
|
|
252
|
-
this.entities =
|
|
253
|
-
let
|
|
238
|
+
let t = performance.now(), n = [], r = {}, i = [];
|
|
239
|
+
this.entities = a(this.entities, e.entities);
|
|
240
|
+
let o = {};
|
|
254
241
|
Object.entries(e.queries).forEach(([e, t]) => {
|
|
255
|
-
let n =
|
|
256
|
-
this.queryEntities[e] = n,
|
|
242
|
+
let n = a(this.queryEntities[e] ?? [], t);
|
|
243
|
+
this.queryEntities[e] = n, o[e] = n;
|
|
257
244
|
});
|
|
258
|
-
let
|
|
259
|
-
entityComponentChanged(e,
|
|
260
|
-
|
|
245
|
+
let s = {
|
|
246
|
+
entityComponentChanged(e, t, r, i) {
|
|
247
|
+
n.push({
|
|
261
248
|
entityId: e,
|
|
262
249
|
event: "component-property-updated",
|
|
263
250
|
args: [
|
|
264
|
-
|
|
251
|
+
t,
|
|
265
252
|
r,
|
|
266
253
|
i
|
|
267
254
|
]
|
|
268
255
|
});
|
|
269
256
|
},
|
|
270
|
-
emitEntityEvent(e,
|
|
271
|
-
|
|
257
|
+
emitEntityEvent(e, t, ...r) {
|
|
258
|
+
n.push({
|
|
272
259
|
entityId: e,
|
|
273
|
-
event:
|
|
260
|
+
event: t,
|
|
274
261
|
args: r
|
|
275
262
|
});
|
|
276
263
|
},
|
|
264
|
+
emitSystemEvent(e, t) {
|
|
265
|
+
(r[e] ?? (r[e] = [])).push(t);
|
|
266
|
+
},
|
|
277
267
|
entityDied(e) {
|
|
278
|
-
|
|
268
|
+
n.push({
|
|
279
269
|
entityId: e,
|
|
280
270
|
event: "death",
|
|
281
271
|
args: []
|
|
282
272
|
});
|
|
283
273
|
},
|
|
284
274
|
createEntity(e) {
|
|
285
|
-
|
|
275
|
+
i.push(e);
|
|
286
276
|
}
|
|
287
277
|
};
|
|
288
|
-
if (this.updateFunction.preRun && this.updateFunction.preRun(e.world, this.entities,
|
|
289
|
-
this.updateFunction(e.world, t.entityId, t.components,
|
|
290
|
-
}), this.updateFunction.entityRemoved) for (let t of e.entities.removed) this.updateFunction.entityRemoved(e.world, t,
|
|
278
|
+
if (this.updateFunction.preRun && this.updateFunction.preRun(e.world, this.entities, o, s), this.entities.forEach((t) => {
|
|
279
|
+
this.updateFunction(e.world, t.entityId, t.components, o, s);
|
|
280
|
+
}), this.updateFunction.entityRemoved) for (let t of e.entities.removed) this.updateFunction.entityRemoved(e.world, t, s);
|
|
281
|
+
let c = performance.now() - t;
|
|
291
282
|
this.onMessageTyped({
|
|
292
283
|
type: "run-complete",
|
|
293
|
-
runTime:
|
|
294
|
-
events:
|
|
295
|
-
|
|
284
|
+
runTime: c,
|
|
285
|
+
events: n,
|
|
286
|
+
systemEvents: r,
|
|
287
|
+
created: i
|
|
296
288
|
});
|
|
297
289
|
}
|
|
298
290
|
}
|
|
299
291
|
onMessageTyped(e) {
|
|
300
292
|
this.onmessage({ data: e });
|
|
301
293
|
}
|
|
302
|
-
},
|
|
303
|
-
entities =
|
|
294
|
+
}, T = "___main", E = class extends b {
|
|
295
|
+
entities = /* @__PURE__ */ new Map();
|
|
304
296
|
options;
|
|
305
297
|
worker;
|
|
306
298
|
isWorkerThread;
|
|
@@ -311,28 +303,33 @@ var v = class extends g {
|
|
|
311
303
|
queryDeltas = {};
|
|
312
304
|
constructor(e, t) {
|
|
313
305
|
super(e, t), this.options = t, Object.keys(this.options.queries ?? {}).forEach((e) => {
|
|
314
|
-
this.queryEntities[e] =
|
|
306
|
+
this.queryEntities[e] = /* @__PURE__ */ new Map();
|
|
315
307
|
}), e.on("entity-added", (e) => {
|
|
316
308
|
this.checkAddEntity(e);
|
|
317
309
|
}), e.on("entity-removed", (e) => {
|
|
318
310
|
this.removeEntity(e);
|
|
319
311
|
}), e.entities.forEach((e) => {
|
|
320
312
|
this.checkAddEntity(e);
|
|
321
|
-
}), !t.forceMainThread && globalThis.Worker !== void 0 && globalThis.SharedArrayBuffer !== void 0 ? (this.worker = t.getWorker(), this.isWorkerThread = !0) : (this.worker = new
|
|
313
|
+
}), !t.forceMainThread && globalThis.Worker !== void 0 && globalThis.SharedArrayBuffer !== void 0 ? (this.worker = t.getWorker(), this.isWorkerThread = !0) : (this.worker = new w(t.updateFunction), this.isWorkerThread = !1), this.initWorker();
|
|
322
314
|
}
|
|
323
315
|
initWorker() {
|
|
324
316
|
this.worker.onmessage = (e) => {
|
|
325
317
|
let t = e.data;
|
|
326
|
-
t.type === "loaded"
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
318
|
+
if (t.type === "loaded") this.loaded = !0, this.loadingPromise &&= (this.loadingPromise.resolve(), null);
|
|
319
|
+
else if (t.type === "run-complete") {
|
|
320
|
+
this.isRunning = !1, this.world.emit(`system-${this.name}-worker-finished`, t.runTime);
|
|
321
|
+
for (let e of Object.keys(t.systemEvents)) this.emit(e, t.systemEvents[e]);
|
|
322
|
+
t.events.forEach((e) => {
|
|
323
|
+
let t = this.world.getEntityByEid(e.entityId);
|
|
324
|
+
if (!t) {
|
|
325
|
+
console.warn(`Could not find entity with id ${e.entityId}`);
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
t.emit(e.event, ...e.args);
|
|
329
|
+
}), t.created.forEach((e) => {
|
|
330
|
+
this.world.loadEntity(e);
|
|
331
|
+
}), this.world.emit(`system-${this.name}-worker-events-finished`, t.runTime);
|
|
332
|
+
}
|
|
336
333
|
}, this.worker.postMessage({ type: "init" });
|
|
337
334
|
}
|
|
338
335
|
init() {
|
|
@@ -353,7 +350,7 @@ var v = class extends g {
|
|
|
353
350
|
elapsedTime: e
|
|
354
351
|
};
|
|
355
352
|
this.addDataToWorld?.(t), this.isRunning = !0;
|
|
356
|
-
let n = this.buildQueryDelta(
|
|
353
|
+
let n = this.buildQueryDelta(T, this.options), r = {};
|
|
357
354
|
Object.entries(this.options.queries ?? {}).forEach(([e, t]) => {
|
|
358
355
|
r[e] = this.buildQueryDelta(e, t);
|
|
359
356
|
});
|
|
@@ -366,13 +363,17 @@ var v = class extends g {
|
|
|
366
363
|
this.worker.postMessage(i);
|
|
367
364
|
}
|
|
368
365
|
buildQueryDelta(e, t) {
|
|
369
|
-
let n = this.getQueryDelta(e), r =
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
366
|
+
let n = this.getQueryDelta(e), r = [];
|
|
367
|
+
n.added.forEach((e) => {
|
|
368
|
+
r.push({
|
|
369
|
+
entityId: e.eid,
|
|
370
|
+
components: this.buildComponents(e, t)
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
let i = Array.from(n.removed);
|
|
373
374
|
return this.queryDeltas[e] = {
|
|
374
|
-
added:
|
|
375
|
-
removed:
|
|
375
|
+
added: /* @__PURE__ */ new Set(),
|
|
376
|
+
removed: /* @__PURE__ */ new Set()
|
|
376
377
|
}, {
|
|
377
378
|
added: r,
|
|
378
379
|
removed: i
|
|
@@ -386,7 +387,7 @@ var v = class extends g {
|
|
|
386
387
|
}), n;
|
|
387
388
|
}
|
|
388
389
|
isEntityInSystem(e) {
|
|
389
|
-
return this.entities.
|
|
390
|
+
return this.entities.has(e.eid);
|
|
390
391
|
}
|
|
391
392
|
matchesQuery(e, t) {
|
|
392
393
|
return !(e.components.entity.dead || t.required.find((t) => !e.components[t]) || t.not?.find((t) => !!e.components[t]) || t.filter && !t.filter(e));
|
|
@@ -394,72 +395,39 @@ var v = class extends g {
|
|
|
394
395
|
getQueryDelta(e) {
|
|
395
396
|
let t = this.queryDeltas[e];
|
|
396
397
|
return t ||= this.queryDeltas[e] = {
|
|
397
|
-
added:
|
|
398
|
-
removed:
|
|
398
|
+
added: /* @__PURE__ */ new Set(),
|
|
399
|
+
removed: /* @__PURE__ */ new Set()
|
|
399
400
|
}, t;
|
|
400
401
|
}
|
|
401
402
|
markAdded(e, t) {
|
|
402
|
-
|
|
403
|
-
n !== -1 && e.removed.splice(n, 1), e.added.indexOf(t) === -1 && e.added.push(t);
|
|
403
|
+
e.removed.delete(t.eid), e.added.add(t);
|
|
404
404
|
}
|
|
405
405
|
markRemoved(e, t) {
|
|
406
|
-
|
|
407
|
-
if (n !== -1) {
|
|
408
|
-
e.added.splice(n, 1);
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
e.removed.indexOf(t.eid) === -1 && e.removed.push(t.eid);
|
|
406
|
+
e.added.delete(t) || e.removed.add(t.eid);
|
|
412
407
|
}
|
|
413
408
|
updateEntityList(e, t, n, r) {
|
|
414
|
-
let i =
|
|
415
|
-
r ? (
|
|
409
|
+
let i = this.getQueryDelta(e);
|
|
410
|
+
r ? (t.set(n.eid, n), this.markAdded(i, n)) : t.delete(n.eid) && this.markRemoved(i, n);
|
|
416
411
|
}
|
|
417
412
|
checkAddEntity(e) {
|
|
418
413
|
let t = this.matchesQuery(e, this.options);
|
|
419
|
-
return this.updateEntityList(
|
|
420
|
-
let r = this.queryEntities[t] ?? (this.queryEntities[t] =
|
|
414
|
+
return this.updateEntityList(T, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
|
|
415
|
+
let r = this.queryEntities[t] ?? (this.queryEntities[t] = /* @__PURE__ */ new Map());
|
|
421
416
|
this.updateEntityList(t, r, e, this.matchesQuery(e, n));
|
|
422
417
|
}), t;
|
|
423
418
|
}
|
|
424
419
|
removeEntity(e) {
|
|
425
|
-
this.updateEntityList(
|
|
420
|
+
this.updateEntityList(T, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
|
|
426
421
|
this.updateEntityList(t, n, e, !1);
|
|
427
422
|
});
|
|
428
423
|
}
|
|
429
424
|
shouldRun() {
|
|
430
|
-
return this.entities.
|
|
425
|
+
return this.entities.size > 0;
|
|
431
426
|
}
|
|
432
427
|
destroy() {
|
|
433
|
-
"terminate" in this.worker && this.worker.terminate();
|
|
434
|
-
}
|
|
435
|
-
}, x = 0, S = 1, C = {
|
|
436
|
-
type: Uint32Array,
|
|
437
|
-
size: 2,
|
|
438
|
-
loadProperties: ["type", "isStatic"],
|
|
439
|
-
load(e, t, n) {
|
|
440
|
-
let r = t.create([+!!n.dead, +!!n.isStatic]), i = t.getBlock(r);
|
|
441
|
-
return {
|
|
442
|
-
index: r,
|
|
443
|
-
type: n.type,
|
|
444
|
-
get dead() {
|
|
445
|
-
return i[0] === 1;
|
|
446
|
-
},
|
|
447
|
-
set dead(e) {
|
|
448
|
-
i[0] = +!!e;
|
|
449
|
-
},
|
|
450
|
-
get isStatic() {
|
|
451
|
-
return i[1] === 1;
|
|
452
|
-
},
|
|
453
|
-
set isStatic(e) {
|
|
454
|
-
i[1] = +!!e;
|
|
455
|
-
}
|
|
456
|
-
};
|
|
457
|
-
},
|
|
458
|
-
save(e) {
|
|
459
|
-
let t = {};
|
|
460
|
-
return e.type && (t.type = e.type), e.dead && (t.dead = !0), t;
|
|
428
|
+
super.destroy(), "terminate" in this.worker && this.worker.terminate();
|
|
461
429
|
}
|
|
462
|
-
},
|
|
430
|
+
}, D = class e extends v.default {
|
|
463
431
|
static eidCounter = 1;
|
|
464
432
|
eid;
|
|
465
433
|
config;
|
|
@@ -522,7 +490,7 @@ var v = class extends g {
|
|
|
522
490
|
i.loadInFinishLoading && i.loadProperties.some((e) => e in t) && this.loadComponent(r, e, !1);
|
|
523
491
|
}
|
|
524
492
|
}
|
|
525
|
-
},
|
|
493
|
+
}, O = class {
|
|
526
494
|
world;
|
|
527
495
|
configs;
|
|
528
496
|
constructor(e = {}) {
|
|
@@ -543,14 +511,13 @@ var v = class extends g {
|
|
|
543
511
|
return this.world.addEntity(n, t);
|
|
544
512
|
}
|
|
545
513
|
createEntity(e) {
|
|
546
|
-
return new
|
|
514
|
+
return new D(this.world, e);
|
|
547
515
|
}
|
|
548
|
-
},
|
|
516
|
+
}, k = c, A = class extends v.default {
|
|
549
517
|
heap;
|
|
550
518
|
registry;
|
|
551
519
|
factory;
|
|
552
|
-
entities =
|
|
553
|
-
entitiesByEid = {};
|
|
520
|
+
entities = /* @__PURE__ */ new Map();
|
|
554
521
|
systems = [];
|
|
555
522
|
gameTime = 0;
|
|
556
523
|
playerTime = 0;
|
|
@@ -558,25 +525,25 @@ var v = class extends g {
|
|
|
558
525
|
paused = !1;
|
|
559
526
|
destroyed = !1;
|
|
560
527
|
constructor(e, t = {}) {
|
|
561
|
-
super(), this.heap = new
|
|
562
|
-
let
|
|
528
|
+
super(), this.heap = new s({ bufferSize: t.heapSize ?? k });
|
|
529
|
+
let n = {
|
|
563
530
|
...e,
|
|
564
|
-
entity:
|
|
531
|
+
entity: r
|
|
565
532
|
}, i = {};
|
|
566
|
-
for (let e of Object.keys(
|
|
567
|
-
let t =
|
|
533
|
+
for (let e of Object.keys(n)) {
|
|
534
|
+
let t = n[e];
|
|
568
535
|
i[e] = {
|
|
569
536
|
...t,
|
|
570
|
-
memoryComponent: new
|
|
537
|
+
memoryComponent: new y(this.heap, t.type, t.size)
|
|
571
538
|
};
|
|
572
539
|
}
|
|
573
|
-
this.registry = i, this.factory = t.factory ?? new
|
|
540
|
+
this.registry = i, this.factory = t.factory ?? new O(), this.factory.world = this;
|
|
574
541
|
}
|
|
575
542
|
async init() {
|
|
576
543
|
await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
|
|
577
544
|
}
|
|
578
545
|
addEntity(e, t = !0) {
|
|
579
|
-
return this.entities.
|
|
546
|
+
return this.entities.set(e.eid, e), e.world = this, e.on("component-added", (t) => {
|
|
580
547
|
this.addEntityToComponentSystem(e, t);
|
|
581
548
|
}), e.on("component-removed", (t) => {
|
|
582
549
|
this.removeEntityFromComponentSystem(e, t);
|
|
@@ -588,7 +555,7 @@ var v = class extends g {
|
|
|
588
555
|
return this.factory.loadEntity(e, t);
|
|
589
556
|
}
|
|
590
557
|
load(e) {
|
|
591
|
-
for (let e of this.entities.
|
|
558
|
+
for (let e of Array.from(this.entities.values())) this.removeEntity(e);
|
|
592
559
|
this.systems.forEach((e) => e.clear());
|
|
593
560
|
let t = e.entities.map((e) => this.loadEntity(e, !1));
|
|
594
561
|
for (let e of t) e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
|
|
@@ -597,14 +564,13 @@ var v = class extends g {
|
|
|
597
564
|
this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1;
|
|
598
565
|
}
|
|
599
566
|
removeEntity(e) {
|
|
600
|
-
|
|
601
|
-
t !== -1 && (this.entities.splice(t, 1), delete this.entitiesByEid[e.eid], this.emit("entity-removed", e)), e.deleteAllComponentMemory();
|
|
567
|
+
this.entities.delete(e.eid) && this.emit("entity-removed", e), e.deleteAllComponentMemory();
|
|
602
568
|
}
|
|
603
569
|
onEntityDied(e) {
|
|
604
570
|
this.removeEntity(e);
|
|
605
571
|
}
|
|
606
572
|
getEntityByEid(e) {
|
|
607
|
-
return this.
|
|
573
|
+
return this.entities.get(e);
|
|
608
574
|
}
|
|
609
575
|
addSystem(e) {
|
|
610
576
|
return this.systems.push(e), this.emit("system-added", e), e;
|
|
@@ -652,12 +618,12 @@ var v = class extends g {
|
|
|
652
618
|
}
|
|
653
619
|
addEntityToComponentSystem(e, t) {
|
|
654
620
|
this.systems.forEach((n) => {
|
|
655
|
-
(n instanceof
|
|
621
|
+
(n instanceof S && n.options.components?.includes(t) || n instanceof E && this.componentAffectsComponentSystem(n, t)) && n.checkAddEntity(e);
|
|
656
622
|
});
|
|
657
623
|
}
|
|
658
624
|
removeEntityFromComponentSystem(e, t) {
|
|
659
625
|
this.systems.forEach((n) => {
|
|
660
|
-
n instanceof
|
|
626
|
+
n instanceof S && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof E && this.componentAffectsComponentSystem(n, t) && n.checkAddEntity(e);
|
|
661
627
|
});
|
|
662
628
|
}
|
|
663
629
|
componentAffectsComponentSystem(e, t) {
|
|
@@ -672,30 +638,19 @@ var v = class extends g {
|
|
|
672
638
|
};
|
|
673
639
|
//#endregion
|
|
674
640
|
//#region src/actions/kill-entity.ts
|
|
675
|
-
function
|
|
641
|
+
function j(e) {
|
|
676
642
|
e.components.entity.dead = !0, e.emit("death");
|
|
677
643
|
}
|
|
678
644
|
//#endregion
|
|
679
|
-
//#region src/actions/kill-entity-worker.ts
|
|
680
|
-
function k(e, t, n) {
|
|
681
|
-
let r = t.entity;
|
|
682
|
-
r && (r[0] = 1), n.entityDied(e);
|
|
683
|
-
}
|
|
684
|
-
//#endregion
|
|
685
|
-
//#region src/actions/create-entity-worker.ts
|
|
686
|
-
function A(e, t) {
|
|
687
|
-
t.createEntity(e);
|
|
688
|
-
}
|
|
689
|
-
//#endregion
|
|
690
645
|
//#region src/performance-timing.ts
|
|
691
|
-
var
|
|
646
|
+
var M = 1e3, N = {
|
|
692
647
|
avg: 0,
|
|
693
648
|
min: 0,
|
|
694
649
|
max: 0,
|
|
695
650
|
samples: 0
|
|
696
651
|
};
|
|
697
|
-
function
|
|
698
|
-
if (!e.length) return { ...
|
|
652
|
+
function P(e) {
|
|
653
|
+
if (!e.length) return { ...N };
|
|
699
654
|
let t = 0, n = Infinity, r = 0;
|
|
700
655
|
for (let i of e) t += i, i < n && (n = i), i > r && (r = i);
|
|
701
656
|
return {
|
|
@@ -705,13 +660,13 @@ function N(e) {
|
|
|
705
660
|
samples: e.length
|
|
706
661
|
};
|
|
707
662
|
}
|
|
708
|
-
var
|
|
663
|
+
var F = class extends v.default {
|
|
709
664
|
world;
|
|
710
665
|
ticksBetweenUpdates;
|
|
711
666
|
stats = {
|
|
712
|
-
update: { ...
|
|
667
|
+
update: { ...N },
|
|
713
668
|
systems: [],
|
|
714
|
-
events: { ...
|
|
669
|
+
events: { ...N }
|
|
715
670
|
};
|
|
716
671
|
ticks = 0;
|
|
717
672
|
updateStart = 0;
|
|
@@ -728,9 +683,9 @@ var P = class extends d.default {
|
|
|
728
683
|
this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
|
|
729
684
|
e.run = [], e.events = [], e.eventStart = -1;
|
|
730
685
|
}), this.stats = {
|
|
731
|
-
update: { ...
|
|
686
|
+
update: { ...N },
|
|
732
687
|
systems: [],
|
|
733
|
-
events: { ...
|
|
688
|
+
events: { ...N }
|
|
734
689
|
};
|
|
735
690
|
}
|
|
736
691
|
destroy() {
|
|
@@ -772,14 +727,14 @@ var P = class extends d.default {
|
|
|
772
727
|
let t = this.systemTimings.get(e.name);
|
|
773
728
|
return {
|
|
774
729
|
name: e.name,
|
|
775
|
-
run:
|
|
776
|
-
events:
|
|
730
|
+
run: P(t?.run ?? []),
|
|
731
|
+
events: P(t?.events ?? [])
|
|
777
732
|
};
|
|
778
|
-
}), t = { ...
|
|
733
|
+
}), t = { ...N };
|
|
779
734
|
e.forEach((e) => {
|
|
780
735
|
t.avg += e.events.avg, t.min += e.events.min, t.max += e.events.max, t.samples += e.events.samples;
|
|
781
736
|
}), this.stats = {
|
|
782
|
-
update:
|
|
737
|
+
update: P(this.updateTimes),
|
|
783
738
|
systems: e,
|
|
784
739
|
events: t
|
|
785
740
|
}, this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
|
|
@@ -788,67 +743,6 @@ var P = class extends d.default {
|
|
|
788
743
|
}
|
|
789
744
|
};
|
|
790
745
|
//#endregion
|
|
791
|
-
|
|
792
|
-
function F(e, t) {
|
|
793
|
-
let n = [], r = {};
|
|
794
|
-
e.onmessage = function(i) {
|
|
795
|
-
let a = i.data;
|
|
796
|
-
if (a.type === "init") I(e, { type: "loaded" });
|
|
797
|
-
else if (a.type === "run") {
|
|
798
|
-
let i = performance.now(), o = [], s = [];
|
|
799
|
-
n = _(n, a.entities);
|
|
800
|
-
let c = {};
|
|
801
|
-
Object.entries(a.queries).forEach(([e, t]) => {
|
|
802
|
-
let n = _(r[e] ?? [], t);
|
|
803
|
-
r[e] = n, c[e] = n;
|
|
804
|
-
});
|
|
805
|
-
let l = {
|
|
806
|
-
entityComponentChanged(e, t, n, r) {
|
|
807
|
-
o.push({
|
|
808
|
-
entityId: e,
|
|
809
|
-
event: "component-property-updated",
|
|
810
|
-
args: [
|
|
811
|
-
t,
|
|
812
|
-
n,
|
|
813
|
-
r
|
|
814
|
-
]
|
|
815
|
-
});
|
|
816
|
-
},
|
|
817
|
-
emitEntityEvent(e, t, ...n) {
|
|
818
|
-
o.push({
|
|
819
|
-
entityId: e,
|
|
820
|
-
event: t,
|
|
821
|
-
args: n
|
|
822
|
-
});
|
|
823
|
-
},
|
|
824
|
-
entityDied(e) {
|
|
825
|
-
o.push({
|
|
826
|
-
entityId: e,
|
|
827
|
-
event: "death",
|
|
828
|
-
args: []
|
|
829
|
-
});
|
|
830
|
-
},
|
|
831
|
-
createEntity(e) {
|
|
832
|
-
s.push(e);
|
|
833
|
-
}
|
|
834
|
-
};
|
|
835
|
-
t.preRun && t.preRun(a.world, n, c, l), n.forEach((e) => {
|
|
836
|
-
t(a.world, e.entityId, e.components, c, l);
|
|
837
|
-
}), t.entityRemoved && a.entities.removed.forEach((e) => {
|
|
838
|
-
t.entityRemoved(a.world, e, l);
|
|
839
|
-
}), I(e, {
|
|
840
|
-
type: "run-complete",
|
|
841
|
-
runTime: performance.now() - i,
|
|
842
|
-
events: o,
|
|
843
|
-
created: s
|
|
844
|
-
});
|
|
845
|
-
}
|
|
846
|
-
};
|
|
847
|
-
}
|
|
848
|
-
function I(e, t) {
|
|
849
|
-
e.postMessage(t);
|
|
850
|
-
}
|
|
851
|
-
//#endregion
|
|
852
|
-
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 };
|
|
746
|
+
export { D as BaseEntity, A as BaseWorld, E as ComponentSystem, w as ComponentWebWorker, t as DEAD_INDEX, M as DEFAULT_TICKS_BETWEEN_UPDATES, O as EntityFactory, S as EntitySystem, x as IterableSystem, y as MemoryComponent, F as PerformanceTiming, e as STATIC_INDEX, b as System, C as WebWorker, o as createComponentWorker, n as createEntityWorker, r as entityDefinition, j as killEntity, i as killEntityWorker };
|
|
853
747
|
|
|
854
748
|
//# sourceMappingURL=index.js.map
|