2017-07-04 15:44:16 +00:00
|
|
|
var Class = require('../utils/Class');
|
|
|
|
|
|
|
|
var UpdateList = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function UpdateList (state)
|
|
|
|
{
|
|
|
|
this.state = state;
|
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list = [];
|
2017-07-04 15:44:16 +00:00
|
|
|
this._pendingInsertion = [];
|
|
|
|
this._pendingRemoval = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
add: function (child)
|
|
|
|
{
|
|
|
|
this._pendingInsertion.push(child);
|
|
|
|
},
|
|
|
|
|
|
|
|
begin: function ()
|
|
|
|
{
|
|
|
|
var toRemove = this._pendingRemoval.length;
|
|
|
|
var toInsert = this._pendingInsertion.length;
|
|
|
|
|
|
|
|
if (toRemove === 0 && toInsert === 0)
|
|
|
|
{
|
|
|
|
// Quick bail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var i;
|
2017-07-04 22:43:13 +00:00
|
|
|
var gameObject;
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-07-04 22:43:13 +00:00
|
|
|
// Delete old gameObjects
|
2017-07-04 15:44:16 +00:00
|
|
|
for (i = 0; i < toRemove; i++)
|
|
|
|
{
|
2017-07-04 22:43:13 +00:00
|
|
|
gameObject = this._pendingRemoval[i];
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
var index = this._list.indexOf(gameObject);
|
2017-07-04 15:44:16 +00:00
|
|
|
|
|
|
|
if (index > -1)
|
|
|
|
{
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list.splice(index, 1);
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pool them?
|
2017-07-04 22:43:13 +00:00
|
|
|
// gameObject.destroy();
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 22:43:13 +00:00
|
|
|
// Move pending to active
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list = this._list.concat(this._pendingInsertion.splice(0));
|
2017-07-04 15:44:16 +00:00
|
|
|
|
|
|
|
// Clear the lists
|
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function (time, delta)
|
|
|
|
{
|
2017-07-07 17:14:27 +00:00
|
|
|
for (var i = 0; i < this._list.length; i++)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2017-07-07 17:14:27 +00:00
|
|
|
var gameObject = this._list[i];
|
2017-07-04 15:44:16 +00:00
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
if (gameObject.active)
|
|
|
|
{
|
|
|
|
gameObject.preUpdate.call(gameObject, time, delta);
|
|
|
|
}
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// State that owns this Clock is shutting down
|
|
|
|
shutdown: function ()
|
|
|
|
{
|
|
|
|
var i;
|
|
|
|
|
|
|
|
for (i = 0; i < this._pendingInsertion.length; i++)
|
|
|
|
{
|
|
|
|
this._pendingInsertion[i].destroy();
|
|
|
|
}
|
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
for (i = 0; i < this._list.length; i++)
|
2017-07-04 15:44:16 +00:00
|
|
|
{
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list[i].destroy();
|
2017-07-04 15:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < this._pendingRemoval.length; i++)
|
|
|
|
{
|
|
|
|
this._pendingRemoval[i].destroy();
|
|
|
|
}
|
|
|
|
|
2017-07-07 17:14:27 +00:00
|
|
|
this._list.length = 0;
|
2017-07-04 15:44:16 +00:00
|
|
|
this._pendingRemoval.length = 0;
|
|
|
|
this._pendingInsertion.length = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Game level nuke
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.shutdown();
|
|
|
|
|
|
|
|
this.state = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = UpdateList;
|