feat: add singleton support to World ECS
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ export class World {
|
||||
private nextId: EntityId = 1;
|
||||
private components: Map<ComponentName, Map<EntityId, unknown>> = new Map();
|
||||
private entities: Set<EntityId> = new Set();
|
||||
private singletons: Map<string, unknown> = new Map();
|
||||
|
||||
createEntity(): EntityId {
|
||||
const id = this.nextId++;
|
||||
@@ -48,4 +49,12 @@ export class World {
|
||||
getAllEntities(): EntityId[] {
|
||||
return [...this.entities];
|
||||
}
|
||||
|
||||
setSingleton<T>(name: string, data: T): void {
|
||||
this.singletons.set(name, data);
|
||||
}
|
||||
|
||||
getSingleton<T>(name: string): T | undefined {
|
||||
return this.singletons.get(name) as T | undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,4 +57,19 @@ describe('World', () => {
|
||||
expect(world.getComponent(entity, 'position')).toBeUndefined();
|
||||
expect(world.getComponent(entity, 'needs')).toEqual({ hunger: 100, energy: 100 });
|
||||
});
|
||||
|
||||
describe('singletons', () => {
|
||||
it('should store and retrieve singletons', () => {
|
||||
const world = new World();
|
||||
world.setSingleton('testRegistry', new Map([['a', 1]]));
|
||||
const result = world.getSingleton<Map<string, number>>('testRegistry');
|
||||
expect(result).toBeDefined();
|
||||
expect(result!.get('a')).toBe(1);
|
||||
});
|
||||
|
||||
it('should return undefined for missing singletons', () => {
|
||||
const world = new World();
|
||||
expect(world.getSingleton('nonexistent')).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user