1+ Phaser . Physics . P2 . BodyDebug = function ( game , body , settings ) {
2+ Phaser . Group . call ( this , game ) ;
3+
4+ defaultSettings = {
5+ pixelsPerLengthUnit : 20 ,
6+ debugPolygons : false ,
7+ lineWidth : 1 ,
8+ alpha : 0.5
9+ }
10+ this . settings = Phaser . Utils . extend ( defaultSettings , settings ) ;
11+
12+ this . ppu = this . settings . pixelsPerLengthUnit ;
13+ this . ppu = - 1 * this . ppu ;
14+ this . body = body ;
15+ this . canvas = new Phaser . Graphics ( game ) ;
16+ this . canvas . alpha = this . settings . alpha
17+ this . add ( this . canvas ) ;
18+
19+ this . draw ( ) ;
20+ }
21+
22+ Phaser . Physics . P2 . BodyDebug . prototype = Object . create ( Phaser . Group . prototype )
23+ Phaser . Physics . P2 . BodyDebug . prototype . constructor = Phaser . Physics . P2 . BodyDebug
24+
25+ Phaser . Utils . extend ( Phaser . Physics . P2 . BodyDebug . prototype , {
26+ update : function ( ) {
27+ this . updateSpriteTransform ( )
28+ } ,
29+
30+ updateSpriteTransform : function ( ) {
31+ this . position . x = this . body . position [ 0 ] * this . ppu ;
32+ this . position . y = this . body . position [ 1 ] * this . ppu ;
33+ return this . rotation = this . body . angle ;
34+ } ,
35+
36+ draw : function ( ) {
37+ var angle , child , color , i , j , lineColor , lw , obj , offset , sprite , v , verts , vrot , _i , _j , _ref , _ref1 , _results ;
38+ obj = this . body ;
39+ sprite = this . canvas ;
40+ sprite . clear ( ) ;
41+ color = parseInt ( this . randomPastelHex ( ) , 16 ) ;
42+ lineColor = 0xff0000 ;
43+ lw = this . lineWidth ;
44+
45+ if ( obj instanceof p2 . Body && obj . shapes . length ) {
46+ var l = obj . shapes . length
47+
48+
49+ i = 0 ;
50+ while ( i !== l ) {
51+ child = obj . shapes [ i ] ;
52+ offset = obj . shapeOffsets [ i ] ;
53+ angle = obj . shapeAngles [ i ] ;
54+ offset = offset || zero ;
55+ angle = angle || 0 ;
56+
57+ if ( child instanceof p2 . Circle ) {
58+ this . drawCircle ( sprite , offset [ 0 ] * this . ppu , - offset [ 1 ] * this . ppu , angle , child . radius * this . ppu , color , lw )
59+
60+ } else if ( child instanceof p2 . Convex ) {
61+ verts = [ ] ;
62+ vrot = p2 . vec2 . create ( ) ;
63+ for ( j = _j = 0 , _ref1 = child . vertices . length ; 0 <= _ref1 ? _j < _ref1 : _j > _ref1 ; j = 0 <= _ref1 ? ++ _j : -- _j ) {
64+ v = child . vertices [ j ] ;
65+ p2 . vec2 . rotate ( vrot , v , angle ) ;
66+ verts . push ( [ ( vrot [ 0 ] + offset [ 0 ] ) * this . ppu , - ( vrot [ 1 ] + offset [ 1 ] ) * this . ppu ] ) ;
67+ }
68+ this . drawConvex ( sprite , verts , child . triangles , lineColor , color , lw , this . settings . debugPolygons , [ offset [ 0 ] * this . ppu , - offset [ 1 ] * this . ppu ] ) ;
69+
70+ } else if ( child instanceof p2 . Plane ) {
71+ this . drawPlane ( sprite , offset [ 0 ] * this . ppu , - offset [ 1 ] * this . ppu , color , lineColor , lw * 5 , lw * 10 , lw * 10 , this . ppu * 100 , angle ) ;
72+
73+ } else if ( child instanceof p2 . Line ) {
74+ this . drawLine ( sprite , child . length * this . ppu , lineColor , lw ) ;
75+
76+ } else if ( child instanceof p2 . Rectangle ) {
77+ this . drawRectangle ( sprite , offset [ 0 ] * this . ppu , - offset [ 1 ] * this . ppu , angle , child . width * this . ppu , child . height * this . ppu , lineColor , color , lw ) ;
78+ }
79+
80+ i ++
81+ }
82+ }
83+ } ,
84+
85+ drawRectangle : function ( g , x , y , angle , w , h , color , fillColor , lineWidth ) {
86+ if ( typeof lineWidth === 'undefined' ) { lineWidth = 1 ; }
87+ if ( typeof color === 'undefined' ) { color = 0x000000 ; }
88+
89+ g . lineStyle ( lineWidth , color , 1 ) ;
90+ g . beginFill ( fillColor ) ;
91+ g . drawRect ( x - w / 2 , y - h / 2 , w , h ) ;
92+ } ,
93+
94+ drawCircle : function ( g , x , y , angle , radius , color , lineWidth ) {
95+ if ( typeof lineWidth === 'undefined' ) { lineWidth = 1 ; }
96+ if ( typeof color === 'undefined' ) { color = 0xffffff ; }
97+ g . lineStyle ( lineWidth , 0x000000 , 1 ) ;
98+ g . beginFill ( color , 1.0 ) ;
99+ g . drawCircle ( x , y , - radius ) ;
100+ g . endFill ( ) ;
101+ g . moveTo ( x , y ) ;
102+ g . lineTo ( x + radius * Math . cos ( - angle ) , y + radius * Math . sin ( - angle ) ) ;
103+ } ,
104+
105+ drawLine : function ( g , len , color , lineWidth ) {
106+ if ( typeof lineWidth === 'undefined' ) { lineWidth = 1 ; }
107+ if ( typeof color === 'undefined' ) { color = 0x000000 ; }
108+
109+ g . lineStyle ( lineWidth * 5 , color , 1 ) ;
110+ g . moveTo ( - len / 2 , 0 ) ;
111+ g . lineTo ( len / 2 , 0 ) ;
112+ } ,
113+
114+ drawConvex : function ( g , verts , triangles , color , fillColor , lineWidth , debug , offset ) {
115+ var colors , i , v , v0 , v1 , x , x0 , x1 , y , y0 , y1 ;
116+ if ( typeof lineWidth === 'undefined' ) { lineWidth = 1 ; }
117+ if ( typeof color === 'undefined' ) { color = 0x000000 ; }
118+
119+ if ( ! debug ) {
120+ g . lineStyle ( lineWidth , color , 1 ) ;
121+ g . beginFill ( fillColor ) ;
122+ i = 0 ;
123+ while ( i !== verts . length ) {
124+ v = verts [ i ] ;
125+ x = v [ 0 ] ;
126+ y = v [ 1 ] ;
127+ if ( i === 0 ) {
128+ g . moveTo ( x , - y ) ;
129+ } else {
130+ g . lineTo ( x , - y ) ;
131+ }
132+ i ++ ;
133+ }
134+ g . endFill ( ) ;
135+ if ( verts . length > 2 ) {
136+ g . moveTo ( verts [ verts . length - 1 ] [ 0 ] , - verts [ verts . length - 1 ] [ 1 ] ) ;
137+ return g . lineTo ( verts [ 0 ] [ 0 ] , - verts [ 0 ] [ 1 ] ) ;
138+ }
139+ } else {
140+ colors = [ 0xff0000 , 0x00ff00 , 0x0000ff ] ;
141+ i = 0 ;
142+ while ( i !== verts . length + 1 ) {
143+ v0 = verts [ i % verts . length ] ;
144+ v1 = verts [ ( i + 1 ) % verts . length ] ;
145+ x0 = v0 [ 0 ] ;
146+ y0 = v0 [ 1 ] ;
147+ x1 = v1 [ 0 ] ;
148+ y1 = v1 [ 1 ] ;
149+ g . lineStyle ( lineWidth , colors [ i % colors . length ] , 1 ) ;
150+ g . moveTo ( x0 , - y0 ) ;
151+ g . lineTo ( x1 , - y1 ) ;
152+ g . drawCircle ( x0 , - y0 , lineWidth * 2 ) ;
153+ i ++ ;
154+ }
155+ g . lineStyle ( lineWidth , 0x000000 , 1 ) ;
156+ return g . drawCircle ( offset [ 0 ] , offset [ 1 ] , lineWidth * 2 ) ;
157+ }
158+ } ,
159+
160+ drawPath : function ( g , path , color , fillColor , lineWidth ) {
161+ var area , i , lastx , lasty , p1x , p1y , p2x , p2y , p3x , p3y , v , x , y ;
162+ if ( typeof lineWidth === 'undefined' ) { lineWidth = 1 ; }
163+ if ( typeof color === 'undefined' ) { color = 0x000000 ; }
164+
165+ g . lineStyle ( lineWidth , color , 1 ) ;
166+ if ( typeof fillColor === "number" ) {
167+ g . beginFill ( fillColor ) ;
168+ }
169+ lastx = null ;
170+ lasty = null ;
171+ i = 0 ;
172+ while ( i < path . length ) {
173+ v = path [ i ] ;
174+ x = v [ 0 ] ;
175+ y = v [ 1 ] ;
176+ if ( x !== lastx || y !== lasty ) {
177+ if ( i === 0 ) {
178+ g . moveTo ( x , y ) ;
179+ } else {
180+ p1x = lastx ;
181+ p1y = lasty ;
182+ p2x = x ;
183+ p2y = y ;
184+ p3x = path [ ( i + 1 ) % path . length ] [ 0 ] ;
185+ p3y = path [ ( i + 1 ) % path . length ] [ 1 ] ;
186+ area = ( ( p2x - p1x ) * ( p3y - p1y ) ) - ( ( p3x - p1x ) * ( p2y - p1y ) ) ;
187+ if ( area !== 0 ) {
188+ g . lineTo ( x , y ) ;
189+ }
190+ }
191+ lastx = x ;
192+ lasty = y ;
193+ }
194+ i ++ ;
195+ }
196+ if ( typeof fillColor === "number" ) {
197+ g . endFill ( ) ;
198+ }
199+ if ( path . length > 2 && typeof fillColor === "number" ) {
200+ g . moveTo ( path [ path . length - 1 ] [ 0 ] , path [ path . length - 1 ] [ 1 ] ) ;
201+ g . lineTo ( path [ 0 ] [ 0 ] , path [ 0 ] [ 1 ] ) ;
202+ }
203+ } ,
204+
205+ drawPlane : function ( g , x0 , x1 , color , lineColor , lineWidth , diagMargin , diagSize , maxLength , angle ) {
206+ var max , xd , yd ;
207+ if ( typeof lineWidth === 'undefined' ) { lineWidth = 1 ; }
208+ if ( typeof color === 'undefined' ) { color = 0xffffff ; }
209+
210+ g . lineStyle ( lineWidth , lineColor , 11 ) ;
211+ g . beginFill ( color ) ;
212+ max = maxLength ;
213+ g . moveTo ( x0 , - x1 ) ;
214+ xd = x0 + Math . cos ( angle ) * this . game . width ;
215+ yd = x1 + Math . sin ( angle ) * this . game . height ;
216+ g . lineTo ( xd , - yd ) ;
217+ g . moveTo ( x0 , - x1 ) ;
218+ xd = x0 + Math . cos ( angle ) * - this . game . width ;
219+ yd = x1 + Math . sin ( angle ) * - this . game . height ;
220+ g . lineTo ( xd , - yd ) ;
221+ } ,
222+
223+ randomPastelHex : function ( ) {
224+ var blue , green , mix , red ;
225+ mix = [ 255 , 255 , 255 ] ;
226+ red = Math . floor ( Math . random ( ) * 256 ) ;
227+ green = Math . floor ( Math . random ( ) * 256 ) ;
228+ blue = Math . floor ( Math . random ( ) * 256 ) ;
229+ red = Math . floor ( ( red + 3 * mix [ 0 ] ) / 4 ) ;
230+ green = Math . floor ( ( green + 3 * mix [ 1 ] ) / 4 ) ;
231+ blue = Math . floor ( ( blue + 3 * mix [ 2 ] ) / 4 ) ;
232+ return this . rgbToHex ( red , green , blue ) ;
233+ } ,
234+
235+ rgbToHex : function ( r , g , b ) {
236+ return this . componentToHex ( r ) + this . componentToHex ( g ) + this . componentToHex ( b ) ;
237+ } ,
238+
239+ componentToHex : function ( c ) {
240+ var hex ;
241+ hex = c . toString ( 16 ) ;
242+
243+ if ( hex . len === 2 ) {
244+ return hex ;
245+ } else {
246+ return hex + '0' ;
247+ }
248+ }
249+ } )
0 commit comments