Skip to content

Commit b494ace

Browse files
committed
Added Pool Manager State plugin and start of ObjectPool class.
1 parent 9f4db03 commit b494ace

8 files changed

Lines changed: 235 additions & 101 deletions

File tree

v3/src/gameobjects/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
Group: require('./group/Group'),
1313
Image: require('./image/Image'),
1414
Mesh: require('./mesh/Mesh'),
15+
ObjectPool: require('./pool/ObjectPool.js'),
1516
Quad: require('./quad/Quad'),
1617
RenderPass: require('./renderpass/RenderPass.js'),
1718
Sprite: require('./sprite/Sprite'),
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Phaser.GameObjects.ObjectPool
2+
3+
var Class = require('../../utils/Class');
4+
var Sprite = require('../sprite/Sprite');
5+
6+
// An Object Pool
7+
8+
var ObjectPool = new Class({
9+
10+
initialize:
11+
12+
function ObjectPool (config)
13+
{
14+
this.processing = false;
15+
16+
this._live = [];
17+
this._pendingInsertion = [];
18+
this._pendingRemoval = [];
19+
this._dead = [];
20+
21+
this.classType = Sprite;
22+
23+
if (config)
24+
{
25+
this.createMultiple(config);
26+
}
27+
},
28+
29+
// Allow them to add a Group too
30+
add: function (child)
31+
{
32+
if (Array.isArray(child))
33+
{
34+
for (var i = 0; i < child.length; i++)
35+
{
36+
this.pendingInsertion.push(child[i]);
37+
}
38+
}
39+
else
40+
{
41+
this.pendingInsertion.push(child);
42+
}
43+
44+
return this;
45+
},
46+
47+
get: function ()
48+
{
49+
50+
},
51+
52+
// getByName: function ()
53+
// {
54+
55+
// },
56+
57+
getByName: function ()
58+
{
59+
60+
},
61+
62+
// Moves from live to pendingRemoval
63+
// is there a reason why it can't do direct to dead?
64+
kill: function ()
65+
{
66+
67+
},
68+
69+
killAndHide: function (gameObject)
70+
{
71+
gameObject.visible = false;
72+
},
73+
74+
createMultiple: function ()
75+
{
76+
},
77+
78+
update: function (time, delta)
79+
{
80+
this.processing = true;
81+
82+
83+
this.processing = false;
84+
},
85+
86+
destroy: function ()
87+
{
88+
89+
}
90+
91+
});
92+
93+
module.exports = ObjectPool;

v3/src/gameobjects/pool/Pool.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

v3/src/gameobjects/pool/PoolCreator.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

v3/src/gameobjects/pool/PoolFactory.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

v3/src/plugins/PoolManager.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
var Class = require('../utils/Class');
2+
var ObjectPool = require('../gameobjects/pool/ObjectPool');
3+
4+
var PoolManager = new Class({
5+
6+
initialize:
7+
8+
function PoolManager (state)
9+
{
10+
this.state = state;
11+
12+
this._active = [];
13+
this._pendingInsertion = [];
14+
this._pendingRemoval = [];
15+
16+
this.processing = false;
17+
},
18+
19+
add: function (pool)
20+
{
21+
if (this.processing)
22+
{
23+
this._pendingInsertion.push(pool);
24+
}
25+
else
26+
{
27+
this._active.push(pool);
28+
}
29+
30+
return this;
31+
},
32+
33+
create: function ()
34+
{
35+
var pool = new ObjectPool();
36+
37+
this.add(pool);
38+
39+
return pool;
40+
},
41+
42+
begin: function ()
43+
{
44+
var toRemove = this._pendingRemoval.length;
45+
var toInsert = this._pendingInsertion.length;
46+
47+
if (toRemove === 0 && toInsert === 0)
48+
{
49+
// Quick bail
50+
return;
51+
}
52+
53+
var i;
54+
var pool;
55+
56+
// Delete old pools
57+
for (i = 0; i < toRemove; i++)
58+
{
59+
pool = this._pendingRemoval[i];
60+
61+
var index = this._active.indexOf(pool);
62+
63+
if (index > -1)
64+
{
65+
this._active.splice(index, 1);
66+
}
67+
68+
// Pool them?
69+
// pool.destroy();
70+
}
71+
72+
// Move pending to active
73+
this._active = this._active.concat(this._pendingInsertion.splice(0));
74+
75+
// Clear the lists
76+
this._pendingRemoval.length = 0;
77+
this._pendingInsertion.length = 0;
78+
},
79+
80+
update: function (time, delta)
81+
{
82+
this.processing = true;
83+
84+
for (var i = 0; i < this._active.length; i++)
85+
{
86+
var pool = this._active[i];
87+
88+
pool.update.call(pool, time, delta);
89+
}
90+
91+
this.processing = false;
92+
},
93+
94+
// State that owns this Pool is shutting down
95+
shutdown: function ()
96+
{
97+
var i;
98+
99+
for (i = 0; i < this._pendingInsertion.length; i++)
100+
{
101+
this._pendingInsertion[i].destroy();
102+
}
103+
104+
for (i = 0; i < this._active.length; i++)
105+
{
106+
this._active[i].destroy();
107+
}
108+
109+
for (i = 0; i < this._pendingRemoval.length; i++)
110+
{
111+
this._pendingRemoval[i].destroy();
112+
}
113+
114+
this._active.length = 0;
115+
this._pendingRemoval.length = 0;
116+
this._pendingInsertion.length = 0;
117+
},
118+
119+
// Game level nuke
120+
destroy: function ()
121+
{
122+
this.shutdown();
123+
124+
this.state = undefined;
125+
}
126+
127+
});
128+
129+
module.exports = PoolManager;

v3/src/state/InjectionMap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ var InjectionMap = {
1414

1515
add: 'add',
1616
cameras: 'cameras',
17+
data: 'data',
18+
displayList: 'children',
1719
events: 'events',
1820
load: 'load',
1921
make: 'make',
22+
pool: 'pool',
2023
stateManager: 'state',
2124
time: 'time',
22-
tweens: 'tweens',
23-
24-
displayList: 'children',
25-
data: 'data'
25+
tweens: 'tweens'
2626

2727
};
2828

0 commit comments

Comments
 (0)