summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/Path.java
blob: a7e1d5e4c2f09fdd203481208e4926939e28b60d (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
package ch.asynk.gdx.boardgame;

import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.StringBuilder;

import ch.asynk.gdx.boardgame.utils.IterableArray;

public class Path extends IterableArray<Tile> implements Disposable, Pool.Poolable
{
    public static int defaultCapacity = 8;

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

    public static Path obtain()
    {
        return pathPool.obtain();
    }

    private Orientation finalOrientation;
    private Tile from;
    private Tile to;

    private Path()
    {
        super(defaultCapacity);
    }

    public void setFinalOrientation(Orientation orientation)
    {
        this.finalOrientation = orientation;
    }

    @Override public void reset()
    {
        clear();
        this.from = null;
        this.to = null;
        this.finalOrientation = null;
    }

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

    @Override public String toString()
    {
        StringBuilder s = new StringBuilder(" o:").append(finalOrientation).append("\n");
        for (Tile t : this)
            s.append("  ").append(t).append("\n");
        return s.toString();
    }

    public Tile from()
    {
        return from;
    }

    public Tile to()
    {
        return to;
    }

    public void show(int i, int j)
    {
        Tile prev = null;
        for (int n = 0; n < size(); n++) {
            final Tile tile = get(n);
            if (prev != null) {
                final Orientation o = Orientation.fromTiles(prev, tile);
                prev.enableOverlay(i, o);
                tile.enableOverlay(j, o.opposite());
            }
            prev = tile;
        }
    }

    public boolean nextPosition(Piece piece, Vector3 v)
    {
        if (hasNext()) {
            to = current();
            if (piece.isOn(to)) {
                // rotation ...
                from = to;
                next();
                Orientation o = (hasNext() ? Orientation.fromTiles(from, current()) : finalOrientation);
                // if already facing, transform into a straight move
                if (piece.isFacing(o)) {
                    to = current();
                    if (to == null) {
                        return true;
                    }
                }
                piece.getPosOn(to, o, v);
            } else {
                // rotation finished, regular move
                piece.getPosOn(to, Orientation.fromR(v.z), v);
            }
            return false;
        }
        to = null;
        return true;
    }
}