mirror of
https://github.com/photonstorm/phaser
synced 2024-12-23 19:43:28 +00:00
286 lines
12 KiB
JavaScript
286 lines
12 KiB
JavaScript
|
/// <reference path="../_definitions.ts" />
|
||
|
/**
|
||
|
* Phaser - Circle
|
||
|
*
|
||
|
* A Circle object is an area defined by its position, as indicated by its center point (x,y) and diameter.
|
||
|
*/
|
||
|
var Phaser;
|
||
|
(function (Phaser) {
|
||
|
var Circle = (function () {
|
||
|
/**
|
||
|
* Creates a new Circle object with the center coordinate specified by the x and y parameters and the diameter specified by the diameter parameter. If you call this function without parameters, a circle with x, y, diameter and radius properties set to 0 is created.
|
||
|
* @class Circle
|
||
|
* @constructor
|
||
|
* @param {Number} [x] The x coordinate of the center of the circle.
|
||
|
* @param {Number} [y] The y coordinate of the center of the circle.
|
||
|
* @param {Number} [diameter] The diameter of the circle.
|
||
|
* @return {Circle} This circle object
|
||
|
**/
|
||
|
function Circle(x, y, diameter) {
|
||
|
if (typeof x === "undefined") { x = 0; }
|
||
|
if (typeof y === "undefined") { y = 0; }
|
||
|
if (typeof diameter === "undefined") { diameter = 0; }
|
||
|
this._diameter = 0;
|
||
|
this._radius = 0;
|
||
|
/**
|
||
|
* The x coordinate of the center of the circle
|
||
|
* @property x
|
||
|
* @type Number
|
||
|
**/
|
||
|
this.x = 0;
|
||
|
/**
|
||
|
* The y coordinate of the center of the circle
|
||
|
* @property y
|
||
|
* @type Number
|
||
|
**/
|
||
|
this.y = 0;
|
||
|
this.setTo(x, y, diameter);
|
||
|
}
|
||
|
Object.defineProperty(Circle.prototype, "diameter", {
|
||
|
get: /**
|
||
|
* The diameter of the circle. The largest distance between any two points on the circle. The same as the radius * 2.
|
||
|
* @method diameter
|
||
|
* @return {Number}
|
||
|
**/
|
||
|
function () {
|
||
|
return this._diameter;
|
||
|
},
|
||
|
set: /**
|
||
|
* The diameter of the circle. The largest distance between any two points on the circle. The same as the radius * 2.
|
||
|
* @method diameter
|
||
|
* @param {Number} The diameter of the circle.
|
||
|
**/
|
||
|
function (value) {
|
||
|
if(value > 0) {
|
||
|
this._diameter = value;
|
||
|
this._radius = value * 0.5;
|
||
|
}
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Object.defineProperty(Circle.prototype, "radius", {
|
||
|
get: /**
|
||
|
* The radius of the circle. The length of a line extending from the center of the circle to any point on the circle itself. The same as half the diameter.
|
||
|
* @method radius
|
||
|
* @return {Number}
|
||
|
**/
|
||
|
function () {
|
||
|
return this._radius;
|
||
|
},
|
||
|
set: /**
|
||
|
* The radius of the circle. The length of a line extending from the center of the circle to any point on the circle itself. The same as half the diameter.
|
||
|
* @method radius
|
||
|
* @param {Number} The radius of the circle.
|
||
|
**/
|
||
|
function (value) {
|
||
|
if(value > 0) {
|
||
|
this._radius = value;
|
||
|
this._diameter = value * 2;
|
||
|
}
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Circle.prototype.circumference = /**
|
||
|
* The circumference of the circle.
|
||
|
* @method circumference
|
||
|
* @return {Number}
|
||
|
**/
|
||
|
function () {
|
||
|
return 2 * (Math.PI * this._radius);
|
||
|
};
|
||
|
Object.defineProperty(Circle.prototype, "bottom", {
|
||
|
get: /**
|
||
|
* The sum of the y and radius properties. Changing the bottom property of a Circle object has no effect on the x and y properties, but does change the diameter.
|
||
|
* @method bottom
|
||
|
* @return {Number}
|
||
|
**/
|
||
|
function () {
|
||
|
return this.y + this._radius;
|
||
|
},
|
||
|
set: /**
|
||
|
* The sum of the y and radius properties. Changing the bottom property of a Circle object has no effect on the x and y properties, but does change the diameter.
|
||
|
* @method bottom
|
||
|
* @param {Number} The value to adjust the height of the circle by.
|
||
|
**/
|
||
|
function (value) {
|
||
|
if(value < this.y) {
|
||
|
this._radius = 0;
|
||
|
this._diameter = 0;
|
||
|
} else {
|
||
|
this.radius = value - this.y;
|
||
|
}
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Object.defineProperty(Circle.prototype, "left", {
|
||
|
get: /**
|
||
|
* The x coordinate of the leftmost point of the circle. Changing the left property of a Circle object has no effect on the x and y properties. However it does affect the diameter, whereas changing the x value does not affect the diameter property.
|
||
|
* @method left
|
||
|
* @return {Number} The x coordinate of the leftmost point of the circle.
|
||
|
**/
|
||
|
function () {
|
||
|
return this.x - this._radius;
|
||
|
},
|
||
|
set: /**
|
||
|
* The x coordinate of the leftmost point of the circle. Changing the left property of a Circle object has no effect on the x and y properties. However it does affect the diameter, whereas changing the x value does not affect the diameter property.
|
||
|
* @method left
|
||
|
* @param {Number} The value to adjust the position of the leftmost point of the circle by.
|
||
|
**/
|
||
|
function (value) {
|
||
|
if(value > this.x) {
|
||
|
this._radius = 0;
|
||
|
this._diameter = 0;
|
||
|
} else {
|
||
|
this.radius = this.x - value;
|
||
|
}
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Object.defineProperty(Circle.prototype, "right", {
|
||
|
get: /**
|
||
|
* The x coordinate of the rightmost point of the circle. Changing the right property of a Circle object has no effect on the x and y properties. However it does affect the diameter, whereas changing the x value does not affect the diameter property.
|
||
|
* @method right
|
||
|
* @return {Number}
|
||
|
**/
|
||
|
function () {
|
||
|
return this.x + this._radius;
|
||
|
},
|
||
|
set: /**
|
||
|
* The x coordinate of the rightmost point of the circle. Changing the right property of a Circle object has no effect on the x and y properties. However it does affect the diameter, whereas changing the x value does not affect the diameter property.
|
||
|
* @method right
|
||
|
* @param {Number} The amount to adjust the diameter of the circle by.
|
||
|
**/
|
||
|
function (value) {
|
||
|
if(value < this.x) {
|
||
|
this._radius = 0;
|
||
|
this._diameter = 0;
|
||
|
} else {
|
||
|
this.radius = value - this.x;
|
||
|
}
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Object.defineProperty(Circle.prototype, "top", {
|
||
|
get: /**
|
||
|
* The sum of the y minus the radius property. Changing the top property of a Circle object has no effect on the x and y properties, but does change the diameter.
|
||
|
* @method bottom
|
||
|
* @return {Number}
|
||
|
**/
|
||
|
function () {
|
||
|
return this.y - this._radius;
|
||
|
},
|
||
|
set: /**
|
||
|
* The sum of the y minus the radius property. Changing the top property of a Circle object has no effect on the x and y properties, but does change the diameter.
|
||
|
* @method bottom
|
||
|
* @param {Number} The amount to adjust the height of the circle by.
|
||
|
**/
|
||
|
function (value) {
|
||
|
if(value > this.y) {
|
||
|
this._radius = 0;
|
||
|
this._diameter = 0;
|
||
|
} else {
|
||
|
this.radius = this.y - value;
|
||
|
}
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Object.defineProperty(Circle.prototype, "area", {
|
||
|
get: /**
|
||
|
* Gets the area of this Circle.
|
||
|
* @method area
|
||
|
* @return {Number} This area of this circle.
|
||
|
**/
|
||
|
function () {
|
||
|
if(this._radius > 0) {
|
||
|
return Math.PI * this._radius * this._radius;
|
||
|
} else {
|
||
|
return 0;
|
||
|
}
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Circle.prototype.setTo = /**
|
||
|
* Sets the members of Circle to the specified values.
|
||
|
* @method setTo
|
||
|
* @param {Number} x The x coordinate of the center of the circle.
|
||
|
* @param {Number} y The y coordinate of the center of the circle.
|
||
|
* @param {Number} diameter The diameter of the circle in pixels.
|
||
|
* @return {Circle} This circle object
|
||
|
**/
|
||
|
function (x, y, diameter) {
|
||
|
this.x = x;
|
||
|
this.y = y;
|
||
|
this._diameter = diameter;
|
||
|
this._radius = diameter * 0.5;
|
||
|
return this;
|
||
|
};
|
||
|
Circle.prototype.copyFrom = /**
|
||
|
* Copies the x, y and diameter properties from any given object to this Circle.
|
||
|
* @method copyFrom
|
||
|
* @param {any} source - The object to copy from.
|
||
|
* @return {Circle} This Circle object.
|
||
|
**/
|
||
|
function (source) {
|
||
|
return this.setTo(source.x, source.y, source.diameter);
|
||
|
};
|
||
|
Object.defineProperty(Circle.prototype, "empty", {
|
||
|
get: /**
|
||
|
* Determines whether or not this Circle object is empty.
|
||
|
* @method empty
|
||
|
* @return {bool} A value of true if the Circle objects diameter is less than or equal to 0; otherwise false.
|
||
|
**/
|
||
|
function () {
|
||
|
return (this._diameter == 0);
|
||
|
},
|
||
|
set: /**
|
||
|
* Sets all of the Circle objects properties to 0. A Circle object is empty if its diameter is less than or equal to 0.
|
||
|
* @method setEmpty
|
||
|
* @return {Circle} This Circle object
|
||
|
**/
|
||
|
function (value) {
|
||
|
this.setTo(0, 0, 0);
|
||
|
},
|
||
|
enumerable: true,
|
||
|
configurable: true
|
||
|
});
|
||
|
Circle.prototype.offset = /**
|
||
|
* Adjusts the location of the Circle object, as determined by its center coordinate, by the specified amounts.
|
||
|
* @method offset
|
||
|
* @param {Number} dx Moves the x value of the Circle object by this amount.
|
||
|
* @param {Number} dy Moves the y value of the Circle object by this amount.
|
||
|
* @return {Circle} This Circle object.
|
||
|
**/
|
||
|
function (dx, dy) {
|
||
|
this.x += dx;
|
||
|
this.y += dy;
|
||
|
return this;
|
||
|
};
|
||
|
Circle.prototype.offsetPoint = /**
|
||
|
* Adjusts the location of the Circle object using a Point object as a parameter. This method is similar to the Circle.offset() method, except that it takes a Point object as a parameter.
|
||
|
* @method offsetPoint
|
||
|
* @param {Point} point A Point object to use to offset this Circle object.
|
||
|
* @return {Circle} This Circle object.
|
||
|
**/
|
||
|
function (point) {
|
||
|
return this.offset(point.x, point.y);
|
||
|
};
|
||
|
Circle.prototype.toString = /**
|
||
|
* Returns a string representation of this object.
|
||
|
* @method toString
|
||
|
* @return {string} a string representation of the instance.
|
||
|
**/
|
||
|
function () {
|
||
|
return "[{Circle (x=" + this.x + " y=" + this.y + " diameter=" + this.diameter + " radius=" + this.radius + ")}]";
|
||
|
};
|
||
|
return Circle;
|
||
|
})();
|
||
|
Phaser.Circle = Circle;
|
||
|
})(Phaser || (Phaser = {}));
|