var Class = require('../utils/Class'); var PluginCache = require('../plugins/PluginCache'); var UpdateList = new Class({ initialize: function UpdateList(scene){ this.scene = scene; this.systems = scene.sys; this._list = [] ; this._pendingInsertion = [] ; this._pendingRemoval = [] ; scene.sys.events.once('boot', this.boot, this); scene.sys.events.on('start', this.start, this); } , boot: function (){ this.systems.events.once('destroy', this.destroy, this); } , start: function (){ var eventEmitter = this.systems.events; eventEmitter.on('preupdate', this.preUpdate, this); eventEmitter.on('update', this.update, this); eventEmitter.once('shutdown', this.shutdown, this); } , add: function (child){ if (this._list.indexOf(child) === -1 && this._pendingInsertion.indexOf(child) === -1) { this._pendingInsertion.push(child); } return child; } , preUpdate: function (){ var toRemove = _AN_Read_length('length', this._pendingRemoval); var toInsert = _AN_Read_length('length', this._pendingInsertion); if (toRemove === 0 && toInsert === 0) { return ; } var i; var gameObject; for (i = 0; i < toRemove; i++ ){ gameObject = this._pendingRemoval[i]; var index = this._list.indexOf(gameObject); if (index > -1) { this._list.splice(index, 1); } } this._list = this._list.concat(this._pendingInsertion.splice(0)); this._pendingRemoval.length = 0; this._pendingInsertion.length = 0; } , update: function (time, delta){ for (var i = 0; i < (_AN_Read_length('length', this._list)); i++ ){ var gameObject = this._list[i]; if (gameObject.active) { gameObject.preUpdate.call(gameObject, time, delta); } } } , remove: function (child){ var index = this._list.indexOf(child); if (index !== -1) { this._list.splice(index, 1); } return child; } , removeAll: function (){ var i = _AN_Read_length('length', this._list); while (i-- ){ this.remove(this._list[i]); } return this; } , shutdown: function (){ this.removeAll(); this._list.length = 0; this._pendingRemoval.length = 0; this._pendingInsertion.length = 0; var eventEmitter = this.systems.events; eventEmitter.off('preupdate', this.preUpdate, this); eventEmitter.off('update', this.update, this); eventEmitter.off('shutdown', this.shutdown, this); } , destroy: function (){ this.shutdown(); this.scene.sys.events.off('start', this.start, this); this.scene = null ; this.systems = null ; } , length: { get: function (){ return (_AN_Read_length('length', this._list)); } } } ); PluginCache.register('UpdateList', UpdateList, 'updateList'); module.exports = UpdateList;