diff options
-rw-r--r-- | TODO.md | 2 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/Camera.java | 8 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/Piece.java | 8 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/Positionable.java | 2 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/boards/Board.java | 8 | ||||
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java | 29 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java | 20 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java | 3 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java | 89 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java | 2 | ||||
-rw-r--r-- | test/src/ch/asynk/gdx/boardgame/test/UiScreen.java | 2 |
11 files changed, 127 insertions, 46 deletions
@@ -1,6 +1,6 @@ ## TODO - UI widgets - - Box V/H/Grid + - Grid - Algos - Line Of Sight diff --git a/core/src/ch/asynk/gdx/boardgame/Camera.java b/core/src/ch/asynk/gdx/boardgame/Camera.java index 5140538..322c6d5 100644 --- a/core/src/ch/asynk/gdx/boardgame/Camera.java +++ b/core/src/ch/asynk/gdx/boardgame/Camera.java @@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.glutils.HdpiUtils; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Matrix4; @@ -152,7 +153,7 @@ public class Camera extends OrthographicCamera { float deltaX = (dx * zoom * widthFactor); float deltaY = (dy * zoom * heightFactor); - translate(deltaX, -deltaY, 0); + translate(-deltaX, deltaY, 0); clampPosition(); update(true); } @@ -179,6 +180,11 @@ public class Camera extends OrthographicCamera } } + public void unprojectTranslation(int dx, int dy, Vector2 v) + { + v.set((dx * zoom * widthFactor), (-dy * zoom * heightFactor)); + } + public void unproject(int x, int y, Vector3 v) { unproject(v.set(x, y, 0), viewport.x, viewport.y, viewport.width, viewport.height); diff --git a/core/src/ch/asynk/gdx/boardgame/Piece.java b/core/src/ch/asynk/gdx/boardgame/Piece.java index ee0353a..6d2bf52 100644 --- a/core/src/ch/asynk/gdx/boardgame/Piece.java +++ b/core/src/ch/asynk/gdx/boardgame/Piece.java @@ -10,6 +10,7 @@ import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.MathUtils; +import ch.asynk.gdx.boardgame.boards.Board; import ch.asynk.gdx.boardgame.Drawable; import ch.asynk.gdx.boardgame.Orientation; import ch.asynk.gdx.boardgame.Positionable; @@ -44,6 +45,13 @@ public class Piece implements Drawable, Positionable, Rotable, Scalable setRotation(r); } + public void dropOnBoard(Board board, Vector2 v) + { + board.toBoard(getCX(), getCY(), v); + board.centerOf((int)v.x, (int)v.y, v); + centerOn(v.x, v.y); + } + public boolean isOn(Tile tile) { return ( diff --git a/core/src/ch/asynk/gdx/boardgame/Positionable.java b/core/src/ch/asynk/gdx/boardgame/Positionable.java index 53d92f8..36a9cda 100644 --- a/core/src/ch/asynk/gdx/boardgame/Positionable.java +++ b/core/src/ch/asynk/gdx/boardgame/Positionable.java @@ -8,6 +8,8 @@ public interface Positionable public float getHeight(); public void translate(float dx, float dy); public void setPosition(float x, float y); + default public float getCX() { return getX() + (getWidth() / 2f); } + default public float getCY() { return getY() + (getHeight() / 2f); } default public void centerOn(float cx, float cy) { diff --git a/core/src/ch/asynk/gdx/boardgame/boards/Board.java b/core/src/ch/asynk/gdx/boardgame/boards/Board.java index 6cabcee..7c72c25 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/Board.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/Board.java @@ -2,6 +2,7 @@ package ch.asynk.gdx.boardgame.boards; import com.badlogic.gdx.math.Vector2; +import ch.asynk.gdx.boardgame.Piece; import ch.asynk.gdx.boardgame.Tile; import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileProvider; import ch.asynk.gdx.boardgame.tilestorages.TileStorage.TileKeyGenerator; @@ -25,4 +26,11 @@ public interface Board extends TileKeyGenerator TCHEBYCHEV } public float distance(int x0, int y0, int x1, int y1, Geometry geometry); + + default public void dropInPlace(Piece piece, Vector2 v) + { + toBoard(piece.getCX(), piece.getCY(), v); + centerOf((int)v.x, (int)v.y, v); + piece.centerOn(v.x, v.y); + } } diff --git a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java index 9b7c4bc..c90d351 100644 --- a/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java +++ b/core/src/ch/asynk/gdx/boardgame/boards/TriangleBoard.java @@ -19,7 +19,6 @@ public class TriangleBoard implements Board private final float m; // h / d private final float h13; // 1/3 height of the triangle private final float h23; // 2/3 height of the triangle - private final float h43; // 4/3 height of the triangle // [0] is 0° facing East // [8] is default @@ -42,12 +41,11 @@ public class TriangleBoard implements Board this.m = this.h / this.d; this.h13 = this.h * 0.33333f; this.h23 = this.h * 0.66666f; - this.h43 = this.h * 1.33333f; this.adjacents = new Tile[3]; } - @Override public int size() { return 0; } // FIXME + @Override public int size() { return cols * rows; } @Override public int[] getAngles() { @@ -60,21 +58,19 @@ public class TriangleBoard implements Board @Override public Tile[] getAdjacents() { return adjacents; } - @Override public void buildAdjacents(int x, int y, TileProvider tileProvider) // FIXME + @Override public void buildAdjacents(int x, int y, TileProvider tileProvider) { + // FIXME } - @Override public int genKey(int x, int y) { return -1; } // FIXME + @Override public int genKey(int x, int y) + { + return (y * cols + x); + } @Override public boolean isOnMap(int x, int y) { - if (this.orientation == BoardFactory.BoardOrientation.VERTICAL) { - if ((y < 0) || (y >= rows)) return false; - if ((x < 0) || (x >= cols)) return false; - } else { - if ((y < 0) || (y >= rows)) return false; - if ((x < -1) || (x >= (cols - 1))) return false; - } + if (x < 0 || x >= cols || y < 0 || y >= rows) return false; return true; } @@ -87,8 +83,8 @@ public class TriangleBoard implements Board cy += (y * this.d); cx += ((x * this.h) + (((x + y) % 2 == 0) ? this.h23 : this.h13)); } else { - cx += (this.d + (x * this.d)); - cy += ((y * this.h) + (((x + y) % 2 == 0) ? this.h13 : this.h23)); + cx += (this.d + ((x -1) * this.d)); + cy += ((y * this.h) + (((x + y) % 2 == 0) ? this.h23 : this.h13)); } v.set(cx, cy); @@ -146,6 +142,7 @@ public class TriangleBoard implements Board col -= 1; } } + col += 1; } v.set(col, row); @@ -160,9 +157,9 @@ public class TriangleBoard implements Board case EUCLIDEAN: return 0; // FIXME case TAXICAB: - return dx + dy; // move should only be allowed through the 3 sides not through de vertices - case TCHEBYCHEV: return dx + dy; + case TCHEBYCHEV: + return (dx > dy ? dx : dy); } return -1; } diff --git a/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java index 4c51780..b3963fb 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/AbstractScreen.java @@ -135,7 +135,7 @@ public abstract class AbstractScreen implements Screen paused = false; } - protected abstract void onTouch(int x, int y); + protected abstract void onTouch(int x, int y, boolean down); protected abstract void onZoom(float dz); protected abstract void onDragged(int dx, int dy); private InputMultiplexer getMultiplexer() @@ -147,19 +147,31 @@ public abstract class AbstractScreen implements Screen onZoom(amount * ZOOM_SCROLL_FACTOR); return true; } + + @Override public boolean touchUp(int x, int y, int pointer, int button) + { + if (inputBlocked) return true; + if (button == Input.Buttons.LEFT) { + dragPos.set(x, y); + onTouch(x, y, false); + } + return true; + } + @Override public boolean touchDown(int x, int y, int pointer, int button) { if (inputBlocked) return true; if (button == Input.Buttons.LEFT) { dragPos.set(x, y); - onTouch(x, y); + onTouch(x, y, true); } return true; } + @Override public boolean touchDragged(int x, int y, int pointer) { - int dx = (int) (dragPos.x - x); - int dy = (int) (dragPos.y - y); + int dx = (int) (x - dragPos.x); + int dy = (int) (y - dragPos.y); dragPos.set(x, y); onDragged(dx, dy); return true; diff --git a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java index 4013273..fc6fc30 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java @@ -1,7 +1,6 @@ package ch.asynk.gdx.boardgame.test; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.Texture; @@ -223,5 +222,5 @@ public class AnimationsScreen extends AbstractScreen @Override protected void onZoom(float dz) { } @Override protected void onDragged(int dx, int dy) { } - @Override protected void onTouch(int x, int y) { } + @Override protected void onTouch(int x, int y, boolean down) { } } diff --git a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java index 086133d..6d762fa 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/BoardScreen.java @@ -1,7 +1,6 @@ package ch.asynk.gdx.boardgame.test; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.Texture; @@ -29,6 +28,7 @@ public class BoardScreen extends AbstractScreen private final Piece panzer; private final Vector2 pos; private final Vector2 v; + private boolean dragging; public Texture map; public Board board; public TileStorage tileStorage; @@ -40,6 +40,7 @@ public class BoardScreen extends AbstractScreen public MyBoard(final Assets assets) { + this.dragging = false; this.assets = assets; this.pos = new Vector2(); this.v = new Vector2(); @@ -60,38 +61,75 @@ public class BoardScreen extends AbstractScreen public void reset() { - pos.set(0, 0); tilesToDraw.clear(); + v.set(0, 0); + pos.set(0, 0); + handleAdjacents(); board.centerOf(0, 0, v); panzer.centerOn(v.x, v.y); panzer.setRotation(Orientation.DEFAULT.r()); } - public boolean touch(float x, float y) + public boolean touch(float x, float y, boolean down) { board.toBoard(x, y, v); + if (!board.isOnMap((int)v.x, (int)v.y)) + return false; + if (down) { + Tile tile = getTile((int)v.x, (int)v.y); + if (!dragging && panzer.isOn(tile)) { + dragging = true; + clearAdjacents(); + } else { + touchInfo(x, y); + pos.set(v); + handleAdjacents(); + board.centerOf((int)v.x, (int)v.y, v); + panzer.centerOn(v.x, v.y); + panzer.setRotation(Orientation.fromR(panzer.getRotation()).left().r()); + GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + } + } else { + if (dragging) { + touchInfo(x, y); + handleAdjacents(); + panzer.dropOnBoard(board, v); + GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + dragging = false; + } + } + return true; + } + + private void touchInfo(float x, float y) + { GdxBoardTest.debug("BoardScreen", String.format("touchDown [%d;%d] => [%d;%d] => %d", (int)x, (int)y, (int)v.x, (int)v.y, board.genKey((int)v.x, (int)v.y))); float d0 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TCHEBYCHEV); float d1 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.TAXICAB); float d2 = board.distance((int)pos.x, (int)pos.y, (int)v.x, (int)v.y, Board.Geometry.EUCLIDEAN); - if (board.isOnMap((int)v.x, (int)v.y)) { - GdxBoardTest.debug("BoardScreen", String.format(" from [%d;%d] => %d :: %d :: %f", (int)pos.x, (int)pos.y, (int)d0, (int)d1, d2)); - pos.set(v); - handleAdjacents(); - board.centerOf((int)v.x, (int)v.y, v); - panzer.centerOn(v.x, v.y); - panzer.setRotation(Orientation.fromR(panzer.getRotation()).left().r()); - GdxBoardTest.debug("BoardScreen", String.format(" => [%d;%d]", (int)v.x, (int)v.y)); + GdxBoardTest.debug("BoardScreen", String.format(" from [%d;%d] => %d :: %d :: %f", (int)pos.x, (int)pos.y, (int)d0, (int)d1, d2)); + } + + public boolean drag(float dx, float dy) + { + if (dragging) { + panzer.translate(dx, dy); + return true; } - return true; + return false; } - private void handleAdjacents() + private void clearAdjacents() { for (Tile tile : board.getAdjacents()) { if (tile != null) tile.enableOverlay(12, false); } + } + + private void handleAdjacents() + { + clearAdjacents(); board.buildAdjacents((int)v.x, (int)v.y, this::getTile); for (Tile tile : board.getAdjacents()) { if (tile != null) { @@ -108,6 +146,7 @@ public class BoardScreen extends AbstractScreen private Tile getTile(int x, int y) { + if (!board.isOnMap(x, y)) return null; return tileStorage.getTile(x, y, board::genKey, this::buildTile); } @@ -206,10 +245,12 @@ public class BoardScreen extends AbstractScreen private final MyBoard board; private final Button btn; private final Root root; + private final Vector2 relative; public BoardScreen(final GdxBoardTest app) { super(app, ""); + this.relative = new Vector2(); this.board = new MyBoard(app.assets); this.camera = this.cam = new Camera(10, board.w, board.h, 1.0f, 0.3f, false); this.btn = new Button( @@ -264,17 +305,25 @@ public class BoardScreen extends AbstractScreen @Override protected void onDragged(int dx, int dy) { - cam.translate(dx, dy); + cam.unprojectTranslation(dx, dy, relative); + if (!board.drag(relative.x, relative.y)) { + cam.translate(dx, dy); + } } - @Override protected void onTouch(int x, int y) + @Override protected void onTouch(int x, int y, boolean down) { - cam.unproject(x, y, boardTouch); - cam.unprojectHud(x, y, hudTouch); - if (btn.touch(hudTouch.x, hudTouch.y) != null) { - setState(state.next()); + if (down) { + cam.unproject(x, y, boardTouch); + cam.unprojectHud(x, y, hudTouch); + if (btn.touch(hudTouch.x, hudTouch.y) != null) { + setState(state.next()); + } else { + board.touch(boardTouch.x, boardTouch.y, true); + } } else { - board.touch(boardTouch.x, boardTouch.y); + cam.unproject(x, y, boardTouch); + board.touch(boardTouch.x, boardTouch.y, false); } } diff --git a/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java index b0a312a..fafb9e0 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/MenuScreen.java @@ -69,7 +69,7 @@ public class MenuScreen extends AbstractScreen @Override protected void onZoom(float dz) { } @Override protected void onDragged(int dx, int dy) { } - @Override protected void onTouch(int x, int y) + @Override protected void onTouch(int x, int y, boolean down) { hudTouch.set(x, y, 0); camera.unproject(hudTouch); diff --git a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java index b490b05..4120fac 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/UiScreen.java @@ -200,7 +200,7 @@ public class UiScreen extends AbstractScreen } @Override protected void onZoom(float dz) { } - @Override protected void onTouch(int x, int y) + @Override protected void onTouch(int x, int y, boolean down) { hudTouch.set(x, y, 0); camera.unproject(hudTouch); |