summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/ch/asynk/tankontank')
-rw-r--r--core/src/ch/asynk/tankontank/game/Hex.java6
-rw-r--r--core/src/ch/asynk/tankontank/game/HexMapFactory.java15
-rw-r--r--core/src/ch/asynk/tankontank/game/HexMapImage.java52
3 files changed, 38 insertions, 35 deletions
diff --git a/core/src/ch/asynk/tankontank/game/Hex.java b/core/src/ch/asynk/tankontank/game/Hex.java
index 2c653e6..b659b45 100644
--- a/core/src/ch/asynk/tankontank/game/Hex.java
+++ b/core/src/ch/asynk/tankontank/game/Hex.java
@@ -2,6 +2,12 @@ package ch.asynk.tankontank.game;
public interface Hex
{
+ public int push(Pawn pawn);
+
+ public void remove(Pawn pawn);
+
+ public Pawn getTop();
+
public enum Orientation
{
KEEP(0),
diff --git a/core/src/ch/asynk/tankontank/game/HexMapFactory.java b/core/src/ch/asynk/tankontank/game/HexMapFactory.java
index a5169e3..ee2aa25 100644
--- a/core/src/ch/asynk/tankontank/game/HexMapFactory.java
+++ b/core/src/ch/asynk/tankontank/game/HexMapFactory.java
@@ -30,13 +30,24 @@ public class HexMapFactory
public static HexMap getMap(AssetManager manager, MapType t)
{
+ HexMap.Config cfg = config();
+
+ Hex[][] board = new Hex[cfg.rows][];
+ for (int i = 0; i < cfg.rows; i++) {
+ int c = cfg.cols;
+ if ((i % 2) == 1) c -= 1;
+ board[i] = new Hex[c];
+ for ( int j = 0; j < c; j ++)
+ board[i][j] = new MapHex(MapHex.Terrain.CLEAR);
+ }
+
HexMap m = null;
switch(t) {
case MAP_A:
- m = new HexMapImage(config(), manager.get("images/map_a.png", Texture.class));
+ m = new HexMapImage(config(), board, manager.get("images/map_a.png", Texture.class));
break;
case MAP_B:
- m = new HexMapImage(config(), manager.get("images/map_b.png", Texture.class));
+ m = new HexMapImage(config(), board, manager.get("images/map_b.png", Texture.class));
break;
}
diff --git a/core/src/ch/asynk/tankontank/game/HexMapImage.java b/core/src/ch/asynk/tankontank/game/HexMapImage.java
index b391f8b..59e35c3 100644
--- a/core/src/ch/asynk/tankontank/game/HexMapImage.java
+++ b/core/src/ch/asynk/tankontank/game/HexMapImage.java
@@ -1,7 +1,5 @@
package ch.asynk.tankontank.game;
-import java.util.ArrayDeque;
-
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
@@ -16,18 +14,14 @@ public class HexMapImage extends Image implements HexMap
private HexMap.Config cfg;
private int cols;
private int rows;
- private ArrayDeque<Pawn>[][] cells;
+ private Hex[][] board;
@SuppressWarnings("unchecked")
- public HexMapImage(HexMap.Config cfg, Texture texture)
+ public HexMapImage(HexMap.Config cfg, Hex[][] board, Texture texture)
{
super(texture);
- cells = new ArrayDeque[cfg.rows][];
- for (int i = 0; i < cfg.rows; i++) {
- if ((i % 2) == 1) cells[i] = new ArrayDeque[cfg.cols - 1];
- else cells[i] = new ArrayDeque[cfg.cols];
- }
this.cfg = cfg;
+ this.board = board;
this.cols = cfg.cols - 1;
this.rows = cfg.rows - 1;
}
@@ -39,10 +33,20 @@ public class HexMapImage extends Image implements HexMap
private Pawn getTopPawnAt(int col, int row)
{
- if ((col < 0) || (row < 0)) return null;
- ArrayDeque<Pawn> st = cells[row][col];
- if ((st == null) || (st.size() == 0)) return null;
- return st.getFirst();
+ // if ((col < 0) || (row < 0)) throw new ();
+ return board[row][col].getTop();
+ }
+
+ private int pushPawnAt(Pawn pawn, int col, int row)
+ {
+ // if ((col < 0) || (row < 0)) throw new ();
+ return board[row][col].push(pawn);
+ }
+
+ private void removePawnFrom(Pawn pawn, int col, int row)
+ {
+ // if ((col < 0) || (row < 0)) throw new ();
+ board[row][col].remove(pawn);
}
public Vector2 getHexCenterAt(GridPoint2 cell)
@@ -66,25 +70,6 @@ public class HexMapImage extends Image implements HexMap
return new Vector2(x, y);
}
- private int pushPawnAt(Pawn pawn, int col, int row)
- {
- ArrayDeque<Pawn> st = cells[row][col];
- if (st == null) st = cells[row][col] = new ArrayDeque<Pawn>();
- st.push(pawn);
- return st.size();
- }
-
- private void removePawnFrom(Pawn pawn, int col, int row)
- {
- if ((col> 0) && (row > 0)) {
- ArrayDeque<Pawn> st = cells[row][col];
- if ((st == null) || (st.size() == 0))
- Gdx.app.error("GameScreen", "remove pawn from " + col + ";" + row + " but pawn stack is empty");
- else
- st.remove(pawn);
- }
- }
-
public void movePawnTo(Pawn pawn, Vector3 coords)
{
GridPoint2 p = getHexAt(null, coords.x, coords.y);
@@ -101,7 +86,8 @@ public class HexMapImage extends Image implements HexMap
public void movePawnTo(final Pawn pawn, final int col, final int row, Hex.Orientation o)
{
GridPoint2 prev = getHexAt(pawn.getLastPosition());
- if (prev != null) removePawnFrom(pawn, prev.x, prev.y);
+ // if (prev == null) throw new ();
+ removePawnFrom(pawn, prev.x, prev.y);
if ((col < 0) || (row < 0)) {
pawn.resetMoves(new Runnable() {