@draug/engine 1.0.16 → 1.0.17

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.d.cts CHANGED
@@ -233,6 +233,24 @@ declare class PluginsManager {
233
233
  private resolveId;
234
234
  }
235
235
 
236
+ declare enum LogLevel {
237
+ Debug = 0,
238
+ Info = 1,
239
+ Warn = 2,
240
+ Error = 3
241
+ }
242
+ type LogMessage = () => string;
243
+ interface Logger {
244
+ debug(message: LogMessage): void;
245
+ info(message: LogMessage): void;
246
+ warn(message: LogMessage): void;
247
+ error(message: LogMessage): void;
248
+ }
249
+
250
+ type WorldConstructor = {
251
+ maxEntityCount?: number;
252
+ logger: Logger;
253
+ };
236
254
  declare class World {
237
255
  readonly entities: EntitiesManager;
238
256
  readonly components: ComponentsManager;
@@ -243,7 +261,7 @@ declare class World {
243
261
  readonly queries: QueryManager;
244
262
  readonly plugins: PluginsManager;
245
263
  private entityRefs_;
246
- constructor(maxEntityCount?: number);
264
+ constructor(params: WorldConstructor);
247
265
  getEntityRef(id: number): EntityRef;
248
266
  query(params: QueryParameters): number[];
249
267
  removeComponent<T extends object>(ref: EntityRef, component: ComponentType<T>): void;
@@ -311,11 +329,17 @@ type SystemCtor<T extends SystemBase = SystemBase> = ClassType<T>;
311
329
  */
312
330
  type SystemComputeContext = {
313
331
  /** Entity IDs from the query for this {@link SystemBase.compute} invocation. */
314
- entities: number[];
332
+ readonly entities: number[];
315
333
  /** ECS world instance. */
316
- world: World;
334
+ readonly world: World;
317
335
  /** Delta time (seconds or your engine's convention) since the previous update. */
318
- dt: number;
336
+ readonly dt: number;
337
+ /** Logger instance for debugging and diagnostics. */
338
+ readonly logger: Logger;
339
+ };
340
+ type SystemInitContext = {
341
+ world: World;
342
+ logger: Logger;
319
343
  };
320
344
  /**
321
345
  * Base class for ECS systems executed by {@link SystemsManager}.
@@ -329,15 +353,16 @@ declare abstract class SystemBase {
329
353
  * Logic for one systems pass: run for all entities in {@link SystemComputeContext.entities}.
330
354
  */
331
355
  abstract compute(ctx: SystemComputeContext): void;
332
- onInit?(world: World): void;
356
+ onInit?(ctx: SystemInitContext): void;
333
357
  }
334
358
  declare class SystemsManager {
335
359
  private readonly world;
360
+ private readonly logger;
336
361
  private systems_;
337
362
  private executionOrder_;
338
363
  private requiredComponents_;
339
364
  private dirty_;
340
- constructor(world: World);
365
+ constructor(world: World, logger: Logger);
341
366
  getRequiredComponents(): ComponentType[];
342
367
  register<T extends SystemBase>(sys: T): void;
343
368
  build(): void;
@@ -364,20 +389,19 @@ declare class Clock {
364
389
  type StepFunction = (dt: number, world: World) => void;
365
390
  type PlatformLoop = (callback: () => void) => void;
366
391
  declare class Loop {
367
- private readonly world;
368
392
  private readonly clock;
369
393
  private readonly stepFn;
370
394
  private readonly platformLoop;
371
395
  private running;
372
- constructor(world: World, clock: Clock, stepFn: StepFunction, platformLoop: PlatformLoop);
373
- start(): void;
396
+ constructor(clock: Clock, stepFn: StepFunction, platformLoop: PlatformLoop);
397
+ start(world: World): void;
374
398
  stop(): void;
375
399
  }
376
400
 
377
401
  declare class Runtime {
378
402
  private readonly loop;
379
403
  constructor(loop: Loop);
380
- run(): void;
404
+ run(world: World): void;
381
405
  }
382
406
 
383
407
  declare enum VisitedState {
@@ -457,14 +481,16 @@ declare class AssetsManager {
457
481
 
458
482
  type EngineConstructor = {
459
483
  loop: Loop;
484
+ logger?: Logger;
460
485
  };
461
486
  declare class Engine {
462
487
  readonly runtime: Runtime;
463
- readonly world: World;
464
488
  readonly assets: AssetsManager;
489
+ readonly world: World;
490
+ readonly logger: Logger;
465
491
  constructor(params: EngineConstructor);
466
492
  init(): void;
467
493
  start(): void;
468
494
  }
469
495
 
470
- export { Asset, type AssetDisposer, type AssetID, type AssetIDGenerator, type AssetLoader, AssetState, AssetStorage, AssetsManager, type ClassType, Clock, Commands, Component, ComponentAlreadyRegisteredError, type ComponentInitFn, ComponentStorage, type ComponentType, ComponentsManager, type CreateEntityComponentEntry, DAGNode, Engine, type EngineConstructor, EntitiesManager, type EntityID, EntityMaskNotFoundError, EntityRef, ErrDAGCycleDetected, ErrMissingPluginMetadata, ErrMissingSystemMetadata, ErrNotAPlugin, ErrNotASystem, ErrPluginNotInit, ErrUnknownPlugin, EventBuffer, EventBus, type IStorage, Loop, ObjectPool, Plugin, PluginBase, type PluginDependencies, PluginError, type PluginID, type PluginMetadata, PluginsManager, ResourcesManager, Runtime, SingletonStorage, type StepFunction, System, SystemBase, type SystemComputeContext, SystemError, SystemPhase, SystemsManager, type TimeSource, UnregisteredComponentStorageError, VisitedState, World, type WorldCommand, createEventKey, entry, getPluginMetadata, getSystemMetadata, isPlugin, isSystem, topologicalSort };
496
+ export { Asset, type AssetDisposer, type AssetID, type AssetIDGenerator, type AssetLoader, AssetState, AssetStorage, AssetsManager, type ClassType, Clock, Commands, Component, ComponentAlreadyRegisteredError, type ComponentInitFn, ComponentStorage, type ComponentType, ComponentsManager, type CreateEntityComponentEntry, DAGNode, Engine, type EngineConstructor, EntitiesManager, type EntityID, EntityMaskNotFoundError, EntityRef, ErrDAGCycleDetected, ErrMissingPluginMetadata, ErrMissingSystemMetadata, ErrNotAPlugin, ErrNotASystem, ErrPluginNotInit, ErrUnknownPlugin, EventBuffer, EventBus, type IStorage, LogLevel, type LogMessage, type Logger, Loop, ObjectPool, type PlatformLoop, Plugin, PluginBase, type PluginDependencies, PluginError, type PluginID, type PluginMetadata, PluginsManager, ResourcesManager, Runtime, SingletonStorage, type StepFunction, System, SystemBase, type SystemComputeContext, SystemError, type SystemInitContext, SystemPhase, SystemsManager, type TimeSource, UnregisteredComponentStorageError, VisitedState, World, type WorldCommand, createEventKey, entry, getPluginMetadata, getSystemMetadata, isPlugin, isSystem, topologicalSort };
package/dist/index.d.ts CHANGED
@@ -233,6 +233,24 @@ declare class PluginsManager {
233
233
  private resolveId;
234
234
  }
235
235
 
236
+ declare enum LogLevel {
237
+ Debug = 0,
238
+ Info = 1,
239
+ Warn = 2,
240
+ Error = 3
241
+ }
242
+ type LogMessage = () => string;
243
+ interface Logger {
244
+ debug(message: LogMessage): void;
245
+ info(message: LogMessage): void;
246
+ warn(message: LogMessage): void;
247
+ error(message: LogMessage): void;
248
+ }
249
+
250
+ type WorldConstructor = {
251
+ maxEntityCount?: number;
252
+ logger: Logger;
253
+ };
236
254
  declare class World {
237
255
  readonly entities: EntitiesManager;
238
256
  readonly components: ComponentsManager;
@@ -243,7 +261,7 @@ declare class World {
243
261
  readonly queries: QueryManager;
244
262
  readonly plugins: PluginsManager;
245
263
  private entityRefs_;
246
- constructor(maxEntityCount?: number);
264
+ constructor(params: WorldConstructor);
247
265
  getEntityRef(id: number): EntityRef;
248
266
  query(params: QueryParameters): number[];
249
267
  removeComponent<T extends object>(ref: EntityRef, component: ComponentType<T>): void;
@@ -311,11 +329,17 @@ type SystemCtor<T extends SystemBase = SystemBase> = ClassType<T>;
311
329
  */
312
330
  type SystemComputeContext = {
313
331
  /** Entity IDs from the query for this {@link SystemBase.compute} invocation. */
314
- entities: number[];
332
+ readonly entities: number[];
315
333
  /** ECS world instance. */
316
- world: World;
334
+ readonly world: World;
317
335
  /** Delta time (seconds or your engine's convention) since the previous update. */
318
- dt: number;
336
+ readonly dt: number;
337
+ /** Logger instance for debugging and diagnostics. */
338
+ readonly logger: Logger;
339
+ };
340
+ type SystemInitContext = {
341
+ world: World;
342
+ logger: Logger;
319
343
  };
320
344
  /**
321
345
  * Base class for ECS systems executed by {@link SystemsManager}.
@@ -329,15 +353,16 @@ declare abstract class SystemBase {
329
353
  * Logic for one systems pass: run for all entities in {@link SystemComputeContext.entities}.
330
354
  */
331
355
  abstract compute(ctx: SystemComputeContext): void;
332
- onInit?(world: World): void;
356
+ onInit?(ctx: SystemInitContext): void;
333
357
  }
334
358
  declare class SystemsManager {
335
359
  private readonly world;
360
+ private readonly logger;
336
361
  private systems_;
337
362
  private executionOrder_;
338
363
  private requiredComponents_;
339
364
  private dirty_;
340
- constructor(world: World);
365
+ constructor(world: World, logger: Logger);
341
366
  getRequiredComponents(): ComponentType[];
342
367
  register<T extends SystemBase>(sys: T): void;
343
368
  build(): void;
@@ -364,20 +389,19 @@ declare class Clock {
364
389
  type StepFunction = (dt: number, world: World) => void;
365
390
  type PlatformLoop = (callback: () => void) => void;
366
391
  declare class Loop {
367
- private readonly world;
368
392
  private readonly clock;
369
393
  private readonly stepFn;
370
394
  private readonly platformLoop;
371
395
  private running;
372
- constructor(world: World, clock: Clock, stepFn: StepFunction, platformLoop: PlatformLoop);
373
- start(): void;
396
+ constructor(clock: Clock, stepFn: StepFunction, platformLoop: PlatformLoop);
397
+ start(world: World): void;
374
398
  stop(): void;
375
399
  }
376
400
 
377
401
  declare class Runtime {
378
402
  private readonly loop;
379
403
  constructor(loop: Loop);
380
- run(): void;
404
+ run(world: World): void;
381
405
  }
382
406
 
383
407
  declare enum VisitedState {
@@ -457,14 +481,16 @@ declare class AssetsManager {
457
481
 
458
482
  type EngineConstructor = {
459
483
  loop: Loop;
484
+ logger?: Logger;
460
485
  };
461
486
  declare class Engine {
462
487
  readonly runtime: Runtime;
463
- readonly world: World;
464
488
  readonly assets: AssetsManager;
489
+ readonly world: World;
490
+ readonly logger: Logger;
465
491
  constructor(params: EngineConstructor);
466
492
  init(): void;
467
493
  start(): void;
468
494
  }
469
495
 
470
- export { Asset, type AssetDisposer, type AssetID, type AssetIDGenerator, type AssetLoader, AssetState, AssetStorage, AssetsManager, type ClassType, Clock, Commands, Component, ComponentAlreadyRegisteredError, type ComponentInitFn, ComponentStorage, type ComponentType, ComponentsManager, type CreateEntityComponentEntry, DAGNode, Engine, type EngineConstructor, EntitiesManager, type EntityID, EntityMaskNotFoundError, EntityRef, ErrDAGCycleDetected, ErrMissingPluginMetadata, ErrMissingSystemMetadata, ErrNotAPlugin, ErrNotASystem, ErrPluginNotInit, ErrUnknownPlugin, EventBuffer, EventBus, type IStorage, Loop, ObjectPool, Plugin, PluginBase, type PluginDependencies, PluginError, type PluginID, type PluginMetadata, PluginsManager, ResourcesManager, Runtime, SingletonStorage, type StepFunction, System, SystemBase, type SystemComputeContext, SystemError, SystemPhase, SystemsManager, type TimeSource, UnregisteredComponentStorageError, VisitedState, World, type WorldCommand, createEventKey, entry, getPluginMetadata, getSystemMetadata, isPlugin, isSystem, topologicalSort };
496
+ export { Asset, type AssetDisposer, type AssetID, type AssetIDGenerator, type AssetLoader, AssetState, AssetStorage, AssetsManager, type ClassType, Clock, Commands, Component, ComponentAlreadyRegisteredError, type ComponentInitFn, ComponentStorage, type ComponentType, ComponentsManager, type CreateEntityComponentEntry, DAGNode, Engine, type EngineConstructor, EntitiesManager, type EntityID, EntityMaskNotFoundError, EntityRef, ErrDAGCycleDetected, ErrMissingPluginMetadata, ErrMissingSystemMetadata, ErrNotAPlugin, ErrNotASystem, ErrPluginNotInit, ErrUnknownPlugin, EventBuffer, EventBus, type IStorage, LogLevel, type LogMessage, type Logger, Loop, ObjectPool, type PlatformLoop, Plugin, PluginBase, type PluginDependencies, PluginError, type PluginID, type PluginMetadata, PluginsManager, ResourcesManager, Runtime, SingletonStorage, type StepFunction, System, SystemBase, type SystemComputeContext, SystemError, type SystemInitContext, SystemPhase, SystemsManager, type TimeSource, UnregisteredComponentStorageError, VisitedState, World, type WorldCommand, createEventKey, entry, getPluginMetadata, getSystemMetadata, isPlugin, isSystem, topologicalSort };
package/dist/index.js CHANGED
@@ -94,10 +94,12 @@ function isSystem(ctor) {
94
94
  var SystemBase = class {
95
95
  };
96
96
  var SystemsManager = class {
97
- constructor(world) {
97
+ constructor(world, logger) {
98
98
  this.world = world;
99
+ this.logger = logger;
99
100
  }
100
101
  world;
102
+ logger;
101
103
  systems_ = /* @__PURE__ */ new Map();
102
104
  executionOrder_ = [];
103
105
  requiredComponents_ = /* @__PURE__ */ new Set();
@@ -121,9 +123,12 @@ var SystemsManager = class {
121
123
  this.requiredComponents_.add(c);
122
124
  }
123
125
  build() {
126
+ this.logger.info(() => "Building systems...");
124
127
  this.buildSystemsArray();
125
128
  for (const sys of this.systems_.values())
126
- sys.onInit?.(this.world);
129
+ sys.onInit?.({ world: this.world, logger: this.logger });
130
+ this.logger.info(() => "Systems sucessfully was built!");
131
+ this.dirty_ = false;
127
132
  }
128
133
  rebuild() {
129
134
  this.build();
@@ -141,7 +146,12 @@ var SystemsManager = class {
141
146
  for (const s of this.executionOrder_) {
142
147
  const { query } = getSystemMetadata(s.constructor);
143
148
  const entities = this.world.query(query);
144
- s.compute({ entities, world: this.world, dt });
149
+ s.compute({
150
+ world: this.world,
151
+ entities,
152
+ dt,
153
+ logger: this.logger
154
+ });
145
155
  }
146
156
  }
147
157
  buildSystemsArray() {
@@ -827,17 +837,24 @@ var PluginsManager = class {
827
837
 
828
838
  // src/ecs/world.ts
829
839
  var World3 = class {
830
- entities = new EntitiesManager();
831
- components = new ComponentsManager();
832
- systems = new SystemsManager(this);
833
- events = new EventBus();
834
- resources = new ResourcesManager();
835
- commands = new Commands(this);
836
- queries = new QueryManager(this);
837
- plugins = new PluginsManager();
840
+ entities;
841
+ components;
842
+ systems;
843
+ events;
844
+ resources;
845
+ commands;
846
+ queries;
847
+ plugins;
838
848
  entityRefs_ = /* @__PURE__ */ new Map();
839
- constructor(maxEntityCount = ECS_DEFAULTS.MAX_ENTITY_COUNT) {
840
- this.components = new ComponentsManager(maxEntityCount);
849
+ constructor(params) {
850
+ this.components = new ComponentsManager(params.maxEntityCount ?? ECS_DEFAULTS.MAX_ENTITY_COUNT);
851
+ this.systems = new SystemsManager(this, params.logger);
852
+ this.entities = new EntitiesManager();
853
+ this.events = new EventBus();
854
+ this.resources = new ResourcesManager();
855
+ this.commands = new Commands(this);
856
+ this.queries = new QueryManager(this);
857
+ this.plugins = new PluginsManager();
841
858
  }
842
859
  getEntityRef(id2) {
843
860
  let ref = this.entityRefs_.get(id2);
@@ -882,6 +899,17 @@ var World3 = class {
882
899
  }
883
900
  };
884
901
 
902
+ // src/runtime/runtime.ts
903
+ var Runtime = class {
904
+ constructor(loop) {
905
+ this.loop = loop;
906
+ }
907
+ loop;
908
+ run(world) {
909
+ this.loop.start(world);
910
+ }
911
+ };
912
+
885
913
  // src/runtime/clock.ts
886
914
  var Clock = class {
887
915
  constructor(timeSource_) {
@@ -909,23 +937,21 @@ var Clock = class {
909
937
 
910
938
  // src/runtime/loop.ts
911
939
  var Loop = class {
912
- constructor(world, clock, stepFn, platformLoop) {
913
- this.world = world;
940
+ constructor(clock, stepFn, platformLoop) {
914
941
  this.clock = clock;
915
942
  this.stepFn = stepFn;
916
943
  this.platformLoop = platformLoop;
917
944
  }
918
- world;
919
945
  clock;
920
946
  stepFn;
921
947
  platformLoop;
922
948
  running = false;
923
- start() {
949
+ start(world) {
924
950
  this.running = true;
925
951
  const loop = () => {
926
952
  if (!this.running) return;
927
953
  this.clock.tick();
928
- this.stepFn(this.clock.dt, this.world);
954
+ this.stepFn(this.clock.dt, world);
929
955
  this.platformLoop(loop);
930
956
  };
931
957
  this.platformLoop(loop);
@@ -935,17 +961,6 @@ var Loop = class {
935
961
  }
936
962
  };
937
963
 
938
- // src/runtime/runtime.ts
939
- var Runtime = class {
940
- constructor(loop) {
941
- this.loop = loop;
942
- }
943
- loop;
944
- run() {
945
- this.loop.start();
946
- }
947
- };
948
-
949
964
  // src/assets/assets.ts
950
965
  var AssetState = /* @__PURE__ */ ((AssetState2) => {
951
966
  AssetState2[AssetState2["NOT_READY"] = 1] = "NOT_READY";
@@ -1092,21 +1107,43 @@ var AssetsManager = class {
1092
1107
  }
1093
1108
  };
1094
1109
 
1110
+ // src/logger/logger.ts
1111
+ var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
1112
+ LogLevel2[LogLevel2["Debug"] = 0] = "Debug";
1113
+ LogLevel2[LogLevel2["Info"] = 1] = "Info";
1114
+ LogLevel2[LogLevel2["Warn"] = 2] = "Warn";
1115
+ LogLevel2[LogLevel2["Error"] = 3] = "Error";
1116
+ return LogLevel2;
1117
+ })(LogLevel || {});
1118
+ var NoopLogger = class {
1119
+ debug() {
1120
+ }
1121
+ info() {
1122
+ }
1123
+ warn() {
1124
+ }
1125
+ error() {
1126
+ }
1127
+ };
1128
+
1095
1129
  // src/engine.ts
1096
1130
  var Engine = class {
1097
1131
  runtime;
1132
+ assets = new AssetsManager();
1098
1133
  world;
1099
- assets;
1134
+ logger;
1100
1135
  constructor(params) {
1101
- this.world = new World3();
1102
1136
  this.runtime = new Runtime(params.loop);
1103
- this.assets = new AssetsManager();
1137
+ this.logger = params.logger ?? new NoopLogger();
1138
+ this.world = new World3({
1139
+ logger: this.logger
1140
+ });
1104
1141
  }
1105
1142
  init() {
1106
1143
  this.world.build();
1107
1144
  }
1108
1145
  start() {
1109
- this.runtime.run();
1146
+ this.runtime.run(this.world);
1110
1147
  }
1111
1148
  };
1112
1149
  export {
@@ -1134,6 +1171,7 @@ export {
1134
1171
  ErrUnknownPlugin,
1135
1172
  EventBuffer,
1136
1173
  EventBus,
1174
+ LogLevel,
1137
1175
  Loop,
1138
1176
  ObjectPool,
1139
1177
  Plugin,