mirror of
https://github.com/wassname/phaser.git
synced 2026-07-14 01:10:16 +08:00
Lots of doc updates!
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create a new <code>Bullet</code>.
|
||||
* Warning: Bullet is an experimental object that we don't advise using for now.
|
||||
*
|
||||
* A Bullet is like a stripped-down Sprite, useful for when you just need to get something moving around the screen quickly with little of the extra
|
||||
* features that a Sprite supports.
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* Create a new <code>Sprite</code> object. Sprites are the lifeblood of your game, used for nearly everything visual.
|
||||
* Create a new `Sprite` object. Sprites are the lifeblood of your game, used for nearly everything visual.
|
||||
* At its most basic a Sprite consists of a set of coordinates and a texture that is rendered to the canvas.
|
||||
* They also contain additional properties allowing for physics motion (via Sprite.body), input handling (via Sprite.input),
|
||||
* events (via Sprite.events), animation (via Sprite.animations), camera culling and more. Please see the Examples for use cases.
|
||||
@@ -68,7 +68,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
/**
|
||||
* If you would like the Sprite to have a lifespan once 'born' you can set this to a positive value. Handy for particles, bullets, etc.
|
||||
* The lifespan is decremented by game.time.elapsed each update, once it reaches zero the kill() function is called.
|
||||
* @property {number} lifespan
|
||||
* @property {number} lifespan - The lifespan of the Sprite (in ms) before it will be killed.
|
||||
* @default
|
||||
*/
|
||||
this.lifespan = 0;
|
||||
@@ -148,7 +148,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
* Setting than anchor to 0.5,0.5 means the textures origin is centered
|
||||
* Setting the anchor to 1,1 would mean the textures origin points will be the bottom right
|
||||
*
|
||||
* @property {Phaser.Point} anchor
|
||||
* @property {Phaser.Point} anchor - The anchor around with Sprite rotation and scaling takes place.
|
||||
*/
|
||||
this.anchor = new Phaser.Point();
|
||||
|
||||
@@ -169,7 +169,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
* Should this Sprite be automatically culled if out of range of the camera?
|
||||
* A culled sprite has its renderable property set to 'false'.
|
||||
*
|
||||
* @property {boolean} autoCull
|
||||
* @property {boolean} autoCull - A flag indicating if the Sprite should be automatically camera culled or not.
|
||||
* @default
|
||||
*/
|
||||
this.autoCull = false;
|
||||
@@ -310,7 +310,7 @@ Phaser.Sprite = function (game, x, y, key, frame) {
|
||||
/**
|
||||
* You can crop the Sprites texture by modifying the crop properties. For example crop.width = 50 would set the Sprite to only render 50px wide.
|
||||
* The crop is only applied if you have set Sprite.cropEnabled to true.
|
||||
* @property {Phaser.Rectangle} crop
|
||||
* @property {Phaser.Rectangle} crop - The crop Rectangle applied to the Sprite texture before rendering.
|
||||
* @default
|
||||
*/
|
||||
this.crop = new Phaser.Rectangle(0, 0, this._cache.width, this._cache.height);
|
||||
|
||||
@@ -1,52 +1,174 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2013 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class Phaser.Physics
|
||||
*/
|
||||
Phaser.Physics = {};
|
||||
|
||||
/**
|
||||
* Arcade Physics constructor.
|
||||
*
|
||||
* @class Phaser.Physics.Arcade
|
||||
* @classdesc Arcade Physics Constructor
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* Used by the QuadTree to set the maximum number of objects
|
||||
* @type {number}
|
||||
* @property {number} maxObjects - Used by the QuadTree to set the maximum number of objects per quad.
|
||||
*/
|
||||
this.maxObjects = 10;
|
||||
|
||||
/**
|
||||
* Used by the QuadTree to set the maximum number of levels
|
||||
* @type {number}
|
||||
* @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;
|
||||
this.TILE_OVERLAP = false;
|
||||
|
||||
// this.TILE_OVERLAP = false;
|
||||
|
||||
/**
|
||||
* @property {Phaser.QuadTree} quadTree - The world QuadTree.
|
||||
*/
|
||||
this.quadTree = new Phaser.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 {number} quadTreeID - The QuadTree ID.
|
||||
*/
|
||||
this.quadTreeID = 0;
|
||||
|
||||
// Avoid gc spikes by caching these values for re-use
|
||||
|
||||
/**
|
||||
* @property {Phaser.Rectangle} _bounds1 - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._bounds1 = new Phaser.Rectangle;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Rectangle} _bounds2 - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._bounds2 = new Phaser.Rectangle;
|
||||
|
||||
/**
|
||||
* @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 {number} _mapTiles - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._mapTiles = 0;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
|
||||
@@ -74,11 +196,13 @@ Phaser.Physics.Arcade.prototype = {
|
||||
/**
|
||||
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
|
||||
*
|
||||
* @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 An absolute value cap for the velocity.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#computeVelocity
|
||||
* @param {number} axis - 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} mMax - An absolute value cap for the velocity.
|
||||
* @return {number} The altered Velocity value.
|
||||
*/
|
||||
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
|
||||
@@ -129,6 +253,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by the core game loop.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#preUpdate
|
||||
* @protected
|
||||
*/
|
||||
preUpdate: function () {
|
||||
|
||||
// Clear the tree
|
||||
@@ -140,6 +270,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called automatically by the core game loop.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#postUpdate
|
||||
* @protected
|
||||
*/
|
||||
postUpdate: function () {
|
||||
|
||||
// Clear the tree ready for the next update
|
||||
@@ -148,12 +284,13 @@ Phaser.Physics.Arcade.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks if two Sprite objects intersect.
|
||||
* Checks if two Sprite objects overlap.
|
||||
*
|
||||
* @param object1 The first object to check. Can be an instance of Phaser.Sprite or anything that extends it.
|
||||
* @param object2 The second object to check. Can be an instance of Phaser.Sprite or anything that extends it.
|
||||
* @method Phaser.Physics.Arcade#overlap
|
||||
* @param {Phaser.Sprite} object1 - The first object to check. Can be an instance of Phaser.Sprite or anything that extends it.
|
||||
* @param {Phaser.Sprite} object2 - The second object to check. Can be an instance of Phaser.Sprite or anything that extends it.
|
||||
* @returns {boolean} true if the two objects overlap.
|
||||
**/
|
||||
*/
|
||||
overlap: function (object1, object2) {
|
||||
|
||||
// Only test valid objects
|
||||
@@ -169,14 +306,16 @@ Phaser.Physics.Arcade.prototype = {
|
||||
/**
|
||||
* Checks for collision between two game objects. The objects can be Sprites, Groups, Emitters or Tilemaps.
|
||||
* You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap or Group vs. Tilemap collisions.
|
||||
* The objects are also automatically separated.
|
||||
*
|
||||
* @param object1 The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap
|
||||
* @param object2 The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap
|
||||
* @param collideCallback 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 passed them to Collision.overlap.
|
||||
* @param processCallback A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collideCallback will only be called if processCallback returns true.
|
||||
* @param callbackContext The context in which to run the callbacks.
|
||||
* @returns {boolean} true if any collisions were detected, otherwise false.
|
||||
**/
|
||||
* @method Phaser.Physics.Arcade#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} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap
|
||||
* @param {function} [collideCallback=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 passed them to Collision.overlap.
|
||||
* @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 collideCallback will only be called if processCallback returns true.
|
||||
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
||||
* @returns {number} The number of collisions that were processed.
|
||||
*/
|
||||
collide: function (object1, object2, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
collideCallback = collideCallback || null;
|
||||
@@ -257,6 +396,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collideSpriteVsTilemapLayer
|
||||
* @private
|
||||
*/
|
||||
collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
this._mapData = tilemapLayer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true);
|
||||
@@ -297,6 +442,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer
|
||||
* @private
|
||||
*/
|
||||
collideGroupVsTilemapLayer: function (group, tilemapLayer, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
if (group.length == 0)
|
||||
@@ -321,6 +472,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collideSpriteVsSprite
|
||||
* @private
|
||||
*/
|
||||
collideSpriteVsSprite: function (sprite1, sprite2, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
this.separate(sprite1.body, sprite2.body);
|
||||
@@ -353,6 +510,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collideSpriteVsGroup
|
||||
* @private
|
||||
*/
|
||||
collideSpriteVsGroup: function (sprite, group, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
if (group.length == 0)
|
||||
@@ -389,6 +552,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#collideGroupVsGroup
|
||||
* @private
|
||||
*/
|
||||
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext) {
|
||||
|
||||
if (group1.length == 0 || group2.length == 0)
|
||||
@@ -413,12 +582,13 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The core separation function to separate two physics bodies.
|
||||
* @param body1 The first Sprite.Body to separate
|
||||
* @param body2 The second Sprite.Body to separate
|
||||
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
||||
*/
|
||||
/**
|
||||
* 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.
|
||||
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
||||
*/
|
||||
separate: function (body1, body2) {
|
||||
|
||||
this._result = (this.separateX(body1, body2) || this.separateY(body1, body2));
|
||||
@@ -426,11 +596,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Separates the two physics bodies on their X axis
|
||||
* @param body1 The first Sprite.Body to separate
|
||||
* @param body2 The second Sprite.Body to separate
|
||||
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
|
||||
*/
|
||||
* The core separation function to separate two physics bodies on the x axis.
|
||||
* @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.
|
||||
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
||||
*/
|
||||
separateX: function (body1, body2) {
|
||||
|
||||
// Can't separate two immovable bodies
|
||||
@@ -533,11 +704,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Separates the two physics bodies on their Y axis
|
||||
* @param body1 The first Sprite.Body to separate
|
||||
* @param body2 The second Sprite.Body to separate
|
||||
* @returns {boolean} Whether the bodys in fact touched and were separated along the Y axis.
|
||||
*/
|
||||
* The core separation function to separate two physics bodies on the y axis.
|
||||
* @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.
|
||||
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
||||
*/
|
||||
separateY: function (body1, body2) {
|
||||
|
||||
// Can't separate two immovable or non-existing bodys
|
||||
@@ -652,12 +824,13 @@ Phaser.Physics.Arcade.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* The core Collision separation function used by Collision.overlap.
|
||||
* @param object1 The first GameObject to separate
|
||||
* @param object2 The second GameObject to separate
|
||||
* @returns {boolean} Returns true if the objects were separated, otherwise false.
|
||||
*/
|
||||
/**
|
||||
* The core separation function to separate a physics body and a tile.
|
||||
* @method Phaser.Physics.Arcade#separateTile
|
||||
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
|
||||
* @param {Phaser.Tile} tile - The tile to collide against.
|
||||
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
||||
*/
|
||||
separateTile: function (body, tile) {
|
||||
|
||||
this._result = (this.separateTileX(body, tile, true) || this.separateTileY(body, tile, true));
|
||||
@@ -665,11 +838,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Separates the two objects on their x axis
|
||||
* @param object The GameObject to separate
|
||||
* @param tile The Tile to separate
|
||||
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
|
||||
*/
|
||||
* The core separation function to separate a physics body and a tile on the x axis.
|
||||
* @method Phaser.Physics.Arcade#separateTileX
|
||||
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
|
||||
* @param {Phaser.Tile} tile - The tile to collide against.
|
||||
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
||||
*/
|
||||
separateTileX: function (body, tile, separate) {
|
||||
|
||||
// Can't separate two immovable objects (tiles are always immovable)
|
||||
@@ -748,11 +922,12 @@ Phaser.Physics.Arcade.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Separates the two objects on their x axis
|
||||
* @param object The GameObject to separate
|
||||
* @param tile The Tile to separate
|
||||
* @returns {boolean} Whether the objects in fact touched and were separated along the X axis.
|
||||
*/
|
||||
* The core separation function to separate a physics body and a tile on the x axis.
|
||||
* @method Phaser.Physics.Arcade#separateTileY
|
||||
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
|
||||
* @param {Phaser.Tile} tile - The tile to collide against.
|
||||
* @returns {boolean} Returns true if the bodies were separated, otherwise false.
|
||||
*/
|
||||
separateTileY: function (body, tile, separate) {
|
||||
|
||||
// Can't separate two immovable objects (tiles are always immovable)
|
||||
|
||||
+316
-11
@@ -1,84 +1,318 @@
|
||||
/**
|
||||
* @author Richard Davey <rich@photonstorm.com>
|
||||
* @copyright 2013 Photon Storm Ltd.
|
||||
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
||||
*/
|
||||
|
||||
/**
|
||||
* The Physics Body is linked to a single Sprite. All physics operations should be performed against the body rather than
|
||||
* the Sprite itself. For example you can set the velocity, acceleration, bounce values etc all on the Body.
|
||||
*
|
||||
* @class Phaser.Physics.Arcade.Body
|
||||
* @classdesc Arcade Physics Body Constructor
|
||||
* @constructor
|
||||
* @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
|
||||
*/
|
||||
Phaser.Physics.Arcade.Body = function (sprite) {
|
||||
|
||||
/**
|
||||
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
|
||||
*/
|
||||
this.sprite = sprite;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
*/
|
||||
this.game = sprite.game;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
|
||||
*/
|
||||
this.offset = new Phaser.Point;
|
||||
|
||||
/**
|
||||
* @property {number} x - The x position of the physics body.
|
||||
* @readonly
|
||||
*/
|
||||
this.x = sprite.x;
|
||||
|
||||
/**
|
||||
* @property {number} y - The y position of the physics body.
|
||||
* @readonly
|
||||
*/
|
||||
this.y = sprite.y;
|
||||
|
||||
/**
|
||||
* @property {number} preX - The previous x position of the physics body.
|
||||
* @readonly
|
||||
*/
|
||||
this.preX = sprite.x;
|
||||
|
||||
/**
|
||||
* @property {number} preY - The previous y position of the physics body.
|
||||
* @readonly
|
||||
*/
|
||||
this.preY = sprite.y;
|
||||
|
||||
/**
|
||||
* @property {number} preRotation - The previous rotation of the physics body.
|
||||
* @readonly
|
||||
*/
|
||||
this.preRotation = sprite.angle;
|
||||
|
||||
/**
|
||||
* @property {number} screenX - The x position of the physics body translated to screen space.
|
||||
* @readonly
|
||||
*/
|
||||
this.screenX = sprite.x;
|
||||
|
||||
/**
|
||||
* @property {number} screenY - The y position of the physics body translated to screen space.
|
||||
* @readonly
|
||||
*/
|
||||
this.screenY = sprite.y;
|
||||
|
||||
// un-scaled original size
|
||||
/**
|
||||
* @property {number} sourceWidth - The un-scaled original size.
|
||||
* @readonly
|
||||
*/
|
||||
this.sourceWidth = sprite.currentFrame.sourceSizeW;
|
||||
|
||||
/**
|
||||
* @property {number} sourceHeight - The un-scaled original size.
|
||||
* @readonly
|
||||
*/
|
||||
this.sourceHeight = sprite.currentFrame.sourceSizeH;
|
||||
|
||||
// calculated (scaled) size
|
||||
/**
|
||||
* @property {number} width - The calculated width of the physics body.
|
||||
*/
|
||||
this.width = sprite.currentFrame.sourceSizeW;
|
||||
|
||||
/**
|
||||
* @property .numInternal ID cache
|
||||
*/
|
||||
this.height = sprite.currentFrame.sourceSizeH;
|
||||
|
||||
/**
|
||||
* @property {number} halfWidth - The calculated width / 2 of the physics body.
|
||||
*/
|
||||
this.halfWidth = Math.floor(sprite.currentFrame.sourceSizeW / 2);
|
||||
|
||||
/**
|
||||
* @property {number} halfHeight - The calculated height / 2 of the physics body.
|
||||
*/
|
||||
this.halfHeight = Math.floor(sprite.currentFrame.sourceSizeH / 2);
|
||||
|
||||
// Scale value cache
|
||||
/**
|
||||
* @property {number} _sx - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._sx = sprite.scale.x;
|
||||
|
||||
/**
|
||||
* @property {number} _sy - Internal cache var.
|
||||
* @private
|
||||
*/
|
||||
this._sy = sprite.scale.y;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} velocity - The velocity in pixels per second sq. of the Body.
|
||||
*/
|
||||
this.velocity = new Phaser.Point;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} acceleration - The velocity in pixels per second sq. of the Body.
|
||||
*/
|
||||
this.acceleration = new Phaser.Point;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} drag - The drag applied to the motion of the Body.
|
||||
*/
|
||||
this.drag = new Phaser.Point;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} gravity - A private Gravity setting for the Body.
|
||||
*/
|
||||
this.gravity = new Phaser.Point;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} bounce - The elasticitiy of the Body when colliding. bounce.x/y = 1 means full rebound, bounce.x/y = 0.5 means 50% rebound velocity.
|
||||
*/
|
||||
this.bounce = new Phaser.Point;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Point} maxVelocity - The maximum velocity in pixels per second sq. that the Body can reach.
|
||||
* @default
|
||||
*/
|
||||
this.maxVelocity = new Phaser.Point(10000, 10000);
|
||||
|
||||
/**
|
||||
* @property {number} angularVelocity - The angular velocity in pixels per second sq. of the Body.
|
||||
* @default
|
||||
*/
|
||||
this.angularVelocity = 0;
|
||||
|
||||
/**
|
||||
* @property {number} angularAcceleration - The angular acceleration in pixels per second sq. of the Body.
|
||||
* @default
|
||||
*/
|
||||
this.angularAcceleration = 0;
|
||||
|
||||
/**
|
||||
* @property {number} angularDrag - The angular drag applied to the rotation of the Body.
|
||||
* @default
|
||||
*/
|
||||
this.angularDrag = 0;
|
||||
|
||||
/**
|
||||
* @property {number} maxAngular - The maximum angular velocity in pixels per second sq. that the Body can reach.
|
||||
* @default
|
||||
*/
|
||||
this.maxAngular = 1000;
|
||||
|
||||
/**
|
||||
* @property {number} mass - The mass of the Body.
|
||||
* @default
|
||||
*/
|
||||
this.mass = 1;
|
||||
|
||||
/**
|
||||
* @property {boolean} skipQuadTree - If the Body is an irregular shape you can set this to true to avoid it being added to the World quad tree.
|
||||
* @default
|
||||
*/
|
||||
this.skipQuadTree = false;
|
||||
|
||||
/**
|
||||
* @property {Array} quadTreeIDs - Internal ID cache.
|
||||
* @protected
|
||||
*/
|
||||
this.quadTreeIDs = [];
|
||||
|
||||
/**
|
||||
* @property {number} quadTreeIndex - Internal ID cache.
|
||||
* @protected
|
||||
*/
|
||||
this.quadTreeIndex = -1;
|
||||
|
||||
// Allow collision
|
||||
|
||||
/**
|
||||
* Set the allowCollision properties to control which directions collision is processed for this Body.
|
||||
* For example allowCollision.up = false means it won't collide when the collision happened while moving up.
|
||||
* @property {object} allowCollision - An object containing allowed collision.
|
||||
*/
|
||||
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
|
||||
|
||||
/**
|
||||
* This object is populated with boolean values when the Body collides with another.
|
||||
* touching.up = true means the collision happened to the top of this Body for example.
|
||||
* @property {object} touching - An object containing touching results.
|
||||
*/
|
||||
this.touching = { none: true, up: false, down: false, left: false, right: false };
|
||||
|
||||
/**
|
||||
* This object is populated with previous touching values from the bodies previous collision.
|
||||
* @property {object} wasTouching - An object containing previous touching results.
|
||||
*/
|
||||
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
|
||||
|
||||
/**
|
||||
* @property {number} facing - A const reference to the direction the Body is traveling or facing.
|
||||
* @default
|
||||
*/
|
||||
this.facing = Phaser.NONE;
|
||||
|
||||
/**
|
||||
* @property {boolean} immovable - An immovable Body will not receive any impacts from other bodies.
|
||||
* @default
|
||||
*/
|
||||
this.immovable = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} moves - Set to true to allow the Physics system to move this Body, other false to move it manually.
|
||||
* @default
|
||||
*/
|
||||
this.moves = true;
|
||||
|
||||
/**
|
||||
* @property {number} rotation - The amount the Body is rotated.
|
||||
* @default
|
||||
*/
|
||||
this.rotation = 0;
|
||||
|
||||
/**
|
||||
* @property {boolean} allowRotation - Allow this Body to be rotated? (via angularVelocity, etc)
|
||||
* @default
|
||||
*/
|
||||
this.allowRotation = true;
|
||||
|
||||
/**
|
||||
* @property {boolean} allowGravity - Allow this Body to be influenced by the global Gravity?
|
||||
* @default
|
||||
*/
|
||||
this.allowGravity = true;
|
||||
|
||||
// These two flags allow you to disable the custom separation that takes place
|
||||
// Used in combination with your own collision processHandler you can create whatever
|
||||
// type of collision response you need.
|
||||
/**
|
||||
* This flag allows you to disable the custom x separation that takes place by Physics.Arcade.separate.
|
||||
* Used in combination with your own collision processHandler you can create whatever type of collision response you need.
|
||||
* @property {boolean} customSeparateX - Use a custom separation system or the built-in one?
|
||||
* @default
|
||||
*/
|
||||
this.customSeparateX = false;
|
||||
|
||||
/**
|
||||
* This flag allows you to disable the custom y separation that takes place by Physics.Arcade.separate.
|
||||
* Used in combination with your own collision processHandler you can create whatever type of collision response you need.
|
||||
* @property {boolean} customSeparateY - Use a custom separation system or the built-in one?
|
||||
* @default
|
||||
*/
|
||||
this.customSeparateY = false;
|
||||
|
||||
// When this body collides with another the amount of overlap is stored in here
|
||||
// These values are useful if you want to provide your own custom separation logic.
|
||||
/**
|
||||
* When this body collides with another, the amount of overlap is stored here.
|
||||
* @property {number} overlapX - The amount of horizontal overlap during the collision.
|
||||
*/
|
||||
this.overlapX = 0;
|
||||
|
||||
/**
|
||||
* When this body collides with another, the amount of overlap is stored here.
|
||||
* @property {number} overlapY - The amount of vertical overlap during the collision.
|
||||
*/
|
||||
this.overlapY = 0;
|
||||
|
||||
/**
|
||||
* @property {Phaser.Rectangle} hullX - The dynamically calculated hull used during collision.
|
||||
*/
|
||||
this.hullX = new Phaser.Rectangle();
|
||||
|
||||
/**
|
||||
* @property {Phaser.Rectangle} hullY - The dynamically calculated hull used during collision.
|
||||
*/
|
||||
this.hullY = new Phaser.Rectangle();
|
||||
|
||||
// If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true
|
||||
/**
|
||||
* If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true.
|
||||
* @property {boolean} embedded - Body embed value.
|
||||
*/
|
||||
this.embedded = false;
|
||||
|
||||
/**
|
||||
* A Body can be set to collide against the World bounds automatically and rebound back into the World if this is set to true. Otherwise it will leave the World.
|
||||
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
|
||||
*/
|
||||
this.collideWorldBounds = false;
|
||||
|
||||
};
|
||||
|
||||
Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#updateBounds
|
||||
* @protected
|
||||
*/
|
||||
updateBounds: function (centerX, centerY, scaleX, scaleY) {
|
||||
|
||||
if (scaleX != this._sx || scaleY != this._sy)
|
||||
@@ -93,6 +327,12 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#preUpdate
|
||||
* @protected
|
||||
*/
|
||||
preUpdate: function () {
|
||||
|
||||
// Store and reset collision flags
|
||||
@@ -131,8 +371,7 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
this.checkWorldBounds();
|
||||
}
|
||||
|
||||
this.updateHulls();
|
||||
}
|
||||
this.updateHulls();Array }
|
||||
|
||||
if (this.skipQuadTree == false && this.allowCollision.none == false && this.sprite.visible && this.sprite.alive)
|
||||
{
|
||||
@@ -143,6 +382,12 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#postUpdate
|
||||
* @protected
|
||||
*/
|
||||
postUpdate: function () {
|
||||
|
||||
// Calculate forward-facing edge
|
||||
@@ -183,6 +428,12 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#updateHulls
|
||||
* @protected
|
||||
*/
|
||||
updateHulls: function () {
|
||||
|
||||
this.hullX.setTo(this.x, this.preY, this.width, this.height);
|
||||
@@ -190,6 +441,12 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Internal method.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#checkWorldBounds
|
||||
* @protected
|
||||
*/
|
||||
checkWorldBounds: function () {
|
||||
|
||||
if (this.x < this.game.world.bounds.x)
|
||||
@@ -216,6 +473,17 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* You can modify the size of the physics Body to be any dimension you need.
|
||||
* So it could be smaller or larger than the parent Sprite. You can also control the x and y offset, which
|
||||
* is the position of the Body relative to the top-left of the Sprite.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#setSize
|
||||
* @param {number} width - The width of the Body.
|
||||
* @param {number} height - The height of the Body.
|
||||
* @param {number} offsetX - The X offset of the Body from the Sprite position.
|
||||
* @param {number} offsetY - The Y offset of the Body from the Sprite position.
|
||||
*/
|
||||
setSize: function (width, height, offsetX, offsetY) {
|
||||
|
||||
offsetX = offsetX || this.offset.x;
|
||||
@@ -231,6 +499,11 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Resets all Body values (velocity, acceleration, rotation, etc)
|
||||
*
|
||||
* @method Phaser.Physics.Arcade#reset
|
||||
*/
|
||||
reset: function () {
|
||||
|
||||
this.velocity.setTo(0, 0);
|
||||
@@ -248,18 +521,42 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the absolute delta x value.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.Body#deltaAbsX
|
||||
* @return {number} The absolute delta value.
|
||||
*/
|
||||
deltaAbsX: function () {
|
||||
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the absolute delta y value.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.Body#deltaAbsY
|
||||
* @return {number} The absolute delta value.
|
||||
*/
|
||||
deltaAbsY: function () {
|
||||
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the delta x value.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.Body#deltaX
|
||||
* @return {number} The delta value.
|
||||
*/
|
||||
deltaX: function () {
|
||||
return this.x - this.preX;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the delta y value.
|
||||
*
|
||||
* @method Phaser.Physics.Arcade.Body#deltaY
|
||||
* @return {number} The delta value.
|
||||
*/
|
||||
deltaY: function () {
|
||||
return this.y - this.preY;
|
||||
},
|
||||
@@ -270,6 +567,10 @@ Phaser.Physics.Arcade.Body.prototype = {
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Arcade.Body#bottom
|
||||
* @property {number} bottom - The bottom value of this Body (same as Body.y + Body.height)
|
||||
*/
|
||||
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
|
||||
|
||||
/**
|
||||
@@ -301,6 +602,10 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
|
||||
|
||||
});
|
||||
|
||||
/**
|
||||
* @name Phaser.Physics.Arcade.Body#right
|
||||
* @property {number} right - The right value of this Body (same as Body.x + Body.width)
|
||||
*/
|
||||
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user