Phaser.Group = function (game, parent, name, addToStage){ this.game = game; if (typeof parent === 'undefined' || parent === null ) { parent = game.world; } this.name = name || 'group'; PIXI.DisplayObjectContainer.call(this); if (typeof addToStage === 'undefined' || addToStage === false ) { if (parent) { parent.addChild(this); } else { this.game.stage.addChild(this); } } else { this.game.stage.addChild(this); } this.type = Phaser.GROUP; this.alive = true ; this.exists = true ; this.scale = new Phaser.Point(1, 1); _AN_Write_cursor('cursor', this, false , null ); this._cursorIndex = 0; this.cameraOffset = new Phaser.Point(); this._cache = new Int16Array([0, 0, 0, 0, 1, 0, 1, 0] ); } ; Phaser.Group.prototype = Object.create(PIXI.DisplayObjectContainer.prototype); Phaser.Group.prototype.constructor = Phaser.Group; Phaser.Group.RETURN_NONE = 0; Phaser.Group.RETURN_TOTAL = 1; Phaser.Group.RETURN_CHILD = 2; Phaser.Group.SORT_ASCENDING = -1; Phaser.Group.SORT_DESCENDING = 1; Phaser.Group.prototype.add = function (child){ if (child.parent !== this) { this.addChild(child); if (child.events) { child.events.onAddedToGroup.dispatch(child, this); } } if (this.cursor === null ) { _AN_Write_cursor('cursor', this, false , child); } return child; } ; Phaser.Group.prototype.addAt = function (child, index){ if (child.parent !== this) { this.addChildAt(child, index); if (child.events) { child.events.onAddedToGroup.dispatch(child, this); } } if (this.cursor === null ) { _AN_Write_cursor('cursor', this, false , child); } return child; } ; Phaser.Group.prototype.getAt = function (index){ return this.getChildAt(index); } ; Phaser.Group.prototype.create = function (x, y, key, frame, exists){ if (typeof exists === 'undefined') { exists = true ; } var child = new Phaser.Sprite(this.game, x, y, key, frame); child.exists = exists; child.visible = exists; child.alive = exists; this.addChild(child); if (child.events) { child.events.onAddedToGroup.dispatch(child, this); } if (this.cursor === null ) { _AN_Write_cursor('cursor', this, false , child); } return child; } ; Phaser.Group.prototype.createMultiple = function (quantity, key, frame, exists){ if (typeof exists === 'undefined') { exists = false ; } for (var i = 0; i < quantity; i++ ){ this.create(0, 0, key, frame, exists); } } ; Phaser.Group.prototype.next = function (){ if (this.cursor) { if (this._cursorIndex === _AN_Read_length('length', this.children)) { this._cursorIndex = 0; } else { this._cursorIndex++ ; } _AN_Write_cursor('cursor', this, false , this.children[this._cursorIndex]); } } ; Phaser.Group.prototype.previous = function (){ if (this.cursor) { if (this._cursorIndex === 0) { this._cursorIndex = _AN_Read_length('length', this.children) - 1; } else { this._cursorIndex-- ; } _AN_Write_cursor('cursor', this, false , this.children[this._cursorIndex]); } } ; Phaser.Group.prototype.swap = function (child1, child2){ return this.swapChildren(child1, child2); } ; Phaser.Group.prototype.bringToTop = function (child){ if (child.parent === this) { this.remove(child); this.add(child); } return child; } ; Phaser.Group.prototype.getIndex = function (child){ return this.children.indexOf(child); } ; Phaser.Group.prototype.replace = function (oldChild, newChild){ var index = this.getIndex(oldChild); if (index !== -1) { if (newChild.parent !== undefined) { newChild.events.onRemovedFromGroup.dispatch(newChild, this); newChild.parent.removeChild(newChild); } this.removeChild(oldChild); this.addChildAt(newChild, index); newChild.events.onAddedToGroup.dispatch(newChild, this); if (this.cursor === oldChild) { _AN_Write_cursor('cursor', this, false , newChild); } } } ; Phaser.Group.prototype.setProperty = function (child, key, value, operation){ operation = operation || 0; var len = _AN_Read_length('length', key); if (len == 1) { if (operation === 0) { child[key[0]] = value; } else if (operation == 1) { child[key[0]] += value; } else if (operation == 2) { child[key[0]] -= value; } else if (operation == 3) { child[key[0]] *= value; } else if (operation == 4) { child[key[0]] /= value; } } else if (len == 2) { if (operation === 0) { child[key[0]][key[1]] = value; } else if (operation == 1) { child[key[0]][key[1]] += value; } else if (operation == 2) { child[key[0]][key[1]] -= value; } else if (operation == 3) { child[key[0]][key[1]] *= value; } else if (operation == 4) { child[key[0]][key[1]] /= value; } } else if (len == 3) { if (operation === 0) { child[key[0]][key[1]][key[2]] = value; } else if (operation == 1) { child[key[0]][key[1]][key[2]] += value; } else if (operation == 2) { child[key[0]][key[1]][key[2]] -= value; } else if (operation == 3) { child[key[0]][key[1]][key[2]] *= value; } else if (operation == 4) { child[key[0]][key[1]][key[2]] /= value; } } else if (len == 4) { if (operation === 0) { child[key[0]][key[1]][key[2]][key[3]] = value; } else if (operation == 1) { child[key[0]][key[1]][key[2]][key[3]] += value; } else if (operation == 2) { child[key[0]][key[1]][key[2]][key[3]] -= value; } else if (operation == 3) { child[key[0]][key[1]][key[2]][key[3]] *= value; } else if (operation == 4) { child[key[0]][key[1]][key[2]][key[3]] /= value; } } } ; Phaser.Group.prototype.set = function (child, key, value, checkAlive, checkVisible, operation){ key = key.split('.'); if (typeof checkAlive === 'undefined') { checkAlive = false ; } if (typeof checkVisible === 'undefined') { checkVisible = false ; } if ((checkAlive === false || (checkAlive && child.alive)) && (checkVisible === false || (checkVisible && child.visible))) { this.setProperty(child, key, value, operation); } } ; Phaser.Group.prototype.setAll = function (key, value, checkAlive, checkVisible, operation){ key = key.split('.'); if (typeof checkAlive === 'undefined') { checkAlive = false ; } if (typeof checkVisible === 'undefined') { checkVisible = false ; } operation = operation || 0; for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ if ((!checkAlive || (checkAlive && this.children[i].alive)) && (!checkVisible || (checkVisible && this.children[i].visible))) { this.setProperty(this.children[i], key, value, operation); } } } ; Phaser.Group.prototype.addAll = function (property, amount, checkAlive, checkVisible){ this.setAll(property, amount, checkAlive, checkVisible, 1); } ; Phaser.Group.prototype.subAll = function (property, amount, checkAlive, checkVisible){ this.setAll(property, amount, checkAlive, checkVisible, 2); } ; Phaser.Group.prototype.multiplyAll = function (property, amount, checkAlive, checkVisible){ this.setAll(property, amount, checkAlive, checkVisible, 3); } ; Phaser.Group.prototype.divideAll = function (property, amount, checkAlive, checkVisible){ this.setAll(property, amount, checkAlive, checkVisible, 4); } ; Phaser.Group.prototype.callAllExists = function (callback, existsValue){ var args = Array.prototype.splice.call(arguments, 2); for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ if (this.children[i].exists === existsValue && this.children[i][callback]) { this.children[i][callback].apply(this.children[i], args); } } } ; Phaser.Group.prototype.callbackFromArray = function (child, callback, length){ if (length == 1) { if (child[callback[0]]) { return child[callback[0]]; } } else if (length == 2) { if (child[callback[0]][callback[1]]) { return child[callback[0]][callback[1]]; } } else if (length == 3) { if (child[callback[0]][callback[1]][callback[2]]) { return child[callback[0]][callback[1]][callback[2]]; } } else if (length == 4) { if (child[callback[0]][callback[1]][callback[2]][callback[3]]) { return child[callback[0]][callback[1]][callback[2]][callback[3]]; } } else { if (child[callback]) { return child[callback]; } } return false ; } ; Phaser.Group.prototype.callAll = function (method, context){ if (typeof method === 'undefined') { return ; } method = method.split('.'); var methodLength = _AN_Read_length('length', method); if (typeof context === 'undefined') { context = null ; } else { if (typeof context === 'string') { context = context.split('.'); var contextLength = _AN_Read_length('length', context); } } var args = Array.prototype.splice.call(arguments, 2); var callback = null ; var callbackContext = null ; for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ callback = this.callbackFromArray(this.children[i], method, methodLength); if (context && callback) { callbackContext = this.callbackFromArray(this.children[i], context, contextLength); if (callback) { callback.apply(callbackContext, args); } } else if (callback) { callback.apply(this.children[i], args); } } } ; Phaser.Group.prototype.preUpdate = function (){ if (!this.exists || !this.parent.exists) { this.renderOrderID = -1; return false ; } var i = _AN_Read_length('length', this.children); while (i-- ){ this.children[i].preUpdate(); } return true ; } ; Phaser.Group.prototype.update = function (){ var i = _AN_Read_length('length', this.children); while (i-- ){ this.children[i].update(); } } ; Phaser.Group.prototype.postUpdate = function (){ if (this._cache[7] === 1) { this.x = this.game.camera.view.x + this.cameraOffset.x; this.y = this.game.camera.view.y + this.cameraOffset.y; } var i = _AN_Read_length('length', this.children); while (i-- ){ this.children[i].postUpdate(); } } ; Phaser.Group.prototype.forEach = function (callback, callbackContext, checkExists){ if (typeof checkExists === 'undefined') { checkExists = false ; } var args = Array.prototype.splice.call(arguments, 3); args.unshift(null ); for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ if (!checkExists || (checkExists && this.children[i].exists)) { args[0] = this.children[i]; callback.apply(callbackContext, args); } } } ; Phaser.Group.prototype.forEachExists = function (callback, callbackContext){ var args = Array.prototype.splice.call(arguments, 2); args.unshift(null ); this.iterate('exists', true , Phaser.Group.RETURN_TOTAL, callback, callbackContext, args); } ; Phaser.Group.prototype.forEachAlive = function (callback, callbackContext){ var args = Array.prototype.splice.call(arguments, 2); args.unshift(null ); this.iterate('alive', true , Phaser.Group.RETURN_TOTAL, callback, callbackContext, args); } ; Phaser.Group.prototype.forEachDead = function (callback, callbackContext){ var args = Array.prototype.splice.call(arguments, 2); args.unshift(null ); this.iterate('alive', false , Phaser.Group.RETURN_TOTAL, callback, callbackContext, args); } ; Phaser.Group.prototype.sort = function (index, order){ if (typeof index === 'undefined') { index = 'y'; } if (typeof order === 'undefined') { order = Phaser.Group.SORT_ASCENDING; } } ; Phaser.Group.prototype.sortHandler = function (a, b){ } ; Phaser.Group.prototype.iterate = function (key, value, returnType, callback, callbackContext, args){ if (returnType === Phaser.Group.RETURN_TOTAL && _AN_Read_length('length', this.children) === 0) { return 0; } if (typeof callback === 'undefined') { callback = false ; } var total = 0; for (var i = 0, len = _AN_Read_length('length', this.children); i < len; i++ ){ if (this.children[i][key] === value) { total++ ; if (callback) { args[0] = this.children[i]; callback.apply(callbackContext, args); } if (returnType === Phaser.Group.RETURN_CHILD) { return this.children[i]; } } } if (returnType === Phaser.Group.RETURN_TOTAL) { return total; } else if (returnType === Phaser.Group.RETURN_CHILD) { return null ; } } ; Phaser.Group.prototype.getFirstExists = function (state){ if (typeof state !== 'boolean') { state = true ; } return this.iterate('exists', state, Phaser.Group.RETURN_CHILD); } ; Phaser.Group.prototype.getFirstAlive = function (){ return this.iterate('alive', true , Phaser.Group.RETURN_CHILD); } ; Phaser.Group.prototype.getFirstDead = function (){ return this.iterate('alive', false , Phaser.Group.RETURN_CHILD); } ; Phaser.Group.prototype.countLiving = function (){ return this.iterate('alive', true , Phaser.Group.RETURN_TOTAL); } ; Phaser.Group.prototype.countDead = function (){ return this.iterate('alive', false , Phaser.Group.RETURN_TOTAL); } ; Phaser.Group.prototype.getRandom = function (startIndex, length){ if (_AN_Read_length('length', this.children) === 0) { return null ; } startIndex = startIndex || 0; length = length || _AN_Read_length('length', this.children); return this.game.math.getRandom(this.children, startIndex, length); } ; Phaser.Group.prototype.remove = function (child){ if (_AN_Read_length('length', this.children) === 0) { return ; } if (child.events) { child.events.onRemovedFromGroup.dispatch(child, this); } this.removeChild(child); if (this.cursor === child) { this.next(); } return true ; } ; Phaser.Group.prototype.removeAll = function (){ if (_AN_Read_length('length', this.children) === 0) { return ; } do { if (this.children[0].events) { this.children[0].events.onRemovedFromGroup.dispatch(this.children[0], this); } this.removeChild(this.children[0]); } while(_AN_Read_length('length', this.children) > 0)_AN_Write_cursor('cursor', this, false , null ); } ; Phaser.Group.prototype.removeBetween = function (startIndex, endIndex){ if (_AN_Read_length('length', this.children) === 0) { return ; } if (startIndex > endIndex || startIndex < 0 || endIndex > _AN_Read_length('length', this.children)) { return false ; } for (var i = startIndex; i < endIndex; i++ ){ if (this.children[i].events) { this.children[i].events.onRemovedFromGroup.dispatch(this.children[i], this); } this.removeChild(this.children[i]); if (this.cursor === child) { _AN_Write_cursor('cursor', this, false , null ); } } } ; Phaser.Group.prototype.destroy = function (destroyChildren){ if (typeof destroyChildren === 'undefined') { destroyChildren = false ; } if (destroyChildren) { if (_AN_Read_length('length', this.children) > 0) { do { if (this.children[0].group) { this.children[0].destroy(); } } while(_AN_Read_length('length', this.children) > 0)} } else { this.removeAll(); } this.parent.removeChild(this); this.game = null ; this.exists = false ; _AN_Write_cursor('cursor', this, false , null ); } ; Object.defineProperty(Phaser.Group.prototype, "total", { get: function (){ return this.iterate('exists', true , Phaser.Group.RETURN_TOTAL); } } ); Object.defineProperty(Phaser.Group.prototype, "length", { get: function (){ return _AN_Read_length("length", this.children); } } ); Object.defineProperty(Phaser.Group.prototype, "angle", { get: function (){ return Phaser.Math.radToDeg(this.rotation); } , set: function (value){ this.rotation = Phaser.Math.degToRad(value); } } ); Object.defineProperty(Phaser.Group.prototype, "fixedToCamera", { get: function (){ return !!this._cache[7]; } , set: function (value){ if (value) { this._cache[7] = 1; this.cameraOffset.set(this.x, this.y); } else { this._cache[7] = 0; } } } );