@daneren2005/shared-memory-ecs 1.0.0 → 1.1.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/dist/index.js CHANGED
@@ -35,58 +35,7 @@ var i = class {
35
35
  clear() {
36
36
  this.pool.clear();
37
37
  }
38
- }, a = class t extends e {
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 {
38
+ }, a = class {
90
39
  world;
91
40
  name;
92
41
  currentDelta = 0;
@@ -110,7 +59,7 @@ var i = class {
110
59
  return !0;
111
60
  }
112
61
  destroy() {}
113
- }, s = class extends o {
62
+ }, o = class extends a {
114
63
  remainingInstancesToRun = [];
115
64
  remainingInstancesStartTime = null;
116
65
  iterationsPerCheck;
@@ -138,7 +87,7 @@ var i = class {
138
87
  this.remainingInstancesToRun = [], this.remainingInstancesStartTime = null;
139
88
  }
140
89
  beforeRunIterables() {}
141
- }, c = class extends s {
90
+ }, s = class extends o {
142
91
  entities = [];
143
92
  options;
144
93
  constructor(e, t = { name: "EntitySystem" }) {
@@ -151,13 +100,13 @@ var i = class {
151
100
  });
152
101
  }
153
102
  getIterables() {
154
- return this.entities.filter((e) => !e.dead);
103
+ return this.entities.filter((e) => !e.components.entity.dead);
155
104
  }
156
105
  updateIterable(e, t) {
157
- e.dead || this.updateEntity(e, t);
106
+ e.components.entity.dead || this.updateEntity(e, t);
158
107
  }
159
108
  filterEntity(e) {
160
- return !e.isStatic;
109
+ return !e.components.entity.isStatic;
161
110
  }
162
111
  isEntityInSystem(e) {
163
112
  return this.entities.indexOf(e) !== -1;
@@ -172,41 +121,83 @@ var i = class {
172
121
  shouldRun() {
173
122
  return this.entities.length > 0;
174
123
  }
175
- }, l = class {
124
+ }, c = class {
176
125
  onmessage(e, t = []) {}
177
- }, u = class extends l {
126
+ };
127
+ //#endregion
128
+ //#region src/systems/workers/apply-query-delta.ts
129
+ function l(e, t) {
130
+ if (t.removed.length) {
131
+ let n = new Set(t.removed);
132
+ e = e.filter((e) => !n.has(e.entityId));
133
+ }
134
+ if (t.added.length) {
135
+ let n = /* @__PURE__ */ new Map();
136
+ e.forEach((e, t) => n.set(e.entityId, t));
137
+ for (let r of t.added) {
138
+ let t = n.get(r.entityId);
139
+ t === void 0 ? (n.set(r.entityId, e.length), e.push(r)) : e[t] = r;
140
+ }
141
+ }
142
+ return e;
143
+ }
144
+ //#endregion
145
+ //#region src/systems/workers/component-web-worker.ts
146
+ var u = class extends c {
178
147
  updateFunction;
148
+ entities = [];
149
+ queryEntities = {};
179
150
  constructor(e) {
180
151
  super(), this.updateFunction = e;
181
152
  }
182
153
  postMessage(e) {
183
154
  if (e.type === "init") this.onMessageTyped({ type: "loaded" });
184
155
  else if (e.type === "run") {
185
- let t = [], n = { entityComponentChanged(e, n, r, i) {
186
- t.push({
187
- entityId: e,
188
- event: "component-property-updated",
189
- args: [
190
- n,
191
- r,
192
- i
193
- ]
194
- });
195
- } };
196
- if (this.updateFunction.preRun && this.updateFunction.preRun(e.world, e.entities, e.queries, n), e.entities.forEach((t) => {
197
- typeof t != "number" && this.updateFunction(e.world, t.entityId, t.components, e.queries, n);
198
- }), this.updateFunction.entityRemoved) for (let t of e.removed) this.updateFunction.entityRemoved(e.world, t, n);
156
+ let t = [], n = [];
157
+ this.entities = l(this.entities, e.entities);
158
+ let r = {};
159
+ Object.entries(e.queries).forEach(([e, t]) => {
160
+ let n = l(this.queryEntities[e] ?? [], t);
161
+ this.queryEntities[e] = n, r[e] = n;
162
+ });
163
+ let i = {
164
+ entityComponentChanged(e, n, r, i) {
165
+ t.push({
166
+ entityId: e,
167
+ event: "component-property-updated",
168
+ args: [
169
+ n,
170
+ r,
171
+ i
172
+ ]
173
+ });
174
+ },
175
+ entityDied(e) {
176
+ t.push({
177
+ entityId: e,
178
+ event: "death",
179
+ args: []
180
+ });
181
+ },
182
+ createEntity(e) {
183
+ n.push(e);
184
+ }
185
+ };
186
+ if (this.updateFunction.preRun && this.updateFunction.preRun(e.world, this.entities, r, i), this.entities.forEach((t) => {
187
+ this.updateFunction(e.world, t.entityId, t.components, r, i);
188
+ }), this.updateFunction.entityRemoved) for (let t of e.entities.removed) this.updateFunction.entityRemoved(e.world, t, i);
199
189
  this.onMessageTyped({
200
190
  type: "run-complete",
201
191
  runTime: 0,
202
- events: t
192
+ events: t,
193
+ created: n
203
194
  });
204
195
  }
205
196
  }
206
197
  onMessageTyped(e) {
207
198
  this.onmessage({ data: e });
208
199
  }
209
- }, d = "___main", f = class extends o {
200
+ }, d = "___main", f = class extends a {
210
201
  entities = [];
211
202
  options;
212
203
  worker;
@@ -214,9 +205,8 @@ var i = class {
214
205
  loaded = !1;
215
206
  loadingPromise = null;
216
207
  isRunning = !1;
217
- queryEntityComponentCache = {};
218
208
  queryEntities = {};
219
- removedEntityBuffer = [];
209
+ queryDeltas = {};
220
210
  constructor(e, t) {
221
211
  super(e, t), this.options = t, Object.keys(this.options.queries ?? {}).forEach((e) => {
222
212
  this.queryEntities[e] = [];
@@ -232,13 +222,15 @@ var i = class {
232
222
  this.worker.onmessage = (e) => {
233
223
  let t = e.data;
234
224
  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.queryEntityComponentCache[d][e.entityId]?.entity ?? this.entities.find((t) => t.eid === e.entityId);
225
+ let t = this.world.getEntityByEid(e.entityId);
236
226
  if (!t) {
237
227
  console.warn(`Could not find entity with id ${e.entityId}`);
238
228
  return;
239
229
  }
240
230
  t.emit(e.event, ...e.args);
241
- }));
231
+ }), t.created.forEach((e) => {
232
+ this.world.loadEntity(e);
233
+ }), this.isWorkerThread && this.world.emit(`system-${this.name}-worker-events-finished`, t.runTime));
242
234
  }, this.worker.postMessage({ type: "init" });
243
235
  }
244
236
  init() {
@@ -259,64 +251,78 @@ var i = class {
259
251
  elapsedTime: e
260
252
  };
261
253
  this.addDataToWorld?.(t), this.isRunning = !0;
262
- let n = this.runQuery(d, this.entities, this.options), r = {};
254
+ let n = this.buildQueryDelta(d, this.options), r = {};
263
255
  Object.entries(this.options.queries ?? {}).forEach(([e, t]) => {
264
- r[e] = this.runQuery(e, this.queryEntities[e] ?? [], t);
256
+ r[e] = this.buildQueryDelta(e, t);
265
257
  });
266
258
  let i = {
267
259
  type: "run",
268
260
  world: t,
269
261
  entities: n,
270
- queries: r,
271
- removed: this.removedEntityBuffer
262
+ queries: r
272
263
  };
273
- this.worker.postMessage(i), this.removedEntityBuffer = [];
274
- }
275
- runQuery(e, t, n) {
276
- let r = [], i = this.queryEntityComponentCache[e];
277
- return i ||= this.queryEntityComponentCache[e] = {}, t.forEach((e) => {
278
- if (e.dead) return;
279
- let t = i[e.eid]?.components;
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;
264
+ this.worker.postMessage(i);
265
+ }
266
+ buildQueryDelta(e, t) {
267
+ let n = this.getQueryDelta(e), r = n.added.map((e) => ({
268
+ entityId: e.eid,
269
+ components: this.buildComponents(e, t)
270
+ })), i = n.removed;
271
+ return this.queryDeltas[e] = {
272
+ added: [],
273
+ removed: []
274
+ }, {
275
+ added: r,
276
+ removed: i
277
+ };
278
+ }
279
+ buildComponents(e, t) {
280
+ let n = {}, r = this.world.registry;
281
+ return [...t.required, ...t.optional ?? []].forEach((t) => {
282
+ let i = e.components[t], a = r[t].memoryComponent;
283
+ i && a && (n[t] = a.getBlock(i.index));
284
+ }), n;
294
285
  }
295
286
  isEntityInSystem(e) {
296
287
  return this.entities.indexOf(e) !== -1;
297
288
  }
298
289
  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));
290
+ return !(e.components.entity.dead || t.required.find((t) => !e.components[t]) || t.not?.find((t) => !!e.components[t]) || t.filter && !t.filter(e));
291
+ }
292
+ getQueryDelta(e) {
293
+ let t = this.queryDeltas[e];
294
+ return t ||= this.queryDeltas[e] = {
295
+ added: [],
296
+ removed: []
297
+ }, t;
298
+ }
299
+ markAdded(e, t) {
300
+ let n = e.removed.indexOf(t.eid);
301
+ n !== -1 && e.removed.splice(n, 1), e.added.indexOf(t) === -1 && e.added.push(t);
302
+ }
303
+ markRemoved(e, t) {
304
+ let n = e.added.indexOf(t);
305
+ if (n !== -1) {
306
+ e.added.splice(n, 1);
307
+ return;
308
+ }
309
+ e.removed.indexOf(t.eid) === -1 && e.removed.push(t.eid);
300
310
  }
