@draug/engine 1.0.21 → 1.0.23

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.ts CHANGED
@@ -1,259 +1,12 @@
1
- import { Bitmap } from 'bitmap-index';
2
-
3
- type ClassType<T> = new (...args: any[]) => T;
4
- type ComponentType<T extends object = object> = ClassType<T>;
5
-
6
- declare enum LogLevel {
7
- Debug = 0,
8
- Info = 1,
9
- Warn = 2,
10
- Error = 3
11
- }
12
- type LogMessage = () => string;
13
- interface Logger {
14
- debug(message: LogMessage): void;
15
- info(message: LogMessage): void;
16
- warn(message: LogMessage): void;
17
- error(message: LogMessage): void;
18
- }
19
-
20
- declare class ComponentStorage<T extends object> {
21
- private bits_;
22
- private data_;
23
- private entityIds_;
24
- private indexMap_;
25
- private pool_;
26
- private id_;
27
- private size_;
28
- private cls;
29
- constructor(cap: number | undefined, factory: () => T, cls: ClassType<T>);
30
- bitmap(): Bitmap;
31
- get id(): number;
32
- _internalSetId(id: number): number;
33
- add(id: number, initFn?: (obj: T) => T): T;
34
- remove(id: number): void;
35
- get(id: number): T | null;
36
- tryGet(id: number): T;
37
- writeComponentsToBuf(ids: ReadonlyArray<number>, out: T[]): number;
38
- has(id: number): boolean;
39
- size(): number;
40
- forEach(cb: (id: number) => void): void;
41
- }
42
-
43
- type RegisterComponentStorageOptions<T extends object> = {
44
- factory?: () => T;
45
- };
46
- type RegisterComponentOptions<T extends object> = RegisterComponentStorageOptions<T>;
47
- declare class ComponentAlreadyRegisteredError extends Error {
48
- constructor(component: ComponentType);
49
- }
50
- declare class ComponentsManager {
51
- private readonly logger;
52
- private maxEntityCount;
53
- private readonly storages_;
54
- private currId_;
55
- private nextId;
56
- constructor(logger: Logger, maxEntityCount?: number);
57
- register<T extends object>(component: ComponentType<T>, opts?: RegisterComponentOptions<T>): ComponentStorage<T>;
58
- private createComponentStore;
59
- getStorage<T extends object>(component: ComponentType<T>): ComponentStorage<T>;
60
- getComponentId(ctor: ComponentType): number;
61
- }
1
+ import { C as Clock, W as World, a as ClassType, L as Logger } from './system-jRDSVzc9.js';
2
+ export { b as Commands, c as ComponentAlreadyRegisteredError, d as ComponentInitFn, e as ComponentStorage, f as ComponentType, g as ComponentsManager, h as CreateEntityComponentEntry, E as EntitiesManager, i as EntityID, j as EntityMaskNotFoundError, k as EntityRef, l as ErrMissingPluginMetadata, m as ErrMissingSystemMetadata, n as ErrNotAPlugin, o as ErrNotASystem, p as ErrPluginNotInit, q as ErrUnknownPlugin, r as EventBuffer, s as EventBus, t as LogLevel, u as LogMessage, P as Plugin, v as PluginBase, w as PluginDependencies, x as PluginError, y as PluginID, z as PluginMetadata, A as PluginsManager, R as Resource, B as ResourceMetadata, D as ResourcesManager, S as System, F as SystemBase, G as SystemComputeContext, H as SystemError, I as SystemInitContext, J as SystemPhase, K as SystemsManager, T as TimeSource, U as UnregisteredComponentStorageError, M as WorldCommand, N as createEventKey, O as entry, Q as getPluginMetadata, V as getResourceMetadata, X as getSystemMetadata, Y as isPlugin, Z as isSystem } from './system-jRDSVzc9.js';
3
+ import 'bitmap-index';
62
4
 
63
5
  type ComponentOptions = {
64
6
  name: string;
65
7
  };
66
8
  declare function Component(options: ComponentOptions): ClassDecorator;
67
9
 
