2017-09-20 22:10:37 +00:00
|
|
|
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
|
|
|
|
|
2017-10-13 15:39:41 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var Curve = require('../curve/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
|
|
|
|
|
|
|
// Phaser.Curves.Line
|
|
|
|
|
|
|
|
var tmpVec2 = new Vector2();
|
|
|
|
|
|
|
|
var LineCurve = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
// vec2s or array
|
2018-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class LineCurve
|
|
|
|
* @extends Phaser.Curves.Curve
|
|
|
|
* @memberOf Phaser.Curves
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} p0 - [description]
|
|
|
|
* @param {[type]} p1 - [description]
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
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]
|
|
|
|
*
|
|
|
|
* @property {[type]} p0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p0 = p0;
|
2018-01-25 05:26:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {[type]} p1
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getBounds
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} out - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getStartPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} out - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getPoint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} t - [description]
|
|
|
|
* @param {[type]} out - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
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-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#getPointAt
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} u - [description]
|
|
|
|
* @param {[type]} out - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
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-01-25 05:26:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Curves.LineCurve#draw
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {[type]} graphics - [description]
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
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
|
|
|
|
*
|
|
|
|
* @return {[type]} [description]
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
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;
|