2017-06-30 14:47:51 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-10-29 21:46:23 +00:00
|
|
|
var Contains = require('./Contains');
|
2017-10-26 15:39:15 +00:00
|
|
|
var GetPoint = require('./GetPoint');
|
|
|
|
var GetPoints = require('./GetPoints');
|
2017-09-25 17:10:01 +00:00
|
|
|
var Random = require('./Random');
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* Encapsulates a 2D rectangle defined by its corner point in the top-left and its extends in x (width) and y (height)
|
|
|
|
*
|
|
|
|
* @class Rectangle
|
|
|
|
* @memberOf Phaser.Geom
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [x] - [description]
|
|
|
|
* @param {number} [y] - [description]
|
|
|
|
* @param {number} [width] - [description]
|
|
|
|
* @param {number} [height] - [description]
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
var Rectangle = new Class({
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
function Rectangle (x, y, width, height)
|
|
|
|
{
|
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
if (width === undefined) { width = 0; }
|
|
|
|
if (height === undefined) { height = 0; }
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @name Phaser.Geom.Rectangle#x
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.x = x;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @name Phaser.Geom.Rectangle#y
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.y = y;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @name Phaser.Geom.Rectangle#width
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.width = width;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @name Phaser.Geom.Rectangle#height
|
|
|
|
* @type {number}
|
|
|
|
* @default 0
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
this.height = height;
|
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#contains
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} x - [description]
|
|
|
|
* @param {[type]} y - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-10-29 21:46:23 +00:00
|
|
|
contains: function (x, y)
|
|
|
|
{
|
|
|
|
return Contains(this, x, y);
|
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#getPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} position - [description]
|
|
|
|
* @param {[type]} output - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-10-26 15:39:15 +00:00
|
|
|
getPoint: function (position, output)
|
|
|
|
{
|
|
|
|
return GetPoint(this, position, output);
|
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#getPoints
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} quantity - [description]
|
|
|
|
* @param {[type]} stepRate - [description]
|
|
|
|
* @param {[type]} output - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-10-26 15:39:15 +00:00
|
|
|
getPoints: function (quantity, stepRate, output)
|
|
|
|
{
|
|
|
|
return GetPoints(this, quantity, stepRate, output);
|
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#getRandomPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} point - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-09-25 17:10:01 +00:00
|
|
|
getRandomPoint: function (point)
|
|
|
|
{
|
|
|
|
return Random(this, point);
|
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#setTo
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} x - [description]
|
|
|
|
* @param {[type]} y - [description]
|
|
|
|
* @param {[type]} width - [description]
|
|
|
|
* @param {[type]} height - [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @return {Phaser.Geom.Rectangle} This Rectangle object.
|
2018-01-26 13:14:41 +00:00
|
|
|
*/
|
2016-12-28 23:41:02 +00:00
|
|
|
setTo: function (x, y, width, height)
|
2016-12-22 01:32:21 +00:00
|
|
|
{
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
return this;
|
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#setEmpty
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @return {Phaser.Geom.Rectangle} This Rectangle object.
|
2018-01-26 13:14:41 +00:00
|
|
|
*/
|
2016-12-28 23:41:02 +00:00
|
|
|
setEmpty: function ()
|
|
|
|
{
|
|
|
|
return this.setTo(0, 0, 0, 0);
|
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#setPosition
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} x - [description]
|
|
|
|
* @param {[type]} y - [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @return {Phaser.Geom.Rectangle} This Rectangle object.
|
2018-01-26 13:14:41 +00:00
|
|
|
*/
|
2016-12-22 01:32:21 +00:00
|
|
|
setPosition: function (x, y)
|
|
|
|
{
|
|
|
|
if (y === undefined) { y = x; }
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
return this;
|
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#setSize
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} width - [description]
|
|
|
|
* @param {[type]} height - [description]
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @return {Phaser.Geom.Rectangle} This Rectangle object.
|
2018-01-26 13:14:41 +00:00
|
|
|
*/
|
2016-12-22 01:32:21 +00:00
|
|
|
setSize: function (width, height)
|
|
|
|
{
|
|
|
|
if (height === undefined) { height = width; }
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
return this;
|
2016-12-29 00:17:20 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#isEmpty
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-07 15:27:21 +00:00
|
|
|
* @return {boolean} [description]
|
2018-01-26 13:14:41 +00:00
|
|
|
*/
|
2016-12-29 00:17:20 +00:00
|
|
|
isEmpty: function ()
|
|
|
|
{
|
|
|
|
return (this.width <= 0 || this.height <= 0);
|
2017-04-04 00:04:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// TOP
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#getLineA
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-04-04 00:04:05 +00:00
|
|
|
getLineA: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.x, y1: this.y, x2: this.right, y2: this.y };
|
|
|
|
},
|
|
|
|
|
|
|
|
// RIGHT
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#getLineB
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-04-04 00:04:05 +00:00
|
|
|
getLineB: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.right, y1: this.y, x2: this.right, y2: this.bottom };
|
|
|
|
},
|
|
|
|
|
|
|
|
// BOTTOM
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#getLineC
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-04-04 00:04:05 +00:00
|
|
|
getLineC: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.right, y1: this.bottom, x2: this.x, y2: this.bottom };
|
|
|
|
},
|
|
|
|
|
|
|
|
// LEFT
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Geom.Rectangle#getLineD
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
2017-04-04 00:04:05 +00:00
|
|
|
getLineD: function ()
|
|
|
|
{
|
|
|
|
return { x1: this.x, y1: this.bottom, x2: this.x, y2: this.y };
|
2017-06-30 14:47:51 +00:00
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Geom.Rectangle#left
|
2018-02-07 15:27:21 +00:00
|
|
|
* @type {number}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-22 01:32:21 +00:00
|
|
|
left: {
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.x;
|
2016-12-20 17:07:16 +00:00
|
|
|
},
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value >= this.right)
|
|
|
|
{
|
|
|
|
this.width = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.width = this.right - value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.x = value;
|
|
|
|
}
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Geom.Rectangle#right
|
2018-02-07 15:27:21 +00:00
|
|
|
* @type {number}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-22 01:32:21 +00:00
|
|
|
right: {
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
get: function ()
|
|
|
|
{
|
2016-12-20 17:07:16 +00:00
|
|
|
return this.x + this.width;
|
|
|
|
},
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value <= this.x)
|
|
|
|
{
|
|
|
|
this.width = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.width = value - this.x;
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Geom.Rectangle#top
|
2018-02-07 15:27:21 +00:00
|
|
|
* @type {number}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-22 01:32:21 +00:00
|
|
|
top: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.y;
|
2016-12-20 17:07:16 +00:00
|
|
|
},
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value >= this.bottom)
|
|
|
|
{
|
|
|
|
this.height = 0;
|
|
|
|
this.y = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.height = (this.bottom - value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Geom.Rectangle#bottom
|
2018-02-07 15:27:21 +00:00
|
|
|
* @type {number}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-22 01:32:21 +00:00
|
|
|
bottom: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
2016-12-20 17:07:16 +00:00
|
|
|
return this.y + this.height;
|
2016-12-22 01:32:21 +00:00
|
|
|
},
|
2016-12-20 17:07:16 +00:00
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
if (value <= this.y)
|
|
|
|
{
|
|
|
|
this.height = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.height = value - this.y;
|
|
|
|
}
|
2016-12-20 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 14:05:06 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Geom.Rectangle#centerX
|
2018-02-07 15:27:21 +00:00
|
|
|
* @type {number}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-03-29 14:05:06 +00:00
|
|
|
centerX: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.x + (this.width / 2);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.x = value - (this.width / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2018-01-26 13:14:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Geom.Rectangle#centerY
|
2018-02-07 15:27:21 +00:00
|
|
|
* @type {number}
|
2018-01-26 13:14:41 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-03-29 14:05:06 +00:00
|
|
|
centerY: {
|
|
|
|
|
|
|
|
get: function ()
|
|
|
|
{
|
|
|
|
return this.y + (this.height / 2);
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this.y = value - (this.height / 2);
|
|
|
|
}
|
|
|
|
|
2016-12-20 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-22 01:32:21 +00:00
|
|
|
});
|
2016-12-20 17:07:16 +00:00
|
|
|
|
|
|
|
module.exports = Rectangle;
|