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

public enum Orientation
{
    ALL(0, 63),
    KEEP(0, 0),
    NORTH(270, 1),
    NORTH_EAST(210, 2),
    SOUTH_EAST(150, 4),
    SOUTH(90, 8),
    SOUTH_WEST (30, 16),
    NORTH_WEST(330, 32);

    public static int offset = 0;
    public static float delta = 5f;
    private final int r;
    public final int s;

    Orientation(int r, int s) { this.r = r; this.s = s; }

    public float r() { return offset + r; }

    public boolean isInSides(int sides)
    {
        return ((sides & s) == s);
    }

    public Orientation left()
    {
        if (this == NORTH) return NORTH_WEST;
        else return fromSide(s >> 1);
    }

    public Orientation right()
    {
        if (this == NORTH_WEST) return NORTH;
        else return fromSide(s << 1);
    }

    public Orientation opposite()
    {
        return left().left().left();
    }

    public int allBut()
    {
        return ALL.s & (s ^ 0xFFFF);
    }

    public int getFrontSides()
    {
        return s | left().s | right().s;
    }

    public int getBackSides()
    {
        return opposite().getFrontSides();
    }

    public static Orientation fromSide(int s)
    {
        if (s == 1) return NORTH;
        else if (s == NORTH_EAST.s) return NORTH_EAST;
        else if (s == SOUTH_EAST.s) return SOUTH_EAST;
        else if (s == SOUTH.s) return SOUTH;
        else if (s == SOUTH_WEST.s) return SOUTH_WEST;
        else if (s == NORTH_WEST.s) return NORTH_WEST;
        else return KEEP;
    }

    public static Orientation fromRotation(float r)
    {
        if (r < 0) r += 360f;
        if ((r > (NORTH.r - 5f)) && (r < (NORTH.r + 5f))) return NORTH;
        else if ((r > (NORTH_EAST.r - delta)) && (r < (NORTH_EAST.r + delta))) return NORTH_EAST;
        else if ((r > (SOUTH_EAST.r - delta)) && (r < (SOUTH_EAST.r + delta))) return SOUTH_EAST;
        else if ((r > (SOUTH.r - delta)) && (r < (SOUTH.r + delta))) return SOUTH;
        else if ((r > (SOUTH_WEST.r - delta)) && (r < (SOUTH_WEST.r + delta))) return SOUTH_WEST;
        else if ((r > (NORTH_WEST.r - delta)) && (r < (NORTH_WEST.r + delta))) return NORTH_WEST;
        else return KEEP;
    }

    public static Orientation fromMove(int col0, int row0, int col1, int row1)
    {
        int dx = col1 - col0;
        int dy = row1 - row0;

        if (dy == 0) {
            if (dx == 0) return Orientation.KEEP;
            if (dx > 0) return Orientation.NORTH;
            return Orientation.SOUTH;
        }
        if (dy > 0) {
            if (dx > 0) return Orientation.NORTH_WEST;
            return Orientation.SOUTH_WEST;
        } else {
            if (dx < 0) return Orientation.SOUTH_EAST;
            return Orientation.NORTH_EAST;
        }
    }
}