Source: Systems.js

goog.provide('entitas.Systems');


/**
 * @constructor
 *
 */
entitas.Systems = function() {
  this._initializeSystems = [];
  this._executeSystems = [];
  /**
   * Load Extensions
   */
}
/**
 * Add System
 * @param system
 * @returns {entitas.Systems}
 */
entitas.Systems.prototype.add = function (system) {
  if ('function' === typeof system) {
    var Klass = system;
    system = new Klass();
  }
  var reactiveSystem = as(system, 'subsystem');
  var initializeSystem = reactiveSystem != null
    ? as(reactiveSystem.subsystem, 'initialize')
    : as(system, 'initialize');
  if (initializeSystem != null) {
    var _initializeSystems = this._initializeSystems;
    _initializeSystems[_initializeSystems.length] = initializeSystem;
  }
  var executeSystem = as(system, 'execute');
  if (executeSystem != null) {
    var _executeSystems = this._executeSystems;
    _executeSystems[_executeSystems.length] = executeSystem;
  }
  return this;
};
/**
 * Initialize Systems
 */
entitas.Systems.prototype.initialize = function () {
  for (var i = 0, initializeSysCount = this._initializeSystems.length; i < initializeSysCount; i++) {
    this._initializeSystems[i].initialize();
  }
};
/**
 * Execute sustems
 */
entitas.Systems.prototype.execute = function () {
  var executeSystems = this._executeSystems;
  for (var i = 0, exeSysCount = executeSystems.length; i < exeSysCount; i++) {
    executeSystems[i].execute();
  }
};
/**
 * Clear subsystems
 */
entitas.Systems.prototype.clearReactiveSystems = function () {
  for (var i = 0, exeSysCount = this._executeSystems.length; i < exeSysCount; i++) {
    var reactiveSystem = as(this._executeSystems[i], 'subsystem');
    if (reactiveSystem != null) {
      reactiveSystem.clear();
    }
    var nestedSystems = as(this._executeSystems[i], 'clearReactiveSystems');
    if (nestedSystems != null) {
      nestedSystems.clearReactiveSystems();
    }
  }
};