feat: add singleton support to World ECS

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-03-07 19:14:13 +00:00
parent e5a6b443e9
commit 6b32e115b0
2 changed files with 24 additions and 0 deletions
+9
View File
@@ -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;
}
}
+15
View File
@@ -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();
});
});
});