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');
|
2017-09-21 01:30:54 +00:00
|
|
|
var CubicBezier = require('../../../math/interpolation/CubicBezierInterpolation');
|
2017-09-20 22:10:37 +00:00
|
|
|
var Curve = require('../Curve');
|
|
|
|
var Vector2 = require('../../../math/Vector2');
|
|
|
|
|
2017-09-21 01:30:54 +00:00
|
|
|
// Phaser.Curves.CubicBezier
|
2017-09-20 22:10:37 +00:00
|
|
|
|
|
|
|
var CubicBezierCurve = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
function CubicBezierCurve (p0, p1, p2, p3)
|
2017-09-20 22:10:37 +00:00
|
|
|
{
|
2017-09-21 00:19:27 +00:00
|
|
|
Curve.call(this);
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
if (Array.isArray(p0))
|
|
|
|
{
|
|
|
|
p3 = new Vector2(p0[6], p0[7]);
|
|
|
|
p2 = new Vector2(p0[4], p0[5]);
|
|
|
|
p1 = new Vector2(p0[2], p0[3]);
|
|
|
|
p0 = new Vector2(p0[0], p0[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.p0 = p0;
|
|
|
|
this.p1 = p1;
|
|
|
|
this.p2 = p2;
|
|
|
|
this.p3 = p3;
|
|
|
|
},
|
|
|
|
|
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-21 16:12:16 +00:00
|
|
|
getResolution: function (divisions)
|
|
|
|
{
|
|
|
|
return divisions;
|
|
|
|
},
|
|
|
|
|
2017-09-20 22:10:37 +00:00
|
|
|
getPoint: function (t, out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
var p0 = this.p0;
|
|
|
|
var p1 = this.p1;
|
|
|
|
var p2 = this.p2;
|
|
|
|
var p3 = this.p3;
|
|
|
|
|
|
|
|
return out.set(CubicBezier(t, p0.x, p1.x, p2.x, p3.x), CubicBezier(t, p0.y, p1.y, p2.y, p3.y));
|
|
|
|
},
|
|
|
|
|
|
|
|
draw: function (graphics, pointsTotal)
|
|
|
|
{
|
|
|
|
if (pointsTotal === undefined) { pointsTotal = 32; }
|
|
|
|
|
|
|
|
var points = this.getPoints(pointsTotal);
|
|
|
|
|
|
|
|
graphics.beginPath();
|
|
|
|
graphics.moveTo(this.p0.x, this.p0.y);
|
|
|
|
|
|
|
|
for (var i = 1; i < points.length; i++)
|
|
|
|
{
|
|
|
|
graphics.lineTo(points[i].x, points[i].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
graphics.strokePath();
|
|
|
|
graphics.closePath();
|
2017-09-20 22:10:37 +00:00
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
// So you can chain graphics calls
|
|
|
|
return graphics;
|
2017-09-22 18:36:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
type: 'CubicBezierCurve',
|
|
|
|
points: [
|
|
|
|
this.p0.x, this.p0.y,
|
|
|
|
this.p1.x, this.p1.y,
|
|
|
|
this.p2.x, this.p2.y,
|
|
|
|
this.p3.x, this.p3.y
|
|
|
|
]
|
|
|
|
};
|
2017-09-20 22:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = CubicBezierCurve;
|