Added Pool Manager State plugin and start of ObjectPool class.

This commit is contained in:
photonstorm 2017-07-05 17:17:50 +01:00
parent 9f4db0317d
commit b494ace580
8 changed files with 235 additions and 101 deletions

View file

@ -12,6 +12,7 @@ module.exports = {
Group: require('./group/Group'), Group: require('./group/Group'),
Image: require('./image/Image'), Image: require('./image/Image'),
Mesh: require('./mesh/Mesh'), Mesh: require('./mesh/Mesh'),
ObjectPool: require('./pool/ObjectPool.js'),
Quad: require('./quad/Quad'), Quad: require('./quad/Quad'),
RenderPass: require('./renderpass/RenderPass.js'), RenderPass: require('./renderpass/RenderPass.js'),
Sprite: require('./sprite/Sprite'), Sprite: require('./sprite/Sprite'),

View file

@ -0,0 +1,93 @@
// Phaser.GameObjects.ObjectPool
var Class = require('../../utils/Class');
var Sprite = require('../sprite/Sprite');
// An Object Pool
var ObjectPool = new Class({
initialize:
function ObjectPool (config)
{
this.processing = false;
this._live = [];
this._pendingInsertion = [];
this._pendingRemoval = [];
this._dead = [];
this.classType = Sprite;
if (config)
{
this.createMultiple(config);
}
},
// Allow them to add a Group too
add: function (child)
{
if (Array.isArray(child))
{
for (var i = 0; i < child.length; i++)
{
this.pendingInsertion.push(child[i]);
}
}
else
{
this.pendingInsertion.push(child);
}
return this;
},
get: function ()
{
},
// getByName: function ()
// {
// },
getByName: function ()
{
},
// Moves from live to pendingRemoval
// is there a reason why it can't do direct to dead?
kill: function ()
{
},
killAndHide: function (gameObject)
{
gameObject.visible = false;
},
createMultiple: function ()
{
},
update: function (time, delta)
{
this.processing = true;
this.processing = false;
},
destroy: function ()
{
}
});
module.exports = ObjectPool;

View file

@ -1,80 +0,0 @@
var Class = require('../../utils/Class');
var GameObject = require('../GameObject');
var Sprite = require('../sprite/Sprite');
var NOOP = require('../../utils/NOOP');
// A Pool
var Pool = new Class({
Extends: GameObject,
initialize:
function Pool (state, children, config)
{
GameObject.call(this, state, 'Pool');
this.live = [];
this.pendingInsertion = [];
this.pendingRemoval = [];
this.dead = [];
this.classType = Sprite;
if (config)
{
this.createMultiple(config);
}
},
// Allow them to add a Group too
add: function (child)
{
if (Array.isArray(child))
{
for (var i = 0; i < child.length; i++)
{
this.pendingInsertion.push(child[i]);
}
}
else
{
this.pendingInsertion.push(child);
}
return this;
},
createMultiple: function ()
{
},
preUpdate: function (time, delta)
{
// Because a Group child may mess with the length of the Group during its update
// var temp = this.children.entries.slice();
// for (var i = 0; i < temp.length; i++)
// {
// if (temp[i].update(time, delta) === false)
// {
// break;
// }
// }
},
renderCanvas: NOOP,
renderWebGL: NOOP,
destroy: function ()
{
}
});
module.exports = Pool;

View file

@ -1,8 +0,0 @@
var Pool = require('./Pool');
var PoolCreator = function (state, config)
{
return new Pool(state, null, config);
};
module.exports = PoolCreator;

View file

@ -1,8 +0,0 @@
var Pool = require('./Pool');
var PoolFactory = function (state, children, config)
{
return new Pool(state, children, config);
};
module.exports = PoolFactory;

View file

@ -0,0 +1,129 @@
var Class = require('../utils/Class');
var ObjectPool = require('../gameobjects/pool/ObjectPool');
var PoolManager = new Class({
initialize:
function PoolManager (state)
{
this.state = state;
this._active = [];
this._pendingInsertion = [];
this._pendingRemoval = [];
this.processing = false;
},
add: function (pool)
{
if (this.processing)
{
this._pendingInsertion.push(pool);
}
else
{
this._active.push(pool);
}
return this;
},
create: function ()
{
var pool = new ObjectPool();
this.add(pool);
return pool;
},
begin: function ()
{
var toRemove = this._pendingRemoval.length;
var toInsert = this._pendingInsertion.length;
if (toRemove === 0 && toInsert === 0)
{
// Quick bail
return;
}
var i;
var pool;
// Delete old pools
for (i = 0; i < toRemove; i++)
{
pool = this._pendingRemoval[i];
var index = this._active.indexOf(pool);
if (index > -1)
{
this._active.splice(index, 1);
}
// Pool them?
// pool.destroy();
}
// Move pending to active
this._active = this._active.concat(this._pendingInsertion.splice(0));
// Clear the lists
this._pendingRemoval.length = 0;
this._pendingInsertion.length = 0;
},
update: function (time, delta)
{
this.processing = true;
for (var i = 0; i < this._active.length; i++)
{
var pool = this._active[i];
pool.update.call(pool, time, delta);
}
this.processing = false;
},
// State that owns this Pool is shutting down
shutdown: function ()
{
var i;
for (i = 0; i < this._pendingInsertion.length; i++)
{
this._pendingInsertion[i].destroy();
}
for (i = 0; i < this._active.length; i++)
{
this._active[i].destroy();
}
for (i = 0; i < this._pendingRemoval.length; i++)
{
this._pendingRemoval[i].destroy();
}
this._active.length = 0;
this._pendingRemoval.length = 0;
this._pendingInsertion.length = 0;
},
// Game level nuke
destroy: function ()
{
this.shutdown();
this.state = undefined;
}
});
module.exports = PoolManager;

View file

@ -14,15 +14,15 @@ var InjectionMap = {
add: 'add', add: 'add',
cameras: 'cameras', cameras: 'cameras',
data: 'data',
displayList: 'children',
events: 'events', events: 'events',
load: 'load', load: 'load',
make: 'make', make: 'make',
pool: 'pool',
stateManager: 'state', stateManager: 'state',
time: 'time', time: 'time',
tweens: 'tweens', tweens: 'tweens'
displayList: 'children',
data: 'data'
}; };

