2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
|
|
|
|
|
2018-02-13 00:40:51 +00:00
|
|
|
var Class = require('../utils/Class');
|
|
|
|
var Curve = require('./Curve');
|
|
|
|
var FromPoints = require('../geom/rectangle/FromPoints');
|
|
|
|
var Rectangle = require('../geom/rectangle/Rectangle');
|
|
|
|
var Vector2 = require('../math/Vector2');
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
var tmpVec2 = new Vector2();
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class LineCurve
|
|
|
|
* @extends Phaser.Curves.Curve
|
|
|
|
* @memberOf Phaser.Curves
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-13 00:40:51 +00:00
|
|
|
* @param {Phaser.Math.Vector2} p0 - [description]
|
|
|
|
* @param {Phaser.Math.Vector2} p1 - [description]
|
2018-02-07 15:27:21 +00:00
|
|
|
*/
|
2017-09-20 22:10:37 +00:00
|
|
|
var LineCurve = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
// vec2s or array
|
|
|
|
function LineCurve (p0, p1)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-10-02 21:42:47 +00:00
|
|
|
Curve.call(this, 'LineCurve');
|
2017-09-22 15:41:11 +00:00
|
|
|
|
|
|
|
if (Array.isArray(p0))
|
2017-09-22 00:34:53 +00:00
|
|
|
{
|
2017-09-22 15:41:11 +00:00
|
|
|
p1 = new Vector2(p0[2], p0[3]);
|
|
|
|
p0 = new Vector2(p0[0], p0[1]);
|
2017-09-22 00:34:53 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 00:40:51 +00:00
|
|
|
* @name Phaser.Curves.LineCurve#p0
|
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p0 = p0;
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2018-02-13 00:40:51 +00:00
|
|
|
* @property Phaser.Curves.LineCurve#p1
|
|
|
|
* @type {Phaser.Math.Vector2}
|
2018-01-25 05:26:13 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p1 = p1;
|
|
|
|
},
|
2017-09-21 00:19:27 +00:00
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Returns a Rectangle where the position and dimensions match the bounds of this Curve.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getBounds
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {Phaser.Geom.Rectangle} [out] - A Rectangle object to store the bounds in. If not given a new Rectangle will be created.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Geom.Rectangle} A Rectangle object holding the bounds of this curve. If `out` was given it will be this object.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-29 10:42:34 +00:00
|
|
|
getBounds: function (out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Rectangle(); }
|
|
|
|
|
|
|
|
return FromPoints([ this.p0, this.p1 ], out);
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Gets the starting point on the curve.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getStartPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the result in. If not given will be created.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The coordinates of the point on the curve. If an `out` object was given this will be returned.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-27 01:32:26 +00:00
|
|
|
getStartPoint: function (out)
|
2017-09-22 15:41:11 +00:00
|
|
|
{
|
2017-09-27 01:32:26 +00:00
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
|
|
|
return out.copy(this.p0);
|
2017-09-20 22:10:37 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getResolution
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {integer} [description]
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
getResolution: function ()
|
2017-09-21 16:12:16 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Get point at relative position in curve according to length.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {float} t - The position along the curve to return. Where 0 is the start and 1 is the end.
|
|
|
|
* @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the result in. If not given will be created.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The coordinates of the point on the curve. If an `out` object was given this will be returned.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-20 22:10:37 +00:00
|
|
|
getPoint: function (t, out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
|
|
|
if (t === 1)
|
|
|
|
{
|
2017-09-22 15:41:11 +00:00
|
|
|
return out.copy(this.p1);
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 00:18:36 +00:00
|
|
|
out.copy(this.p1).subtract(this.p0).scale(t).add(this.p0);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Line curve is linear, so we can overwrite default getPointAt
|
2018-03-18 13:43:37 +00:00
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getPointAt
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {float} u - The position along the curve to return. Where 0 is the start and 1 is the end.
|
|
|
|
* @param {Phaser.Math.Vector2} [out] - A Vector2 object to store the result in. If not given will be created.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} The coordinates of the point on the curve. If an `out` object was given this will be returned.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-20 22:10:37 +00:00
|
|
|
getPointAt: function (u, out)
|
|
|
|
{
|
|
|
|
return this.getPoint(u, out);
|
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getTangent
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.Math.Vector2} [description]
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-20 22:10:37 +00:00
|
|
|
getTangent: function ()
|
|
|
|
{
|
2018-01-12 00:18:36 +00:00
|
|
|
var tangent = tmpVec2.copy(this.p1).subtract(this.p0);
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
return tangent.normalize();
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Override default Curve.draw because this is better than calling getPoints on a line!
|
2018-03-18 13:43:37 +00:00
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
2018-03-18 13:43:37 +00:00
|
|
|
* Draws this curve on the given Graphics object.
|
2018-03-19 15:53:55 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* The curve is drawn using `Graphics.lineBetween` so will be drawn at whatever the present Graphics line color is.
|
|
|
|
* The Graphics object is not cleared before the draw, so the curve will appear on-top of anything else already rendered to it.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#draw
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics instance onto which this curve will be drawn.
|
2018-01-25 05:26:13 +00:00
|
|
|
*
|
2018-03-18 13:43:37 +00:00
|
|
|
* @return {Phaser.GameObjects.Graphics} The Graphics object to which the curve was drawn.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
draw: function (graphics)
|
|
|
|
{
|
|
|
|
graphics.lineBetween(this.p0.x, this.p0.y, this.p1.x, this.p1.y);
|
|
|
|
|
|
|
|
// So you can chain graphics calls
|
|
|
|
return graphics;
|
2017-09-22 18:36:00 +00:00
|
|
|
},
|
|
|
|
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#toJSON
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:53:55 +00:00
|
|
|
* @return {JSONCurve} The JSON object containing this curve data.
|
2018-01-25 05:26:13 +00:00
|
|
|
*/
|
2017-09-22 18:36:00 +00:00
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
return {
|
2017-10-02 21:42:47 +00:00
|
|
|
type: this.type,
|
2017-09-22 18:36:00 +00:00
|
|
|
points: [
|
|
|
|
this.p0.x, this.p0.y,
|
|
|
|
this.p1.x, this.p1.y
|
|
|
|
]
|
|
|
|
};
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-03-18 13:43:37 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @function Phaser.Curves.LineCurve.fromJSON
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:53:55 +00:00
|
|
|
* @param {JSONCurve} data - The JSON object containing this curve data.
|
2018-03-18 13:43:37 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Curves.LineCurve} [description]
|
|
|
|
*/
|
2017-10-02 21:42:47 +00:00
|
|
|
LineCurve.fromJSON = function (data)
|
|
|
|
{
|
|
|
|
var points = data.points;
|
|
|
|
|
|
|
|
var p0 = new Vector2(points[0], points[1]);
|
|
|
|
var p1 = new Vector2(points[2], points[3]);
|
|
|
|
|
|
|
|
return new LineCurve(p0, p1);
|
|
|
|
};
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
module.exports = LineCurve;
|