/** * @author Richard Davey * @copyright 2014 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ /** * Ninja Physics constructor. * * @class Phaser.Physics.Ninja * @classdesc Arcade Physics Constructor * @constructor * @param {Phaser.Game} game reference to the current game instance. */ Phaser.Physics.Ninja = function (game) { /** * @property {Phaser.Game} game - Local reference to game. */ this.game = game; this.time = this.game.time; /** * @property {number} gravity - The World gravity setting. */ this.gravity = 0.2; /** * @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); /** * @property {Phaser.QuadTree} quadTree - The world QuadTree. */ // this.quadTree = new Phaser.Physics.Ninja.QuadTree(this, 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 {Array} _mapData - Internal cache var. * @private */ this._mapData = []; }; Phaser.Physics.Ninja.prototype.constructor = Phaser.Physics.Ninja; Phaser.Physics.Ninja.prototype = { /** * This will create a Ninja Physics AABB body on the given game object. Its dimensions will match the width and height of the object at the point it is created. * 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.Ninja#enableAABB * @param {object|array} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array. */ enableAABB: function (object) { this.enable(object, 1); }, /** * This will create a Ninja Physics Circle 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 object is destroyed. * * @method Phaser.Physics.Ninja#enableCircle * @param {object|array} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array. * @param {number} radius - The radius of the Circle. */ enableCircle: function (object, radius) { this.enable(object, 2, 0, radius); }, /** * This will create a Ninja Physics Tile body on the given game object. There are 34 different types of tile you can create, including 45 degree slopes, * convex and concave circles and more. The id parameter controls which Tile type is created, but you can also change it at run-time. * Note that for all degree based tile types they need to have an equal width and height. If the given object doesn't have equal width and height it will use the width. * 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.Ninja#enableTile * @param {object|array} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array. * @param {number} radius - The radius of the Circle. */ enableTile: function (object, id) { this.enable(object, 3, id); }, /** * This will create a Ninja Physics Tile body on the given game object. There are 34 different types of tile you can create, including 45 degree slopes, * convex and concave circles and more. The id parameter controls which Tile type is created, but you can also change it at run-time. * Note that for all degree based tile types they need to have an equal width and height. If the given object doesn't have equal width and height it will use the width. * 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.Ninja#enable * @param {object|array} object - The game object to create the physics body on. Can also be an array of objects, a body will be created on every object in the array. * @param {number} [type=1] - The type of Ninja shape to create. 1 = AABB, 2 = Circle or 3 = Tile. * @param {number} [id=1] - If this body is using a Tile shape, you can set the Tile id here, i.e. Phaser.Physics.Ninja.Tile.SLOPE_45DEGpn, Phaser.Physics.Ninja.Tile.CONVEXpp, etc. * @param {number} [radius=0] - If this body is using a Circle shape this controls the radius. */ enable: function (object, type, id, radius) { if (typeof type === 'undefined') { type = 1; } if (typeof id === 'undefined') { id = 1; } var i = 1; if (Array.isArray(object)) { // Add to Group i = object.length; } else { object = [object]; } while (i--) { if (object[i].body === null) { object[i].body = new Phaser.Physics.Ninja.Body(this, object[i], type, id); object[i].anchor.set(0.5); } } }, /** * Called automatically by a Physics body, it updates all motion related values on the Body. * * @method Phaser.Physics.Ninja#updateMotion * @param {Phaser.Physics.Ninja.Body} The Body object to be updated. */ update: function () { }, /** * 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. * The second parameter can be an array of objects, of differing types. * * @method Phaser.Physics.Arcade#overlap * @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter} object1 - The first object to check. Can be an instance of 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. * @returns {boolean} True if an overlap occured 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(object2)) { for (var i = 0, len = object2.length; i < len; i++) { this.collideHandler(object1, object2[i], 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. * The second parameter can be an array of objects, of differing types. * 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. * * @method Phaser.Physics.Ninja#collide * @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap. * @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap|array} object2 - The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap. * @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. * @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. * @returns {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(object2)) { for (var i = 0, len = object2.length; i < len; i++) { this.collideHandler(object1, object2[i], 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.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap. * @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap. 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 */ collideSpriteVsSprite: function (sprite1, sprite2, collideCallback, processCallback, callbackContext, overlapOnly) { if (this.separate(sprite1.body, sprite2.body, processCallback, callbackContext, overlapOnly)) { if (collideCallback) { collideCallback.call(callbackContext, sprite1, sprite2); } this._total++; } }, /** * An internal function. Use Phaser.Physics.Arcade.collide instead. * * @method Phaser.Physics.Arcade#collideSpriteVsGroup * @private */ collideSpriteVsGroup: function (sprite, group, collideCallback, processCallback, callbackContext, overlapOnly) { if (group.length === 0) { return; } // What is the sprite colliding with in the quadtree? // this.quadTree.clear(); // 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); // this.quadTree.populate(group); // this._potentials = this.quadTree.retrieve(sprite); for (var i = 0, len = group.children.length; i < len; i++) { // We have our potential suspects, are they in this group? if (group.children[i].exists && group.children[i].body && this.separate(sprite.body, group.children[i].body, processCallback, callbackContext, overlapOnly)) { if (collideCallback) { collideCallback.call(callbackContext, sprite, group.children[i]); } this._total++; } } }, /** * An internal function. Use Phaser.Physics.Arcade.collide instead. * * @method Phaser.Physics.Arcade#collideGroupVsSelf * @private */ 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 */ 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) { this.collideSpriteVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly); } } }, /** * The core separation function to separate two physics bodies. * @method Phaser.Physics.Arcade#separate * @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate. * @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate. * @param {function} [processCallback=null] - UN-USED: 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] - UN-USED: The context in which to run the process callback. * @returns {boolean} Returns true if the bodies collided, otherwise false. */ separate: function (body1, body2, processCallback, callbackContext, overlapOnly) { if (body1.type !== Phaser.Physics.NINJA || body2.type !== Phaser.Physics.NINJA) { return; } if (body1.aabb && body2.tile) { return body1.aabb.collideAABBVsTile(body2.tile); } if (body1.tile && body2.aabb) { return body2.aabb.collideAABBVsTile(body1.tile); } // circles next } };