entity-system-js icon indicating copy to clipboard operation
entity-system-js copied to clipboard

Send events to Processors

Open adngdb opened this issue 9 years ago • 1 comments

Based on @CodestarGames' proposition in #21.

This is a proposal. I am not sure yet I want to include that. I am concerned that it could change the nature of processors from a simple "function" to something event-based. There are definitely places where I could see abusing the event thing, notably in node.js applications where running a loop is less practical.


The manager will emit events to processors on various occasions. Processors which implement the on method will receive each event emitted by the manager, and then do whatever they want with it. Processors which do not implement the on method will simply be skipped.

The on method accepts 2 arguments, the first one is a string identifying the event, and the second is an object containing data associated with the event. For example:

class MyProcessor {
    on(type, data) {
        switch (type) {
            // ...
        }
    }
}

For a start, events will be:

Type Data
COMPONENT_CREATED entity, component, state
COMPONENT_REMOVED entity, component
COMPONENT_STATE_UPDATED entity, component, state

The data sent along with each event will be:

{
    entity: 'entity ID', 
    component: 'component name', 
    state: {
        /* component state */
    }
}

Here's what a Processor using that could look like:

class RenderingProcessor {
    constructor(manager) {
        this.manager = manager;
        this._sprites = {};
    }

    on(type, data) {
        switch (type) {
            case 'COMPONENT_CREATED':
                if (data.component === 'Display') {
                    let display = data.state;
                    let position = this.manager.getComponentDataForEntity('Position', data.entity);
                    let newSprite = this.game.add.sprite(position.x, position.y, display.spriteImagePath);
                    this._sprites[data.entity] = newSprite;
                }
                break;
            case 'COMPONENT_REMOVED':
                if (data.component === 'Display' && this._sprites[data.entity]) {
                    this._sprites[entity].kill();
                    delete this._sprites[entity];
                }
                break;
            default:
                break;
        }
    }

    update(dt) {
        let displays = this.manager.getComponentsData('Display');
        for (let entity in displays) {
            let display = displays[entity];
            let position = this.manager.getComponentDataForEntity('Position', entity);

            this._sprites[entity].x = position.x;
            this._sprites[entity].y = position.y;
        }
    }
}

adngdb avatar Oct 19 '16 23:10 adngdb

Events COMPONENT_CREATED and COMPONENT_UPDATED are now sent to processors, thanks to @maxailloud in #37. Keeping this open to remember to add COMPONENT_REMOVED eventually.

adngdb avatar Jun 08 '20 08:06 adngdb