summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/animations/AnimationBatch.java
blob: 1bc047f4e58760c03a3b4e824394e431afcceac2 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package ch.asynk.gdx.boardgame.animations;

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

import ch.asynk.gdx.boardgame.utils.IterableArray;

public class AnimationBatch implements Animation, Pool.Poolable
{
    private static final Pool<AnimationBatch> animationBatchPool = new Pool<AnimationBatch>()
    {
        @Override protected AnimationBatch newObject()
        {
            return new AnimationBatch();
        }
    };

    public static AnimationBatch obtain(int capacity)
    {
        AnimationBatch batch = animationBatchPool.obtain();
        if (batch.animations == null) {
            batch.animations = new IterableArray<Animation>(capacity);
        } else {
            batch.animations.ensureCapacity(capacity);
        }

        return batch;
    }

    private IterableArray<Animation> animations;

    private AnimationBatch()
    {
    }

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

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

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

    @Override public boolean completed()
    {
        return animations.isEmpty();
    }

    @Override public boolean animate(float delta)
    {
        if (!completed()) {
            for (Animation animation : animations) {
                if (animation.animate(delta)) {
                    animations.remove(animation);
                    animation.dispose();
                }
            }
        }

        return completed();
    }

    @Override public void draw(Batch batch)
    {
        if (!completed()) {
            for (Animation animation : animations) {
                animation.draw(batch);
            }
        }
    }

    @Override public void drawDebug(ShapeRenderer shapeRenderer)
    {
        if (!completed()) {
            for (Animation animation : animations) {
                animation.drawDebug(shapeRenderer);
            }
        }
    }
}