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

157 lines
2.6 KiB
JavaScript
Raw Normal View History

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 = function (x, y, width, height)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (width === undefined) { width = 0; }
if (height === undefined) { height = 0; }
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
Rectangle.prototype.constructor = Rectangle;
2016-12-20 17:07:16 +00:00
Rectangle.prototype = {
2016-12-20 17:07:16 +00:00
set: 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
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-20 17:07:16 +00:00
};
2016-12-20 17:07:16 +00:00
Object.defineProperties(Rectangle.prototype, {
2016-12-20 17:07:16 +00:00
left: {
2016-12-20 17:07:16 +00:00
enumerable: true,
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
enumerable: true,
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: {
enumerable: true,
2016-12-20 17:07:16 +00:00
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: {
enumerable: true,
2016-12-20 17:07:16 +00:00
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
}
}
});
2016-12-20 17:07:16 +00:00
module.exports = Rectangle;