phaser-ce
Version:
Phaser CE (Community Edition) is a fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.
1,206 lines (1,034 loc) • 95.7 kB
JavaScript
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Arcade Physics world. Contains Arcade Physics related collision, overlap and motion methods.
*
* @class Phaser.Physics.Arcade
* @constructor
* @param {Phaser.Game} game - reference to the current game instance.
*/
Phaser.Physics.Arcade = function (game) {
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = game;
/**
* @property {Phaser.Point} gravity - The World gravity setting. Defaults to x: 0, y: 0, or no gravity.
*/
this.gravity = new Phaser.Point();
/**
* @property {Phaser.Rectangle} bounds - The bounds inside of which the physics world exists. Defaults to match the world bounds.
*/
this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
/**
* Which edges of the World bounds Bodies can collide against when `collideWorldBounds` is `true`.
* For example checkCollision.down = false means Bodies cannot collide with the World.bounds.bottom.
* @property {object} checkCollision - An object containing allowed collision flags (up, down, left, right).
*/
this.checkCollision = { up: true, down: true, left: true, right: true };
/**
* @property {number} maxObjects - Used by the QuadTree to set the maximum number of objects per quad.
*/
this.maxObjects = 10;
/**
* @property {number} maxLevels - Used by the QuadTree to set the maximum number of iteration levels.
*/
this.maxLevels = 4;
/**
* @property {number} OVERLAP_BIAS - A value added to the delta values during collision checks.
*/
this.OVERLAP_BIAS = 4;
/**
* @property {boolean} forceX - If true World.separate will always separate on the X axis before Y. Otherwise it will check gravity totals first.
*/
this.forceX = false;
/**
* @property {number} sortDirection - Used when colliding a Sprite vs. a Group, or a Group vs. a Group, this defines the direction the sort is based on. Default is Phaser.Physics.Arcade.LEFT_RIGHT.
* @default
*/
this.sortDirection = Phaser.Physics.Arcade.LEFT_RIGHT;
/**
* @property {boolean} skipQuadTree - If true the QuadTree will not be used for any collision. QuadTrees are great if objects are well spread out in your game, otherwise they are a performance hit. If you enable this you can disable on a per body basis via `Body.skipQuadTree`.
*/
this.skipQuadTree = true;
/**
* @property {boolean} isPaused - If `true` the `Body.preUpdate` method will be skipped, halting all motion for all bodies. Note that other methods such as `collide` will still work, so be careful not to call them on paused bodies.
*/
this.isPaused = false;
/**
* @property {Phaser.QuadTree} quadTree - The world QuadTree.
*/
this.quadTree = new Phaser.QuadTree(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
/**
* @property {number} _total - Internal cache var.
* @private
*/
this._total = 0;
// By default we want the bounds the same size as the world bounds
this.setBoundsToWorld();
};
Phaser.Physics.Arcade.prototype.constructor = Phaser.Physics.Arcade;
/**
* A constant used for the sortDirection value.
* Use this if you don't wish to perform any pre-collision sorting at all, or will manually sort your Groups.
* @constant
* @type {number}
*/
Phaser.Physics.Arcade.SORT_NONE = 0;
/**
* A constant used for the sortDirection value.
* Use this if your game world is wide but short and scrolls from the left to the right (i.e. Mario)
* @constant
* @type {number}
*/
Phaser.Physics.Arcade.LEFT_RIGHT = 1;
/**
* A constant used for the sortDirection value.
* Use this if your game world is wide but short and scrolls from the right to the left (i.e. Mario backwards)
* @constant
* @type {number}
*/
Phaser.Physics.Arcade.RIGHT_LEFT = 2;
/**
* A constant used for the sortDirection value.
* Use this if your game world is narrow but tall and scrolls from the top to the bottom (i.e. Dig Dug)
* @constant
* @type {number}
*/
Phaser.Physics.Arcade.TOP_BOTTOM = 3;
/**
* A constant used for the sortDirection value.
* Use this if your game world is narrow but tall and scrolls from the bottom to the top (i.e. Commando or a vertically scrolling shoot-em-up)
* @constant
* @type {number}
*/
Phaser.Physics.Arcade.BOTTOM_TOP = 4;
Phaser.Physics.Arcade.prototype = {
/**
* Updates the size of this physics world.
*
* @method Phaser.Physics.Arcade#setBounds
* @param {number} x - Top left most corner of the world.
* @param {number} y - Top left most corner of the world.
* @param {number} width - New width of the world. Can never be smaller than the Game.width.
* @param {number} height - New height of the world. Can never be smaller than the Game.height.
*/
setBounds: function (x, y, width, height) {
this.bounds.setTo(x, y, width, height);
},
/**
* Updates the size of this physics world to match the size of the game world.
*
* @method Phaser.Physics.Arcade#setBoundsToWorld
*/
setBoundsToWorld: function () {
this.bounds.copyFrom(this.game.world.bounds);
},
/**
* This will create an Arcade Physics body on the given game object or array of game objects.
* A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed.
*
* @method Phaser.Physics.Arcade#enable
* @param {object|array|Phaser.Group} object - The game object to create the physics body on. Can also be an array or Group of objects, a body will be created on every child that has a `body` property.
* @param {boolean} [children=true] - Should a body be created on all children of this object? If true it will recurse down the display list as far as it can go.
*/
enable: function (object, children) {
if (children === undefined) { children = true; }
var i = 1;
if (Array.isArray(object))
{
i = object.length;
while (i--)
{
if (object[i] instanceof Phaser.Group)
{
// If it's a Group then we do it on the children regardless
this.enable(object[i].children, children);
}
else
{
this.enableBody(object[i]);
if (children && object[i].hasOwnProperty('children') && object[i].children.length > 0)
{
this.enable(object[i], true);
}
}
}
}
else
{
if (object instanceof Phaser.Group)
{
// If it's a Group then we do it on the children regardless
this.enable(object.children, children);
}
else
{
this.enableBody(object);
if (children && object.hasOwnProperty('children') && object.children.length > 0)
{
this.enable(object.children, true);
}
}
}
},
/**
* Creates an Arcade Physics body on the given game object.
*
* A game object can only have 1 physics body active at any one time, and it can't be changed until the body is nulled.
*
* When you add an Arcade Physics body to an object it will automatically add the object into its parent Groups hash array.
*
* @method Phaser.Physics.Arcade#enableBody
* @param {object} object - The game object to create the physics body on. A body will only be created if this object has a null `body` property.
*/
enableBody: function (object) {
if (object.hasOwnProperty('body') && object.body === null)
{
object.body = new Phaser.Physics.Arcade.Body(object);
if (object.parent && object.parent instanceof Phaser.Group)
{
object.parent.addToHash(object);
}
}
},
/**
* Called automatically by a Physics body, it updates all motion related values on the Body unless `World.isPaused` is `true`.
*
* @method Phaser.Physics.Arcade#updateMotion
* @param {Phaser.Physics.Arcade.Body} The Body object to be updated.
*/
updateMotion: function (body) {
if (body.allowRotation)
{
var velocityDelta = this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity;
body.angularVelocity += velocityDelta;
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
}
body.velocity.x = this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x);
body.velocity.y = this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y);
},
/**
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
* Based on a function in Flixel by @ADAMATOMIC
*
* @method Phaser.Physics.Arcade#computeVelocity
* @param {number} axis - 0 for nothing, 1 for horizontal, 2 for vertical.
* @param {Phaser.Physics.Arcade.Body} body - The Body object to be updated.
* @param {number} velocity - Any component of velocity (e.g. 20).
* @param {number} acceleration - Rate at which the velocity is changing.
* @param {number} drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
* @param {number} [max=10000] - An absolute value cap for the velocity.
* @return {number} The altered Velocity value.
*/
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
if (max === undefined) { max = 10000; }
if (axis === 1 && body.allowGravity)
{
velocity += (this.gravity.x + body.gravity.x) * this.game.time.physicsElapsed;
}
else if (axis === 2 && body.allowGravity)
{
velocity += (this.gravity.y + body.gravity.y) * this.game.time.physicsElapsed;
}
if (acceleration)
{
velocity += acceleration * this.game.time.physicsElapsed;
}
else if (drag && body.allowDrag)
{
drag *= this.game.time.physicsElapsed;
if (velocity - drag > 0)
{
velocity -= drag;
}
else if (velocity + drag < 0)
{
velocity += drag;
}
else
{
velocity = 0;
}
}
if (velocity > max)
{
velocity = max;
}
else if (velocity < -max)
{
velocity = -max;
}
return velocity;
},
/**
* Checks for overlaps between two game objects. The objects can be Sprites, Groups or Emitters.
*
* Unlike {@link #collide} the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
*
* You can perform Sprite vs. Sprite, Sprite vs. Group and Group vs. Group overlap checks.
* Both the first and second parameter can be arrays of objects, of differing types.
* If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
*
* **This function is not recursive**, and will not test against children of objects passed (i.e. Groups within Groups).
*
* ##### Tilemaps
*
* Any overlapping tiles, including blank/null tiles, will give a positive result. Tiles marked via {@link Phaser.Tilemap#setCollision} (and similar methods) have no special status, and callbacks added via {@link Phaser.Tilemap#setTileIndexCallback} or {@link Phaser.Tilemap#setTileLocationCallback} are not invoked. So calling this method without any callbacks isn't very useful.
*
* If you're interested only in whether an object overlaps a certain tile or class of tiles, filter the tiles with `processCallback` and then use the result returned by this method. Blank/null tiles can be excluded by their {@link Phaser.Tile#index index} (-1).
*
* If you want to take action on certain overlaps, examine the tiles in `collideCallback` and then handle as you like.
*
* @method Phaser.Physics.Arcade#overlap
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|array} object1 - The first object or array of objects to check. Can be Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|array} object2 - The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
* @param {function} [overlapCallback=null] - An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them, unless you are checking Group vs. Sprite, in which case Sprite will always be the first parameter.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then `overlapCallback` will only be called if this callback returns `true`.
* @param {object} [callbackContext] - The context in which to run the callbacks.
* @return {boolean} True if an overlap occurred otherwise false.
*/
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) {
overlapCallback = overlapCallback || null;
processCallback = processCallback || null;
callbackContext = callbackContext || overlapCallback;
this._total = 0;
this.collideObjects(object1, object2, overlapCallback, processCallback, callbackContext, true);
return (this._total > 0);
},
/**
* Checks for collision between two game objects and separates them if colliding ({@link https://gist.github.com/samme/cbb81dd19f564dcfe2232761e575063d details}). If you don't require separation then use {@link #overlap} instead.
*
* You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
* Both the `object1` and `object2` can be arrays of objects, of differing types.
*
* If two Groups or arrays are passed, each member of one will be tested against each member of the other.
*
* If one Group **only** is passed (as `object1`), each member of the Group will be collided against the other members.
*
* If either object is `null` the collision test will fail.
*
* Bodies with `enable = false` and Sprites with `exists = false` are skipped (ignored).
*
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place, giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
*
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
*
* **This function is not recursive**, and will not test against children of objects passed (i.e. Groups or Tilemaps within other Groups).
*
* ##### Examples
*
* ```javascript
* collide(group);
* collide(group, undefined); // equivalent
*
* collide(sprite1, sprite2);
*
* collide(sprite, group);
*
* collide(group1, group2);
*
* collide([sprite1, sprite2], [sprite3, sprite4]); // 1 vs. 3, 1 vs. 4, 2 vs. 3, 2 vs. 4
* ```
*
* ##### Tilemaps
*
* Tiles marked via {@link Phaser.Tilemap#setCollision} (and similar methods) are "solid". If a Sprite collides with one of these tiles, the two are separated by moving the Sprite outside the tile's edges. Enable {@link Phaser.TilemapLayer#debug} to see the colliding edges of the Tilemap.
*
* Tiles with a callback attached via {@link Phaser.Tilemap#setTileIndexCallback} or {@link Phaser.Tilemap#setTileLocationCallback} invoke the callback if a Sprite collides with them. If a tile has a callback attached via both methods, only the location callback is invoked. The colliding Sprite is separated from the tile only if the callback returns `true`.
*
* @method Phaser.Physics.Arcade#collide
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.TilemapLayer|array} object1 - The first object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.TilemapLayer.
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.TilemapLayer|array} object2 - The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.TilemapLayer.
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them, unless you are colliding Group vs. Sprite, in which case Sprite will always be the first parameter.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them, unless you are colliding Group vs. Sprite, in which case Sprite will always be the first parameter.
* @param {object} [callbackContext] - The context in which to run the callbacks.
* @return {boolean} True if a collision occurred otherwise false.
*/
collide: function (object1, object2, collideCallback, processCallback, callbackContext) {
collideCallback = collideCallback || null;
processCallback = processCallback || null;
callbackContext = callbackContext || collideCallback;
this._total = 0;
this.collideObjects(object1, object2, collideCallback, processCallback, callbackContext, false);
return (this._total > 0);
},
/**
* A Sort function for sorting two bodies based on a LEFT to RIGHT sort direction.
*
* This is called automatically by World.sort
*
* @method Phaser.Physics.Arcade#sortLeftRight
* @param {Phaser.Sprite} a - The first Sprite to test. The Sprite must have an Arcade Physics Body.
* @param {Phaser.Sprite} b - The second Sprite to test. The Sprite must have an Arcade Physics Body.
* @return {integer} A negative value if `a > b`, a positive value if `a < b` or 0 if `a === b` or the bodies are invalid.
*/
sortLeftRight: function (a, b) {
if (!a.body || !b.body)
{
return 0;
}
return a.body.x - b.body.x;
},
/**
* A Sort function for sorting two bodies based on a RIGHT to LEFT sort direction.
*
* This is called automatically by World.sort
*
* @method Phaser.Physics.Arcade#sortRightLeft
* @param {Phaser.Sprite} a - The first Sprite to test. The Sprite must have an Arcade Physics Body.
* @param {Phaser.Sprite} b - The second Sprite to test. The Sprite must have an Arcade Physics Body.
* @return {integer} A negative value if `a > b`, a positive value if `a < b` or 0 if `a === b` or the bodies are invalid.
*/
sortRightLeft: function (a, b) {
if (!a.body || !b.body)
{
return 0;
}
return b.body.x - a.body.x;
},
/**
* A Sort function for sorting two bodies based on a TOP to BOTTOM sort direction.
*
* This is called automatically by World.sort
*
* @method Phaser.Physics.Arcade#sortTopBottom
* @param {Phaser.Sprite} a - The first Sprite to test. The Sprite must have an Arcade Physics Body.
* @param {Phaser.Sprite} b - The second Sprite to test. The Sprite must have an Arcade Physics Body.
* @return {integer} A negative value if `a > b`, a positive value if `a < b` or 0 if `a === b` or the bodies are invalid.
*/
sortTopBottom: function (a, b) {
if (!a.body || !b.body)
{
return 0;
}
return a.body.y - b.body.y;
},
/**
* A Sort function for sorting two bodies based on a BOTTOM to TOP sort direction.
*
* This is called automatically by World.sort
*
* @method Phaser.Physics.Arcade#sortBottomTop
* @param {Phaser.Sprite} a - The first Sprite to test. The Sprite must have an Arcade Physics Body.
* @param {Phaser.Sprite} b - The second Sprite to test. The Sprite must have an Arcade Physics Body.
* @return {integer} A negative value if `a > b`, a positive value if `a < b` or 0 if `a === b` or the bodies are invalid.
*/
sortBottomTop: function (a, b) {
if (!a.body || !b.body)
{
return 0;
}
return b.body.y - a.body.y;
},
/**
* This method will sort a Groups hash array.
*
* If the Group has `physicsSortDirection` set it will use the sort direction defined.
*
* Otherwise if the sortDirection parameter is undefined, or Group.physicsSortDirection is null, it will use Phaser.Physics.Arcade.sortDirection.
*
* By changing Group.physicsSortDirection you can customise each Group to sort in a different order.
*
* @method Phaser.Physics.Arcade#sort
* @param {Phaser.Group} group - The Group to sort.
* @param {integer} [sortDirection] - The sort direction used to sort this Group.
*/
sort: function (group, sortDirection) {
if (group.physicsSortDirection !== null)
{
sortDirection = group.physicsSortDirection;
}
else
{
if (sortDirection === undefined) { sortDirection = this.sortDirection; }
}
if (sortDirection === Phaser.Physics.Arcade.LEFT_RIGHT)
{
// Game world is say 2000x600 and you start at 0
group.hash.sort(this.sortLeftRight);
}
else if (sortDirection === Phaser.Physics.Arcade.RIGHT_LEFT)
{
// Game world is say 2000x600 and you start at 2000
group.hash.sort(this.sortRightLeft);
}
else if (sortDirection === Phaser.Physics.Arcade.TOP_BOTTOM)
{
// Game world is say 800x2000 and you start at 0
group.hash.sort(this.sortTopBottom);
}
else if (sortDirection === Phaser.Physics.Arcade.BOTTOM_TOP)
{
// Game world is say 800x2000 and you start at 2000
group.hash.sort(this.sortBottomTop);
}
},
/**
* Internal collision handler. Extracts objects for {@link #collideHandler}.
*
* @method Phaser.Physics.Arcade#collideObjects
* @private
*/
collideObjects: function (object1, object2, collideCallback, processCallback, callbackContext, overlapOnly) {
if (!Array.isArray(object1) && Array.isArray(object2))
{
for (var i = 0; i < object2.length; i++)
{
if (!object2[i]) { continue; }
this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, overlapOnly);
}
}
else if (Array.isArray(object1) && !Array.isArray(object2))
{
for (var i = 0; i < object1.length; i++)
{
if (!object1[i]) { continue; }
this.collideHandler(object1[i], object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
else if (Array.isArray(object1) && Array.isArray(object2))
{
for (var i = 0; i < object1.length; i++)
{
if (!object1[i]) { continue; }
for (var j = 0; j < object2.length; j++)
{
if (!object2[j]) { continue; }
this.collideHandler(object1[i], object2[j], collideCallback, processCallback, callbackContext, overlapOnly);
}
}
}
else
{
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
},
/**
* Internal collision handler. Matches arguments to other handlers.
*
* @method Phaser.Physics.Arcade#collideHandler
* @private
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.TilemapLayer} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.TilemapLayer.
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.TilemapLayer} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.TilemapLayer. Can also be an array of objects to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideHandler: function (object1, object2, collideCallback, processCallback, callbackContext, overlapOnly) {
// Only collide valid objects
if (object2 === undefined && object1.physicsType === Phaser.GROUP)
{
this.sort(object1);
this.collideGroupVsSelf(object1, collideCallback, processCallback, callbackContext, overlapOnly);
return;
}
// If neither of the objects are set or exist then bail out
if (!object1 || !object2 || !object1.exists || !object2.exists)
{
return;
}
// Groups? Sort them
if (this.sortDirection !== Phaser.Physics.Arcade.SORT_NONE)
{
if (object1.physicsType === Phaser.GROUP)
{
this.sort(object1);
}
if (object2.physicsType === Phaser.GROUP)
{
this.sort(object2);
}
}
// SPRITES
if (object1.physicsType === Phaser.SPRITE)
{
if (object2.physicsType === Phaser.SPRITE)
{
this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
else if (object2.physicsType === Phaser.GROUP)
{
this.collideSpriteVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
else if (object2.physicsType === Phaser.TILEMAPLAYER)
{
this.collideSpriteVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
// GROUPS
else if (object1.physicsType === Phaser.GROUP)
{
if (object2.physicsType === Phaser.SPRITE)
{
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
}
else if (object2.physicsType === Phaser.GROUP)
{
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
else if (object2.physicsType === Phaser.TILEMAPLAYER)
{
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
// TILEMAP LAYERS
else if (object1.physicsType === Phaser.TILEMAPLAYER)
{
if (object2.physicsType === Phaser.SPRITE)
{
this.collideSpriteVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
}
else if (object2.physicsType === Phaser.GROUP)
{
this.collideGroupVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
},
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @method Phaser.Physics.Arcade#collideSpriteVsSprite
* @private
* @param {Phaser.Sprite} sprite1 - The first sprite to check.
* @param {Phaser.Sprite} sprite2 - The second sprite to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
* @return {boolean} True if there was a collision, otherwise false.
*/
collideSpriteVsSprite: function (sprite1, sprite2, collideCallback, processCallback, callbackContext, overlapOnly) {
if (!sprite1.body || !sprite2.body)
{
return false;
}
if (this.separate(sprite1.body, sprite2.body, processCallback, callbackContext, overlapOnly))
{
if (collideCallback)
{
collideCallback.call(callbackContext, sprite1, sprite2);
}
this._total++;
}
return true;
},
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @method Phaser.Physics.Arcade#collideSpriteVsGroup
* @private
* @param {Phaser.Sprite} sprite - The sprite to check.
* @param {Phaser.Group} group - The Group to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideSpriteVsGroup: function (sprite, group, collideCallback, processCallback, callbackContext, overlapOnly) {
if (group.length === 0 || !sprite.body)
{
return;
}
if (this.skipQuadTree || sprite.body.skipQuadTree)
{
var bounds = {};
for (var i = 0; i < group.hash.length; i++)
{
var object1 = group.hash[i];
// Skip duff entries - we can't check a non-existent sprite or one with no body
if (!object1 || !object1.exists || !object1.body)
{
continue;
}
// Inject the Body bounds data into the bounds object
bounds = object1.body.getBounds(bounds);
// Skip items either side of the sprite
if (this.sortDirection === Phaser.Physics.Arcade.LEFT_RIGHT)
{
if (sprite.body.right < bounds.x)
{
break;
}
else if (bounds.right < sprite.body.x)
{
continue;
}
}
else if (this.sortDirection === Phaser.Physics.Arcade.RIGHT_LEFT)
{
if (sprite.body.x > bounds.right)
{
break;
}
else if (bounds.x > sprite.body.right)
{
continue;
}
}
else if (this.sortDirection === Phaser.Physics.Arcade.TOP_BOTTOM)
{
if (sprite.body.bottom < bounds.y)
{
break;
}
else if (bounds.bottom < sprite.body.y)
{
continue;
}
}
else if (this.sortDirection === Phaser.Physics.Arcade.BOTTOM_TOP)
{
if (sprite.body.y > bounds.bottom)
{
break;
}
else if (bounds.y > sprite.body.bottom)
{
continue;
}
}
this.collideSpriteVsSprite(sprite, object1, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
else
{
// What is the sprite colliding with in the quadtree?
this.quadTree.clear();
this.quadTree.reset(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
this.quadTree.populate(group);
var items = this.quadTree.retrieve(sprite);
for (var i = 0; i < items.length; i++)
{
// We have our potential suspects, are they in this group?
if (this.separate(sprite.body, items[i], processCallback, callbackContext, overlapOnly))
{
if (collideCallback)
{
collideCallback.call(callbackContext, sprite, items[i].sprite);
}
this._total++;
}
}
}
},
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @method Phaser.Physics.Arcade#collideGroupVsSelf
* @private
* @param {Phaser.Group} group - The Group to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
* @return {boolean} True if there was a collision, otherwise false.
*/
collideGroupVsSelf: function (group, collideCallback, processCallback, callbackContext, overlapOnly) {
if (group.length === 0)
{
return;
}
for (var i = 0; i < group.hash.length; i++)
{
var bounds1 = {};
var object1 = group.hash[i];
// Skip duff entries - we can't check a non-existent sprite or one with no body
if (!object1 || !object1.exists || !object1.body)
{
continue;
}
// Inject the Body bounds data into the bounds1 object
bounds1 = object1.body.getBounds(bounds1);
for (var j = i + 1; j < group.hash.length; j++)
{
var bounds2 = {};
var object2 = group.hash[j];
// Skip duff entries - we can't check a non-existent sprite or one with no body
if (!object2 || !object2.exists || !object2.body)
{
continue;
}
// Inject the Body bounds data into the bounds2 object
bounds2 = object2.body.getBounds(bounds2);
// Skip items either side of the sprite
if (this.sortDirection === Phaser.Physics.Arcade.LEFT_RIGHT)
{
if (bounds1.right < bounds2.x)
{
break;
}
else if (bounds2.right < bounds1.x)
{
continue;
}
}
else if (this.sortDirection === Phaser.Physics.Arcade.RIGHT_LEFT)
{
if (bounds1.x > bounds2.right)
{
continue;
}
else if (bounds2.x > bounds1.right)
{
break;
}
}
else if (this.sortDirection === Phaser.Physics.Arcade.TOP_BOTTOM)
{
if (bounds1.bottom < bounds2.y)
{
continue;
}
else if (bounds2.bottom < bounds1.y)
{
break;
}
}
else if (this.sortDirection === Phaser.Physics.Arcade.BOTTOM_TOP)
{
if (bounds1.y > bounds2.bottom)
{
continue;
}
else if (bounds2.y > object1.body.bottom)
{
break;
}
}
this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
},
/**
* An internal function. Use Phaser.Physics.Arcade.collide instead.
*
* @method Phaser.Physics.Arcade#collideGroupVsGroup
* @private
* @param {Phaser.Group} group1 - The first Group to check.
* @param {Phaser.Group} group2 - The second Group to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext, overlapOnly) {
if (group1.length === 0 || group2.length === 0)
{
return;
}
for (var i = 0; i < group1.children.length; i++)
{
if (group1.children[i].exists)
{
if (group1.children[i].physicsType === Phaser.GROUP)
{
this.collideGroupVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly);
}
else
{
this.collideSpriteVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
}
},
/**
* The core separation function to separate two physics bodies.
*
* @private
* @method Phaser.Physics.Arcade#separate
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body object to separate.
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body object to separate.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this function is set then the sprites will only be collided if it returns true.
* @param {object} [callbackContext] - The context in which to run the process callback.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
* @return {boolean} Returns true if the bodies collided, otherwise false.
*/
separate: function (body1, body2, processCallback, callbackContext, overlapOnly) {
if (
!body1.enable ||
!body2.enable ||
body1.checkCollision.none ||
body2.checkCollision.none ||
!this.intersects(body1, body2))
{
return false;
}
// They overlap. Is there a custom process callback? If it returns true then we can carry on, otherwise we should abort.
if (processCallback && processCallback.call(callbackContext, body1.sprite, body2.sprite) === false)
{
return false;
}
// Circle vs. Circle quick bail out
if (body1.isCircle && body2.isCircle)
{
return this.separateCircle(body1, body2, overlapOnly);
}
// We define the behavior of bodies in a collision circle and rectangle
// If a collision occurs in the corner points of the rectangle, the body behave like circles
// Either body1 or body2 is a circle
if (body1.isCircle !== body2.isCircle)
{
var bodyRect = (body1.isCircle) ? body2 : body1;
var bodyCircle = (body1.isCircle) ? body1 : body2;
var rect = {
x: bodyRect.x,
y: bodyRect.y,
right: bodyRect.right,
bottom: bodyRect.bottom
};
var circle = bodyCircle.center;
if (circle.y < rect.y || circle.y > rect.bottom)
{
if (circle.x < rect.x || circle.x > rect.right)
{
return this.separateCircle(body1, body2, overlapOnly);
}
}
}
var resultX = false;
var resultY = false;
// Do we separate on x or y first?
if (this.forceX || Math.abs(this.gravity.y + body1.gravity.y) < Math.abs(this.gravity.x + body1.gravity.x))
{
resultX = this.separateX(body1, body2, overlapOnly);
// Are they still intersecting? Let's do the other axis then
if (this.intersects(body1, body2))
{
resultY = this.separateY(body1, body2, overlapOnly);
}
}
else
{
resultY = this.separateY(body1, body2, overlapOnly);
// Are they still intersecting? Let's do the other axis then
if (this.intersects(body1, body2))
{
resultX = this.separateX(body1, body2, overlapOnly);
}
}
var result = (resultX || resultY);
if (result)
{
if (overlapOnly)
{
if (body1.onOverlap)
{
body1.onOverlap.dispatch(body1.sprite, body2.sprite);
}
if (body2.onOverlap)
{
body2.onOverlap.dispatch(body2.sprite, body1.sprite);
}
}
else
{
if (body1.onCollide)
{
body1.onCollide.dispatch(body1.sprite, body2.sprite);
}
if (body2.onCollide)
{
body2.onCollide.dispatch(body2.sprite, body1.sprite);
}
}
}
return result;
},
/**
* Check for intersection against two bodies.
*
* @method Phaser.Physics.Arcade#intersects
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body object to check.
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body object to check.
* @return {boolean} True if they intersect, otherwise false.
*/
intersects: function (body1, body2) {
if (body1 === body2)
{
return false;
}
if (body1.isCircle)
{
if (body2.isCircle)
{
// Circle vs. Circle
return Phaser.Math.distance(body1.center.x, body1.center.y, body2.center.x, body2.center.y) <= (body1.halfWidth + body2.halfWidth);
}
else
{
// Circle vs. Rect
return this.circleBodyIntersects(body1, body2);
}
}
else
{
if (body2.isCircle)
{
// Rect vs. Circle
return this.circleBodyIntersects(body2, body1);
}
else
{
// Rect vs. Rect
if (body1.right <= body2.position.x)
{
return false;
}
if (body1.bottom <= body2.position.y)
{
return false;
}
if (body1.position.x >= body2.right)
{
return false;
}
if (body1.position.y >= body2.bottom)
{
return false;
}
return true;
}
}
},
/**
* Checks to see if a circular Body intersects with a Rectangular Body.
*
* @method Phaser.Physics.Arcade#circleBodyIntersects
* @param {Phaser.Physics.Arcade.Body} circle - The Body with `isCircle` set.
* @param {Phaser.Physics.Arcade.Body} body - The Body with `isCircle` not set (i.e. uses Rectangle shape)
* @return {boolean} Returns true if the bodies intersect, otherwise false.
*/
circleBodyIntersects: function (circle, body) {
var x = Phaser.Math.clamp(circle.center.x, body.left, body.right);
var y = Phaser.Math.clamp(circle.center.y, body.top, body.bottom);
var dx = (circle.center.x - x) * (circle.center.x - x);
var dy = (circle.center.y - y) * (circle.center.y - y);
return (dx + dy) <= (circle.halfWidth * circle.halfWidth);
},
/**
* The core separation function to separate two circular physics bodies.
*
* @method Phaser.Physics.Arcade#separateCircle
* @private
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body to separate. Must have `Body.isCircle` true and a positive `radius`.
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body to separate. Must have `Body.isCircle` true and a positive `radius`.
* @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place.
* @return {boolean} Returns true if the bodies were separated or overlap, otherwise false.
*/
separateCircle: function (body1, body2, overlapOnly) {
// Set the bounding box overlap values
this.getOverlapX(body1, body2);
this.getOverlapY(body1, body2);
var dx = body2.center.x - body1.center.x;
var dy = body2.center.y - body1.center.y;
var angleCollision = Math.atan2(dy, dx);
var overlap = 0;
if (body1.isCircle !== body2.isCircle)
{
var rect = {
x: (body2.isCi