diff options
| author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-27 23:35:05 +0200 | 
|---|---|---|
| committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-27 23:35:05 +0200 | 
| commit | 619a78c85048be3a92253d680fcb48b5c8a7d820 (patch) | |
| tree | e5df03ad0a0a6b1d1601a4a00269a14c508b2bc0 | |
| parent | 90540cdf46b9a04af48926bcba2d971a54b6d640 (diff) | |
| download | RustAndDust-619a78c85048be3a92253d680fcb48b5c8a7d820.zip RustAndDust-619a78c85048be3a92253d680fcb48b5c8a7d820.tar.gz | |
go from orthogonal coordinates to skew coordinates
- X axis follows the north, tiles in the same row have the same Y
- Y axis follows the south-west, tiles in that column have the same X
this complicates the transormation from tiles coordinates into array offset,
but eases movement computation and should be more than usefull for line
of sight computaion
| -rw-r--r-- | core/src/ch/asynk/tankontank/engine/Board.java | 48 | ||||
| -rw-r--r-- | core/src/ch/asynk/tankontank/engine/SearchBoard.java | 86 | 
2 files changed, 60 insertions, 74 deletions
| diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java index a923fb4..f64e294 100644 --- a/core/src/ch/asynk/tankontank/engine/Board.java +++ b/core/src/ch/asynk/tankontank/engine/Board.java @@ -176,15 +176,15 @@ public abstract class Board extends Image implements Disposable      public Tile getTile(int col, int row)      { -        return tiles[col + (row * cfg.cols)]; +        int idx = ((col - ((row + 1) / 2))) + (row * cfg.cols); +        // Gdx.app.debug("Board", " getTile: " + col + " ; " + row + " -> " + idx); +        return tiles[idx];      }      public int distance(int col0, int row0, int col1, int row1)      {          int a = (row1 - row0); -        // transform into a system where all tiles in the same row have the same value of X -        // and all tiles in the same column have the same value of Y non-staggering coordinates -        int b = ((col1 + ((row1 + 1) / 2)) - (col0 + ((row0 + 1) / 2))); +        int b = (col1 - col0);          int c = (b - a);          int aa = Math.abs(a);          int ab = Math.abs(b); @@ -452,57 +452,59 @@ public abstract class Board extends Image implements Disposable          return getHexAt(null, v.x, v.y);      } -    public GridPoint2 getHexAt(GridPoint2 hex, float cx, float cy) +    public GridPoint2 getHexAt(GridPoint2 hex, float mx, float my)      {          if (hex == null) hex = new GridPoint2();          // compute row -        int row; -        boolean oddRow = true; -        float y = (cy - cfg.y0); +        float y = (my - cfg.y0); +        int row = (int) (y / cfg.h); +        boolean oddRow = ((row % 2) == 1);          if (y < 0.f) {              row = -1; -        } else { -            row = (int) (y / cfg.h); -            oddRow = ((row % 2) == 1); +            oddRow = true;          }          // compute col -        int col; -        float x = (cx - cfg.x0); +        float x = (mx - cfg.x0);          if (oddRow) x -= cfg.dw; -        if (x < 0.f) { +        int col = (int) (x / cfg.w); +        if (x < 0.f)              col = -1; -        } else { -            col = (int) (x / cfg.w); -        } + +        int colOffset = ((row +1) / 2);          // check upper boundaries          float dy = (y - (row * cfg.h));          if (dy > cfg.s) {              dy -= cfg.s;              float dx = (x - (col * cfg.w)); +            col += colOffset;              if (dx < cfg.dw) {                  if ((dx * cfg.slope) < dy) { +                    // upper left corner                      row += 1; -                    if (!oddRow) col -= 1; -                    oddRow = !oddRow; +                    colOffset = ((row +1) / 2);                  }              } else {                  if (((cfg.w - dx) * cfg.slope) < dy) { +                    // upper right corner                      row += 1; -                    if (oddRow) col += 1; -                    oddRow = !oddRow; +                    col += 1; +                    colOffset = ((row +1) / 2);                  }              } -        } +        } else +            col += colOffset; +          // validate hex -        if ((col < 0) || (row < 0) || (row >= cfg.rows) || (col >= cfg.cols) || (oddRow && ((col + 1) >= cfg.cols))) +        if ((col < colOffset) || (row < 0) || (row >= cfg.rows) || ((col - colOffset) >= cfg.cols))              hex.set(-1, -1);          else              hex.set(col, row); +        Gdx.app.debug("Board", " hex: " + hex.x + " ; " + hex.y);          return hex;      }  } diff --git a/core/src/ch/asynk/tankontank/engine/SearchBoard.java b/core/src/ch/asynk/tankontank/engine/SearchBoard.java index 0366b9a..a9cee78 100644 --- a/core/src/ch/asynk/tankontank/engine/SearchBoard.java +++ b/core/src/ch/asynk/tankontank/engine/SearchBoard.java @@ -41,9 +41,11 @@ public class SearchBoard          this.rows = rows;          this.board = board;          this.nodes = new Node[cols * rows]; -        for (int j = 0; j < rows; j++) +        for (int j = 0; j < rows; j++) { +            int dx = ((j + 1) / 2);              for (int i = 0; i < cols; i++) -                nodes[i + (j * cols)] = new Node(i, j); +                nodes[i + (j * cols)] = new Node((i + dx), j); +        }          this.searchCount = 0;          this.queue = new LinkedList<Node>();          this.stack = new ArrayDeque<Node>(20); @@ -61,26 +63,22 @@ public class SearchBoard      private Node getNode(int col, int row)      { -        if ((col < 0) || (col >= cols) || (row < 0) || (row >= rows)) return null; -        return nodes[col + (row * cols)]; +        int colOffset = ((row +1) / 2); +        if ((col < colOffset) || (row < 0) || (row >= rows) || ((col - colOffset) >= cols)) +            return null; + +        return nodes[((col - ((row + 1) / 2))) + (row * cols)];      }      public void adjacentMoves(Node src)      { -        // move to enter dst by +        // move to enter dst by directions[i]          adjacents[0] = getNode((src.col - 1), src.row); +        adjacents[1] = getNode(src.col, (src.row + 1)); +        adjacents[2] = getNode((src.col + 1), (src.row + 1));          adjacents[3] = getNode((src.col + 1), src.row); -        if ((src.row % 2) == 0) { -            adjacents[1] = getNode((src.col - 1), (src.row + 1)); -            adjacents[2] = getNode(src.col, (src.row + 1)); -            adjacents[4] = getNode(src.col, (src.row - 1)); -            adjacents[5] = getNode((src.col - 1), (src.row - 1)); -        } else { -            adjacents[1] = getNode(src.col, (src.row + 1)); -            adjacents[2] = getNode((src.col + 1), (src.row + 1)); -            adjacents[4] = getNode((src.col + 1), (src.row - 1)); -            adjacents[5] = getNode(src.col, (src.row - 1)); -        } +        adjacents[4] = getNode(src.col, (src.row - 1)); +        adjacents[5] = getNode((src.col - 1), (src.row - 1));      }      public List<Node> reachableFrom(Pawn pawn, int col, int row) @@ -198,45 +196,31 @@ public class SearchBoard              adjacents[0] = getNode((src.col + 1), src.row);          else              adjacents[0] = null; + +        if (Board.Orientation.NORTH_EAST.isInSides(angle)) +            adjacents[1] = getNode(src.col, (src.row - 1)); +        else +            adjacents[1] = null; + +        if (Board.Orientation.SOUTH_EAST.isInSides(angle)) +            adjacents[2] = getNode((src.col - 1), (src.row - 1)); +        else +            adjacents[2] = null; +          if (Board.Orientation.SOUTH.isInSides(angle))              adjacents[3] = getNode((src.col - 1), src.row);          else              adjacents[3] = null; -        if ((src.row % 2) == 0) { -            if (Board.Orientation.NORTH_EAST.isInSides(angle)) -                adjacents[1] = getNode(src.col, (src.row - 1)); -            else -                adjacents[1] = null; -            if (Board.Orientation.SOUTH_EAST.isInSides(angle)) -                adjacents[2] = getNode((src.col - 1), (src.row - 1)); -            else -                adjacents[2] = null; -            if (Board.Orientation.NORTH_WEST.isInSides(angle)) -                adjacents[4] = getNode(src.col, (src.row + 1)); -            else -                adjacents[4] = null; -            if (Board.Orientation.SOUTH_WEST.isInSides(angle)) -                adjacents[5] = getNode((src.col - 1), (src.row + 1)); -            else -                adjacents[5] = null; -        } else { -            if (Board.Orientation.NORTH_EAST.isInSides(angle)) -                adjacents[1] = getNode((src.col + 1), (src.row - 1)); -            else -                adjacents[1] = null; -            if (Board.Orientation.SOUTH_EAST.isInSides(angle)) -                adjacents[2] = getNode(src.col, (src.row - 1)); -            else -                adjacents[2] = null; -            if (Board.Orientation.NORTH_WEST.isInSides(angle)) -                adjacents[4] = getNode((src.col + 1), (src.row + 1)); -            else -                adjacents[4] = null; -            if (Board.Orientation.SOUTH_WEST.isInSides(angle)) -                adjacents[5] = getNode(src.col, (src.row + 1)); -            else -                adjacents[5] = null; -        } + +        if (Board.Orientation.SOUTH_WEST.isInSides(angle)) +            adjacents[4] = getNode(src.col, (src.row + 1)); +        else +            adjacents[4] = null; + +        if (Board.Orientation.NORTH_WEST.isInSides(angle)) +            adjacents[5] = getNode((src.col + 1), (src.row + 1)); +        else +            adjacents[5] = null;      }      private boolean hasClearLineOfSight(Tile from, Tile to) | 
