forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoolManager.js
More file actions
140 lines (104 loc) · 2.84 KB
/
Copy pathPoolManager.js
File metadata and controls
140 lines (104 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var Class = require('../utils/Class');
var ObjectPool = require('../gameobjects/pool/ObjectPool');
var SpritePool = require('../gameobjects/pool/SpritePool');
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;
},
createSpritePool: function (maxSize, key, frame)
{
var pool = new SpritePool(this, maxSize, key, frame);
this.add(pool);
return pool;
},
createObjectPool: function (classType, maxSize)
{
if (maxSize === undefined) { maxSize = -1; }
var pool = new ObjectPool(this, classType, maxSize);
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.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;