'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
};
}
};