System

System

A system defines an operation that should be performed each tick of the engine. A system will only affect entities that own the required system components. A system will watch and unwatch an entity as the required components are added and removed from it. A system is created based on a configuration that is passed to the constructor.

Constructor

new System(config)

Example
#Creating a new system.
const system = new System({
    name: 'spawn',
    requirements: ['player', 'spawn'],
    interval: 16,
    executor: ({ entity }) => {
        entity.addComponents(new Component('position', { x: 0, y: 0, z:0 }));
        entity.removeComponents('spawn');
        const name = entity.getComponents('player').getState('name');
        console.log(`${name} has come into the world.`);
     },
});
Mixes In:
Parameters
Name Type Description
config Object The system configuration.
Properties
Name Type Attributes Default Description
name string The system name.
requirements Array.<string> <optional>
The component types that are required by the system.
interval number <optional>
16 The number of milliseconds between executions.
executor function The function to execute.

Members

(private) executedOn :number

The last time the system executed.

(private) executor :function

The function that is executed each update.

(private) interval :number

The number milliseconds between executions.

(private) latency :number

The number of milliseconds that the system has fallen behind.

(private) manager :EntityManager

The entity manager for the scope of entities the system is watching.

(private) name :string

A literal that identifies the system.

(private) requirements :Array.<string>

The component types that an entity must have to be watched by the system.

(private) state :string

The state of the system.

Methods

execute(context) → {Promise}

Executes the system executor for each entity within scope. The system will pass a context object to the executor that exposes resources to be used within the executor.
Parameters
Name Type Description
context Object A context from a parent scopes.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
The system execution.
Type
Promise

getExecutedOn() → {number}

Get the time last executed.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
The last execution time.
Type
number

getInterval() → {number}

Get the execution interval.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
The execution interval.
Type
number

getName() → {string}

Get the system name.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
The system name.
Type
string

getRequirements() → {Array.<string>}

Get the system requirements.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
The system requirements.
Type
Array.<string>

getScope() → {Array.<Entity>}

Get the entities in scope of the system.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
The entity scope of the system.
Type
Array.<Entity>

getState() → {string}

Get the system state.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
The system state.
Type
string

init(source) → {Promise}

Initialize the system by providing it with a source entity manager. The system sets up the appropriate listeners that is will need to identify the entities that are within scope of the system requirements and then identify the initial scope from the already existing entities.
Parameters
Name Type Description
source EntityManager The source entity manager for the system.
Throws
ReferenceError
Will throw this error if invoked after being disposed.
TypeError
Will throw this error is the source is not an EntityManager.
Returns
The system initialization.
Type
Promise

unwatchEntity(entity) → {System}

Will remove the entity from the scope of entities that are updated by the system, if the entity no longer meets the system requirements.
Parameters
Name Type Description
entity Entity The entity to watch.
Fires:
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
Itself
Type
System

watchEntity(entity) → {System}

Will add the entity to the scope of entities that are updated by the system, if the entity meets the system requirements.
Parameters
Name Type Description
entity Entity The entity to watch.
Fires:
Throws
ReferenceError
Will throw this error if invoked after being disposed.
Returns
Itself
Type
System

Events

system:unwatch

A system:unwatch event occurs when the systems unwatches an entity.
Parameters
Name Type Description
entity Entity The entity that has been unwatched.

system:watch

A system:watch event occurs when the systems watches an entity.
Parameters
Name Type Description
entity Entity The entity that has been watched.