Skip to content

Commit 56e026e

Browse files
committed
Start of the Path class.
1 parent b399d3d commit 56e026e

2 files changed

Lines changed: 203 additions & 0 deletions

File tree

v3/src/paths/Path.js

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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 LineCurve = require('./curves/line/LineCurve');
5+
var Vector2 = require('../math/Vector2');
6+
7+
// Local cache vars
8+
9+
// var tmpVec2A = new Vector2();
10+
// var tmpVec2B = new Vector2();
11+
12+
var Path = new Class({
13+
14+
initialize:
15+
16+
function Path ()
17+
{
18+
this.curves = [];
19+
20+
this.cacheLengths = [];
21+
22+
// Automatically closes the path
23+
this.autoClose = false;
24+
},
25+
26+
add: function (curve)
27+
{
28+
this.curves.push(curve);
29+
30+
return this;
31+
},
32+
33+
closePath: function ()
34+
{
35+
// Add a line curve if start and end of lines are not connected
36+
var startPoint = this.curves[0].getPoint(0);
37+
var endPoint = this.curves[this.curves.length - 1].getPoint(1);
38+
39+
if (!startPoint.equals(endPoint))
40+
{
41+
// This will copy a reference to the vectors, which probably isn't sensible
42+
this.curves.push(new LineCurve(endPoint, startPoint));
43+
}
44+
45+
return this;
46+
},
47+
48+
// To get accurate point with reference to
49+
// entire path distance at time t,
50+
// following has to be done:
51+
52+
// 1. Length of each sub path have to be known
53+
// 2. Locate and identify type of curve
54+
// 3. Get t for the curve
55+
// 4. Return curve.getPointAt(t')
56+
57+
getPoint: function (t, out)
58+
{
59+
if (out === undefined) { out = new Vector2(); }
60+
61+
var d = t * this.getLength();
62+
var curveLengths = this.getCurveLengths();
63+
var i = 0;
64+
65+
// To think about boundaries points.
66+
67+
while (i < curveLengths.length)
68+
{
69+
if (curveLengths[i] >= d)
70+
{
71+
var diff = curveLengths[i] - d;
72+
var curve = this.curves[i];
73+
74+
var segmentLength = curve.getLength();
75+
var u = (segmentLength === 0) ? 0 : 1 - diff / segmentLength;
76+
77+
return curve.getPointAt(u, out);
78+
}
79+
80+
i++;
81+
}
82+
83+
// loop where sum != 0, sum > d , sum+1 <d
84+
return null;
85+
},
86+
87+
// We cannot use the default THREE.Curve getPoint() with getLength() because in
88+
// THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
89+
// getPoint() depends on getLength
90+
91+
getLength: function ()
92+
{
93+
var lens = this.getCurveLengths();
94+
95+
return lens[lens.length - 1];
96+
},
97+
98+
// cacheLengths must be recalculated.
99+
updateArcLengths: function ()
100+
{
101+
this.needsUpdate = true;
102+
this.cacheLengths = null;
103+
104+
this.getCurveLengths();
105+
},
106+
107+
// Compute lengths and cache them
108+
// We cannot overwrite getLengths() because UtoT mapping uses it.
109+
110+
getCurveLengths: function ()
111+
{
112+
// We use cache values if curves and cache array are same length
113+
114+
if (this.cacheLengths.length === this.curves.length)
115+
{
116+
return this.cacheLengths;
117+
}
118+
119+
// Get length of sub-curve
120+
// Push sums into cached array
121+
122+
var lengths = [];
123+
var sums = 0;
124+
125+
for (var i = 0; i < this.curves.length; i++)
126+
{
127+
sums += this.curves[i].getLength();
128+
129+
lengths.push(sums);
130+
}
131+
132+
this.cacheLengths = lengths;
133+
134+
return lengths;
135+
},
136+
137+
getSpacedPoints: function (divisions)
138+
{
139+
if (divisions === undefined) { divisions = 40; }
140+
141+
var points = [];
142+
143+
for (var i = 0; i <= divisions; i++)
144+
{
145+
points.push(this.getPoint(i / divisions));
146+
}
147+
148+
if (this.autoClose)
149+
{
150+
points.push(points[0]);
151+
}
152+
153+
return points;
154+
},
155+
156+
getPoints: function (divisions)
157+
{
158+
if (divisions === undefined) { divisions = 12; }
159+
160+
var points = [];
161+
var last;
162+
163+
for (var i = 0; i < this.curves.length; i++)
164+
{
165+
var curve = this.curves[i];
166+
167+
var resolution = curve.getResolution(divisions);
168+
169+
var pts = curve.getPoints(resolution);
170+
171+
for (var j = 0; j < pts.length; j++)
172+
{
173+
var point = pts[j];
174+
175+
if (last && last.equals(point))
176+
{
177+
// ensures no consecutive points are duplicates
178+
continue;
179+
}
180+
181+
points.push(point);
182+
183+
last = point;
184+
}
185+
}
186+
187+
if (this.autoClose && points.length > 1 && !points[points.length - 1].equals(points[0]))
188+
{
189+
points.push(points[0]);
190+
}
191+
192+
return points;
193+
}
194+
195+
});
196+
197+
module.exports = Path;

v3/src/paths/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
// Phaser.Paths
22

3+
// Phaser.Curves.Path ? or ...
4+
// Phaser.Paths.Path ?
5+
36
module.exports = {
7+
8+
9+
410
};

0 commit comments

Comments
 (0)