@draug/engine 1.0.20 → 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,260 +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
- private resolveId;
236
- }
237
-
238
- interface TimeSource {
239
- now(): number;
240
- }
241
- declare class Clock {
242
- private readonly timeSource_;
243
- private lastTimeMs_;
244
- private ellapsedTime_;
245
- private delta_;
246
- private readonly time_;
247
- constructor(timeSource_: TimeSource);
248
- get deltaMs(): number;
249
- get ellapsedTime(): number;
250
- tick(): void;
251
- getTime(): Readonly<Time>;
252
- }
253
- type Time = {
254
- delta: number;
255
- elapsed: number;
256
- };
257
-
258
10
  type StepFunction = (dt: number, world: World) => void;
259
11
  type PlatformLoop = (callback: () => void) => void;
260
12
  declare class Loop {
@@ -273,136 +25,6 @@ declare class Runtime {
273
25
  run(world: World): void;
274
26
  }
275
27
 
276
- type WorldConstructor = {
277
- maxEntityCount?: number;
278
- logger: Logger;
279
- };
280
- declare class World {
281
- readonly entities: EntitiesManager;
282
- readonly components: ComponentsManager;
283
- readonly systems: SystemsManager;
284
- readonly events: EventBus;
285
- readonly resources: ResourcesManager;
286
- readonly commands: Commands;
287
- readonly queries: QueryManager;
288
- readonly plugins: PluginsManager;
289
- private readonly logger;
290
- private entityRefs_;
291
- private updatesCount_;
292
- get updatesCount(): number;
293
- constructor(params: WorldConstructor);
294
- getEntityRef(id: number): EntityRef;
295
- query(params: QueryParameters): number[];
296
- removeComponent<T extends object>(ref: EntityRef, component: ComponentType<T>): void;
297
- removeComponent<T extends object>(entity: EntityID, component: ComponentType<T>): void;
298
- addComponent<T extends object>(id: EntityID, component: ClassType<T>, initFn?: (obj: T) => void): T;
299
- addComponent<T extends object>(id: EntityRef, component: ClassType<T>, initFn?: (obj: T) => void): T;
300
- update(clock: Clock): void;
301
- build(): void;
302
- }
303
-
304
- declare class SystemError extends Error {
305
- constructor(target: Function);
306
- }
307
- declare class ErrNotASystem extends Error {
308
- constructor(target: Function);
309
- }
310
- declare class ErrMissingSystemMetadata extends SystemError {
311
- constructor(target: SystemCtor);
312
- }
313
- declare enum SystemPhase {
314
- PRE = 0,
315
- MAIN = 1,
316
- POST = 2
317
- }
318
- type SystemMetadata = {
319
- /**
320
- * ECS query definition used to select entities for this system execution.
321
- *
322
- * Determines the iteration set passed to {@link SystemBase.compute}.
323
- * The ECS runtime resolves entities based on this query each update.
324
- */
325
- query: Readonly<QueryParameters>;
326
- /**
327
- * Explicit list of component types required by the system but not necessarily
328
- * part of the iteration query.
329
- *
330
- * Used for:
331
- * - ensuring component storages are initialized in the world
332
- * - safe access via `world.components.getStorage`
333
- * - dependencies that should not affect entity selection
334
- *
335
- * This does NOT influence entity iteration; only runtime validation/setup.
336
- */
337
- requiredComponents: Set<ComponentType>;
338
- /**
339
- * Systems that must run before this one. Pass constructor arguments
340
- * `super(OtherSystemCtor, ...)` to add edges; each listed system is scheduled earlier than
341
- * this instance. Used when {@link SystemsManager.build} computes a topological execution order.
342
- */
343
- computeAfter?: Set<SystemCtor>;
344
- phase?: SystemPhase;
345
- name: string;
346
- };
347
- type SystemDecoratorProps = {
348
- query: SystemMetadata['query'];
349
- requiredComponents?: ComponentType[];
350
- computeAfter?: SystemCtor[];
351
- phase?: SystemPhase;
352
- name: SystemMetadata['name'];
353
- };
354
- declare function System(props: SystemDecoratorProps): ClassDecorator;
355
- declare function getSystemMetadata(system: SystemCtor): SystemMetadata;
356
- declare function isSystem(ctor: Function): boolean;
357
- type SystemCtor<T extends SystemBase = SystemBase> = ClassType<T>;
358
- /**
359
- * Arguments passed to {@link SystemBase.compute} on each {@link SystemsManager.update} call.
360
- */
361
- type SystemComputeContext = {
362
- /** Entity IDs from the query for this {@link SystemBase.compute} invocation. */
363
- readonly entities: number[];
364
- /** ECS world instance. */
365
- readonly world: World;
366
- /** Delta time (seconds or your engine's convention) since the previous update. */
367
- readonly time: Time;
368
- /** Logger instance for debugging and diagnostics. */
369
- readonly logger: Logger;
370
- };
371
- type SystemInitContext = {
372
- world: World;
373
- logger: Logger;
374
- };
375
- /**
376
- * Base class for ECS systems executed by {@link SystemsManager}.
377
- *
378
- * Subclasses declare which components they iterate over (`queryComponents`), which component
379
- * types must exist in the world for registration (`requiredComponents`), and optional ordering
380
- * relative to other systems (`computeAfter` / `super(OtherSystem)`).
381
- */
382
- declare abstract class SystemBase {
383
- /**
384
- * Logic for one systems pass: run for all entities in {@link SystemComputeContext.entities}.
385
- */
386
- abstract compute(ctx: SystemComputeContext): void;
387
- onInit?(ctx: SystemInitContext): void;
388
- }
389
- declare class SystemsManager {
390
- private readonly world;
391
- private readonly logger;
392
- private systems_;
393
- private executionOrder_;
394
- private requiredComponents_;
395
- private dirty_;
396
- constructor(world: World, logger: Logger);
397
- getRequiredComponents(): ComponentType[];
398
- register<T extends SystemBase>(sys: T): void;
399
- build(): void;
400
- private rebuild;
401
- get<T extends SystemBase>(ctor: SystemCtor<T>): T;
402
- update(time: Time): void;
403
- private buildSystemsArray;
404
- }
405
-
406
28
  declare enum VisitedState {
407
29
  Unvisited = 0,
408
30
  Visiting = 1,
@@ -483,14 +105,18 @@ type EngineConstructor = {
483
105
  logger?: Logger;
484
106
  maxEntityCount?: number;
485
107
  };
486
- declare class Engine {
108
+ interface TickProvider {
109
+ getTick(): number;
110
+ }
111
+ declare class Engine implements TickProvider {
487
112
  readonly runtime: Runtime;
488
113
  readonly assets: AssetsManager;
489
114
  readonly world: World;
490
115
  readonly logger: Logger;
491
116
  constructor(params: EngineConstructor);
117
+ getTick(): number;
492
118
  init(): void;
493
119
  start(): void;
494
120
  }
495
121
 
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, 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 };