From c09dc7241937b13904207e5ed7a60fcbfd2a82c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 15 Sep 2014 21:49:09 +0200 Subject: actors/Tile -> actors/Pawn --- core/src/ch/asynk/tankontank/actors/HexMap.java | 57 +++++++------- core/src/ch/asynk/tankontank/actors/Pawn.java | 83 +++++++++++++++++++++ core/src/ch/asynk/tankontank/actors/Tile.java | 86 ---------------------- .../ch/asynk/tankontank/screens/GameScreen.java | 17 +++-- 4 files changed, 123 insertions(+), 120 deletions(-) create mode 100644 core/src/ch/asynk/tankontank/actors/Pawn.java delete mode 100644 core/src/ch/asynk/tankontank/actors/Tile.java diff --git a/core/src/ch/asynk/tankontank/actors/HexMap.java b/core/src/ch/asynk/tankontank/actors/HexMap.java index f5ac9ba..722f5c7 100644 --- a/core/src/ch/asynk/tankontank/actors/HexMap.java +++ b/core/src/ch/asynk/tankontank/actors/HexMap.java @@ -24,7 +24,7 @@ public class HexMap extends Image private int cols; private int rows; - private ArrayDeque[][] cells; + private ArrayDeque[][] cells; @SuppressWarnings("unchecked") public HexMap(int cols, int rows, Texture texture) @@ -39,15 +39,15 @@ public class HexMap extends Image this.rows = rows - 1; } - public Tile getTopTileAt(GridPoint2 cell) + public Pawn getTopPawnAt(GridPoint2 cell) { - return getTopTileAt(cell.x, cell.y); + return getTopPawnAt(cell.x, cell.y); } - private Tile getTopTileAt(int col, int row) + private Pawn getTopPawnAt(int col, int row) { if ((col < 0) || (row < 0)) return null; - ArrayDeque st = cells[row][col]; + ArrayDeque st = cells[row][col]; if ((st == null) || (st.size() == 0)) return null; return st.getFirst(); } @@ -60,48 +60,53 @@ public class HexMap extends Image image.setCenterPosition(x, y); } - public Vector2 getTilePosAt(Tile tile, GridPoint3 cell) + // public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 cell) + // { + // return getPawnPosAt(pawn, cell.x, cell.y); + // } + + public Vector2 getPawnPosAt(Pawn pawn, GridPoint3 cell) { - return getTilePosAt(tile, cell.x, cell.y); + return getPawnPosAt(pawn, cell.x, cell.y); } - private Vector2 getTilePosAt(Tile tile, int col, int row) + private Vector2 getPawnPosAt(Pawn pawn, int col, int row) { - float x = x0 + ((col * w) + ((w - tile.getHeight()) / 2)); - float y = y0 + ((row * H) + ((h - tile.getWidth()) / 2)); + float x = x0 + ((col * w) + ((w - pawn.getHeight()) / 2)); + float y = y0 + ((row * H) + ((h - pawn.getWidth()) / 2)); if ((row % 2) == 1) x += dw; return new Vector2(x, y); } - private void removeTileFrom(Tile tile, int col, int row) + private void removePawnFrom(Pawn pawn, int col, int row) { if ((col> 0) && (row > 0)) { - ArrayDeque st = cells[row][col]; + ArrayDeque st = cells[row][col]; if ((st == null) || (st.size() == 0)) - Gdx.app.error("GameScreen", "remove tile from " + col + ";" + row + " but tile stack is empty"); + Gdx.app.error("GameScreen", "remove pawn from " + col + ";" + row + " but pawn stack is empty"); else - st.remove(tile); + st.remove(pawn); } } - public void setTileOn(Tile tile, GridPoint3 cell) + public void setPawnOn(Pawn pawn, GridPoint3 cell) { - setTileOn(tile, cell.x, cell.y, cell.z); + setPawnOn(pawn, cell.x, cell.y, cell.z); } - private void setTileOn(Tile tile, int col, int row, int angle) + private void setPawnOn(Pawn pawn, int col, int row, int angle) { - GridPoint3 prev = tile.cell; - if (prev != null) removeTileFrom(tile, prev.x, prev.y); + GridPoint3 prev = pawn.getHex(); + if (prev != null) removePawnFrom(pawn, prev.x, prev.y); - Vector2 pos = getTilePosAt(tile, col, row); - tile.setPosition(pos.x, pos.y); - tile.setRotation(angle); + Vector2 pos = getPawnPosAt(pawn, col, row); + pawn.setPosition(pos.x, pos.y); + pawn.setRotation(angle); - ArrayDeque st = cells[row][col]; - if (st == null) st = cells[row][col] = new ArrayDeque(); - st.push(tile); - tile.setZIndex(st.size()); + ArrayDeque st = cells[row][col]; + if (st == null) st = cells[row][col] = new ArrayDeque(); + st.push(pawn); + pawn.setZIndex(st.size()); } public GridPoint2 getCellAt(GridPoint2 cell, float cx, float cy) diff --git a/core/src/ch/asynk/tankontank/actors/Pawn.java b/core/src/ch/asynk/tankontank/actors/Pawn.java new file mode 100644 index 0000000..b33e049 --- /dev/null +++ b/core/src/ch/asynk/tankontank/actors/Pawn.java @@ -0,0 +1,83 @@ +package ch.asynk.tankontank.actors; + +import java.util.ArrayDeque; + +import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.badlogic.gdx.scenes.scene2d.ui.Image; +import com.badlogic.gdx.scenes.scene2d.actions.Actions; +import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; + +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.GridPoint2; +import com.badlogic.gdx.math.GridPoint3; + +public class Pawn extends Image +{ + public static final int DRAGGED_Z_INDEX = 10; + private static final float MOVE_TIME = 0.3f; + + private HexMap map; + private ArrayDeque path = new ArrayDeque(); + + public Pawn(TextureRegion region, HexMap map) + { + super(region); + this.map = map; + setOrigin((getWidth() / 2.f), (getHeight() / 2.f)); + } + + public GridPoint3 getHex() + { + if (path.size() == 0) return null; + return path.getFirst(); + } + + public void moveTo(GridPoint2 hex) + { + moveTo(new GridPoint3(hex.x, hex.y, (int) getRotation())); + } + + public void moveTo(int col, int row, int angle) + { + moveTo(new GridPoint3(col, row, angle)); + } + + private void moveTo(GridPoint3 hex) + { + if ((hex.x == -1) || (hex.y == -1)) { + resetMoves(); + } else { + map.setPawnOn(this, hex); + path.push(hex); + } + } + + public void resetMoves() + { + final Pawn self = this; + final GridPoint3 finalHex = path.getLast(); + + SequenceAction seq = new SequenceAction(); + while(path.size() != 0) { + Vector2 v = map.getPawnPosAt(this, path.pop()); + seq.addAction(Actions.moveTo(v.x, v.y, MOVE_TIME)); + } + + seq.addAction( Actions.run(new Runnable() { + @Override + public void run() { + map.setPawnOn(self, finalHex); + path.push(finalHex); + } + })); + + addAction(seq); + } + + public void moveDone() + { + GridPoint3 hex = path.pop(); + path.clear(); + path.push(hex); + } +} diff --git a/core/src/ch/asynk/tankontank/actors/Tile.java b/core/src/ch/asynk/tankontank/actors/Tile.java deleted file mode 100644 index dbc2da0..0000000 --- a/core/src/ch/asynk/tankontank/actors/Tile.java +++ /dev/null @@ -1,86 +0,0 @@ -package ch.asynk.tankontank.actors; - -import java.util.ArrayDeque; - -import com.badlogic.gdx.graphics.g2d.TextureRegion; -import com.badlogic.gdx.scenes.scene2d.ui.Image; -import com.badlogic.gdx.scenes.scene2d.actions.Actions; -import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; - -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.math.GridPoint3; - -public class Tile extends Image -{ - public static final int DRAGGED_Z_INDEX = 10; - private static final float MOVE_TIME = 0.3f; - - public GridPoint3 cell = new GridPoint3(-1, -1, 0); - - private HexMap map; - private ArrayDeque path = new ArrayDeque(); - - public Tile(TextureRegion region, HexMap map) - { - super(region); - this.map = map; - setOrigin((getWidth() / 2.f), (getHeight() / 2.f)); - } - - public void setRotation(int angle) - { - super.setRotation(angle); - cell.z = angle; - } - - public void moveTo(int col, int row) - { - moveTo(new GridPoint3(col, row, cell.z)); - } - - public void moveTo(int col, int row, int angle) - { - moveTo(new GridPoint3(col, row, angle)); - } - - private void moveTo(GridPoint3 nextCell) - { - if ((nextCell.x == -1) || (nextCell.y == -1)) { - resetMoves(); - } else { - map.setTileOn(this, nextCell); - path.push(nextCell); - cell = nextCell; - } - } - - public void resetMoves() - { - final Tile self = this; - final GridPoint3 finalPos = path.getLast(); - - SequenceAction seq = new SequenceAction(); - while(path.size() != 0) { - Vector2 v = map.getTilePosAt(this, path.pop()); - seq.addAction(Actions.moveTo(v.x, v.y, MOVE_TIME)); - } - - seq.addAction( Actions.run(new Runnable() { - @Override - public void run() { - map.setTileOn(self, finalPos); - path.push(finalPos); - cell = finalPos; - } - })); - - addAction(seq); - } - - public void done() - { - GridPoint3 p = path.pop(); - path.clear(); - path.push(p); - } -} diff --git a/core/src/ch/asynk/tankontank/screens/GameScreen.java b/core/src/ch/asynk/tankontank/screens/GameScreen.java index c4d06be..8f05039 100644 --- a/core/src/ch/asynk/tankontank/screens/GameScreen.java +++ b/core/src/ch/asynk/tankontank/screens/GameScreen.java @@ -25,7 +25,7 @@ import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.ScreenViewport; import ch.asynk.tankontank.TankOnTank; -import ch.asynk.tankontank.actors.Tile; +import ch.asynk.tankontank.actors.Pawn; import ch.asynk.tankontank.actors.HexMap; public class GameScreen extends AbstractScreen @@ -49,7 +49,7 @@ public class GameScreen extends AbstractScreen private Vector3 touchPos = new Vector3(); // world coordinates private Vector2 dragPos = new Vector2(); // screen coordinates - private Tile draggedTile = null; + private Pawn draggedPawn = null; private GridPoint2 cell = new GridPoint2(-1, -1); // current map cell public GameScreen(final TankOnTank game) @@ -103,14 +103,15 @@ public class GameScreen extends AbstractScreen float deltaX = ((x - dragPos.x) * cam.zoom * screenToViewport.x); float deltaY = ((dragPos.y - y) * cam.zoom * screenToViewport.y); dragPos.set(x, y); - if (draggedTile == null) { + if (draggedPawn == null) { cam.translate(-deltaX, -deltaY, 0); clampCameraPos(); } else { - draggedTile.moveBy(deltaX, deltaY); + draggedPawn.moveBy(deltaX, deltaY); cam.unproject(touchPos.set(x, y, 0)); map.getCellAt(cell, touchPos.x, touchPos.y); map.setImageCenterAt(selectedHex, cell); + } return true; } @@ -121,8 +122,8 @@ public class GameScreen extends AbstractScreen dragPos.set(x, y); cam.unproject(touchPos.set(x, y, 0)); map.getCellAt(cell, touchPos.x, touchPos.y); - draggedTile = map.getTopTileAt(cell); - if (draggedTile != null) draggedTile.setZIndex(Tile.DRAGGED_Z_INDEX); + draggedPawn = map.getTopPawnAt(cell); + if (draggedPawn != null) draggedPawn.setZIndex(Pawn.DRAGGED_Z_INDEX); map.setImageCenterAt(selectedHex, cell); selectedHex.setVisible(true); } @@ -133,9 +134,9 @@ public class GameScreen extends AbstractScreen { if (button == Input.Buttons.LEFT) { cam.unproject(touchPos.set(x, y, 0)); - if (draggedTile != null) { + if (draggedPawn != null) { map.getCellAt(cell, touchPos.x, touchPos.y); - draggedTile.moveTo(cell.x, cell.y); + draggedPawn.moveTo(cell); } selectedHex.setVisible(false); } -- cgit v1.1-2-g2b99