@draug/engine 1.0.17 → 1.0.19

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.cjs CHANGED
@@ -195,13 +195,14 @@ var SystemsManager = class {
195
195
  this.requiredComponents_.add(c);
196
196
  for (const c of requiredComponents)
197
197
  this.requiredComponents_.add(c);
198
+ this.logger.debug(() => `[Systems]: "${ctor.name}" was registered`);
198
199
  }
199
200
  build() {
200
- this.logger.info(() => "Building systems...");
201
201
  this.buildSystemsArray();
202
- for (const sys of this.systems_.values())
202
+ for (const sys of this.systems_.values()) {
203
203
  sys.onInit?.({ world: this.world, logger: this.logger });
204
- this.logger.info(() => "Systems sucessfully was built!");
204
+ }
205
+ this.logger.debug(() => `Built ${this.systems_.size} systems`);
205
206
  this.dirty_ = false;
206
207
  }
207
208
  rebuild() {
@@ -213,7 +214,7 @@ var SystemsManager = class {
213
214
  throw new Error("System not registered");
214
215
  return s;
215
216
  }
216
- update(dt) {
217
+ update(time) {
217
218
  if (this.dirty_)
218
219
  this.rebuild();
219
220
  this.world.events.swapAll();
@@ -223,7 +224,7 @@ var SystemsManager = class {
223
224
  s.compute({
224
225
  world: this.world,
225
226
  entities,
226
- dt,
227
+ time,
227
228
  logger: this.logger
228
229
  });
229
230
  }
@@ -279,12 +280,18 @@ var EntityMaskNotFoundError = class extends Error {
279
280
  }
280
281
  };
281
282
  var EntitiesManager = class {
283
+ constructor(logger) {
284
+ this.logger = logger;
285
+ }
286
+ logger;
282
287
  id_ = 0;
283
288
  nextId() {
284
289
  return ++this.id_;
285
290
  }
286
291
  create() {
287
- return this.nextId();
292
+ const id2 = this.nextId();
293
+ this.logger.debug(() => `[Entities]: Created new entity with ID ${id2}`);
294
+ return id2;
288
295
  }
289
296
  };
290
297
  var EntityRef = class {
@@ -304,7 +311,7 @@ var EntityRef = class {
304
311
 
305
312
  // src/ecs/constant.ts
306
313
  var ECS_DEFAULTS = {
307
- MAX_ENTITY_COUNT: Math.pow(2, 16)
314
+ MAX_ENTITY_COUNT: Math.pow(2, 12)
308
315
  };
309
316
 
310
317
  // src/ecs/events-buffer.ts
@@ -369,17 +376,16 @@ var ObjectPool = class {
369
376
  pool_;
370
377
  factory_;
371
378
  cursor_;
372
- constructor(factory, initialSize = 1024) {
379
+ constructor(factory, initialSize = 0) {
373
380
  this.pool_ = new Array(initialSize);
374
- for (let i = 0; i < initialSize; i++) {
375
- this.pool_[i] = factory();
376
- }
377
381
  this.factory_ = factory;
378
382
  this.cursor_ = initialSize - 1;
379
383
  }
380
384
  acquire() {
381
- if (this.cursor_ < 0) this.grow();
382
- return this.pool_[this.cursor_--];
385
+ if (this.cursor_ >= 0) {
386
+ return this.pool_[this.cursor_--];
387
+ }
388
+ return this.factory_();
383
389
  }
384
390
  release(obj) {
385
391
  this.pool_[++this.cursor_] = obj;
@@ -395,17 +401,18 @@ var ObjectPool = class {
395
401
 
396
402
  // src/ecs/components/component-storage.ts
397
403
  var import_bitmap_index = require("bitmap-index");
398
- var import_ts_sparse_set = require("ts-sparse-set");
399
404
  var ComponentStorage = class {
400
405
  bits_;
401
- set_;
406
+ data_ = [];
407
+ entityIds_ = [];
408
+ indexMap_ = /* @__PURE__ */ new Map();
402
409
  pool_;
403
410
  id_ = 0;
411
+ size_ = 0;
404
412
  cls;
405
413
  constructor(cap = ECS_DEFAULTS.MAX_ENTITY_COUNT, factory, cls) {
406
- this.set_ = new import_ts_sparse_set.SparseSet(cap);
407
414
  this.bits_ = new import_bitmap_index.Bitmap(cap);
408
- this.pool_ = new ObjectPool(factory, cap);
415
+ this.pool_ = new ObjectPool(factory, 0);
409
416
  this.cls = cls;
410
417
  }
411
418
  bitmap() {
@@ -418,47 +425,65 @@ var ComponentStorage = class {
418
425
  return this.id_ = id2;
419
426
  }
420
427
  add(id2, initFn) {
428
+ if (this.indexMap_.has(id2)) {
429
+ throw new Error(`[ComponentStorage "${this.cls.name}"]: Entity ${id2} already has this component`);
430
+ }
421
431
  const obj = this.pool_.acquire();
422
432
  initFn?.(obj);
423
- const value = this.set_.add(id2, obj);
433
+ const index = this.data_.length;
434
+ this.data_.push(obj);
435
+ this.entityIds_.push(id2);
436
+ this.indexMap_.set(id2, index);
424
437
  this.bits_.set(id2);
425
- return value;
438
+ this.size_++;
439
+ return obj;
426
440
  }
427
441
  remove(id2) {
428
- const obj = this.set_.get(id2);
429
- if (!obj) return;
442
+ const index = this.indexMap_.get(id2);
443
+ if (index === void 0) return;
430
444
  this.bits_.remove(id2);
431
- this.pool_.release(obj);
432
- this.set_.remove(id2);
445
+ const lastIndex = this.data_.length - 1;
446
+ const lastEntityId = this.entityIds_[lastIndex];
447
+ const removedObj = this.data_[index];
448
+ if (index !== lastIndex) {
449
+ this.data_[index] = this.data_[lastIndex];
450
+ this.entityIds_[index] = lastEntityId;
451
+ this.indexMap_.set(lastEntityId, index);
452
+ }
453
+ this.data_.pop();
454
+ this.entityIds_.pop();
455
+ this.indexMap_.delete(id2);
456
+ this.pool_.release(removedObj);
457
+ this.size_--;
433
458
  }
434
459
  get(id2) {
435
- return this.set_.get(id2);
460
+ const index = this.indexMap_.get(id2);
461
+ return index !== void 0 ? this.data_[index] : null;
436
462
  }
437
463
  tryGet(id2) {
438
- const x = this.set_.get(id2);
439
- if (!x)
464
+ const index = this.indexMap_.get(id2);
465
+ if (index === void 0)
440
466
  throw new Error(`[ComponentStorage "${this.cls.name}"]: Requesting non-existing item with ID ${id2}.`);
441
- return x;
467
+ return this.data_[index];
442
468
  }
443
469
  writeComponentsToBuf(ids, out) {
444
470
  let len = 0;
445
471
  for (const id2 of ids) {
446
- const obj = this.set_.get(id2);
447
- if (obj !== null) out[len++] = obj;
472
+ const index = this.indexMap_.get(id2);
473
+ if (index !== void 0) out[len++] = this.data_[index];
448
474
  }
449
475
  return len;
450
476
  }
451
477
  has(id2) {
452
478
  return this.bits_.contains(id2);
453
479
  }
454
- entityIds() {
455
- return Array.from(this.bits_);
456
- }
457
480
  size() {
458
- return this.bits_.count();
481
+ return this.size_;
459
482
  }
460
483
  forEach(cb) {
461
- this.bits_.range((x) => cb(x));
484
+ for (const id2 of this.entityIds_) {
485
+ cb(id2);
486
+ }
462
487
  }
463
488
  };
464
489
 
@@ -588,9 +613,14 @@ function getComponentId(ctor) {
588
613
 
589
614
  // src/ecs/resources/resources.ts
590
615
  var ResourcesManager = class {
616
+ constructor(logger) {
617
+ this.logger = logger;
618
+ }
619
+ logger;
591
620
  items_ = /* @__PURE__ */ new Map();
592
621
  insert(type, value) {
593
622
  this.items_.set(type, value);
623
+ this.logger.debug(() => `[Resources]: Inserted new Resource "${type.name}"`);
594
624
  return value;
595
625
  }
596
626
  get(type) {
@@ -603,11 +633,12 @@ var ResourcesManager = class {
603
633
  let value = this.items_.get(type) ?? null;
604
634
  if (value === null) {
605
635
  value = factory();
606
- this.items_.set(type, value);
636
+ this.insert(type, value);
607
637
  }
608
638
  return value;
609
639
  }
610
640
  remove(type) {
641
+ this.logger.debug(() => `[Resources]: Removed resource "${type.name}"`);
611
642
  this.items_.delete(type);
612
643
  }
613
644
  };
@@ -618,10 +649,12 @@ function entry(component, init = () => {
618
649
  return [component, init];
619
650
  }
620
651
  var Commands = class {
621
- constructor(world) {
652
+ constructor(world, logger) {
622
653
  this.world = world;
654
+ this.logger = logger;
623
655
  }
624
656
  world;
657
+ logger;
625
658
  commandsQueue_ = [];
626
659
  add(cmd) {
627
660
  this.commandsQueue_.push(cmd);
@@ -639,6 +672,7 @@ var Commands = class {
639
672
  }
640
673
  };
641
674
  this.add(cmd);
675
+ this.logger.debug(() => `[Commands.createEntity]: Created new entity with ID ${id2}. Linked components: [${entries.map((x) => x[0].name).join(", ")}]`);
642
676
  return id2;
643
677
  }
644
678
  };
@@ -828,6 +862,10 @@ var ErrDAGCycleDetectedPlugin = class extends Error {
828
862
  }
829
863
  };
830
864
  var PluginsManager = class {
865
+ constructor(logger) {
866
+ this.logger = logger;
867
+ }
868
+ logger;
831
869
  plugins_ = /* @__PURE__ */ new Map();
832
870
  isInitiated_ = false;
833
871
  install(plugin, ...constructorProps) {
@@ -842,8 +880,12 @@ var PluginsManager = class {
842
880
  metadata
843
881
  };
844
882
  this.plugins_.set(metadata.id, entry2);
883
+ this.logger.debug(() => `[Plugins]: Installed plugin ${metadata.name} (${metadata.version})`);
845
884
  }
846
885
  build() {
886
+ if (this.plugins_.size === 0) {
887
+ return;
888
+ }
847
889
  const nodes = /* @__PURE__ */ new Map();
848
890
  for (const id2 of this.plugins_.keys()) {
849
891
  nodes.set(id2, new DAGNode(id2));
@@ -876,7 +918,11 @@ var PluginsManager = class {
876
918
  instance.onPluginLoad?.();
877
919
  }
878
920
  this.isInitiated_ = true;
921
+ this.logger.debug(() => `[Plugins]: Plugins built successfully!`);
879
922
  }
923
+ /**
924
+ * @internal
925
+ */
880
926
  __internal__onAfterWorldInit(world) {
881
927
  for (const p of this.plugins_.values()) {
882
928
  p.instance?.onAfterWorldInit?.(world);
@@ -919,16 +965,18 @@ var World3 = class {
919
965
  commands;
920
966
  queries;
921
967
  plugins;
968
+ logger;
922
969
  entityRefs_ = /* @__PURE__ */ new Map();
923
970
  constructor(params) {
971
+ this.entities = new EntitiesManager(params.logger);
924
972
  this.components = new ComponentsManager(params.maxEntityCount ?? ECS_DEFAULTS.MAX_ENTITY_COUNT);
925
973
  this.systems = new SystemsManager(this, params.logger);
926
- this.entities = new EntitiesManager();
927
974
  this.events = new EventBus();
928
- this.resources = new ResourcesManager();
929
- this.commands = new Commands(this);
975
+ this.resources = new ResourcesManager(params.logger);
976
+ this.commands = new Commands(this, params.logger);
930
977
  this.queries = new QueryManager(this);
931
- this.plugins = new PluginsManager();
978
+ this.plugins = new PluginsManager(params.logger);
979
+ this.logger = params.logger;
932
980
  }
933
981
  getEntityRef(id2) {
934
982
  let ref = this.entityRefs_.get(id2);
@@ -964,12 +1012,13 @@ var World3 = class {
964
1012
  this.queries.invalidate(component);
965
1013
  return c;
966
1014
  }
967
- update(dt) {
968
- this.systems.update(dt);
1015
+ update(clock) {
1016
+ this.systems.update(clock.getTime());
969
1017
  this.commands.flush(this);
970
1018
  }
971
1019
  build() {
972
1020
  this.plugins.build();
1021
+ this.logger.debug(() => "World was built successfully");
973
1022
  }
974
1023
  };
975
1024
 
@@ -992,21 +1041,28 @@ var Clock = class {
992
1041
  }
993
1042
  timeSource_;
994
1043
  lastTimeMs_;
995
- elapsedTime_ = 0;
996
- dt_ = 0;
997
- get dt() {
998
- return this.dt_;
1044
+ ellapsedTime_ = 0;
1045
+ delta_ = 0;
1046
+ time_ = {
1047
+ delta: 0,
1048
+ elapsed: 0
1049
+ };
1050
+ get deltaMs() {
1051
+ return this.delta_;
999
1052
  }
1000
1053
  get ellapsedTime() {
1001
- return this.elapsedTime_;
1054
+ return this.ellapsedTime_;
1002
1055
  }
1003
1056
  tick() {
1004
1057
  const now = this.timeSource_.now();
1005
- const dt = now - this.lastTimeMs_;
1006
- this.dt_ = dt;
1007
- this.elapsedTime_ += dt;
1058
+ const dt = Math.min(now - this.lastTimeMs_, 100);
1059
+ this.delta_ = this.time_.delta = dt / 1e3;
1060
+ this.ellapsedTime_ = this.time_.elapsed += dt / 1e3;
1008
1061
  this.lastTimeMs_ = now;
1009
1062
  }
1063
+ getTime() {
1064
+ return this.time_;
1065
+ }
1010
1066
  };
1011
1067
 
1012
1068
  // src/runtime/loop.ts
@@ -1025,7 +1081,7 @@ var Loop = class {
1025
1081
  const loop = () => {
1026
1082
  if (!this.running) return;
1027
1083
  this.clock.tick();
1028
- this.stepFn(this.clock.dt, world);
1084
+ this.stepFn(this.clock.deltaMs, world);
1029
1085
  this.platformLoop(loop);
1030
1086
  };
1031
1087
  this.platformLoop(loop);
@@ -1210,7 +1266,8 @@ var Engine = class {
1210
1266
  this.runtime = new Runtime(params.loop);
1211
1267
  this.logger = params.logger ?? new NoopLogger();
1212
1268
  this.world = new World3({
1213
- logger: this.logger
1269
+ logger: this.logger,
1270
+ maxEntityCount: params.maxEntityCount ?? ECS_DEFAULTS.MAX_ENTITY_COUNT
1214
1271
  });
1215
1272
  }
1216
1273
  init() {