Skip to content

Commit ad07334

Browse files
committed
Path rendering routines
1 parent c30a95c commit ad07334

1 file changed

Lines changed: 148 additions & 1 deletion

File tree

v3/src/gameobjects/graphics/Graphics.js

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,70 @@ var GameObject = require('../GameObject');
33
var Components = require('../../components');
44
var Render = require('./GraphicsRender');
55

6+
/* This is specific to Graphics internal */
7+
var GraphicsPointPool = [];
8+
var GraphicsPathPool = [];
9+
var MakeGraphicsPoint = function (x, y)
10+
{
11+
var point;
12+
if (GraphicsPointPool.length > 0)
13+
{
14+
point = GraphicsPointPool.pop();
15+
point.x = x;
16+
point.y = y;
17+
}
18+
else
19+
{
20+
point = new GraphicsPoint(x, y);
21+
}
22+
return point;
23+
};
24+
var MakeGraphicsPath = function (x, y, color) {
25+
var path;
26+
if (GraphicsPathPool.length > 0)
27+
{
28+
path = GraphicsPathPool.pop();
29+
path.points.length = 0;
30+
path.points.push({x: x, y: y});
31+
}
32+
else
33+
{
34+
path = new GraphicsPath(x, y);
35+
}
36+
path.color = color;
37+
return path;
38+
};
39+
var RecycleGraphicsPoint = function (point) {
40+
var index = GraphicsPointPool.indexOf(point);
41+
if (index < 0)
42+
{
43+
GraphicsPointPool.push(point);
44+
}
45+
};
46+
var RecycleGraphicsPath = function (path) {
47+
var index = GraphicsPathPool.indexOf(path);
48+
if (index < 0)
49+
{
50+
GraphicsPathPool.push(path);
51+
}
52+
};
53+
var GraphicsPoint = function (x, y)
54+
{
55+
this.x = x;
56+
this.y = y;
57+
};
58+
var GraphicsPath = function (x, y, color)
59+
{
60+
this.color = color;
61+
this.points = [];
62+
this.points.push(MakeGraphicsPoint(x, y));
63+
};
64+
var lerp = function (norm, min, max)
65+
{
66+
return (max - min) * norm + min;
67+
};
68+
var cos = Math.cos;
69+
var sin = Math.sin;
670
var Graphics = new Class({
771

872
Mixins: [
@@ -19,9 +83,92 @@ var Graphics = new Class({
1983
{
2084
GameObject.call(this, state);
2185

86+
this._lastPath = null;
87+
this._pathArray = [];
88+
this._lastColor = 0xFFFFFFFF;
89+
2290
this.setPosition(x, y);
23-
}
91+
},
92+
93+
arc: function (x, y, radius, startAngle, endAngle, anticlockwise) {
94+
var iteration = 0;
95+
var iterStep = 0.01;
96+
var tx = 0;
97+
var ty = 0;
98+
var ta = 0;
99+
var moveTo = this.moveTo;
100+
var lineTo = this.lineTo;
101+
while (iteration < 1) {
102+
ta = lerp(iteration, startAngle, endAngle);
103+
tx = x + cos(ta) * radius;
104+
ty = y + sin(ta) * radius;
105+
if (iteration === 0) {
106+
moveTo(tx, ty);
107+
} else {
108+
lineTo(tx, ty);
109+
}
110+
iteration += iterStep;
111+
}
112+
},
113+
114+
beginFill: function (color) {
115+
this.clear();
116+
this._lastColor = color;
117+
},
118+
119+
endFill: function () {
120+
var lastPath = this._lastPath;
121+
if (lastPath !== null && lastPath.point.length > 0)
122+
{
123+
var firstPoint = lastPath.points[0];
124+
var lastPoint = lastPath.points[lastPath.points.length - 1];
125+
lastPath.points.push(firstPoint);
126+
this.moveTo(lastPoint.x, lastPoint.y);
127+
}
128+
},
129+
130+
clear: function () {
131+
var path;
132+
var points;
133+
var pathArray = this._pathArray;
134+
var pointIndex = 0;
135+
for (var pathIndex = 0, pathLength = pathArray.length; pathIndex < pathLength; ++pathIndex)
136+
{
137+
path = pathArray[pathIndex];
138+
points = path.points;
139+
RecycleGraphicsPoint(path);
140+
for (pointIndex = 0, pointsLength = points.length; pointIndex < pointsLength; ++pointIndex)
141+
{
142+
RecycleGraphicsPoint(points[pointIndex]);
143+
}
144+
points.length = 0;
145+
}
146+
pathArray.length = 0;
147+
this._lastColor = 0xFFFFFFFF;
148+
this._lastPath = null;
149+
},
24150

151+
drawCircle: function (x, y, diameter) {},
152+
153+
drawPolygon: function (path) {},
154+
155+
drawRect: function (x, y, width, height) {},
156+
157+
lineTo: function (x, y) {
158+
if (this._lastPath !== null)
159+
{
160+
this._lastPath.points.push(MakeGraphicsPath(x, y, this._lastColor));
161+
}
162+
else
163+
{
164+
this.moveTo(x, y);
165+
}
166+
},
167+
168+
moveTo: function (x, y) {
169+
this._lastPath = MakeGraphicsPath(x, y, this._lastColor);
170+
this._pathArray.push(this._lastPath);
171+
}
25172
});
26173

27174
module.exports = Graphics;

0 commit comments

Comments
 (0)