summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java
blob: d3c9009e177d03d10e9bd54c78bd6a69f5bc73e2 (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
package ch.asynk.gdx.boardgame.animations;

import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.math.MathUtils;

import ch.asynk.gdx.boardgame.Piece;

public class BounceAnimation extends TimedAnimation implements Pool.Poolable
{
    private static final Pool<BounceAnimation> bounceAnimationPool = new Pool<BounceAnimation>()
    {
        @Override protected BounceAnimation newObject()
        {
            return new BounceAnimation();
        }
    };

    public static BounceAnimation obtain(Piece piece, float duration, float bounceFactor, int rotations)
    {
        BounceAnimation a = bounceAnimationPool.obtain();

        a.piece = piece;
        a.bounceFactor = bounceFactor;
        a.setDuration(duration);
        a.computeRotationDegrees(rotations);

        return a;
    }

    private Piece piece;
    private float startScale;
    private float startRotation;
    private float bounceFactor;
    private float rotationDegrees;

    private BounceAnimation()
    {
    }

    private void computeRotationDegrees(int rotations)
    {
        if (rotations == 0) {
            rotationDegrees = 0f;
        } else {
            rotationDegrees = (360 * rotations);
        }
    }

    @Override public void reset()
    {
        super.reset();
    }

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

    @Override protected void begin()
    {
        this.startScale = piece.getScale();
        this.startRotation = piece.getRotation();
    }

    @Override protected void end()
    {
        piece.setScale(this.startScale);
        if (rotationDegrees != 0f) {
            piece.setRotation(this.startRotation);
        }
    }

    @Override protected void update(float delta)
    {
        piece.setScale(this.startScale + this.bounceFactor * (float) MathUtils.sin(percent * MathUtils.PI));
        if (rotationDegrees != 0f) {
            piece.setRotation(this.startRotation + (percent * this.rotationDegrees));
        }
    }
}