ecspresso 0.0.8 → 0.1.1
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/README.md +168 -363
- package/dist/bundle.d.ts +14 -8
- package/dist/ecspresso.d.ts +110 -34
- package/dist/entity-manager.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/index.js.map +7 -7
- package/dist/resource-manager.d.ts +6 -6
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2,454 +2,259 @@
|
|
|
2
2
|
|
|
3
3
|
*(pronounced "ex-presso")*
|
|
4
4
|
|
|
5
|
-
__Note: This is a VERY early work in progress. The documention is being autogenerated while
|
|
5
|
+
__Note: This is a VERY early work in progress. 0 work on performance has been done while the API is being nailed down. The documention is also being autogenerated while ECSpresso is being iterated on.__
|
|
6
6
|
|
|
7
|
-
A
|
|
7
|
+
A type-safe, modular, and extensible Entity Component System (ECS) framework for TypeScript.
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Features
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- **
|
|
14
|
-
- **Event
|
|
15
|
-
- **Resource Management**: Global
|
|
16
|
-
- **Bundle System**: Modular and reusable collections of components, resources, and systems
|
|
17
|
-
- **Query System**: Efficient entity filtering based on component presence/absence
|
|
18
|
-
- **Flexible System Creation**: Create systems directly or through bundles
|
|
19
|
-
|
|
20
|
-
## Core Concepts
|
|
21
|
-
|
|
22
|
-
### Entity Component System
|
|
23
|
-
|
|
24
|
-
The ECS pattern separates data (Components) from behavior (Systems) through Entities:
|
|
25
|
-
|
|
26
|
-
- **Entities**: Unique identifiers that components can be attached to
|
|
27
|
-
- **Components**: Plain data objects that hold state but no behavior
|
|
28
|
-
- **Systems**: Logic that processes entities with specific components
|
|
29
|
-
- **Resources**: Global state shared between systems
|
|
30
|
-
- **Events**: Messages passed between systems
|
|
31
|
-
|
|
32
|
-
### Key Features
|
|
33
|
-
|
|
34
|
-
- Type-safe queries to filter entities based on component combinations
|
|
35
|
-
- Fluent builder API for creating systems directly or through bundles
|
|
36
|
-
- Event handling with lifecycle hooks for systems
|
|
37
|
-
- Resource management for global state
|
|
38
|
-
- Simplified system API - all system methods receive the ECSpresso instance
|
|
39
|
-
|
|
40
|
-
### System API Design
|
|
41
|
-
|
|
42
|
-
All system methods (process, onAttach, onDetach, and event handlers) receive the ECSpresso instance as a parameter, which provides:
|
|
43
|
-
|
|
44
|
-
- Access to entity management via `ecspresso.entityManager`
|
|
45
|
-
- Access to resources via `ecspresso.resourceManager`
|
|
46
|
-
- Access to events via `ecspresso.eventBus`
|
|
47
|
-
|
|
48
|
-
This design simplifies the API and allows systems access to all ECS functionality through a single reference. Benefits of this approach include:
|
|
49
|
-
|
|
50
|
-
- **Simpler method signatures**: Systems only need to deal with one additional parameter (the ECS instance) rather than multiple managers
|
|
51
|
-
- **Future extensibility**: New functionality added to the ECSpresso class is automatically available to all systems without changing method signatures
|
|
52
|
-
- **Consistency**: All system methods (process, lifecycle hooks, event handlers) use the same parameter pattern
|
|
53
|
-
- **Reduced verbosity**: Systems can be written more concisely while still having access to all ECS functionality
|
|
11
|
+
- 🔒 **Type-Safe**: Full TypeScript support with type inference for components, events, and resources
|
|
12
|
+
- 🧩 **Modular**: Bundle-based architecture for modular gameplay systems and features
|
|
13
|
+
- 💡 **Flexible**: Easily create entities, add components, and build systems with a clean, fluent API
|
|
14
|
+
- 🔄 **Event-Driven**: Integrated event bus for communication between systems
|
|
15
|
+
- 🗄️ **Resource Management**: Global resources for sharing state across systems
|
|
54
16
|
|
|
55
17
|
## Installation
|
|
56
18
|
|
|
57
|
-
```
|
|
58
|
-
# Add the library to your project
|
|
19
|
+
```sh
|
|
59
20
|
npm install ecspresso
|
|
60
21
|
```
|
|
61
22
|
|
|
62
|
-
##
|
|
63
|
-
|
|
64
|
-
### Basic Setup
|
|
23
|
+
## Quick Start
|
|
65
24
|
|
|
66
25
|
```typescript
|
|
67
|
-
import
|
|
26
|
+
import { ECSpresso } from 'ecspresso';
|
|
68
27
|
|
|
69
28
|
// Define your component types
|
|
70
|
-
interface
|
|
29
|
+
interface Components {
|
|
71
30
|
position: { x: number; y: number };
|
|
72
|
-
velocity: {
|
|
73
|
-
sprite: { url: string
|
|
31
|
+
velocity: { x: number; y: number };
|
|
32
|
+
sprite: { url: string };
|
|
74
33
|
}
|
|
75
34
|
|
|
76
35
|
// Define your event types
|
|
77
|
-
interface
|
|
78
|
-
collision: {
|
|
79
|
-
|
|
36
|
+
interface Events {
|
|
37
|
+
collision: { entity1: number; entity2: number };
|
|
38
|
+
scoreChange: { amount: number };
|
|
80
39
|
}
|
|
81
40
|
|
|
82
41
|
// Define your resource types
|
|
83
|
-
interface
|
|
84
|
-
|
|
85
|
-
|
|
42
|
+
interface Resources {
|
|
43
|
+
score: { value: number };
|
|
44
|
+
gameState: 'playing' | 'paused' | 'gameOver';
|
|
86
45
|
}
|
|
87
46
|
|
|
88
|
-
// Create
|
|
89
|
-
const world = new ECSpresso<
|
|
90
|
-
|
|
91
|
-
// Add resources
|
|
92
|
-
world.addResource('gameState', { score: 0, level: 1 });
|
|
93
|
-
world.addResource('config', { debug: true, maxEntities: 1000 });
|
|
47
|
+
// Create a world instance directly
|
|
48
|
+
const world = new ECSpresso<Components, Events, Resources>();
|
|
94
49
|
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// Add components to the entity
|
|
99
|
-
world.entityManager.addComponent(entity.id, 'position', { x: 0, y: 0 });
|
|
100
|
-
world.entityManager.addComponent(entity.id, 'velocity', { dx: 1, dy: 2 });
|
|
101
|
-
|
|
102
|
-
// Or add multiple components at once
|
|
103
|
-
world.entityManager.addComponents(entity.id, {
|
|
104
|
-
position: { x: 0, y: 0 },
|
|
105
|
-
velocity: { dx: 1, dy: 2 }
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
// Run the simulation
|
|
109
|
-
world.update(16.67); // Pass delta time in ms
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Creating Systems Directly
|
|
113
|
-
|
|
114
|
-
ECSpresso allows you to create systems directly on the ECSpresso instance:
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
// Create a movement system directly on the ECSpresso instance
|
|
118
|
-
world
|
|
119
|
-
.addSystem('movement')
|
|
120
|
-
.addQuery('movable', {
|
|
50
|
+
// Add a movement system directly to the world
|
|
51
|
+
world.addSystem('movement')
|
|
52
|
+
.addQuery('movingEntities', {
|
|
121
53
|
with: ['position', 'velocity']
|
|
122
54
|
})
|
|
123
|
-
.setProcess((queries, deltaTime
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
position.x += velocity.dx * (deltaTime / 1000);
|
|
128
|
-
position.y += velocity.dy * (deltaTime / 1000);
|
|
55
|
+
.setProcess((queries, deltaTime) => {
|
|
56
|
+
for (const entity of queries.movingEntities) {
|
|
57
|
+
entity.components.position.x += entity.components.velocity.x * deltaTime;
|
|
58
|
+
entity.components.position.y += entity.components.velocity.y * deltaTime;
|
|
129
59
|
}
|
|
130
60
|
})
|
|
131
|
-
.build(); //
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
The `build()` method finalizes the system and registers it with the ECSpresso instance. Any lifecycle hooks like `onAttach` will be called immediately when the system is built.
|
|
61
|
+
.build(); // Don't forget to call build() to finalize the system
|
|
135
62
|
|
|
136
|
-
|
|
63
|
+
// Create an entity with position and velocity components
|
|
64
|
+
const entity = world.entityManager.createEntity();
|
|
65
|
+
world.entityManager.addComponent(entity.id, 'position', { x: 0, y: 0 });
|
|
66
|
+
world.entityManager.addComponent(entity.id, 'velocity', { x: 10, y: 5 });
|
|
137
67
|
|
|
138
|
-
|
|
68
|
+
// Run a single update
|
|
69
|
+
world.update(1/60);
|
|
139
70
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
.addSystem('renderer')
|
|
144
|
-
.addQuery('sprites', {
|
|
145
|
-
with: ['position', 'sprite']
|
|
146
|
-
})
|
|
147
|
-
.setOnAttach((ecs) => {
|
|
148
|
-
console.log('Render system attached');
|
|
149
|
-
// Initialize rendering resources
|
|
150
|
-
})
|
|
151
|
-
.setOnDetach((ecs) => {
|
|
152
|
-
console.log('Render system detached');
|
|
153
|
-
// Clean up rendering resources
|
|
154
|
-
})
|
|
155
|
-
.setProcess((queries, deltaTime, ecs) => {
|
|
156
|
-
// Render all sprites at their positions
|
|
157
|
-
for (const entity of queries.sprites) {
|
|
158
|
-
const { position, sprite } = entity.components;
|
|
159
|
-
// Render sprite at position
|
|
160
|
-
}
|
|
161
|
-
})
|
|
162
|
-
.build();
|
|
71
|
+
// Check new position
|
|
72
|
+
const position = world.entityManager.getComponent(entity.id, 'position');
|
|
73
|
+
console.log(position); // { x: 0.16666..., y: 0.08333... }
|
|
163
74
|
```
|
|
164
75
|
|
|
165
|
-
|
|
76
|
+
## Building Modular Systems with Bundles
|
|
166
77
|
|
|
167
|
-
|
|
78
|
+
Bundles are a powerful way to organize game features:
|
|
168
79
|
|
|
169
80
|
```typescript
|
|
170
|
-
// Create a
|
|
171
|
-
|
|
172
|
-
.addSystem('
|
|
173
|
-
.
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const gameState = ecs.getResourceOrThrow('gameState');
|
|
178
|
-
gameState.score += 10;
|
|
179
|
-
|
|
180
|
-
console.log(`Score: ${gameState.score}`);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
})
|
|
184
|
-
.build();
|
|
185
|
-
|
|
186
|
-
// Emit an event from another system
|
|
187
|
-
world.eventBus.publish('collision', { entityA: entity.id, entityB: 2 });
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
### Using Bundles for System Organization
|
|
191
|
-
|
|
192
|
-
Bundles allow you to group related systems, resources, and components:
|
|
81
|
+
// Create a player input bundle
|
|
82
|
+
const inputBundle = new Bundle<Components, Events, Resources>()
|
|
83
|
+
.addSystem('playerInput')
|
|
84
|
+
.setProcess((_queries, _deltaTime, ecs) => {
|
|
85
|
+
// Handle keyboard input and modify player velocity
|
|
86
|
+
// ...
|
|
87
|
+
});
|
|
193
88
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
with: ['position']
|
|
203
|
-
})
|
|
204
|
-
.setProcess((queries, deltaTime, ecs) => {
|
|
205
|
-
// Check for collisions between entities
|
|
206
|
-
for (const entity of queries.collidable) {
|
|
207
|
-
// Collision detection logic
|
|
89
|
+
// Create a rendering bundle
|
|
90
|
+
const renderBundle = new Bundle<Components, Events, Resources>()
|
|
91
|
+
.addSystem('renderer')
|
|
92
|
+
.addQuery('sprites', { with: ['position', 'sprite'] })
|
|
93
|
+
.setProcess((queries) => {
|
|
94
|
+
// Render all sprites
|
|
95
|
+
for (const entity of queries.sprites) {
|
|
96
|
+
// Draw entities at their positions
|
|
208
97
|
// ...
|
|
209
|
-
|
|
210
|
-
// Emit collision events when detected
|
|
211
|
-
ecs.eventBus.publish('collision', {
|
|
212
|
-
entityA: entity.id,
|
|
213
|
-
entityB: otherEntity.id
|
|
214
|
-
});
|
|
215
98
|
}
|
|
216
99
|
});
|
|
217
100
|
|
|
218
|
-
//
|
|
219
|
-
|
|
220
|
-
.
|
|
221
|
-
.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
})
|
|
231
|
-
.bundle // Access bundle for chaining to add another system
|
|
232
|
-
.addSystem('friction')
|
|
233
|
-
.addQuery('moving', {
|
|
234
|
-
with: ['velocity']
|
|
235
|
-
})
|
|
236
|
-
.setProcess((queries, deltaTime, ecs) => {
|
|
237
|
-
for (const entity of queries.moving) {
|
|
238
|
-
const { velocity } = entity.components;
|
|
239
|
-
// Apply friction
|
|
240
|
-
velocity.dx *= 0.99;
|
|
241
|
-
velocity.dy *= 0.99;
|
|
101
|
+
// Create a scoring bundle that adds a resource and listens for events
|
|
102
|
+
const scoringBundle = new Bundle<Components, Events, Resources>()
|
|
103
|
+
.addResource('score', { value: 0 })
|
|
104
|
+
.addSystem('scoreKeeper')
|
|
105
|
+
.setEventHandlers({
|
|
106
|
+
scoreChange: {
|
|
107
|
+
handler: (data, ecs) => {
|
|
108
|
+
const score = ecs.getResource('score');
|
|
109
|
+
score.value += data.amount;
|
|
110
|
+
console.log(`Score: ${score.value}`);
|
|
111
|
+
}
|
|
242
112
|
}
|
|
243
113
|
});
|
|
244
114
|
|
|
245
|
-
//
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
.addSystem('newSystem')
|
|
252
|
-
.addQuery('withBundleComponents', {
|
|
253
|
-
// Component types from the bundle are now recognized
|
|
254
|
-
with: ['position', 'velocity']
|
|
255
|
-
})
|
|
256
|
-
.setProcess((queries, deltaTime, ecs) => {
|
|
257
|
-
// Process entities using components from the bundle
|
|
258
|
-
})
|
|
115
|
+
// Create the game world with all features using the builder pattern
|
|
116
|
+
const game = ECSpresso.create<Components, Events, Resources>()
|
|
117
|
+
.withBundle(physicsBundle)
|
|
118
|
+
.withBundle(inputBundle)
|
|
119
|
+
.withBundle(renderBundle)
|
|
120
|
+
.withBundle(scoringBundle)
|
|
259
121
|
.build();
|
|
260
122
|
```
|
|
261
123
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
### System Chaining with ECSpresso
|
|
124
|
+
## Type Safety with the Builder Pattern
|
|
265
125
|
|
|
266
|
-
|
|
126
|
+
ECSpresso uses a builder pattern to provide strong type checking for bundle compatibility:
|
|
267
127
|
|
|
268
128
|
```typescript
|
|
269
|
-
//
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
.
|
|
276
|
-
|
|
277
|
-
})
|
|
278
|
-
.build()
|
|
279
|
-
.ecspresso // Access the ECSpresso instance after building
|
|
280
|
-
.addSystem('enemySystem')
|
|
281
|
-
.addQuery('enemies', {
|
|
282
|
-
with: ['position', 'velocity']
|
|
283
|
-
})
|
|
284
|
-
.setProcess((queries, deltaTime, ecs) => {
|
|
285
|
-
// Process enemy entities
|
|
286
|
-
})
|
|
129
|
+
// These bundles have compatible component types
|
|
130
|
+
const bundle1 = new Bundle<{position: {x: number, y: number}}, {}, {}>();
|
|
131
|
+
const bundle2 = new Bundle<{velocity: {x: number, y: number}}, {}, {}>();
|
|
132
|
+
|
|
133
|
+
// Create a world with both bundles - TypeScript will allow this
|
|
134
|
+
const world = ECSpresso.create()
|
|
135
|
+
.withBundle(bundle1)
|
|
136
|
+
.withBundle(bundle2)
|
|
287
137
|
.build();
|
|
288
|
-
```
|
|
289
138
|
|
|
290
|
-
|
|
139
|
+
// These bundles have conflicting component types
|
|
140
|
+
const bundle3 = new Bundle<{position: {x: number, y: number}}, {}, {}>();
|
|
141
|
+
const bundle4 = new Bundle<{position: string}, {}, {}>();
|
|
291
142
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
// Create individual feature bundles with their own type definitions
|
|
298
|
-
const physicsBundle = new Bundle<{ position: {x: number, y: number}, velocity: {dx: number, dy: number} }, {}, {}>('physics');
|
|
299
|
-
const renderBundle = new Bundle<{ sprite: {url: string, width: number, height: number} }, {}, {}>('render');
|
|
300
|
-
const aiBundle = new Bundle<{}, { enemySpotted: {entityId: number} }, {}>('ai');
|
|
301
|
-
|
|
302
|
-
// Add systems to each bundle
|
|
303
|
-
// ...
|
|
304
|
-
|
|
305
|
-
// Merge bundles into a game bundle
|
|
306
|
-
// Type information is automatically combined!
|
|
307
|
-
const gameBundle = mergeBundles(
|
|
308
|
-
'game',
|
|
309
|
-
physicsBundle,
|
|
310
|
-
renderBundle,
|
|
311
|
-
aiBundle
|
|
312
|
-
);
|
|
313
|
-
|
|
314
|
-
// Install the merged bundle
|
|
315
|
-
// The ECSpresso instance now knows about all components, events, and resources
|
|
316
|
-
// from all the merged bundles
|
|
317
|
-
const typedWorld = world.install(gameBundle);
|
|
318
|
-
|
|
319
|
-
// TypeScript fully recognizes all the component types from all bundles
|
|
320
|
-
typedWorld
|
|
321
|
-
.addSystem('combinedSystem')
|
|
322
|
-
.addQuery('renderableMovingEntities', {
|
|
323
|
-
// Can use components from both physics and render bundles
|
|
324
|
-
with: ['position', 'velocity', 'sprite'],
|
|
325
|
-
})
|
|
326
|
-
.setEventHandlers({
|
|
327
|
-
// Can use events from the AI bundle
|
|
328
|
-
enemySpotted: {
|
|
329
|
-
handler: (event, ecs) => {
|
|
330
|
-
// Handle enemy spotted event
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
})
|
|
143
|
+
// TypeScript will show an error because bundles have conflicting types
|
|
144
|
+
const world2 = ECSpresso.create()
|
|
145
|
+
.withBundle(bundle3)
|
|
146
|
+
// @ts-expect-error - TypeScript will flag this because the position types conflict
|
|
147
|
+
.withBundle(bundle4)
|
|
334
148
|
.build();
|
|
335
149
|
```
|
|
336
150
|
|
|
337
|
-
##
|
|
338
|
-
|
|
339
|
-
### Removing Systems
|
|
340
|
-
|
|
341
|
-
You can remove systems by their label:
|
|
151
|
+
## Working with Entities and Components
|
|
342
152
|
|
|
343
153
|
```typescript
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
```
|
|
154
|
+
const world = ECSpresso.create<Components, Events, Resources>()
|
|
155
|
+
.withBundle(/* your bundle */)
|
|
156
|
+
.build();
|
|
348
157
|
|
|
349
|
-
|
|
158
|
+
// Create an entity
|
|
159
|
+
const entity = world.entityManager.createEntity();
|
|
350
160
|
|
|
351
|
-
|
|
161
|
+
// Add components individually
|
|
162
|
+
world.entityManager.addComponent(entity.id, 'position', { x: 0, y: 0 });
|
|
163
|
+
world.entityManager.addComponent(entity.id, 'velocity', { x: 0, y: 0 });
|
|
352
164
|
|
|
353
|
-
|
|
165
|
+
// Add multiple components at once
|
|
166
|
+
world.entityManager.addComponents(entity, {
|
|
167
|
+
position: { x: 10, y: 20 },
|
|
168
|
+
velocity: { x: 5, y: -2 }
|
|
169
|
+
});
|
|
354
170
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
const uiBundle = new Bundle<MyComponents, MyEvents, MyResources>('ui');
|
|
171
|
+
// Get component data
|
|
172
|
+
const position = world.entityManager.getComponent(entity.id, 'position');
|
|
358
173
|
|
|
359
|
-
//
|
|
360
|
-
|
|
361
|
-
menuOpen: false,
|
|
362
|
-
currentScreen: 'main'
|
|
363
|
-
});
|
|
174
|
+
// Check if an entity has a component
|
|
175
|
+
const hasPosition = world.entityManager.hasComponent(entity.id, 'position');
|
|
364
176
|
|
|
365
|
-
//
|
|
366
|
-
|
|
367
|
-
.addSystem('uiRenderer')
|
|
368
|
-
// ...
|
|
177
|
+
// Remove a component
|
|
178
|
+
world.entityManager.removeComponent(entity.id, 'velocity');
|
|
369
179
|
|
|
370
|
-
//
|
|
371
|
-
world.
|
|
180
|
+
// Remove an entity (and all its components)
|
|
181
|
+
world.entityManager.removeEntity(entity.id);
|
|
372
182
|
```
|
|
373
183
|
|
|
374
|
-
##
|
|
184
|
+
## Working with Systems
|
|
375
185
|
|
|
376
|
-
|
|
186
|
+
Systems can be added directly to an ECSpresso instance:
|
|
377
187
|
|
|
378
188
|
```typescript
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
189
|
+
const world = ECSpresso.create<Components, Events, Resources>()
|
|
190
|
+
.build();
|
|
191
|
+
|
|
192
|
+
world.addSystem('physicsSystem')
|
|
193
|
+
.addQuery('movingEntities', {
|
|
383
194
|
with: ['position', 'velocity']
|
|
384
195
|
})
|
|
385
|
-
.setProcess((queries, deltaTime
|
|
386
|
-
for (const entity of queries.
|
|
387
|
-
//
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
// This would cause a TypeScript error - sprite not guaranteed to exist
|
|
392
|
-
// const sprite = entity.components.sprite;
|
|
196
|
+
.setProcess((queries, deltaTime) => {
|
|
197
|
+
for (const entity of queries.movingEntities) {
|
|
198
|
+
// Update positions based on velocity
|
|
199
|
+
entity.components.position.x += entity.components.velocity.x * deltaTime;
|
|
200
|
+
entity.components.position.y += entity.components.velocity.y * deltaTime;
|
|
393
201
|
}
|
|
394
202
|
})
|
|
395
|
-
.build();
|
|
203
|
+
.build(); // Finalizes and adds the system to the world
|
|
396
204
|
```
|
|
397
205
|
|
|
398
|
-
|
|
206
|
+
## Event System
|
|
399
207
|
|
|
400
|
-
|
|
208
|
+
The event system allows communication between systems:
|
|
401
209
|
|
|
402
210
|
```typescript
|
|
403
|
-
//
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
//
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
413
|
-
const weaponsBundle = new Bundle<WeaponComponents, {}, {}>('weapons');
|
|
414
|
-
|
|
415
|
-
// When installed, the returned ECSpresso instance has combined types
|
|
416
|
-
const enhancedWorld = world.install(weaponsBundle);
|
|
417
|
-
// Type is now: ECSpresso<BaseComponents & WeaponComponents, {}, {}>
|
|
418
|
-
|
|
419
|
-
// Full type safety with components from both the base and bundle
|
|
420
|
-
enhancedWorld
|
|
421
|
-
.addSystem('combatSystem')
|
|
422
|
-
.addQuery('fighters', {
|
|
423
|
-
// TypeScript recognizes both component types
|
|
424
|
-
with: ['health', 'weapon']
|
|
425
|
-
})
|
|
426
|
-
.setProcess((queries, deltaTime, ecs) => {
|
|
427
|
-
for (const entity of queries.fighters) {
|
|
428
|
-
// Type-safe access to components from both sources
|
|
429
|
-
const health = entity.components.health; // From BaseComponents
|
|
430
|
-
const weapon = entity.components.weapon; // From WeaponComponents
|
|
431
|
-
|
|
432
|
-
// TypeScript provides full intellisense for these combined types
|
|
433
|
-
console.log(`Entity with ${health.current}/${health.max} HP and ${weapon.damage} damage`);
|
|
211
|
+
// Define an event handler in a system
|
|
212
|
+
const collisionBundle = new Bundle<Components, Events, Resources>()
|
|
213
|
+
.addSystem('collisionResponse')
|
|
214
|
+
.setEventHandlers({
|
|
215
|
+
collision: {
|
|
216
|
+
handler: (data, ecs) => {
|
|
217
|
+
// Handle collision event
|
|
218
|
+
// data contains entity1 and entity2 from the event
|
|
219
|
+
}
|
|
434
220
|
}
|
|
435
|
-
})
|
|
436
|
-
.build();
|
|
437
|
-
```
|
|
221
|
+
});
|
|
438
222
|
|
|
439
|
-
|
|
223
|
+
const world = ECSpresso.create<Components, Events, Resources>()
|
|
224
|
+
.withBundle(collisionBundle)
|
|
225
|
+
.build();
|
|
440
226
|
|
|
441
|
-
|
|
227
|
+
// Publish an event from anywhere
|
|
228
|
+
world.eventBus.publish('collision', {
|
|
229
|
+
entity1: 1,
|
|
230
|
+
entity2: 2
|
|
231
|
+
});
|
|
442
232
|
|
|
443
|
-
|
|
233
|
+
// Subscribe to events manually (outside of systems)
|
|
234
|
+
const unsubscribe = world.eventBus.subscribe('collision', (data) => {
|
|
235
|
+
console.log(`Collision between entities ${data.entity1} and ${data.entity2}`);
|
|
236
|
+
});
|
|
444
237
|
|
|
445
|
-
|
|
446
|
-
|
|
238
|
+
// Stop listening
|
|
239
|
+
unsubscribe();
|
|
447
240
|
```
|
|
448
241
|
|
|
449
|
-
|
|
242
|
+
## Resources
|
|
450
243
|
|
|
451
|
-
|
|
452
|
-
|
|
244
|
+
Resources provide global state accessible to all systems:
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
// Add a resource
|
|
248
|
+
world.addResource('score', { value: 0 });
|
|
249
|
+
|
|
250
|
+
// Get a resource
|
|
251
|
+
const score = world.getResource('score');
|
|
252
|
+
score.value += 10;
|
|
253
|
+
|
|
254
|
+
// Check if a resource exists
|
|
255
|
+
const hasScore = world.hasResource('score');
|
|
453
256
|
```
|
|
454
257
|
|
|
455
|
-
|
|
258
|
+
## License
|
|
259
|
+
|
|
260
|
+
MIT
|
package/dist/bundle.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { SystemBuilder } from './system-builder';
|
|
2
2
|
import type ECSpresso from './ecspresso';
|
|
3
|
-
import { MergeAll } from './types';
|
|
4
3
|
/**
|
|
5
4
|
* Bundle class that encapsulates a set of components, resources, events, and systems
|
|
6
5
|
* that can be merged into a ECSpresso instance
|
|
@@ -60,10 +59,17 @@ export default class Bundle<ComponentTypes extends Record<string, any> = Record<
|
|
|
60
59
|
*/
|
|
61
60
|
hasResource<K extends keyof ResourceTypes>(key: K): boolean;
|
|
62
61
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
62
|
+
type Exactly<T, U> = T extends U ? U extends T ? true : false : false;
|
|
63
|
+
type IncompatibleBundles<C1 extends Record<string, any>, C2 extends Record<string, any>, E1 extends Record<string, any>, E2 extends Record<string, any>, R1 extends Record<string, any>, R2 extends Record<string, any>> = {
|
|
64
|
+
[K in keyof C1 & keyof C2]: Exactly<C1[K], C2[K]> extends false ? never : unknown;
|
|
65
|
+
} & {
|
|
66
|
+
[K in keyof E1 & keyof E2]: Exactly<E1[K], E2[K]> extends false ? never : unknown;
|
|
67
|
+
} & {
|
|
68
|
+
[K in keyof R1 & keyof R2]: Exactly<R1[K], R2[K]> extends false ? never : unknown;
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Function that merges multiple bundles into a single bundle
|
|
72
|
+
*/
|
|
73
|
+
export declare function mergeBundles<C1 extends Record<string, any>, E1 extends Record<string, any>, R1 extends Record<string, any>, C2 extends Record<string, any>, E2 extends Record<string, any>, R2 extends Record<string, any>>(id: string, bundle1: Bundle<C1, E1, R1>, bundle2: Bundle<C2, E2, R2> & IncompatibleBundles<C1, C2, E1, E2, R1, R2>): Bundle<C1 & C2, E1 & E2, R1 & R2>;
|
|
74
|
+
export declare function mergeBundles<ComponentTypes extends Record<string, any>, EventTypes extends Record<string, any>, ResourceTypes extends Record<string, any>>(id: string, ...bundles: Array<Bundle<ComponentTypes, EventTypes, ResourceTypes>>): Bundle<ComponentTypes, EventTypes, ResourceTypes>;
|
|
75
|
+
export {};
|
package/dist/ecspresso.d.ts
CHANGED
|
@@ -1,42 +1,106 @@
|
|
|
1
1
|
import EntityManager from "./entity-manager";
|
|
2
2
|
import EventBus from "./event-bus";
|
|
3
3
|
import ResourceManager from "./resource-manager";
|
|
4
|
-
import type { MergeAll } from "./types";
|
|
5
4
|
import type Bundle from "./bundle";
|
|
6
5
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
6
|
+
* Type helper to detect conflicting types between two record types.
|
|
7
|
+
* Returns a union of keys that exist in both T and U but have incompatible types.
|
|
8
|
+
*/
|
|
9
|
+
type GetConflictingKeys<T, U> = {
|
|
10
|
+
[K in keyof T & keyof U]: T[K] extends U[K] ? U[K] extends T[K] ? never : K : K;
|
|
11
|
+
}[keyof T & keyof U];
|
|
12
|
+
/**
|
|
13
|
+
* Simplified type helper to check bundle type compatibility.
|
|
14
|
+
* Returns true if bundles can be merged without type conflicts.
|
|
15
|
+
*/
|
|
16
|
+
type BundlesAreCompatible<C1 extends Record<string, any>, C2 extends Record<string, any>, E1 extends Record<string, any>, E2 extends Record<string, any>, R1 extends Record<string, any>, R2 extends Record<string, any>> = [
|
|
17
|
+
keyof C1
|
|
18
|
+
] extends [never] ? [keyof E1] extends [never] ? [keyof R1] extends [never] ? true : GetConflictingKeys<R1, R2> extends never ? true : false : GetConflictingKeys<E1, E2> extends never ? GetConflictingKeys<R1, R2> extends never ? true : false : false : GetConflictingKeys<C1, C2> extends never ? GetConflictingKeys<E1, E2> extends never ? GetConflictingKeys<R1, R2> extends never ? true : false : false : false;
|
|
19
|
+
/**
|
|
20
|
+
* Interface declaration for ECSpresso constructor to ensure type augmentation works properly.
|
|
21
|
+
* This merges with the class declaration below.
|
|
22
|
+
*/
|
|
23
|
+
export default interface ECSpresso<ComponentTypes extends Record<string, any> = Record<string, any>, EventTypes extends Record<string, any> = Record<string, any>, ResourceTypes extends Record<string, any> = Record<string, any>> {
|
|
24
|
+
/**
|
|
25
|
+
* Default constructor
|
|
26
|
+
*/
|
|
27
|
+
new (): ECSpresso<ComponentTypes, EventTypes, ResourceTypes>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Static methods on the ECSpresso class
|
|
31
|
+
*/
|
|
32
|
+
export default interface ECSpresso {
|
|
33
|
+
/**
|
|
34
|
+
* Create a new ECSpresso builder with type-safe bundle installation.
|
|
35
|
+
* This is the preferred way to create an ECSpresso instance with bundles.
|
|
36
|
+
*
|
|
37
|
+
* Example:
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const ecs = ECSpresso.create<MyComponents, MyEvents, MyResources>()
|
|
40
|
+
* .withBundle(bundle1)
|
|
41
|
+
* .withBundle(bundle2)
|
|
42
|
+
* .build();
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
create(): ECSpressoBuilder<{}, {}, {}>;
|
|
46
|
+
/**
|
|
47
|
+
* Create a new ECSpresso builder with type-safe bundle installation and explicit starting types.
|
|
48
|
+
*/
|
|
49
|
+
create<BaseC extends Record<string, any>, BaseE extends Record<string, any>, BaseR extends Record<string, any>>(): ECSpressoBuilder<BaseC, BaseE, BaseR>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* ECSpresso is the central ECS framework class that connects all features.
|
|
53
|
+
* It handles creation and management of entities, components, and systems, and provides lifecycle hooks.
|
|
18
54
|
*/
|
|
19
55
|
export default class ECSpresso<ComponentTypes extends Record<string, any> = Record<string, any>, EventTypes extends Record<string, any> = Record<string, any>, ResourceTypes extends Record<string, any> = Record<string, any>> {
|
|
56
|
+
/** Library version */
|
|
20
57
|
static readonly VERSION: string;
|
|
58
|
+
/** Access/modify stored components and entities */
|
|
21
59
|
private _entityManager;
|
|
22
|
-
|
|
60
|
+
/** Publish/subscribe to events */
|
|
23
61
|
private _eventBus;
|
|
62
|
+
/** Access/modify registered resources */
|
|
24
63
|
private _resourceManager;
|
|
64
|
+
/** Registered systems that will be updated in order */
|
|
65
|
+
private _systems;
|
|
66
|
+
/** Track installed bundles to prevent duplicates */
|
|
25
67
|
private _installedBundles;
|
|
68
|
+
/**
|
|
69
|
+
* Creates a new ECSpresso instance.
|
|
70
|
+
*/
|
|
26
71
|
constructor();
|
|
27
72
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* @returns A
|
|
73
|
+
* Creates a new ECSpresso builder for type-safe bundle installation.
|
|
74
|
+
* This is the preferred way to create an ECSpresso instance with bundles.
|
|
75
|
+
*
|
|
76
|
+
* @returns A builder instance for fluent method chaining
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const ecs = ECSpresso.create<BaseComponents, BaseEvents, BaseResources>()
|
|
81
|
+
* .withBundle(bundle1)
|
|
82
|
+
* .withBundle(bundle2)
|
|
83
|
+
* .build();
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
static create<C extends Record<string, any> = {}, E extends Record<string, any> = {}, R extends Record<string, any> = {}>(): ECSpressoBuilder<C, E, R>;
|
|
87
|
+
/**
|
|
88
|
+
* Adds a system directly to this ECSpresso instance
|
|
89
|
+
* @param label Unique name to identify the system
|
|
90
|
+
* @returns A SystemBuilder instance for method chaining
|
|
91
|
+
*/
|
|
92
|
+
addSystem(label: string): import("./system-builder").SystemBuilderWithEcspresso<ComponentTypes, EventTypes, ResourceTypes, {}>;
|
|
93
|
+
/**
|
|
94
|
+
* Update all systems, passing deltaTime and query results to each system's process function
|
|
95
|
+
* @param deltaTime Time elapsed since the last update (in seconds)
|
|
32
96
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
97
|
+
update(deltaTime: number): void;
|
|
98
|
+
/**
|
|
99
|
+
* Internal method to install a bundle into this ECSpresso instance.
|
|
100
|
+
* Called by the ECSpressoBuilder during the build process.
|
|
101
|
+
* The type safety is guaranteed by the builder's type system.
|
|
102
|
+
*/
|
|
103
|
+
_installBundle<C extends Record<string, any>, E extends Record<string, any>, R extends Record<string, any>>(bundle: Bundle<C, E, R>): this;
|
|
40
104
|
/**
|
|
41
105
|
* Remove a system by its label
|
|
42
106
|
* Calls the system's onDetach method with this ECSpresso instance if defined
|
|
@@ -69,22 +133,34 @@ export default class ECSpresso<ComponentTypes extends Record<string, any> = Reco
|
|
|
69
133
|
*/
|
|
70
134
|
getEntitiesWithComponents(withComponents: (keyof ComponentTypes)[], withoutComponents?: (keyof ComponentTypes)[]): import("./types").FilteredEntity<ComponentTypes, keyof ComponentTypes extends infer T ? T extends keyof ComponentTypes ? T extends never ? never : T : never : never, keyof ComponentTypes extends infer T_1 ? T_1 extends keyof ComponentTypes ? T_1 extends never ? never : T_1 : never : never>[];
|
|
71
135
|
/**
|
|
72
|
-
*
|
|
73
|
-
* Calls each system's process method with this ECSpresso instance
|
|
74
|
-
* @param deltaTime Time elapsed since the last update in seconds
|
|
136
|
+
* Get all installed bundle IDs
|
|
75
137
|
*/
|
|
76
|
-
|
|
138
|
+
get installedBundles(): string[];
|
|
77
139
|
get entityManager(): EntityManager<ComponentTypes>;
|
|
78
140
|
get eventBus(): EventBus<EventTypes>;
|
|
79
141
|
get resourceManager(): ResourceManager<ResourceTypes>;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Builder class for ECSpresso that provides fluent type-safe bundle installation.
|
|
145
|
+
* Handles type checking during build process to ensure type safety.
|
|
146
|
+
*/
|
|
147
|
+
export declare class ECSpressoBuilder<C extends Record<string, any> = {}, E extends Record<string, any> = {}, R extends Record<string, any> = {}> {
|
|
148
|
+
/** The ECSpresso instance being built */
|
|
149
|
+
private ecspresso;
|
|
150
|
+
constructor();
|
|
80
151
|
/**
|
|
81
|
-
*
|
|
152
|
+
* Add the first bundle when starting with empty types.
|
|
153
|
+
* This overload allows any bundle to be added to an empty ECSpresso instance.
|
|
82
154
|
*/
|
|
83
|
-
|
|
155
|
+
withBundle<BC extends Record<string, any>, BE extends Record<string, any>, BR extends Record<string, any>>(this: ECSpressoBuilder<{}, {}, {}>, bundle: Bundle<BC, BE, BR>): ECSpressoBuilder<BC, BE, BR>;
|
|
84
156
|
/**
|
|
85
|
-
* Add a
|
|
86
|
-
*
|
|
87
|
-
* @returns A SystemBuilder instance for method chaining
|
|
157
|
+
* Add a subsequent bundle with type checking.
|
|
158
|
+
* This overload enforces bundle type compatibility.
|
|
88
159
|
*/
|
|
89
|
-
|
|
160
|
+
withBundle<BC extends Record<string, any>, BE extends Record<string, any>, BR extends Record<string, any>>(bundle: BundlesAreCompatible<C, BC, E, BE, R, BR> extends true ? Bundle<BC, BE, BR> : never): ECSpressoBuilder<C & BC, E & BE, R & BR>;
|
|
161
|
+
/**
|
|
162
|
+
* Complete the build process and return the built ECSpresso instance
|
|
163
|
+
*/
|
|
164
|
+
build(): ECSpresso<C, E, R>;
|
|
90
165
|
}
|
|
166
|
+
export {};
|
package/dist/entity-manager.d.ts
CHANGED
|
@@ -10,12 +10,12 @@ export default class EntityManager<ComponentTypes> {
|
|
|
10
10
|
* @param entityOrId Entity or entity ID to add components to
|
|
11
11
|
* @param components Object with component names as keys and component data as values
|
|
12
12
|
*/
|
|
13
|
-
addComponents
|
|
14
|
-
[
|
|
15
|
-
}>): this;
|
|
13
|
+
addComponents<T extends {
|
|
14
|
+
[K in keyof ComponentTypes]?: ComponentTypes[K];
|
|
15
|
+
}>(entityOrId: number | Entity<ComponentTypes>, components: T & Record<Exclude<keyof T, keyof ComponentTypes>, never>): this;
|
|
16
16
|
removeComponent<ComponentName extends keyof ComponentTypes>(entityId: number, componentName: ComponentName): void;
|
|
17
17
|
getComponent<ComponentName extends keyof ComponentTypes>(entityId: number, componentName: ComponentName): ComponentTypes[ComponentName] | null;
|
|
18
18
|
getEntitiesWithComponents<WithComponents extends keyof ComponentTypes = never, WithoutComponents extends keyof ComponentTypes = never>(required?: ReadonlyArray<WithComponents>, excluded?: ReadonlyArray<WithoutComponents>): Array<FilteredEntity<ComponentTypes, WithComponents extends never ? never : WithComponents, WithoutComponents extends never ? never : WithoutComponents>>;
|
|
19
|
-
removeEntity(
|
|
19
|
+
removeEntity(entityOrId: number | Entity<ComponentTypes>): boolean;
|
|
20
20
|
getEntity(entityId: number): Entity<ComponentTypes> | undefined;
|
|
21
21
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
class M{nextId=1;entities=new Map;componentIndices=new Map;createEntity(){let j=this.nextId++,g={id:j,components:{}};return this.entities.set(j,g),g}addComponent(j,g,f){let F=typeof j==="number"?this.entities.get(j):j;if(!F)throw new Error(`Entity ${j} does not exist`);if(F.components[g]=f,!this.componentIndices.has(g))this.componentIndices.set(g,new Set);return this.componentIndices.get(g)?.add(F.id),this}addComponents(j,g){let f=typeof j==="number"?this.entities.get(j):j;if(!f)throw new Error(`Entity ${j} does not exist`);for(let F in g)
|
|
1
|
+
class M{nextId=1;entities=new Map;componentIndices=new Map;createEntity(){let j=this.nextId++,g={id:j,components:{}};return this.entities.set(j,g),g}addComponent(j,g,f){let F=typeof j==="number"?this.entities.get(j):j;if(!F)throw new Error(`Entity ${j} does not exist`);if(F.components[g]=f,!this.componentIndices.has(g))this.componentIndices.set(g,new Set);return this.componentIndices.get(g)?.add(F.id),this}addComponents(j,g){let f=typeof j==="number"?this.entities.get(j):j;if(!f)throw new Error(`Entity ${j} does not exist`);for(let F in g)this.addComponent(f,F,g[F]);return this}removeComponent(j,g){let f=this.entities.get(j);if(!f)throw new Error(`Entity ${j} does not exist`);delete f.components[g],this.componentIndices.get(g)?.delete(j)}getComponent(j,g){let f=this.entities.get(j);if(!f)throw new Error(`Entity ${j} does not exist`);return f.components[g]||null}getEntitiesWithComponents(j=[],g=[]){if(j.length===0){if(g.length===0)return Array.from(this.entities.values());return Array.from(this.entities.values()).filter((D)=>{return g.every((J)=>!(J in D.components))})}let f=j.reduce((D,J)=>{let L=this.componentIndices.get(J),G=L?L.size:0,H=this.componentIndices.get(D)?.size??1/0;return G<H?J:D},j[0]);return Array.from(this.componentIndices.get(f)||[]).filter((D)=>{let J=this.entities.get(D);return J&&j.every((L)=>(L in J.components))&&g.every((L)=>!(L in J.components))}).map((D)=>this.entities.get(D))}removeEntity(j){let g=typeof j==="number"?this.entities.get(j):j;if(!g)return!1;for(let f of Object.keys(g.components))this.componentIndices.get(f)?.delete(g.id);return this.entities.delete(g.id)}getEntity(j){return this.entities.get(j)}}class P{handlers=new Map;subscribe(j,g){return this.addHandler(j,g,!1)}once(j,g){return this.addHandler(j,g,!0)}addHandler(j,g,f){if(!this.handlers.has(j))this.handlers.set(j,[]);let F={callback:g,once:f};return this.handlers.get(j).push(F),()=>{let D=this.handlers.get(j);if(D){let J=D.indexOf(F);if(J!==-1)D.splice(J,1)}}}publish(j,g){let f=this.handlers.get(j);if(!f)return;let F=[...f],D=[];for(let J of F)if(J.callback(g),J.once)D.push(J);if(D.length>0)for(let J of D){let L=f.indexOf(J);if(L!==-1)f.splice(L,1)}}clear(){this.handlers.clear()}clearEvent(j){this.handlers.delete(j)}}class Q{resources=new Map;add(j,g){return this.resources.set(j,g),this}get(j){let g=this.resources.get(j);if(g===void 0)throw new Error(`Resource ${String(j)} not found`);return g}getOptional(j){return this.resources.get(j)}has(j){return this.resources.has(j)}remove(j){return this.resources.delete(j)}getKeys(){return Array.from(this.resources.keys())}}class X{_label;_ecspresso;_bundle;queries={};processFunction;attachFunction;detachFunction;eventHandlers;constructor(j,g=null,f=null){this._label=j;this._ecspresso=g;this._bundle=f}get label(){return this._label}get bundle(){return this._bundle}get ecspresso(){return this._ecspresso}addQuery(j,g){let f=this;return f.queries={...this.queries,[j]:g},f}setProcess(j){return this.processFunction=j,this}setOnAttach(j){return this.attachFunction=j,this}setOnDetach(j){return this.detachFunction=j,this}setEventHandlers(j){return this.eventHandlers=j,this}build(j){let g={label:this._label,entityQueries:this.queries};if(this.processFunction)g.process=this.processFunction;if(this.attachFunction)g.onAttach=this.attachFunction;if(this.detachFunction)g.onDetach=this.detachFunction;if(this.eventHandlers)g.eventHandlers=this.eventHandlers;if(this._ecspresso)Y(g,this._ecspresso);if(j)Y(g,j);return this}}function Y(j,g){if(g._systems.push(j),j.onAttach)j.onAttach(g);if(j.eventHandlers)for(let f in j.eventHandlers){let F=j.eventHandlers[f];if(F?.handler){let D=(J)=>{F.handler(J,g)};g.eventBus.subscribe(f,D)}}}function Z(j,g){return new X(j,g)}function _(j,g){return new X(j,null,g)}var $="0.1.1";class U{static VERSION=$;_entityManager;_eventBus;_resourceManager;_systems=[];_installedBundles=new Set;constructor(){this._entityManager=new M,this._eventBus=new P,this._resourceManager=new Q}static create(){return new x}addSystem(j){return Z(j,this)}update(j){for(let g of this._systems){if(!g.process)continue;let f={};if(g.entityQueries)for(let F in g.entityQueries){let D=g.entityQueries[F];if(D)f[F]=this._entityManager.getEntitiesWithComponents(D.with,D.without||[])}g.process(f,j,this)}}_installBundle(j){if(this._installedBundles.has(j.id))return this;this._installedBundles.add(j.id),j.registerSystemsWithEcspresso(this);let g=j.getResources();for(let[f,F]of g.entries())this._resourceManager.add(f,F);return this}removeSystem(j){let g=this._systems.findIndex((F)=>F.label===j);if(g===-1)return!1;let f=this._systems[g];if(!f)return!1;if(f.onDetach)f.onDetach(this);return this._systems.splice(g,1),!0}hasResource(j){return this._resourceManager.has(j)}getResource(j){return this._resourceManager.getOptional(j)}getResourceOrThrow(j){return this._resourceManager.get(j)}addResource(j,g){return this._resourceManager.add(j,g),this}hasComponent(j,g){return this._entityManager.getComponent(j,g)!==null}getEntitiesWithComponents(j,g=[]){return this._entityManager.getEntitiesWithComponents(j,g)}get installedBundles(){return Array.from(this._installedBundles)}get entityManager(){return this._entityManager}get eventBus(){return this._eventBus}get resourceManager(){return this._resourceManager}}class x{ecspresso;constructor(){this.ecspresso=new U}withBundle(j){return this.ecspresso._installBundle(j),this}build(){return this.ecspresso}}function W(){return`bundle_${Date.now().toString(36)}_${Math.random().toString(36).substring(2,9)}`}class V{_systems=[];_resources=new Map;_id;constructor(j){this._id=j||W()}get id(){return this._id}set id(j){this._id=j}addSystem(j){let g=_(j,this);return this._systems.push(g),g}addResource(j,g){return this._resources.set(j,g),this}getSystems(){return this._systems.map((j)=>j.build())}registerSystemsWithEcspresso(j){for(let g of this._systems)g.build(j)}getResources(){return new Map(this._resources)}getResource(j){return this._resources.get(j)}getSystemBuilders(){return[...this._systems]}hasResource(j){return this._resources.has(j)}}function A(j,...g){if(g.length===0)return new V(j);let f=new V(j);for(let F of g){for(let D of F.getSystemBuilders())f.addSystem(D);for(let[D,J]of F.getResources().entries())f.addResource(D,J)}return f}var o=U;export{A as mergeBundles,o as default,X as SystemBuilder,Q as ResourceManager,P as EventBus,M as EntityManager,V as Bundle};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=BDBE8C30396183E764756E2164756E21
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/entity-manager.ts", "../src/event-bus.ts", "../src/resource-manager.ts", "../src/system-builder.ts", "../src/ecspresso.ts", "../src/bundle.ts", "../src/index.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { Entity, FilteredEntity } from \"./types\";\n\nexport default\nclass EntityManager<ComponentTypes> {\n\tprivate nextId: number = 1;\n\tprivate entities: Map<number, Entity<ComponentTypes>> = new Map();\n\tprivate componentIndices: Map<keyof ComponentTypes, Set<number>> = new Map();\n\n\tcreateEntity(): Entity<ComponentTypes> {\n\t\tconst id = this.nextId++;\n\t\tconst entity: Entity<ComponentTypes> = { id, components: {} };\n\t\tthis.entities.set(id, entity);\n\t\treturn entity;\n\t}\n\n\t// TODO: Component object pooling if(/when) garbage collection is an issue...?\n\taddComponent<ComponentName extends keyof ComponentTypes>(\n\t\tentityOrId: number | Entity<ComponentTypes>,\n\t\tcomponentName: ComponentName,\n\t\tdata: ComponentTypes[ComponentName]\n\t) {\n\t\tconst entity = typeof entityOrId === 'number' ?\n\t\t\tthis.entities.get(entityOrId) :\n\t\t\tentityOrId;\n\n\t\tif (!entity) throw new Error(`Entity ${entityOrId} does not exist`);\n\n\t\tentity.components[componentName] = data;\n\n\t\t// Update component index\n\t\tif (!this.componentIndices.has(componentName)) {\n\t\t\tthis.componentIndices.set(componentName, new Set());\n\t\t}\n\t\tthis.componentIndices.get(componentName)?.add(entity.id);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add multiple components to an entity at once\n\t * @param entityOrId Entity or entity ID to add components to\n\t * @param components Object with component names as keys and component data as values\n\t */\n\taddComponents(\n\t\tentityOrId: number | Entity<ComponentTypes>,\n\t\tcomponents:
|
|
6
|
-
"import type { EventHandler } from \"./types\";\n\nexport default\nclass EventBus<EventTypes> {\n\tprivate handlers: Map<
|
|
7
|
-
"export default\nclass ResourceManager<ResourceTypes extends Record<string, any> = Record<string, any>> {\n\tprivate resources: Map<
|
|
5
|
+
"import type { Entity, FilteredEntity } from \"./types\";\n\nexport default\nclass EntityManager<ComponentTypes> {\n\tprivate nextId: number = 1;\n\tprivate entities: Map<number, Entity<ComponentTypes>> = new Map();\n\tprivate componentIndices: Map<keyof ComponentTypes, Set<number>> = new Map();\n\n\tcreateEntity(): Entity<ComponentTypes> {\n\t\tconst id = this.nextId++;\n\t\tconst entity: Entity<ComponentTypes> = { id, components: {} };\n\t\tthis.entities.set(id, entity);\n\t\treturn entity;\n\t}\n\n\t// TODO: Component object pooling if(/when) garbage collection is an issue...?\n\taddComponent<ComponentName extends keyof ComponentTypes>(\n\t\tentityOrId: number | Entity<ComponentTypes>,\n\t\tcomponentName: ComponentName,\n\t\tdata: ComponentTypes[ComponentName]\n\t) {\n\t\tconst entity = typeof entityOrId === 'number' ?\n\t\t\tthis.entities.get(entityOrId) :\n\t\t\tentityOrId;\n\n\t\tif (!entity) throw new Error(`Entity ${entityOrId} does not exist`);\n\n\t\tentity.components[componentName] = data;\n\n\t\t// Update component index\n\t\tif (!this.componentIndices.has(componentName)) {\n\t\t\tthis.componentIndices.set(componentName, new Set());\n\t\t}\n\t\tthis.componentIndices.get(componentName)?.add(entity.id);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Add multiple components to an entity at once\n\t * @param entityOrId Entity or entity ID to add components to\n\t * @param components Object with component names as keys and component data as values\n\t */\n\taddComponents<\n\t\tT extends { [K in keyof ComponentTypes]?: ComponentTypes[K] }\n\t>(\n\t\tentityOrId: number | Entity<ComponentTypes>,\n\t\tcomponents: T & Record<Exclude<keyof T, keyof ComponentTypes>, never>\n\t) {\n\t\tconst entity = typeof entityOrId === 'number' ?\n\t\t\tthis.entities.get(entityOrId) :\n\t\t\tentityOrId;\n\n\t\tif (!entity) throw new Error(`Entity ${entityOrId} does not exist`);\n\n\t\tfor (const componentName in components) {\n\t\t\tthis.addComponent(\n\t\t\t\tentity,\n\t\t\t\tcomponentName as keyof ComponentTypes,\n\t\t\t\tcomponents[componentName as keyof T] as ComponentTypes[keyof ComponentTypes]\n\t\t\t);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\tremoveComponent<ComponentName extends keyof ComponentTypes>(entityId: number, componentName: ComponentName) {\n\t\tconst entity = this.entities.get(entityId);\n\t\tif (!entity) throw new Error(`Entity ${entityId} does not exist`);\n\n\t\tdelete entity.components[componentName];\n\n\t\t// Update component index\n\t\tthis.componentIndices.get(componentName)?.delete(entityId);\n\t}\n\n\tgetComponent<ComponentName extends keyof ComponentTypes>(entityId: number, componentName: ComponentName): ComponentTypes[ComponentName] | null {\n\t\tconst entity = this.entities.get(entityId);\n\n\t\tif (!entity) throw new Error(`Entity ${entityId} does not exist`);\n\n\t\treturn entity.components[componentName] || null;\n\t}\n\n\tgetEntitiesWithComponents<\n\t\tWithComponents extends keyof ComponentTypes = never,\n\t\tWithoutComponents extends keyof ComponentTypes = never\n\t>(\n\t\trequired: ReadonlyArray<WithComponents> = [],\n\t\texcluded: ReadonlyArray<WithoutComponents> = [],\n\t): Array<FilteredEntity<ComponentTypes, WithComponents extends never ? never : WithComponents, WithoutComponents extends never ? never : WithoutComponents>> {\n\t\t// Use the smallest component set as base for better performance\n\t\tif (required.length === 0) {\n\t\t\tif (excluded.length === 0) {\n\t\t\t\treturn Array.from(this.entities.values()) as any;\n\t\t\t}\n\n\t\t\treturn Array\n\t\t\t\t.from(this.entities.values())\n\t\t\t\t.filter((entity) => {\n\t\t\t\t\treturn excluded.every(comp => !(comp in entity.components));\n\t\t\t\t}) as any;\n\t\t}\n\n\t\t// Find the component with the smallest entity set to start with\n\t\tconst smallestComponent = required.reduce((smallest, comp) => {\n\t\t\tconst set = this.componentIndices.get(comp);\n\t\t\tconst currentSize = set ? set.size : 0;\n\t\t\tconst smallestSize = this.componentIndices.get(smallest!)?.size ?? Infinity;\n\n\t\t\treturn currentSize < smallestSize ? comp : smallest;\n\t\t}, required[0])!;\n\n\t\t// Start with the entities from the smallest component set\n\t\tconst candidates = Array.from(this.componentIndices.get(smallestComponent) || []);\n\n\t\t// Return full entity objects, not just IDs\n\t\treturn candidates\n\t\t\t.filter(id => {\n\t\t\t\tconst entity = this.entities.get(id);\n\t\t\t\treturn (\n\t\t\t\t\tentity &&\n\t\t\t\t\trequired.every(comp => comp in entity.components) &&\n\t\t\t\t\texcluded.every(comp => !(comp in entity.components))\n\t\t\t\t);\n\t\t\t})\n\t\t\t.map(id => this.entities.get(id)!) as Array<FilteredEntity<ComponentTypes, WithComponents extends never ? never : WithComponents, WithoutComponents extends never ? never : WithoutComponents>>;\n\t}\n\n\tremoveEntity(entityOrId: number | Entity<ComponentTypes>): boolean {\n\t\tconst entity = typeof entityOrId === 'number' ?\n\t\t\tthis.entities.get(entityOrId) :\n\t\t\tentityOrId;\n\n\t\tif (!entity) return false;\n\n\t\t// Remove entity from all component indices\n\t\tfor (const componentName of Object.keys(entity.components) as Array<keyof ComponentTypes>) {\n\t\t\tthis.componentIndices.get(componentName)?.delete(entity.id);\n\t\t}\n\n\t\t// Remove the entity itself\n\t\treturn this.entities.delete(entity.id);\n\t}\n\n\tgetEntity(entityId: number): Entity<ComponentTypes> | undefined {\n\t\treturn this.entities.get(entityId);\n\t}\n}\n",
|
|
6
|
+
"import type { EventHandler } from \"./types\";\n\nexport default\nclass EventBus<EventTypes> {\n\tprivate handlers: Map<string, Array<EventHandler<any>>> = new Map();\n\n\t/**\n\t * Subscribe to an event\n\t */\n\tsubscribe<E extends keyof EventTypes>(\n\t\teventType: E,\n\t\tcallback: (data: EventTypes[E]) => void\n\t): () => void {\n\t\treturn this.addHandler(eventType, callback, false);\n\t}\n\n\t/**\n\t * Subscribe to an event once\n\t */\n\tonce<E extends keyof EventTypes>(\n\t\teventType: E,\n\t\tcallback: (data: EventTypes[E]) => void\n\t): () => void {\n\t\treturn this.addHandler(eventType, callback, true);\n\t}\n\n\t/**\n\t * Internal method to add an event handler\n\t */\n\tprivate addHandler<E extends keyof EventTypes>(\n\t\teventType: E,\n\t\tcallback: (data: EventTypes[E]) => void,\n\t\tonce: boolean\n\t): () => void {\n\t\tif (!this.handlers.has(eventType as string)) {\n\t\t\tthis.handlers.set(eventType as string, []);\n\t\t}\n\n\t\tconst handler: EventHandler<any> = {\n\t\t\tcallback,\n\t\t\tonce\n\t\t};\n\n\t\tthis.handlers.get(eventType as string)!.push(handler);\n\n\t\t// Return unsubscribe function\n\t\treturn () => {\n\t\t\tconst handlers = this.handlers.get(eventType as string);\n\t\t\tif (handlers) {\n\t\t\t\tconst index = handlers.indexOf(handler);\n\t\t\t\tif (index !== -1) {\n\t\t\t\t\thandlers.splice(index, 1);\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t}\n\n\tpublish<E extends keyof EventTypes>(\n\t\teventType: E,\n\t\tdata?: EventTypes[E]\n\t): void {\n\t\tconst handlers = this.handlers.get(eventType as string);\n\t\tif (!handlers) return;\n\n\t\t// Create a copy of handlers to avoid issues with handlers that modify the array\n\t\tconst handlersToCall = [...handlers];\n\n\t\t// Call all handlers and collect handlers to remove\n\t\tconst handlersToRemove: EventHandler<any>[] = [];\n\n\t\tfor (const handler of handlersToCall) {\n\t\t\thandler.callback(data as EventTypes[E]);\n\t\t\tif (handler.once) {\n\t\t\t\thandlersToRemove.push(handler);\n\t\t\t}\n\t\t}\n\n\t\tif (handlersToRemove.length > 0) {\n\t\t\tfor (const handler of handlersToRemove) {\n\t\t\t\tconst index = handlers.indexOf(handler);\n\t\t\t\tif (index !== -1) {\n\t\t\t\t\thandlers.splice(index, 1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tclear(): void {\n\t\tthis.handlers.clear();\n\t}\n\n\tclearEvent<E extends keyof EventTypes>(eventType: E): void {\n\t\tthis.handlers.delete(eventType as string);\n\t}\n}\n",
|
|
7
|
+
"export default\nclass ResourceManager<ResourceTypes extends Record<string, any> = Record<string, any>> {\n\tprivate resources: Map<string, any> = new Map();\n\n\t/**\n\t * Add a resource to the manager\n\t * @param label The resource key\n\t * @param resource The resource value\n\t * @returns The resource manager instance for chaining\n\t */\n\tadd<K extends keyof ResourceTypes | string>(label: K, resource: K extends keyof ResourceTypes ? ResourceTypes[K] : any) {\n\t\tthis.resources.set(label as string, resource);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get a resource from the manager\n\t * @param label The resource key\n\t * @returns The resource value\n\t * @throws Error if resource not found\n\t */\n\tget<K extends keyof ResourceTypes | string>(label: K): K extends keyof ResourceTypes ? ResourceTypes[K] : any {\n\t\tconst resource = this.resources.get(label as string);\n\n\t\tif (resource === undefined) {\n\t\t\tthrow new Error(`Resource ${String(label)} not found`);\n\t\t}\n\n\t\treturn resource as any;\n\t}\n\n\t/**\n\t * Get a resource from the manager, returning undefined if not found\n\t * @param label The resource key\n\t * @returns The resource value or undefined if not found\n\t */\n\tgetOptional<K extends keyof ResourceTypes | string>(label: K): K extends keyof ResourceTypes ? ResourceTypes[K] : any | undefined {\n\t\tconst resource = this.resources.get(label as string);\n\t\treturn resource as any | undefined;\n\t}\n\n\t/**\n\t * Check if a resource exists\n\t * @param label The resource key\n\t * @returns True if the resource exists\n\t */\n\thas<K extends keyof ResourceTypes | string>(label: K): boolean {\n\t\treturn this.resources.has(label as string);\n\t}\n\n\t/**\n\t * Remove a resource\n\t * @param label The resource key\n\t * @returns True if the resource was removed\n\t */\n\tremove<K extends keyof ResourceTypes | string>(label: K): boolean {\n\t\treturn this.resources.delete(label as string);\n\t}\n\n\t/**\n\t * Get all resource keys\n\t * @returns Array of resource keys\n\t */\n\tgetKeys(): Array<string> {\n\t\treturn Array.from(this.resources.keys());\n\t}\n}\n",
|
|
8
8
|
"import Bundle from \"./bundle\";\nimport ECSpresso from \"./ecspresso\";\nimport type { FilteredEntity, System } from \"./types\";\n\n/**\n * Builder class for creating type-safe ECS Systems with proper query inference\n */\nexport class SystemBuilder<\n\tComponentTypes extends Record<string, any> = Record<string, any>,\n\tEventTypes extends Record<string, any> = Record<string, any>,\n\tResourceTypes extends Record<string, any> = Record<string, any>,\n\tQueries extends Record<string, QueryDefinition<ComponentTypes>> = {},\n> {\n\tprivate queries: Queries = {} as Queries;\n\tprivate processFunction?: ProcessFunction<ComponentTypes, EventTypes, ResourceTypes, Queries>;\n\tprivate attachFunction?: LifecycleFunction<ComponentTypes, EventTypes, ResourceTypes>;\n\tprivate detachFunction?: LifecycleFunction<ComponentTypes, EventTypes, ResourceTypes>;\n\tprivate eventHandlers?: {\n\t\t[EventName in keyof EventTypes]?: {\n\t\t\thandler(\n\t\t\t\tdata: EventTypes[EventName],\n\t\t\t\tecs: ECSpresso<\n\t\t\t\t\tComponentTypes & Record<string, any>,\n\t\t\t\t\tEventTypes,\n\t\t\t\t\tResourceTypes\n\t\t\t\t>,\n\t\t\t): void;\n\t\t};\n\t};\n\n\tconstructor(\n\t\tprivate _label: string,\n\t\tprivate _ecspresso: ECSpresso<ComponentTypes, EventTypes, ResourceTypes> | null = null,\n\t\tprivate _bundle: Bundle<ComponentTypes, EventTypes, ResourceTypes> | null = null,\n\t) {}\n\n\tget label() {\n\t\treturn this._label;\n\t}\n\n\t/**\n\t * Returns the associated bundle if one was provided in the constructor\n\t */\n\tget bundle() {\n\t\treturn this._bundle;\n\t}\n\n\t/**\n\t * Returns the associated ECSpresso instance if one was provided in the constructor\n\t */\n\tget ecspresso() {\n\t\treturn this._ecspresso;\n\t}\n\n\t/**\n\t * Add a query definition to the system\n\t */\n\taddQuery<\n\t\tQueryName extends string,\n\t\tWithComponents extends keyof ComponentTypes,\n\t\tWithoutComponents extends keyof ComponentTypes = never,\n\t\tNewQueries extends Queries & Record<QueryName, QueryDefinition<ComponentTypes, WithComponents, WithoutComponents>> =\n\t\t\tQueries & Record<QueryName, QueryDefinition<ComponentTypes, WithComponents, WithoutComponents>>\n\t>(\n\t\tname: QueryName,\n\t\tdefinition: {\n\t\t\twith: ReadonlyArray<WithComponents>;\n\t\t\twithout?: ReadonlyArray<WithoutComponents>;\n\t\t}\n\t): this extends SystemBuilderWithEcspresso<ComponentTypes, EventTypes, ResourceTypes, Queries>\n\t\t? SystemBuilderWithEcspresso<ComponentTypes, EventTypes, ResourceTypes, NewQueries>\n\t\t: this extends SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes, Queries>\n\t\t\t? SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes, NewQueries>\n\t\t\t: SystemBuilder<ComponentTypes, EventTypes, ResourceTypes, NewQueries> {\n\t\t// Cast is needed because TypeScript can't preserve the type information\n\t\t// when modifying an object property\n\t\tconst newBuilder = this as any;\n\t\tnewBuilder.queries = {\n\t\t\t...this.queries,\n\t\t\t[name]: definition,\n\t\t};\n\t\treturn newBuilder;\n\t}\n\n\t/**\n\t * Set the system's process function that runs each update\n\t * @param process Function to process entities matching the system's queries each update\n\t * @returns This SystemBuilder instance for method chaining\n\t */\n\tsetProcess(\n\t\tprocess: ProcessFunction<ComponentTypes, EventTypes, ResourceTypes, Queries>\n\t): this {\n\t\tthis.processFunction = process;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Set the onAttach lifecycle hook\n\t * Called when the system is attached to the ECS\n\t * @param onAttach Function to run when this system is attached to the ECS\n\t * @returns This SystemBuilder instance for method chaining\n\t */\n\tsetOnAttach(\n\t\tonAttach: LifecycleFunction<ComponentTypes, EventTypes, ResourceTypes>\n\t): this {\n\t\tthis.attachFunction = onAttach;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Set the onDetach lifecycle hook\n\t * Called when the system is removed from the ECS\n\t * @param onDetach Function to run when this system is detached from the ECS\n\t * @returns This SystemBuilder instance for method chaining\n\t */\n\tsetOnDetach(\n\t\tonDetach: LifecycleFunction<ComponentTypes, EventTypes, ResourceTypes>\n\t): this {\n\t\tthis.detachFunction = onDetach;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Set event handlers for the system\n\t * These handlers will be automatically subscribed when the system is attached\n\t * @param handlers Object mapping event names to handler functions\n\t * @returns This SystemBuilder instance for method chaining\n\t */\n\tsetEventHandlers(\n\t\thandlers: {\n\t\t\t[EventName in keyof EventTypes]?: {\n\t\t\t\thandler(\n\t\t\t\t\tdata: EventTypes[EventName],\n\t\t\t\t\tecs: ECSpresso<\n\t\t\t\t\t\tComponentTypes & Record<string, any>,\n\t\t\t\t\t\tEventTypes,\n\t\t\t\t\t\tResourceTypes\n\t\t\t\t\t>\n\t\t\t\t): void;\n\t\t\t};\n\t\t}\n\t): this {\n\t\tthis.eventHandlers = handlers;\n\t\treturn this;\n\t}\n\n\t/**\n\t * Build the final system object\n\t */\n\tbuild(ecspresso?: ECSpresso<ComponentTypes, EventTypes, ResourceTypes>) {\n\t\tconst system: System<ComponentTypes, any, any, EventTypes, ResourceTypes> = {\n\t\t\tlabel: this._label,\n\t\t\tentityQueries: this.queries,\n\t\t};\n\n\t\tif (this.processFunction) {\n\t\t\tsystem.process = this.processFunction;\n\t\t}\n\n\t\tif (this.attachFunction) {\n\t\t\tsystem.onAttach = this.attachFunction;\n\t\t}\n\n\t\tif (this.detachFunction) {\n\t\t\tsystem.onDetach = this.detachFunction;\n\t\t}\n\n\t\tif (this.eventHandlers) {\n\t\t\tsystem.eventHandlers = this.eventHandlers;\n\t\t}\n\n\t\tif (this._ecspresso) {\n\t\t\tregisterSystemWithEcspresso(system, this._ecspresso);\n\t\t}\n\n\t\tif(ecspresso) {\n\t\t\tregisterSystemWithEcspresso(system, ecspresso);\n\t\t}\n\n\t\treturn this;\n\t}\n}\n\n/**\n * Helper function to register a system with an ECSpresso instance\n * This handles attaching the system and setting up event handlers\n * @internal Used by SystemBuilder and Bundle\n */\nexport function registerSystemWithEcspresso<\n\tComponentTypes extends Record<string, any>,\n\tEventTypes extends Record<string, any>,\n\tResourceTypes extends Record<string, any>\n>(\n\tsystem: System<ComponentTypes, any, any, EventTypes, ResourceTypes>,\n\tecspresso: ECSpresso<ComponentTypes, EventTypes, ResourceTypes>\n) {\n\t// Add system to ECSpresso's system list\n\tecspresso[\"_systems\"].push(system);\n\n\t// Call onAttach lifecycle hook if defined\n\tif (system.onAttach) {\n\t\tsystem.onAttach(ecspresso);\n\t}\n\n\t// Auto-subscribe to events if eventHandlers are defined\n\tif (system.eventHandlers) {\n\t\tfor (const eventName in system.eventHandlers) {\n\t\t\tconst handler = system.eventHandlers[eventName];\n\t\t\tif (handler?.handler) {\n\t\t\t\t// Create a wrapper that passes the additional parameters to the handler\n\t\t\t\tconst wrappedHandler = (data: any) => {\n\t\t\t\t\thandler.handler(data, ecspresso);\n\t\t\t\t};\n\n\t\t\t\tecspresso.eventBus.subscribe(eventName, wrappedHandler);\n\t\t\t}\n\t\t}\n\t}\n}\n\n// Helper type definitions\ntype QueryDefinition<\n\tComponentTypes,\n\tWithComponents extends keyof ComponentTypes = any,\n\tWithoutComponents extends keyof ComponentTypes = any,\n> = {\n\twith: ReadonlyArray<WithComponents>;\n\twithout?: ReadonlyArray<WithoutComponents>;\n};\n\ntype QueryResults<\n\tComponentTypes,\n\tQueries extends Record<string, QueryDefinition<ComponentTypes>>,\n> = {\n\t[QueryName in keyof Queries]: QueryName extends string\n\t\t? FilteredEntity<\n\t\t\tComponentTypes,\n\t\t\tQueries[QueryName] extends QueryDefinition<ComponentTypes, infer W, any> ? W : never,\n\t\t\tQueries[QueryName] extends QueryDefinition<ComponentTypes, any, infer WO> ? WO : never\n\t\t>[]\n\t\t: never;\n};\n\n/**\n * Function signature for system process methods\n * @param queries Results of entity queries defined by the system\n * @param deltaTime Time elapsed since last update in seconds\n * @param ecs The ECSpresso instance providing access to all ECS functionality\n */\ntype ProcessFunction<\n\tComponentTypes,\n\tEventTypes extends Record<string, any>,\n\tResourceTypes extends Record<string, any>,\n\tQueries extends Record<string, QueryDefinition<ComponentTypes>>,\n> = (\n\tqueries: QueryResults<ComponentTypes, Queries>,\n\tdeltaTime: number,\n\tecs: ECSpresso<\n\t\tComponentTypes & Record<string, any>,\n\t\tEventTypes,\n\t\tResourceTypes\n\t>\n) => void;\n\n/**\n * Function signature for system lifecycle hooks (onAttach and onDetach)\n * @param ecs The ECSpresso instance providing access to all ECS functionality\n */\ntype LifecycleFunction<\n\tComponentTypes,\n\tEventTypes extends Record<string, any>,\n\tResourceTypes extends Record<string, any>,\n> = (\n\tecs: ECSpresso<\n\t\tComponentTypes & Record<string, any>,\n\t\tEventTypes,\n\t\tResourceTypes\n\t>,\n) => void;\n\n/**\n * Create a SystemBuilder attached to an ECSpresso instance\n * Helper function used by ECSpresso.addSystem\n */\nexport function createEcspressoSystemBuilder<\n\tComponentTypes extends Record<string, any>,\n\tEventTypes extends Record<string, any>,\n\tResourceTypes extends Record<string, any>\n>(\n\tlabel: string,\n\tecspresso: ECSpresso<ComponentTypes, EventTypes, ResourceTypes>\n): SystemBuilderWithEcspresso<ComponentTypes, EventTypes, ResourceTypes> {\n\treturn new SystemBuilder<ComponentTypes, EventTypes, ResourceTypes>(\n\t\tlabel,\n\t\tecspresso\n\t) as SystemBuilderWithEcspresso<ComponentTypes, EventTypes, ResourceTypes>;\n}\n\n/**\n * Create a SystemBuilder attached to a Bundle\n * Helper function used by Bundle.addSystem\n */\nexport function createBundleSystemBuilder<\n\tComponentTypes extends Record<string, any>,\n\tEventTypes extends Record<string, any>,\n\tResourceTypes extends Record<string, any>\n>(\n\tlabel: string,\n\tbundle: Bundle<ComponentTypes, EventTypes, ResourceTypes>\n): SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes> {\n\treturn new SystemBuilder<ComponentTypes, EventTypes, ResourceTypes>(\n\t\tlabel,\n\t\tnull,\n\t\tbundle\n\t) as SystemBuilderWithBundle<ComponentTypes, EventTypes, ResourceTypes>;\n}\n\n// Type interfaces for specialized SystemBuilders\n\n/**\n * SystemBuilder with a guaranteed non-null reference to an ECSpresso instance\n */\nexport interface SystemBuilderWithEcspresso<\n\tComponentTypes extends Record<string, any>,\n\tEventTypes extends Record<string, any>,\n\tResourceTypes extends Record<string, any>,\n\tQueries extends Record<string, QueryDefinition<ComponentTypes>> = {}\n> extends SystemBuilder<ComponentTypes, EventTypes, ResourceTypes, Queries> {\n\treadonly ecspresso: ECSpresso<ComponentTypes, EventTypes, ResourceTypes>;\n}\n\n/**\n * SystemBuilder with a guaranteed non-null reference to a Bundle\n */\nexport interface SystemBuilderWithBundle<\n\tComponentTypes extends Record<string, any>,\n\tEventTypes extends Record<string, any>,\n\tResourceTypes extends Record<string, any>,\n\tQueries extends Record<string, QueryDefinition<ComponentTypes>> = {}\n> extends SystemBuilder<ComponentTypes, EventTypes, ResourceTypes, Queries> {\n\treadonly bundle: Bundle<ComponentTypes, EventTypes, ResourceTypes>;\n}\n",
|
|
9
|
-
"import EntityManager from \"./entity-manager\";\nimport EventBus from \"./event-bus\";\nimport ResourceManager from \"./resource-manager\";\nimport type { System, MergeAll } from \"./types\";\nimport type Bundle from \"./bundle\";\nimport { createEcspressoSystemBuilder } from \"./system-builder\";\nimport { version } from \"../package.json\";\n\n\n/**\n * The main ECS (Entity Component System) container class\n *\n * This class manages entities, components, systems, resources, and events.\n *\n * Systems interact with the ECS through a single parameter that provides access\n * to the entire ECSpresso instance. This provides a simplified API for systems\n * and allows them access to all ECS functionality through a single reference.\n *\n * @template ComponentTypes Record of component types used in this ECS instance\n * @template EventTypes Record of event types used in this ECS instance\n * @template ResourceTypes Record of resource types used in this ECS instance\n */\nexport default\nclass ECSpresso<\n\tComponentTypes extends Record<string, any> = Record<string, any>,\n\tEventTypes extends Record<string, any> = Record<string, any>,\n\tResourceTypes extends Record<string, any> = Record<string, any>,\n> {\n\tpublic static readonly VERSION = version;\n\tprivate _entityManager: EntityManager<ComponentTypes>;\n\tprivate _systems: System<ComponentTypes, any, any, EventTypes, ResourceTypes>[] = [];\n\tprivate _eventBus: EventBus<EventTypes>;\n\tprivate _resourceManager: ResourceManager<ResourceTypes>;\n\tprivate _installedBundles: Set<string> = new Set();\n\n\tconstructor() {\n\t\tthis._entityManager = new EntityManager<ComponentTypes>();\n\t\tthis._eventBus = new EventBus<EventTypes>();\n\t\tthis._resourceManager = new ResourceManager<ResourceTypes>();\n\t}\n\n\t/**\n\t * Install one or more bundles into this ECS instance\n\t * Systems in the bundle will have their onAttach method called with this ECSpresso instance\n\t * @param bundles One or more bundles to install\n\t * @returns A new ECSpresso instance with merged types from all bundles\n\t */\n\tinstall<\n\t\tBundles extends Array<Bundle<any, any, any> | null>\n\t>(...bundles: Bundles): ECSpresso<\n\t\tComponentTypes & MergeAll<{ [K in keyof Bundles]: Bundles[K] extends Bundle<infer C, any, any> ? C : {} }>,\n\t\tEventTypes & MergeAll<{ [K in keyof Bundles]: Bundles[K] extends Bundle<any, infer E, any> ? E : {} }>,\n\t\tResourceTypes & MergeAll<{ [K in keyof Bundles]: Bundles[K] extends Bundle<any, any, infer R> ? R : {} }>\n\t> {\n\t\tfor (const bundle of bundles) {\n\t\t\tif (!bundle) continue;\n\n\t\t\t// Check if this bundle is already installed\n\t\t\tif (this._installedBundles.has(bundle.id)) {\n\t\t\t\tconsole.warn(`Bundle ${bundle.id} is already installed`);\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Register all systems from the bundle\n\t\t\tbundle.registerSystemsWithEcspresso(this);\n\n\t\t\t// Register all resources from the bundle\n\t\t\tconst resources = bundle.getResources();\n\t\t\tfor (const [key, value] of resources.entries()) {\n\t\t\t\t// We need to cast here because TypeScript can't verify the type compatibility\n\t\t\t\t// between bundles, but we trust that the bundle's resource types are compatible\n\t\t\t\tthis._resourceManager.add(key as unknown as keyof ResourceTypes, value);\n\t\t\t}\n\n\t\t\t// Mark this bundle as installed\n\t\t\tthis._installedBundles.add(bundle.id);\n\t\t}\n\n\t\treturn this as any;\n\t}\n\n\t/**\n\t * Remove a system by its label\n\t * Calls the system's onDetach method with this ECSpresso instance if defined\n\t * @param label The unique label of the system to remove\n\t * @returns true if the system was found and removed, false otherwise\n\t */\n\tremoveSystem(label: string): boolean {\n\t\tconst index = this._systems.findIndex(system => system.label === label);\n\t\tif (index === -1) return false;\n\n\t\tconst system = this._systems[index];\n\t\tif (!system) return false;\n\n\t\tsystem.onDetach?.(\n\t\t\tthis\n\t\t);\n\n\t\t// Remove system\n\t\tthis._systems.splice(index, 1);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Check if a resource exists\n\t */\n\thasResource<K extends keyof ResourceTypes>(key: K): boolean {\n\t\treturn this._resourceManager.has(key);\n\t}\n\n\t/**\n\t * Get a resource if it exists, or undefined if not\n\t */\n\tgetResource<K extends keyof ResourceTypes>(key: K): ResourceTypes[K] {\n\t\treturn this._resourceManager.getOptional(key) as ResourceTypes[K];\n\t}\n\n\t/**\n\t * Get a resource, throws error if not found\n\t */\n\tgetResourceOrThrow<K extends keyof ResourceTypes>(key: K): ResourceTypes[K] {\n\t\treturn this._resourceManager.get(key) as ResourceTypes[K];\n\t}\n\n\t/**\n\t * Add a resource to the ECS instance\n\t */\n\taddResource<K extends keyof ResourceTypes>(key: K, resource: ResourceTypes[K]): this {\n\t\tthis._resourceManager.add(key, resource);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Check if an entity has a component\n\t */\n\thasComponent<K extends keyof ComponentTypes>(\n\t\tentityId: number,\n\t\tcomponentName: K\n\t): boolean {\n\t\tconst component = this._entityManager.getComponent(entityId, componentName);\n\t\treturn component !== null;\n\t}\n\n\t/**\n\t * Get all entities with specific components\n\t */\n\tgetEntitiesWithComponents(\n\t\twithComponents: (keyof ComponentTypes)[],\n\t\twithoutComponents: (keyof ComponentTypes)[] = []\n\t) {\n\t\treturn this._entityManager.getEntitiesWithComponents(\n\t\t\twithComponents,\n\t\t\twithoutComponents\n\t\t);\n\t}\n\n\t/**\n\t * Update all systems\n\t * Calls each system's process method with this ECSpresso instance\n\t * @param deltaTime Time elapsed since the last update in seconds\n\t */\n\tupdate(deltaTime: number) {\n\t\tfor (const system of this._systems) {\n\t\t\tif (!system.process) continue;\n\n\t\t\t// Prepare query results\n\t\t\tconst queryResults: Record<string, any[]> = {};\n\n\t\t\t// Process entity queries if defined\n\t\t\tif (system.entityQueries) {\n\t\t\t\tfor (const queryName in system.entityQueries) {\n\t\t\t\t\tconst query = system.entityQueries[queryName];\n\t\t\t\t\tif (query) {\n\t\t\t\t\t\tqueryResults[queryName] = this._entityManager.getEntitiesWithComponents(\n\t\t\t\t\t\t\tquery.with,\n\t\t\t\t\t\t\tquery.without || []\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Call the system's process method\n\t\t\t\tsystem.process(\n\t\t\t\t\tqueryResults,\n\t\t\t\t\tdeltaTime,\n\t\t\t\t\tthis\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\t// No queries defined, pass an empty array\n\t\t\t\tsystem.process(\n\t\t\t\t\t[],\n\t\t\t\t\tdeltaTime,\n\t\t\t\t\tthis\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Getters for the internal managers\n\tget entityManager() {\n\t\treturn this._entityManager;\n\t}\n\n\tget eventBus() {\n\t\treturn this._eventBus;\n\t}\n\n\tget resourceManager() {\n\t\treturn this._resourceManager;\n\t}\n\n\t/**\n\t * Get all installed bundle IDs\n\t */\n\tget installedBundles(): string[] {\n\t\treturn Array.from(this._installedBundles);\n\t}\n\n\t/**\n\t * Add a system directly to this ECSpresso instance\n\t * @param label Unique identifier for the system\n\t * @returns A SystemBuilder instance for method chaining\n\t */\n\taddSystem(label: string) {\n\t\tconst system = createEcspressoSystemBuilder<ComponentTypes, EventTypes, ResourceTypes>(label, this);\n\n\t\treturn system;\n\t}\n}\n",
|
|
10
|
-
"import { createBundleSystemBuilder, SystemBuilder } from './system-builder';\nimport type ECSpresso from './ecspresso';\
|
|
9
|
+
"import EntityManager from \"./entity-manager\";\nimport EventBus from \"./event-bus\";\nimport ResourceManager from \"./resource-manager\";\nimport type { System } from \"./types\";\nimport type Bundle from \"./bundle\";\nimport { createEcspressoSystemBuilder } from \"./system-builder\";\nimport { version } from \"../package.json\";\n\n/**\n * Type helper to detect conflicting types between two record types.\n * Returns a union of keys that exist in both T and U but have incompatible types.\n */\ntype GetConflictingKeys<T, U> = {\n [K in keyof T & keyof U]: T[K] extends U[K]\n ? U[K] extends T[K]\n ? never\n : K\n : K\n}[keyof T & keyof U];\n\n/**\n * Simplified type helper to check bundle type compatibility.\n * Returns true if bundles can be merged without type conflicts.\n */\ntype BundlesAreCompatible<\n C1 extends Record<string, any>,\n C2 extends Record<string, any>,\n E1 extends Record<string, any>,\n E2 extends Record<string, any>,\n R1 extends Record<string, any>,\n R2 extends Record<string, any>\n> =\n // If all base types are empty, any bundle is compatible\n [keyof C1] extends [never]\n ? [keyof E1] extends [never]\n ? [keyof R1] extends [never]\n ? true\n : GetConflictingKeys<R1, R2> extends never ? true : false\n : GetConflictingKeys<E1, E2> extends never\n ? GetConflictingKeys<R1, R2> extends never ? true : false\n : false\n : GetConflictingKeys<C1, C2> extends never\n ? GetConflictingKeys<E1, E2> extends never\n ? GetConflictingKeys<R1, R2> extends never\n ? true\n : false\n : false\n : false;\n\n/**\n * Interface declaration for ECSpresso constructor to ensure type augmentation works properly.\n * This merges with the class declaration below.\n */\nexport default interface ECSpresso<\n\tComponentTypes extends Record<string, any> = Record<string, any>,\n\tEventTypes extends Record<string, any> = Record<string, any>,\n\tResourceTypes extends Record<string, any> = Record<string, any>,\n> {\n\t/**\n\t * Default constructor\n\t */\n\tnew(): ECSpresso<ComponentTypes, EventTypes, ResourceTypes>;\n}\n\n/**\n * Static methods on the ECSpresso class\n */\nexport default interface ECSpresso {\n /**\n * Create a new ECSpresso builder with type-safe bundle installation.\n * This is the preferred way to create an ECSpresso instance with bundles.\n *\n * Example:\n * ```typescript\n * const ecs = ECSpresso.create<MyComponents, MyEvents, MyResources>()\n * .withBundle(bundle1)\n * .withBundle(bundle2)\n * .build();\n * ```\n */\n create(): ECSpressoBuilder<{}, {}, {}>; // No type parameters - returns a builder with empty types\n\n /**\n * Create a new ECSpresso builder with type-safe bundle installation and explicit starting types.\n */\n create<\n BaseC extends Record<string, any>,\n BaseE extends Record<string, any>,\n BaseR extends Record<string, any>\n >(): ECSpressoBuilder<BaseC, BaseE, BaseR>;\n}\n\n/**\n * ECSpresso is the central ECS framework class that connects all features.\n * It handles creation and management of entities, components, and systems, and provides lifecycle hooks.\n */\nexport default class ECSpresso<\n\tComponentTypes extends Record<string, any> = Record<string, any>,\n\tEventTypes extends Record<string, any> = Record<string, any>,\n\tResourceTypes extends Record<string, any> = Record<string, any>,\n> {\n\t/** Library version */\n\tpublic static readonly VERSION = version;\n\n\t/** Access/modify stored components and entities */\n\tprivate _entityManager: EntityManager<ComponentTypes>;\n\t/** Publish/subscribe to events */\n\tprivate _eventBus: EventBus<EventTypes>;\n\t/** Access/modify registered resources */\n\tprivate _resourceManager: ResourceManager<ResourceTypes>;\n\n\t/** Registered systems that will be updated in order */\n\tprivate _systems: Array<System<ComponentTypes, any, any, EventTypes, ResourceTypes>> = [];\n\t/** Track installed bundles to prevent duplicates */\n\tprivate _installedBundles: Set<string> = new Set();\n\n\t/**\n\t * Creates a new ECSpresso instance.\n\t */\n\tconstructor() {\n\t\tthis._entityManager = new EntityManager<ComponentTypes>();\n\t\tthis._eventBus = new EventBus<EventTypes>();\n\t\tthis._resourceManager = new ResourceManager<ResourceTypes>();\n\t}\n\n\t/**\n\t * Creates a new ECSpresso builder for type-safe bundle installation.\n\t * This is the preferred way to create an ECSpresso instance with bundles.\n\t *\n\t * @returns A builder instance for fluent method chaining\n\t *\n\t * @example\n\t * ```typescript\n\t * const ecs = ECSpresso.create<BaseComponents, BaseEvents, BaseResources>()\n\t * .withBundle(bundle1)\n\t * .withBundle(bundle2)\n\t * .build();\n\t * ```\n\t */\n\tstatic create<\n\t\tC extends Record<string, any> = {},\n\t\tE extends Record<string, any> = {},\n\t\tR extends Record<string, any> = {}\n\t>(): ECSpressoBuilder<C, E, R> {\n\t\treturn new ECSpressoBuilder<C, E, R>();\n\t}\n\n\t/**\n\t * Adds a system directly to this ECSpresso instance\n\t * @param label Unique name to identify the system\n\t * @returns A SystemBuilder instance for method chaining\n\t */\n\taddSystem(label: string) {\n\t\treturn createEcspressoSystemBuilder<\n\t\t\tComponentTypes,\n\t\t\tEventTypes,\n\t\t\tResourceTypes\n\t\t>(label, this);\n\t}\n\n\t/**\n\t * Update all systems, passing deltaTime and query results to each system's process function\n\t * @param deltaTime Time elapsed since the last update (in seconds)\n\t */\n\tupdate(deltaTime: number) {\n\t\tfor (const system of this._systems) {\n\t\t\tif (!system.process) continue;\n\n\t\t\t// Prepare query results for each defined query in the system\n\t\t\tconst queryResults: Record<string, any> = {};\n\n\t\t\tif (system.entityQueries) {\n\t\t\t\tfor (const queryName in system.entityQueries) {\n\t\t\t\t\tconst query = system.entityQueries[queryName];\n\t\t\t\t\tif (query) {\n\t\t\t\t\t\tqueryResults[queryName] = this._entityManager.getEntitiesWithComponents(\n\t\t\t\t\t\t\tquery.with,\n\t\t\t\t\t\t\tquery.without || []\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Call the system's process function\n\t\t\tsystem.process(queryResults, deltaTime, this);\n\t\t}\n\t}\n\n\t/**\n\t * Internal method to install a bundle into this ECSpresso instance.\n\t * Called by the ECSpressoBuilder during the build process.\n\t * The type safety is guaranteed by the builder's type system.\n\t */\n\t_installBundle<\n\t\tC extends Record<string, any>,\n\t\tE extends Record<string, any>,\n\t\tR extends Record<string, any>\n\t>(bundle: Bundle<C, E, R>): this {\n\t\t// Prevent duplicate installation of the same bundle\n\t\tif (this._installedBundles.has(bundle.id)) {\n\t\t\treturn this;\n\t\t}\n\n\t\t// Mark this bundle as installed\n\t\tthis._installedBundles.add(bundle.id);\n\n\t\t// Register systems from the bundle\n\t\t// Type casting is necessary because the generic parameters don't match\n\t\tbundle.registerSystemsWithEcspresso(this as any);\n\n\t\t// Register resources from the bundle\n\t\tconst resources = bundle.getResources();\n\t\tfor (const [key, value] of resources.entries()) {\n\t\t\t// Type compatibility is guaranteed by the builder's type system\n\t\t\tthis._resourceManager.add(key as unknown as keyof ResourceTypes, value as any);\n\t\t}\n\n\t\treturn this;\n\t}\n\n\t/**\n\t * Remove a system by its label\n\t * Calls the system's onDetach method with this ECSpresso instance if defined\n\t * @param label The unique label of the system to remove\n\t * @returns true if the system was found and removed, false otherwise\n\t */\n\tremoveSystem(label: string): boolean {\n\t\tconst index = this._systems.findIndex(system => system.label === label);\n\t\tif (index === -1) return false;\n\n\t\tconst system = this._systems[index];\n\t\t// This should never happen since we just found the system by index\n\t\tif (!system) return false;\n\n\t\t// Call the onDetach lifecycle hook if defined\n\t\tif (system.onDetach) {\n\t\t\tsystem.onDetach(this);\n\t\t}\n\n\t\t// Remove system\n\t\tthis._systems.splice(index, 1);\n\t\treturn true;\n\t}\n\n\t/**\n\t * Check if a resource exists\n\t */\n\thasResource<K extends keyof ResourceTypes>(key: K): boolean {\n\t\treturn this._resourceManager.has(key);\n\t}\n\n\t/**\n\t * Get a resource if it exists, or undefined if not\n\t */\n\tgetResource<K extends keyof ResourceTypes>(key: K): ResourceTypes[K] {\n\t\treturn this._resourceManager.getOptional(key) as ResourceTypes[K];\n\t}\n\n\t/**\n\t * Get a resource, throws error if not found\n\t */\n\tgetResourceOrThrow<K extends keyof ResourceTypes>(key: K): ResourceTypes[K] {\n\t\treturn this._resourceManager.get(key) as ResourceTypes[K];\n\t}\n\n\t/**\n\t * Add a resource to the ECS instance\n\t */\n\taddResource<K extends keyof ResourceTypes>(key: K, resource: ResourceTypes[K]): this {\n\t\tthis._resourceManager.add(key, resource);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Check if an entity has a component\n\t */\n\thasComponent<K extends keyof ComponentTypes>(\n\t\tentityId: number,\n\t\tcomponentName: K\n\t): boolean {\n\t\tconst component = this._entityManager.getComponent(entityId, componentName);\n\t\treturn component !== null;\n\t}\n\n\t/**\n\t * Get all entities with specific components\n\t */\n\tgetEntitiesWithComponents(\n\t\twithComponents: (keyof ComponentTypes)[],\n\t\twithoutComponents: (keyof ComponentTypes)[] = []\n\t) {\n\t\treturn this._entityManager.getEntitiesWithComponents(\n\t\t\twithComponents,\n\t\t\twithoutComponents\n\t\t);\n\t}\n\n\t/**\n\t * Get all installed bundle IDs\n\t */\n\tget installedBundles(): string[] {\n\t\treturn Array.from(this._installedBundles);\n\t}\n\n\t// Getters for the internal managers\n\tget entityManager() {\n\t\treturn this._entityManager;\n\t}\n\n\tget eventBus() {\n\t\treturn this._eventBus;\n\t}\n\n\tget resourceManager() {\n\t\treturn this._resourceManager;\n\t}\n}\n\n/**\n * Builder class for ECSpresso that provides fluent type-safe bundle installation.\n * Handles type checking during build process to ensure type safety.\n */\nexport class ECSpressoBuilder<\n C extends Record<string, any> = {},\n E extends Record<string, any> = {},\n R extends Record<string, any> = {}\n> {\n /** The ECSpresso instance being built */\n private ecspresso: ECSpresso<C, E, R>;\n\n constructor() {\n this.ecspresso = new ECSpresso<C, E, R>();\n }\n\n /**\n * Add the first bundle when starting with empty types.\n * This overload allows any bundle to be added to an empty ECSpresso instance.\n */\n withBundle<\n BC extends Record<string, any>,\n BE extends Record<string, any>,\n BR extends Record<string, any>\n >(\n this: ECSpressoBuilder<{}, {}, {}>,\n bundle: Bundle<BC, BE, BR>\n ): ECSpressoBuilder<BC, BE, BR>;\n\n /**\n * Add a subsequent bundle with type checking.\n * This overload enforces bundle type compatibility.\n */\n withBundle<\n BC extends Record<string, any>,\n BE extends Record<string, any>,\n BR extends Record<string, any>\n >(\n bundle: BundlesAreCompatible<C, BC, E, BE, R, BR> extends true\n ? Bundle<BC, BE, BR>\n : never\n ): ECSpressoBuilder<C & BC, E & BE, R & BR>;\n\n /**\n * Implementation of both overloads.\n * Since the type compatibility is checked in the method signature,\n * we can safely assume the bundle is compatible here.\n */\n withBundle<\n BC extends Record<string, any>,\n BE extends Record<string, any>,\n BR extends Record<string, any>\n >(\n bundle: Bundle<BC, BE, BR>\n ): ECSpressoBuilder<C & BC, E & BE, R & BR> {\n // Install the bundle\n // Type compatibility is guaranteed by method overloads\n this.ecspresso._installBundle(bundle as any);\n\n // Return a builder with the updated type parameters\n return this as unknown as ECSpressoBuilder<C & BC, E & BE, R & BR>;\n }\n\n /**\n * Complete the build process and return the built ECSpresso instance\n */\n build(): ECSpresso<C, E, R> {\n return this.ecspresso;\n }\n}\n",
|
|
10
|
+
"import { createBundleSystemBuilder, SystemBuilder } from './system-builder';\nimport type ECSpresso from './ecspresso';\n\n/**\n * Generates a unique ID for a bundle\n */\nfunction generateBundleId(): string {\n\treturn `bundle_${Date.now().toString(36)}_${Math.random().toString(36).substring(2, 9)}`;\n}\n\n/**\n * Bundle class that encapsulates a set of components, resources, events, and systems\n * that can be merged into a ECSpresso instance\n */\nexport default class Bundle<\n\tComponentTypes extends Record<string, any> = Record<string, any>,\n\tEventTypes extends Record<string, any> = Record<string, any>,\n\tResourceTypes extends Record<string, any> = Record<string, any>,\n> {\n\tprivate _systems: SystemBuilder<ComponentTypes, EventTypes, ResourceTypes, any>[] = [];\n\tprivate _resources: Map<keyof ResourceTypes, ResourceTypes[keyof ResourceTypes]> = new Map();\n\tprivate _id: string;\n\n\tconstructor(id?: string) {\n\t\tthis._id = id || generateBundleId();\n\t}\n\n\t/**\n\t * Get the unique ID of this bundle\n\t */\n\tget id(): string {\n\t\treturn this._id;\n\t}\n\n\t/**\n\t * Set the ID of this bundle\n\t * @internal Used by combineBundles\n\t */\n\tset id(value: string) {\n\t\tthis._id = value;\n\t}\n\n\t/**\n\t * Add a system to this bundle\n\t */\n\taddSystem(label: string) {\n\t\tconst system = createBundleSystemBuilder<ComponentTypes, EventTypes, ResourceTypes>(label, this);\n\n\t\tthis._systems.push(system);\n\n\t\treturn system;\n\t}\n\n\t/**\n\t * Add a resource to this bundle\n\t * @param label The resource key\n\t * @param resource The resource value\n\t */\n\taddResource<K extends keyof ResourceTypes>(label: K, resource: ResourceTypes[K]) {\n\t\tthis._resources.set(label, resource);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Get all systems defined in this bundle\n\t * Returns built System objects instead of SystemBuilders\n\t */\n\tgetSystems() {\n\t\treturn this._systems.map(system => system.build());\n\t}\n\n\t/**\n\t * Register all systems in this bundle with an ECSpresso instance\n\t * @internal Used by ECSpresso when adding a bundle\n\t */\n\tregisterSystemsWithEcspresso(ecspresso: ECSpresso<ComponentTypes, EventTypes, ResourceTypes>) {\n\t\tfor (const systemBuilder of this._systems) {\n\t\t\tsystemBuilder.build(ecspresso);\n\t\t}\n\t}\n\n\t/**\n\t * Get all resources defined in this bundle\n\t */\n\tgetResources(): Map<keyof ResourceTypes, ResourceTypes[keyof ResourceTypes]> {\n\t\treturn new Map(this._resources);\n\t}\n\n\t/**\n\t * Get a specific resource by key\n\t * @param key The resource key\n\t * @returns The resource value or undefined if not found\n\t */\n\tgetResource<K extends keyof ResourceTypes>(key: K): ResourceTypes[K] | undefined {\n\t\treturn this._resources.get(key) as ResourceTypes[K] | undefined;\n\t}\n\n\t/**\n\t * Get all system builders in this bundle\n\t */\n\tgetSystemBuilders(): SystemBuilder<ComponentTypes, EventTypes, ResourceTypes, any>[] {\n\t\treturn [...this._systems];\n\t}\n\n\t/**\n\t * Check if this bundle has a specific resource\n\t * @param key The resource key to check\n\t * @returns True if the resource exists\n\t */\n\thasResource<K extends keyof ResourceTypes>(key: K): boolean {\n\t\treturn this._resources.has(key);\n\t}\n}\n\n// Check if object has exactly the same type\ntype Exactly<T, U> =\n T extends U\n ? U extends T\n ? true\n : false\n : false;\n\n// Create a type error for incompatible types\ntype IncompatibleBundles<\n C1 extends Record<string, any>,\n C2 extends Record<string, any>,\n E1 extends Record<string, any>,\n E2 extends Record<string, any>,\n R1 extends Record<string, any>,\n R2 extends Record<string, any>\n> = {\n [K in keyof C1 & keyof C2]: Exactly<C1[K], C2[K]> extends false ? never : unknown;\n} & {\n [K in keyof E1 & keyof E2]: Exactly<E1[K], E2[K]> extends false ? never : unknown;\n} & {\n [K in keyof R1 & keyof R2]: Exactly<R1[K], R2[K]> extends false ? never : unknown;\n};\n\n/**\n * Function that merges multiple bundles into a single bundle\n */\nexport function mergeBundles<\n C1 extends Record<string, any>,\n E1 extends Record<string, any>,\n R1 extends Record<string, any>,\n C2 extends Record<string, any>,\n E2 extends Record<string, any>,\n R2 extends Record<string, any>\n>(\n id: string,\n bundle1: Bundle<C1, E1, R1>,\n bundle2: Bundle<C2, E2, R2> & IncompatibleBundles<C1, C2, E1, E2, R1, R2>\n): Bundle<C1 & C2, E1 & E2, R1 & R2>;\n\nexport function mergeBundles<\n ComponentTypes extends Record<string, any>,\n EventTypes extends Record<string, any>,\n ResourceTypes extends Record<string, any>\n>(\n id: string,\n ...bundles: Array<Bundle<ComponentTypes, EventTypes, ResourceTypes>>\n): Bundle<ComponentTypes, EventTypes, ResourceTypes>;\n\nexport function mergeBundles(\n id: string,\n ...bundles: Array<Bundle<any, any, any>>\n): Bundle<any, any, any> {\n if (bundles.length === 0) {\n return new Bundle(id);\n }\n\n const combined = new Bundle(id);\n\n for (const bundle of bundles) {\n for (const system of bundle.getSystemBuilders()) {\n combined.addSystem(system as any);\n }\n\n // Add resources from this bundle\n for (const [label, resource] of bundle.getResources().entries()) {\n combined.addResource(label as any, resource);\n }\n }\n\n return combined as any;\n}\n",
|
|
11
11
|
"import ECSpresso from './ecspresso';\nimport { SystemBuilder } from './system-builder';\nimport Bundle, { mergeBundles } from './bundle';\n\nexport * from './types';\nexport { default as EntityManager } from './entity-manager';\nexport { default as EventBus } from './event-bus';\nexport { default as ResourceManager } from './resource-manager';\nexport { SystemBuilder };\nexport { Bundle, mergeBundles };\nexport default ECSpresso;\n"
|
|
12
12
|
],
|
|
13
|
-
"mappings": "AAEA,MACM,CAA8B,CAC3B,OAAiB,EACjB,SAAgD,IAAI,IACpD,iBAA2D,IAAI,IAEvE,YAAY,EAA2B,CACtC,IAAM,EAAK,KAAK,SACV,EAAiC,CAAE,KAAI,WAAY,CAAC,CAAE,EAE5D,OADA,KAAK,SAAS,IAAI,EAAI,CAAM,EACrB,EAIR,YAAwD,CACvD,EACA,EACA,EACC,CACD,IAAM,EAAS,OAAO,IAAe,SACpC,KAAK,SAAS,IAAI,CAAU,EAC5B,EAED,IAAK,EAAQ,MAAM,IAAI,MAAM,UAAU,kBAA2B,EAKlE,GAHA,EAAO,WAAW,GAAiB,GAG9B,KAAK,iBAAiB,IAAI,CAAa,EAC3C,KAAK,iBAAiB,IAAI,EAAe,IAAI,GAAK,EAGnD,OADA,KAAK,iBAAiB,IAAI,CAAa,GAAG,IAAI,EAAO,EAAE,EAChD,KAQR,
|
|
14
|
-
"debugId": "
|
|
13
|
+
"mappings": "AAEA,MACM,CAA8B,CAC3B,OAAiB,EACjB,SAAgD,IAAI,IACpD,iBAA2D,IAAI,IAEvE,YAAY,EAA2B,CACtC,IAAM,EAAK,KAAK,SACV,EAAiC,CAAE,KAAI,WAAY,CAAC,CAAE,EAE5D,OADA,KAAK,SAAS,IAAI,EAAI,CAAM,EACrB,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EAElB,EAQR,WAA0C,CAAC,EAAU,EAA4B,CAEhF,OADA,KAAK,WAAW,IAAI,EAAO,CAAQ,EAC5B,KAOR,UAAU,EAAG,CACZ,OAAO,KAAK,SAAS,IAAI,KAAU,EAAO,MAAM,CAAC,EAOlD,4BAA4B,CAAC,EAAiE,CAC7F,QAAW,KAAiB,KAAK,SAChC,EAAc,MAAM,CAAS,EAO/B,YAAY,EAAiE,CAC5E,OAAO,IAAI,IAAI,KAAK,UAAU,EAQ/B,WAA0C,CAAC,EAAsC,CAChF,OAAO,KAAK,WAAW,IAAI,CAAG,EAM/B,iBAAiB,EAAoE,CACpF,MAAO,CAAC,GAAG,KAAK,QAAQ,EAQzB,WAA0C,CAAC,EAAiB,CAC3D,OAAO,KAAK,WAAW,IAAI,CAAG,EAEhC,CAmDO,SAAS,CAAY,CAC1B,KACG,EACoB,CACvB,GAAI,EAAQ,SAAW,EACrB,OAAO,IAAI,EAAO,CAAE,EAGtB,IAAM,EAAW,IAAI,EAAO,CAAE,EAE9B,QAAW,KAAU,EAAS,CAC5B,QAAW,KAAU,EAAO,kBAAkB,EAC5C,EAAS,UAAU,CAAa,EAIlC,QAAY,EAAO,KAAa,EAAO,aAAa,EAAE,QAAQ,EAC5D,EAAS,YAAY,EAAc,CAAQ,EAI/C,OAAO,EC9KT,IAAe",
|
|
14
|
+
"debugId": "BDBE8C30396183E764756E2164756E21",
|
|
15
15
|
"names": []
|
|
16
16
|
}
|
|
@@ -6,35 +6,35 @@ export default class ResourceManager<ResourceTypes extends Record<string, any> =
|
|
|
6
6
|
* @param resource The resource value
|
|
7
7
|
* @returns The resource manager instance for chaining
|
|
8
8
|
*/
|
|
9
|
-
add<K extends keyof ResourceTypes>(label: K, resource: ResourceTypes[K]): this;
|
|
9
|
+
add<K extends keyof ResourceTypes | string>(label: K, resource: K extends keyof ResourceTypes ? ResourceTypes[K] : any): this;
|
|
10
10
|
/**
|
|
11
11
|
* Get a resource from the manager
|
|
12
12
|
* @param label The resource key
|
|
13
13
|
* @returns The resource value
|
|
14
14
|
* @throws Error if resource not found
|
|
15
15
|
*/
|
|
16
|
-
get<K extends keyof ResourceTypes>(label: K): ResourceTypes[K];
|
|
16
|
+
get<K extends keyof ResourceTypes | string>(label: K): K extends keyof ResourceTypes ? ResourceTypes[K] : any;
|
|
17
17
|
/**
|
|
18
18
|
* Get a resource from the manager, returning undefined if not found
|
|
19
19
|
* @param label The resource key
|
|
20
20
|
* @returns The resource value or undefined if not found
|
|
21
21
|
*/
|
|
22
|
-
getOptional<K extends keyof ResourceTypes>(label: K): ResourceTypes[K] | undefined;
|
|
22
|
+
getOptional<K extends keyof ResourceTypes | string>(label: K): K extends keyof ResourceTypes ? ResourceTypes[K] : any | undefined;
|
|
23
23
|
/**
|
|
24
24
|
* Check if a resource exists
|
|
25
25
|
* @param label The resource key
|
|
26
26
|
* @returns True if the resource exists
|
|
27
27
|
*/
|
|
28
|
-
has<K extends keyof ResourceTypes>(label: K): boolean;
|
|
28
|
+
has<K extends keyof ResourceTypes | string>(label: K): boolean;
|
|
29
29
|
/**
|
|
30
30
|
* Remove a resource
|
|
31
31
|
* @param label The resource key
|
|
32
32
|
* @returns True if the resource was removed
|
|
33
33
|
*/
|
|
34
|
-
remove<K extends keyof ResourceTypes>(label: K): boolean;
|
|
34
|
+
remove<K extends keyof ResourceTypes | string>(label: K): boolean;
|
|
35
35
|
/**
|
|
36
36
|
* Get all resource keys
|
|
37
37
|
* @returns Array of resource keys
|
|
38
38
|
*/
|
|
39
|
-
getKeys(): Array<
|
|
39
|
+
getKeys(): Array<string>;
|
|
40
40
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ecspresso",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"build:ts": "bun tsc -p tsconfig.build.json",
|
|
35
35
|
"build:js": "bun build --target=browser --sourcemap=linked --minify --outdir=dist src/index.ts",
|
|
36
36
|
"build": "bun build:clean && bun build:ts && bun build:js",
|
|
37
|
+
"examples": "bun ./examples/serve-examples.ts",
|
|
37
38
|
"prepublishOnly": "bun tsc && bun test && bun run build"
|
|
38
39
|
},
|
|
39
40
|
"type": "module"
|