From 8d6f8738203d45d4da116e390b6e6685f4636bb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 17 Nov 2014 14:52:51 +0100 Subject: Player: clean up API --- core/src/ch/asynk/tankontank/engine/Board.java | 11 +++--- core/src/ch/asynk/tankontank/engine/Player.java | 46 +++++++++------------- core/src/ch/asynk/tankontank/game/Map.java | 6 +-- .../game/battles/BattleFrontalAssault.java | 2 +- .../ch/asynk/tankontank/game/hud/PlayerInfo.java | 4 +- .../src/ch/asynk/tankontank/game/hud/UnitDock.java | 4 +- .../asynk/tankontank/game/states/StateEngage.java | 4 +- .../asynk/tankontank/game/states/StateSelect.java | 4 +- 8 files changed, 35 insertions(+), 46 deletions(-) diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java index 203436c..a7ebabf 100644 --- a/core/src/ch/asynk/tankontank/engine/Board.java +++ b/core/src/ch/asynk/tankontank/engine/Board.java @@ -1,5 +1,6 @@ package ch.asynk.tankontank.engine; +import java.util.List; import java.util.Iterator; import java.util.Collection; import java.util.ArrayList; @@ -302,11 +303,10 @@ public abstract class Board implements Disposable return searchBoard.possibleTargetsFrom(pawn, targets); } - protected int collectPossibleTargets(Pawn pawn, Iterator units, PawnCollection targets) + protected int collectPossibleTargets(Pawn pawn, List units, PawnCollection targets) { targets.clear(); - while (units.hasNext()) { - Pawn target = units.next(); + for (Pawn target : units) { if (pawn.canEngage(target) && searchBoard.collectAttacks(pawn, target, true)) targets.add(target); } @@ -332,11 +332,10 @@ public abstract class Board implements Disposable return assists.size(); } - protected int collectAttackAssists(Pawn pawn, Pawn target, Iterator units, PawnCollection assists) + protected int collectAttackAssists(Pawn pawn, Pawn target, List units, PawnCollection assists) { assists.clear(); - while (units.hasNext()) { - Pawn p = units.next(); + for (Pawn p : units) { if ((p != pawn) && p.canEngage(target) && searchBoard.collectAttacks(p, target, !p.canAssistEngagementWithoutLos())) assists.add(p); } diff --git a/core/src/ch/asynk/tankontank/engine/Player.java b/core/src/ch/asynk/tankontank/engine/Player.java index cb9543d..8bda179 100644 --- a/core/src/ch/asynk/tankontank/engine/Player.java +++ b/core/src/ch/asynk/tankontank/engine/Player.java @@ -9,10 +9,10 @@ import ch.asynk.tankontank.engine.Pawn; public abstract class Player { - protected Faction faction; - protected ArrayList units; - protected ArrayList casualties; - protected ArrayList reinforcement; + public Faction faction; + public ArrayList units; + public ArrayList casualties; + public ArrayList reinforcement; public abstract void turnEnd(); public abstract void turnStart(); @@ -25,44 +25,39 @@ public abstract class Player this.reinforcement = new ArrayList(n); } - public Faction getFaction() - { - return faction; - } - - public List getReinforcement() + public String getName() { - return reinforcement; + return faction.toString(); } - public int unitsLeft() + public boolean is(Faction faction) { - return (units.size() + reinforcement.size()); + return (this.faction == faction); } - public int casualties() + public boolean isEnemy(Pawn pawn) { - return casualties.size(); + return pawn.isEnemy(faction); } - public String getName() + public boolean isEnemy(Faction other) { - return faction.toString(); + return faction.isEnemy(other); } - public boolean is(Faction faction) + public int unitsLeft() { - return (this.faction == faction); + return (units.size() + reinforcement.size()); } - public boolean isEnemy(Pawn pawn) + public int reinforcement() { - return pawn.isEnemy(faction); + return reinforcement.size(); } - public boolean isEnemy(Faction other) + public int casualties() { - return faction.isEnemy(other); + return casualties.size(); } public void addUnit(Pawn pawn) @@ -92,9 +87,4 @@ public abstract class Player units.remove(pawn); casualties.add(pawn); } - - public Iterator unitIterator() - { - return units.iterator(); - } } diff --git a/core/src/ch/asynk/tankontank/game/Map.java b/core/src/ch/asynk/tankontank/game/Map.java index 168d07d..f405845 100644 --- a/core/src/ch/asynk/tankontank/game/Map.java +++ b/core/src/ch/asynk/tankontank/game/Map.java @@ -1,6 +1,6 @@ package ch.asynk.tankontank.game; -import java.util.Iterator; +import java.util.List; import java.util.Random; import com.badlogic.gdx.audio.Sound; @@ -118,7 +118,7 @@ public abstract class Map extends Board return possiblePaths.toggleCtrlTile(hex); } - public int collectPossibleTargets(Pawn pawn, Iterator foes) + public int collectPossibleTargets(Pawn pawn, List foes) { if (!pawn.canEngage()) { possibleTargets.clear(); @@ -140,7 +140,7 @@ public abstract class Map extends Board return moveablePawns.size(); } - public int collectAttackAssists(Pawn pawn, Pawn target, Iterator units) + public int collectAttackAssists(Pawn pawn, Pawn target, List units) { int s = collectAttackAssists(pawn, target, units, engagementAssists); activatedPawns.add(pawn); diff --git a/core/src/ch/asynk/tankontank/game/battles/BattleFrontalAssault.java b/core/src/ch/asynk/tankontank/game/battles/BattleFrontalAssault.java index f31aff4..4571772 100644 --- a/core/src/ch/asynk/tankontank/game/battles/BattleFrontalAssault.java +++ b/core/src/ch/asynk/tankontank/game/battles/BattleFrontalAssault.java @@ -47,7 +47,7 @@ public class BattleFrontalAssault extends BattleCommon @Override public boolean deploymentDone(Player player) { - int n = player.getReinforcement().size(); + int n = player.reinforcement(); if (n == 0) { player.deploymentDone(); return true; diff --git a/core/src/ch/asynk/tankontank/game/hud/PlayerInfo.java b/core/src/ch/asynk/tankontank/game/hud/PlayerInfo.java index 5cd4b5f..4ac8c88 100644 --- a/core/src/ch/asynk/tankontank/game/hud/PlayerInfo.java +++ b/core/src/ch/asynk/tankontank/game/hud/PlayerInfo.java @@ -95,7 +95,7 @@ public class PlayerInfo implements Disposable, Drawable unitDock.hide(); turns.write("" + player.getTurn()); aps.write("" + player.getAp()); - int r = player.getReinforcement().size(); + int r = player.reinforcement(); if (r == 0) { reinforcement.visible = false; } else { @@ -103,7 +103,7 @@ public class PlayerInfo implements Disposable, Drawable reinforcement.write("" + r); } - if (player.getFaction() == Army.GE) + if (player.is(Army.GE)) flag = geFlag; else flag = usFlag; diff --git a/core/src/ch/asynk/tankontank/game/hud/UnitDock.java b/core/src/ch/asynk/tankontank/game/hud/UnitDock.java index 0e67630..ce2ac04 100644 --- a/core/src/ch/asynk/tankontank/game/hud/UnitDock.java +++ b/core/src/ch/asynk/tankontank/game/hud/UnitDock.java @@ -96,9 +96,9 @@ public class UnitDock extends Bg { float x = position.getX(rect.width * SCALE); if (done) { - pawns = ctrl.player.getReinforcement(); - if(pawns.size() == 0) + if(ctrl.player.reinforcement() == 0) return; + pawns = ctrl.player.reinforcement; rect.width = pawns.get(0).getWidth() + (2 * padding); rect.height = ((pawns.get(0).getHeight() * pawns.size()) + ((pawns.size() + 1) * padding)); rect.x = (position.isLeft() ? (0 - (rect.width * SCALE)) : (x + (rect.width * SCALE))); diff --git a/core/src/ch/asynk/tankontank/game/states/StateEngage.java b/core/src/ch/asynk/tankontank/game/states/StateEngage.java index 1586d80..4b1539b 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateEngage.java +++ b/core/src/ch/asynk/tankontank/game/states/StateEngage.java @@ -19,7 +19,7 @@ public class StateEngage extends StateCommon activeUnit = null; // use selectedHex and selectedUnit map.hidePossibleTargets(); - map.collectPossibleTargets(selectedUnit, ctrl.opponent.unitIterator()); + map.collectPossibleTargets(selectedUnit, ctrl.opponent.units); map.showPossibleTargets(); if (to != null) { // quick fire -> replay touchUp @@ -89,7 +89,7 @@ public class StateEngage extends StateCommon to = upHex; activeUnit = unit; activeUnit.showTarget(); - map.collectAttackAssists(selectedUnit, activeUnit, ctrl.player.unitIterator()); + map.collectAttackAssists(selectedUnit, activeUnit, ctrl.player.units); map.showAttackAssists(); ctrl.hud.actionButtons.show(Buttons.ENGAGE.b | Buttons.DONE.b | ((ctrl.cfg.canCancel) ? Buttons.ABORT.b : 0)); } diff --git a/core/src/ch/asynk/tankontank/game/states/StateSelect.java b/core/src/ch/asynk/tankontank/game/states/StateSelect.java index 7e7059e..7bdc815 100644 --- a/core/src/ch/asynk/tankontank/game/states/StateSelect.java +++ b/core/src/ch/asynk/tankontank/game/states/StateSelect.java @@ -77,9 +77,9 @@ public class StateSelect extends StateCommon int moves = map.collectPossibleMoves(selectedUnit); int targets = 0; if (isEnemy) - targets = map.collectPossibleTargets(selectedUnit, ctrl.player.unitIterator()); + targets = map.collectPossibleTargets(selectedUnit, ctrl.player.units); else - targets = map.collectPossibleTargets(selectedUnit, ctrl.opponent.unitIterator()); + targets = map.collectPossibleTargets(selectedUnit, ctrl.opponent.units); if (moves > 0) map.collectMoveablePawns(selectedUnit); showPossibilities(selectedUnit); -- cgit v1.1-2-g2b99