|
1 | 1 | // Phaser.GameObjects.ObjectPool |
2 | 2 |
|
3 | 3 | var Class = require('../../utils/Class'); |
4 | | -var Sprite = require('../sprite/Sprite'); |
5 | 4 |
|
6 | 5 | // An Object Pool |
7 | 6 |
|
8 | 7 | var ObjectPool = new Class({ |
9 | 8 |
|
10 | 9 | initialize: |
11 | 10 |
|
12 | | - function ObjectPool (config) |
| 11 | + function ObjectPool (manager, classType, maxSize, createCallback, callbackScope) |
13 | 12 | { |
14 | | - this.processing = false; |
| 13 | + if (maxSize === undefined) { maxSize = -1; } |
| 14 | + if (createCallback === undefined) { createCallback = this.makeGameObject; } |
| 15 | + if (callbackScope === undefined) { callbackScope = this; } |
15 | 16 |
|
16 | | - this._live = []; |
17 | | - this._pendingInsertion = []; |
18 | | - this._pendingRemoval = []; |
19 | | - this._dead = []; |
| 17 | + this.manager = manager; |
| 18 | + this.state = manager.state; |
20 | 19 |
|
21 | | - this.classType = Sprite; |
| 20 | + this.displayList = this.state.sys.displayList; |
| 21 | + this.updateList = this.state.sys.updateList; |
22 | 22 |
|
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 = []; |
27 | 31 | }, |
28 | 32 |
|
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 |
30 | 46 | add: function (child) |
31 | 47 | { |
| 48 | + var children; |
| 49 | + |
32 | 50 | if (Array.isArray(child)) |
33 | 51 | { |
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) |
35 | 70 | { |
36 | | - this.pendingInsertion.push(child[i]); |
| 71 | + len = free; |
37 | 72 | } |
38 | 73 | } |
39 | | - else |
| 74 | + |
| 75 | + for (var i = 0; i < len; i++) |
40 | 76 | { |
41 | | - this.pendingInsertion.push(child); |
| 77 | + this._list.push(children[i]); |
42 | 78 | } |
43 | 79 |
|
44 | 80 | return this; |
45 | 81 | }, |
46 | 82 |
|
47 | | - get: function () |
| 83 | + // Create X new GameObjects in this Pool if there is capacity to do so |
| 84 | + create: function (quantity) |
48 | 85 | { |
| 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 | + } |
49 | 93 |
|
| 94 | + return this; |
50 | 95 | }, |
51 | 96 |
|
52 | | - // getByName: function () |
53 | | - // { |
54 | | - |
55 | | - // }, |
| 97 | + // Proxy method for sub-classes to override |
| 98 | + get: function () |
| 99 | + { |
| 100 | + return this.getFreeGameObject(); |
| 101 | + }, |
56 | 102 |
|
57 | | - getByName: function () |
| 103 | + getFreeGameObject: function () |
58 | 104 | { |
| 105 | + var gameObject; |
| 106 | + |
| 107 | + for (var i = 0; i < this._list.length; i++) |
| 108 | + { |
| 109 | + gameObject = this._list[i]; |
59 | 110 |
|
| 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; |
60 | 133 | }, |
61 | 134 |
|
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) |
65 | 136 | { |
66 | | - |
| 137 | + if (this._list.indexOf(gameObject) > -1) |
| 138 | + { |
| 139 | + gameObject.setActive(false); |
| 140 | + } |
67 | 141 | }, |
68 | 142 |
|
69 | 143 | killAndHide: function (gameObject) |
70 | 144 | { |
71 | | - gameObject.visible = false; |
| 145 | + if (this._list.indexOf(gameObject) > -1) |
| 146 | + { |
| 147 | + gameObject.setActive(false); |
| 148 | + gameObject.setVisible(false); |
| 149 | + } |
72 | 150 | }, |
73 | 151 |
|
74 | | - createMultiple: function () |
| 152 | + isFull: function () |
75 | 153 | { |
| 154 | + if (this.maxSize === -1) |
| 155 | + { |
| 156 | + return false; |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + return (this._list.length === this.maxSize); |
| 161 | + } |
76 | 162 | }, |
77 | 163 |
|
78 | 164 | update: function (time, delta) |
79 | 165 | { |
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; |
81 | 180 |
|
| 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; |
82 | 196 |
|
83 | | - this.processing = false; |
| 197 | + return (capacity - used); |
84 | 198 | }, |
85 | 199 |
|
86 | 200 | destroy: function () |
|
0 commit comments