2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-09-29 17:54:51 +00:00
|
|
|
var Rectangle = require('../../geom/rectangle/Rectangle');
|
2017-11-01 23:56:36 +00:00
|
|
|
var RotateAround = require('../../math/RotateAround');
|
|
|
|
var Vector2 = require('../../math/Vector2');
|
2017-09-29 17:54:51 +00:00
|
|
|
|
2018-02-01 00:04:45 +00:00
|
|
|
/**
|
|
|
|
* Provides methods used for obtaining the bounds of a Game Object.
|
|
|
|
* Should be applied as a mixin and not used directly.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Components.GetBounds
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
|
2017-02-23 17:15:41 +00:00
|
|
|
var GetBounds = {
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2018-02-01 00:04:45 +00:00
|
|
|
/**
|
|
|
|
* Gets the center coordinate of this Game Object, regardless of origin.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.GetBounds#getCenter
|
2018-02-01 00:04:45 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Math.Vector2|object} [output] - An object to store the values in. If not provided a new Vector2 will be created.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2|object} The values stored in the output object.
|
|
|
|
*/
|
2017-11-11 03:51:28 +00:00
|
|
|
getCenter: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = this.x - (this.displayWidth * this.originX) + (this.displayWidth / 2);
|
|
|
|
output.y = this.y - (this.displayHeight * this.originY) + (this.displayHeight / 2);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:04:45 +00:00
|
|
|
/**
|
|
|
|
* Gets the top-left corner coordinate of this Game Object, regardless of origin.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.GetBounds#getTopLeft
|
2018-02-01 00:04:45 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Math.Vector2|object} [output] - An object to store the values in. If not provided a new Vector2 will be created.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2|object} The values stored in the output object.
|
|
|
|
*/
|
2017-11-01 23:56:36 +00:00
|
|
|
getTopLeft: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = this.x - (this.displayWidth * this.originX);
|
|
|
|
output.y = this.y - (this.displayHeight * this.originY);
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:04:45 +00:00
|
|
|
/**
|
|
|
|
* Gets the top-right corner coordinate of this Game Object, regardless of origin.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.GetBounds#getTopRight
|
2018-02-01 00:04:45 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Math.Vector2|object} [output] - An object to store the values in. If not provided a new Vector2 will be created.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2|object} The values stored in the output object.
|
|
|
|
*/
|
2017-11-01 23:56:36 +00:00
|
|
|
getTopRight: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
|
|
|
|
output.y = this.y - (this.displayHeight * this.originY);
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:04:45 +00:00
|
|
|
/**
|
|
|
|
* Gets the bottom-left corner coordinate of this Game Object, regardless of origin.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.GetBounds#getBottomLeft
|
2018-02-01 00:04:45 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Math.Vector2|object} [output] - An object to store the values in. If not provided a new Vector2 will be created.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2|object} The values stored in the output object.
|
|
|
|
*/
|
2017-11-01 23:56:36 +00:00
|
|
|
getBottomLeft: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = this.x - (this.displayWidth * this.originX);
|
|
|
|
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:04:45 +00:00
|
|
|
/**
|
|
|
|
* Gets the bottom-right corner coordinate of this Game Object, regardless of origin.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.GetBounds#getBottomRight
|
2018-02-01 00:04:45 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Math.Vector2|object} [output] - An object to store the values in. If not provided a new Vector2 will be created.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Math.Vector2|object} The values stored in the output object.
|
|
|
|
*/
|
2017-11-01 23:56:36 +00:00
|
|
|
getBottomRight: function (output)
|
|
|
|
{
|
|
|
|
if (output === undefined) { output = new Vector2(); }
|
|
|
|
|
|
|
|
output.x = (this.x - (this.displayWidth * this.originX)) + this.displayWidth;
|
|
|
|
output.y = (this.y - (this.displayHeight * this.originY)) + this.displayHeight;
|
|
|
|
|
|
|
|
if (this.rotation !== 0)
|
|
|
|
{
|
|
|
|
RotateAround(output, this.x, this.y, this.rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-02-01 00:04:45 +00:00
|
|
|
/**
|
|
|
|
* Gets the bounds of this Game Object, regardless of origin.
|
|
|
|
* The values are stored and returned in a Rectangle, or Rectangle-like, object.
|
|
|
|
*
|
2018-02-01 01:36:52 +00:00
|
|
|
* @method Phaser.GameObjects.Components.GetBounds#getBounds
|
2018-02-01 00:04:45 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Geom.Rectangle|object} [output] - An object to store the values in. If not provided a new Rectangle will be created.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Geom.Rectangle|object} The values stored in the output object.
|
|
|
|
*/
|
2017-02-24 01:58:27 +00:00
|
|
|
getBounds: function (output)
|
2016-12-07 01:40:56 +00:00
|
|
|
{
|
2017-09-29 17:54:51 +00:00
|
|
|
if (output === undefined) { output = new Rectangle(); }
|
2016-12-07 01:40:56 +00:00
|
|
|
|
2017-11-02 00:30:04 +00:00
|
|
|
// We can use the output object to temporarily store the x/y coords in:
|
2017-11-01 23:56:36 +00:00
|
|
|
|
2017-11-02 00:30:04 +00:00
|
|
|
this.getTopLeft(output);
|
2017-11-01 23:56:36 +00:00
|
|
|
|
2017-11-02 00:30:04 +00:00
|
|
|
var TLx = output.x;
|
|
|
|
var TLy = output.y;
|
|
|
|
|
|
|
|
this.getTopRight(output);
|
|
|
|
|
|
|
|
var TRx = output.x;
|
|
|
|
var TRy = output.y;
|
|
|
|
|
|
|
|
this.getBottomLeft(output);
|
|
|
|
|
|
|
|
var BLx = output.x;
|
|
|
|
var BLy = output.y;
|
|
|
|
|
|
|
|
this.getBottomRight(output);
|
|
|
|
|
|
|
|
var BRx = output.x;
|
|
|
|
var BRy = output.y;
|
|
|
|
|
|
|
|
output.x = Math.min(TLx, TRx, BLx, BRx);
|
|
|
|
output.y = Math.min(TLy, TRy, BLy, BRy);
|
|
|
|
output.width = Math.max(TLx, TRx, BLx, BRx) - output.x;
|
|
|
|
output.height = Math.max(TLy, TRy, BLy, BRy) - output.y;
|
|
|
|
|
2017-02-24 01:58:27 +00:00
|
|
|
return output;
|
2017-02-23 17:15:41 +00:00
|
|
|
}
|
2018-02-01 01:36:52 +00:00
|
|
|
|
2016-12-07 01:40:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = GetBounds;
|