Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send events to Processors #24

Open
adngdb opened this issue Oct 19, 2016 · 1 comment
Open

Send events to Processors #24

adngdb opened this issue Oct 19, 2016 · 1 comment
Assignees
Labels

Comments

@adngdb
Copy link
Owner

adngdb commented Oct 19, 2016

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
Copy link
Owner Author

adngdb commented Jun 8, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant