summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/animations/TimedAnimation.java
blob: 5a22c2b04aafd742bf29098db5228c79a5bb1e8a (plain)
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
package ch.asynk.gdx.boardgame.animations;

public abstract class TimedAnimation implements Animation
{
    private float dp;
    protected float percent;
    protected float elapsed;

    // begin() may be called after the first call to draw()
    // only use it to capture variables when the animation starts
    // not to setup the Animation, do that it the Animation initialisation
    abstract protected void begin();
    abstract protected void end();
    abstract protected void update(float delta);

    public void setDuration(float duration)
    {
        dp = 1f / duration;
    }

    public void reset()
    {
        percent = 0f;
        elapsed = 0f;
    }

    @Override public boolean completed()
    {
        return (percent >= 1f);
    }

    @Override public boolean animate(float delta)
    {
        if (elapsed == 0f) {
            begin();
        }

        elapsed += delta;
        percent = (dp * elapsed);

        if (percent >= 1f) {
            // percent = 1f;
            // update(delta);
            end();
            return true;
        } else {
            update(delta);
            return false;
        }
    }
}