summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/gfx/animations/RunnableAnimation.java
blob: 231f8599cf873f1f1e6cef02ebe333133c3dd2fc (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ch.asynk.rustanddust.engine.gfx.animations;

import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import ch.asynk.rustanddust.engine.gfx.Moveable;
import ch.asynk.rustanddust.engine.gfx.Animation;

public class RunnableAnimation implements Animation, Pool.Poolable
{
    private Runnable runnable;
    private Moveable moveable;
    private boolean ran;

    private static final Pool<RunnableAnimation> runnableAnimationPool = new Pool<RunnableAnimation>() {
        @Override
        protected RunnableAnimation newObject() {
            return new RunnableAnimation();
        }
    };

    public static RunnableAnimation get(Moveable moveable, Runnable runnable)
    {
        RunnableAnimation a = runnableAnimationPool.obtain();
        a.runnable = runnable;
        a.moveable = moveable;
        return a;
    }

    @Override
    public void reset()
    {
        ran = false;
    }

    @Override
    public void dispose()
    {
        runnableAnimationPool.free(this);
    }

    @Override
    public boolean animate(float delta)
    {
        if (ran) return true;

        runnable.run();
        runnable = null;
        ran = true;
        dispose();

        return true;
    }

    @Override
    public void draw(Batch batch)
    {
        moveable.draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        moveable.drawDebug(debugShapes);
    }
}