diff options
author | elioat <{ID}+{username}@users.noreply.github.com> | 2024-12-27 10:12:40 -0500 |
---|---|---|
committer | elioat <{ID}+{username}@users.noreply.github.com> | 2024-12-27 10:12:40 -0500 |
commit | 558906f2349403e781c78e0385b70a08d320a21e (patch) | |
tree | 3fe1985fbd0f7c8cb131e940b9ba4e42a4c3cc6f /html/text-world/js/ecs.js | |
parent | 17a21fefda47f5c6d70c450dd6782e1c1e2d5877 (diff) | |
download | tour-558906f2349403e781c78e0385b70a08d320a21e.tar.gz |
*
Diffstat (limited to 'html/text-world/js/ecs.js')
-rw-r--r-- | html/text-world/js/ecs.js | 93 |
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 + }; + } +}; |