@draug/engine 1.0.16 → 1.0.18

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
@@ -44,6 +44,7 @@ __export(index_exports, {
44
44
  ErrUnknownPlugin: () => ErrUnknownPlugin,
45
45
  EventBuffer: () => EventBuffer,
46
46
  EventBus: () => EventBus,
47
+ LogLevel: () => LogLevel,
47
48
  Loop: () => Loop,
48
49
  ObjectPool: () => ObjectPool,
49
50
  Plugin: () => Plugin,
@@ -167,10 +168,12 @@ function isSystem(ctor) {
167
168
  var SystemBase = class {
168
169
  };
169
170
  var SystemsManager = class {
170
- constructor(world) {
171
+ constructor(world, logger) {
171
172
  this.world = world;
173
+ this.logger = logger;
172
174
  }
173
175
  world;
176
+ logger;
174
177
  systems_ = /* @__PURE__ */ new Map();
175
178
  executionOrder_ = [];
176
179
  requiredComponents_ = /* @__PURE__ */ new Set();
@@ -192,11 +195,15 @@ var SystemsManager = class {
192
195
  this.requiredComponents_.add(c);
193
196
  for (const c of requiredComponents)
194
197
  this.requiredComponents_.add(c);
198
+ this.logger.debug(() => `[Systems]: "${ctor.name}" was registered`);
195
199
  }
196
200
  build() {
197
201
  this.buildSystemsArray();
198
- for (const sys of this.systems_.values())
199
- sys.onInit?.(this.world);
202
+ for (const sys of this.systems_.values()) {
203
+ sys.onInit?.({ world: this.world, logger: this.logger });
204
+ }
205
+ this.logger.debug(() => `Built ${this.systems_.size} systems`);
206
+ this.dirty_ = false;
200
207
  }
201
208
  rebuild() {
202
209
  this.build();
@@ -214,7 +221,12 @@ var SystemsManager = class {
214
221
  for (const s of this.executionOrder_) {
215
222
  const { query } = getSystemMetadata(s.constructor);
216
223
  const entities = this.world.query(query);
217
- s.compute({ entities, world: this.world, dt });
224
+ s.compute({
225
+ world: this.world,
226
+ entities,
227
+ dt,
228
+ logger: this.logger
229
+ });
218
230
  }
219
231
  }
220
232
  buildSystemsArray() {
@@ -268,12 +280,18 @@ var EntityMaskNotFoundError = class extends Error {
268
280
  }
269
281
  };
270
282
  var EntitiesManager = class {
283
+ constructor(logger) {
284
+ this.logger = logger;
285
+ }
286
+ logger;
271
287
  id_ = 0;
272
288
  nextId() {
273
289
  return ++this.id_;
274
290
  }
275
291
  create() {
276
- return this.nextId();
292
+ const id2 = this.nextId();
293
+ this.logger.debug(() => `[Entities]: Created new entity with ID ${id2}`);
294
+ return id2;
277
295
  }
278
296
  };
279
297
  var EntityRef = class {
@@ -577,9 +595,14 @@ function getComponentId(ctor) {
577
595
 
578
596
  // src/ecs/resources/resources.ts
579
597
  var ResourcesManager = class {
598
+ constructor(logger) {
599
+ this.logger = logger;
600
+ }
601
+ logger;
580
602
  items_ = /* @__PURE__ */ new Map();
581
603
  insert(type, value) {
582
604
  this.items_.set(type, value);
605
+ this.logger.debug(() => `[Resources]: Inserted new Resource "${type.name}"`);
583
606
  return value;
584
607
  }
585
608
  get(type) {
@@ -592,11 +615,12 @@ var ResourcesManager = class {
592
615
  let value = this.items_.get(type) ?? null;
593
616
  if (value === null) {
594
617
  value = factory();
595
- this.items_.set(type, value);
618
+ this.insert(type, value);
596
619
  }
597
620
  return value;
598
621
  }
599
622
  remove(type) {
623
+ this.logger.debug(() => `[Resources]: Removed resource "${type.name}"`);
600
624
  this.items_.delete(type);
601
625
  }
602
626
  };
@@ -607,10 +631,12 @@ function entry(component, init = () => {
607
631
  return [component, init];
608
632
  }
609
633
  var Commands = class {
610
- constructor(world) {
634
+ constructor(world, logger) {
611
635
  this.world = world;
636
+ this.logger = logger;
612
637
  }
613
638
  world;
639
+ logger;
614
640
  commandsQueue_ = [];
615
641
  add(cmd) {
616
642
  this.commandsQueue_.push(cmd);
@@ -628,6 +654,7 @@ var Commands = class {
628
654
  }
629
655
  };
630
656
  this.add(cmd);
657
+ this.logger.debug(() => `[Commands.createEntity]: Created new entity with ID ${id2}. Linked components: [${entries.map((x) => x[0].name).join(", ")}]`);
631
658
  return id2;
632
659
  }
633
660
  };
@@ -817,6 +844,10 @@ var ErrDAGCycleDetectedPlugin = class extends Error {
817
844
  }
818
845
  };
819
846
  var PluginsManager = class {
847
+ constructor(logger) {
848
+ this.logger = logger;
849
+ }
850
+ logger;
820
851
  plugins_ = /* @__PURE__ */ new Map();
821
852
  isInitiated_ = false;
822
853
  install(plugin, ...constructorProps) {
@@ -831,6 +862,7 @@ var PluginsManager = class {
831
862
  metadata
832
863
  };
833
864
  this.plugins_.set(metadata.id, entry2);
865
+ this.logger.debug(() => `[Plugins]: Installed plugin ${metadata.name} (${metadata.version})`);
834
866
  }
835
867
  build() {
836
868
  const nodes = /* @__PURE__ */ new Map();
@@ -865,7 +897,11 @@ var PluginsManager = class {
865
897
  instance.onPluginLoad?.();
866
898
  }
867
899
  this.isInitiated_ = true;
900
+ this.logger.debug(() => `[Plugins]: Plugins built successfully!`);
868
901
  }
902
+ /**
903
+ * @internal
904
+ */
869
905
  __internal__onAfterWorldInit(world) {
870
906
  for (const p of this.plugins_.values()) {
871
907
  p.instance?.onAfterWorldInit?.(world);
@@ -900,17 +936,26 @@ var PluginsManager = class {
900
936
 
901
937
  // src/ecs/world.ts
902
938
  var World3 = class {
903
- entities = new EntitiesManager();
904
- components = new ComponentsManager();
905
- systems = new SystemsManager(this);
906
- events = new EventBus();
907
- resources = new ResourcesManager();
908
- commands = new Commands(this);
909
- queries = new QueryManager(this);
910
- plugins = new PluginsManager();
939
+ entities;
940
+ components;
941
+ systems;
942
+ events;
943
+ resources;
944
+ commands;
945
+ queries;
946
+ plugins;
947
+ logger;
911
948
  entityRefs_ = /* @__PURE__ */ new Map();
912
- constructor(maxEntityCount = ECS_DEFAULTS.MAX_ENTITY_COUNT) {
913
- this.components = new ComponentsManager(maxEntityCount);
949
+ constructor(params) {
950
+ this.entities = new EntitiesManager(params.logger);
951
+ this.components = new ComponentsManager(params.maxEntityCount ?? ECS_DEFAULTS.MAX_ENTITY_COUNT);
952
+ this.systems = new SystemsManager(this, params.logger);
953
+ this.events = new EventBus();
954
+ this.resources = new ResourcesManager(params.logger);
955
+ this.commands = new Commands(this, params.logger);
956
+ this.queries = new QueryManager(this);
957
+ this.plugins = new PluginsManager(params.logger);
958
+ this.logger = params.logger;
914
959
  }
915
960
  getEntityRef(id2) {
916
961
  let ref = this.entityRefs_.get(id2);
@@ -952,6 +997,18 @@ var World3 = class {
952
997
  }
953
998
  build() {
954
999
  this.plugins.build();
1000
+ this.logger.debug(() => "World was built successfully");
1001
+ }
1002
+ };
1003
+
1004
+ // src/runtime/runtime.ts
1005
+ var Runtime = class {
1006
+ constructor(loop) {
1007
+ this.loop = loop;
1008
+ }
1009
+ loop;
1010
+ run(world) {
1011
+ this.loop.start(world);
955
1012
  }
956
1013
  };
957
1014
 
@@ -982,23 +1039,21 @@ var Clock = class {
982
1039
 
983
1040
  // src/runtime/loop.ts
984
1041
  var Loop = class {
985
- constructor(world, clock, stepFn, platformLoop) {
986
- this.world = world;
1042
+ constructor(clock, stepFn, platformLoop) {
987
1043
  this.clock = clock;
988
1044
  this.stepFn = stepFn;
989
1045
  this.platformLoop = platformLoop;
990
1046
  }
991
- world;
992
1047
  clock;
993
1048
  stepFn;
994
1049
  platformLoop;
995
1050
  running = false;
996
- start() {
1051
+ start(world) {
997
1052
  this.running = true;
998
1053
  const loop = () => {
999
1054
  if (!this.running) return;
1000
1055
  this.clock.tick();
1001
- this.stepFn(this.clock.dt, this.world);
1056
+ this.stepFn(this.clock.dt, world);
1002
1057
  this.platformLoop(loop);
1003
1058
  };
1004
1059
  this.platformLoop(loop);
@@ -1008,17 +1063,6 @@ var Loop = class {
1008
1063
  }
1009
1064
  };
1010
1065
 
1011
- // src/runtime/runtime.ts
1012
- var Runtime = class {
1013
- constructor(loop) {
1014
- this.loop = loop;
1015
- }
1016
- loop;
1017
- run() {
1018
- this.loop.start();
1019
- }
1020
- };
1021
-
1022
1066
  // src/assets/assets.ts
1023
1067
  var AssetState = /* @__PURE__ */ ((AssetState2) => {
1024
1068
  AssetState2[AssetState2["NOT_READY"] = 1] = "NOT_READY";
@@ -1165,21 +1209,43 @@ var AssetsManager = class {
1165
1209
  }
1166
1210
  };
1167
1211
 
1212
+ // src/logger/logger.ts
1213
+ var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
1214
+ LogLevel2[LogLevel2["Debug"] = 0] = "Debug";
1215
+ LogLevel2[LogLevel2["Info"] = 1] = "Info";
1216
+ LogLevel2[LogLevel2["Warn"] = 2] = "Warn";
1217
+ LogLevel2[LogLevel2["Error"] = 3] = "Error";
1218
+ return LogLevel2;
1219
+ })(LogLevel || {});
1220
+ var NoopLogger = class {
1221
+ debug() {
1222
+ }
1223
+ info() {
1224
+ }
1225
+ warn() {
1226
+ }
1227
+ error() {
1228
+ }
1229
+ };
1230
+
1168
1231
  // src/engine.ts
1169
1232
  var Engine = class {
1170
1233
  runtime;
1234
+ assets = new AssetsManager();
1171
1235
  world;
1172
- assets;
1236
+ logger;
1173
1237
  constructor(params) {
1174
- this.world = new World3();
1175
1238
  this.runtime = new Runtime(params.loop);
1176
- this.assets = new AssetsManager();
1239
+ this.logger = params.logger ?? new NoopLogger();
1240
+ this.world = new World3({
1241
+ logger: this.logger
1242
+ });
1177
1243
  }
1178
1244
  init() {
1179
1245
  this.world.build();
1180
1246
  }
1181
1247
  start() {
1182
- this.runtime.run();
1248
+ this.runtime.run(this.world);
1183
1249
  }
1184
1250
  };
1185
1251
  // Annotate the CommonJS export names for ESM import in node:
@@ -1208,6 +1274,7 @@ var Engine = class {
1208
1274
  ErrUnknownPlugin,
1209
1275
  EventBuffer,
1210
1276
  EventBus,
1277
+ LogLevel,
1211
1278
  Loop,
1212
1279
  ObjectPool,
1213
1280
  Plugin,