Phaser.QuadTree = 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 = [] ; this.nodes = [] ; } ; Phaser.QuadTree.prototype = { populate: function (group){ group.forEach(this.populateHandler, this, true ); } , populateHandler: function (sprite){ if (sprite.body && sprite.body.checkCollision.none === false && sprite.alive) { this.insert(sprite.body); } } , split: function (){ this.level++ ; this.nodes[0] = new Phaser.QuadTree(this.bounds.right, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level); this.nodes[1] = new Phaser.QuadTree(this.bounds.x, this.bounds.y, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level); this.nodes[2] = new Phaser.QuadTree(this.bounds.x, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level); this.nodes[3] = new Phaser.QuadTree(this.bounds.right, this.bounds.bottom, this.bounds.subWidth, this.bounds.subHeight, this.maxObjects, this.maxLevels, this.level); } , 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 (sprite){ var returnObjects = this.objects; sprite.body.quadTreeIndex = this.getIndex(sprite.body); if (this.nodes[0]) { if (sprite.body.quadTreeIndex !== -1) { returnObjects = returnObjects.concat(this.nodes[sprite.body.quadTreeIndex].retrieve(sprite)); } else { returnObjects = returnObjects.concat(this.nodes[0].retrieve(sprite)); returnObjects = returnObjects.concat(this.nodes[1].retrieve(sprite)); returnObjects = returnObjects.concat(this.nodes[2].retrieve(sprite)); returnObjects = returnObjects.concat(this.nodes[3].retrieve(sprite)); } } return returnObjects; } , clear: function (){ this.objects = [] ; for (var i = 0, len = _AN_Read_length('length', this.nodes); i < len; i++ ){ if (this.nodes[i]) { _AN_Call_clear('clear', this.nodes[i]); delete this.nodes[i]; } } } } ; Phaser.QuadTree.prototype.constructor = Phaser.QuadTree;