summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/game/PawnImage.java
blob: 9381a6de9d2833bc7745b7bdf25de81f9305384e (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
package ch.asynk.tankontank.game;

import java.util.ArrayDeque;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;

import com.badlogic.gdx.math.Vector3;

public class PawnImage extends Image implements Pawn
{
    private static final float MOVE_TIME = 0.3f;
    private static final float ROTATE_TIME = 0.2f;

    private ArrayDeque<Vector3> path = new ArrayDeque<Vector3>();

    public PawnImage(TextureRegion region)
    {
        super(region);
        setOrigin((getWidth() / 2.f), (getHeight() / 2.f));
    }

    public Vector3 getLastPosition()
    {
        if ((path == null) || (path.size() == 0)) return null;
        return path.getFirst();
    }

    public void pushMove(float x, float y, int z, HexOrientation r)
    {
        setPosition(x, y);
        if (r != HexOrientation.KEEP) setRotation(r.v);
        setZIndex(z);
        path.push(new Vector3(x, y, r.v));
    }

    public void resetMoves(Runnable cb)
    {
        final Pawn self = this;
        final Vector3 finalPos = path.getLast();

        SequenceAction seq = new SequenceAction();
        while(path.size() != 0) {
            Vector3 v = path.pop();
            seq.addAction(Actions.moveTo(v.x, v.y, MOVE_TIME));
            if (v.z != HexOrientation.KEEP.v)
                seq.addAction(Actions.rotateTo(v.z, ROTATE_TIME));
        }

        seq.addAction( Actions.run(new Runnable() {
            @Override
            public void run() {
                path.push(finalPos);
            }
        }));

        // map set z index and push me in hex stack
        seq.addAction(Actions.run(cb));

        addAction(seq);
    }

    public void moveDone()
    {
        Vector3 v = path.pop();
        path.clear();
        path.push(v);
    }
}