Phaser.QuadTree = function (x, y, width, height, maxObjects, maxLevels, level){ this.maxObjects = 10; this.maxLevels = 4; this.level = 0; this.bounds = { } ; this.objects = [] ; this.nodes = [] ; this._empty = [] ; this.reset(x, y, width, height, maxObjects, maxLevels, level); } ; Phaser.QuadTree.prototype = { reset: function (x, y, width, height, maxObjects, maxLevels, level){ this.maxObjects = maxObjects || 10; this.maxLevels = maxLevels || 4; this.level = level || 0; this.bounds = { x: Math.round(x), y: Math.round(y), width: width, height: height, subWidth: Math.floor(width / 2), subHeight: Math.floor(height / 2), right: Math.round(x) + Math.floor(width / 2), bottom: Math.round(y) + Math.floor(height / 2)} ; this.objects.length = 0; this.nodes.length = 0; } , populate: function (group){ group.forEach(this.populateHandler, this, true ); } , populateHandler: function (sprite){ if (sprite.body && sprite.exists) { this.insert(sprite.body); } } , split: function (){ this.nodes[0] = new Phaser.QuadTree(this.bounds.right, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, (this.level + 1)); this.nodes[1] = new Phaser.QuadTree(this.bounds.x, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, (this.level + 1)); this.nodes[2] = new Phaser.QuadTree(this.bounds.x, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, (this.level + 1)); this.nodes[3] = new Phaser.QuadTree(this.bounds.right, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, (this.level + 1)); } , insert: function (body){ var i = 0; var index; if (this.nodes[0] != null ) { index = this.getIndex(body); if (index !== -1) { this.nodes[index].insert(body); return ; } } this.objects.push(body); if (_AN_Read_length('length', this.objects) > this.maxObjects && this.level < this.maxLevels) { if (this.nodes[0] == null ) { this.split(); } while (i < _AN_Read_length('length', this.objects)){ index = this.getIndex(this.objects[i]); if (index !== -1) { this.nodes[index].insert(this.objects.splice(i, 1)[0]); } else { i++ ; } } } } , getIndex: function (rect){ var index = -1; if (rect.x < this.bounds.right && rect.right < this.bounds.right) { if (rect.y < this.bounds.bottom && rect.bottom < this.bounds.bottom) { index = 1; } else if (rect.y > this.bounds.bottom) { index = 2; } } else if (rect.x > this.bounds.right) { if (rect.y < this.bounds.bottom && rect.bottom < this.bounds.bottom) { index = 0; } else if (rect.y > this.bounds.bottom) { index = 3; } } return index; } , retrieve: function (source){ if (source instanceof Phaser.Rectangle) { var returnObjects = this.objects; var index = this.getIndex(source); } else { if (!source.body) { return this._empty; } var returnObjects = this.objects; var index = this.getIndex(source.body); } if (this.nodes[0]) { if (index !== -1) { returnObjects = returnObjects.concat(this.nodes[index].retrieve(source)); } else { returnObjects = returnObjects.concat(this.nodes[0].retrieve(source)); returnObjects = returnObjects.concat(this.nodes[1].retrieve(source)); returnObjects = returnObjects.concat(this.nodes[2].retrieve(source)); returnObjects = returnObjects.concat(this.nodes[3].retrieve(source)); } } return returnObjects; } , clear: function (){ this.objects.length = 0; var i = _AN_Read_length('length', this.nodes); while (i-- ){ _AN_Call_clear('clear', this.nodes[i]); this.nodes.splice(i, 1); } this.nodes.length = 0; } } ; Phaser.QuadTree.prototype.constructor = Phaser.QuadTree;