2017-09-21 01:31:11 +00:00
|
|
|
// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog)
|
|
|
|
|
|
|
|
var CatmullRom = require('../../../math/CatmullRom');
|
|
|
|
var Class = require('../../../utils/Class');
|
|
|
|
var Curve = require('../Curve');
|
|
|
|
var Vector2 = require('../../../math/Vector2');
|
|
|
|
|
|
|
|
// Phaser.Curves.Spline
|
|
|
|
|
|
|
|
var SplineCurve = new Class({
|
|
|
|
|
|
|
|
Extends: Curve,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
// Array of vec2s
|
|
|
|
function SplineCurve (points)
|
|
|
|
{
|
|
|
|
if (points === undefined) { points = []; }
|
|
|
|
|
2017-10-02 21:42:47 +00:00
|
|
|
Curve.call(this, 'SplineCurve');
|
2017-09-21 01:31:11 +00:00
|
|
|
|
2017-09-27 21:14:59 +00:00
|
|
|
// if points is an array of numbers ...
|
|
|
|
|
|
|
|
this.points = [];
|
|
|
|
|
|
|
|
this.addPoints(points);
|
|
|
|
},
|
|
|
|
|
|
|
|
addPoints: function (points)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < points.length; i++)
|
|
|
|
{
|
|
|
|
var p = new Vector2();
|
|
|
|
|
|
|
|
if (typeof points[i] === 'number')
|
|
|
|
{
|
|
|
|
p.x = points[i];
|
|
|
|
p.y = points[i + 1];
|
|
|
|
i++;
|
|
|
|
}
|
2017-09-29 10:41:49 +00:00
|
|
|
else if (Array.isArray(points[i]))
|
2017-09-27 21:14:59 +00:00
|
|
|
{
|
|
|
|
// An array of arrays?
|
|
|
|
p.x = points[i][0];
|
|
|
|
p.y = points[i][1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.x = points[i].x;
|
|
|
|
p.y = points[i].y;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.points.push(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
2017-09-21 01:31:11 +00:00
|
|
|
},
|
|
|
|
|
2017-09-22 16:41:32 +00:00
|
|
|
addPoint: function (x, y)
|
|
|
|
{
|
|
|
|
var vec = new Vector2(x, y);
|
|
|
|
|
|
|
|
this.points.push(vec);
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
},
|
|
|
|
|
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.points[0]);
|
2017-09-22 15:41:11 +00:00
|
|
|
},
|
|
|
|
|
2017-09-21 16:12:16 +00:00
|
|
|
getResolution: function (divisions)
|
|
|
|
{
|
|
|
|
return divisions * this.points.length;
|
|
|
|
},
|
|
|
|
|
2017-09-21 01:31:11 +00:00
|
|
|
getPoint: function (t, out)
|
|
|
|
{
|
|
|
|
if (out === undefined) { out = new Vector2(); }
|
|
|
|
|
|
|
|
var points = this.points;
|
|
|
|
|
|
|
|
var point = (points.length - 1) * t;
|
|
|
|
|
|
|
|
var intPoint = Math.floor(point);
|
|
|
|
|
|
|
|
var weight = point - intPoint;
|
|
|
|
|
2017-09-22 15:41:11 +00:00
|
|
|
var p0 = points[(intPoint === 0) ? intPoint : intPoint - 1];
|
|
|
|
var p1 = points[intPoint];
|
|
|
|
var p2 = points[(intPoint > points.length - 2) ? points.length - 1 : intPoint + 1];
|
|
|
|
var p3 = points[(intPoint > points.length - 3) ? points.length - 1 : intPoint + 2];
|
2017-09-21 01:31:11 +00:00
|
|
|
|
|
|
|
return out.set(CatmullRom(weight, p0.x, p1.x, p2.x, p3.x), CatmullRom(weight, p0.y, p1.y, p2.y, p3.y));
|
2017-09-22 18:36:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
var points = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < this.points.length; i++)
|
|
|
|
{
|
|
|
|
points.push(this.points[i].x);
|
|
|
|
points.push(this.points[i].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2017-10-02 21:42:47 +00:00
|
|
|
type: this.type,
|
2017-09-22 18:36:00 +00:00
|
|
|
points: points
|
|
|
|
};
|
2017-09-21 01:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-10-02 21:42:47 +00:00
|
|
|
SplineCurve.fromJSON = function (data)
|
|
|
|
{
|
|
|
|
return new SplineCurve(data.points);
|
|
|
|
};
|
|
|
|
|
2017-09-21 01:31:11 +00:00
|
|
|
module.exports = SplineCurve;
|