-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
BaseTweenData.js
667 lines (589 loc) · 19.6 KB
/
BaseTweenData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2024 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var Events = require('../events');
var TWEEN_CONST = require('./const');
/**
* @classdesc
* BaseTweenData is the class that the TweenData and TweenFrameData classes
* extend from. You should not typically instantiate this class directly, but instead
* use it to form your own tween data classes from, should you require it.
*
* Prior to Phaser 3.60 the TweenData was just an object, but was refactored to a class,
* to make it responsible for its own state and updating.
*
* @class BaseTweenData
* @memberof Phaser.Tweens
* @constructor
* @since 3.60.0
*
* @param {Phaser.Tweens.Tween} tween - The tween this TweenData instance belongs to.
* @param {number} targetIndex - The target index within the Tween targets array.
* @param {string} key - The property of the target to tween.
* @param {Phaser.Types.Tweens.GetEndCallback} getEnd - What the property will be at the END of the Tween.
* @param {Phaser.Types.Tweens.GetStartCallback} getStart - What the property will be at the START of the Tween.
* @param {?Phaser.Types.Tweens.GetActiveCallback} getActive - If not null, is invoked _immediately_ as soon as the TweenData is running, and is set on the target property.
* @param {function} ease - The ease function this tween uses.
* @param {function} delay - Function that returns the time in milliseconds before tween will start.
* @param {number} duration - The duration of the tween in milliseconds.
* @param {boolean} yoyo - Determines whether the tween should return back to its start value after hold has expired.
* @param {number} hold - Function that returns the time in milliseconds the tween will pause before repeating or returning to its starting value if yoyo is set to true.
* @param {number} repeat - Function that returns the number of times to repeat the tween. The tween will always run once regardless, so a repeat value of '1' will play the tween twice.
* @param {number} repeatDelay - Function that returns the time in milliseconds before the repeat will start.
* @param {boolean} flipX - Should toggleFlipX be called when yoyo or repeat happens?
* @param {boolean} flipY - Should toggleFlipY be called when yoyo or repeat happens?
* @param {?function} interpolation - The interpolation function to be used for arrays of data. Defaults to 'null'.
* @param {?number[]} interpolationData - The array of interpolation data to be set. Defaults to 'null'.
*/
var BaseTweenData = new Class({
initialize:
function BaseTweenData (tween, targetIndex, delay, duration, yoyo, hold, repeat, repeatDelay, flipX, flipY)
{
/**
* A reference to the Tween that this TweenData instance belongs to.
*
* @name Phaser.Tweens.BaseTweenData#tween
* @type {Phaser.Tweens.Tween}
* @since 3.60.0
*/
this.tween = tween;
/**
* The index of the target within the Tween `targets` array.
*
* @name Phaser.Tweens.BaseTweenData#targetIndex
* @type {number}
* @since 3.60.0
*/
this.targetIndex = targetIndex;
/**
* The duration of the tween in milliseconds, excluding any time required
* for yoyo or repeats.
*
* @name Phaser.Tweens.BaseTweenData#duration
* @type {number}
* @since 3.60.0
*/
this.duration = duration;
/**
* The total calculated duration, in milliseconds, of this TweenData.
* Factoring in the duration, repeats, delays and yoyos.
*
* @name Phaser.Tweens.BaseTweenData#totalDuration
* @type {number}
* @since 3.60.0
*/
this.totalDuration = 0;
/**
* The time, in milliseconds, before this tween will start playing.
*
* This value is generated by the `getDelay` function.
*
* @name Phaser.Tweens.BaseTweenData#delay
* @type {number}
* @since 3.60.0
*/
this.delay = 0;
/**
* This function returns the value to be used for `TweenData.delay`.
*
* @name Phaser.Tweens.BaseTweenData#getDelay
* @type {function}
* @since 3.60.0
*/
this.getDelay = delay;
/**
* Will the Tween ease back to its starting values, after reaching the end
* and any `hold` value that may be set?
*
* @name Phaser.Tweens.BaseTweenData#yoyo
* @type {boolean}
* @since 3.60.0
*/
this.yoyo = yoyo;
/**
* The time, in milliseconds, before this tween will start a yoyo to repeat.
*
* @name Phaser.Tweens.BaseTweenData#hold
* @type {number}
* @since 3.60.0
*/
this.hold = hold;
/**
* The number of times this tween will repeat.
*
* The tween will always run once regardless of this value,
* so a repeat value of '1' will play the tween twice: I.e. the original
* play-through and then it repeats that once (1).
*
* If this value is set to -1 this tween will repeat forever.
*
* @name Phaser.Tweens.BaseTweenData#repeat
* @type {number}
* @since 3.60.0
*/
this.repeat = repeat;
/**
* The time, in milliseconds, before the repeat will start.
*
* @name Phaser.Tweens.BaseTweenData#repeatDelay
* @type {number}
* @since 3.60.0
*/
this.repeatDelay = repeatDelay;
/**
* How many repeats are left to run?
*
* @name Phaser.Tweens.BaseTweenData#repeatCounter
* @type {number}
* @since 3.60.0
*/
this.repeatCounter = 0;
/**
* If `true` this Tween will call `toggleFlipX` on the Tween target
* whenever it yoyo's or repeats. It will only be called if the target
* has a function matching this name, like most Phaser GameObjects do.
*
* @name Phaser.Tweens.BaseTweenData#flipX
* @type {boolean}
* @since 3.60.0
*/
this.flipX = flipX;
/**
* If `true` this Tween will call `toggleFlipY` on the Tween target
* whenever it yoyo's or repeats. It will only be called if the target
* has a function matching this name, like most Phaser GameObjects do.
*
* @name Phaser.Tweens.BaseTweenData#flipY
* @type {boolean}
* @since 3.60.0
*/
this.flipY = flipY;
/**
* A value between 0 and 1 holding the progress of this TweenData.
*
* @name Phaser.Tweens.BaseTweenData#progress
* @type {number}
* @since 3.60.0
*/
this.progress = 0;
/**
* The amount of time, in milliseconds, that has elapsed since this
* TweenData was made active.
*
* @name Phaser.Tweens.BaseTweenData#elapsed
* @type {number}
* @since 3.60.0
*/
this.elapsed = 0;
/**
* The state of this TweenData.
*
* @name Phaser.Tweens.BaseTweenData#state
* @type {Phaser.Tweens.StateType}
* @since 3.60.0
*/
this.state = 0;
/**
* Is this Tween Data currently waiting for a countdown to elapse, or not?
*
* @name Phaser.Tweens.BaseTweenData#isCountdown
* @type {boolean}
* @since 3.60.0
*/
this.isCountdown = false;
},
/**
* Returns a reference to the target object belonging to this TweenData.
*
* @method Phaser.Tweens.BaseTweenData#getTarget
* @since 3.60.0
*
* @return {object} The target object. Can be any JavaScript object, but is typically a Game Object.
*/
getTarget: function ()
{
return this.tween.targets[this.targetIndex];
},
/**
* Sets this TweenData's target object property to be the given value.
*
* @method Phaser.Tweens.BaseTweenData#setTargetValue
* @since 3.60.0
*
* @param {number} [value] - The value to set on the target. If not given, sets it to the last `current` value.
*/
setTargetValue: function (value)
{
if (value === undefined) { value = this.current; }
this.tween.targets[this.targetIndex][this.key] = value;
},
/**
* Sets this TweenData state to CREATED.
*
* @method Phaser.Tweens.BaseTweenData#setCreatedState
* @since 3.60.0
*/
setCreatedState: function ()
{
this.state = TWEEN_CONST.CREATED;
this.isCountdown = false;
},
/**
* Sets this TweenData state to DELAY.
*
* @method Phaser.Tweens.BaseTweenData#setDelayState
* @since 3.60.0
*/
setDelayState: function ()
{
this.state = TWEEN_CONST.DELAY;
this.isCountdown = true;
},
/**
* Sets this TweenData state to PENDING_RENDER.
*
* @method Phaser.Tweens.BaseTweenData#setPendingRenderState
* @since 3.60.0
*/
setPendingRenderState: function ()
{
this.state = TWEEN_CONST.PENDING_RENDER;
this.isCountdown = false;
},
/**
* Sets this TweenData state to PLAYING_FORWARD.
*
* @method Phaser.Tweens.BaseTweenData#setPlayingForwardState
* @since 3.60.0
*/
setPlayingForwardState: function ()
{
this.state = TWEEN_CONST.PLAYING_FORWARD;
this.isCountdown = false;
},
/**
* Sets this TweenData state to PLAYING_BACKWARD.
*
* @method Phaser.Tweens.BaseTweenData#setPlayingBackwardState
* @since 3.60.0
*/
setPlayingBackwardState: function ()
{
this.state = TWEEN_CONST.PLAYING_BACKWARD;
this.isCountdown = false;
},
/**
* Sets this TweenData state to HOLD_DELAY.
*
* @method Phaser.Tweens.BaseTweenData#setHoldState
* @since 3.60.0
*/
setHoldState: function ()
{
this.state = TWEEN_CONST.HOLD_DELAY;
this.isCountdown = true;
},
/**
* Sets this TweenData state to REPEAT_DELAY.
*
* @method Phaser.Tweens.BaseTweenData#setRepeatState
* @since 3.60.0
*/
setRepeatState: function ()
{
this.state = TWEEN_CONST.REPEAT_DELAY;
this.isCountdown = true;
},
/**
* Sets this TweenData state to COMPLETE.
*
* @method Phaser.Tweens.BaseTweenData#setCompleteState
* @since 3.60.0
*/
setCompleteState: function ()
{
this.state = TWEEN_CONST.COMPLETE;
this.isCountdown = false;
},
/**
* Returns `true` if this TweenData has a _current_ state of CREATED, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isCreated
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of CREATED, otherwise `false`.
*/
isCreated: function ()
{
return (this.state === TWEEN_CONST.CREATED);
},
/**
* Returns `true` if this TweenData has a _current_ state of DELAY, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isDelayed
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of DELAY, otherwise `false`.
*/
isDelayed: function ()
{
return (this.state === TWEEN_CONST.DELAY);
},
/**
* Returns `true` if this TweenData has a _current_ state of PENDING_RENDER, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isPendingRender
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of PENDING_RENDER, otherwise `false`.
*/
isPendingRender: function ()
{
return (this.state === TWEEN_CONST.PENDING_RENDER);
},
/**
* Returns `true` if this TweenData has a _current_ state of PLAYING_FORWARD, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isPlayingForward
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of PLAYING_FORWARD, otherwise `false`.
*/
isPlayingForward: function ()
{
return (this.state === TWEEN_CONST.PLAYING_FORWARD);
},
/**
* Returns `true` if this TweenData has a _current_ state of PLAYING_BACKWARD, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isPlayingBackward
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of PLAYING_BACKWARD, otherwise `false`.
*/
isPlayingBackward: function ()
{
return (this.state === TWEEN_CONST.PLAYING_BACKWARD);
},
/**
* Returns `true` if this TweenData has a _current_ state of HOLD_DELAY, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isHolding
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of HOLD_DELAY, otherwise `false`.
*/
isHolding: function ()
{
return (this.state === TWEEN_CONST.HOLD_DELAY);
},
/**
* Returns `true` if this TweenData has a _current_ state of REPEAT_DELAY, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isRepeating
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of REPEAT_DELAY, otherwise `false`.
*/
isRepeating: function ()
{
return (this.state === TWEEN_CONST.REPEAT_DELAY);
},
/**
* Returns `true` if this TweenData has a _current_ state of COMPLETE, otherwise `false`.
*
* @method Phaser.Tweens.BaseTweenData#isComplete
* @since 3.60.0
*
* @return {boolean} `true` if this TweenData has a _current_ state of COMPLETE, otherwise `false`.
*/
isComplete: function ()
{
return (this.state === TWEEN_CONST.COMPLETE);
},
/**
* Internal method used as part of the playback process that checks if this
* TweenData should yoyo, repeat, or has completed.
*
* @method Phaser.Tweens.BaseTweenData#setStateFromEnd
* @fires Phaser.Tweens.Events#TWEEN_REPEAT
* @fires Phaser.Tweens.Events#TWEEN_YOYO
* @since 3.60.0
*
* @param {number} diff - Any extra time that needs to be accounted for in the elapsed and progress values.
*/
setStateFromEnd: function (diff)
{
if (this.yoyo)
{
this.onRepeat(diff, true, true);
}
else if (this.repeatCounter > 0)
{
this.onRepeat(diff, true, false);
}
else
{
this.setCompleteState();
}
},
/**
* Internal method used as part of the playback process that checks if this
* TweenData should repeat or has completed.
*
* @method Phaser.Tweens.BaseTweenData#setStateFromStart
* @fires Phaser.Tweens.Events#TWEEN_REPEAT
* @since 3.60.0
*
* @param {number} diff - Any extra time that needs to be accounted for in the elapsed and progress values.
*/
setStateFromStart: function (diff)
{
if (this.repeatCounter > 0)
{
this.onRepeat(diff, false);
}
else
{
this.setCompleteState();
}
},
/**
* Internal method that resets this Tween Data entirely, including the progress and elapsed values.
*
* Called automatically by the parent Tween. Should not be called directly.
*
* @method Phaser.Tweens.BaseTweenData#reset
* @since 3.60.0
*/
reset: function ()
{
var tween = this.tween;
var totalTargets = tween.totalTargets;
var targetIndex = this.targetIndex;
var target = tween.targets[targetIndex];
var key = this.key;
this.progress = 0;
this.elapsed = 0;
// Function signature: target, key, value, index, total, tween
this.delay = this.getDelay(target, key, 0, targetIndex, totalTargets, tween);
this.repeatCounter = (this.repeat === -1) ? TWEEN_CONST.MAX : this.repeat;
this.setPendingRenderState();
// calcDuration:
// Set t1 (duration + hold + yoyo)
var t1 = this.duration + this.hold;
if (this.yoyo)
{
t1 += this.duration;
}
// Set t2 (repeatDelay + duration + hold + yoyo)
var t2 = t1 + this.repeatDelay;
// Total Duration
this.totalDuration = this.delay + t1;
if (this.repeat === -1)
{
this.totalDuration += (t2 * TWEEN_CONST.MAX);
tween.isInfinite = true;
}
else if (this.repeat > 0)
{
this.totalDuration += (t2 * this.repeat);
}
if (this.totalDuration > tween.duration)
{
// Set the longest duration in the parent Tween
tween.duration = this.totalDuration;
}
if (this.delay < tween.startDelay)
{
tween.startDelay = this.delay;
}
if (this.delay > 0)
{
this.elapsed = this.delay;
this.setDelayState();
}
},
/**
* Internal method that handles repeating or yoyo'ing this TweenData.
*
* Called automatically by `setStateFromStart` and `setStateFromEnd`.
*
* @method Phaser.Tweens.BaseTweenData#onRepeat
* @fires Phaser.Tweens.Events#TWEEN_REPEAT
* @fires Phaser.Tweens.Events#TWEEN_YOYO
* @since 3.60.0
*
* @param {number} diff - Any extra time that needs to be accounted for in the elapsed and progress values.
* @param {boolean} setStart - Set the TweenData start values?
* @param {boolean} isYoyo - Is this call a Yoyo check?
*/
onRepeat: function (diff, setStart, isYoyo)
{
var tween = this.tween;
var totalTargets = tween.totalTargets;
var targetIndex = this.targetIndex;
var target = tween.targets[targetIndex];
var key = this.key;
var isTweenData = (key !== 'texture');
// Account for any extra time we got from the previous frame
this.elapsed = diff;
this.progress = diff / this.duration;
if (this.flipX)
{
target.toggleFlipX();
}
if (this.flipY)
{
target.toggleFlipY();
}
if (isTweenData && (setStart || isYoyo))
{
this.start = this.getStartValue(target, key, this.start, targetIndex, totalTargets, tween);
}
if (isYoyo)
{
this.setPlayingBackwardState();
this.dispatchEvent(Events.TWEEN_YOYO, 'onYoyo');
return;
}
this.repeatCounter--;
// Custom
if (isTweenData)
{
this.end = this.getEndValue(target, key, this.start, targetIndex, totalTargets, tween);
}
// Delay?
if (this.repeatDelay > 0)
{
this.elapsed = this.repeatDelay - diff;
if (isTweenData)
{
this.current = this.start;
target[key] = this.current;
}
this.setRepeatState();
}
else
{
this.setPlayingForwardState();
this.dispatchEvent(Events.TWEEN_REPEAT, 'onRepeat');
}
},
/**
* Immediately destroys this TweenData, nulling of all its references.
*
* @method Phaser.Tweens.BaseTweenData#destroy
* @since 3.60.0
*/
destroy: function ()
{
this.tween = null;
this.getDelay = null;
this.setCompleteState();
}
});
module.exports = BaseTweenData;