Note: Pre-alpha, subject to breaking changes.
const manager = new Simple.EntityManager([entities])
Orchestrator of operations for Entities.
entities(optional) Pre-allocated structure for stack of entity IDs. If omitted, one is created by default.
To use with the World interface:
const world = new Simple.World();
world.EntityManager = new Simple.EntityManager();
// This interface is then available as `world.entities`
const entity = world.entities.create();const entity = manager.create()
Get the next available entity ID.
manager.register(entity, componentsKey)
Associate an entity (ID) with a componentsKey.
In context with Component:
let componentsKey = world.components.createComponentsKey(
'name',
'position'
),
entity = world.entities.create();
world.entities.register(entity, componentsKey);manager.attachComponents(id, ...components)
Attach any number of components to an entity (ID).
idEntity ID...componentsObjects representing component data.
let ids = manager.query(componentsKey)
Retrieve an array of Entity IDs associated with a componentsKey.
const manager = new Simple.ComponentManager()
Orchestrator of operations for Components.
To use with the World interface:
const world = new Simple.World();
world.ComponentManager = new Simple.ComponentManager();
// This interface is then available as `world.components`
world.components.createComponentsKey("name", "position");let key = manager.createComponentsKey("name", "position");
Create a key by providing the names of components.
In context:
const world = new Simple.World();
world.ComponentManager = new Simple.ComponentManager();
world.EntityManager = new Simple.EntityManager();
world.SystemManager = new Simple.SystemManager();
let componentsKey = world.components.createComponentsKey([
'name', 'position', 'direction', 'velocity'
]);
let player = world.entities.create();
world.entities.register(player, componentsKey);const manager = new Simple.SystemManager()
Orchestrator of operations for Systems.
To use with the World interface:
const world = new Simple.World();
world.SystemManager = new Simple.SystemManager();
// ...setup
// This interface is then available as `world.systems`
world.systems.register_queue(queue, componentsKey);world.systems.register_queue(queue, componentsKey)
Register a queue of Systems, grouped by component key, with the manager. This key determines which entities the systems will receive.
queueAn Iterable ofSystemobjects implementingupdate(entity, components).componentsKeyKey generated withcomponents.createComponentsKey(...components).
world.systems.query(componentsKey)
Returns the SystemQueue associated with componentsKey.
componentsKeyKey generated withcomponents.createComponentsKey(...components).
world.systems.runQueue(componentsKey, entity, components)
Run all systems in a queue, passing them entity, components. This is called by World when used with world integration (and so not usually called directly).
let queue = new SystemQueue([system1, system2])
A queue of System objects implementing update(entity, components).
Optionally, Systems can be run in a specified order. Set queue.sort to true, and set a priority attribute on each System.
queueAn iterable ofSystemobjects.
class NewSystem extends Simple.System {}
It's not required to inherit from this class to create instances, just potentially convenient. The only requirement for Systems is that they implement an interface containing update(entity, components).
Example usage:
class MovementSystem extends Simple.System {
update(entity, components) {
let dirRads = components[0].direction * Math.PI/180;
let velX = Math.sin(dirRads) * components[0].speed;
let velY = Math.cos(dirRads) * components[0].speed;
components[0].position[0] += velX;
components[0].position[1] += velY;
}
}
// Set up some entities and components
const world = new Simple.World();
world.ComponentManager = new Simple.ComponentManager();
world.EntityManager = new Simple.EntityManager();
world.SystemManager = new Simple.SystemManager();
let componentKey = world.components.createComponentsKey([
'name', 'position', 'direction', 'speed'
]);
let mob = world.entities.create();
world.entities.register(mob, componentKey);
world.entities.attachComponents(mob, {
name: 'mob',
position: [10,10],
direction: 270,
speed: 1
});
// Register the name system with the SystemManager.
let moveSystem = new MovementSystem(),
queue = new Simple.SystemQueue([moveSystem]);
// Register a new system queue
world.systems.register_queue(queue, componentKey);On each tick, all entities with a matching componentsKey with be passed to the movementSystem and have their Component instance data evaluated and updated.
const world = new Simple.World();
The World is the only object aware of all other Simple objects. The prescribed pattern is to set three special attributes with new instances at start-up time:
const world = new Simple.World();
// Worlds have special behavior when these three particular attributes
// set:
world.ComponentManager = new Simple.ComponentManager();
world.EntityManager = new Simple.EntityManager();
world.SystemManager = new Simple.SystemManager();
// Get instances like this:
// `world.components` came from `ComponentManager`
let key = world.components.createComponentKey(['name', 'position']);
// `world.entities` came from `EntityManager`
let tree = world.entities.create();
world.entities.register(tree, key);
world.entities.attachComponents(tree, {
name: 'tree',
position: [2, 1, 0]
});
// `world.systems` came from `SystemManager`
class PositionSystem extends Simple.System {
update(entity, components) {
// do something with components[0].position[0], etc.
}
}
let positionSystem = new PositionSystem();
// Register a key with a queue:
world.systems.register_queue(
new Simple.SystemQueue([positionSystem]),
key
);At this point, any calls to World.update() will run all System.update() for Systems that have been registered with a componentsKey, so it's ready to be used in a loop.