2017-09-20 22:10:37 +00:00
|
|
|
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
|
|
|
|
|
|
|
|
var Class = require('../../../utils/Class');
|
|
|
|
var Curve = require('../Curve');
|
|
|
|
var Vector2 = require('../../../math/Vector2');
|
|
|
|
|
|
|
|
// 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
|
|
|
|
function LineCurve (p0, p1)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-09-22 15:41:11 +00:00
|
|
|
Curve.call(this);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
this.p0 = p0;
|
|
|
|
this.p1 = p1;
|
|
|
|
},
|
2017-09-21 00:19:27 +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
|
|
|
},
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
getResolution: function ()
|
2017-09-21 16:12:16 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
out.copy(this.p1).sub(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
|
|
|
|
getPointAt: function (u, out)
|
|
|
|
{
|
|
|
|
return this.getPoint(u, out);
|
|
|
|
},
|
|
|
|
|
|
|
|
getTangent: function ()
|
|
|
|
{
|
2017-09-22 15:41:11 +00:00
|
|
|
var tangent = tmpVec2.copy(this.p1).sub(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!
|
|
|
|
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
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
type: 'LineCurve',
|
|
|
|
points: [
|
|
|
|
this.p0.x, this.p0.y,
|
|
|
|
this.p1.x, this.p1.y
|
|
|
|
]
|
|
|
|
};
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = LineCurve;
|