summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/gfx/animations/AnimationSequence.java
blob: 49dd353dae1182a49da29a2c33711c0ecd9b1e44 (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
68
69
70
71
72
73
74
75
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.Animation;
import ch.asynk.rustanddust.engine.util.IterableArray;

public class AnimationSequence implements Animation, Pool.Poolable
{
    private IterableArray<Animation> animations;

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

    public static AnimationSequence get(int capacity)
    {
        AnimationSequence seq = animationSequencePool.obtain();
        if (seq.animations == null)
            seq.animations = new IterableArray<Animation>(capacity);
        else
            seq.animations.ensureCapacity(capacity);

        return seq;
    }

    @Override
    public void reset()
    {
        for (Animation a : animations)
            a.dispose();
        animations.clear();
    }

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

    public void addAnimation(Animation animation)
    {
        animations.add(animation);
    }

    @Override
    public boolean animate(float delta)
    {
        if (animations.isEmpty()) return true;

        Animation animation = animations.get(0);
        if (animation.animate(delta)) {
            animations.remove(0);
        }

        return (animations.isEmpty());
    }

    @Override
    public void draw(Batch batch)
    {
        animations.get(0).draw(batch);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        animations.get(0).drawDebug(debugShapes);
    }
}