301
- updateEntityList(e, t, n) {
302
- let r = e.indexOf(t);
303
- n ? r === -1 && e.push(t) : r !== -1 && e.splice(r, 1);
311
+ updateEntityList(e, t, n, r) {
312
+ let i = t.indexOf(n), a = this.getQueryDelta(e);
313
+ r ? (i === -1 && t.push(n), this.markAdded(a, n)) : i !== -1 && (t.splice(i, 1), this.markRemoved(a, n));
304
314
  }
305
315
  checkAddEntity(e) {
306
316
  let t = this.matchesQuery(e, this.options);
307
- return this.updateEntityList(this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
317
+ return this.updateEntityList(d, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
308
318
  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];
319
+ this.updateEntityList(t, r, e, this.matchesQuery(e, n));
312
320
  }), t;
313
321
  }
314
322
  removeEntity(e) {
315
- this.updateEntityList(this.entities, e, !1), Object.values(this.queryEntities).forEach((t) => {
316
- this.updateEntityList(t, e, !1);
317
- }), Object.values(this.queryEntityComponentCache).forEach((t) => {
318
- t[e.eid] && delete t[e.eid];
319
- }), this.removedEntityBuffer.push(e.eid);
323
+ this.updateEntityList(d, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
324
+ this.updateEntityList(t, n, e, !1);
325
+ });
320
326
  }
