summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2014-09-16 17:14:25 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2014-09-16 17:14:25 +0200
commitaed778eefd7dfeb5a42920c36b4101c769383f8c (patch)
tree0aefc6948e5829f685710c9cb844db1943261188 /core
parentf61f47a2f403d39531482c036726715958b16901 (diff)
downloadRustAndDust-aed778eefd7dfeb5a42920c36b4101c769383f8c.zip
RustAndDust-aed778eefd7dfeb5a42920c36b4101c769383f8c.tar.gz
refactor
- PawnImage have no more reference to HexMap - HexMapImage is in charge of moving Pawns
Diffstat (limited to 'core')
-rw-r--r--core/src/ch/asynk/tankontank/game/HexMap.java16
-rw-r--r--core/src/ch/asynk/tankontank/game/HexMapImage.java66
-rw-r--r--core/src/ch/asynk/tankontank/game/Pawn.java27
-rw-r--r--core/src/ch/asynk/tankontank/game/PawnImage.java54
-rw-r--r--core/src/ch/asynk/tankontank/game/Unit.java8
-rw-r--r--core/src/ch/asynk/tankontank/game/UnitFactory.java34
-rw-r--r--core/src/ch/asynk/tankontank/screens/GameScreen.java49
7 files changed, 129 insertions, 125 deletions
diff --git a/core/src/ch/asynk/tankontank/game/HexMap.java b/core/src/ch/asynk/tankontank/game/HexMap.java
index e1f3d7b..faf9881 100644
--- a/core/src/ch/asynk/tankontank/game/HexMap.java
+++ b/core/src/ch/asynk/tankontank/game/HexMap.java
@@ -1,26 +1,28 @@
package ch.asynk.tankontank.game;
import com.badlogic.gdx.math.Vector2;
+import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.GridPoint2;
-import com.badlogic.gdx.math.GridPoint3;
public interface HexMap
{
- // Gfx related
+ // libgdx
+ public float getWidth();
public float getHeight();
- public float getWidth();
+ // game
- // Map related
+ public GridPoint2 getHexAt(GridPoint2 hex, float x, float y);
public Pawn getTopPawnAt(GridPoint2 hex);
public Vector2 getHexCenterAt(GridPoint2 hex);
- public Vector2 getPawnPosAt(Pawn pawn, GridPoint3 hex);
+ public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 hex);
- public void setPawnAt(Pawn pawn, GridPoint3 hex);
+ public void movePawnTo(Pawn pawn, Vector3 coords);
+
+ public void movePawnTo(Pawn pawn, int col, int row, HexOrientation o);
- public GridPoint2 getHexAt(GridPoint2 hex, float x, float y);
}
diff --git a/core/src/ch/asynk/tankontank/game/HexMapImage.java b/core/src/ch/asynk/tankontank/game/HexMapImage.java
index 60e218f..3a728c8 100644
--- a/core/src/ch/asynk/tankontank/game/HexMapImage.java
+++ b/core/src/ch/asynk/tankontank/game/HexMapImage.java
@@ -8,8 +8,8 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.math.Vector2;
+import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.GridPoint2;
-import com.badlogic.gdx.math.GridPoint3;
public class HexMapImage extends Image implements HexMap
{
@@ -60,12 +60,7 @@ public class HexMapImage extends Image implements HexMap
return new Vector2(x, y);
}
- // public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 cell)
- // {
- // return getPawnPosAt(pawn, cell.x, cell.y);
- // }
-
- public Vector2 getPawnPosAt(Pawn pawn, GridPoint3 cell)
+ public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 cell)
{
return getPawnPosAt(pawn, cell.x, cell.y);
}
@@ -78,6 +73,15 @@ 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);
+ System.out.println("pushed at: "+col+" " +row);
+ return st.size();
+ }
+
private void removePawnFrom(Pawn pawn, int col, int row)
{
if ((col> 0) && (row > 0)) {
@@ -86,31 +90,47 @@ public class HexMapImage extends Image implements HexMap
Gdx.app.error("GameScreen", "remove pawn from " + col + ";" + row + " but pawn stack is empty");
else
st.remove(pawn);
+ System.out.println("poped from: "+col+" " +row);
}
}
- public void setPawnAt(Pawn pawn, GridPoint3 cell)
+ public void movePawnTo(Pawn pawn, Vector3 coords)
{
- setPawnAt(pawn, cell.x, cell.y, cell.z);
+ GridPoint2 p = getHexAt(null, coords.x, coords.y);
+ movePawnTo(pawn, p.x, p.y, HexOrientation.KEEP);
}
- private void setPawnAt(Pawn pawn, int col, int row, int angle)
+ public void movePawnTo(final Pawn pawn, final int col, final int row, HexOrientation o)
{
- GridPoint3 prev = pawn.getBoardPosition();
+ GridPoint2 prev = getHexAt(pawn.getLastPosition());
if (prev != null) removePawnFrom(pawn, prev.x, prev.y);
- Vector2 pos = getPawnPosAt(pawn, col, row);
- pawn.setPosition(pos.x, pos.y);
- pawn.setRotation(angle);
+ if ((col < 0) || (row < 0)) {
+ pawn.resetMoves(new Runnable() {
+ @Override
+ public void run() {
+ GridPoint2 hex = getHexAt(pawn.getLastPosition());
+ pawn.setZIndex(pushPawnAt(pawn, hex.x, hex.y));
+ }
+ });
+ return;
+ } else {
+ int z = pushPawnAt(pawn, col, row);
+ Vector2 pos = getPawnPosAt(pawn, col, row);
+ pawn.pushMove(pos.x, pos.y, z, o);
+ }
+ }
- ArrayDeque<Pawn> st = cells[row][col];
- if (st == null) st = cells[row][col] = new ArrayDeque<Pawn>();
- st.push(pawn);
- pawn.setZIndex(st.size());
+ private GridPoint2 getHexAt(Vector3 v)
+ {
+ if (v == null) return null;
+ return getHexAt(null, v.x, v.y);
}
- public GridPoint2 getHexAt(GridPoint2 cell, float cx, float cy)
+ public GridPoint2 getHexAt(GridPoint2 hex, float cx, float cy)
{
+ if (hex == null) hex = new GridPoint2();
+
// compute row
int row;
boolean oddRow = true;
@@ -152,12 +172,12 @@ public class HexMapImage extends Image implements HexMap
}
}
- // validate cell
+ // validate hex
if ((col < 0) || (row < 0) || (row > rows) || (col > cols) || (oddRow && ((col +1)> cols)))
- cell.set(-1, -1);
+ hex.set(-1, -1);
else
- cell.set(col, row);
+ hex.set(col, row);
- return cell;
+ return hex;
}
}
diff --git a/core/src/ch/asynk/tankontank/game/Pawn.java b/core/src/ch/asynk/tankontank/game/Pawn.java
index 519a56e..b360eaa 100644
--- a/core/src/ch/asynk/tankontank/game/Pawn.java
+++ b/core/src/ch/asynk/tankontank/game/Pawn.java
@@ -1,33 +1,24 @@
package ch.asynk.tankontank.game;
-import com.badlogic.gdx.math.GridPoint2;
-import com.badlogic.gdx.math.GridPoint3;
+import com.badlogic.gdx.math.Vector3;
public interface Pawn
{
- // Gfx related
-
- public float getHeight();
+ // libgdx
public float getWidth();
+ public float getHeight();
+ public void setZIndex(int z);
- public void moveBy(float x, float y);
-
- public void setPosition(float x, float y);
-
- public void setRotation(float angle);
-
- public void setZIndex(int zIndex);
-
- // Board related
+ // game
- public GridPoint3 getBoardPosition();
+ public Vector3 getLastPosition();
- public void moveTo(GridPoint2 hex);
+ public void moveBy(float x, float y);
- public void moveTo(int col, int row, int angle);
+ public void pushMove(float x, float y, int z, HexOrientation o);
- public void resetMoves();
+ public void resetMoves(Runnable cb);
public void moveDone();
}
diff --git a/core/src/ch/asynk/tankontank/game/PawnImage.java b/core/src/ch/asynk/tankontank/game/PawnImage.java
index 0f7a77d..c4b7581 100644
--- a/core/src/ch/asynk/tankontank/game/PawnImage.java
+++ b/core/src/ch/asynk/tankontank/game/PawnImage.java
@@ -7,76 +7,64 @@ 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;
+import com.badlogic.gdx.math.Vector3;
public class PawnImage extends Image implements Pawn
{
private static final float MOVE_TIME = 0.3f;
+ private static final float ROTATE_TIME = 0.2f;
- private HexMap map;
- private ArrayDeque<GridPoint3> path = new ArrayDeque<GridPoint3>();
+ private ArrayDeque<Vector3> path = new ArrayDeque<Vector3>();
- public PawnImage(TextureRegion region, HexMap map)
+ public PawnImage(TextureRegion region)
{
super(region);
- this.map = map;
setOrigin((getWidth() / 2.f), (getHeight() / 2.f));
}
- public GridPoint3 getBoardPosition()
+ public Vector3 getLastPosition()
{
- if (path.size() == 0) return null;
+ if ((path == null) || (path.size() == 0)) return null;
return path.getFirst();
}
- public void moveTo(GridPoint2 hex)
+ public void pushMove(float x, float y, int z, HexOrientation r)
{
- moveTo(new GridPoint3(hex.x, hex.y, (int) getRotation()));
+ setPosition(x, y);
+ if (r != HexOrientation.KEEP) setRotation(r.v);
+ setZIndex(z);
+ path.push(new Vector3(x, y, r.v));
}
- 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.setPawnAt(this, hex);
- path.push(hex);
- }
- }
-
- public void resetMoves()
+ public void resetMoves(Runnable cb)
{
final Pawn self = this;
- final GridPoint3 finalHex = path.getLast();
+ final Vector3 finalPos = path.getLast();
SequenceAction seq = new SequenceAction();
while(path.size() != 0) {
- Vector2 v = map.getPawnPosAt(this, path.pop());
+ Vector3 v = path.pop();
seq.addAction(Actions.moveTo(v.x, v.y, MOVE_TIME));
+ seq.addAction(Actions.rotateTo(v.z, ROTATE_TIME));
}
seq.addAction( Actions.run(new Runnable() {
@Override
public void run() {
- map.setPawnAt(self, finalHex);
- path.push(finalHex);
+ path.push(finalPos);
}
}));
+ // map set z index and push me in hex stack
+ seq.addAction(Actions.run(cb));
+
addAction(seq);
}
public void moveDone()
{
- GridPoint3 hex = path.pop();
+ Vector3 v = path.pop();
path.clear();
- path.push(hex);
+ path.push(v);
}
}
diff --git a/core/src/ch/asynk/tankontank/game/Unit.java b/core/src/ch/asynk/tankontank/game/Unit.java
index 6606c14..965ed47 100644
--- a/core/src/ch/asynk/tankontank/game/Unit.java
+++ b/core/src/ch/asynk/tankontank/game/Unit.java
@@ -13,9 +13,9 @@ public class Unit extends PawnImage
public Army army;
// hard tager
- public Unit(Army army, boolean hq, int range, int defense, int movementPoints, TextureRegion region, HexMap map)
+ public Unit(Army army, boolean hq, int range, int defense, int movementPoints, TextureRegion region)
{
- super(region, map);
+ super(region);
this.army = army;
this.hq = hq;
this.rng = range;
@@ -25,9 +25,9 @@ public class Unit extends PawnImage
}
// soft tager
- public Unit(Army army, boolean hq, int range, int defense, int concealedDefense, int movementPoints, TextureRegion region, HexMap map)
+ public Unit(Army army, boolean hq, int range, int defense, int concealedDefense, int movementPoints, TextureRegion region)
{
- super(region, map);
+ super(region);
this.army = army;
this.hq = hq;
this.rng = range;
diff --git a/core/src/ch/asynk/tankontank/game/UnitFactory.java b/core/src/ch/asynk/tankontank/game/UnitFactory.java
index 0ce2a6e..c2156ce 100644
--- a/core/src/ch/asynk/tankontank/game/UnitFactory.java
+++ b/core/src/ch/asynk/tankontank/game/UnitFactory.java
@@ -26,13 +26,11 @@ public class UnitFactory
US_WOLVERINE
}
- private static HexMap map;
private static TextureAtlas usAtlas;
private static TextureAtlas geAtlas;
- public static void init(AssetManager manager, HexMap m)
+ public static void init(AssetManager manager)
{
- map = m;
usAtlas = manager.get("images/us.pack", TextureAtlas.class);
geAtlas = manager.get("images/ge.pack", TextureAtlas.class);
}
@@ -48,49 +46,49 @@ public class UnitFactory
Unit u = null;
switch(t) {
case GE_AT_GUN:
- u = new Unit(Army.GE, false, 3, 8, 9, 1, geAtlas.findRegion("at-gun"), map);
+ u = new Unit(Army.GE, false, 3, 8, 9, 1, geAtlas.findRegion("at-gun"));
break;
case GE_INFANTRY:
- u = new Unit(Army.GE, false, 1, 7, 10, 1, geAtlas.findRegion("infantry"), map);
+ u = new Unit(Army.GE, false, 1, 7, 10, 1, geAtlas.findRegion("infantry"));
break;
case GE_KINGTIGER:
- u = new Unit(Army.GE, false, 3, 12, 1, geAtlas.findRegion("kingtiger"), map);
+ u = new Unit(Army.GE, false, 3, 12, 1, geAtlas.findRegion("kingtiger"));
break;
case GE_PANZER_IV:
- u = new Unit(Army.GE, false, 2, 9, 2, geAtlas.findRegion("panzer-iv"), map);
+ u = new Unit(Army.GE, false, 2, 9, 2, geAtlas.findRegion("panzer-iv"));
break;
case GE_PANZER_IV_HQ:
- u = new Unit(Army.GE, true, 2, 9, 2, geAtlas.findRegion("panzer-iv-hq"), map);
+ u = new Unit(Army.GE, true, 2, 9, 2, geAtlas.findRegion("panzer-iv-hq"));
break;
case GE_TIGER:
- u = new Unit(Army.GE, false, 3, 11, 1, geAtlas.findRegion("tiger"), map);
+ u = new Unit(Army.GE, false, 3, 11, 1, geAtlas.findRegion("tiger"));
break;
case GE_WESPE:
- u = new Unit(Army.GE, false, 5, 8, 1, geAtlas.findRegion("wespe"), map);
+ u = new Unit(Army.GE, false, 5, 8, 1, geAtlas.findRegion("wespe"));
break;
case US_AT_GUN:
- u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("at-gun"), map);
+ u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("at-gun"));
break;
case US_INFANTRY:
- u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("infantry"), map);
+ u = new Unit(Army.US, false, 1, 7, 10, 1, usAtlas.findRegion("infantry"));
break;
case US_PERSHING:
- u = new Unit(Army.US, false, 3, 10, 2, usAtlas.findRegion("pershing"), map);
+ u = new Unit(Army.US, false, 3, 10, 2, usAtlas.findRegion("pershing"));
break;
case US_PERSHING_HQ:
- u = new Unit(Army.US, true, 3, 10, 2, usAtlas.findRegion("pershing-hq"), map);
+ u = new Unit(Army.US, true, 3, 10, 2, usAtlas.findRegion("pershing-hq"));
break;
case US_PRIEST:
- u = new Unit(Army.US, false, 5, 8, 1, usAtlas.findRegion("priest"), map);
+ u = new Unit(Army.US, false, 5, 8, 1, usAtlas.findRegion("priest"));
break;
case US_SHERMAN:
- u = new Unit(Army.US, false, 2, 9, 2, usAtlas.findRegion("sherman"), map);
+ u = new Unit(Army.US, false, 2, 9, 2, usAtlas.findRegion("sherman"));
break;
case US_SHERMAN_HQ:
- u = new Unit(Army.US, true, 2, 9, 2, usAtlas.findRegion("sherman-hq"), map);
+ u = new Unit(Army.US, true, 2, 9, 2, usAtlas.findRegion("sherman-hq"));
break;
case US_WOLVERINE:
- u = new Unit(Army.US, false, 3, 8, 3, usAtlas.findRegion("wolverine"), map);
+ u = new Unit(Army.US, false, 3, 8, 3, usAtlas.findRegion("wolverine"));
break;
}
diff --git a/core/src/ch/asynk/tankontank/screens/GameScreen.java b/core/src/ch/asynk/tankontank/screens/GameScreen.java
index 4c0b4ef..0c4ebff 100644
--- a/core/src/ch/asynk/tankontank/screens/GameScreen.java
+++ b/core/src/ch/asynk/tankontank/screens/GameScreen.java
@@ -27,6 +27,7 @@ import com.badlogic.gdx.utils.viewport.ScreenViewport;
import ch.asynk.tankontank.TankOnTank;
import ch.asynk.tankontank.game.Pawn;
import ch.asynk.tankontank.game.HexMap;
+import ch.asynk.tankontank.game.HexOrientation;
import ch.asynk.tankontank.game.HexMapImage;
import ch.asynk.tankontank.game.HexMapFactory;
import ch.asynk.tankontank.game.Unit;
@@ -76,23 +77,26 @@ public class GameScreen extends AbstractScreen
gameStage.addActor((HexMapImage) map);
gameStage.addActor(selectedHex);
- UnitFactory.init(game.manager, map);
- addUnit(gameStage, UnitType.GE_AT_GUN, 1, 4, 0);
- addUnit(gameStage, UnitType.GE_INFANTRY, 2, 4, 0);
- addUnit(gameStage, UnitType.GE_KINGTIGER, 3, 4, 0);
- addUnit(gameStage, UnitType.GE_PANZER_IV, 4, 4, 0);
- addUnit(gameStage, UnitType.GE_PANZER_IV_HQ, 5, 4, 0);
- addUnit(gameStage, UnitType.GE_TIGER, 6, 4, 0);
- addUnit(gameStage, UnitType.GE_WESPE, 7, 4, 0);
-
- addUnit(gameStage, UnitType.US_AT_GUN, 1, 3, 0);
- addUnit(gameStage, UnitType.US_INFANTRY, 2, 3, 0);
- addUnit(gameStage, UnitType.US_PERSHING, 3, 3, 0);
- addUnit(gameStage, UnitType.US_PERSHING_HQ, 4, 3, 0);
- addUnit(gameStage, UnitType.US_PRIEST, 5, 3, 0);
- addUnit(gameStage, UnitType.US_SHERMAN, 6, 3, 0);
- addUnit(gameStage, UnitType.US_SHERMAN_HQ, 7, 3, 0);
- addUnit(gameStage, UnitType.US_WOLVERINE, 8, 3, 0);
+ UnitFactory.init(game.manager);
+
+ HexOrientation o = HexOrientation.SOUTH_EAST;
+ addUnit(gameStage, UnitType.GE_AT_GUN, 1, 4, o);
+ addUnit(gameStage, UnitType.GE_INFANTRY, 2, 4, o);
+ addUnit(gameStage, UnitType.GE_KINGTIGER, 3, 4, o);
+ addUnit(gameStage, UnitType.GE_PANZER_IV, 4, 4, o);
+ addUnit(gameStage, UnitType.GE_PANZER_IV_HQ, 5, 4, o);
+ addUnit(gameStage, UnitType.GE_TIGER, 6, 4, o);
+ addUnit(gameStage, UnitType.GE_WESPE, 7, 4, o);
+
+ o = HexOrientation.NORTH_WEST;
+ addUnit(gameStage, UnitType.US_AT_GUN, 1, 3, o);
+ addUnit(gameStage, UnitType.US_INFANTRY, 2, 3, o);
+ addUnit(gameStage, UnitType.US_PERSHING, 3, 3, o);
+ addUnit(gameStage, UnitType.US_PERSHING_HQ, 4, 3, o);
+ addUnit(gameStage, UnitType.US_PRIEST, 5, 3, o);
+ addUnit(gameStage, UnitType.US_SHERMAN, 6, 3, o);
+ addUnit(gameStage, UnitType.US_SHERMAN_HQ, 7, 3, o);
+ addUnit(gameStage, UnitType.US_WOLVERINE, 8, 3, o);
hud = new Stage(new ScreenViewport());
hud.addActor(fps);
@@ -100,10 +104,10 @@ public class GameScreen extends AbstractScreen
Gdx.input.setInputProcessor(getMultiplexer());
}
- private void addUnit(Stage stage, UnitType t, int col, int row, int angle)
+ private void addUnit(Stage stage, UnitType t, int col, int row, HexOrientation o)
{
Unit u = UnitFactory.getUnit(t);
- u.moveTo(col, row, angle);
+ map.movePawnTo((Pawn) u, col, row, o);
stage.addActor(u);
}
@@ -150,7 +154,9 @@ public class GameScreen extends AbstractScreen
cam.unproject(touchPos.set(x, y, 0));
map.getHexAt(cell, touchPos.x, touchPos.y);
draggedPawn = map.getTopPawnAt(cell);
- if (draggedPawn != null) draggedPawn.setZIndex(DRAGGED_Z_INDEX);
+ if (draggedPawn != null) {
+ draggedPawn.setZIndex(DRAGGED_Z_INDEX);
+ }
Vector2 v = map.getHexCenterAt(cell);
selectedHex.setCenterPosition(v.x, v.y);
selectedHex.setVisible(true);
@@ -163,8 +169,7 @@ public class GameScreen extends AbstractScreen
if (button == Input.Buttons.LEFT) {
cam.unproject(touchPos.set(x, y, 0));
if (draggedPawn != null) {
- map.getHexAt(cell, touchPos.x, touchPos.y);
- draggedPawn.moveTo(cell);
+ map.movePawnTo(draggedPawn, touchPos);
}
selectedHex.setVisible(false);
}