phaser/v3/src/geom/rectangle/Rectangle.js

214 lines
3.7 KiB
JavaScript
Raw Normal View History

var Class = require('../../utils/Class');
var Random = require('./Random');
2016-12-20 17:07:16 +00:00
// Encapsulates a 2D rectangle defined by its corner point in the top-left
// and its extends in x (width) and y (height)
var Rectangle = new Class({
2016-12-20 17:07:16 +00:00
initialize:
2016-12-20 17:07:16 +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
this.x = x;
2016-12-20 17:07:16 +00:00
this.y = y;
2016-12-20 17:07:16 +00:00
this.width = width;
2016-12-20 17:07:16 +00:00
this.height = height;
},
2016-12-20 17:07:16 +00:00
getRandomPoint: function (point)
{
return Random(this, point);
},
2016-12-28 23:41:02 +00:00
setTo: function (x, y, width, height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
2016-12-20 17:07:16 +00:00
return this;
},
2016-12-20 17:07:16 +00:00
2016-12-28 23:41:02 +00:00
setEmpty: function ()
{
return this.setTo(0, 0, 0, 0);
},
setPosition: function (x, y)
{
if (y === undefined) { y = x; }
2016-12-20 17:07:16 +00:00
this.x = x;
this.y = y;
2016-12-20 17:07:16 +00:00
return this;
},
2016-12-20 17:07:16 +00:00
setSize: function (width, height)
{
if (height === undefined) { height = width; }
2016-12-20 17:07:16 +00:00
this.width = width;
this.height = height;
2016-12-20 17:07:16 +00:00
return this;
2016-12-29 00:17:20 +00:00
},
isEmpty: function ()
{
return (this.width <= 0 || this.height <= 0);
},
// TOP
getLineA: function ()
{
return { x1: this.x, y1: this.y, x2: this.right, y2: this.y };
},
// RIGHT
getLineB: function ()
{
return { x1: this.right, y1: this.y, x2: this.right, y2: this.bottom };
},
// BOTTOM
getLineC: function ()
{
return { x1: this.right, y1: this.bottom, x2: this.x, y2: this.bottom };
},
// LEFT
getLineD: function ()
{
return { x1: this.x, y1: this.bottom, x2: this.x, y2: this.y };
},
2016-12-20 17:07:16 +00:00
left: {
2016-12-20 17:07:16 +00:00
get: function ()
{
return this.x;
2016-12-20 17:07:16 +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-20 17:07:16 +00:00
right: {
2016-12-20 17:07:16 +00:00
get: function ()
{
2016-12-20 17:07:16 +00:00
return this.x + this.width;
},
set: function (value)
{
if (value <= this.x)
{
this.width = 0;
}
else
{
this.width = value - this.x;
}
}
2016-12-20 17:07:16 +00:00
},
top: {
get: function ()
{
return this.y;
2016-12-20 17:07:16 +00:00
},
set: function (value)
{
if (value >= this.bottom)
{
this.height = 0;
this.y = value;
}
else
{
this.height = (this.bottom - value);
}
}
},
bottom: {
get: function ()
{
2016-12-20 17:07:16 +00:00
return this.y + this.height;
},
2016-12-20 17:07:16 +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
},
centerX: {
get: function ()
{
return this.x + (this.width / 2);
},
set: function (value)
{
this.x = value - (this.width / 2);
}
},
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-20 17:07:16 +00:00
module.exports = Rectangle;