1 module animate.animate.animatable;
2 
3 import core.time;
4 
5 import animate.animate.animation;
6 
7 interface Animatable
8 {
9     void runAnimation(Updatable anim);
10     void updateAnimations(Duration time);
11     void update(Duration time);
12 }
13 
14 mixin template NormalNode()
15 {
16     import animate.animate.animation;
17 
18     private
19     {
20         Updatable[] m_animations;
21     }
22 
23     /// Add the animation to the queue
24     void runAnimation(Updatable anim)
25     {
26         m_animations ~= anim;
27     }
28 
29     /// Update the currently running animations.
30     void updateAnimations(Duration time)
31     {
32         int[] itemsToRemove;
33         foreach(i, anim; m_animations)
34         {
35             if(anim.isRunning())
36             {
37                 anim.update(time);
38             }
39             else
40             {
41                 itemsToRemove ~= cast(int) i;
42             }
43         }
44 
45         foreach(index; itemsToRemove)
46         {
47             removeAtUnstable(m_animations, index);
48         }
49 
50     }
51 
52     private static void removeAtUnstable(T)(ref T[] arr, size_t index)
53     {
54         if(index >= arr.length)
55         {
56             debug import std.stdio;
57             debug stderr.writeln("YA DOOF, index(", index, ") >= arr.length(", arr.length, ")");
58         }
59         else
60         {
61             if(index != arr.length - 1)
62             {
63                 arr[index] = arr[$-1];
64             }
65             arr = arr[0 .. $-1];
66 
67         }
68     }
69 
70 }