about summary refs log tree commit diff stats
path: root/html/text-world/js/ecs.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/text-world/js/ecs.js')
-rw-r--r--html/text-world/js/ecs.js93
1 files changed, 93 insertions, 0 deletions
diff --git a/html/text-world/js/ecs.js b/html/text-world/js/ecs.js
new file mode 100644
index 0000000..c2c5add
--- /dev/null
+++ b/html/text-world/js/ecs.js
@@ -0,0 +1,93 @@
+'use strict';
+
+// Components remain as simple data structures
+class Component {
+    constructor(data = {}) {
+        Object.assign(this, data);
+    }
+}
+
+class Position extends Component {
+    constructor(x = 0, y = 0) {
+        super({ x, y });
+    }
+}
+
+class Description extends Component {
+    constructor(short = "", long = "", explorable = {}) {
+        super({ short, long, explorable });
+    }
+}
+
+class Inventory extends Component {
+    constructor(items = []) {
+        super({ items });
+    }
+}
+
+class Messages extends Component {
+    constructor(messages = []) {
+        super({ messages });
+    }
+}
+
+class Item extends Component {
+    constructor(name, description, collectable = true) {
+        super({ name, description, collectable });
+    }
+}
+
+// World becomes a pure functional interface
+const World = {
+    create() {
+        return {
+            entities: new Map(),
+            nextEntityId: 1
+        };
+    },
+
+    createEntity(world) {
+        const id = world.nextEntityId;
+        const newEntities = new Map(world.entities);
+        newEntities.set(id, new Map());
+        
+        return {
+            ...world,
+            entities: newEntities,
+            nextEntityId: id + 1
+        };
+    },
+
+    addComponent(world, entityId, component) {
+        if (!world.entities.has(entityId)) return world;
+        
+        const newEntities = new Map(world.entities);
+        const entityComponents = new Map(world.entities.get(entityId));
+        entityComponents.set(component.constructor.name, component);
+        newEntities.set(entityId, entityComponents);
+        
+        return {
+            ...world,
+            entities: newEntities
+        };
+    },
+
+    getComponent(world, entityId, componentType) {
+        if (!world.entities.has(entityId)) return null;
+        return world.entities.get(entityId).get(componentType);
+    },
+
+    removeComponent(world, entityId, componentType) {
+        if (!world.entities.has(entityId)) return world;
+        
+        const newEntities = new Map(world.entities);
+        const entityComponents = new Map(world.entities.get(entityId));
+        entityComponents.delete(componentType);
+        newEntities.set(entityId, entityComponents);
+        
+        return {
+            ...world,
+            entities: newEntities
+        };
+    }
+};