321
327
  shouldRun() {
322
328
  return this.entities.length > 0;
@@ -324,23 +330,145 @@ var i = class {
324
330
  destroy() {
325
331
  "terminate" in this.worker && this.worker.terminate();
326
332
  }
327
- }, p = n, m = class extends e {
333
+ }, p = 0, m = 1, h = {
334
+ type: Uint32Array,
335
+ size: 2,
336
+ loadProperties: ["type", "isStatic"],
337
+ load(e, t, n) {
338
+ let r = t.create([+!!n.dead, +!!n.isStatic]), i = t.getBlock(r);
339
+ return {
340
+ index: r,
341
+ type: n.type,
342
+ get dead() {
343
+ return i[0] === 1;
344
+ },
345
+ set dead(e) {
346
+ i[0] = +!!e;
347
+ },
348
+ get isStatic() {
349
+ return i[1] === 1;
350
+ },
351
+ set isStatic(e) {
352
+ i[1] = +!!e;
353
+ }
354
+ };
355
+ },
356
+ save(e) {
357
+ let t = {};
358
+ return e.type && (t.type = e.type), e.dead && (t.dead = !0), t;
359
+ }
360
+ }, g = class t extends e {
361
+ static eidCounter = 1;
362
+ eid;
363
+ config;
364
+ world;
365
+ components = {};
366
+ constructor(e, n) {
367
+ super(), this.world = e, this.eid = t.eidCounter++, this.loadComponent("entity", n ?? {}, !1), n && this.load(n);
368
+ }
369
+ loadComponent(e, t, n = !0) {
370
+ let r = this.world.registry[e], i = r.memoryComponent, a = r.load(this, i, t);
371
+ return this.components[e] = a, n && this.emit("component-added", e, a), a;
372
+ }
373
+ removeComponent(e) {
374
+ let t = this.components[e];
375
+ t && (this.world.registry[e].memoryComponent.delete(t.index), delete this.components[e], this.emit("component-removed", e));
376
+ }
377
+ setComponent(e, t, n) {
378
+ let r = this.components[e];
379
+ r && (r[t] = n, this.emit("component-property-updated", e, t, n));
380
+ }
381
+ setComponentBulk(e, t) {
382
+ let n = this.components[e];
383
+ n && Object.assign(n, t);
384
+ }
385
+ deleteComponent(e, t) {
386
+ let n = this.components[e];
387
+ n && (delete n[t], this.emit("component-property-deleted", e, t));
388
+ }
389
+ deleteAllComponentMemory() {
390
+ let e = this.world.registry;
391
+ for (let t of Object.keys(this.components)) {
392
+ let n = this.components[t];
393
+ n && e[t].memoryComponent.delete(n.index);
394
+ }
395
+ }
396
+ load(e) {
397
+ let t = e, n = this.world.registry;
398
+ for (let r of Object.keys(n)) {
399
+ if (r === "entity") continue;
400
+ let i = n[r];
401
+ i.loadInFinishLoading || i.loadProperties.some((e) => e in t) && this.loadComponent(r, e, !1);
402
+ }
403
+ this.config = e;
404
+ }
405
+ save() {
406
+ let e = {}, t = this.world.registry, n = this.components;
407
+ for (let r of Object.keys(n)) {
408
+ let i = t[r], a = n[r];
409
+ i.save && a && Object.assign(e, i.save(a));
410
+ }
411
+ return e;
412
+ }
413
+ finishLoading() {
414
+ let e = this.config;
415
+ if (!e) return;
416
+ let t = e, n = this.world.registry;
417
+ for (let r of Object.keys(n)) {
418
+ if (r === "entity") continue;
419
+ let i = n[r];
420
+ i.loadInFinishLoading && i.loadProperties.some((e) => e in t) && this.loadComponent(r, e, !1);
421
+ }
422
+ }
423
+ }, _ = class {
424
+ world;
425
+ configs;
426
+ constructor(e = {}) {
427
+ this.configs = e;
428
+ }
429
+ register(e, t) {
430
+ this.configs[e] = t;
431
+ }
432
+ getConfig(e) {
433
+ let t = e?.type, n = t ? this.configs[t] : void 0;
434
+ return n ? {
435
+ ...n,
436
+ ...e
437
+ } : e;
438
+ }
439
+ loadEntity(e, t = !0) {
440
+ let n = this.createEntity(this.getConfig(e));
441
+ return this.world.addEntity(n, t);
442
+ }
443
+ createEntity(e) {
444
+ return new g(this.world, e);
445
+ }
446
+ }, v = n, y = class extends e {
328
447
  heap;
329
448
  registry;
330
- components;
449
+ factory;
331
450
  entities = [];
332
451
  entitiesByEid = {};
333
452
  systems = [];
334
453
  gameTime = 0;
454
+ playerTime = 0;
455
+ timeScale = 1;
456
+ paused = !1;
335
457
  destroyed = !1;
336
458
  constructor(e, t = {}) {
337
- super(), this.registry = e, this.heap = new r({ bufferSize: t.heapSize ?? p });
338
- let n = {};
339
- for (let t of Object.keys(e)) {
340
- let r = e[t];
341
- n[t] = new i(this.heap, r.type, r.size);
459
+ super(), this.heap = new r({ bufferSize: t.heapSize ?? v });
460
+ let n = {
461
+ ...e,
462
+ entity: h
463
+ }, a = {};
464
+ for (let e of Object.keys(n)) {
465
+ let t = n[e];
466
+ a[e] = {
467
+ ...t,
468
+ memoryComponent: new i(this.heap, t.type, t.size)
469
+ };
342
470
  }
343
- this.components = n;
471
+ this.registry = a, this.factory = t.factory ?? new _(), this.factory.world = this;
344
472
  }
345
473
  async init() {
346
474
  await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
@@ -350,15 +478,29 @@ var i = class {
350
478
  this.addEntityToComponentSystem(e, t);
351
479
  }), e.on("component-removed", (t) => {
352
480
  this.removeEntityFromComponentSystem(e, t);
353
- }), t && e.finishLoading(), this.emit("entity-added", e), e;
481
+ }), t && (e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
482
+ this.onEntityDied(e);
483
+ })), e;
354
484
  }
