-
Notifications
You must be signed in to change notification settings - Fork 13
/
TeaTime.cs
840 lines (667 loc) · 24.3 KB
/
TeaTime.cs
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
// TeaTime v0.9
// TeaTime is a fast & simple queue for timed callbacks, focused on solving
// common coroutines patterns in Unity games.
// Author: Andrés Villalobos | andresalvivar@gmail.com | github.com/alvivar
// Copyright (c) 2014/12/26 andresalvivar@gmail.com
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// Timed Task node.
public class TeaTask
{
public bool isLoop = false;
public float time = 0;
public Func<float> timeByFunc = null;
public Action callback = null;
public Action<TeaHandler> callbackWithHandler = null;
public TeaHandler handler = null;
}
/// TeaTime handler for callbacks.
public class TeaHandler
{
/// Current TeaTime queue.
public TeaTime self;
public float t = 0;
public float deltaTime = 0;
public float timeSinceStart = 0;
public bool isLooping = false;
public bool isReversed = false;
public List<YieldInstruction> yields = null;
/// Ends the current loop.
public void Break()
{
isLooping = false;
}
/// Appends a YieldInstruction to wait after the current callback execution.
public void Wait(YieldInstruction yi)
{
if (yields == null)
yields = new List<YieldInstruction>();
yields.Add(yi);
}
/// Appends a time delay to wait after the current callback execution.
public void Wait(float time)
{
if (time <= 0)
return;
Wait(TeaYield.WaitForSeconds(time));
}
/// Appends a TeaTime to wait after the current callback execution that is
/// also affected by the queue .Stop() and .Reset().
public void Wait(TeaTime tt)
{
// A reference to the waiting list
if (!self.waiting.Contains(tt))
{
self.waiting.Add(tt);
Wait(tt.WaitForCompletion());
}
}
/// Appends a boolean condition to wait until true after the current
/// callback execution.
public void Wait(Func<bool> condition, float checkDelay)
{
// @todo This need to be cached somehow
Wait(self.monoBehaviour.tt().Wait(condition, checkDelay));
}
}
/// TeaTime core extensions (static magic).
public static class TeaTimeExtensions
{
private static Dictionary<MonoBehaviour, Dictionary<string, TeaTime>> register; // Queues bounded by 'tt(string)'
/// Returns a new TeaTime queue ready to be used. This is basically a
/// shorcut to 'new TeaTime(this);' in MonoBehaviours.
public static TeaTime tt(this MonoBehaviour instance)
{
return new TeaTime(instance);
}
/// Returns a TeaTime queue bounded to his name, unique per MonoBehaviour
/// instance, new on the first call. This allows you to access queues
/// without a formal definition.
public static TeaTime tt(this MonoBehaviour instance, string queueName)
{
// @todo 'register' will (probably) need an auto clean up from time to
// time if this technique is used in volatile GameObjects.
// First time
if (register == null)
register = new Dictionary<MonoBehaviour, Dictionary<string, TeaTime>>();
if (!register.ContainsKey(instance))
register[instance] = new Dictionary<string, TeaTime>();
if (!register[instance].ContainsKey(queueName))
register[instance][queueName] = new TeaTime(instance);
return register[instance][queueName];
}
}
/// YieldInstruction static cache! Found here:
/// http://forum.unity3d.com/threads/c-coroutine-waitforseconds-garbage-collection-tip.224878/
public static class TeaYield
{
private class FloatComparer : IEqualityComparer<float>
{
bool IEqualityComparer<float>.Equals(float x, float y) { return x == y; }
int IEqualityComparer<float>.GetHashCode(float obj) { return obj.GetHashCode(); }
}
private static Dictionary<float, WaitForSeconds> secondsCache = new Dictionary<float, WaitForSeconds>(new FloatComparer());
private static WaitForEndOfFrame endOfFrame = new WaitForEndOfFrame();
public static WaitForEndOfFrame EndOfFrame { get { return endOfFrame; } }
private static WaitForFixedUpdate fixedUpdate = new WaitForFixedUpdate();
public static WaitForFixedUpdate FixedUpdate { get { return fixedUpdate; } }
public static WaitForSeconds WaitForSeconds(float seconds)
{
WaitForSeconds wfs;
if (!secondsCache.TryGetValue(seconds, out wfs))
secondsCache.Add(seconds, wfs = new WaitForSeconds(seconds));
return wfs;
}
}
/// TeaTime is a fast & simple queue for timed callbacks, focused on solving
/// common coroutines patterns in Unity games.
public class TeaTime
{
// Queue
private List<TeaTask> tasks = new List<TeaTask>(); // Tasks list used as a queue
public List<TeaTime> waiting = new List<TeaTime>(); // TeaTimes to wait via TeaHandler.Wait(
private int taskIndex = 0; // Current task mark (to be executed)
private int executedCount = 0; // Executed task count
private int lastPlayExecutedCount = 0; // Executed task count during the last play
// Dependencies
public MonoBehaviour monoBehaviour = null; // Required to access Unity coroutine fuctions
private Coroutine currentCoroutine = null; // Coroutine that holds the queue execution
// States
private bool isPlaying = false; // True while queue execution
private bool isPaused = false; // On .Pause()
private bool isImmutable = false; // On .Immutable() mode
private bool isRepeating = false; // On .Repeat() mode
private bool isConsuming = false; // On .Consume() mode
private bool isReversed = false; // On .Reverse() Backward() Forward() mode
private bool isYoyo = false; // On .Yoyo() mode
/// True while the queue is being executed.
public bool IsPlaying
{
get { return isPlaying; }
}
/// True if the queue execution is done.
public bool IsCompleted
{
get { return taskIndex >= tasks.Count && !isPlaying; }
}
/// Queue count.
public int Count
{
get { return tasks.Count; }
}
/// Current queue position to be executed.
public int Current
{
get { return taskIndex; }
}
/// Executed callback count.
public int ExecutedCount
{
get { return executedCount; }
}
/// A TeaTime queue requires a MonoBehaviour instance to access his coroutine fuctions.
public TeaTime(MonoBehaviour instance)
{
monoBehaviour = instance;
}
// ADD
/// Appends a new TeaTask.
private TeaTime Add(float timeDelay, Func<float> timeDelayByFunc, Action callback, Action<TeaHandler> callbackWithHandler)
{
// Ignores appends on Immutable mode
if (!isImmutable)
{
TeaTask newTask = new TeaTask();
newTask.time = timeDelay;
newTask.timeByFunc = timeDelayByFunc;
newTask.callback = callback;
newTask.callbackWithHandler = callbackWithHandler;
tasks.Add(newTask);
}
// Autoplay if not paused or playing
return isPaused || isPlaying ? this : Play();
}
/// Appends a timed callback.
public TeaTime Add(float timeDelay, Action callback)
{
return Add(timeDelay, null, callback, null);
}
/// Appends a timed callback.
public TeaTime Add(Func<float> timeByFunc, Action callback)
{
return Add(0, timeByFunc, callback, null);
}
/// Appends a timed callback.
public TeaTime Add(float timeDelay, Action<TeaHandler> callback)
{
return Add(timeDelay, null, null, callback);
}
/// Appends a timed callback.
public TeaTime Add(Func<float> timeByFunc, Action<TeaHandler> callback)
{
return Add(0, timeByFunc, null, callback);
}
/// Appends a time delay.
public TeaTime Add(float timeDelay)
{
return Add(timeDelay, null, null, null);
}
/// Appends a time delay.
public TeaTime Add(Func<float> timeByFunc)
{
return Add(0, timeByFunc, null, null);
}
/// Appends a callback.
public TeaTime Add(Action callback)
{
return Add(0, null, callback, null);
}
/// Appends a callback.
public TeaTime Add(Action<TeaHandler> callback)
{
return Add(0, null, null, callback);
}
/// Appends a TeaTime.
public TeaTime Add(TeaTime tt)
{
return Add((TeaHandler t) => t.Wait(tt));
}
// LOOP
/// Appends a callback loop (if duration is less than 0, the loop runs infinitely).
private TeaTime Loop(float duration, Func<float> durationByFunc, Action<TeaHandler> callback)
{
// Ignores appends on Immutable mode
if (!isImmutable)
{
TeaTask newTask = new TeaTask();
newTask.isLoop = true;
newTask.time = duration;
newTask.timeByFunc = durationByFunc;
newTask.callbackWithHandler = callback;
tasks.Add(newTask);
}
// Autoplay if not paused or playing
return isPaused || isPlaying ? this : Play();
}
/// Appends a callback loop (if duration is less than 0, the loop runs
/// infinitely).
public TeaTime Loop(float duration, Action<TeaHandler> callback)
{
return Loop(duration, null, callback);
}
/// Appends a callback loop (if duration is less than 0, the loop runs
/// infinitely).
public TeaTime Loop(Func<float> durationByFunc, Action<TeaHandler> callback)
{
return Loop(0, durationByFunc, callback);
}
/// Appends an infinite callback loop.
public TeaTime Loop(Action<TeaHandler> callback)
{
return Loop(-1, null, callback);
}
// QUEUE MODES
/// Enables Immutable mode, the queue will ignore new appends (.Add .Loop
/// .If)
public TeaTime Immutable()
{
isImmutable = true;
return this;
}
/// Enables Repeat mode, the queue will always be restarted on completion.
public TeaTime Repeat()
{
isRepeating = true;
return this;
}
/// Enables Consume mode, the queue will remove each callback after
/// execution.
public TeaTime Consume()
{
isConsuming = true;
return this;
}
/// Reverses the callback execution order (From .Forward() to .Backward()
/// mode and viceversa).
public TeaTime Reverse()
{
isReversed = !isReversed;
// 1 I don't remember why IsPlaying is needed to allow reversing the
// index, probably en edge case? @todo Or maybe I need to remove it and
// test everything again.
// 2 ...But taskIndex != 0 is important when Reverse() is called before
// executing the first task, to make sure everything runs reversed from
// the beginning.
if (IsPlaying && taskIndex != 0)
taskIndex = tasks.Count - taskIndex;
return this;
}
/// Enables Backward mode, executing callbacks on reverse order (including
/// Loops).
public TeaTime Backward()
{
if (!isReversed)
return Reverse();
return this;
}
/// Enables Forward mode (the default), executing callbacks one after the
/// other.
public TeaTime Forward()
{
if (isReversed)
return Reverse();
return this;
}
/// Enables Yoyo mode, that will .Reverse() the callback execution order
/// when the queue is completed. Only once per play without Repeat mode.
public TeaTime Yoyo()
{
isYoyo = true;
return this;
}
/// Disables all modes (Immutable, Repeat, Consume, Backward, Yoyo). Just
/// like new.
public TeaTime Release()
{
isImmutable = isRepeating = isConsuming = isYoyo = false;
return Forward();
}
// CONTROL
/// Pauses the queue execution (use .Play() to resume).
public TeaTime Pause()
{
isPaused = true;
return this;
}
/// Stops the queue execution (use .Play() to start over).
public TeaTime Stop()
{
if (currentCoroutine != null)
monoBehaviour.StopCoroutine(currentCoroutine);
taskIndex = 0;
isPlaying = false;
// Stop all TeaTimes on .Wait(
for (int i = 0, len = waiting.Count; i < len; i++)
waiting[i].Stop();
waiting.Clear();
return this;
}
/// Starts or resumes the queue execution.
public TeaTime Play()
{
// Unpause always
isPaused = false;
// Ignore if currently playing
if (isPlaying)
return this;
// or empty?
if (tasks.Count <= 0)
return this;
// Restart if already finished
if (taskIndex >= tasks.Count)
taskIndex = 0;
// Execute!
currentCoroutine = monoBehaviour.StartCoroutine(ExecuteQueue());
return this;
}
/// Restarts the queue execution (.Stop().Play()).
public TeaTime Restart()
{
// Alias
return Stop().Play();
}
// DESTRUCTION
/// Stops and cleans the queue, turning off all modes (Immutable, Repeat,
/// Consume, Backward, Yoyo). Just like new.
public TeaTime Reset()
{
// Reset current
if (currentCoroutine != null)
{
monoBehaviour.StopCoroutine(currentCoroutine);
currentCoroutine = null;
}
tasks.Clear();
taskIndex = 0;
executedCount = 0;
isPlaying = false;
isPaused = false;
// Modes off
isImmutable = false;
isRepeating = false;
isConsuming = false;
isYoyo = false;
Forward();
// Reset all TeaTimes on .Wait(
for (int i = 0, len = waiting.Count; i < len; i++)
waiting[i].Reset();
waiting.Clear();
return this;
}
// SPECIAL
// A note about .Wait(
// 1 It would be redundant to add a .Wait(time) because there is an
// Add(time) currently. The same with .Wait(TeaTime).
// 2 A Wait(YieldInstruction) would be useless after the first time a
// TeaTime runs, because the Yield reference can't be used again when a
// TeaTime is replayed.
/// The queue will stop if the condition isn't fullfiled, or restarted on
/// Repeat mode.
public TeaTime If(Func<bool> condition)
{
return Add(() =>
{
if (!condition())
{
if (isRepeating)
{
Restart();
}
else
{
Stop();
}
}
});
}
/// The queue will wait until the boolean condition is fullfiled, checking
/// every tick.
public TeaTime Wait(Func<bool> until, float tick = 0)
{
return Loop((TeaHandler t) =>
{
if (until())
t.Break();
t.Wait(tick);
});
}
// CUSTOM YIELDS
/// IEnumerator that waits the completion of a TeaTime.
private IEnumerator WaitForCompletion(TeaTime tt)
{
while (!tt.IsCompleted) yield return null;
}
/// Returns a YieldInstruction that waits until the queue is completed.
public YieldInstruction WaitForCompletion()
{
// @todo Could this be cached somehow?
return monoBehaviour.StartCoroutine(WaitForCompletion(this));
}
// THE COROUTINE
/// This is the main algorithm. Executes all tasks, one after the other,
/// calling their callbacks according to type, time and config.
private IEnumerator ExecuteQueue()
{
isPlaying = true;
int reverseLastTask = -1; // Important: This value needs to be reset to default on most queue changes
lastPlayExecutedCount = 0;
// Let's wait a frame
// 1 For secuencial Adds or Loops before their first execution
// 2 Maybe a callback is trying to modify his own queue
yield return null;
while (taskIndex < tasks.Count)
{
// Current task to be executed
int taskId = taskIndex;
if (isReversed)
taskId = tasks.Count - 1 - taskIndex;
TeaTask task = tasks[taskId];
// Next task (or previous if the queue is backward)
taskIndex++;
// Avoid executing a task twice when reversed and the queue hasn't
// reached the end
if (taskId == reverseLastTask)
continue;
reverseLastTask = taskId;
// It's a loop
if (task.isLoop)
{
// Holds the duration
float loopDuration = task.time;
// Func<float> added
if (task.timeByFunc != null)
loopDuration += task.timeByFunc();
// Nothing to do, skip
if (loopDuration == 0)
continue;
// Loops always need a handler
if (task.handler == null)
{
task.handler = new TeaHandler();
task.handler.self = this;
}
// Reset handler to defaults
task.handler.t = 0;
task.handler.timeSinceStart = 0;
task.handler.isReversed = isReversed;
task.handler.isLooping = true;
// Negative time means the loop is infinite
bool isInfinite = loopDuration < 0;
// T quotient
float tRate = isInfinite ? 0 : 1 / loopDuration;
// Progresion depends on current direction
if (task.handler.isReversed)
{
task.handler.t = 1f;
tRate = -tRate;
}
// While looping and, until time or infinite
while (task.handler.isLooping && (task.handler.isReversed ? task.handler.t >= 0 : task.handler.t <= 1))
{
// Check for queue reversal
if (isReversed != task.handler.isReversed)
{
tRate = -tRate;
task.handler.isReversed = isReversed;
}
float unityDeltaTime = Time.deltaTime;
// Completion % from 0 to 1
if (!isInfinite)
task.handler.t += tRate * unityDeltaTime;
// On finite loops this .deltaTime is sincronized with the
// exact loop duration
task.handler.deltaTime =
isInfinite ?
unityDeltaTime :
1 / (loopDuration - task.handler.timeSinceStart) * unityDeltaTime;
// .deltaTime is also reversed
if (task.handler.isReversed)
task.handler.deltaTime = -task.handler.deltaTime;
// A classic
task.handler.timeSinceStart += unityDeltaTime;
// Pause?
while (isPaused)
yield return null;
// Loops will always have a callback with a handler
task.callbackWithHandler(task.handler);
// Handler .WaitFor(
if (task.handler.yields != null)
{
for (int i = 0, len = task.handler.yields.Count; i < len; i++)
yield return task.handler.yields[i];
task.handler.yields.Clear();
}
// Minimum sane delay
else yield return null;
}
// Executed +1
executedCount += 1;
lastPlayExecutedCount += 1;
}
// It's a timed callback
else
{
// Holds the delay
float delayDuration = task.time;
// Func<float> added
if (task.timeByFunc != null)
delayDuration += task.timeByFunc();
// Time delay
if (delayDuration > 0)
yield return TeaYield.WaitForSeconds(delayDuration);
// Is this more precise that the previous code?
// float time = 0;
// while (time < delayDuration)
// {
// time += Time.deltaTime;
// yield return null;
// }
// Pause?
while (isPaused)
yield return null;
// Normal callback
if (task.callback != null)
task.callback();
// Callback with handler
if (task.callbackWithHandler != null)
{
if (task.handler == null)
{
task.handler = new TeaHandler();
task.handler.self = this;
}
task.handler.t = 1;
task.handler.deltaTime = Time.deltaTime;
task.handler.timeSinceStart = delayDuration;
task.callbackWithHandler(task.handler);
// Handler WaitFor
if (task.handler.yields != null)
{
for (int i = 0, len = task.handler.yields.Count; i < len; i++)
yield return task.handler.yields[i];
task.handler.yields.Clear();
}
// Minimum sane delay
if (delayDuration <= 0 && task.handler.yields == null)
yield return null;
}
else if (delayDuration <= 0)
yield return null;
// Executed +1
executedCount += 1;
lastPlayExecutedCount += 1;
}
// Just at the end of a complete queue execution
if (tasks.Count > 0 && taskIndex >= tasks.Count)
{
// Forget current nested queues
waiting.Clear();
}
// Consume mode removes the task after execution
// @todo Need to be tested with .Reverse() stuff
if (isConsuming)
{
taskIndex -= 1;
tasks.Remove(task);
reverseLastTask = -1; // To default
}
// On Yoyo mode the queue is reversed at the end, only once per play
// without Repeat mode
if (isYoyo && taskIndex >= tasks.Count && (lastPlayExecutedCount <= tasks.Count || isRepeating))
{
Reverse();
reverseLastTask = -1; // To default
}
// Repeats on Repeat mode
if (isRepeating && tasks.Count > 0 && taskIndex >= tasks.Count)
{
taskIndex = 0;
reverseLastTask = -1; // To default
}
}
// Done!
isPlaying = false;
}
}
// <3
// Lerp Formulas
// Ease out
// t = Mathf.Sin(t * Mathf.PI * 0.5f);
// Ease in
// t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f)
// Exponential
// t = t*t
// Smoothstep
// t = t*t * (3f - 2f*t)
// Smootherstep
// t = t*t*t * (t * (6f*t - 15f) + 10f)
// Created 2014/12/26 12:21 am
// Rewritten 2015/09/15 12:28 pm
// Last revision 2021/02/16 11.53 pm, 2021/09/26 01:37 am