import { Component , ComponentId } from "./component" ; import ECS from "./ecs" ; import Entity from "./entity" ; import { ComponentProperties as Components } from "../../components" ; import { fastSplice } from "./utils" ; description class System { /**
     * Frequency of update execution, a frequency of `1` run the system every
     * update, `2` will run the system every 2 updates, ect.
     */ frequency : number ; ecs ! : ECS ; /**
     * Entities of the system.
     */ entities : Entity [ ] = [ ] ; constructor ( frequency = 1 ) { this . frequency = frequency ; } /**
     * Add an entity to the system entities.
     */ addEntity ( entity ) { entity . addSystem ( this ) ; this . entities . push ( entity ) ; this . enter ( entity ) ; } param { Entity } entity removeEntity ( entity ) { let index = this . entities . findIndex ( e => e . id === entity . id ) ; if ( index !== - 1 ) { entity . removeSystem ( this ) ; fastSplice ( this . entities , index , 1 ) ; this . exit ( entity ) ; } } /**
     * Apply update to each entity of this system.
     */ updateAll ( elapsed = 1 ) { this . preUpdate ( elapsed ) ; for ( let i = 0 , entity ; ( entity = this . entities [ i ] ) ; i += 1 ) { this . update ( entity , elapsed ) ; } this . postUpdate ( elapsed ) ; } /**
     * dispose the system by exiting all the entities
     */ dispose ( ) { for ( let i = 0 , entity ; ( entity = this . entities [ i ] ) ; i += 1 ) { entity . removeSystem ( this ) ; this . exit ( entity ) ; } } /**
     * Abstract method to subclass. Called once per update, before entities
     * iteration.
     */ preUpdate ( _elapsed ) { } /**
     * Abstract method to subclass. Called once per update, after entities
     * iteration. Use this to publish any relevant updates.
     */ postUpdate ( _elapsed ) { } /**
     * Abstract method to subclass. Should return true if the entity is eligible
     * to the system, false otherwise.
     */ test ( _entity ) { return false ; } /**
     * Abstract method to subclass. Called when an entity is added to the system.
     */ enter ( _entity ) { } /**
     * Abstract method to subclass. Called when an entity is removed from the system.
     */ exit ( _entity ) { } /**
     * Abstract method to subclass. Called for each entity to update. This is
     * the only method that should actual mutate entity state.
     */ update ( _entity , _elapsedMs ) { } } export default System ;