This commit is contained in:
Richard Davey 2018-03-05 00:21:07 +00:00
commit 0653846b3f
5 changed files with 260 additions and 9 deletions

View file

@ -0,0 +1,191 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../utils/Class');
var QuadraticBezierInter = require('../math/interpolation/QuadraticBezierInterpolation');
var Curve = require('./Curve');
var Vector2 = require('../math/Vector2');
/**
* @classdesc
* [description]
*
* @class QuadraticBezier
* @extends Phaser.Curves.Curve
* @memberOf Phaser.Curves
* @constructor
* @since 3.0.0
*
* @param {Phaser.Math.Vector2|Phaser.Math.Vector2[]} p0 - Start point, or an array of point pairs.
* @param {Phaser.Math.Vector2} p1 - Control Point 1.
* @param {Phaser.Math.Vector2} p2 - Control Point 2.
* @param {Phaser.Math.Vector2} p3 - End Point.
*/
var QuadraticBezier = new Class({
Extends: Curve,
initialize:
function QuadraticBezier (p0, p1, p2)
{
Curve.call(this, 'QuadraticBezier');
if (Array.isArray(p0))
{
p2 = new Vector2(p0[4], p0[5]);
p1 = new Vector2(p0[2], p0[3]);
p0 = new Vector2(p0[0], p0[1]);
}
/**
* [description]
*
* @name Phaser.Curves.QuadraticBezier#p0
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.p0 = p0;
/**
* [description]
*
* @name Phaser.Curves.QuadraticBezier#p1
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.p1 = p1;
/**
* [description]
*
* @name Phaser.Curves.QuadraticBezier#p2
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.p2 = p2;
},
/**
* [description]
*
* @method Phaser.Curves.QuadraticBezier#getStartPoint
* @since 3.0.0
*
* @param {Phaser.Math.Vector2} out - [description]
*
* @return {Phaser.Math.Vector2} [description]
*/
getStartPoint: function (out)
{
if (out === undefined) { out = new Vector2(); }
return out.copy(this.p0);
},
/**
* [description]
*
* @method Phaser.Curves.QuadraticBezier#getResolution
* @since 3.0.0
*
* @param {[type]} divisions - [description]
*
* @return {[type]} [description]
*/
getResolution: function (divisions)
{
return divisions;
},
/**
* [description]
*
* @method Phaser.Curves.QuadraticBezier#getPoint
* @since 3.0.0
*
* @param {[type]} t - [description]
* @param {[type]} out - [description]
*
* @return {[type]} [description]
*/
getPoint: function (t, out)
{
if (out === undefined) { out = new Vector2(); }
var p0 = this.p0;
var p1 = this.p1;
var p2 = this.p2;
return out.set(QuadraticBezierInter(t, p0.x, p1.x, p2.x), QuadraticBezierInter(t, p0.y, p1.y, p2.y));
},
/**
* [description]
*
* @method Phaser.Curves.QuadraticBezier#draw
* @since 3.0.0
*
* @param {Phaser.GameObjects.Graphics} graphics - [description]
* @param {integer} [pointsTotal=32] - [description]
*
* @return {Phaser.GameObjects.Graphics} [description]
*/
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();
// So you can chain graphics calls
return graphics;
},
/**
* [description]
*
* @method Phaser.Curves.QuadraticBezier#toJSON
* @since 3.0.0
*
* @return {object} [description]
*/
toJSON: function ()
{
return {
type: this.type,
points: [
this.p0.x, this.p0.y,
this.p1.x, this.p1.y,
this.p2.x, this.p2.y
]
};
}
});
QuadraticBezier.fromJSON = function (data)
{
var points = data.points;
var p0 = new Vector2(points[0], points[1]);
var p1 = new Vector2(points[2], points[3]);
var p2 = new Vector2(points[4], points[5]);
return new QuadraticBezier(p0, p1, p2);
};
module.exports = QuadraticBezier;

View file

@ -9,13 +9,12 @@
*/
module.exports = {
Path: require('./path/Path'),
CubicBezier: require('./CubicBezierCurve'),
Curve: require('./Curve'),
Ellipse: require('./EllipseCurve'),
Line: require('./LineCurve'),
Spline: require('./SplineCurve')
Spline: require('./SplineCurve'),
QuadraticBezier: require('./QuadraticBezier')
};

View file

@ -0,0 +1,48 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
function P0 (t, p)
{
var k = 1 - t;
return k * k * p;
}
function P1 (t, p)
{
return 2 * ( 1 - t ) * t * p;
}
function P2 (t, p)
{
return t * t * p;
}
// p0 = start point
// p1 = control point 1
// p2 = end point
// https://github.com/mrdoob/three.js/blob/master/src/extras/core/Interpolations.js
/**
* [description]
*
* @function Phaser.Math.Interpolation.QuadraticBezier
* @since 3.0.0
*
* @param {float} t - [description]
* @param {number} p0 - [description]
* @param {number} p1 - [description]
* @param {number} p2 - [description]
*
* @return {number} [description]
*/
var QuadraticBezierInterpolation = function (t, p0, p1, p2)
{
return P0(t, p0) + P1(t, p1) + P2(t, p2);
};
module.exports = QuadraticBezierInterpolation;

View file

@ -9,10 +9,9 @@
*/
module.exports = {
Bezier: require('./BezierInterpolation'),
CatmullRom: require('./CatmullRomInterpolation'),
CubicBezier: require('./CubicBezierInterpolation'),
Linear: require('./LinearInterpolation')
Linear: require('./LinearInterpolation'),
QuadraticBezier: require('./QuadraticBezierInterpolation')
};

View file

@ -67,6 +67,17 @@ var RandomDataGenerator = new Class({
*/
this.s2 = 0;
/**
* Internal var.
*
* @name Phaser.Math.RandomDataGenerator#n
* @type {number}
* @default 0
* @private
* @since 3.2.0
*/
this.n = 0;
/**
* [description]
*
@ -117,7 +128,7 @@ var RandomDataGenerator = new Class({
hash: function (data)
{
var h;
var n = 0xefc8249d;
var n = this.n;
data = data.toString();
@ -133,6 +144,8 @@ var RandomDataGenerator = new Class({
n += h * 0x100000000;// 2^32
}
this.n = n;
return (n >>> 0) * 2.3283064365386963e-10;// 2^-32
},
@ -169,9 +182,10 @@ var RandomDataGenerator = new Class({
sow: function (seeds)
{
// Always reset to default seed
this.n = 0xefc8249d;
this.s0 = this.hash(' ');
this.s1 = this.hash(this.s0);
this.s2 = this.hash(this.s1);
this.s1 = this.hash(' ');
this.s2 = this.hash(' ');
this.c = 1;
if (!seeds)