11var GetValue = require ( '../utils/object/GetValue' ) ;
22var GetEaseFunction = require ( './GetEaseFunction' ) ;
33var CloneObject = require ( '../utils/object/Clone' ) ;
4- var TweenData = require ( './TweenData' ) ;
4+ var MergeRight = require ( '../utils/object/MergeRight' ) ;
5+ // var TweenData = require('./TweenData');
56
67var RESERVED = [ 'targets' , 'ease' , 'duration' , 'yoyo' , 'repeat' , 'loop' , 'paused' , 'useFrames' , 'offset' ] ;
78
@@ -58,6 +59,14 @@ var RESERVED = [ 'targets', 'ease', 'duration', 'yoyo', 'repeat', 'loop', 'pause
5859 y: { value: 300, duration: 1000, ease: 'Sine' }
5960 });
6061
62+ var tween = this.tweens.add({
63+ targets: player,
64+ props: {
65+ x: { value: 400, duration: 2000, ease: 'Power1' },
66+ y: { value: 300, duration: 1000, ease: 'Sine' }
67+ }
68+ });
69+
6170 */
6271
6372var Tween = function ( manager , config )
@@ -97,6 +106,16 @@ var Tween = function (manager, config)
97106 this . loop = true ;
98107 }
99108
109+ // Move to global
110+ this . defaultInstance = {
111+ key : '' ,
112+ running : false ,
113+ complete : false ,
114+ current : 0 ,
115+ queue : [ ] ,
116+ totalDuration : 0
117+ } ;
118+
100119 this . defaultTweenData = {
101120 value : undefined ,
102121 progress : 0 ,
@@ -107,13 +126,21 @@ var Tween = function (manager, config)
107126 repeat : this . repeat ,
108127 loop : this . loop ,
109128 delay : this . delay ,
110- startAt : undefined
129+ startAt : undefined ,
130+ elapsed : 0
111131 } ;
112132
113133 this . paused = GetValue ( config , 'paused' , false ) ;
114134
115135 this . useFrames = GetValue ( config , 'useFrames' , false ) ;
116136
137+ this . autoStart = GetValue ( config , 'autoStart' , true ) ;
138+
139+ this . running = this . autoStart ;
140+
141+ this . progress = 0 ;
142+ this . totalDuration = 0 ;
143+
117144 this . onStart ;
118145 this . onStartScope ;
119146 this . onStartParams ;
@@ -158,29 +185,131 @@ Tween.prototype = {
158185 {
159186 // For now let's just assume `config.props` is being used:
160187
161- // this.defaultTweenData = {
162- // value: undefined,
163- // progress: 0,
164- // startTime: 0,
165- // ease: this.ease,
166- // duration: this.duration,
167- // yoyo: this.yoyo,
168- // repeat: this.repeat,
169- // loop: this.loop,
170- // delay: this.delay,
171- // startAt: undefined
172- // };
188+ // props: {
189+ // x: 400,
190+ // y: 300
191+ // }
192+
193+ // props: {
194+ // x: { value: 400, duration: 2000, ease: 'Power1' },
195+ // y: { value: 300, duration: 1000, ease: 'Sine' }
196+ // }
173197
174198 for ( var key in config . props )
175199 {
176- var data = CloneObject ( this . defaultTweenData ) ;
177-
178- this . props [ key ] =
200+ // Check it's not a string or number (or function?)
201+ // TODO: value might be an Array
202+
203+ var data ;
204+ var value = config . props [ key ] ;
205+
206+ if ( typeof value === 'number' )
207+ {
208+ data = CloneObject ( this . defaultTweenData ) ;
209+
210+ data . value = value ;
211+ }
212+ else if ( typeof value === 'string' )
213+ {
214+ // Do something :)
215+ }
216+ else
217+ {
218+ data = MergeRight ( this . defaultTweenData , config . props [ key ] ) ;
219+ }
220+
221+ // this.props = [
222+ // {
223+ // key: 'x',
224+ // running: true,
225+ // complete: false,
226+ // current: 0,
227+ // queue: [ TweenData, TweenData, TweenData ],
228+ // totalDuration: Number (ms)
229+ // }
230+ // ]
231+
232+ // Convert to ms
233+ data . duration *= 1000 ;
234+
235+ var propertyMarker = CloneObject ( this . defaultInstance ) ;
236+
237+ propertyMarker . key = key ;
238+
239+ // Adapt these to support array based multi-inserts
240+ propertyMarker . queue . push ( data ) ;
241+ propertyMarker . totalDuration = data . duration ;
242+
243+ this . props . push ( propertyMarker ) ;
244+
245+ this . totalDuration += propertyMarker . totalDuration ;
179246 }
180247 } ,
181248
182249 update : function ( timestep , delta )
183250 {
251+ if ( ! this . running )
252+ {
253+ return ;
254+ }
255+
256+ // Calculate tweens
257+
258+ var list = this . props ;
259+ var targets = this . targets ;
260+
261+ // this.props = [
262+ // {
263+ // key: 'x',
264+ // start: [ Target0 startValue, Target1 startValue, Target2 startValue ],
265+ // running: true,
266+ // complete: false,
267+ // current: 0,
268+ // queue: [ TweenData, TweenData, TweenData ],
269+ // totalDuration: Number (ms)
270+ // }
271+ // ]
272+
273+ for ( var i = 0 ; i < list . length ; i ++ )
274+ {
275+ var entry = list [ i ] ;
276+
277+ // Update TweenData
278+
279+ if ( entry . running )
280+ {
281+ // TweenData = {
282+ // value: undefined,
283+ // progress: 0,
284+ // startTime: 0,
285+ // ease: this.ease,
286+ // duration: this.duration,
287+ // yoyo: this.yoyo,
288+ // repeat: this.repeat,
289+ // loop: this.loop,
290+ // delay: this.delay,
291+ // startAt: undefined,
292+ // elapsed: 0
293+ // };
294+
295+ var tweenData = entry . queue [ entry . current ] ;
296+
297+ tweenData . elapsed += delta ;
298+
299+ if ( tweenData . elapsed > tweenData . duration )
300+ {
301+ tweenData . elapsed = tweenData . duration ;
302+ }
303+
304+ // What % is that?
305+ tweenData . progress = tweenData . elapsed / tweenData . duration ;
306+
307+ for ( var t = 0 ; t < targets . length ; t ++ )
308+ {
309+ targets [ i ] [ entry . key ] = tweenData . value ;
310+ }
311+ }
312+ }
184313
185314 } ,
186315
0 commit comments