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

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

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()
    {
        String s = String.format(" o:%s\n", finalOrientation);
        for (Tile t : this)
            s += String.format("  %s\n", t.toString());
        return s;
    }

    public Tile from()
    {
        return from;
    }

    public Tile to()
    {
        return to;
    }

    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;
    }
}