355
485
  loadEntity(e, t = !0) {
356
- return this.addEntity(new a(this, e), t);
486
+ return this.factory.loadEntity(e, t);
487
+ }
488
+ load(e) {
489
+ for (let e of this.entities.slice()) this.removeEntity(e);
490
+ this.systems.forEach((e) => e.clear());
491
+ let t = e.entities.map((e) => this.loadEntity(e, !1));
492
+ for (let e of t) e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
493
+ this.onEntityDied(e);
494
+ });
495
+ this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1;
357
496
  }
358
497
  removeEntity(e) {
359
498
  let t = this.entities.indexOf(e);
360
499
  t !== -1 && (this.entities.splice(t, 1), delete this.entitiesByEid[e.eid], this.emit("entity-removed", e)), e.deleteAllComponentMemory();
361
500
  }
501
+ onEntityDied(e) {
502
+ this.removeEntity(e);
503
+ }
362
504
  getEntityByEid(e) {
363
505
  return this.entitiesByEid[e];
364
506
  }
@@ -373,7 +515,8 @@ var i = class {
373
515
  t !== -1 && this.systems.splice(t, 1);
374
516
  }
375
517
  update(e) {
376
- this.gameTime += e;
518
+ if (this.playerTime += e, this.paused) return {};
519
+ e = this.timeScale * e, this.gameTime += e;
377
520
  let t = null;
378
521
  return this.systems.forEach((n) => {
379
522
  let r = !0, i = !1, a = !1;
@@ -391,14 +534,20 @@ var i = class {
391
534
  });
392
535
  }), { lastSystemError: t };
