@@ -2,71 +2,8 @@ var Class = require('../../utils/Class');
22var GameObject = require ( '../GameObject' ) ;
33var Components = require ( '../../components' ) ;
44var Render = require ( './GraphicsRender' ) ;
5+ var Commands = require ( './Commands' ) ;
56
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 ;
707var Graphics = new Class ( {
718
729 Mixins : [
@@ -82,92 +19,68 @@ var Graphics = new Class({
8219 function Graphics ( state , x , y )
8320 {
8421 GameObject . call ( this , state ) ;
85-
86- this . _lastPath = null ;
87- this . _pathArray = [ ] ;
88- this . _lastColor = 0xFFFFFFFF ;
89-
22+ this . commandBuffer = [ ] ;
9023 this . setPosition ( x , y ) ;
9124 } ,
9225
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- }
26+ arc : function ( x , y , radius , startAngle , endAngle , anticlockwise )
27+ {
28+ this . commandBuffer . push (
29+ Commands . ARC ,
30+ x , y , radius , startAngle , endAngle , anticlockwise
31+ ) ;
11232 } ,
11333
114- beginFill : function ( color ) {
115- this . clear ( ) ;
116- this . _lastColor = color ;
34+ beginFill : function ( color )
35+ {
36+ this . commandBuffer . push (
37+ Commands . BEGIN_FILL ,
38+ color
39+ ) ;
11740 } ,
11841
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- }
42+ endFill : function ( )
43+ {
44+ this . commandBuffer . push (
45+ Commands . END_FILL
46+ ) ;
12847 } ,
12948
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 ;
49+ drawCircle : function ( x , y , radius )
50+ {
51+ this . commandBuffer . push (
52+ Commands . DRAW_CIRCLE ,
53+ x , y , radius
54+ ) ;
14955 } ,
15056
151- drawCircle : function ( x , y , diameter ) { } ,
152-
153- drawPolygon : function ( path ) { } ,
57+ drawRect : function ( x , y , width , height )
58+ {
59+ this . commandBuffer . push (
60+ Commands . DRAW_RECT ,
61+ x , y , width , height
62+ ) ;
63+ } ,
15464
155- drawRect : function ( x , y , width , height ) { } ,
65+ lineTo : function ( x , y )
66+ {
67+ this . commandBuffer . push (
68+ Commands . LINE_TO ,
69+ x , y
70+ ) ;
71+ } ,
15672
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- }
73+ moveTo : function ( x , y )
74+ {
75+ this . commandBuffer . push (
76+ Commands . MOVE_TO ,
77+ x , y
78+ ) ;
16679 } ,
16780
168- moveTo : function ( x , y ) {
169- this . _lastPath = MakeGraphicsPath ( x , y , this . _lastColor ) ;
170- this . _pathArray . push ( this . _lastPath ) ;
81+ clear : function ( )
82+ {
83+ commandBuffer . length = 0 ;
17184 }
17285} ) ;
17386
0 commit comments