curbl-ecs
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

CURBL-ECS

curbl-ecs is an lightweight Entity Component System using decorator magic for Components, Entities and Systems. There is also a simple web worker support.

Example

  • Creating a Component
 
@ECS.Component()
class PositionComponent {
    public x: number;
    public y: number;
 
    constructor(config: {x: number, y: number} = { x:0, y:0 }) {
        this.x = config.x;
        this.y = config.y;
    }
}
  • Creating an Entity with components
 
@ECS.Entity(
    { component: PositionComponent, config: { x:12, y:12 }}
)
class PositionEntity {}
  • Adding a Entity with predefined Components
const entity = ECS.addEntity(new PositionEntity());
  • Create entity and add Component
const entity = ECS.createEntity();
entity.add(new PositionComponent());
  • Get Component from Entity
entity.get(PositionComponent).x = 42;
entity.get("PositionComponent").x = 42;
  • Creating a System
 
//All Entities with a PositionComponent will be added to the System
@ECS.System(PositionComponent) 
export class MySystem {
     
     setUp(): void{
          //Called when the System is created/added to the ECS 
     }
     
     tearDown(): void{
         //Called when the System is removed from the ECS
     }
     
     update(): void{
         for(let i = 0, entity: Entity; entity = this.entities[i]; i++){
             //Do stuff with the entities
         }
     }
}
  • Using Web Workers by adding the WebWorker to the main-thread. All Entities are automatically shared between all workers.
@ECS.Entity(
    {component: PositionComponent, config: {x: 1, y: 2, z: 4}}
)
class PositionEntity {}
 
ECS.addEntity(new PositionEntity());
 
const readWorker = new ReadWorker();
ECS.addWorker(readWorker);
 
const writeWorker = new WriteWorker();
ECS.addWorker(writeWorker);
 
ECS.update();

Package Sidebar

Install

npm i curbl-ecs

Weekly Downloads

1

Version

2.1.1

License

MIT

Unpacked Size

85.6 kB

Total Files

33

Last publish

Collaborators

  • nan0c