68
- type EntityID = number;
69
- declare class UnregisteredComponentStorageError extends Error {
70
- constructor(component: ComponentType);
71
- }
72
- declare class EntityMaskNotFoundError extends Error {
73
- constructor(id: EntityID);
74
- }
75
- declare class EntitiesManager {
76
- private readonly logger;
77
- private id_;
78
- constructor(logger: Logger);
79
- private nextId;
80
- create(): EntityID;
81
- }
82
- declare class EntityRef {
83
- private world;
84
- id: EntityID;
85
- constructor(world: World, id: EntityID);
86
- with<T extends ComponentType[], Result extends {
87
- [K in keyof T]: InstanceType<T[K]>;
88
- }>(...components: T): Result;
89
- }
90
-
91
- declare class EventBuffer<T extends unknown> {
92
- private readBuf;
93
- private writeBuf;
94
- write(event: T): void;
95
- /**
96
- * Advances the buffer to the next frame.
97
- *
98
- * Performs a double-buffer flip:
99
- * - Promotes all events written during the previous frame (`writeBuf`)
100
- * to be readable in the current frame (`readBuf`).
101
- * - Reuses the previous `readBuf` as the new `writeBuf` and clears it
102
- * to collect events for the next frame.
103
- *
104
- * After calling this method:
105
- * - `get()` will return a stable snapshot of events produced in the previous frame.
106
- * - `add()` will write into an empty buffer for the current frame.
107
- *
108
- * Guarantees:
109
- * - No events written during the current frame are visible until the next `swap()`.
110
- * - Readers observe a consistent, immutable snapshot within a frame.
111
- *
112
- * Expected to be called exactly once per frame, before system execution.
113
- */
114
- swap(): void;
115
- read(): ReadonlyArray<T>;
116
- size(): number;
117
- }
118
- type EventKey<T> = symbol & {
119
- __type?: T;
120
- };
121
- declare function createEventKey<T>(description?: string): EventKey<T>;
122
- declare class EventBus {
123
- private storage;
124
- swapAll(): void;
125
- getBuffer<T>(key: EventKey<T>): EventBuffer<T>;
126
- }
127
-
128
- type ResourceMetadata = {
129
- name: string;
130
- };
131
- type ResourceParams = {
132
- name: string;
133
- };
134
- declare function Resource(params: ResourceParams): ClassDecorator;
135
- declare function getResourceMetadata(resource: Function): ResourceMetadata;
136
- declare class ResourcesManager {
137
- private readonly logger;
138
- private readonly items_;
139
- constructor(logger: Logger);
140
- insert<T extends object>(type: ClassType<T>, value: T): T;
141
- get<T extends object>(type: ClassType<T>): T;
142
- getOrInsert<T extends object>(type: ClassType<T>, factory: () => T): T;
143
- remove<T>(type: ClassType<T>): void;
144
- }
145
-
146
- type WorldCommand = (world: World) => void;
147
- type ComponentInitFn<C extends ComponentType> = (component: InstanceType<C>) => void;
148
- type CreateEntityComponentEntry<T extends ComponentType = ComponentType> = T extends unknown ? [T, ComponentInitFn<T>] : never;
149
- declare function entry<T extends ComponentType>(component: T, init?: ComponentInitFn<T>): CreateEntityComponentEntry;
150
- declare class Commands {
151
- private readonly world;
152
- private readonly logger;
153
- private readonly commandsQueue_;
154
- constructor(world: World, logger: Logger);
155
- add(cmd: WorldCommand): void;
156
- flush(world: World): void;
157
- createEntity(...entries: CreateEntityComponentEntry[]): number;
158
- }
159
-
160
- type QueryParameters = {
161
- include?: ComponentType[];
162
- exclude?: ComponentType[];
163
- anyOf?: ComponentType[];
164
- excludeEntitiesIds?: number[];
165
- filter?: (id: number) => boolean;
166
- };
167
- declare class QueryManager {
168
- private readonly world;
169
- private cache;
170
- constructor(world: World);
171
- get(params: QueryParameters): EntityID[];
172
- invalidate(component: ComponentType): void;
173
- private getKey;
174
- private ids;
175
- private collectDeps;
176
- private compute;
177
- private combineBitmaps;
178
- private applyExclusions;
179
- private extractIds;
180
- }
181
-
182
- type PluginID = string;
183
- type PluginConstructor = ClassType<PluginBase>;
184
- type PluginDependencies = {
185
- components?: ComponentType[];
186
- resources?: ClassType<any>[];
187
- systems?: ClassType<SystemBase>[];
188
- plugins?: Array<{
189
- plugin: PluginConstructor;
190
- version?: string;
191
- }>;
192
- };
193
- interface PluginMetadata {
194
- id?: PluginID;
195
- version: string;
196
- name: string;
197
- dependencies?: PluginDependencies;
198
- }
199
- declare function Plugin(metadata: PluginMetadata): ClassDecorator;
200
- declare function getPluginMetadata(plugin: PluginConstructor): PluginMetadata;
201
- declare function isPlugin(ctor: Function): boolean;
202
- declare abstract class PluginBase {
203
- onPluginLoad?: () => void;
204
- onPluginUnload?: (world: World) => void;
205
- onAfterWorldInit?: (world: World) => void;
206
- }
207
- declare class PluginError extends Error {
208
- constructor(pluginId: PluginConstructor);
209
- }
210
- declare class ErrNotAPlugin extends Error {
211
- constructor(target: Function);
212
- }
213
- declare class ErrMissingPluginMetadata extends Error {
214
- constructor(plugin: PluginConstructor);
215
- }
216
- declare class ErrUnknownPlugin extends PluginError {
217
- constructor(pluginId: PluginConstructor);
218
- }
219
- declare class ErrPluginNotInit extends PluginError {
220
- constructor(pluginId: PluginConstructor);
221
- }
222
- declare class PluginsManager {
223
- private readonly logger;
224
- private plugins_;
225
- private isInitiated_;
226
- constructor(logger: Logger);
227
- install<T extends PluginConstructor>(plugin: T, ...constructorProps: ConstructorParameters<T>): void;
228
- build(): void;
229
- /**
230
- * @internal
231
- */
232
- __internal__onAfterWorldInit(world: World): void;
233
- getPluginMetadata(plugin: PluginConstructor): PluginMetadata;
234
- getPluginInstance<T extends PluginBase>(plugin: ClassType<T>): T;
235
- }
236
-
237
- interface TimeSource {
238
- now(): number;
239
- }
240
- declare class Clock {
241
- private readonly timeSource_;
242
- private lastTimeMs_;
243
- private ellapsedTime_;
244
- private delta_;
245
- private readonly time_;
246
- constructor(timeSource_: TimeSource);
247
- get deltaMs(): number;
248
- get ellapsedTime(): number;
249
- tick(): void;
250
- getTime(): Readonly<Time>;
251
- }
252
- type Time = {
253
- delta: number;
254
- elapsed: number;
255
- };
256
-
257
10
  type StepFunction = (dt: number, world: World) => void;
