System

A system in PrimeEngine is a class that extends the abstract System class. Systems must implement the queries property which contain the different components that will be queried for each query. The system can also extend the methods preStart, start, postStart as well as preUpdate, update, postUpdate. These methods are called when the Scene starts and updates. The pre and post methods does not call with a specific query name but with all the queried entities while start and update is called multiple times, once for each matching query. When start and update is called they are called with the parameters for the query that matched and all of the matching entities for that specific query. preUpdate, update, postUpdate are also called with a parameter for the current delta time of the Scene update.

Example

A simple system for rotating entities:

export class RotatorSystem extends System {
    private angle: number = 0;

    public readonly queries = { Rotatable: [Transform] };

    public update(query: string, entities: Entity[], delta: number): void {
        this.angle += delta;

        if (this.angle > Math.PI * 2) this.angle = 0;

        for (const entity of entities) {
            entity.components.get(Transform).rotation = this.angle;
        }
    }
}

This system queries for all entities with the Transform Component. Each update when all of the entities matching the query are found the update method is called. In this case the query parameter is Rotatable because the matched queries name is Rotatable. The entities parameter contains all of the matched entities in an array.

In the update method of the system the systems internal property angle is changed by the delta time. The system then resets the angle property to zero if its rotated 360°. After that the system loops over each Entity that matched changing its Transform rotation to the angle.