2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="../Game.ts" />
|
2013-05-31 14:13:03 +00:00
|
|
|
/// <reference path="../math/LinkedList.ts" />
|
2013-05-29 01:58:56 +00:00
|
|
|
/// <reference path="../gameobjects/IGameObject.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - QuadTree
|
|
|
|
*
|
|
|
|
* A fairly generic quad tree structure for rapid overlap checks. QuadTree is also configured for single or dual list operation.
|
|
|
|
* You can add items either to its A list or its B list. When you do an overlap check, you can compare the A list to itself,
|
|
|
|
* or the A list against the B list. Handy for different things!
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
module Phaser.Physics {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
export class QuadTree extends Rectangle {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new Quad Tree node.
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-20 05:21:12 +00:00
|
|
|
* @param {Number} x The X-coordinate of the point in space.
|
|
|
|
* @param {Number} y The Y-coordinate of the point in space.
|
|
|
|
* @param {Number} width Desired width of this node.
|
|
|
|
* @param {Number} height Desired height of this node.
|
|
|
|
* @param {Number} parent The parent branch or node. Pass null to create a root.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-20 05:21:12 +00:00
|
|
|
constructor(x: number, y: number, width: number, height: number, parent: QuadTree = null) {
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-20 05:21:12 +00:00
|
|
|
super(x, y, width, height);
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
this._headA = this._tailA = new LinkedList();
|
|
|
|
this._headB = this._tailB = new LinkedList();
|
|
|
|
|
|
|
|
//Copy the parent's children (if there are any)
|
2013-05-20 05:21:12 +00:00
|
|
|
if (parent != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-20 05:21:12 +00:00
|
|
|
if (parent._headA.object != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._iterator = parent._headA;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
while (this._iterator != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._tailA.object != null)
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot = this._tailA;
|
2013-04-18 13:16:18 +00:00
|
|
|
this._tailA = new LinkedList();
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot.next = this._tailA;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this._tailA.object = this._iterator.object;
|
|
|
|
this._iterator = this._iterator.next;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 05:21:12 +00:00
|
|
|
if (parent._headB.object != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._iterator = parent._headB;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
while (this._iterator != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._tailB.object != null)
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot = this._tailB;
|
2013-04-18 13:16:18 +00:00
|
|
|
this._tailB = new LinkedList();
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot.next = this._tailB;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this._tailB.object = this._iterator.object;
|
|
|
|
this._iterator = this._iterator.next;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
QuadTree._min = (this.width + this.height) / (2 * QuadTree.divisions);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._canSubdivide = (this.width > QuadTree._min) || (this.height > QuadTree._min);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//Set up comparison/sort helpers
|
|
|
|
this._northWestTree = null;
|
|
|
|
this._northEastTree = null;
|
|
|
|
this._southEastTree = null;
|
|
|
|
this._southWestTree = null;
|
|
|
|
this._leftEdge = this.x;
|
|
|
|
this._rightEdge = this.x + this.width;
|
|
|
|
this._halfWidth = this.width / 2;
|
|
|
|
this._midpointX = this._leftEdge + this._halfWidth;
|
|
|
|
this._topEdge = this.y;
|
|
|
|
this._bottomEdge = this.y + this.height;
|
|
|
|
this._halfHeight = this.height / 2;
|
|
|
|
this._midpointY = this._topEdge + this._halfHeight;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
// Reused temporary vars to help avoid gc spikes
|
|
|
|
private _iterator: LinkedList;
|
|
|
|
private _ot: LinkedList;
|
|
|
|
private _i;
|
|
|
|
private _basic;
|
|
|
|
private _members;
|
|
|
|
private _l: number;
|
|
|
|
private _overlapProcessed: bool;
|
|
|
|
private _checkObject;
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Flag for specifying that you want to add an object to the A list.
|
|
|
|
*/
|
|
|
|
public static A_LIST: number = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag for specifying that you want to add an object to the B list.
|
|
|
|
*/
|
|
|
|
public static B_LIST: number = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls the granularity of the quad tree. Default is 6 (decent performance on large and small worlds).
|
|
|
|
*/
|
|
|
|
public static divisions: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this branch of the tree can be subdivided or not.
|
|
|
|
*/
|
|
|
|
private _canSubdivide: bool;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refers to the internal A and B linked lists,
|
|
|
|
* which are used to store objects in the leaves.
|
|
|
|
*/
|
|
|
|
private _headA: LinkedList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refers to the internal A and B linked lists,
|
|
|
|
* which are used to store objects in the leaves.
|
|
|
|
*/
|
|
|
|
private _tailA: LinkedList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refers to the internal A and B linked lists,
|
|
|
|
* which are used to store objects in the leaves.
|
|
|
|
*/
|
|
|
|
private _headB: LinkedList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refers to the internal A and B linked lists,
|
|
|
|
* which are used to store objects in the leaves.
|
|
|
|
*/
|
|
|
|
private _tailB: LinkedList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private static _min: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _northWestTree: QuadTree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _northEastTree: QuadTree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _southEastTree: QuadTree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _southWestTree: QuadTree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _leftEdge: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _rightEdge: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _topEdge: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _bottomEdge: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _halfWidth: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _halfHeight: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _midpointX: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, governs and assists with the formation of the tree.
|
|
|
|
*/
|
|
|
|
private _midpointY: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, used to reduce recursive method parameters during object placement and tree formation.
|
|
|
|
*/
|
|
|
|
private static _object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, used during tree processing and overlap checks.
|
|
|
|
*/
|
|
|
|
private static _list: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, used during tree processing and overlap checks.
|
|
|
|
*/
|
|
|
|
private static _useBothLists: bool;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, used during tree processing and overlap checks.
|
|
|
|
*/
|
|
|
|
private static _processingCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, used during tree processing and overlap checks.
|
|
|
|
*/
|
|
|
|
private static _notifyCallback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal, used during tree processing and overlap checks.
|
|
|
|
*/
|
2013-05-20 05:21:12 +00:00
|
|
|
private static _callbackContext;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-20 05:21:12 +00:00
|
|
|
* Internal, used during tree processing and overlap checks.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-20 05:21:12 +00:00
|
|
|
private static _iterator: LinkedList;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up memory.
|
|
|
|
*/
|
|
|
|
public destroy() {
|
|
|
|
|
|
|
|
this._tailA.destroy();
|
|
|
|
this._tailB.destroy();
|
|
|
|
this._headA.destroy();
|
|
|
|
this._headB.destroy();
|
|
|
|
|
|
|
|
this._tailA = null;
|
|
|
|
this._tailB = null;
|
|
|
|
this._headA = null;
|
|
|
|
this._headB = null;
|
|
|
|
|
|
|
|
if (this._northWestTree != null)
|
|
|
|
{
|
|
|
|
this._northWestTree.destroy();
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._northEastTree != null)
|
|
|
|
{
|
|
|
|
this._northEastTree.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._southEastTree != null)
|
|
|
|
{
|
|
|
|
this._southEastTree.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._southWestTree != null)
|
|
|
|
{
|
|
|
|
this._southWestTree.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._northWestTree = null;
|
|
|
|
this._northEastTree = null;
|
|
|
|
this._southEastTree = null;
|
|
|
|
this._southWestTree = null;
|
|
|
|
|
|
|
|
QuadTree._object = null;
|
|
|
|
QuadTree._processingCallback = null;
|
|
|
|
QuadTree._notifyCallback = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Load objects and/or groups into the quad tree, and register notify and processing callbacks.
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-29 01:58:56 +00:00
|
|
|
* @param {} objectOrGroup1 Any object that is or extends IGameObject or Group.
|
|
|
|
* @param {} objectOrGroup2 Any object that is or extends IGameObject or Group. If null, the first parameter will be checked against itself.
|
2013-05-20 05:21:12 +00:00
|
|
|
* @param {Function} notifyCallback A function with the form <code>myFunction(Object1:GameObject,Object2:GameObject)</code> that is called whenever two objects are found to overlap in world space, and either no processCallback is specified, or the processCallback returns true.
|
|
|
|
* @param {Function} processCallback A function with the form <code>myFunction(Object1:GameObject,Object2:GameObject):bool</code> that is called whenever two objects are found to overlap in world space. The notifyCallback is only called if this function returns true. See GameObject.separate().
|
|
|
|
* @param context The context in which the callbacks will be called
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-29 01:58:56 +00:00
|
|
|
public load(objectOrGroup1, objectOrGroup2 = null, notifyCallback = null, processCallback = null, context = null) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-20 05:21:12 +00:00
|
|
|
this.add(objectOrGroup1, QuadTree.A_LIST);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-20 05:21:12 +00:00
|
|
|
if (objectOrGroup2 != null)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-20 05:21:12 +00:00
|
|
|
this.add(objectOrGroup2, QuadTree.B_LIST);
|
2013-04-18 13:16:18 +00:00
|
|
|
QuadTree._useBothLists = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QuadTree._useBothLists = false;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-20 05:21:12 +00:00
|
|
|
QuadTree._notifyCallback = notifyCallback;
|
|
|
|
QuadTree._processingCallback = processCallback;
|
|
|
|
QuadTree._callbackContext = context;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Call this function to add an object to the root of the tree.
|
|
|
|
* This function will recursively add all group members, but
|
|
|
|
* not the groups themselves.
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
2013-05-29 01:58:56 +00:00
|
|
|
* @param {} objectOrGroup GameObjects are just added, Groups are recursed and their applicable members added accordingly.
|
2013-05-20 05:21:12 +00:00
|
|
|
* @param {Number} list A <code>uint</code> flag indicating the list to which you want to add the objects. Options are <code>QuadTree.A_LIST</code> and <code>QuadTree.B_LIST</code>.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-05-29 01:58:56 +00:00
|
|
|
public add(objectOrGroup, list: number) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-20 05:21:12 +00:00
|
|
|
QuadTree._list = list;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if (objectOrGroup.type == Types.GROUP)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._i = 0;
|
|
|
|
this._members = <Group> objectOrGroup['members'];
|
|
|
|
this._l = objectOrGroup['length'];
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
while (this._i < this._l)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._basic = this._members[this._i++];
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
if (this._basic != null && this._basic.exists)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
if (this._basic.type == Phaser.Types.GROUP)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this.add(this._basic, list);
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
else
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
QuadTree._object = this._basic;
|
2013-04-18 13:16:18 +00:00
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if (QuadTree._object.exists && QuadTree._object.body.allowCollisions)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
|
|
|
this.addObject();
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
else
|
|
|
|
{
|
2013-05-20 05:21:12 +00:00
|
|
|
QuadTree._object = objectOrGroup;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if (QuadTree._object.exists && QuadTree._object.body.allowCollisions)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
|
|
|
this.addObject();
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Internal function for recursively navigating and creating the tree
|
|
|
|
* while adding objects to the appropriate nodes.
|
|
|
|
*/
|
|
|
|
private addObject() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//If this quad (not its children) lies entirely inside this object, add it here
|
2013-05-31 14:13:03 +00:00
|
|
|
if (!this._canSubdivide || ((this._leftEdge >= QuadTree._object.body.bounds.x) && (this._rightEdge <= QuadTree._object.body.bounds.right) && (this._topEdge >= QuadTree._object.body.bounds.y) && (this._bottomEdge <= QuadTree._object.body.bounds.bottom)))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this.addToList();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//See if the selected object fits completely inside any of the quadrants
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.x > this._leftEdge) && (QuadTree._object.body.bounds.right < this._midpointX))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.y > this._topEdge) && (QuadTree._object.body.bounds.bottom < this._midpointY))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._northWestTree == null)
|
|
|
|
{
|
|
|
|
this._northWestTree = new QuadTree(this._leftEdge, this._topEdge, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._northWestTree.addObject();
|
|
|
|
return;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.y > this._midpointY) && (QuadTree._object.body.bounds.bottom < this._bottomEdge))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
|
|
|
if (this._southWestTree == null)
|
|
|
|
{
|
|
|
|
this._southWestTree = new QuadTree(this._leftEdge, this._midpointY, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._southWestTree.addObject();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.x > this._midpointX) && (QuadTree._object.body.bounds.right < this._rightEdge))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.y > this._topEdge) && (QuadTree._object.body.bounds.bottom < this._midpointY))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._northEastTree == null)
|
|
|
|
{
|
|
|
|
this._northEastTree = new QuadTree(this._midpointX, this._topEdge, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._northEastTree.addObject();
|
|
|
|
return;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.y > this._midpointY) && (QuadTree._object.body.bounds.bottom < this._bottomEdge))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
|
|
|
if (this._southEastTree == null)
|
|
|
|
{
|
|
|
|
this._southEastTree = new QuadTree(this._midpointX, this._midpointY, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._southEastTree.addObject();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//If it wasn't completely contained we have to check out the partial overlaps
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.right > this._leftEdge) && (QuadTree._object.body.bounds.x < this._midpointX) && (QuadTree._object.body.bounds.bottom > this._topEdge) && (QuadTree._object.body.bounds.y < this._midpointY))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._northWestTree == null)
|
|
|
|
{
|
|
|
|
this._northWestTree = new QuadTree(this._leftEdge, this._topEdge, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._northWestTree.addObject();
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.right > this._midpointX) && (QuadTree._object.body.bounds.x < this._rightEdge) && (QuadTree._object.body.bounds.bottom > this._topEdge) && (QuadTree._object.body.bounds.y < this._midpointY))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-04-12 16:19:56 +00:00
|
|
|
if (this._northEastTree == null)
|
|
|
|
{
|
|
|
|
this._northEastTree = new QuadTree(this._midpointX, this._topEdge, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._northEastTree.addObject();
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.right > this._midpointX) && (QuadTree._object.body.bounds.x < this._rightEdge) && (QuadTree._object.body.bounds.bottom > this._midpointY) && (QuadTree._object.body.bounds.y < this._bottomEdge))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
|
|
|
if (this._southEastTree == null)
|
|
|
|
{
|
|
|
|
this._southEastTree = new QuadTree(this._midpointX, this._midpointY, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._southEastTree.addObject();
|
|
|
|
}
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object.body.bounds.right > this._leftEdge) && (QuadTree._object.body.bounds.x < this._midpointX) && (QuadTree._object.body.bounds.bottom > this._midpointY) && (QuadTree._object.body.bounds.y < this._bottomEdge))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._southWestTree == null)
|
|
|
|
{
|
|
|
|
this._southWestTree = new QuadTree(this._leftEdge, this._midpointY, this._halfWidth, this._halfHeight, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._southWestTree.addObject();
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* Internal function for recursively adding objects to leaf lists.
|
|
|
|
*/
|
|
|
|
private addToList() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (QuadTree._list == QuadTree.A_LIST)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._tailA.object != null)
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot = this._tailA;
|
2013-04-18 13:16:18 +00:00
|
|
|
this._tailA = new LinkedList();
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot.next = this._tailA;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._tailA.object = QuadTree._object;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this._tailB.object != null)
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot = this._tailB;
|
2013-04-18 13:16:18 +00:00
|
|
|
this._tailB = new LinkedList();
|
2013-05-29 01:58:56 +00:00
|
|
|
this._ot.next = this._tailB;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._tailB.object = QuadTree._object;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (!this._canSubdivide)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
return;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._northWestTree != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this._northWestTree.addToList();
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._northEastTree != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
this._northEastTree.addToList();
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._southEastTree != null)
|
|
|
|
{
|
|
|
|
this._southEastTree.addToList();
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._southWestTree != null)
|
|
|
|
{
|
|
|
|
this._southWestTree.addToList();
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
|
|
|
* <code>QuadTree</code>'s other main function. Call this after adding objects
|
|
|
|
* using <code>QuadTree.load()</code> to compare the objects that you loaded.
|
|
|
|
*
|
2013-05-11 16:26:30 +00:00
|
|
|
* @return {Boolean} Whether or not any overlaps were found.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
public execute(): bool {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = false;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (this._headA.object != null)
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._iterator = this._headA;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
while (this._iterator != null)
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
QuadTree._object = this._iterator.object;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (QuadTree._useBothLists)
|
|
|
|
{
|
|
|
|
QuadTree._iterator = this._headB;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
QuadTree._iterator = this._iterator.next;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if (QuadTree._object.exists && (QuadTree._object.body.allowCollisions > 0) && (QuadTree._iterator != null) && (QuadTree._iterator.object != null) && QuadTree._iterator.object.exists && this.overlapNode())
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this._iterator = this._iterator.next;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//Advance through the tree by calling overlap on each child
|
|
|
|
if ((this._northWestTree != null) && this._northWestTree.execute())
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = true;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if ((this._northEastTree != null) && this._northEastTree.execute())
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if ((this._southEastTree != null) && this._southEastTree.execute())
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if ((this._southWestTree != null) && this._southWestTree.execute())
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
return this._overlapProcessed;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-05-20 05:21:12 +00:00
|
|
|
* A private for comparing an object against the contents of a node.
|
2013-05-11 16:26:30 +00:00
|
|
|
*
|
|
|
|
* @return {Boolean} Whether or not any overlaps were found.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
|
|
|
private overlapNode(): bool {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
//Walk the list and check for overlaps
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = false;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
while (QuadTree._iterator != null)
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-05-31 14:13:03 +00:00
|
|
|
if (!QuadTree._object.exists || (QuadTree._object.body.allowCollisions <= 0))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
this._checkObject = QuadTree._iterator.object;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if ((QuadTree._object === this._checkObject) || !this._checkObject.exists || (this._checkObject.body.allowCollisions <= 0))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
QuadTree._iterator = QuadTree._iterator.next;
|
|
|
|
continue;
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:13:03 +00:00
|
|
|
if (QuadTree._object.body.bounds.checkHullIntersection(this._checkObject.body.bounds))
|
2013-04-12 16:19:56 +00:00
|
|
|
{
|
2013-04-18 13:16:18 +00:00
|
|
|
//Execute callback functions if they exist
|
2013-05-29 01:58:56 +00:00
|
|
|
if ((QuadTree._processingCallback == null) || QuadTree._processingCallback(QuadTree._object, this._checkObject))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
this._overlapProcessed = true;
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
if (this._overlapProcessed && (QuadTree._notifyCallback != null))
|
2013-04-18 13:16:18 +00:00
|
|
|
{
|
2013-05-20 05:21:12 +00:00
|
|
|
if (QuadTree._callbackContext !== null)
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
QuadTree._notifyCallback.call(QuadTree._callbackContext, QuadTree._object, this._checkObject);
|
2013-05-20 05:21:12 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-29 01:58:56 +00:00
|
|
|
QuadTree._notifyCallback(QuadTree._object, this._checkObject);
|
2013-05-20 05:21:12 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
QuadTree._iterator = QuadTree._iterator.next;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-05-29 01:58:56 +00:00
|
|
|
return this._overlapProcessed;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
}
|