From 1119f87fff38d7c82091d1724059cb2ff22c1894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 18 Jun 2020 14:43:17 +0200 Subject: =?UTF-8?q?HexBoard=20:=20merge=20verticalLineOfSight(=E2=80=A6)?= =?UTF-8?q?=20into=20diagonalLineOfSight(=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ch/asynk/gdx/boardgame/boards/HexBoard.java | 75 +++++----------------- 1 file changed, 17 insertions(+), 58 deletions(-) diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java index 785da0e..c371b71 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java @@ -312,10 +312,8 @@ public class HexBoard implements Board int dx3 = 3 * dx; int dy3 = 3 * dy; - if (dx == 0) - return verticalLineOfSight(x0, y0, x1, y1, tiles); - if (dx == dy3) - return diagonalLineOfSight(x0, y0, x1, y1, tiles); + if (dx == 0 || dx == dy3) + return diagonalLineOfSight(x0, y0, x1, y1, (dx == 0), q13, tiles, v); // angle is less than 45° boolean flat = (dx > dy3); @@ -367,58 +365,10 @@ public class HexBoard implements Board return tiles.get(tiles.size() - 1).blocked; } - private boolean verticalLineOfSight(int x0, int y0, int x1, int y1, Collection tiles) - { - int dt = ( (y1 > y0) ? 1 : -1); - int x = x0; - int y = y0; - - - Tile from = getTile(x0, y0); - Tile to = getTile(x1, y1); - float d = distance(x0, y0, x1, y1); - tiles.add(from); - from.blocked = false; - int blocked = 0; - boolean losBlocked = false; - while ((x != x1) || (y != y1)) { - - y += dt; // up left - Tile t = getTile(x, y); - if (t.isOnMap()) { - tiles.add(t); - t.blocked = losBlocked; - if (t.blockLos(from, to, d, distance(x0, y0, x, y))) - blocked |= 0x01; - } - - x += dt; // up right - t = getTile(x, y); - if (t.isOnMap()) { - tiles.add(t); - t.blocked = losBlocked; - if (t.blockLos(from, to, d, distance(x0, y0, x, y))) - blocked |= 0x02; - } - - y += dt; // vertical - t = getTile(x, y); - if (t.isOnMap()) { - tiles.add(t); - t.blocked = (losBlocked || blocked == 0x03); - losBlocked = (t.blocked || t.blockLos(from, to, d, distance(x0, y0, x, y))); - } - } - - return tiles.get(tiles.size() - 1).blocked; - } - - private boolean diagonalLineOfSight(int x0, int y0, int x1, int y1, Collection tiles) + private boolean diagonalLineOfSight(int x0, int y0, int x1, int y1, boolean vert, boolean q13, Collection tiles, Vector2 v) { int dy = ( (y1 > y0) ? 1 : -1); int dx = ( (x1 > x0) ? 1 : -1); - // quadrant I && III - boolean q13 = (((dx >= 0) && (dy >= 0)) || ((dx < 0) && (dy < 0))); int x = x0; int y = y0; @@ -432,7 +382,10 @@ public class HexBoard implements Board boolean losBlocked = false; while ((x != x1) || (y != y1)) { - x += dx; // right + if (vert) + y += dy; // up left + else + x += dx; // right Tile t = getTile(x, y); if (t.isOnMap()) { tiles.add(t); @@ -441,9 +394,12 @@ public class HexBoard implements Board blocked |= 0x01; } - y += dy; // up right - if (!q13) - x -= dx; + if (vert) + x += dx; // up right + else { + y += dy; // up right + if (!q13) x -= dx; + } t = getTile(x, y); if (t.isOnMap()) { tiles.add(t); @@ -452,7 +408,10 @@ public class HexBoard implements Board blocked |= 0x02; } - x += dx; // diagonal + if (vert) + y += dy; // up + else + x += dx; // diagonal t = getTile(x, y); if (t.isOnMap()) { tiles.add(t); -- cgit v1.1-2-g2b99