View file

@ -8,6 +8,7 @@ var EventDispatcher = require('../events/EventDispatcher');
var GameObjectCreator = require('../plugins/GameObjectCreator'); var GameObjectCreator = require('../plugins/GameObjectCreator');
var GameObjectFactory = require('../plugins/GameObjectFactory'); var GameObjectFactory = require('../plugins/GameObjectFactory');
var Loader = require('../plugins/Loader'); var Loader = require('../plugins/Loader');
var PoolManager = require('../plugins/PoolManager');
var Settings = require('./Settings'); var Settings = require('./Settings');
var StableSort = require('../utils/array/StableSort'); var StableSort = require('../utils/array/StableSort');
var StateManager = require('../plugins/StateManager'); var StateManager = require('../plugins/StateManager');
@ -48,6 +49,7 @@ var Systems = new Class({
this.events; this.events;
this.load; this.load;
this.make; this.make;
this.pool;
this.stateManager; this.stateManager;
this.time; this.time;
this.tweens; this.tweens;
@ -85,6 +87,7 @@ var Systems = new Class({
this.events = new EventDispatcher(); this.events = new EventDispatcher();
this.load = new Loader(state); this.load = new Loader(state);
this.make = new GameObjectCreator(state); this.make = new GameObjectCreator(state);
this.pool = new PoolManager(state);
this.stateManager = new StateManager(state, game); this.stateManager = new StateManager(state, game);
this.time = new Clock(state); this.time = new Clock(state);
this.tweens = new TweenManager(state); this.tweens = new TweenManager(state);
@ -117,10 +120,12 @@ var Systems = new Class({
return; return;
} }
this.updateList.begin(); this.pool.begin(time);
this.updateList.begin(time);
this.time.begin(time); this.time.begin(time);
this.tweens.begin(time); this.tweens.begin(time);
this.pool.update(time, delta);
this.updateList.update(time, delta); this.updateList.update(time, delta);
this.time.update(time, delta); this.time.update(time, delta);
this.tweens.update(time, delta); this.tweens.update(time, delta);
@ -219,6 +224,7 @@ var Systems = new Class({
this.settings.active = false; this.settings.active = false;
this.settings.visible = false; this.settings.visible = false;
this.pool.shutdown();
this.displayList.shutdown(); this.displayList.shutdown();
this.updateList.shutdown(); this.updateList.shutdown();
this.time.shutdown(); this.time.shutdown();
@ -235,6 +241,7 @@ var Systems = new Class({
{ {
// TODO // TODO
this.pool.destroy();
this.time.destroy(); this.time.destroy();
this.tweens.destroy(); this.tweens.destroy();