@@ -19,40 +19,38 @@ var Tween = function (manager, target, key, value)
1919 this . repeat = 0 ;
2020 this . loop = false ;
2121 this . delay = 0 ;
22+ this . repeatDelay = 0 ;
2223 this . onCompleteDelay = 0 ;
2324 this . elasticity = 0 ;
2425
25- // this.startAt
26+ // Changes the property to be this before starting the tween
27+ this . startAt ;
2628
2729 this . progress = 0 ;
28- this . startTime = 0 ;
2930 this . elapsed = 0 ;
31+ this . countdown = 0 ;
3032
31- // 0 = forward, 1 = reverse
32- this . direction = 0 ;
33+ this . repeatCounter = 0 ;
3334
34- this . useFrames = false ;
35- this . paused = false ;
36- this . running = false ;
37- this . pending = true ;
38-
39- // Callbacks
40-
41- this . onStart ;
42- this . onStartScope ;
43- this . onStartParams ;
35+ // 0 = Waiting to be added to the TweenManager
36+ // 1 = Paused (dev needs to invoke Tween.start)
37+ // 2 = Started, but waiting for delay to expire
38+ // 3 = Playing Forward
39+ // 4 = Playing Backwards
40+ // 5 = Completed
41+ this . state = 0 ;
4442
45- this . onUpdate ;
46- this . onUpdateScope ;
47- this . onUpdateParams ;
43+ // if true then duration, delay, etc values are all frame totals
44+ this . useFrames = false ;
4845
49- this . onRepeat ;
50- this . onRepeatScope ;
51- this . onRepeatParams ;
46+ this . paused = false ;
5247
53- this . onComplete ;
54- this . onCompleteScope ;
55- this . onCompleteParams ;
48+ this . callbacks = {
49+ onStart : { callback : null , scope : null , params : null } ,
50+ onUpdate : { callback : null , scope : null , params : null } ,
51+ onRepeat : { callback : null , scope : null , params : null } ,
52+ onComplete : { callback : null , scope : null , params : null }
53+ } ;
5654
5755 this . callbackScope ;
5856} ;
@@ -63,62 +61,172 @@ Tween.prototype = {
6361
6462 init : function ( )
6563 {
66- console . log ( 'Tween init' , ( ! this . paused ) ) ;
64+ this . state = 1 ;
6765
6866 return ( ! this . paused ) ;
6967 } ,
7068
71- start : function ( timestep )
69+ start : function ( )
7270 {
73- console . log ( 'Tween started' ) ;
71+ if ( this . state !== 1 )
72+ {
73+ return ;
74+ }
7475
75- this . startTime = timestep ;
76+ if ( this . delay > 0 )
77+ {
78+ this . countdown = this . delay ;
79+ this . state = 2 ;
80+ }
81+ else
82+ {
83+ this . loadValues ( ) ;
84+ }
85+ } ,
7686
87+ loadValues : function ( )
88+ {
7789 this . start = this . target [ this . key ] ;
7890 this . current = this . start ;
7991 this . end = this . value ( ) ;
8092
81- this . paused = false ;
82- this . running = true ;
93+ if ( this . repeat === - 1 )
94+ {
95+ this . loop = true ;
96+ }
97+
98+ this . repeatCounter = ( this . loop ) ? Number . MAX_SAFE_INTEGER : this . repeat ;
99+
100+ this . state = 3 ;
83101 } ,
84102
85103 update : function ( timestep , delta )
86104 {
87- if ( ! this . running )
105+ if ( this . state === 2 )
88106 {
89- return ;
107+ // Waiting for delay to expire
108+ this . countdown -= ( this . useFrames ) ? 1 : delta ;
109+
110+ if ( this . countdown <= 0 )
111+ {
112+ this . loadValues ( ) ;
113+ }
90114 }
91115
92- this . elapsed += delta ;
116+ if ( this . state === 3 )
117+ {
118+ // Playing forwards
119+ this . forward ( delta ) ;
120+ }
121+ else if ( this . state === 4 )
122+ {
123+ // Playing backwards
124+ this . backward ( delta ) ;
125+ }
126+
127+ // Complete? Delete from the Tween Manager
128+ return ( this . state !== 5 ) ;
129+ } ,
93130
94- if ( this . elapsed > this . duration )
131+ forward : function ( delta )
132+ {
133+ var elapsed = this . elapsed ;
134+ var duration = this . duration ;
135+
136+ elapsed += ( this . useFrames ) ? 1 : delta ;
137+
138+ if ( elapsed > duration )
95139 {
96- this . elapsed = this . duration ;
140+ elapsed = duration ;
97141 }
98142
99- this . progress = this . elapsed / this . duration ;
143+ var progress = elapsed / duration ;
100144
101- var p = this . ease ( this . progress ) ;
145+ var p = this . ease ( progress ) ;
102146
147+ // Optimize
103148 this . current = this . start + ( ( this . end - this . start ) * p ) ;
104149
105150 this . target [ this . key ] = this . current ;
106151
107- if ( this . progress === 1 )
152+ this . elapsed = elapsed ;
153+ this . progress = progress ;
154+
155+ if ( progress === 1 )
108156 {
109- this . running = false ;
157+ // Tween has reached end
158+ // Do we yoyo or repeat?
159+
160+ this . state = this . processRepeat ( ) ;
161+ }
162+ } ,
163+
164+ backward : function ( delta )
165+ {
166+ var elapsed = this . elapsed ;
167+ var duration = this . duration ;
168+
169+ elapsed += ( this . useFrames ) ? 1 : delta ;
170+
171+ if ( elapsed > duration )
172+ {
173+ elapsed = duration ;
174+ }
175+
176+ var progress = elapsed / duration ;
177+
178+ var p = this . ease ( 1 - progress ) ;
179+
180+ // Optimize
181+ this . current = this . start + ( ( this . end - this . start ) * p ) ;
182+
183+ this . target [ this . key ] = this . current ;
184+
185+ this . elapsed = elapsed ;
186+ this . progress = progress ;
187+
188+ if ( progress === 0 )
189+ {
190+ // Tween has reached start
191+ // Do we yoyo or repeat?
192+
193+ this . state = this . processRepeat ( ) ;
110194 }
111195 } ,
112196
197+ processRepeat : function ( )
198+ {
199+ // Playing forward, and Yoyo is enabled?
200+ if ( this . state === 3 && this . yoyo )
201+ {
202+ // Play backwards
203+ this . elapsed = 0 ;
204+ this . progress = 0 ;
205+
206+ return 4 ;
207+ }
208+ else if ( this . repeatCounter > 0 )
209+ {
210+ this . repeatCounter -- ;
211+
212+ // Reset the elapsed
213+ this . current = this . start ;
214+ this . elapsed = 0 ;
215+ this . progress = 0 ;
216+
217+ return 3 ;
218+ }
219+
220+ return 5 ;
221+ } ,
222+
113223 eventCallback : function ( type , callback , params , scope )
114224 {
115225 var types = [ 'onStart' , 'onUpdate' , 'onRepeat' , 'onComplete' ] ;
116226
117227 if ( types . indexOf ( type ) !== - 1 )
118228 {
119- this [ type ] = callback ;
120- this [ type + 'Params' ] = params ;
121- this [ type + 'Scope' ] = scope ;
229+ this . callbacks [ type ] = { callback : callback , scope : scope , params : params } ;
122230 }
123231
124232 return this ;
0 commit comments