Skip to content
A dead simple example Entity-Component-System in JS, with a Manager class.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
ECSManager.js
Entity.js
LICENSE
README.md
System.js

README.md

simple-js-ecs

A dead simple example Entity-Component-System in JS, with a Manager class.

// create a new System (for wagging tails on Entities that have the Animal and Tail components)
ecs.addSystem(
    new ClassThatExtendsSystem('tail-wagger', ['animal', 'tail']);
);

// make a new Entity, a dog
let dog = ecs.createEntity('dog');

// add components to the dog
ecs.addComponentToEntity(
    ecs.createComponent('animal', { 'type': 'doggo', 'good_boy': 'the best' }),
    dog
);

ecs.addComponentToEntity(
    ecs.createComponent('tail', { 'length': 'medium', 'color': 'brown', 'bushy': 'you bet' }),
    dog
);

// the tail-wagger system will now be "subscribed" to this dog Entity, as it has all the components needed
console.log(ecs.systems[0].subscribed_to);

// get the ECS manager to process all systems, which will in turn call ClassThatExtendsSystem.processEntity() 
// with the dog's entity ID (implementation left as an exercise for the reader)

ecs.process();

// other things you might like to do:
ecs.deleteEntity(entity_uid);
ecs.removeComponentFromEntity(component_name, entity_uid); // both of these automatically unsubscribe from relevant Systems

ecs.pauseSystems(); // stops all systems from processing when ecs.process() is called
ecs.unpauseSystems();

system.pause(); // stops a specific system from processing during the main ecs.process() loop
system.unpause();
You can’t perform that action at this time.