* Pull a new instance of the requested object (if added into the object pool) * @param {string} name - as used in pool.register * @param {...*} [args] - arguments to be passed when instantiating/reinitializing the object * @returns {object} the instance of the requested object * @exa
(name, ...args)
| 123 | * app.world.removeChild(bullet); |
| 124 | */ |
| 125 | pull(name, ...args) { |
| 126 | const className = this.objectClass[name]; |
| 127 | if (className) { |
| 128 | const proto = className["class"]; |
| 129 | const poolArray = className.pool; |
| 130 | let obj; |
| 131 | |
| 132 | if (poolArray && (obj = poolArray.pop())) { |
| 133 | // poolable object must implement a `onResetEvent` method |
| 134 | obj.onResetEvent.apply(obj, args); |
| 135 | this.instance_counter--; |
| 136 | } else { |
| 137 | // create a new instance |
| 138 | obj = new (proto.bind.apply(proto, [proto, ...args]))(); |
| 139 | if (poolArray) { |
| 140 | obj.className = name; |
| 141 | } |
| 142 | } |
| 143 | return obj; |
| 144 | } |
| 145 | throw new Error("Cannot instantiate object of type '" + name + "'"); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * purge the object pool from any inactive object <br> |