33*/
44Phaser . Plugin . VirtualJoystick = function ( game , parent ) {
55
6- Phaser . Plugin . call ( this , game , parent ) ;
6+ Phaser . Plugin . call ( this , game , parent ) ;
77
8- this . x = 0 ;
9- this . y = 0 ;
10- this . limit = 10 ;
8+ this . x = 0 ;
9+ this . y = 0 ;
10+ this . limit = 10 ;
1111
12- this . baseCircle ;
12+ this . baseCircle ;
1313
14- this . baseBMD ;
15- this . nubBMD ;
14+ this . baseBMD ;
15+ this . nubBMD ;
1616
17- this . base ;
18- this . nub ;
17+ this . base ;
18+ this . nub ;
1919
20- this . baseCenter ;
21- this . nubCenter ;
20+ this . baseCenter ;
21+ this . nubCenter ;
2222
23- this . isDragging = false ;
23+ this . isDragging = false ;
2424
25- this . angle = 0 ;
26- this . distance = 0 ;
27- this . force = 0 ;
28- this . deltaX = 0 ;
29- this . deltaY = 0 ;
30- this . speed = 0 ;
31-
25+ this . angle = 0 ;
26+ this . distance = 0 ;
27+ this . force = 0 ;
28+ this . deltaX = 0 ;
29+ this . deltaY = 0 ;
30+ this . speed = 0 ;
31+
3232} ;
3333
34- // Extends the Phaser.Plugin template, setting up values we need
3534Phaser . Plugin . VirtualJoystick . prototype = Object . create ( Phaser . Plugin . prototype ) ;
3635Phaser . Plugin . VirtualJoystick . prototype . constructor = Phaser . Plugin . VirtualJoystick ;
3736
3837Phaser . Plugin . VirtualJoystick . prototype . init = function ( x , y , diameter , limit ) {
3938
40- if ( typeof diameter === 'undefined' ) { diameter = 80 ; }
41- if ( typeof limit === 'undefined' ) { limit = Math . floor ( diameter / 2 ) ; }
39+ if ( typeof diameter === 'undefined' ) { diameter = 80 ; }
40+ if ( typeof limit === 'undefined' ) { limit = Math . floor ( diameter / 2 ) ; }
4241
43- this . x = x ;
44- this . y = y ;
42+ this . x = x ;
43+ this . y = y ;
4544
46- var radius = Math . floor ( diameter / 2 ) ;
47- var nr = radius - 4 ;
45+ var radius = Math . floor ( diameter / 2 ) ;
46+ var nr = radius - 4 ;
4847
49- this . baseCircle = new Phaser . Circle ( x , y , diameter ) ;
48+ this . baseCircle = new Phaser . Circle ( x , y , diameter ) ;
5049
51- this . baseBMD = this . game . make . bitmapData ( diameter , diameter ) ;
52- this . nubBMD = this . game . make . bitmapData ( nr * 2 , nr * 2 ) ;
50+ this . baseBMD = this . game . make . bitmapData ( diameter , diameter ) ;
51+ this . nubBMD = this . game . make . bitmapData ( nr * 2 , nr * 2 ) ;
5352
54- this . baseBMD . circle ( radius , radius , radius , 'rgb(255, 0, 0)' ) ;
55- this . nubBMD . circle ( nr , nr , nr , 'rgb(0, 255, 0)' ) ;
53+ this . baseBMD . circle ( radius , radius , radius , 'rgb(255, 0, 0)' ) ;
54+ this . nubBMD . circle ( nr , nr , nr , 'rgb(0, 255, 0)' ) ;
5655
57- // Base
58- this . base = this . game . add . sprite ( x , y , this . baseBMD ) ;
59- this . base . anchor . set ( 0.5 ) ;
56+ // Base
57+ this . base = this . game . add . sprite ( x , y , this . baseBMD ) ;
58+ this . base . anchor . set ( 0.5 ) ;
6059
61- // Nub
62- this . nub = this . game . add . sprite ( x , y , this . nubBMD ) ;
63- this . nub . anchor . set ( 0.5 ) ;
60+ // Nub
61+ this . nub = this . game . add . sprite ( x , y , this . nubBMD ) ;
62+ this . nub . anchor . set ( 0.5 ) ;
6463
65- this . nub . inputEnabled = true ;
64+ this . nub . inputEnabled = true ;
6665
67- this . nub . events . onInputDown . add ( this . startDrag , this ) ;
68- this . nub . events . onInputUp . add ( this . stopDrag , this ) ;
66+ this . nub . events . onInputDown . add ( this . startDrag , this ) ;
67+ this . nub . events . onInputUp . add ( this . stopDrag , this ) ;
6968
70- // Need to find a way to not hog this callback
71- this . game . input . setMoveCallback ( this . move , this ) ;
69+ // Need to find a way to not hog this callback
70+ this . game . input . setMoveCallback ( this . move , this ) ;
7271
73- this . isDragging = false ;
72+ this . isDragging = false ;
7473
75- this . distance = 0 ;
76- this . speed = 0 ;
77- this . force = 0 ;
78- this . limit = limit ;
79- this . limitPoint = new Phaser . Point ( ) ;
74+ this . distance = 0 ;
75+ this . speed = 0 ;
76+ this . force = 0 ;
77+ this . limit = limit ;
78+ this . limitPoint = new Phaser . Point ( ) ;
8079
81- this . location = new Phaser . Point ( ) ;
80+ this . location = new Phaser . Point ( ) ;
8281
8382}
8483
8584Phaser . Plugin . VirtualJoystick . prototype . startDrag = function ( nub , pointer ) {
8685
87- this . isDragging = true ;
86+ this . isDragging = true ;
8887
89- this . location . set ( pointer . x , pointer . y ) ;
90- this . distance = Phaser . Point . distance ( this . base , this . location , true ) ;
91- this . angle = this . game . math . wrapAngle ( this . location . angle ( this . base , true ) + 180 ) ;
92- this . force = this . game . math . percent ( this . distance , this . limit ) ;
88+ this . location . set ( pointer . x , pointer . y ) ;
89+ this . distance = Phaser . Point . distance ( this . base , this . location , true ) ;
90+ this . angle = this . game . math . wrapAngle ( this . location . angle ( this . base , true ) + 180 ) ;
91+ this . force = this . game . math . percent ( this . distance , this . limit ) ;
9392
9493} ;
9594
9695Phaser . Plugin . VirtualJoystick . prototype . stopDrag = function ( nub , pointer ) {
9796
98- this . isDragging = false ;
97+ this . isDragging = false ;
9998
100- this . distance = 0 ;
101- this . angle = 0 ;
102- this . force = 0 ;
99+ this . distance = 0 ;
100+ this . angle = 0 ;
101+ this . force = 0 ;
103102
104- this . nub . x = this . base . x ;
105- this . nub . y = this . base . y ;
106-
107- this . limitPoint . set ( this . base . x , this . base . y ) ;
103+ this . nub . x = this . base . x ;
104+ this . nub . y = this . base . y ;
105+
106+ this . limitPoint . set ( this . base . x , this . base . y ) ;
108107
109108} ;
110109
111110Phaser . Plugin . VirtualJoystick . prototype . move = function ( pointer , x , y ) {
112111
113- if ( ! this . isDragging )
114- {
115- return ;
116- }
112+ if ( ! this . isDragging )
113+ {
114+ return ;
115+ }
117116
118- this . location . set ( x , y ) ;
117+ this . location . set ( x , y ) ;
119118
120- this . distance = Phaser . Point . distance ( this . base , this . location , true ) ;
119+ this . distance = Phaser . Point . distance ( this . base , this . location , true ) ;
121120
122- this . angle = this . game . math . wrapAngle ( this . location . angle ( this . base , true ) + 180 ) ;
121+ this . angle = this . game . math . wrapAngle ( this . location . angle ( this . base , true ) + 180 ) ;
123122
124- this . force = this . game . math . percent ( this . distance , this . limit ) ;
123+ this . force = this . game . math . percent ( this . distance , this . limit ) ;
125124
126- if ( this . distance < this . limit )
127- {
128- this . limitPoint . copyFrom ( this . location ) ;
129- }
130- else
131- {
132- this . baseCircle . circumferencePoint ( this . angle , true , this . limitPoint ) ;
133- }
125+ if ( this . distance < this . limit )
126+ {
127+ this . limitPoint . copyFrom ( this . location ) ;
128+ }
129+ else
130+ {
131+ this . baseCircle . circumferencePoint ( this . angle , true , this . limitPoint ) ;
132+ }
134133
135- this . nub . position . set ( this . limitPoint . x , this . limitPoint . y ) ;
134+ this . nub . position . set ( this . limitPoint . x , this . limitPoint . y ) ;
136135
137136} ;
138137
@@ -153,13 +152,13 @@ Phaser.Plugin.VirtualJoystick.prototype.setVelocity = function (sprite, minSpeed
153152
154153 if ( this . force === 0 && minSpeed === 0 )
155154 {
156- sprite . body . velocity . set ( 0 , 0 ) ;
155+ sprite . body . velocity . set ( 0 , 0 ) ;
157156 }
158157 else
159158 {
160- var speed = ( maxSpeed - minSpeed ) * this . force ;
159+ var speed = ( maxSpeed - minSpeed ) * this . force ;
161160
162- sprite . body . velocity . set ( ( Math . cos ( this . game . math . degToRad ( this . angle ) ) * speed ) , ( Math . sin ( this . game . math . degToRad ( this . angle ) ) * speed ) ) ;
161+ sprite . body . velocity . set ( ( Math . cos ( this . game . math . degToRad ( this . angle ) ) * speed ) , ( Math . sin ( this . game . math . degToRad ( this . angle ) ) * speed ) ) ;
163162 }
164163
165164 return sprite ;
@@ -172,15 +171,15 @@ Phaser.Plugin.VirtualJoystick.prototype.update = function () {
172171
173172Phaser . Plugin . VirtualJoystick . prototype . render = function ( ) {
174173
175- this . game . debug . text ( 'force: ' + this . force , 32 , 32 ) ;
176- // this.game.debug.text(this.distance, 32, 32);
177- // this.game.debug.text(this.angle, 132, 32);
174+ this . game . debug . text ( 'force: ' + this . force , 32 , 32 ) ;
175+ // this.game.debug.text(this.distance, 32, 32);
176+ // this.game.debug.text(this.angle, 132, 32);
178177
179- // this.game.debug.text('x: ' + this.location.x + ' y: ' + this.location.y, 32, 64);
180- // this.game.debug.text('x: ' + this.limitPoint.x + ' y: ' + this.limitPoint.y, 32, 64);
181- // this.game.debug.text('x: ' + this.prev.x + ' y: ' + this.prev.y, 32, 64);
182- // this.game.debug.text(Phaser.Point.distance(this.base, this.prev, true), 32, 96);
178+ // this.game.debug.text('x: ' + this.location.x + ' y: ' + this.location.y, 32, 64);
179+ // this.game.debug.text('x: ' + this.limitPoint.x + ' y: ' + this.limitPoint.y, 32, 64);
180+ // this.game.debug.text('x: ' + this.prev.x + ' y: ' + this.prev.y, 32, 64);
181+ // this.game.debug.text(Phaser.Point.distance(this.base, this.prev, true), 32, 96);
183182
184- this . game . debug . geom ( this . limitPoint , 'rgb(255,255,0)' ) ;
183+ this . game . debug . geom ( this . limitPoint , 'rgb(255,255,0)' ) ;
185184
186185} ;
0 commit comments