Skip to content

Commit 1b70df7

Browse files
committed
Lots of Pool updates and extensions. The Pool will now update children automatically, making them far more useful.
1 parent 8bc485e commit 1b70df7

3 files changed

Lines changed: 208 additions & 35 deletions

File tree

v3/src/gameobjects/pool/ObjectPool.js

Lines changed: 145 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,200 @@
11
// Phaser.GameObjects.ObjectPool
22

33
var Class = require('../../utils/Class');
4-
var Sprite = require('../sprite/Sprite');
54

65
// An Object Pool
76

87
var ObjectPool = new Class({
98

109
initialize:
1110

12-
function ObjectPool (config)
11+
function ObjectPool (manager, classType, maxSize, createCallback, callbackScope)
1312
{
14-
this.processing = false;
13+
if (maxSize === undefined) { maxSize = -1; }
14+
if (createCallback === undefined) { createCallback = this.makeGameObject; }
15+
if (callbackScope === undefined) { callbackScope = this; }
1516

16-
this._live = [];
17-
this._pendingInsertion = [];
18-
this._pendingRemoval = [];
19-
this._dead = [];
17+
this.manager = manager;
18+
this.state = manager.state;
2019

21-
this.classType = Sprite;
20+
this.displayList = this.state.sys.displayList;
21+
this.updateList = this.state.sys.updateList;
2222

23-
if (config)
24-
{
25-
this.createMultiple(config);
26-
}
23+
this.createCallback = createCallback;
24+
this.callbackScope = callbackScope;
25+
26+
this.maxSize = maxSize;
27+
28+
this.classType = classType;
29+
30+
this._list = [];
2731
},
2832

29-
// Allow them to add a Group too
33+
makeGameObject: function ()
34+
{
35+
var gameObject = new this.classType(this.state);
36+
37+
this.displayList.add(gameObject);
38+
39+
gameObject.setActive(false);
40+
gameObject.setVisible(false);
41+
42+
return gameObject;
43+
},
44+
45+
// Add an existing GameObject, or Array or Group of GameObjects into this Pool
3046
add: function (child)
3147
{
48+
var children;
49+
3250
if (Array.isArray(child))
3351
{
34-
for (var i = 0; i < child.length; i++)
52+
children = child;
53+
}
54+
else if (child.hasOwnProperty('children'))
55+
{
56+
children = child.children.getArray();
57+
}
58+
else
59+
{
60+
children = [ child ];
61+
}
62+
63+
var len = children.length;
64+
65+
if (this.maxSize > -1)
66+
{
67+
var free = this.maxSize - this._list.length;
68+
69+
if (len > free)
3570
{
36-
this.pendingInsertion.push(child[i]);
71+
len = free;
3772
}
3873
}
39-
else
74+
75+
for (var i = 0; i < len; i++)
4076
{
41-
this.pendingInsertion.push(child);
77+
this._list.push(children[i]);
4278
}
4379

4480
return this;
4581
},
4682

47-
get: function ()
83+
// Create X new GameObjects in this Pool if there is capacity to do so
84+
create: function (quantity)
4885
{
86+
for (var i = 0; i < quantity; i++)
87+
{
88+
if (!this.isFull())
89+
{
90+
this._list.push(this.createCallback.call(this.callbackScope));
91+
}
92+
}
4993

94+
return this;
5095
},
5196

52-
// getByName: function ()
53-
// {
54-
55-
// },
97+
// Proxy method for sub-classes to override
98+
get: function ()
99+
{
100+
return this.getFreeGameObject();
101+
},
56102

57-
getByName: function ()
103+
getFreeGameObject: function ()
58104
{
105+
var gameObject;
106+
107+
for (var i = 0; i < this._list.length; i++)
108+
{
109+
gameObject = this._list[i];
59110

111+
if (!gameObject.active)
112+
{
113+
gameObject.setActive(true);
114+
gameObject.setVisible(true);
115+
116+
return gameObject;
117+
}
118+
}
119+
120+
if (!this.isFull())
121+
{
122+
gameObject = this.createCallback.call(this.callbackScope);
123+
124+
gameObject.setActive(true);
125+
gameObject.setVisible(true);
126+
127+
this._list.push(gameObject);
128+
129+
return gameObject;
130+
}
131+
132+
return null;
60133
},
61134

62-
// Moves from live to pendingRemoval
63-
// is there a reason why it can't do direct to dead?
64-
kill: function ()
135+
kill: function (gameObject)
65136
{
66-
137+
if (this._list.indexOf(gameObject) > -1)
138+
{
139+
gameObject.setActive(false);
140+
}
67141
},
68142

69143
killAndHide: function (gameObject)
70144
{
71-
gameObject.visible = false;
145+
if (this._list.indexOf(gameObject) > -1)
146+
{
147+
gameObject.setActive(false);
148+
gameObject.setVisible(false);
149+
}
72150
},
73151

74-
createMultiple: function ()
152+
isFull: function ()
75153
{
154+
if (this.maxSize === -1)
155+
{
156+
return false;
157+
}
158+
else
159+
{
160+
return (this._list.length === this.maxSize);
161+
}
76162
},
77163

78164
update: function (time, delta)
79165
{
80-
this.processing = true;
166+
for (var i = 0; i < this._list.length; i++)
167+
{
168+
var gameObject = this._list[i];
169+
170+
if (gameObject.active)
171+
{
172+
gameObject.update(time, delta);
173+
}
174+
}
175+
},
176+
177+
getTotalUsed: function ()
178+
{
179+
var total = 0;
81180

181+
for (var i = 0; i < this._list.length; i++)
182+
{
183+
if (this._list[i].active)
184+
{
185+
total++;
186+
}
187+
}
188+
189+
return total;
190+
},
191+
192+
getTotalFree: function ()
193+
{
194+
var used = this.getTotalUsed();
195+
var capacity = (this.maxSize === -1) ? 999999999999 : this.maxSize;
82196

83-
this.processing = false;
197+
return (capacity - used);
84198
},
85199

86200
destroy: function ()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Phaser.GameObjects.SpritePool
2+
3+
var Class = require('../../utils/Class');
4+
var Sprite = require('../sprite/Sprite');
5+
var ObjectPool = require('./ObjectPool');
6+
7+
// An Object Pool
8+
9+
var SpritePool = new Class({
10+
11+
Extends: ObjectPool,
12+
13+
initialize:
14+
15+
function SpritePool (manager, maxSize, quantity, key, frame)
16+
{
17+
ObjectPool.call(this, manager, Sprite, maxSize, this.makeSprite, this);
18+
19+
this.defaultKey = key;
20+
21+
this.defaultFrame = frame;
22+
},
23+
24+
makeSprite: function ()
25+
{
26+
var gameObject = new this.classType(this.state, 0, 0, this.defaultKey, this.defaultFrame);
27+
28+
this.displayList.add(gameObject);
29+
this.updateList.add(gameObject);
30+
31+
gameObject.setActive(false);
32+
gameObject.setVisible(false);
33+
34+
return gameObject;
35+
},
36+
37+
get: function (x, y)
38+
{
39+
var gameObject = this.getFreeGameObject();
40+
41+
gameObject.setPosition(x, y);
42+
43+
return gameObject;
44+
}
45+
46+
});
47+
48+
module.exports = SpritePool;

v3/src/plugins/PoolManager.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Class = require('../utils/Class');
22
var ObjectPool = require('../gameobjects/pool/ObjectPool');
3+
var SpritePool = require('../gameobjects/pool/SpritePool');
34

45
var PoolManager = new Class({
56

@@ -30,9 +31,20 @@ var PoolManager = new Class({
3031
return this;
3132
},
3233

33-
create: function ()
34+
createSpritePool: function (maxSize, key, frame)
3435
{
35-
var pool = new ObjectPool();
36+
var pool = new SpritePool(this, maxSize, key, frame);
37+
38+
this.add(pool);
39+
40+
return pool;
41+
},
42+
43+
createObjectPool: function (classType, maxSize)
44+
{
45+
if (maxSize === undefined) { maxSize = -1; }
46+
47+
var pool = new ObjectPool(this, classType, maxSize);
3648

3749
this.add(pool);
3850

@@ -65,8 +77,7 @@ var PoolManager = new Class({
6577
this._active.splice(index, 1);
6678
}
6779

68-
// Pool them?
69-
// pool.destroy();
80+
pool.destroy();
7081
}
7182

7283
// Move pending to active

0 commit comments

Comments
 (0)