From f6800af5b9013589e305cccf5cdcfe91a3981a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 3 Jun 2020 18:04:28 +0200 Subject: =?UTF-8?q?Tile=20:=20add=20distances=20to=20blockLos(=E2=80=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/ch/asynk/gdx/boardgame/Tile.java | 2 +- .../ch/asynk/gdx/boardgame/boards/HexBoard.java | 25 ++++++++++++--------- .../src/ch/asynk/gdx/boardgame/test/HexScreen.java | 26 ++++++++++++++++++---- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/core/src/ch/asynk/gdx/boardgame/Tile.java b/core/src/ch/asynk/gdx/boardgame/Tile.java index 077a796..78a17e6 100644 --- a/core/src/ch/asynk/gdx/boardgame/Tile.java +++ b/core/src/ch/asynk/gdx/boardgame/Tile.java @@ -43,7 +43,7 @@ public class Tile implements Drawable return onMap; } - public boolean blockLos(final Tile from, final Tile to) + public boolean blockLos(final Tile from, final Tile to, float d, float dt) { return false; } diff --git a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java index b560e1a..a0f1149 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/HexBoard.java @@ -312,6 +312,7 @@ public class HexBoard implements Board Tile from = getTile(x0, y0); Tile to = getTile(x1, y1); + float d = distance(x0, y0, x1, y1); tiles.add(from); boolean losBlocked = false; while ((x != x1) || (y != y1)) { @@ -345,7 +346,7 @@ public class HexBoard implements Board final Tile t = getTile(x, y); tiles.add(t); t.blocked = losBlocked; - losBlocked = (losBlocked || t.blockLos(from, to)); + losBlocked = (losBlocked || t.blockLos(from, to, d, distance(x0, y0, x, y))); } return tiles.get(tiles.size() - 1).blocked; @@ -353,40 +354,41 @@ public class HexBoard implements Board private boolean verticalLineOfSight(int x0, int y0, int x1, int y1, Collection tiles) { - int d = ( (y1 > y0) ? 1 : -1); + 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); boolean losBlocked = false; while ((x != x1) || (y != y1)) { boolean blocked = losBlocked; - y += d; // up left + y += dt; // up left Tile t = getTile(x, y); if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; - blocked = (blocked || t.blockLos(from, to)); + blocked = (blocked || t.blockLos(from, to, d, distance(x0, y0, x, y))); } - x += d; // up right + x += dt; // up right t = getTile(x, y); if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; - blocked = (blocked && t.blockLos(from, to)); + blocked = (blocked && t.blockLos(from, to, d, distance(x0, y0, x, y))); } - y += d; // vertical + y += dt; // vertical t = getTile(x, y); if (t.isOnMap()) { tiles.add(t); t.blocked = (losBlocked || blocked); - losBlocked = (t.blocked || t.blockLos(from, to)); + losBlocked = (t.blocked || t.blockLos(from, to, d, distance(x0, y0, x, y))); } } @@ -405,6 +407,7 @@ public class HexBoard implements Board Tile from = getTile(x0, y0); Tile to = getTile(x1, y1); + float d = distance(x0, y0, x1, y1); tiles.add(from); boolean losBlocked = false; while ((x != x1) || (y != y1)) { @@ -415,7 +418,7 @@ public class HexBoard implements Board if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; - blocked = (blocked || t.blockLos(from, to)); + blocked = (blocked || t.blockLos(from, to, d, distance(x0, y0, x, y))); } y += dy; // up right @@ -425,7 +428,7 @@ public class HexBoard implements Board if (t.isOnMap()) { tiles.add(t); t.blocked = losBlocked; - blocked = (blocked && t.blockLos(from, to)); + blocked = (blocked && t.blockLos(from, to, d, distance(x0, y0, x, y))); } x += dx; // diagonal @@ -433,7 +436,7 @@ public class HexBoard implements Board if (t.isOnMap()) { tiles.add(t); t.blocked = (losBlocked || blocked); - losBlocked = (t.blocked || t.blockLos(from, to)); + losBlocked = (t.blocked || t.blockLos(from, to, d, distance(x0, y0, x, y))); } } diff --git a/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java b/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java index 1d5947c..b6c7f28 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/HexScreen.java @@ -1,5 +1,7 @@ package ch.asynk.gdx.boardgame.test; +import com.badlogic.gdx.math.MathUtils; + import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; @@ -35,7 +37,7 @@ public class HexScreen extends AbstractScreen private enum Terrain { - WOODS, CITY, HILL, PLAIN; + WOODS(0,1), CITY(0,2), HILL(2,0), PLAIN(0,0); static private int[] cv = {23, 74}; static private int[] hv = {68, 78, 79, 15, 45, 46}; @@ -46,6 +48,13 @@ public class HexScreen extends AbstractScreen static public boolean v = true; + public int elevation; + public int height; + private Terrain(int elevation, int height) { + this.elevation = elevation; + this.height = height; + } + static public Terrain get(int k) { if (v) { @@ -89,10 +98,19 @@ public class HexScreen extends AbstractScreen this.terrain = terrain; } - @Override public boolean blockLos(final Tile from, final Tile to) + @Override public boolean blockLos(final Tile from, final Tile to, float d, float dt) { - if (terrain != Terrain.PLAIN) return true; - return false; + int h = terrain.elevation + terrain.height; + if (h == 0) return false; + + int e = ((Hex)from).terrain.elevation; + if (e > h) { + if (((Hex)to).terrain.elevation > h) return false; + return (h * dt / (e - h)) >= (d - dt); + } else { + h -= e; + return (h * d / dt) >= (((Hex)to).terrain.elevation - e); + } } public String toString() -- cgit v1.1-2-g2b99