|
| 1 | +// Based on the three.js Curve classes created by [zz85](http://www.lab4games.net/zz85/blog) |
| 2 | + |
| 3 | +var Class = require('../../utils/Class'); |
| 4 | +var CubicBezier = require('../../math/interpolation/CubicBezierInterpolation'); |
| 5 | +var Curve = require('../curve/Curve'); |
| 6 | +var Vector2 = require('../../math/Vector2'); |
| 7 | + |
| 8 | +// Phaser.Curves.CubicBezier |
| 9 | + |
| 10 | +var CubicBezierCurve = new Class({ |
| 11 | + |
| 12 | + Extends: Curve, |
| 13 | + |
| 14 | + initialize: |
| 15 | + |
| 16 | + // p0 = start point (or an array of point pairs) |
| 17 | + // p1 = control point 1 |
| 18 | + // p2 = control point 2 |
| 19 | + // p3 = end point |
| 20 | + function CubicBezierCurve (p0, p1, p2, p3) |
| 21 | + { |
| 22 | + Curve.call(this, 'CubicBezierCurve'); |
| 23 | + |
| 24 | + if (Array.isArray(p0)) |
| 25 | + { |
| 26 | + p3 = new Vector2(p0[6], p0[7]); |
| 27 | + p2 = new Vector2(p0[4], p0[5]); |
| 28 | + p1 = new Vector2(p0[2], p0[3]); |
| 29 | + p0 = new Vector2(p0[0], p0[1]); |
| 30 | + } |
| 31 | + |
| 32 | + this.p0 = p0; |
| 33 | + this.p1 = p1; |
| 34 | + this.p2 = p2; |
| 35 | + this.p3 = p3; |
| 36 | + }, |
| 37 | + |
| 38 | + getStartPoint: function (out) |
| 39 | + { |
| 40 | + if (out === undefined) { out = new Vector2(); } |
| 41 | + |
| 42 | + return out.copy(this.p0); |
| 43 | + }, |
| 44 | + |
| 45 | + getResolution: function (divisions) |
| 46 | + { |
| 47 | + return divisions; |
| 48 | + }, |
| 49 | + |
| 50 | + getPoint: function (t, out) |
| 51 | + { |
| 52 | + if (out === undefined) { out = new Vector2(); } |
| 53 | + |
| 54 | + var p0 = this.p0; |
| 55 | + var p1 = this.p1; |
| 56 | + var p2 = this.p2; |
| 57 | + var p3 = this.p3; |
| 58 | + |
| 59 | + return out.set(CubicBezier(t, p0.x, p1.x, p2.x, p3.x), CubicBezier(t, p0.y, p1.y, p2.y, p3.y)); |
| 60 | + }, |
| 61 | + |
| 62 | + draw: function (graphics, pointsTotal) |
| 63 | + { |
| 64 | + if (pointsTotal === undefined) { pointsTotal = 32; } |
| 65 | + |
| 66 | + var points = this.getPoints(pointsTotal); |
| 67 | + |
| 68 | + graphics.beginPath(); |
| 69 | + graphics.moveTo(this.p0.x, this.p0.y); |
| 70 | + |
| 71 | + for (var i = 1; i < points.length; i++) |
| 72 | + { |
| 73 | + graphics.lineTo(points[i].x, points[i].y); |
| 74 | + } |
| 75 | + |
| 76 | + graphics.strokePath(); |
| 77 | + graphics.closePath(); |
| 78 | + |
| 79 | + // So you can chain graphics calls |
| 80 | + return graphics; |
| 81 | + }, |
| 82 | + |
| 83 | + toJSON: function () |
| 84 | + { |
| 85 | + return { |
| 86 | + type: this.type, |
| 87 | + points: [ |
| 88 | + this.p0.x, this.p0.y, |
| 89 | + this.p1.x, this.p1.y, |
| 90 | + this.p2.x, this.p2.y, |
| 91 | + this.p3.x, this.p3.y |
| 92 | + ] |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | +}); |
| 97 | + |
| 98 | +CubicBezierCurve.fromJSON = function (data) |
| 99 | +{ |
| 100 | + var points = data.points; |
| 101 | + |
| 102 | + var p0 = new Vector2(points[0], points[1]); |
| 103 | + var p1 = new Vector2(points[2], points[3]); |
| 104 | + var p2 = new Vector2(points[4], points[5]); |
| 105 | + var p3 = new Vector2(points[6], points[7]); |
| 106 | + |
| 107 | + return new CubicBezierCurve(p0, p1, p2, p3); |
| 108 | +}; |
| 109 | + |
| 110 | +module.exports = CubicBezierCurve; |
0 commit comments