diff --git a/server/src/ecs/World.ts b/server/src/ecs/World.ts index 1b6a946..c1a6ee0 100644 --- a/server/src/ecs/World.ts +++ b/server/src/ecs/World.ts @@ -6,6 +6,7 @@ export class World { private nextId: EntityId = 1; private components: Map> = new Map(); private entities: Set = new Set(); + private singletons: Map = new Map(); createEntity(): EntityId { const id = this.nextId++; @@ -48,4 +49,12 @@ export class World { getAllEntities(): EntityId[] { return [...this.entities]; } + + setSingleton(name: string, data: T): void { + this.singletons.set(name, data); + } + + getSingleton(name: string): T | undefined { + return this.singletons.get(name) as T | undefined; + } } diff --git a/server/src/ecs/__tests__/World.test.ts b/server/src/ecs/__tests__/World.test.ts index ce09e40..fa7a92d 100644 --- a/server/src/ecs/__tests__/World.test.ts +++ b/server/src/ecs/__tests__/World.test.ts @@ -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>('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(); + }); + }); });