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

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, Pawn.Orientation r)
    {
        setPosition(x, y);
        if (r != Pawn.Orientation.KEEP) setRotation(r.v);
        setZIndex(z);
        Vector3 v = new Vector3(x, y, r.v);
        if ((path.size() == 0) || (!v.equals(path.getFirst())))
            path.push(new Vector3(x, y, r.v));
    }

    public void resetMoves(Runnable cb)
    {
        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 != Pawn.Orientation.KEEP.v)
                seq.addAction(Actions.rotateTo(v.z, ROTATE_TIME));
        }

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

        // the map must finalize this move
        seq.addAction(Actions.run(cb));

        addAction(seq);
    }

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