393
536
  }
537
+ pause() {
538
+ this.paused = !0;
539
+ }
540
+ resume() {
541
+ this.paused = !1;
542
+ }
394
543
  addEntityToComponentSystem(e, t) {
395
544
  this.systems.forEach((n) => {
396
- (n instanceof c && n.options.components?.includes(t) || n instanceof f && this.componentAffectsComponentSystem(n, t)) && n.checkAddEntity(e);
545
+ (n instanceof s && n.options.components?.includes(t) || n instanceof f && this.componentAffectsComponentSystem(n, t)) && n.checkAddEntity(e);
397
546
  });
398
547
  }
399
548
  removeEntityFromComponentSystem(e, t) {
400
549
  this.systems.forEach((n) => {
401
- n instanceof c && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof f && this.componentAffectsComponentSystem(n, t) && n.checkAddEntity(e);
550
+ n instanceof s && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof f && this.componentAffectsComponentSystem(n, t) && n.checkAddEntity(e);
402
551
  });
403
552
  }
404
553
  componentAffectsComponentSystem(e, t) {
@@ -412,61 +561,76 @@ var i = class {
412
561
  }
413
562
  };
414
563
  //#endregion
564
+ //#region src/actions/kill-entity.ts
565
+ function b(e) {
566
+ e.components.entity.dead = !0, e.emit("death");
567
+ }
568
+ //#endregion
569
+ //#region src/actions/kill-entity-worker.ts
570
+ function x(e, t, n) {
571
+ let r = t.entity;
572
+ r && (r[0] = 1), n.entityDied(e);
573
+ }
574
+ //#endregion
575
+ //#region src/actions/create-entity-worker.ts
576
+ function S(e, t) {
577
+ t.createEntity(e);
578
+ }
579
+ //#endregion
415
580
  //#region src/systems/workers/create-component-worker.ts
