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

import java.util.ArrayDeque;

import com.badlogic.gdx.utils.Disposable;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import com.badlogic.gdx.math.Vector3;

import ch.asynk.tankontank.engine.gfx.Image;
import ch.asynk.tankontank.engine.gfx.StackedImages;
import ch.asynk.tankontank.engine.gfx.animations.MoveToAnimation;
import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation;
import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence;

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

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

    public Pawn(TextureRegion region, TextureAtlas atlas)
    {
        super(region);
        if (atlas != null)
            this.overlays = new StackedImages(atlas);
    }

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

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

    public void pushMove(float x, float y, Board.Orientation o)
    {
        float r = ((o == Board.Orientation.KEEP) ? getRotation() : o.r());
        setPosition(x, y, r);
        Vector3 v = new Vector3(x, y, r);
        if ((path.size() == 0) || (!v.equals(path.getFirst())))
            path.push(new Vector3(x, y, r));
    }

    public AnimationSequence getResetMovesAnimation()
    {
        final Vector3 finalPos = path.getLast();

        AnimationSequence seq = AnimationSequence.get(path.size() + 1);

        while(path.size() != 0) {
            seq.addAnimation(MoveToAnimation.get(this, path.pop(), MOVE_TIME));
        }

        seq.addAnimation(RunnableAnimation.get(this, new Runnable() {
            @Override
            public void run() {
                path.push(finalPos);
            }
        }));

        return seq;
    }

    public boolean hasOverlayEnabled()
    {
        if (overlays == null) return false;
        return overlays.isEnabled();
    }

    public boolean enableOverlay(int i, boolean enable)
    {
        if (overlays == null) return false;
        overlays.enable(i, enable);
        if (enable) return true;
        return hasOverlayEnabled();
    }

    @Override
    public void translate(float x, float y)
    {
        super.translate(x, y);
        if (overlays != null) overlays.translate(x, y);
    }

    @Override
    public void setPosition(float x, float y, float z)
    {
        super.setPosition(x, y, z);
        if (overlays != null) overlays.setPosition(x, y, z);
    }

    @Override
    public void draw(Batch batch, float parentAlpha)
    {
        super.draw(batch, parentAlpha);
        if (overlays != null) overlays.draw(batch, parentAlpha);
    }

    @Override
    public void drawDebug(ShapeRenderer debugShapes)
    {
        super.drawDebug(debugShapes);
        if (overlays != null) overlays.drawDebug(debugShapes);
    }
}