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

import java.util.Iterator;

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

import ch.asynk.gdx.boardgame.pieces.Piece;
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 Path()
    {
        super(defaultCapacity);
    }

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

    @Override public void reset()
    {
        clear();
        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 boolean nextVector(Piece piece, Vector3 v)
    {
        if (hasNext()) {
            Tile cur = current();
            if (piece.isOn(cur)) {
                // rotation ...
                next();
                Orientation o = (hasNext() ? Orientation.fromTiles(cur, current()) : finalOrientation);
                // if already facing, transform into a move
                if (piece.isFacing(o)) {
                    cur = current();
                    if (cur == null) {
                        return true;
                    }
                }
                piece.getPosOn(cur, o, v);
            } else {
                // regular move, no rotation
                piece.getPosOn(cur, Orientation.fromR(v.z), v);
            }
            return false;
        }
        return true;
    }
}