itay-ecs
A generic entity component system in typescript.
The goal of this library is to allow combining multiple independent libraries to create games.
For example: Use a rendering library like "pixi", "threejs", "babylon" etc. With a physics system like "Box2D" or "p2.js".
Install
npm install --save itay-ecs
Concept
Component
A piece of information (of an entity) on a specific topic.
For example, A person entity might have a "HeightComponent" that holds a number to represent the person's height.
Components can be used to mark an entity, without having any additional properties.
For example, A person entity can hold a "TeacherComponent" to be marked as a teacher.
Entity
A logical representatin of an object. Its data is contained in the components it holds.
System
Contains logic that acts on entities, using the entities components.
In this library
Every class can be a component, from a simple number to a complex class.
A component is usualy identified by its class. To be more exact, his constructor function (well thats javascript).
A component can also be referenced by other and multiple constructors. See the advanced section.
The library allows you to choose what to use and how.
The library provides:
- ComponentsCollection class - to hold the entity's components.
- EntitiesCollection class - to hold entities (objects that implements Entity).
When it comes to systems, implement as you like.
Check the usage sections to learn about classes and functionality that will make your life very simple when implementin a system.
Basic Usage
ComponentsCollection
let collection = new ComponentsCollection();
let component = 1;
collection.add(component);
let myNumber: number = collection.get(Number);
EntitiesCollection
class TankEntity implements Entity {
public components: ComponentsCollection = new ComponentsCollection();
constructor() {
this.components.add(new GunsComponent(7));
}
}
class GunsComponent {
public power: number = 1;
constructor(power?: number) {
if (power) {
this.power = power;
}
}
}
let entities = new EntitiesCollection();
entities.add(new TankEntity());
let myShootingEntities: Entity[] = collection.getByComponent(GunsComponent);
console.log(myShootingEntities.length);
Advanced Usage
Shared Components
Usualy a component is tailor made for a specific system.
That raises some common problems:
- Sharing information between systems is hard, and might cause coupling.
- Mixing third-party libraries is hard.
For example, a "PositionComponent" is used by the physics-system and by the render-system.
This library offers a simple solution: Multiple component keys.
Given the classes:
class StaticBodyComponent {
public x: number;
public y: number;
}
class RenderablePositionComponent {
public posX: number;
public posY: number;
}
class MyPositionComponent {
public x: number;
public y: number;
public get posX() {
return this.x;
}
public get posY() {
return this.y;
}
}
We can:
let collection = new ComponentsCollection();
let myComponent = new MyPositionComponent();
myComponent.x = 1;
myComponent.y = 2;
collection.add(myComponent, StaticBodyComponent, RenderablePositionComponent);
let staticBody = collection.get(StaticBodyComponent);
let renderable = collection.get(RenderablePositionComponent);
Observe entities
Observing entities collection is a simple and usefull way to keep track of a set of entities.
One use case is using observation to keep track of entities for a system.
let collection = new EntitiesCollection();
let addedTriggered = false;
let observation = new EntitiesObservation();
observation.added = entity => addedTriggered = true;
observation.filter = EntitiesFilter.componentsContainsAny([GunsComponent, SomeOtherComponent])
collection.addObservation(observation);
collection.add(new TankEntity());
console.log(addedTriggered);
EntitiesSearchCache
EntitiesSearchCache class holds a list of filtered entities from an EntitiesCollection.
Its implemented using the observation feature.
You can use this class to hold entities for a system. This will be the simplest approach.
let collection = new EntitiesCollection();
let entityToRemove = new NumberBooleanEntity();
collection.add(entityToRemove);
collection.add(new NumberStringEntity());
let cache = EntitiesSearchCache.from(collection).componentsContainsAll([Number, Boolean]);
collection.add(new NumberBooleanEntity());
collection.remove(entityToRemove);
for (let entity of cache.entities){
console.log(entity);
}