arcade-physics
Version:
Use Arcade Physics without Phaser.
293 lines • 9.01 kB
JavaScript
"use strict";
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Class_1 = __importDefault(require("../../utils/Class"));
const GetPoint_1 = __importDefault(require("./GetPoint"));
const GetPoints_1 = __importDefault(require("./GetPoints"));
const const_1 = __importDefault(require("../const"));
const Random_1 = __importDefault(require("./Random"));
const Vector2_1 = __importDefault(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.
*/
const Line = new Class_1.default({
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 geometry constant type of this object: `GEOM_CONST.LINE`.
* Used for fast type comparisons.
*
* @name Phaser.Geom.Line#type
* @type {number}
* @readonly
* @since 3.19.0
*/
this.type = const_1.default.LINE;
/**
* 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;
},
/**
* Get a point on a line that's a given percentage along its length.
*
* @method Phaser.Geom.Line#getPoint
* @since 3.0.0
*
* @generic {Phaser.Geom.Point} O - [output,$return]
*
* @param {number} position - A value between 0 and 1, where 0 is the start, 0.5 is the middle and 1 is the end of the line.
* @param {(Phaser.Geom.Point|object)} [output] - An optional point, or point-like object, to store the coordinates of the point on the line.
*
* @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 (0, GetPoint_1.default)(this, position, output);
},
/**
* Get a number of points along a line's length.
*
* Provide a `quantity` to get an exact number of points along the line.
*
* Provide a `stepRate` to ensure a specific distance between each point on the line. Set `quantity` to `0` when
* providing a `stepRate`.
*
* @method Phaser.Geom.Line#getPoints
* @since 3.0.0
*
* @generic {Phaser.Geom.Point[]} O - [output,$return]
*
* @param {number} quantity - The number of points to place on the line. Set to `0` to use `stepRate` instead.
* @param {number} [stepRate] - The distance between each point on the line. When set, `quantity` is implied and should be set to `0`.
* @param {(array|Phaser.Geom.Point[])} [output] - An optional array of Points, or point-like objects, to store the coordinates of the points on the line.
*
* @return {(array|Phaser.Geom.Point[])} An array of Points, or point-like objects, containing the coordinates of the points on the line.
*/
getPoints: function (quantity, stepRate, output) {
return (0, GetPoints_1.default)(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 (0, Random_1.default)(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 {this} 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_1.default();
}
vec2.set(this.x1, this.y1);
return vec2;
},
/**
* Returns a Vector2 object that corresponds to the end 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 end of this Line.
*/
getPointB: function (vec2) {
if (vec2 === undefined) {
vec2 = new Vector2_1.default();
}
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;
}
}
}
});
exports.default = Line;
//# sourceMappingURL=Line.js.map