416
- function h(e) {
417
- let t = {}, n = {};
418
- self.onmessage = function(r) {
419
- let i = r.data;
420
- if (i.type === "init") g({ type: "loaded" });
421
- else if (i.type === "run") {
422
- let r = performance.now(), a = [], o = {};
423
- Object.entries(i.queries).forEach(([e, t]) => {
424
- let r = n[e];
425
- r ||= n[e] = {}, o[e] = t.map((e) => {
426
- let t, n;
427
- return typeof e == "number" ? (t = r[e], n = e) : (r[e.entityId] = t = e.components, n = e.entityId), {
428
- entityId: n,
429
- components: t
430
- };
431
- });
581
+ function C(e, t) {
582
+ let n = [], r = {};
583
+ e.onmessage = function(i) {
584
+ let a = i.data;
585
+ if (a.type === "init") w(e, { type: "loaded" });
586
+ else if (a.type === "run") {
587
+ let i = performance.now(), o = [], s = [];
588
+ n = l(n, a.entities);
589
+ let c = {};
590
+ Object.entries(a.queries).forEach(([e, t]) => {
591
+ let n = l(r[e] ?? [], t);
592
+ r[e] = n, c[e] = n;
432
593
  });
433
- let s = [];
434
- for (let e of i.entities) {
435
- let n, r;
436
- typeof e == "number" ? (n = t[e], r = e) : (t[e.entityId] = n = e.components, r = e.entityId), s.push({
437
- entityId: r,
438
- components: n
439
- });
440
- }
441
- let c = { entityComponentChanged(e, t, n, r) {
442
- a.push({
443
- entityId: e,
444
- event: "component-property-updated",
445
- args: [
446
- t,
447
- n,
448
- r
449
- ]
450
- });
451
- } };
452
- e.preRun && e.preRun(i.world, s, o, c), s.forEach((t) => {
453
- e(i.world, t.entityId, t.components, o, c);
454
- }), i.removed.forEach((r) => {
455
- delete t[r], Object.values(n).forEach((e) => {
456
- delete e[r];
457
- }), e.entityRemoved && e.entityRemoved(i.world, r, c);
458
- }), g({
594
+ let u = {
595
+ entityComponentChanged(e, t, n, r) {
596
+ o.push({
597
+ entityId: e,
598
+ event: "component-property-updated",
599
+ args: [
600
+ t,
601
+ n,
602
+ r
603
+ ]
604
+ });
605
+ },
606
+ entityDied(e) {
607
+ o.push({
608
+ entityId: e,
609
+ event: "death",
610
+ args: []
611
+ });
612
+ },
613
+ createEntity(e) {
614
+ s.push(e);
615
+ }
616
+ };
617
+ t.preRun && t.preRun(a.world, n, c, u), n.forEach((e) => {
618
+ t(a.world, e.entityId, e.components, c, u);
619
+ }), t.entityRemoved && a.entities.removed.forEach((e) => {
620
+ t.entityRemoved(a.world, e, u);
621
+ }), w(e, {
459
622
  type: "run-complete",
460
- runTime: performance.now() - r,
461
- events: a
623
+ runTime: performance.now() - i,
624
+ events: o,
625
+ created: s
462
626
  });
463
627
  }
464
628
  };
465
629
  }
466
- function g(e) {
467
- self.postMessage(e);
630
+ function w(e, t) {
631
+ e.postMessage(t);
468
632
  }
469
633
  //#endregion
470
- export { a as BaseEntity, m as BaseWorld, f as ComponentSystem, u as ComponentWebWorker, c as EntitySystem, s as IterableSystem, i as MemoryComponent, o as System, l as WebWorker, h as createComponentWorker };
634
+ export { g as BaseEntity, y as BaseWorld, f as ComponentSystem, u as ComponentWebWorker, p as DEAD_INDEX, _ as EntityFactory, s as EntitySystem, o as IterableSystem, i as MemoryComponent, m as STATIC_INDEX, a as System, c as WebWorker, C as createComponentWorker, S as createEntityWorker, h as entityDefinition, b as killEntity, x as killEntityWorker };
471
635
 
472
636
  //# sourceMappingURL=index.js.map