11var Class = require ( '../../utils/Class' ) ;
2+ var DegToRad = require ( '../../math/DegToRad' ) ;
23var GetBoolean = require ( '../../tween/builder/GetBoolean' ) ;
34var GetValue = require ( '../../utils/object/GetValue' ) ;
45var Sprite = require ( '../sprite/Sprite' ) ;
6+ var TWEEN_CONST = require ( '../../tween/tween/const' ) ;
57var Vector2 = require ( '../../math/Vector2' ) ;
68
79var PathFollower = new Class ( {
@@ -17,13 +19,18 @@ var PathFollower = new Class({
1719 this . path = path ;
1820
1921 this . rotateToPath = false ;
22+
23+ this . pathRotationVerticalAdjust = false ;
24+
2025 this . pathRotationOffset = 0 ;
2126
2227 this . pathOffset = new Vector2 ( x , y ) ;
2328
2429 this . pathVector = new Vector2 ( ) ;
2530
2631 this . pathTween ;
32+
33+ this . _prevDirection = TWEEN_CONST . PLAYING_FORWARD ;
2734 } ,
2835
2936 setPath : function ( path , config )
@@ -45,13 +52,16 @@ var PathFollower = new Class({
4552 return this ;
4653 } ,
4754
48- // rotation offset in radians
49- setRotateToPath : function ( value , offset )
55+ // rotation offset in degrees
56+ setRotateToPath : function ( value , offset , verticalAdjust )
5057 {
5158 if ( offset === undefined ) { offset = 0 ; }
59+ if ( verticalAdjust === undefined ) { verticalAdjust = false ; }
5260
5361 this . rotateToPath = value ;
62+
5463 this . pathRotationOffset = offset ;
64+ this . pathRotationVerticalAdjust = verticalAdjust ;
5565
5666 return this ;
5767 } ,
@@ -87,6 +97,7 @@ var PathFollower = new Class({
8797
8898 this . rotateToPath = GetBoolean ( config , 'rotateToPath' , false ) ;
8999 this . pathRotationOffset = GetValue ( config , 'rotationOffset' , 0 ) ;
100+ this . pathRotationVerticalAdjust = GetBoolean ( config , 'verticalAdjust' , false ) ;
90101
91102 this . pathTween = this . scene . sys . tweens . addCounter ( config ) ;
92103
@@ -96,6 +107,16 @@ var PathFollower = new Class({
96107 this . pathOffset . x = this . x - this . pathOffset . x ;
97108 this . pathOffset . y = this . y - this . pathOffset . y ;
98109
110+ this . _prevDirection = TWEEN_CONST . PLAYING_FORWARD ;
111+
112+ if ( this . rotateToPath )
113+ {
114+ // Set the rotation now (in case the tween has a delay on it, etc)
115+ var nextPoint = this . path . getPoint ( 0.1 ) ;
116+
117+ this . rotation = Math . atan2 ( nextPoint . y - this . y , nextPoint . x - this . x ) + DegToRad ( this . pathRotationOffset ) ;
118+ }
119+
99120 return this ;
100121 } ,
101122
@@ -141,8 +162,16 @@ var PathFollower = new Class({
141162
142163 var tween = this . pathTween ;
143164
144- if ( tween && tween . isPlaying ( ) )
165+ if ( tween )
145166 {
167+ var tweenData = tween . data [ 0 ] ;
168+
169+ if ( tweenData . state !== TWEEN_CONST . PLAYING_FORWARD && tweenData . state !== TWEEN_CONST . PLAYING_BACKWARD )
170+ {
171+ // If delayed, etc then bail out
172+ return ;
173+ }
174+
146175 var pathVector = this . pathVector ;
147176
148177 this . path . getPoint ( tween . getValue ( ) , pathVector ) ;
@@ -154,9 +183,31 @@ var PathFollower = new Class({
154183
155184 this . setPosition ( pathVector . x , pathVector . y ) ;
156185
186+ var speedX = this . x - oldX ;
187+ var speedY = this . y - oldY ;
188+
189+ if ( speedX === 0 && speedY === 0 )
190+ {
191+ // Bail out early
192+ return ;
193+ }
194+
195+ if ( tweenData . state !== this . _prevDirection )
196+ {
197+ // We've changed direction, so don't do a rotate this frame
198+ this . _prevDirection = tweenData . state ;
199+
200+ return ;
201+ }
202+
157203 if ( this . rotateToPath )
158204 {
159- this . rotation = Math . atan2 ( this . y - oldY , this . x - oldX ) ;
205+ this . rotation = Math . atan2 ( speedY , speedX ) + DegToRad ( this . pathRotationOffset ) ;
206+
207+ if ( this . pathRotationVerticalAdjust )
208+ {
209+ this . flipY = ( this . rotation !== 0 && tweenData . state === TWEEN_CONST . PLAYING_BACKWARD ) ;
210+ }
160211 }
161212 }
162213 }
0 commit comments