summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/game/PawnImage.java
blob: 0f7a77dfa7dcfb7f8744dba0f049a41279850a6f (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
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.Vector2;
import com.badlogic.gdx.math.GridPoint2;
import com.badlogic.gdx.math.GridPoint3;

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

    private HexMap map;
    private ArrayDeque<GridPoint3> path = new ArrayDeque<GridPoint3>();

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

    public GridPoint3 getBoardPosition()
    {
        if (path.size() == 0) return null;
        return path.getFirst();
    }

    public void moveTo(GridPoint2 hex)
    {
        moveTo(new GridPoint3(hex.x, hex.y, (int) getRotation()));
    }

    public void moveTo(int col, int row, int angle)
    {
        moveTo(new GridPoint3(col, row, angle));
    }

    private void moveTo(GridPoint3 hex)
    {
        if ((hex.x == -1) || (hex.y == -1)) {
            resetMoves();
        } else {
            map.setPawnAt(this, hex);
            path.push(hex);
        }
    }

    public void resetMoves()
    {
        final Pawn self = this;
        final GridPoint3 finalHex = path.getLast();

        SequenceAction seq = new SequenceAction();
        while(path.size() != 0) {
            Vector2 v = map.getPawnPosAt(this, path.pop());
            seq.addAction(Actions.moveTo(v.x, v.y, MOVE_TIME));
        }

        seq.addAction( Actions.run(new Runnable() {
            @Override
            public void run() {
                map.setPawnAt(self, finalHex);
                path.push(finalHex);
            }
        }));

        addAction(seq);
    }

    public void moveDone()
    {
        GridPoint3 hex = path.pop();
        path.clear();
        path.push(hex);
    }
}