258
11
  type PlatformLoop = (callback: () => void) => void;
259
12
  declare class Loop {
@@ -272,136 +25,6 @@ declare class Runtime {
272
25
  run(world: World): void;
273
26
  }
274
27
 
275
- type WorldConstructor = {
276
- maxEntityCount?: number;
277
- logger: Logger;
278
- };
279
- declare class World {
280
- readonly entities: EntitiesManager;
281
- readonly components: ComponentsManager;
282
- readonly systems: SystemsManager;
283
- readonly events: EventBus;
284
- readonly resources: ResourcesManager;
285
- readonly commands: Commands;
286
- readonly queries: QueryManager;
287
- readonly plugins: PluginsManager;
288
- private readonly logger;
289
- private entityRefs_;
290
- private updatesCount_;
291
- get updatesCount(): number;
292
- constructor(params: WorldConstructor);
293
- getEntityRef(id: number): EntityRef;
294
- query(params: QueryParameters): number[];
295
- removeComponent<T extends object>(ref: EntityRef, component: ComponentType<T>): void;
296
- removeComponent<T extends object>(entity: EntityID, component: ComponentType<T>): void;
297
- addComponent<T extends object>(id: EntityID, component: ClassType<T>, initFn?: (obj: T) => void): T;
298
- addComponent<T extends object>(id: EntityRef, component: ClassType<T>, initFn?: (obj: T) => void): T;
299
- update(clock: Clock): void;
300
- build(): void;
301
- }
302
-
303
- declare class SystemError extends Error {
304
- constructor(target: Function);
305
- }
306
- declare class ErrNotASystem extends Error {
307
- constructor(target: Function);
308
- }
309
- declare class ErrMissingSystemMetadata extends SystemError {
310
- constructor(target: SystemCtor);
311
- }
312
- declare enum SystemPhase {
313
- PRE = 0,
314
- MAIN = 1,
315
- POST = 2
316
- }
317
- type SystemMetadata = {
318
- /**
319
- * ECS query definition used to select entities for this system execution.
320
- *
321
- * Determines the iteration set passed to {@link SystemBase.compute}.
322
- * The ECS runtime resolves entities based on this query each update.
323
- */
324
- query: Readonly<QueryParameters>;
325
- /**
326
- * Explicit list of component types required by the system but not necessarily
327
- * part of the iteration query.
328
- *
329
- * Used for:
330
- * - ensuring component storages are initialized in the world
331
- * - safe access via `world.components.getStorage`
332
- * - dependencies that should not affect entity selection
333
- *
334
- * This does NOT influence entity iteration; only runtime validation/setup.
335
- */
336
- requiredComponents: Set<ComponentType>;
337
- /**
338
- * Systems that must run before this one. Pass constructor arguments
339
- * `super(OtherSystemCtor, ...)` to add edges; each listed system is scheduled earlier than
340
- * this instance. Used when {@link SystemsManager.build} computes a topological execution order.
341
- */
342
- computeAfter?: Set<SystemCtor>;
343
- phase?: SystemPhase;
344
- name: string;
345
- };
346
- type SystemDecoratorProps = {
347
- query: SystemMetadata['query'];
348
- requiredComponents?: ComponentType[];
349
- computeAfter?: SystemCtor[];
350
- phase?: SystemPhase;
351
- name: SystemMetadata['name'];
352
- };
353
- declare function System(props: SystemDecoratorProps): ClassDecorator;
354
- declare function getSystemMetadata(system: SystemCtor): SystemMetadata;
355
- declare function isSystem(ctor: Function): boolean;
356
- type SystemCtor<T extends SystemBase = SystemBase> = ClassType<T>;
357
- /**
358
- * Arguments passed to {@link SystemBase.compute} on each {@link SystemsManager.update} call.
359
- */
360
- type SystemComputeContext = {
361
- /** Entity IDs from the query for this {@link SystemBase.compute} invocation. */
362
- readonly entities: number[];
363
- /** ECS world instance. */
364
- readonly world: World;
365
- /** Delta time (seconds or your engine's convention) since the previous update. */
366
- readonly time: Time;
367
- /** Logger instance for debugging and diagnostics. */
368
- readonly logger: Logger;
369
- };
370
- type SystemInitContext = {
371
- world: World;
372
- logger: Logger;
373
- };
374
- /**
375
- * Base class for ECS systems executed by {@link SystemsManager}.
376
- *
377
- * Subclasses declare which components they iterate over (`queryComponents`), which component
378
- * types must exist in the world for registration (`requiredComponents`), and optional ordering
379
- * relative to other systems (`computeAfter` / `super(OtherSystem)`).
380
- */
381
- declare abstract class SystemBase {
382
- /**
383
- * Logic for one systems pass: run for all entities in {@link SystemComputeContext.entities}.
384
- */
385
- abstract compute(ctx: SystemComputeContext): void;
386
- onInit?(ctx: SystemInitContext): void;
387
- }
388
- declare class SystemsManager {
389
- private readonly world;
390
- private readonly logger;
391
- private systems_;
392
- private executionOrder_;
393
- private requiredComponents_;
394
- private dirty_;
395
- constructor(world: World, logger: Logger);
396
- getRequiredComponents(): ComponentType[];
397
- register<T extends SystemBase>(sys: T): void;
398
- build(): void;
399
- private rebuild;
400
- get<T extends SystemBase>(ctor: SystemCtor<T>): T;
401
- update(time: Time): void;
402
- private buildSystemsArray;
403
- }
404
-
405
28
  declare enum VisitedState {
406
29
  Unvisited = 0,
407
30
  Visiting = 1,
@@ -482,14 +105,18 @@ type EngineConstructor = {
482
105
  logger?: Logger;
483
106
  maxEntityCount?: number;
484
107
  };
485
- declare class Engine {
108
+ interface TickProvider {
109
+ getTick(): number;
110
+ }
111
+ declare class Engine implements TickProvider {
486
112
  readonly runtime: Runtime;
487
113
  readonly assets: AssetsManager;
488
114
  readonly world: World;
489
115
  readonly logger: Logger;
490
116
  constructor(params: EngineConstructor);
117
+ getTick(): number;
491
118
  init(): void;
492
119
  start(): void;
493
120
  }
494
121
 
495
- 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, LogLevel, type LogMessage, type Logger, Loop, ObjectPool, type PlatformLoop, Plugin, PluginBase, type PluginDependencies, PluginError, type PluginID, type PluginMetadata, PluginsManager, Resource, type ResourceMetadata, ResourcesManager, Runtime, type StepFunction, System, SystemBase, type SystemComputeContext, SystemError, type SystemInitContext, SystemPhase, SystemsManager, type TimeSource, UnregisteredComponentStorageError, VisitedState, World, type WorldCommand, createEventKey, entry, getPluginMetadata, getResourceMetadata, getSystemMetadata, isPlugin, isSystem, topologicalSort };
122
+ export { Asset, type AssetDisposer, type AssetID, type AssetIDGenerator, type AssetLoader, AssetState, AssetStorage, AssetsManager, ClassType, Clock, Component, DAGNode, Engine, type EngineConstructor, ErrDAGCycleDetected, Logger, Loop, ObjectPool, type PlatformLoop, Runtime, type StepFunction, type TickProvider, VisitedState, World, topologicalSort };