mirror of
https://github.com/wassname/phaser.git
synced 2026-09-09 11:28:47 +08:00
Refactoring ready for 1.0 release.
This commit is contained in:
@@ -0,0 +1,307 @@
|
||||
/// <reference path="../Game.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Circle
|
||||
*
|
||||
* A Circle object is an area defined by its position, as indicated by its center point (x,y) and diameter.
|
||||
*/
|
||||
|
||||
module Phaser {
|
||||
|
||||
export class Circle {
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
constructor(x: number = 0, y: number = 0, diameter: number = 0) {
|
||||
|
||||
this.setTo(x, y, diameter);
|
||||
|
||||
}
|
||||
|
||||
private _diameter: number = 0;
|
||||
private _radius: number = 0;
|
||||
private _pos: Vector2;
|
||||
|
||||
/**
|
||||
* The x coordinate of the center of the circle
|
||||
* @property x
|
||||
* @type Number
|
||||
**/
|
||||
public x: number = 0;
|
||||
|
||||
/**
|
||||
* The y coordinate of the center of the circle
|
||||
* @property y
|
||||
* @type Number
|
||||
**/
|
||||
public y: number = 0;
|
||||
|
||||
/**
|
||||
* 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}
|
||||
**/
|
||||
get diameter(): number {
|
||||
return this._diameter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
set diameter(value: number) {
|
||||
|
||||
if (value > 0)
|
||||
{
|
||||
this._diameter = value;
|
||||
this._radius = value * 0.5;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}
|
||||
**/
|
||||
get radius(): number {
|
||||
return this._radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
set radius(value: number) {
|
||||
|
||||
if (value > 0)
|
||||
{
|
||||
this._radius = value;
|
||||
this._diameter = value * 2;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The circumference of the circle.
|
||||
* @method circumference
|
||||
* @return {Number}
|
||||
**/
|
||||
circumference(): number {
|
||||
return 2 * (Math.PI * this._radius);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}
|
||||
**/
|
||||
get bottom(): number {
|
||||
return this.y + this._radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
set bottom(value: number) {
|
||||
|
||||
if (value < this.y)
|
||||
{
|
||||
this._radius = 0;
|
||||
this._diameter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.radius = value - this.y;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
get left(): number {
|
||||
return this.x - this._radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
set left(value: number) {
|
||||
|
||||
if (value > this.x)
|
||||
{
|
||||
this._radius = 0;
|
||||
this._diameter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.radius = this.x - value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}
|
||||
**/
|
||||
get right(): number {
|
||||
return this.x + this._radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
set right(value: number) {
|
||||
|
||||
if (value < this.x)
|
||||
{
|
||||
this._radius = 0;
|
||||
this._diameter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.radius = value - this.x;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}
|
||||
**/
|
||||
get top(): number {
|
||||
return this.y - this._radius;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
set top(value: number) {
|
||||
|
||||
if (value > this.y)
|
||||
{
|
||||
this._radius = 0;
|
||||
this._diameter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.radius = this.y - value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the area of this Circle.
|
||||
* @method area
|
||||
* @return {Number} This area of this circle.
|
||||
**/
|
||||
get area(): number {
|
||||
|
||||
if (this._radius > 0)
|
||||
{
|
||||
return Math.PI * this._radius * this._radius;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
public setTo(x: number, y: number, diameter: number): Circle {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this._diameter = diameter;
|
||||
this._radius = diameter * 0.5;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether or not this Circle object is empty.
|
||||
* @method empty
|
||||
* @return {Boolean} A value of true if the Circle objects diameter is less than or equal to 0; otherwise false.
|
||||
**/
|
||||
get empty(): bool {
|
||||
return (this._diameter == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
set empty(value: bool) {
|
||||
return this.setTo(0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
public offset(dx: number, dy: number): Circle {
|
||||
|
||||
this.x += dx;
|
||||
this.y += dy;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
public offsetPoint(point: Point): Circle {
|
||||
return this.offset(point.x, point.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of this object.
|
||||
* @method toString
|
||||
* @return {string} a string representation of the instance.
|
||||
**/
|
||||
public toString(): string {
|
||||
return "[{Circle (x=" + this.x + " y=" + this.y + " diameter=" + this.diameter + " radius=" + this.radius + ")}]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/// <reference path="../Game.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Point
|
||||
*
|
||||
* The Point object represents a location in a two-dimensional coordinate system, where x represents the horizontal axis and y represents the vertical axis.
|
||||
*/
|
||||
|
||||
module Phaser {
|
||||
|
||||
export class Point {
|
||||
|
||||
/**
|
||||
* Creates a new Point. If you pass no parameters a Point is created set to (0,0).
|
||||
* @class Point
|
||||
* @constructor
|
||||
* @param {Number} x The horizontal position of this Point (default 0)
|
||||
* @param {Number} y The vertical position of this Point (default 0)
|
||||
**/
|
||||
constructor(x: number = 0, y: number = 0) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
}
|
||||
|
||||
x: number;
|
||||
y: number;
|
||||
|
||||
/**
|
||||
* Copies the x and y properties from any given object to this Point.
|
||||
* @method copyFrom
|
||||
* @param {any} source - The object to copy from.
|
||||
* @return {Point} This Point object.
|
||||
**/
|
||||
public copyFrom(source: any): Point {
|
||||
return this.setTo(source.x, source.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the x and y values from this Point to any given object.
|
||||
* @method copyTo
|
||||
* @param {any} target - The object to copy to.
|
||||
* @return {any} The target object.
|
||||
**/
|
||||
public copyTo(target: any): Point {
|
||||
|
||||
target.x = this.x;
|
||||
target.y = this.y;
|
||||
|
||||
return target;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Inverts the x and y values of this Point
|
||||
* @method invert
|
||||
* @return {Point} This Point object.
|
||||
**/
|
||||
public invert(): Point {
|
||||
return this.setTo(this.y, this.x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the x and y values of this MicroPoint object to the given coordinates.
|
||||
* @method setTo
|
||||
* @param {Number} x - The horizontal position of this point.
|
||||
* @param {Number} y - The vertical position of this point.
|
||||
* @return {MicroPoint} This MicroPoint object. Useful for chaining method calls.
|
||||
**/
|
||||
public setTo(x: number, y: number): Point {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of this object.
|
||||
* @method toString
|
||||
* @return {string} a string representation of the instance.
|
||||
**/
|
||||
public toString(): string {
|
||||
return '[{Point (x=' + this.x + ' y=' + this.y + ')}]';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,314 @@
|
||||
/// <reference path="Point.ts" />
|
||||
|
||||
/**
|
||||
* Rectangle
|
||||
*
|
||||
* @desc A Rectangle object is an area defined by its position, as indicated by its top-left corner (x,y) and width and height.
|
||||
*
|
||||
* @version 1.6 - 24th May 2013
|
||||
* @author Richard Davey
|
||||
*/
|
||||
|
||||
module Phaser {
|
||||
|
||||
export class Rectangle {
|
||||
|
||||
/**
|
||||
* Creates a new Rectangle object with the top-left corner specified by the x and y parameters and with the specified width and height parameters. If you call this function without parameters, a rectangle with x, y, width, and height properties set to 0 is created.
|
||||
* @class Rectangle
|
||||
* @constructor
|
||||
* @param {Number} x The x coordinate of the top-left corner of the rectangle.
|
||||
* @param {Number} y The y coordinate of the top-left corner of the rectangle.
|
||||
* @param {Number} width The width of the rectangle in pixels.
|
||||
* @param {Number} height The height of the rectangle in pixels.
|
||||
* @return {Rectangle} This rectangle object
|
||||
**/
|
||||
constructor(x: number = 0, y: number = 0, width: number = 0, height: number = 0) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The x coordinate of the top-left corner of the rectangle
|
||||
* @property x
|
||||
* @type Number
|
||||
**/
|
||||
x: number;
|
||||
|
||||
/**
|
||||
* The y coordinate of the top-left corner of the rectangle
|
||||
* @property y
|
||||
* @type Number
|
||||
**/
|
||||
y: number;
|
||||
|
||||
/**
|
||||
* The width of the rectangle in pixels
|
||||
* @property width
|
||||
* @type Number
|
||||
**/
|
||||
width: number;
|
||||
|
||||
/**
|
||||
* The height of the rectangle in pixels
|
||||
* @property height
|
||||
* @type Number
|
||||
**/
|
||||
height: number;
|
||||
|
||||
/**
|
||||
* Half of the width of the rectangle
|
||||
* @property halfWidth
|
||||
* @type Number
|
||||
**/
|
||||
get halfWidth(): number {
|
||||
return Math.round(this.width / 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Half of the height of the rectangle
|
||||
* @property halfHeight
|
||||
* @type Number
|
||||
**/
|
||||
get halfHeight(): number {
|
||||
return Math.round(this.height / 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
|
||||
* @method bottom
|
||||
* @return {Number}
|
||||
**/
|
||||
get bottom(): number {
|
||||
return this.y + this.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sum of the y and height properties. Changing the bottom property of a Rectangle object has no effect on the x, y and width properties, but does change the height property.
|
||||
* @method bottom
|
||||
* @param {Number} value
|
||||
**/
|
||||
set bottom(value: number) {
|
||||
|
||||
if (value <= this.y)
|
||||
{
|
||||
this.height = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.height = (this.y - value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bottom-right corner of the Rectangle, determined by the values of the given Point object.
|
||||
* @method bottomRight
|
||||
* @param {Point} value
|
||||
**/
|
||||
set bottomRight(value: Point) {
|
||||
|
||||
this.right = value.x;
|
||||
this.bottom = value.y;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The x coordinate of the left of the Rectangle. Changing the left property of a Rectangle object has no effect on the y and height properties. However it does affect the width property, whereas changing the x value does not affect the width property.
|
||||
* @method left
|
||||
* @ return {number}
|
||||
**/
|
||||
get left(): number {
|
||||
return this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* The x coordinate of the left of the Rectangle. Changing the left property of a Rectangle object has no effect on the y and height properties.
|
||||
* However it does affect the width, whereas changing the x value does not affect the width property.
|
||||
* @method left
|
||||
* @param {Number} value
|
||||
**/
|
||||
set left(value: number) {
|
||||
|
||||
if (value >= this.right)
|
||||
{
|
||||
this.width = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.width = this.right - value;
|
||||
}
|
||||
|
||||
this.x = value;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
|
||||
* However it does affect the width property.
|
||||
* @method right
|
||||
* @return {Number}
|
||||
**/
|
||||
get right(): number {
|
||||
return this.x + this.width;
|
||||
}
|
||||
|
||||
/**
|
||||
* The sum of the x and width properties. Changing the right property of a Rectangle object has no effect on the x, y and height properties.
|
||||
* However it does affect the width property.
|
||||
* @method right
|
||||
* @param {Number} value
|
||||
**/
|
||||
set right(value: number) {
|
||||
|
||||
if (value <= this.x)
|
||||
{
|
||||
this.width = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.width = this.x + value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The volume of the Rectangle derived from width * height
|
||||
* @method volume
|
||||
* @return {Number}
|
||||
**/
|
||||
get volume(): number {
|
||||
return this.width * this.height;
|
||||
}
|
||||
|
||||
/**
|
||||
* The perimeter size of the Rectangle. This is the sum of all 4 sides.
|
||||
* @method perimeter
|
||||
* @return {Number}
|
||||
**/
|
||||
get perimeter(): number {
|
||||
return (this.width * 2) + (this.height * 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* The y coordinate of the top of the Rectangle. Changing the top property of a Rectangle object has no effect on the x and width properties.
|
||||
* However it does affect the height property, whereas changing the y value does not affect the height property.
|
||||
* @method top
|
||||
* @return {Number}
|
||||
**/
|
||||
get top(): number {
|
||||
return this.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* The y coordinate of the top of the Rectangle. Changing the top property of a Rectangle object has no effect on the x and width properties.
|
||||
* However it does affect the height property, whereas changing the y value does not affect the height property.
|
||||
* @method top
|
||||
* @param {Number} value
|
||||
**/
|
||||
set top(value: number) {
|
||||
|
||||
if (value >= this.bottom)
|
||||
{
|
||||
this.height = 0;
|
||||
this.y = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.height = (this.bottom - value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The location of the Rectangles top-left corner, determined by the x and y coordinates of the Point.
|
||||
* @method topLeft
|
||||
* @param {Point} value
|
||||
**/
|
||||
set topLeft(value: Point) {
|
||||
|
||||
this.x = value.x;
|
||||
this.y = value.y;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether or not this Rectangle object is empty.
|
||||
* @method isEmpty
|
||||
* @return {Boolean} A value of true if the Rectangle object's width or height is less than or equal to 0; otherwise false.
|
||||
**/
|
||||
get empty(): bool {
|
||||
return (!this.width || !this.height);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all of the Rectangle object's properties to 0. A Rectangle object is empty if its width or height is less than or equal to 0.
|
||||
* @method setEmpty
|
||||
* @return {Rectangle} This rectangle object
|
||||
**/
|
||||
set empty(value: bool) {
|
||||
return this.setTo(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the location of the Rectangle object, as determined by its top-left corner, by the specified amounts.
|
||||
* @method offset
|
||||
* @param {Number} dx Moves the x value of the Rectangle object by this amount.
|
||||
* @param {Number} dy Moves the y value of the Rectangle object by this amount.
|
||||
* @return {Rectangle} This Rectangle object.
|
||||
**/
|
||||
offset(dx: number, dy: number): Rectangle {
|
||||
|
||||
this.x += dx;
|
||||
this.y += dy;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjusts the location of the Rectangle object using a Point object as a parameter. This method is similar to the Rectangle.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 Rectangle object.
|
||||
* @return {Rectangle} This Rectangle object.
|
||||
**/
|
||||
offsetPoint(point: Point): Rectangle {
|
||||
return this.offset(point.x, point.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the members of Rectangle to the specified values.
|
||||
* @method setTo
|
||||
* @param {Number} x The x coordinate of the top-left corner of the rectangle.
|
||||
* @param {Number} y The y coordinate of the top-left corner of the rectangle.
|
||||
* @param {Number} width The width of the rectangle in pixels.
|
||||
* @param {Number} height The height of the rectangle in pixels.
|
||||
* @return {Rectangle} This rectangle object
|
||||
**/
|
||||
setTo(x: number, y: number, width: number, height: number): Rectangle {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of this object.
|
||||
* @method toString
|
||||
* @return {string} a string representation of the instance.
|
||||
**/
|
||||
toString(): string {
|
||||
return "[{Rectangle (x=" + this.x + " y=" + this.y + " width=" + this.width + " height=" + this.height + " empty=" + this.empty + ")}]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
/// <reference path="../Game.ts" />
|
||||
|
||||
/**
|
||||
* Phaser - Vec2
|
||||
*
|
||||
* A Circle object is an area defined by its position, as indicated by its center point (x,y) and diameter.
|
||||
*/
|
||||
|
||||
module Phaser {
|
||||
|
||||
export class Vec2 {
|
||||
|
||||
/**
|
||||
* Creates a new Vec2 object.
|
||||
* @class Vec2
|
||||
* @constructor
|
||||
* @param {Number} x The x position of the vector
|
||||
* @param {Number} y The y position of the vector
|
||||
* @return {Vec2} This object
|
||||
**/
|
||||
constructor(x: number = 0, y: number = 0) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The x coordinate of the vector
|
||||
* @property x
|
||||
* @type Number
|
||||
**/
|
||||
public x: number;
|
||||
|
||||
/**
|
||||
* The y coordinate of the vector
|
||||
* @property y
|
||||
* @type Number
|
||||
**/
|
||||
public y: number;
|
||||
|
||||
/**
|
||||
* Sets the x and y properties of the Vector.
|
||||
* @param {Number} x The x position of the vector
|
||||
* @param {Number} y The y position of the vector
|
||||
* @return {Vec2} This object
|
||||
**/
|
||||
public setTo(x: number, y: number): Vec2 {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add another vector to this one.
|
||||
*
|
||||
* @param {Vec2} other The other Vector.
|
||||
* @return {Vec2} This for chaining.
|
||||
*/
|
||||
public add(a: Vec2): Vec2 {
|
||||
|
||||
this.x += a.x;
|
||||
this.y += a.y;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract another vector from this one.
|
||||
*
|
||||
* @param {Vec2} other The other Vector.
|
||||
* @return {Vec2} This for chaining.
|
||||
*/
|
||||
public subtract(v: Vec2): Vec2 {
|
||||
|
||||
this.x -= v.x;
|
||||
this.y -= v.y;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiply another vector with this one.
|
||||
*
|
||||
* @param {Vec2} other The other Vector.
|
||||
* @return {Vec2} This for chaining.
|
||||
*/
|
||||
public multiply(v: Vec2): Vec2 {
|
||||
|
||||
this.x *= v.x;
|
||||
this.y *= v.y;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide this vector by another one.
|
||||
*
|
||||
* @param {Vec2} other The other Vector.
|
||||
* @return {Vec2} This for chaining.
|
||||
*/
|
||||
public divide(v: Vec2): Vec2 {
|
||||
|
||||
this.x /= v.x;
|
||||
this.y /= v.y;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the length of this vector.
|
||||
*
|
||||
* @return {number} The length of this vector.
|
||||
*/
|
||||
public length(): number {
|
||||
return Math.sqrt((this.x * this.x) + (this.y * this.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the length squared of this vector.
|
||||
*
|
||||
* @return {number} The length^2 of this vector.
|
||||
*/
|
||||
public lengthSq(): number {
|
||||
return (this.x * this.x) + (this.y * this.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* The dot product of two 2D vectors.
|
||||
*
|
||||
* @param {Vec2} a Reference to a source Vec2 object.
|
||||
* @return {Number}
|
||||
*/
|
||||
public dot(a: Vec2): number {
|
||||
return ((this.x * a.x) + (this.y * a.y));
|
||||
}
|
||||
|
||||
/**
|
||||
* The cross product of two 2D vectors.
|
||||
*
|
||||
* @param {Vec2} a Reference to a source Vec2 object.
|
||||
* @return {Number}
|
||||
*/
|
||||
public cross(a: Vec2): number {
|
||||
return ((this.x * a.y) - (this.y * a.x));
|
||||
}
|
||||
|
||||
/**
|
||||
* The projection magnitude of two 2D vectors.
|
||||
*
|
||||
* @param {Vec2} a Reference to a source Vec2 object.
|
||||
* @return {Number}
|
||||
*/
|
||||
public projectionLength(a: Vec2): number {
|
||||
|
||||
var den: number = a.dot(a);
|
||||
|
||||
if (den == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Math.abs(this.dot(a) / den);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The angle between two 2D vectors.
|
||||
*
|
||||
* @param {Vec2} a Reference to a source Vec2 object.
|
||||
* @return {Number}
|
||||
*/
|
||||
public angle(a: Vec2): number {
|
||||
return Math.atan2(a.x * this.y - a.y * this.x, a.x * this.x + a.y * this.y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale this vector.
|
||||
*
|
||||
* @param {number} x The scaling factor in the x direction.
|
||||
* @param {?number=} y The scaling factor in the y direction. If this is not specified, the x scaling factor will be used.
|
||||
* @return {Vec2} This for chaining.
|
||||
*/
|
||||
public scale(x: number, y?:number): Vec2 {
|
||||
|
||||
this.x *= x;
|
||||
this.y *= y || x;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide this vector by the given scalar.
|
||||
*
|
||||
* @param {number} scalar
|
||||
* @return {Vec2} This for chaining.
|
||||
*/
|
||||
public divideByScalar(scalar: number): Vec2 {
|
||||
|
||||
this.x /= scalar;
|
||||
this.y /= scalar;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse this vector.
|
||||
*
|
||||
* @return {Vec2} This for chaining.
|
||||
*/
|
||||
public reverse(): Vec2 {
|
||||
|
||||
this.x = -this.x;
|
||||
this.y = -this.y;
|
||||
return this;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of this object.
|
||||
* @method toString
|
||||
* @return {string} a string representation of the object.
|
||||
**/
|
||||
public toString(): string {
|
||||
return "[{Vec2 (x=" + this.x + " y=" + this.y + ")}]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user