phaser
Version:
A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.
315 lines (275 loc) • 7.8 kB
JavaScript
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var GetPoint = require('./GetPoint');
var GetPoints = require('./GetPoints');
var Random = require('./Random');
var Vector2 = require('../../math/Vector2');
/**
* @classdesc
* Defines a Line segment, a part of a line between two endpoints.
*
* @class Line
* @memberOf Phaser.Geom
* @constructor
* @since 3.0.0
*
* @param {number} [x1=0] - The x coordinate of the lines starting point.
* @param {number} [y1=0] - The y coordinate of the lines starting point.
* @param {number} [x2=0] - The x coordinate of the lines ending point.
* @param {number} [y2=0] - The y coordinate of the lines ending point.
*/
var Line = new Class({
initialize:
function Line (x1, y1, x2, y2)
{
if (x1 === undefined) { x1 = 0; }
if (y1 === undefined) { y1 = 0; }
if (x2 === undefined) { x2 = 0; }
if (y2 === undefined) { y2 = 0; }
/**
* The x coordinate of the lines starting point.
*
* @name Phaser.Geom.Line#x1
* @type {number}
* @since 3.0.0
*/
this.x1 = x1;
/**
* The y coordinate of the lines starting point.
*
* @name Phaser.Geom.Line#y1
* @type {number}
* @since 3.0.0
*/
this.y1 = y1;
/**
* The x coordinate of the lines ending point.
*
* @name Phaser.Geom.Line#x2
* @type {number}
* @since 3.0.0
*/
this.x2 = x2;
/**
* The y coordinate of the lines ending point.
*
* @name Phaser.Geom.Line#y2
* @type {number}
* @since 3.0.0
*/
this.y2 = y2;
},
/**
* [description]
*
* @method Phaser.Geom.Line#getPoint
* @since 3.0.0
*
* @generic {Phaser.Geom.Point} O - [output,$return]
*
* @param {float} position - [description]
* @param {(Phaser.Geom.Point|object)} [output] - [description]
*
* @return {(Phaser.Geom.Point|object)} A Point, or point-like object, containing the coordinates of the point on the line.
*/
getPoint: function (position, output)
{
return GetPoint(this, position, output);
},
/**
* [description]
*
* @method Phaser.Geom.Line#getPoints
* @since 3.0.0
*
* @generic {Phaser.Geom.Point} O - [output,$return]
*
* @param {integer} quantity - [description]
* @param {integer} [stepRate] - [description]
* @param {(array|Phaser.Geom.Point[])} [output] - [description]
*
* @return {(array|Phaser.Geom.Point[])} [description]
*/
getPoints: function (quantity, stepRate, output)
{
return GetPoints(this, quantity, stepRate, output);
},
/**
* Get a random Point on the Line.
*
* @method Phaser.Geom.Line#getRandomPoint
* @since 3.0.0
*
* @generic {Phaser.Geom.Point} O - [point,$return]
*
* @param {(Phaser.Geom.Point|object)} [point] - An instance of a Point to be modified.
*
* @return {Phaser.Geom.Point} A random Point on the Line.
*/
getRandomPoint: function (point)
{
return Random(this, point);
},
/**
* Set new coordinates for the line endpoints.
*
* @method Phaser.Geom.Line#setTo
* @since 3.0.0
*
* @param {number} [x1=0] - The x coordinate of the lines starting point.
* @param {number} [y1=0] - The y coordinate of the lines starting point.
* @param {number} [x2=0] - The x coordinate of the lines ending point.
* @param {number} [y2=0] - The y coordinate of the lines ending point.
*
* @return {Phaser.Geom.Line} This Line object.
*/
setTo: function (x1, y1, x2, y2)
{
if (x1 === undefined) { x1 = 0; }
if (y1 === undefined) { y1 = 0; }
if (x2 === undefined) { x2 = 0; }
if (y2 === undefined) { y2 = 0; }
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
return this;
},
/**
* Returns a Vector2 object that corresponds to the start of this Line.
*
* @method Phaser.Geom.Line#getPointA
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [vec2,$return]
*
* @param {Phaser.Math.Vector2} [vec2] - A Vector2 object to set the results in. If `undefined` a new Vector2 will be created.
*
* @return {Phaser.Math.Vector2} A Vector2 object that corresponds to the start of this Line.
*/
getPointA: function (vec2)
{
if (vec2 === undefined) { vec2 = new Vector2(); }
vec2.set(this.x1, this.y1);
return vec2;
},
/**
* Returns a Vector2 object that corresponds to the start of this Line.
*
* @method Phaser.Geom.Line#getPointB
* @since 3.0.0
*
* @generic {Phaser.Math.Vector2} O - [vec2,$return]
*
* @param {Phaser.Math.Vector2} [vec2] - A Vector2 object to set the results in. If `undefined` a new Vector2 will be created.
*
* @return {Phaser.Math.Vector2} A Vector2 object that corresponds to the start of this Line.
*/
getPointB: function (vec2)
{
if (vec2 === undefined) { vec2 = new Vector2(); }
vec2.set(this.x2, this.y2);
return vec2;
},
/**
* The left position of the Line.
*
* @name Phaser.Geom.Line#left
* @type {number}
* @since 3.0.0
*/
left: {
get: function ()
{
return Math.min(this.x1, this.x2);
},
set: function (value)
{
if (this.x1 <= this.x2)
{
this.x1 = value;
}
else
{
this.x2 = value;
}
}
},
/**
* The right position of the Line.
*
* @name Phaser.Geom.Line#right
* @type {number}
* @since 3.0.0
*/
right: {
get: function ()
{
return Math.max(this.x1, this.x2);
},
set: function (value)
{
if (this.x1 > this.x2)
{
this.x1 = value;
}
else
{
this.x2 = value;
}
}
},
/**
* The top position of the Line.
*
* @name Phaser.Geom.Line#top
* @type {number}
* @since 3.0.0
*/
top: {
get: function ()
{
return Math.min(this.y1, this.y2);
},
set: function (value)
{
if (this.y1 <= this.y2)
{
this.y1 = value;
}
else
{
this.y2 = value;
}
}
},
/**
* The bottom position of the Line.
*
* @name Phaser.Geom.Line#bottom
* @type {number}
* @since 3.0.0
*/
bottom: {
get: function ()
{
return Math.max(this.y1, this.y2);
},
set: function (value)
{
if (this.y1 > this.y2)
{
this.y1 = value;
}
else
{
this.y2 = value;
}
}
}
});
module.exports = Line;