summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/gfx/animations/MoveToAnimation.java
blob: 1f5ede51dabffd03b0fac0c721c31105a1b7e22e (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package ch.asynk.rustanddust.engine.gfx.animations;

import com.badlogic.gdx.math.Vector3;
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;

public class MoveToAnimation extends TimedAnimation
{
    public interface MoveToAnimationCb {
        void moveToAnimationLeave(Moveable moveable, float x, float y, float r);
        void moveToAnimationEnter(Moveable moveable, float x, float y, float r);
        void moveToAnimationDone(Moveable moveable, float x, float y, float r);
    }

    private Moveable moveable;
    private float fromX;
    private float fromY;
    private float fromR;
    private float toX;
    private float toY;
    private float toR;
    private float rDelta;
    private boolean aimed;
    private float aimDelta;
    private boolean notified;
    private MoveToAnimationCb cb;

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

    public static MoveToAnimation get(Moveable moveable, Vector3 v, float duration)
    {
        return get(moveable, v.x, v.y, v.z, duration);
    }

    public static MoveToAnimation get(Moveable moveable, Vector3 v, float duration, MoveToAnimationCb cb)
    {
        return get(moveable, v.x, v.y, v.z, duration, cb);
    }

    public static MoveToAnimation get(Moveable moveable, float x, float y, float r, float duration)
    {
        return get(moveable, x, y, r, duration, null);
    }

    public static MoveToAnimation get(Moveable moveable, float x, float y, float r, float duration, MoveToAnimationCb cb)
    {
        MoveToAnimation a = moveToAnimationPool.obtain();

        a.moveable = moveable;
        a.toX = x;
        a.toY = y;
        a.toR = r;
        a.duration = duration;
        a.cb = cb;
        a.rDelta = 0;
        a.notified = false;

        return a;
    }

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

    @Override
    protected void begin()
    {
        fromX = moveable.getX();
        fromY = moveable.getY();
        fromR = moveable.getRotation();
        notified = ((fromX == toX) && (fromY == toY));

        if (Math.abs(toR - fromR) <= 180.f)
            rDelta = (toR - fromR);
        else {
            if (toR > fromR)
                rDelta = (toR - 360 - fromR);
            else
                rDelta = (toR + 360 - fromR);
        }

        aimDelta = moveable.getAiming();
        aimed = (Math.abs(aimDelta) < 1f);
    }

    @Override
    protected void end()
    {
        if (cb != null)
            cb.moveToAnimationDone(moveable, (toX + (moveable.getWidth() / 2)), (toY + (moveable.getHeight() / 2)), toR);
        dispose();
    }

    @Override
    protected void update(float percent)
    {
        if ((cb != null) && !notified && (percent >= 0.5)) {
            float dw = (moveable.getWidth() / 2);
            float dh = (moveable.getHeight() / 2);
            cb.moveToAnimationLeave(moveable, (fromX + dw), (fromY + dh), fromR);
            cb.moveToAnimationEnter(moveable, (toX + dw), (toY + dh), toR);
            notified = true;
        }
        if (percent == 1f) {
            moveable.setPosition(toX, toY, (int) toR);
            if (!aimed) moveable.aimAt(0f);
        }
        else {
            moveable.setPosition(fromX + ((toX - fromX) * percent), fromY + ((toY - fromY) * percent), (fromR + (rDelta * percent)));
            if (!aimed) moveable.aimAt((aimDelta * (1f - percent)));
        }
    }

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

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