diff options
| author | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-10 06:44:07 +0200 | 
|---|---|---|
| committer | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-10 06:44:07 +0200 | 
| commit | f5c6b7a9b4cc6be2db93ff1f274216b90116fec6 (patch) | |
| tree | cd04bc836250284ad94824d10b516c6a878d44ec /core | |
| parent | 909d7771fa7a5e65bc7b11b8674a147620f3350d (diff) | |
| download | gdx-boardgame-f5c6b7a9b4cc6be2db93ff1f274216b90116fec6.zip gdx-boardgame-f5c6b7a9b4cc6be2db93ff1f274216b90116fec6.tar.gz | |
add Orientation
Diffstat (limited to 'core')
6 files changed, 137 insertions, 5 deletions
| diff --git a/core/src/ch/asynk/gdx/boardgame/Orientation.java b/core/src/ch/asynk/gdx/boardgame/Orientation.java new file mode 100644 index 0000000..98d1eda --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/Orientation.java @@ -0,0 +1,90 @@ +package ch.asynk.gdx.boardgame; + +public enum Orientation +{ +    ALL(255,    0), +    KEEP( 0,    0), +    DEFAULT(0, -1), +    N(   1,    -1), +    NW(  2,    -1), +    W(   4,    -1), +    SW(  8,    -1), +    S(  16,    -1), +    SE( 32,    -1), +    E(  64,    -1), +    NE(128,    -1); +    private int s; +    private int r; +    Orientation(int s, int r) +    { +        this.s = s; +        this.r = r; +    } +    public int s() { return s; } +    public int r() { return r; } + +    private static float delta = 5f; + +    public static void setValues(int [] angles) +    { +        DEFAULT.r = angles[0]; +        N.r  = angles[1]; +        NW.r = angles[2]; +        W.r  = angles[3]; +        SW.r = angles[4]; +        S.r  = angles[5]; +        SE.r = angles[6]; +        E.r  = angles[7]; +        NE.r = angles[8]; +    } + +    public int allBut() +    { +        return ALL.s & (s ^ 0xFFFF); +    } + +    public boolean belongsTo(int sides) +    { +        return ((sides & s) == s); +    } + +    public Orientation left() +    { +        Orientation o = (this == NE) ? N : fromS(this.s << 1); +        return (o.r == -1) ? o.left() : o; +    } + +    public Orientation right() +    { +        Orientation o = (this == N) ? NE : fromS(this.s >> 1); +        return (o.r == -1) ? o.right() : o; +    } + +    public static Orientation fromS(int s) +    { +        if (s == N.s) return N; +        else if (s == NW.s) return NW; +        else if (s == W.s) return W; +        else if (s == SW.s) return SW; +        else if (s == S.s) return S; +        else if (s == SE.s) return SE; +        else if (s == E.s) return E; +        else if (s == NE.s) return NE; +        else return KEEP; +    } + +    public static Orientation fromR(float r) +    { +        if (r < 0) r += 360f; +        if ((r > (N.r - delta)) && (r < (N.r + delta))) return N; +        else if ((r > (NW.r - delta)) && (r < (NW.r + delta))) return NW; +        else if ((r > (W.r - delta)) && (r < (W.r + delta))) return W; +        else if ((r > (SW.r - delta)) && (r < (SW.r + delta))) return SW; +        else if ((r > (S.r - delta)) && (r < (S.r + delta))) return S; +        else if ((r > (SE.r - delta)) && (r < (SE.r + delta))) return SE; +        else if ((r > (E.r - delta)) && (r < (E.r + delta))) return E; +        else if ((r > (NE.r - delta)) && (r < (NE.r + delta))) return NE; +        else return KEEP; + +    } +} diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java index ab12ce6..1037842 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/Board.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.math.Vector2;  public interface Board  { +    public int[] getAngles();      public void centerOf(int x, int y, Vector2 v);      public void toBoard(float x, float y, Vector2 v);  } diff --git a/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java index ff5ba08..8eba838 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/BoardFactory.java @@ -1,5 +1,7 @@  package ch.asynk.gdx.boardgame.boards; +import ch.asynk.gdx.boardgame.Orientation; +  public class BoardFactory  {      public enum BoardType @@ -25,16 +27,24 @@ public class BoardFactory      public static Board getBoard(BoardType boardType, float side, float x0, float y0, BoardOrientation boardOrientation)      { +        Board board = null;          switch(boardType)          {              case HEX: -                return new HexBoard(side, x0, y0, boardOrientation); +                board = new HexBoard(side, x0, y0, boardOrientation); +                break;              case SQUARE: -                return new SquareBoard(side, x0, y0); +                board = new SquareBoard(side, x0, y0); +                break;              case TRIANGLE: -                return new TriangleBoard(side, x0, y0, boardOrientation); -            default: -                throw new RuntimeException( String.format("%s board type is not implemented yet.", boardType) ); +                board = new TriangleBoard(side, x0, y0, boardOrientation); +                break; +        } +        if (board == null) { +            throw new RuntimeException( String.format("%s board type is not implemented yet.", boardType) );          } +        Orientation.setValues(board.getAngles()); + +        return board;      }  } diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java index cd66e5a..30aef0b 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java @@ -33,6 +33,9 @@ public class HexBoard implements Board      //   rows are vertical°      //   bottom left is the left vertice of the most bottom-left horizontal hex side of the map +    private static final int [] vAngles = {330, -1, 30, 90, 150,  -1, 210, 270, 330}; +    private static final int [] hAngles = {  0,  0, 60, -1, 120, 180, 240,  -1, 300}; +      public HexBoard(float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation)      {          this.side = side; @@ -47,6 +50,15 @@ public class HexBoard implements Board          this.slope = dh / dw;      } +    @Override public int[] getAngles() +    { +        if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) { +            return vAngles; +        } else { +            return hAngles; +        } +    } +      @Override public void centerOf(int x, int y, Vector2 v)      {          float cx = this.x0; diff --git a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java index 8ad98f7..bfe23a7 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/SquareBoard.java @@ -8,6 +8,8 @@ public class SquareBoard implements Board      private final float x0;     // bottom left x offset      private final float y0;     // bottom left y offset +    private static final int [] angles = { 0, 0, -1, 90, -1, 180, -1, 270, -1}; +      public SquareBoard(float side, float x0, float y0)      {          this.side = side; @@ -15,6 +17,11 @@ public class SquareBoard implements Board          this.y0 = y0;      } +    @Override public int[] getAngles() +    { +        return angles; +    } +      @Override public void centerOf(int x, int y, Vector2 v)      {          float cx = this.x0 + (this.side / 2) + (this.side * x); diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java index dcb6324..f49550d 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java @@ -16,6 +16,9 @@ public class TriangleBoard implements Board      private final float h23;    // 2/3 height of the triangle      private final float h43;    // 4/3 height of the triangle +    private static final int [] vAngles = {330, -1, 30, 90, 150,  -1, 210, 270, 330}; +    private static final int [] hAngles = {  0,  0, 60, -1, 120, 180, 240,  -1, 300}; +      public TriangleBoard(float side, float x0, float y0, BoardFactory.BoardOrientation boardOrientation)      {          this.side = side; @@ -31,6 +34,15 @@ public class TriangleBoard implements Board          this.h43 = this.h * 1.33333f;      } +    @Override public int[] getAngles() +    { +        if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) { +            return vAngles; +        } else { +            return hAngles; +        } +    } +      @Override public void centerOf(int x, int y, Vector2 v)      {          float cx = this.x0; | 
