Skip to content
Rogan Murley edited this page Oct 28, 2015 · 11 revisions

A System describes how the game state responds to changes in the World. They can be told to track certain Components, allowing them to keep references to all Entities with those Components at any time.

Usage

var VelocitySystem = {
    update: {
        velocity: function (entity, dt) {
            entity.c.position.x += utils.delta(entity.c.velocity.xspeed, dt);
            entity.c.position.y += utils.delta(entity.c.velocity.yspeed, dt);
        }
    }
};
var VelocitySystem = function () {
    this.update = {
        velocity: function (entity, dt) {
            entity.c.position.x += utils.delta(entity.c.velocity.xspeed, dt);
            entity.c.position.y += utils.delta(entity.c.velocity.yspeed, dt);
        }
    };
};
var PlayerSystem = function () {
    this.$tracking = {
        'player': 'single',
        'enemy': 'many'
    };

    this.tickStart = function () {
        console.log(this.$tracked.player);
        _.each(this.$tracked.enemy, function (enemy) {console.log(enemy);});
    };
};

Optional properties

update: During a tick the System gets the chance to update Entities with Components it's interested in. This property is an object mapping Component IDs to functions that update Entities with those Components.

{
    string: function {
            parameters:
                entity - Entity being updated
                dt - delta time
            return nothing
        }
}

build: When an Entity is added to the World a System gets the chance to do something with it ('build' it) if it has Components it's interested in. This property is an object mapping Component IDs to functions that build Entities with those Components.

{
    string: function {
            parameters:
                entity - Entity being built
            return nothing
        }
}

destroy: When an Entity is removed from the World a System gets the chance to do something with it ('destroy' it) if it has Components it's interested in. This property is an object mapping Component IDs to functions that destroy Entities with those Components.

{
    string: function {
            parameters:
                entity - Entity being destroyed
            return nothing
        }
}

$tracking: Systems may want to keep references to all Entities with Components they're interested in. The $tracking object maps Component IDs to a string with the values 'many' or 'single'. Any Entities in the World with these Components are referenced by the '$tracked' property (see below). If a Component is marked as 'single', an error will be thrown if the World has more then one Entity with that Component.

{
    string: 'many' or 'single'
}

$tracked: This property keeps references to Entities with the Components tracked by the $tracking property. It is a map of Component IDs to either a single Entity in the case of the tracking type being 'single', or a map of Entity UIDs to Entities if the tracking type is 'many'.

{
    string: Single tracked Entity,
    string: {
        string: Many tracked Entity
    }
}

Optional methods

tickStart: Called by the World at the start of every tick, before updates occur.

parameters:
    dt - delta time
return: nothing

tickEnd: Called by the World at the end of every tick, after updates occur.

parameters:
    dt - delta time
return: nothing

deregister: Called by the World when the system is deregistered. Used for cleanup.

parameters:
    none
return: nothing
Clone this wiki locally