@daneren2005/shared-memory-ecs 1.9.0 → 1.10.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/dist/index.js CHANGED
@@ -1,17 +1,17 @@
1
- import { _ as e, a as t, c as n, d as r, f as i, g as a, h as o, i as s, l as c, m as l, n as u, o as d, p as f, r as p, s as m, t as h, u as g, v as _, y as v } from "./create-system-worker-CNpRs2FO.js";
2
- import y from "@daneren2005/shared-memory-objects/memory-heap";
3
- import { MAX_BYTE_OFFSET_LENGTH as b } from "@daneren2005/shared-memory-objects/utils/pointer";
1
+ import { _ as e, a as t, c as n, d as r, f as i, g as a, h as o, i as s, l as c, m as l, n as u, o as d, p as f, r as p, s as m, t as h, u as ee, v as g, y as _ } from "./create-system-worker-BU5CnyVh.js";
2
+ import te from "@daneren2005/shared-memory-objects/memory-heap";
3
+ import { MAX_BYTE_OFFSET_LENGTH as v } from "@daneren2005/shared-memory-objects/utils/pointer";
4
4
  //#region \0rolldown/runtime.js
5
- var x = Object.create, S = Object.defineProperty, C = Object.getOwnPropertyDescriptor, w = Object.getOwnPropertyNames, T = Object.getPrototypeOf, E = Object.prototype.hasOwnProperty, D = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), O = (e, t, n, r) => {
6
- if (t && typeof t == "object" || typeof t == "function") for (var i = w(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !E.call(e, s) && s !== n && S(e, s, {
5
+ var y = Object.create, b = Object.defineProperty, ne = Object.getOwnPropertyDescriptor, re = Object.getOwnPropertyNames, ie = Object.getPrototypeOf, x = Object.prototype.hasOwnProperty, ae = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), oe = (e, t, n, r) => {
6
+ if (t && typeof t == "object" || typeof t == "function") for (var i = re(t), a = 0, o = i.length, s; a < o; a++) s = i[a], !x.call(e, s) && s !== n && b(e, s, {
7
7
  get: ((e) => t[e]).bind(null, s),
8
- enumerable: !(r = C(t, s)) || r.enumerable
8
+ enumerable: !(r = ne(t, s)) || r.enumerable
9
9
  });
10
10
  return e;
11
- }, k = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : x(T(e)), O(t || !e || !e.__esModule || !E.call(e, "default") ? S(n, "default", {
11
+ }, S = /* @__PURE__ */ ((e, t, n) => (n = e == null ? {} : y(ie(e)), oe(t || !e || !e.__esModule || !x.call(e, "default") ? b(n, "default", {
12
12
  value: e,
13
13
  enumerable: !0
14
- }) : n, e)))((/* @__PURE__ */ D(((e, t) => {
14
+ }) : n, e)))((/* @__PURE__ */ ae(((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,7 +98,7 @@ var x = Object.create, S = Object.defineProperty, C = 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), A = class extends k.default {
101
+ })))(), 1), C = class extends S.default {
102
102
  world;
103
103
  name;
104
104
  currentDelta = 0;
@@ -174,13 +174,13 @@ var x = Object.create, S = Object.defineProperty, C = Object.getOwnPropertyDescr
174
174
  this.removeAllListeners();
175
175
  }
176
176
  };
177
- function j(e, t, n) {
177
+ function w(e, t, n) {
178
178
  let r = e.groups.map(t);
179
179
  for (let t = 0; t < e.entities.length; t++) r[e.groupIndexes[t]] && n(e.entities[t]);
180
180
  }
181
181
  //#endregion
182
182
  //#region src/systems/iterable-system.ts
183
- var M = class extends A {
183
+ var T = class extends C {
184
184
  remainingInstancesToRun = [];
185
185
  remainingInstancesStartTime = null;
186
186
  iterationsPerCheck;
@@ -230,7 +230,7 @@ var M = class extends A {
230
230
  }
231
231
  beforeRunIterables() {}
232
232
  getIterableEntityId(e) {}
233
- }, N = class e extends M {
233
+ }, E = class e extends T {
234
234
  entities = /* @__PURE__ */ new Map();
235
235
  options;
236
236
  constructor(e, t = { name: "EntitySystem" }) {
@@ -268,7 +268,7 @@ var M = class extends A {
268
268
  t.entities.forEach((e) => this.onEntityAdded(e));
269
269
  return;
270
270
  }
271
- j(t, (e) => this.hasComponents(e), (e) => {
271
+ w(t, (e) => this.hasComponents(e), (e) => {
272
272
  this.filterEntity(e) && (this.entities.set(e.eid, e), this.options.updateEntityOnAdd && this.updateEntity(e, 0));
273
273
  });
274
274
  }
@@ -300,11 +300,11 @@ var M = class extends A {
300
300
  ...this.reads ?? []
301
301
  ] : void 0;
302
302
  }
303
- }, P = class {
303
+ }, D = class {
304
304
  onmessage(e, t = []) {}
305
- }, F = class extends P {
305
+ }, O = class extends D {
306
306
  updateFunction;
307
- entities = [];
307
+ entities = new e();
308
308
  workState = new a();
309
309
  queryEntities = {};
310
310
  worldExtension;
@@ -321,76 +321,69 @@ var M = class extends A {
321
321
  };
322
322
  }
323
323
  getString = (e) => this.world.constantStrings.getString(e) ?? "";
324
- postMessage(t) {
325
- if (t.type === "init") this.onMessageTyped({ type: "init-complete" });
326
- else if (t.type === "load") {
327
- this.entities = e(this.entities, t.entities), Object.entries(t.queries).forEach(([t, n]) => {
328
- this.queryEntities[t] = e(this.queryEntities[t] ?? [], n);
329
- });
330
- let n = {
331
- gameTime: t.gameTime,
324
+ postMessage(e) {
325
+ if (e.type === "init") this.onMessageTyped({ type: "init-complete" });
326
+ else if (e.type === "load") {
327
+ this.entities.apply(e.entities);
328
+ let t = this.applyQueryDeltas(e.queries), n = {
329
+ gameTime: e.gameTime,
332
330
  getString: this.getString,
333
331
  heap: this.world.heap
334
332
  };
335
- this.worldExtension = this.updateFunction.init?.(n, t.data, this.entities, this.queryEntities) ?? void 0, this.onMessageTyped({ type: "loaded" });
336
- } else if (t.type === "reset") this.workState.clear(), this.entities = [], this.queryEntities = {};
337
- else if (t.type === "run") {
338
- if (t.world = this.workState.getWorld(t.world, t.workBatch), this.worldExtension && Object.assign(t.world, this.worldExtension), t.world.getString = this.getString, t.world.heap = this.world.heap, t.world.allocate = this.allocator, this.createsEntities) {
339
- let e = this.world.registry;
340
- t.world.buildEntityDescriptor = (t) => o(t, this.world.factory.configs, e, this.allocator, this.world.factory.getWorkerClasses());
333
+ this.worldExtension = this.updateFunction.init?.(n, e.data, this.entities.entries, t) ?? void 0, this.onMessageTyped({ type: "loaded" });
334
+ } else if (e.type === "reset") this.workState.clear(), this.entities.clear(), this.queryEntities = {};
335
+ else if (e.type === "run") {
336
+ if (e.world = this.workState.getWorld(e.world, e.workBatch), this.worldExtension && Object.assign(e.world, this.worldExtension), e.world.getString = this.getString, e.world.heap = this.world.heap, e.world.allocate = this.allocator, this.createsEntities) {
337
+ let t = this.world.registry;
338
+ e.world.buildEntityDescriptor = (e) => o(e, this.world.factory.configs, t, this.allocator, this.world.factory.getWorkerClasses());
341
339
  }
342
340
  if (this.addsComponents) {
343
- let e = this.world.registry;
344
- t.world.buildComponentDescriptor = (t, n) => l(t, n, e, this.allocator);
341
+ let t = this.world.registry;
342
+ e.world.buildComponentDescriptor = (e, n) => l(e, n, t, this.allocator);
345
343
  }
346
- let n = performance.now(), r = [], i = {}, a = [], s = [], c = [];
347
- this.entities = e(this.entities, t.entities);
348
- let u = this.workState.getEntities(this.entities, t.entities, t.workBatch), d = {};
349
- Object.entries(t.queries).forEach(([t, n]) => {
350
- let r = e(this.queryEntities[t] ?? [], n);
351
- this.queryEntities[t] = r, d[t] = r;
352
- });
353
- let f = {
354
- entityComponentChanged(e, t, n, i) {
355
- r.push({
344
+ let t = performance.now(), n = [], r = {}, i = [], a = [], s = [];
345
+ this.entities.apply(e.entities);
346
+ let c = this.workState.getEntities(this.entities.entries, e.entities, e.workBatch), u = this.applyQueryDeltas(e.queries), d = {
347
+ entityComponentChanged(e, t, r, i) {
348
+ n.push({
356
349
  entityId: e,
357
350
  event: "component-property-updated",
358
351
  args: [
359
352
  t,
360
- n,
353
+ r,
361
354
  i
362
355
  ]
363
356
  });
364
357
  },
365
- emitEntityEvent(e, t, ...n) {
366
- r.push({
358
+ emitEntityEvent(e, t, ...r) {
359
+ n.push({
367
360
  entityId: e,
368
361
  event: t,
369
- args: n
362
+ args: r
370
363
  });
371
364
  },
372
365
  emitSystemEvent(e, t) {
373
- (i[e] ?? (i[e] = [])).push(t);
366
+ (r[e] ?? (r[e] = [])).push(t);
374
367
  },
375
368
  entityDied(e) {
376
- r.push({
369
+ n.push({
377
370
  entityId: e,
378
371
  event: "death",
379
372
  args: []
380
373
  });
381
374
  },
382
375
  createEntity(e) {
383
- a.push(e);
376
+ i.push(e);
384
377
  },
385
378
  addComponent(e, t) {
386
- s.push({
379
+ a.push({
387
380
  type: "add",
388
381
  entityId: e,
389
382
  component: t
390
383
  });
391
384
  },
392
385
  removeComponent(e, t) {
393
- s.push({
386
+ a.push({
394
387
  type: "remove",
395
388
  entityId: e,
396
389
  componentName: String(t)
@@ -398,69 +391,77 @@ var M = class extends A {
398
391
  }
399
392
  };
400
393
  if (this.updateFunction.queryChanged) {
401
- for (let [e, n] of Object.entries(t.queries)) if (n.added.length || n.removed.length) try {
402
- this.updateFunction.queryChanged(t.world, e, n, f);
394
+ for (let [t, n] of Object.entries(e.queries)) if (n.added.length || n.removed.length) try {
395
+ this.updateFunction.queryChanged(e.world, t, n, d);
403
396
  } catch (e) {
404
- c.push({
397
+ s.push({
405
398
  error: e,
406
399
  phase: "queryChanged"
407
400
  });
408
401
  }
409
402
  }
410
- let p = !1;
411
- if (this.updateFunction.preRun && (!t.workBatch || t.workBatch.first)) try {
412
- this.updateFunction.preRun(t.world, this.entities, d, f);
403
+ let f = !1;
404
+ if (this.updateFunction.preRun && (!e.workBatch || e.workBatch.first)) try {
405
+ this.updateFunction.preRun(e.world, this.entities.entries, u, d);
413
406
  } catch (e) {
414
- p = !0, c.push({
407
+ f = !0, s.push({
415
408
  error: e,
416
409
  phase: "preRun"
417
410
  });
418
411
  }
419
- if (!p && this.updateFunction.preBatch) try {
420
- this.updateFunction.preBatch(t.world, u, d, f);
412
+ if (!f && this.updateFunction.preBatch) try {
413
+ this.updateFunction.preBatch(e.world, c, u, d);
421
414
  } catch (e) {
422
- p = !0, c.push({
415
+ f = !0, s.push({
423
416
  error: e,
424
417
  phase: "preBatch"
425
418
  });
426
419
  }
427
- if (p || u.forEach((e) => {
420
+ if (f || c.forEach((t) => {
428
421
  try {
429
- this.updateFunction(t.world, e.entityId, e.components, d, f);
430
- } catch (t) {
431
- c.push({
432
- error: t,
422
+ this.updateFunction(e.world, t.entityId, t.components, u, d);
423
+ } catch (e) {
424
+ s.push({
425
+ error: e,
433
426
  phase: "update",
434
- entityId: e.entityId
427
+ entityId: t.entityId
435
428
  });
436
429
  }
437
- }), this.updateFunction.entityRemoved) for (let e of t.entities.removed) try {
438
- this.updateFunction.entityRemoved(t.world, e, f);
439
- } catch (t) {
440
- c.push({
441
- error: t,
430
+ }), this.updateFunction.entityRemoved) for (let t of e.entities.removed) try {
431
+ this.updateFunction.entityRemoved(e.world, t, d);
432
+ } catch (e) {
433
+ s.push({
434
+ error: e,
442
435
  phase: "entityRemoved",
443
- entityId: e
436
+ entityId: t
444
437
  });
445
438
  }
446
- let m = performance.now() - n;
447
- this.workState.finish(t.workBatch, p), this.onMessageTyped({
439
+ let p = performance.now() - t;
440
+ this.workState.finish(e.workBatch, f), this.onMessageTyped({
448
441
  type: "run-complete",
449
- generation: t.generation,
450
- runTime: m,
451
- events: r,
452
- systemEvents: i,
453
- created: a,
454
- componentChanges: s,
455
- errors: c,
456
- workFailed: p
442
+ generation: e.generation,
443
+ runTime: p,
444
+ events: n,
445
+ systemEvents: r,
446
+ created: i,
447
+ componentChanges: a,
448
+ errors: s,
449
+ workFailed: f
457
450
  });
458
451
  }
459
452
  }
453
+ applyQueryDeltas(t) {
454
+ let n = {};
455
+ for (let [r, i] of Object.entries(t)) {
456
+ let t = this.queryEntities[r] ??= new e();
457
+ t.apply(i), n[r] = t.entries;
458
+ }
459
+ return n;
460
+ }
460
461
  onMessageTyped(e) {
461
462
  this.onmessage({ data: e });
462
463
  }
463
- }, I = "___main", L = class e extends A {
464
+ }, k = "___main", A = class e extends C {
464
465
  entities = /* @__PURE__ */ new Map();
465
466
  options;
466
467
  worker;
@@ -493,13 +494,13 @@ var M = class extends A {
493
494
  type: "grow-buffer",
494
495
  buffer: e
495
496
  });
496
- })) : (this.worker = new F(t.updateFunction, e, !!t.createsEntities, !!t.addsComponents), this.isWorkerThread = !1), this.initWorker();
497
+ })) : (this.worker = new O(t.updateFunction, e, !!t.createsEntities, !!t.addsComponents), this.isWorkerThread = !1), this.initWorker();
497
498
  }
498
499
  initWorker() {
499
500
  let e = (e) => {
500
501
  console.error(e, e.cause), this.initialized || (this.initError = e, this.initPromise?.reject(e));
501
502
  };
502
- if (!(this.worker instanceof F)) {
503
+ if (!(this.worker instanceof O)) {
503
504
  let t = (t) => {
504
505
  e(Error(`Worker ${this.name} failed to load`, { cause: t }));
505
506
  };
@@ -604,7 +605,7 @@ var M = class extends A {
604
605
  type: "load",
605
606
  data: this.options.getInitData?.(),
606
607
  gameTime: this.gameTime,
607
- entities: this.buildQueryDelta(I, this.options),
608
+ entities: this.buildQueryDelta(k, this.options),
608
609
  queries: n,
609
610
  heap: this.isWorkerThread ? this.world.heap.getSharedMemory() : void 0,
610
611
  sharedMemory: this.isWorkerThread ? this.world.getSharedComponentMemory() : void 0,
@@ -704,7 +705,7 @@ var M = class extends A {
704
705
  first: t.first,
705
706
  last: t.last
706
707
  }, this.isRunning = !0, this.completeEffects = this.world.scheduler.prepareEffects?.(this.world, this);
707
- let i = this.buildQueryDelta(I, this.options), a = {};
708
+ let i = this.buildQueryDelta(k, this.options), a = {};
708
709
  Object.entries(this.options.queries ?? {}).forEach(([e, t]) => {
709
710
  a[e] = this.buildQueryDelta(e, t);
710
711
  });
@@ -773,7 +774,7 @@ var M = class extends A {
773
774
  }
774
775
  checkAddEntity(e) {
775
776
  let t = this.tracksMainQuery && this.matchesQuery(e, this.options);
776
- return this.updateEntityList(I, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
777
+ return this.updateEntityList(k, this.entities, e, t), Object.entries(this.options.queries ?? {}).forEach(([t, n]) => {
777
778
  let r = this.queryEntities[t] ?? (this.queryEntities[t] = /* @__PURE__ */ new Map());
778
779
  this.updateEntityList(t, r, e, this.matchesQuery(e, n));
779
780
  }), t;
@@ -783,19 +784,19 @@ var M = class extends A {
783
784
  t.entities.forEach((e) => this.checkAddEntity(e));
784
785
  return;
785
786
  }
786
- this.tracksMainQuery && this.addBatchToQuery(I, this.entities, this.options, t), Object.entries(this.options.queries ?? {}).forEach(([e, n]) => {
787
+ this.tracksMainQuery && this.addBatchToQuery(k, this.entities, this.options, t), Object.entries(this.options.queries ?? {}).forEach(([e, n]) => {
787
788
  let r = this.queryEntities[e] ?? (this.queryEntities[e] = /* @__PURE__ */ new Map());
788
789
  this.addBatchToQuery(e, r, n, t);
789
790
  });
790
791
  }
791
792
  addBatchToQuery(e, t, n, r) {
792
793
  let i = this.getQueryDelta(e);
793
- j(r, (e) => this.matchesComponents(e, n), (e) => {
794
+ w(r, (e) => this.matchesComponents(e, n), (e) => {
794
795
  e.components.entity.dead || n.filter && !n.filter(e) || (t.set(e.eid, e), this.markAdded(i, e));
795
796
  });
796
797
  }
797
798
  removeEntity(e) {
798
- this.updateEntityList(I, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
799
+ this.updateEntityList(k, this.entities, e, !1), Object.entries(this.queryEntities).forEach(([t, n]) => {
799
800
  this.updateEntityList(t, n, e, !1);
800
801
  });
801
802
  }
@@ -810,7 +811,7 @@ var M = class extends A {
810
811
  destroy() {
811
812
  super.destroy(), this.pendingEffects?.discard(), "terminate" in this.worker && this.worker.terminate();
812
813
  }
813
- }, R = class extends k.default {
814
+ }, j = class extends S.default {
814
815
  eid;
815
816
  config;
816
817
  world;
@@ -911,7 +912,7 @@ var M = class extends A {
911
912
  assertComponentPropertyMutable(e, t) {
912
913
  if (e === "entity" && (t === "type" || t === "class")) throw Error(`Entity ${t} is immutable`);
913
914
  }
914
- }, z = class e {
915
+ }, M = class e {
915
916
  world;
916
917
  configs;
917
918
  classes;
@@ -928,7 +929,7 @@ var M = class extends A {
928
929
  this.world = e, this.forbiddenProperties.clear();
929
930
  for (let t of Object.keys(this.classes)) {
930
931
  let n = this.classes[t].entity;
931
- if (n !== R && !(n.prototype instanceof R)) throw Error(`Entity class ${t} must extend BaseEntity`);
932
+ if (n !== j && !(n.prototype instanceof j)) throw Error(`Entity class ${t} must extend BaseEntity`);
932
933
  for (let n of this.classes[t].components) if (n === "entity" || !(n in e.registry)) throw Error(`Entity class ${t} contains unknown or reserved component ${n}`);
933
934
  }
934
935
  for (let e of Object.keys(this.configs)) this.validateTemplate(e, this.configs[e]);
@@ -954,7 +955,7 @@ var M = class extends A {
954
955
  createAdoptedEntity(e, t, n) {
955
956
  if (!t) {
956
957
  if (this.hasClasses) throw Error(`Entity type ${e} has no class`);
957
- return new R(this.world, void 0, n);
958
+ return new j(this.world, void 0, n);
958
959
  }
959
960
  let r = this.classes[t];
960
961
  if (!r) throw Error(`Unknown entity class: ${t}`);
@@ -980,7 +981,7 @@ var M = class extends A {
980
981
  return e ? this.getAllowedComponents(e) : void 0;
981
982
  }
982
983
  createEntity(e) {
983
- return new R(this.world, e);
984
+ return new j(this.world, e);
984
985
  }
985
986
  get hasClasses() {
986
987
  return this.allowedComponents.size > 0;
@@ -1045,7 +1046,160 @@ var M = class extends A {
1045
1046
  }
1046
1047
  throw Error(`Entity type ${e} class ${t} loads a component outside its class`);
1047
1048
  }
1048
- }, B = class {
1049
+ }, N = 45, P = "!", F = "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO", I = "PQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}", L = (/* @__PURE__ */ new Int8Array(128)).fill(-1);
1050
+ for (let e = 0; e < N; e++) L[F.charCodeAt(e)] = e, L[I.charCodeAt(e)] = e + N;
1051
+ function R(e) {
1052
+ let t = "";
1053
+ for (; e >= N;) t += I[e % N], e = Math.floor(e / N);
1054
+ return t + F[e];
1055
+ }
1056
+ var z = /* @__PURE__ */ new Float32Array(1), B = new Uint32Array(z.buffer);
1057
+ function se(e, t) {
1058
+ switch (e.type) {
1059
+ case "boolean": return typeof t == "boolean" ? F[Number(t)] : void 0;
1060
+ case "string": return t === null ? F[0] : typeof t == "string" && t.length ? R(t.length) + t : void 0;
1061
+ case "float32": return typeof t != "number" || !Number.isFinite(t) || Math.fround(t) !== t ? void 0 : (z[0] = t, R(B[0]));
1062
+ default: {
1063
+ let n = V(e, t);
1064
+ return n === void 0 ? void 0 : R(n);
1065
+ }
1066
+ }
1067
+ }
1068
+ function V(e, t) {
1069
+ if (typeof t != "number") return;
1070
+ let n = Math.round((t - (e.offset ?? 0)) * 10 ** (e.decimals ?? 0) / (e.step ?? 1));
1071
+ if (!Number.isSafeInteger(n) || H(e, n) !== t) return;
1072
+ if (e.type === "uint") return n >= 0 ? n : void 0;
1073
+ let r = n >= 0 ? n * 2 : -n * 2 - 1;
1074
+ return Number.isSafeInteger(r) ? r : void 0;
1075
+ }
1076
+ function H(e, t) {
1077
+ return t * (e.step ?? 1) / 10 ** (e.decimals ?? 0) + (e.offset ?? 0);
1078
+ }
1079
+ function U(e, t) {
1080
+ switch (e.type) {
1081
+ case "boolean": return t !== 0;
1082
+ case "float32": return B[0] = t, z[0];
1083
+ case "uint": return H(e, t);
1084
+ default: return H(e, t % 2 == 0 ? t / 2 : -(t + 1) / 2);
1085
+ }
1086
+ }
1087
+ function W(e) {
1088
+ return e.types === void 0 ? [e.type] : e.types;
1089
+ }
1090
+ var G = class {
1091
+ encoders = /* @__PURE__ */ new Map();
1092
+ used = /* @__PURE__ */ new Set();
1093
+ constructor(e) {
1094
+ let t = 0;
1095
+ for (let n of e) {
1096
+ let e = {
1097
+ key: t,
1098
+ definition: n,
1099
+ indices: new Map(n.properties.map((e, t) => [e.name, t]))
1100
+ };
1101
+ for (let r of W(n)) {
1102
+ if (this.encoders.has(r)) throw Error(`Duplicate compressed entity definition for ${r}`);
1103
+ this.encoders.set(r, {
1104
+ prefix: R(t++),
1105
+ group: e
1106
+ });
1107
+ }
1108
+ }
1109
+ }
1110
+ get entityDefinitions() {
1111
+ let e = {};
1112
+ for (let t of this.used) e[t.key] = t.definition;
1113
+ return e;
1114
+ }
1115
+ compress(e) {
1116
+ let t = e.type === void 0 ? void 0 : this.encoders.get(e.type), n = t && this.encode(t, e);
1117
+ return !t || n === void 0 ? e : (this.used.add(t.group), n);
1118
+ }
1119
+ compressAll(e) {
1120
+ let t = e.map((e) => this.compress(e));
1121
+ return {
1122
+ entityDefinitions: this.entityDefinitions,
1123
+ entities: t
1124
+ };
1125
+ }
1126
+ encode(e, t) {
1127
+ let n = [];
1128
+ for (let [r, i] of Object.entries(t)) {
1129
+ if (r === "type" || i === void 0) continue;
1130
+ let t = e.group.indices.get(r);
1131
+ if (t === void 0) return;
1132
+ let a = se(e.group.definition.properties[t], i);
1133
+ if (a === void 0) return;
1134
+ n[t] = a;
1135
+ }
1136
+ let r = e.prefix;
1137
+ for (let e = 0; e < n.length; e++) r += n[e] ?? P;
1138
+ return r;
1139
+ }
1140
+ };
1141
+ function ce(e, t) {
1142
+ return new G(t).compressAll(e);
1143
+ }
1144
+ var K = /* @__PURE__ */ new WeakMap();
1145
+ function le(e) {
1146
+ let t = K.get(e);
1147
+ if (t) return t;
1148
+ t = /* @__PURE__ */ new Map();
1149
+ for (let [n, r] of Object.entries(e)) {
1150
+ let e = W(r);
1151
+ for (let i = 0; i < e.length; i++) {
1152
+ if (t.has(Number(n) + i)) throw Error(`Compressed entity definitions overlap at key ${Number(n) + i}`);
1153
+ t.set(Number(n) + i, {
1154
+ type: e[i],
1155
+ properties: r.properties
1156
+ });
1157
+ }
1158
+ }
1159
+ return K.set(e, t), t;
1160
+ }
1161
+ function q(e, t) {
1162
+ let n = 0, r = () => {
1163
+ if (e[n] === P) {
1164
+ n++;
1165
+ return;
1166
+ }
1167
+ let t = 0, r = 1;
1168
+ for (; n < e.length;) {
1169
+ let i = e.charCodeAt(n++), a = i < 128 ? L[i] : -1;
1170
+ if (a < 0) throw Error(`Invalid compressed entity ${e}`);
1171
+ if (a < N) return t + a * r;
1172
+ t += (a - N) * r, r *= N;
1173
+ }
1174
+ throw Error(`Truncated compressed entity ${e}`);
1175
+ }, i = r(), a = i === void 0 ? void 0 : le(t).get(i);
1176
+ if (!a) throw Error(`No compressed entity definition for ${e}`);
1177
+ let o = { type: a.type };
1178
+ for (let t = 0; n < e.length; t++) {
1179
+ let i = a.properties[t];
1180
+ if (!i) throw Error(`Compressed entity ${e} has more values than ${a.type} defines`);
1181
+ let s = r();
1182
+ if (s !== void 0) {
1183
+ if (i.type !== "string") o[i.name] = U(i, s);
1184
+ else if (s === 0) o[i.name] = null;
1185
+ else {
1186
+ if (n + s > e.length) throw Error(`Truncated compressed entity ${e}`);
1187
+ o[i.name] = e.slice(n, n += s);
1188
+ }
1189
+ }
1190
+ }
1191
+ return o;
1192
+ }
1193
+ function J(e, t) {
1194
+ return e.map((e) => {
1195
+ if (typeof e != "string") return e;
1196
+ if (!t) throw Error("Compressed entities need entityDefinitions to load");
1197
+ return q(e, t);
1198
+ });
1199
+ }
1200
+ //#endregion
1201
+ //#region src/scheduling/default-scheduler.ts
1202
+ var Y = class {
1049
1203
  update(e, t) {
1050
1204
  let n = null;
1051
1205
  return e.systems.forEach((r) => {
@@ -1068,7 +1222,7 @@ var M = class extends A {
1068
1222
  });
1069
1223
  }), { lastSystemError: n };
1070
1224
  }
1071
- }, V = b, H = 1e4, U = class extends k.default {
1225
+ }, ue = v, X = 1e4, de = class extends S.default {
1072
1226
  heap;
1073
1227
  constantStrings;
1074
1228
  eidCounter;
@@ -1096,7 +1250,7 @@ var M = class extends A {
1096
1250
  };
1097
1251
  clearing = !1;
1098
1252
  constructor(e, t = {}) {
1099
- super(), this.scheduler = t.scheduler ?? new B(), this.heap = new y({ bufferSize: t.heapSize ?? V }), this.constantStrings = new _(this.heap), this.eidCounter = this.heap.allocUI32(1), this.heap.addOnGrowBufferHandlers((e) => this.emit("grow-buffer", e));
1253
+ super(), this.scheduler = t.scheduler ?? new Y(), this.heap = new te({ bufferSize: t.heapSize ?? ue }), this.constantStrings = new g(this.heap), this.eidCounter = this.heap.allocUI32(1), this.heap.addOnGrowBufferHandlers((e) => this.emit("grow-buffer", e));
1100
1254
  let n = {
1101
1255
  ...e,
1102
1256
  entity: i
@@ -1105,10 +1259,10 @@ var M = class extends A {
1105
1259
  let t = n[e];
1106
1260
  r[e] = {
1107
1261
  ...t,
1108
- memoryComponent: new v(this.heap, t.type, t.size)
1262
+ memoryComponent: new _(this.heap, t.type, t.size)
1109
1263
  };
1110
1264
  }
1111
- this.registry = r, this.factory = t.factory ?? new z(), this.factory.setWorld(this);
1265
+ this.registry = r, this.factory = t.factory ?? new M(), this.factory.setWorld(this);
1112
1266
  }
1113
1267
  allocateEid() {
1114
1268
  return Atomics.add(this.eidCounter.data, 0, 1) + 1;
@@ -1125,7 +1279,7 @@ var M = class extends A {
1125
1279
  this.heap.buffers[e.bufferPosition] === void 0 && this.heap.addSharedBuffer(e), this.emit("grow-buffer", e);
1126
1280
  }
1127
1281
  get needsSpareBuffer() {
1128
- return this.systems.some((e) => e instanceof L && e.isWorkerThread && (!!e.options.createsEntities || !!e.options.addsComponents));
1282
+ return this.systems.some((e) => e instanceof A && e.isWorkerThread && (!!e.options.createsEntities || !!e.options.addsComponents));
1129
1283
  }
1130
1284
  async init() {
1131
1285
  await Promise.all(this.systems.map((e) => e.init()).filter((e) => e instanceof Promise));
@@ -1174,14 +1328,14 @@ var M = class extends A {
1174
1328
  for (let e of Object.keys(this.factory.classes)) this.constantStrings.getOrCreate(e);
1175
1329
  this.isBulkLoading = !0;
1176
1330
  try {
1177
- let t = e.entities.map((e) => this.loadEntity(e, !1));
1331
+ let t = ("entityDefinitions" in e ? J(e.entities, e.entityDefinitions) : e.entities).map((e) => this.loadEntity(e, !1));
1178
1332
  for (let e of t) e.finishLoading(), this.emit("entity-added", e), e.on("death", () => {
1179
1333
  this.onEntityDied(e);
1180
1334
  });
1181
1335
  } finally {
1182
1336
  this.isBulkLoading = !1;
1183
1337
  }
1184
- let t = W(this.entities.values());
1338
+ let t = fe(this.entities.values());
1185
1339
  return this.systems.forEach((e) => e.addEntities(t)), this.gameTime = e.gameTime ?? 0, this.playerTime = e.playerTime ?? 0, this.timeScale = e.timeScale ?? 1, this.finishLoadingSystems();
1186
1340
  }
1187
1341
  async clear() {
@@ -1253,9 +1407,9 @@ var M = class extends A {
1253
1407
  }
1254
1408
  checkFreeBufferTimeout(e) {
1255
1409
  let t = this.activeFreeBuffer;
1256
- if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >= H)) {
1410
+ if (t.waitingSystems.size !== 0 && (t.waitElapsed += e, t.waitElapsed >= X)) {
1257
1411
  let e = Array.from(t.waitingSystems).map((e) => e.name).join(", ");
1258
- console.warn(`shared-memory-ecs: deferred component free waited over ${H}ms on system(s): ${e}; freeing anyway`), t.waitingSystems.clear(), this.processFreeBuffers();
1412
+ console.warn(`shared-memory-ecs: deferred component free waited over ${X}ms on system(s): ${e}; freeing anyway`), t.waitingSystems.clear(), this.processFreeBuffers();
1259
1413
  }
1260
1414
  }
1261
1415
  addSystem(e) {
@@ -1303,16 +1457,16 @@ var M = class extends A {
1303
1457
  }
1304
1458
  addEntityToEntityWorkerSystem(e, t) {
1305
1459
  this.isBulkLoading || this.systems.forEach((n) => {
1306
- (n instanceof N && n.options.components?.includes(t) || n instanceof L && this.componentAffectsEntityWorkerSystem(n, t)) && n.checkAddEntity(e);
1460
+ (n instanceof E && n.options.components?.includes(t) || n instanceof A && this.queriesReferenceComponent(n.options, t)) && n.checkAddEntity(e);
1307
1461
  });
1308
1462
  }
1309
1463
  removeEntityFromEntityWorkerSystem(e, t) {
1310
1464
  this.isBulkLoading || this.systems.forEach((n) => {
1311
- n instanceof N && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof L && this.componentAffectsEntityWorkerSystem(n, t) && n.checkAddEntity(e);
1465
+ n instanceof E && n.options.components?.includes(t) ? n.removeEntity(e) : n instanceof A && this.queriesReferenceComponent(n.options, t) && n.checkAddEntity(e);
1312
1466
  });
1313
1467
  }
1314
- componentAffectsEntityWorkerSystem(e, t) {
1315
- 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));
1468
+ queriesReferenceComponent(e, t) {
1469
+ let n = e.required.includes(t) || !!e.not?.includes(t) || !!e.optional?.includes(t), r = Object.values(e.queries ?? {}).some((e) => e.required.includes(t) || !!e.not?.includes(t) || !!e.optional?.includes(t));
1316
1470
  return n || r;
1317
1471
  }
1318
1472
  destroy() {
@@ -1321,7 +1475,7 @@ var M = class extends A {
1321
1475
  }), this.scheduler.destroy?.(this), !0);
1322
1476
  }
1323
1477
  };
1324
- function W(e) {
1478
+ function fe(e) {
1325
1479
  let t = {
1326
1480
  entities: [],
1327
1481
  groupIndexes: [],
@@ -1335,7 +1489,7 @@ function W(e) {
1335
1489
  }
1336
1490
  //#endregion
1337
1491
  //#region src/scheduling/conflict-scheduler.ts
1338
- var G = class {
1492
+ var pe = class {
1339
1493
  compiled = Object.freeze({
1340
1494
  order: Object.freeze([]),
1341
1495
  batches: Object.freeze([])
@@ -1418,7 +1572,7 @@ var G = class {
1418
1572
  }, 0);
1419
1573
  }
1420
1574
  isSliced(e) {
1421
- return e.system instanceof M && e.system.remainingInstancesToRun.length > 0;
1575
+ return e.system instanceof T && e.system.remainingInstancesToRun.length > 0;
1422
1576
  }
1423
1577
  reset() {
1424
1578
  this.continuationTimer !== void 0 && (clearTimeout(this.continuationTimer), this.continuationTimer = void 0);
@@ -1586,27 +1740,27 @@ var G = class {
1586
1740
  };
1587
1741
  //#endregion
1588
1742
  //#region src/entity-class.ts
1589
- function K(e, t) {
1743
+ function me(e, t) {
1590
1744
  return t;
1591
1745
  }
1592
- function q(e, t) {
1746
+ function Z(e, t) {
1593
1747
  return t;
1594
1748
  }
1595
1749
  //#endregion
1596
1750
  //#region src/actions/kill-entity.ts
1597
- function J(e) {
1751
+ function he(e) {
1598
1752
  e.components.entity.dead = !0, e.emit("death");
1599
1753
  }
1600
1754
  //#endregion
1601
1755
  //#region src/performance-timing.ts
1602
- var Y = 1e3, X = {
1756
+ var ge = 1e3, Q = {
1603
1757
  avg: 0,
1604
1758
  min: 0,
1605
1759
  max: 0,
1606
1760
  samples: 0
1607
1761
  };
1608
- function Z(e) {
1609
- if (!e.length) return { ...X };
1762
+ function $(e) {
1763
+ if (!e.length) return { ...Q };
1610
1764
  let t = 0, n = Infinity, r = 0;
1611
1765
  for (let i of e) t += i, i < n && (n = i), i > r && (r = i);
1612
1766
  return {
@@ -1616,13 +1770,13 @@ function Z(e) {
1616
1770
  samples: e.length
1617
1771
  };
1618
1772
  }
1619
- var Q = class extends k.default {
1773
+ var _e = class extends S.default {
1620
1774
  world;
1621
1775
  ticksBetweenUpdates;
1622
1776
  stats = {
1623
- update: { ...X },
1777
+ update: { ...Q },
1624
1778
  systems: [],
1625
- events: { ...X }
1779
+ events: { ...Q }
1626
1780
  };
1627
1781
  autoUpdate;
1628
1782
  ticks = 0;
@@ -1640,9 +1794,9 @@ var Q = class extends k.default {
1640
1794
  this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
1641
1795
  e.run = [], e.events = [], e.eventStart = -1, e.pendingRun = 0, e.pendingEvents = 0;
1642
1796
  }), this.stats = {
1643
- update: { ...X },
1797
+ update: { ...Q },
1644
1798
  systems: [],
1645
- events: { ...X }
1799
+ events: { ...Q }
1646
1800
  };
1647
1801
  }
1648
1802
  destroy() {
@@ -1687,21 +1841,21 @@ var Q = class extends k.default {
1687
1841
  let t = this.systemTimings.get(e.name);
1688
1842
  return {
1689
1843
  name: e.name,
1690
- run: Z(t?.run ?? []),
1691
- events: Z(t?.events ?? [])
1844
+ run: $(t?.run ?? []),
1845
+ events: $(t?.events ?? [])
1692
1846
  };
1693
- }), t = { ...X };
1847
+ }), t = { ...Q };
1694
1848
  return e.forEach((e) => {
1695
1849
  t.avg += e.events.avg, t.min += e.events.min, t.max += e.events.max, t.samples += e.events.samples;
1696
1850
  }), this.stats = {
1697
- update: Z(this.updateTimes),
1851
+ update: $(this.updateTimes),
1698
1852
  systems: e,
1699
1853
  events: t
1700
1854
  }, this.ticks = 0, this.updateTimes = [], this.systemTimings.forEach((e) => {
1701
1855
  e.run = [], e.events = [];
1702
1856
  }), this.emit("stats-updated", this.stats), this.stats;
1703
1857
  }
1704
- }, $ = class extends L {
1858
+ }, ve = class extends A {
1705
1859
  constructor(e, t) {
1706
1860
  super(e, {
1707
1861
  ...t,
@@ -1717,6 +1871,6 @@ var Q = class extends k.default {
1717
1871
  }
1718
1872
  };
1719
1873
  //#endregion
1720
- export { R as BaseEntity, U as BaseWorld, n as CLASS_INDEX, f as Component, G as ConflictScheduler, _ as ConstantStringCache, c as DEAD_INDEX, Y as DEFAULT_TICKS_BETWEEN_UPDATES, B as DefaultScheduler, z as EntityFactory, N as EntitySystem, F as EntitySystemWebWorker, L as EntityWorkerSystem, M as IterableSystem, v as MemoryComponent, Q as PerformanceTiming, g as STATIC_INDEX, A as System, r as TYPE_INDEX, P as WebWorker, $ as WorkerSystem, t as addComponentWorker, p as createEntitySystemWorker, d as createEntityWorker, h as createSystemWorker, K as defineEntityClasses, q as defineEntityConfigs, i as entityDefinition, J as killEntity, m as killEntityWorker, s as removeComponentWorker };
1874
+ export { j as BaseEntity, de as BaseWorld, n as CLASS_INDEX, f as Component, pe as ConflictScheduler, g as ConstantStringCache, c as DEAD_INDEX, ge as DEFAULT_TICKS_BETWEEN_UPDATES, Y as DefaultScheduler, G as EntityCompressor, M as EntityFactory, E as EntitySystem, O as EntitySystemWebWorker, A as EntityWorkerSystem, T as IterableSystem, _ as MemoryComponent, _e as PerformanceTiming, ee as STATIC_INDEX, C as System, r as TYPE_INDEX, D as WebWorker, ve as WorkerSystem, t as addComponentWorker, ce as compressEntities, p as createEntitySystemWorker, d as createEntityWorker, h as createSystemWorker, J as decompressEntities, q as decompressEntity, me as defineEntityClasses, Z as defineEntityConfigs, i as entityDefinition, he as killEntity, m as killEntityWorker, s as removeComponentWorker };
1721
1875
 
1722
1876
  //# sourceMappingURL=index.js.map