Node/Web Implementation of an Entity Component System.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
lib
test
.editorconfig
.eslintrc.json
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
index.js
package-lock.json
package.json

README.md

Entity Component System (ECS)

Build Status Dependencies License

An Entity Component System (ECS) is an alternative to inheritance-based development patterns. Commonly used in Game Development due to components (E.g. Health) being used by multiple systems that cannot share a parent class.

Installation

npm install --save @oyed/ecs

Explanation

Entity

Purely a JavaScript Object containing a key (Component) with an Object defining the config overrides for that Component. For example:

const Dog = {
    barks: { // `barks` is a Component.
        says: 'woof woof!', // Overrides the default `says` value for the Component.
    },
};

Component

Like Entity definitions, Components are purely JavaScript Objects. Currently they only define default config for the Component. For example:

const barks = {
    defaults: { // The default config for the Component.
        cnt: 0,
        max: 60,
        says: 'bark bark!',
    },
    sync: true, // Whether this Component should be returned in a JSON representation of this Component. Defaults to `true`.
};

System

Systems are where everything happens; created/removed hooks, regular updates and entity updates. For example:

import { System } from '@oyed/ecs';

class Barks extends System {

    /**
     * Initializes the new System.
     *
     * @param {Manager} manager
     */
    constructor(manager) {
        super(manager, 'barks');

        this.totalWeirdEntities = 0;
    }

    /**
     * Called when the given Entity has been created with the given config.
     *
     * @param {Entity} entity
     * @param {Object} config
     */
    created(entity, config) {
        this.totalWeirdEntities++;
    }

    /**
     * Called when the given Entity has been removed.
     *
     * @param {Entity} entity
     */
    removed(entity) {
        this.totalWeirdEntities--;
    }

    /**
     * Called to run this System on the given Entities.
     *
     * @param {Array} entities
     */
    update(entities) {
        const total = entities.length;

        for (let e = 0; e < total; e++) {
            const entity = entities[e];

            entity.components.barks.cnt++;

            if (entity.components.barks.cnt >= entity.components.barks.max) {
                console.log(`Says ${entity.components.barks.says}`);
                entity.components.barks.cnt = 0;
            }
        }
    }

}

A full breakdown of the functions in a System:

  • constructor - Happens once after the Manager has been initialized.
  • created - Whenever an Entity with this Component is created, this will be called.
  • removed - Whenever an Entity with this Component is removed, this will be called.
  • update - Happens once every update call to the Manager.

Testing

The library uses Mocha to run testing.

npm test