UNPKG

phaser

Version:

A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.

1,164 lines (995 loc) 77.9 kB
/** * @author Richard Davey <rich@photonstorm.com> * @copyright 2014 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); /** * Set the checkCollision properties to control for which bounds collision is processed. * For example checkCollision.down = false means Bodies cannot collide with the World.bounds.bottom. * @property {object} checkCollision - An object containing allowed collision flags. */ 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 {number} TILE_BIAS - A value added to the delta values during collision with tiles. Adjust this if you get tunnelling. */ this.TILE_BIAS = 16; /** * @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 {boolean} skipQuadTree - If true a QuadTree will never be used for any collision. Handy for tightly packed games. See also Body.skipQuadTree. */ this.skipQuadTree = 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); // Avoid gc spikes by caching these values for re-use /** * @property {number} _overlap - Internal cache var. * @private */ this._overlap = 0; /** * @property {number} _maxOverlap - Internal cache var. * @private */ this._maxOverlap = 0; /** * @property {number} _velocity1 - Internal cache var. * @private */ this._velocity1 = 0; /** * @property {number} _velocity2 - Internal cache var. * @private */ this._velocity2 = 0; /** * @property {number} _newVelocity1 - Internal cache var. * @private */ this._newVelocity1 = 0; /** * @property {number} _newVelocity2 - Internal cache var. * @private */ this._newVelocity2 = 0; /** * @property {number} _average - Internal cache var. * @private */ this._average = 0; /** * @property {Array} _mapData - Internal cache var. * @private */ this._mapData = []; /** * @property {boolean} _result - Internal cache var. * @private */ this._result = false; /** * @property {number} _total - Internal cache var. * @private */ this._total = 0; /** * @property {number} _angle - Internal cache var. * @private */ this._angle = 0; /** * @property {number} _dx - Internal cache var. * @private */ this._dx = 0; /** * @property {number} _dy - Internal cache var. * @private */ this._dy = 0; // By default we want the bounds the same size as the world bounds this.setBoundsToWorld(); }; Phaser.Physics.Arcade.prototype.constructor = Phaser.Physics.Arcade; 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.setTo(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height); }, /** * 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 (typeof 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. * * @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); } }, /** * Called automatically by a Physics body, it updates all motion related values on the Body. * * @method Phaser.Physics.Arcade#updateMotion * @param {Phaser.Physics.Arcade.Body} The Body object to be updated. */ updateMotion: function (body) { this._velocityDelta = this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity; body.angularVelocity += this._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) { max = 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) { this._drag = drag * this.game.time.physicsElapsed; if (velocity - this._drag > 0) { velocity -= this._drag; } else if (velocity + this._drag < 0) { velocity += this._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. * You can perform Sprite vs. Sprite, Sprite vs. Group and Group vs. Group overlap checks. * Unlike collide the objects are NOT automatically separated or have any physics applied, they merely test for overlap results. * 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. * NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups within Groups). * * @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. * @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 processCallback 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._result = false; this._total = 0; if (!Array.isArray(object1) && Array.isArray(object2)) { for (var i = 0, len = object2.length; i < len; i++) { this.collideHandler(object1, object2[i], overlapCallback, processCallback, callbackContext, true); } } else if (Array.isArray(object1) && !Array.isArray(object2)) { for (var i = 0, len = object1.length; i < len; i++) { this.collideHandler(object1[i], object2, overlapCallback, processCallback, callbackContext, true); } } else if (Array.isArray(object1) && Array.isArray(object2)) { for (var i = 0, len = object1.length; i < len; i++) { for (var j = 0, len2 = object2.length; j < len2; j++) { this.collideHandler(object1[i], object2[j], overlapCallback, processCallback, callbackContext, true); } } } else { this.collideHandler(object1, object2, overlapCallback, processCallback, callbackContext, true); } return (this._total > 0); }, /** * Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions. * 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. * The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead. * 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. * NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups or Tilemaps within other Groups). * * @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. * @param {object} [callbackContext] - The context in which to run the callbacks. * @return {boolean} True if a collision occured otherwise false. */ collide: function (object1, object2, collideCallback, processCallback, callbackContext) { collideCallback = collideCallback || null; processCallback = processCallback || null; callbackContext = callbackContext || collideCallback; this._result = false; this._total = 0; if (!Array.isArray(object1) && Array.isArray(object2)) { for (var i = 0, len = object2.length; i < len; i++) { this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, false); } } else if (Array.isArray(object1) && !Array.isArray(object2)) { for (var i = 0, len = object1.length; i < len; i++) { this.collideHandler(object1[i], object2, collideCallback, processCallback, callbackContext, false); } } else if (Array.isArray(object1) && Array.isArray(object2)) { for (var i = 0, len1 = object1.length; i < len1; i++) { for (var j = 0, len2 = object2.length; j < len2; j++) { this.collideHandler(object1[i], object2[j], collideCallback, processCallback, callbackContext, false); } } } else { this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, false); } return (this._total > 0); }, /** * Internal collision handler. * * @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 (typeof object2 === 'undefined' && (object1.type === Phaser.GROUP || object1.type === Phaser.EMITTER)) { this.collideGroupVsSelf(object1, collideCallback, processCallback, callbackContext, overlapOnly); return; } if (object1 && object2 && object1.exists && object2.exists) { // SPRITES if (object1.type == Phaser.SPRITE || object1.type == Phaser.TILESPRITE) { if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE) { this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly); } else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER) { this.collideSpriteVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly); } else if (object2.type == Phaser.TILEMAPLAYER) { this.collideSpriteVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext); } } // GROUPS else if (object1.type == Phaser.GROUP) { if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE) { this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly); } else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER) { this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly); } else if (object2.type == Phaser.TILEMAPLAYER) { this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext); } } // TILEMAP LAYERS else if (object1.type == Phaser.TILEMAPLAYER) { if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE) { this.collideSpriteVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext); } else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER) { this.collideGroupVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext); } } // EMITTER else if (object1.type == Phaser.EMITTER) { if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE) { this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly); } else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER) { this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly); } else if (object2.type == Phaser.TILEMAPLAYER) { this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext); } } } }, /** * 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 (sprite.body.skipQuadTree || this.skipQuadTree) { for (var i = 0, len = group.children.length; i < len; i++) { if (group.children[i] && group.children[i].exists) { this.collideSpriteVsSprite(sprite, group.children[i], 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); this._potentials = this.quadTree.retrieve(sprite); for (var i = 0, len = this._potentials.length; i < len; i++) { // We have our potential suspects, are they in this group? if (this.separate(sprite.body, this._potentials[i], processCallback, callbackContext, overlapOnly)) { if (collideCallback) { collideCallback.call(callbackContext, sprite, this._potentials[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; } var len = group.children.length; for (var i = 0; i < len; i++) { for (var j = i + 1; j <= len; j++) { if (group.children[i] && group.children[j] && group.children[i].exists && group.children[j].exists) { this.collideSpriteVsSprite(group.children[i], group.children[j], 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, len = group1.children.length; i < len; i++) { if (group1.children[i].exists) { if (group1.children[i].type === Phaser.GROUP) { this.collideGroupVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly); } else { this.collideSpriteVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly); } } } }, /** * An internal function. Use Phaser.Physics.Arcade.collide instead. * * @method Phaser.Physics.Arcade#collideSpriteVsTilemapLayer * @private * @param {Phaser.Sprite} sprite - The sprite to check. * @param {Phaser.TilemapLayer} tilemapLayer - The layer 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. */ collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) { if (!sprite.body) { return; } this._mapData = tilemapLayer.getTiles( sprite.body.position.x - sprite.body.tilePadding.x, sprite.body.position.y - sprite.body.tilePadding.y, sprite.body.width + sprite.body.tilePadding.x, sprite.body.height + sprite.body.tilePadding.y, false, false); if (this._mapData.length === 0) { return; } for (var i = 0; i < this._mapData.length; i++) { if (processCallback) { if (processCallback.call(callbackContext, sprite, this._mapData[i])) { if (this.separateTile(i, sprite.body, this._mapData[i])) { this._total++; if (collideCallback) { collideCallback.call(callbackContext, sprite, this._mapData[i]); } } } } else { if (this.separateTile(i, sprite.body, this._mapData[i])) { this._total++; if (collideCallback) { collideCallback.call(callbackContext, sprite, this._mapData[i]); } } } } }, /** * An internal function. Use Phaser.Physics.Arcade.collide instead. * * @private * @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer * @param {Phaser.Group} group - The Group to check. * @param {Phaser.TilemapLayer} tilemapLayer - The layer 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. */ collideGroupVsTilemapLayer: function (group, tilemapLayer, collideCallback, processCallback, callbackContext) { if (group.length === 0) { return; } for (var i = 0, len = group.children.length; i < len; i++) { if (group.children[i].exists) { this.collideSpriteVsTilemapLayer(group.children[i], tilemapLayer, collideCallback, processCallback, callbackContext); } } }, /** * 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 || !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; } // Do we separate on x or y first? // If we weren't having to carry around so much legacy baggage with us, we could do this properly. But alas ... if (this.forceX || Math.abs(this.gravity.y + body1.gravity.y) < Math.abs(this.gravity.x + body1.gravity.x)) { this._result = (this.separateX(body1, body2, overlapOnly) || this.separateY(body1, body2, overlapOnly)); } else { this._result = (this.separateY(body1, body2, overlapOnly) || this.separateX(body1, body2, overlapOnly)); } if (overlapOnly) { // We already know they intersect from the check above, but by this point we know they've now had their overlapX/Y values populated return true; } else { return this._result; } }, /** * Check for intersection against two bodies. * * @method Phaser.Physics.Arcade#intersects * @param {Phaser.Physics.Arcade.Body} body1 - The Body object to check. * @param {Phaser.Physics.Arcade.Body} body2 - The Body object to check. * @return {boolean} True if they intersect, otherwise false. */ intersects: function (body1, body2) { 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; }, /** * The core separation function to separate two physics bodies on the x axis. * * @private * @method Phaser.Physics.Arcade#separateX * @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate. * @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate. * @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, otherwise false. */ separateX: function (body1, body2, overlapOnly) { // Can't separate two immovable bodies if (body1.immovable && body2.immovable) { return false; } this._overlap = 0; // Check if the hulls actually overlap if (this.intersects(body1, body2)) { this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS; if (body1.deltaX() === 0 && body2.deltaX() === 0) { // They overlap but neither of them are moving body1.embedded = true; body2.embedded = true; } else if (body1.deltaX() > body2.deltaX()) { // Body1 is moving right and/or Body2 is moving left this._overlap = body1.right - body2.x; if ((this._overlap > this._maxOverlap) || body1.checkCollision.right === false || body2.checkCollision.left === false) { this._overlap = 0; } else { body1.touching.none = false; body1.touching.right = true; body2.touching.none = false; body2.touching.left = true; } } else if (body1.deltaX() < body2.deltaX()) { // Body1 is moving left and/or Body2 is moving right this._overlap = body1.x - body2.width - body2.x; if ((-this._overlap > this._maxOverlap) || body1.checkCollision.left === false || body2.checkCollision.right === false) { this._overlap = 0; } else { body1.touching.none = false; body1.touching.left = true; body2.touching.none = false; body2.touching.right = true; } } // Resets the overlapX to zero if there is no overlap, or to the actual pixel value if there is body1.overlapX = this._overlap; body2.overlapX = this._overlap; // Then adjust their positions and velocities accordingly (if there was any overlap) if (this._overlap !== 0) { if (overlapOnly || body1.customSeparateX || body2.customSeparateX) { return true; } this._velocity1 = body1.velocity.x; this._velocity2 = body2.velocity.x; if (!body1.immovable && !body2.immovable) { this._overlap *= 0.5; body1.x = body1.x - this._overlap; body2.x += this._overlap; this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1); this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1); this._average = (this._newVelocity1 + this._newVelocity2) * 0.5; this._newVelocity1 -= this._average; this._newVelocity2 -= this._average; body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x; body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x; } else if (!body1.immovable) { body1.x = body1.x - this._overlap; body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x; } else if (!body2.immovable) { body2.x += this._overlap; body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x; } return true; } } return false; }, /** * The core separation function to separate two physics bodies on the y axis. * * @private * @method Phaser.Physics.Arcade#separateY * @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate. * @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate. * @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, otherwise false. */ separateY: function (body1, body2, overlapOnly) { // Can't separate two immovable or non-existing bodys if (body1.immovable && body2.immovable) { return false; } this._overlap = 0; // Check if the hulls actually overlap if (this.intersects(body1, body2)) { this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS; if (body1.deltaY() === 0 && body2.deltaY() === 0) { // They overlap but neither of them are moving body1.embedded = true; body2.embedded = true; } else if (body1.deltaY() > body2.deltaY()) { // Body1 is moving down and/or Body2 is moving up this._overlap = body1.bottom - body2.y; if ((this._overlap > this._maxOverlap) || body1.checkCollision.down === false || body2.checkCollision.up === false) { this._overlap = 0; } else { body1.touching.none = false; body1.touching.down = true; body2.touching.none = false; body2.touching.up = true; } } else if (body1.deltaY() < body2.deltaY()) { // Body1 is moving up and/or Body2 is moving down this._overlap = body1.y - body2.bottom; if ((-this._overlap > this._maxOverlap) || body1.checkCollision.up === false || body2.checkCollision.down === false) { this._overlap = 0; } else { body1.touching.none = false; body1.touching.up = true; body2.touching.none = false; body2.touching.down = true; } } // Resets the overlapY to zero if there is no overlap, or to the actual pixel value if there is body1.overlapY = this._overlap; body2.overlapY = this._overlap; // Then adjust their positions and velocities accordingly (if there was any overlap) if (this._overlap !== 0) { if (overlapOnly || body1.customSeparateY || body2.customSeparateY) { return true; } this._velocity1 = body1.velocity.y; this._velocity2 = body2.velocity.y; if (!body1.immovable && !body2.immovable) { this._overlap *= 0.5; body1.y = body1.y - this._overlap; body2.y += this._overlap; this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1); this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1); this._average = (this._newVelocity1 + this._newVelocity2) * 0.5; this._newVelocity1 -= this._average; this._newVelocity2 -= this._average; body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y; body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y; } else if (!body1.immovable) { body1.y = body1.y - this._overlap; body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y; // This is special case code that handles things like horizontal moving platforms you can ride if (body2.moves) { body1.x += body2.x - body2.prev.x; } } else if (!body2.immovable) { body2.y += this._overlap; body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y; // This is special case code that handles things like horizontal moving platforms you can ride if (body1.moves) { body2.x += body1.x - body1.prev.x; } } return true; } } return false; }, /** * The core separation function to separate a physics body and a tile. * * @private * @method Phaser.Physics.Arcade#separateTile * @param {Phaser.Physics.Arcade.Body} body - The Body object to separate. * @param {Phaser.Tile} tile - The tile to collide against. * @return {boolean} Returns true if the body was separated, otherwise false. */ separateTile: function (i, body, tile) { // We re-check for collision in case body was separated in a previous step if (!body.enable || !tile.intersects(body.position.x, body.position.y, body.right, body.bottom)) { // no collision so bail out (separted in a previous step) return false; } // They overlap. Any custom callbacks? // A local callback always takes priority over a layer level callback if (tile.collisionCallback && !tile.collisionCallback.call(tile.collisionCallbackContext, body.sprite, tile)) { // If it returns true then we can carry on, otherwise we should abort. return false; } else if (tile.layer.callbacks[tile.index] && !tile.layer.callbacks[tile.index].callback.call(tile.layer.callbacks[tile.index].callbackContext, body.sprite, tile)) { // If it returns true then we can carry on, otherwise we should abort. return false; } // We don't need to go any further if this tile doesn't actually separate if (!tile.faceLeft && !tile.faceRight && !tile.faceTop && !tile.faceBottom) { // This could happen if the tile was meant to be collided with re: a callback, but otherwise isn't needed for separation return false; }