From 5d0f17b24691308cadeacd2d86774c5b3c8cb7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 24 Jan 2016 23:53:27 +0100 Subject: engine.Order, engine.OrderList, game.Command -> game.Order, game.OrderList --- core/src/ch/asynk/rustanddust/engine/Order.java | 16 -- .../src/ch/asynk/rustanddust/engine/OrderList.java | 79 ------- core/src/ch/asynk/rustanddust/game/Command.java | 225 ------------------- core/src/ch/asynk/rustanddust/game/Map.java | 6 +- core/src/ch/asynk/rustanddust/game/Order.java | 219 +++++++++++++++++++ core/src/ch/asynk/rustanddust/game/OrderList.java | 79 +++++++ .../asynk/rustanddust/game/map/Map4Commands.java | 239 --------------------- .../ch/asynk/rustanddust/game/map/Map4Orders.java | 239 +++++++++++++++++++++ 8 files changed, 540 insertions(+), 562 deletions(-) delete mode 100644 core/src/ch/asynk/rustanddust/engine/Order.java delete mode 100644 core/src/ch/asynk/rustanddust/engine/OrderList.java delete mode 100644 core/src/ch/asynk/rustanddust/game/Command.java create mode 100644 core/src/ch/asynk/rustanddust/game/Order.java create mode 100644 core/src/ch/asynk/rustanddust/game/OrderList.java delete mode 100644 core/src/ch/asynk/rustanddust/game/map/Map4Commands.java create mode 100644 core/src/ch/asynk/rustanddust/game/map/Map4Orders.java diff --git a/core/src/ch/asynk/rustanddust/engine/Order.java b/core/src/ch/asynk/rustanddust/engine/Order.java deleted file mode 100644 index 960b126..0000000 --- a/core/src/ch/asynk/rustanddust/engine/Order.java +++ /dev/null @@ -1,16 +0,0 @@ -package ch.asynk.rustanddust.engine; - -import java.lang.Comparable; - -import com.badlogic.gdx.utils.Disposable; -import com.badlogic.gdx.utils.Pool; -import com.badlogic.gdx.utils.Json; - -public abstract class Order implements Disposable, Pool.Poolable, Json.Serializable, Comparable -{ - public interface OrderType - { - } - - public abstract boolean isA(OrderType type); -} diff --git a/core/src/ch/asynk/rustanddust/engine/OrderList.java b/core/src/ch/asynk/rustanddust/engine/OrderList.java deleted file mode 100644 index 9e4fe6c..0000000 --- a/core/src/ch/asynk/rustanddust/engine/OrderList.java +++ /dev/null @@ -1,79 +0,0 @@ -package ch.asynk.rustanddust.engine; - -import java.util.Iterator; - -import com.badlogic.gdx.utils.Json; -import com.badlogic.gdx.utils.JsonValue; -import com.badlogic.gdx.utils.JsonWriter.OutputType; - -import ch.asynk.rustanddust.engine.util.IterableArray; - -public class OrderList extends IterableArray implements Json.Serializable -{ - public OrderList(int capacity) - { - super(capacity); - } - - public Order get(Pawn pawn, Order.OrderType type) - { - for (Order o : this) { - if ((o.compareTo(pawn) == 0) && (o.isA(type))) - return o; - } - return null; - } - - public void dispose(Pawn pawn) - { - Iterator it = iterator(); - while (it.hasNext()) { - Order order = it.next(); - if (order.compareTo(pawn) == 0) { - it.remove(); - order.dispose(); - } - } - } - - public void dispose(Pawn pawn, Order.OrderType type) - { - Iterator it = iterator(); - while (it.hasNext()) { - Order order = it.next(); - if ((order.compareTo(pawn) == 0) && (order.isA(type))) { - it.remove(); - order.dispose(); - } - } - } - - public void dispose() - { - for (Order o : this) - o.dispose(); - clear(); - } - - public String toJson() - { - Json json = new Json(); - json.setOutputType(OutputType.json); - return json.toJson(this); - } - - @Override - public void write(Json json) - { - json.writeArrayStart("commands"); - for (Order o : this) - json.writeValue(o); - json.writeArrayEnd(); - } - - @Override - public void read(Json json, JsonValue jsonMap) - { - // TODO read(Json json, JsonValue jsonMap) - } -} diff --git a/core/src/ch/asynk/rustanddust/game/Command.java b/core/src/ch/asynk/rustanddust/game/Command.java deleted file mode 100644 index a146257..0000000 --- a/core/src/ch/asynk/rustanddust/game/Command.java +++ /dev/null @@ -1,225 +0,0 @@ -package ch.asynk.rustanddust.game; - -import com.badlogic.gdx.utils.Pool; -import com.badlogic.gdx.utils.Json; -import com.badlogic.gdx.utils.JsonValue; - -import ch.asynk.rustanddust.engine.Order; -import ch.asynk.rustanddust.engine.Move; -import ch.asynk.rustanddust.engine.Pawn; -import ch.asynk.rustanddust.engine.Tile; -import ch.asynk.rustanddust.engine.util.Collection; - -public class Command extends Order -{ - public enum CommandType implements Order.OrderType - { - NONE, - MOVE, - ENGAGE, - PROMOTE, - END_OF_TURN; - } - - private static final Pool commandPool = new Pool() - { - @Override - protected Command newObject() { - return new Command(); - } - }; - - public static void clearPool() - { - commandPool.clear(); - } - - public static Command get(Player player) - { - Command c = commandPool.obtain(); - c.player = player; - c.ap = player.getAp(); - c.turn = player.getCurrentTurn(); - return c; - } - - public CommandType type; - public Player player; - public int ap; - public int turn; - public Unit unit; - public Unit.UnitId unitId; - public Unit.UnitType unitType; - public Tile unitTile; - public Move move; - public Engagement engagement; - - private Command() - { - reset(); - } - - @Override - public void dispose() - { - commandPool.free(this); - } - - @Override - public void reset() - { - this.type = CommandType.NONE; - this.player = null; - this.unit = null; - if (this.move != null) { - this.move.dispose(); - this.move = null; - } - if (this.engagement != null) { - this.engagement.dispose(); - this.engagement = null; - } - } - - @Override - public int compareTo(Pawn pawn) - { - if (pawn == unit) - return 0; - return 1; - } - - @Override - public boolean isA(OrderType type) - { - return (type == this.type); - } - - @Override - public String toString() - { - return String.format("%s : %s", type, unit.id); - } - - public void setMove(Unit unit, Move move) - { - this.type = CommandType.MOVE; - this.move = move; - setUnit(unit); - } - - public void setPromote(Unit unit) - { - this.type = CommandType.PROMOTE; - setUnit(unit); - } - - public void setEngage(Unit unit, Unit target) - { - this.type = CommandType.ENGAGE; - this.engagement = Engagement.get(unit, target); - setUnit(unit); - } - - private void setUnit(Unit unit) - { - this.unit = unit; - this.unitId = unit.id; - this.unitType = unit.type; - this.unitTile = unit.getTile(); - } - - @Override - public void write(Json json) - { - json.writeValue("type", type); - json.writeObjectStart("player"); - json.writeValue("army", player.getName()); - json.writeValue("turn", turn); - json.writeValue("aps", ap); - json.writeObjectEnd(); - json.writeObjectStart("unit"); - json.writeValue("id", unitId); - json.writeValue("type", unitType); - json.writeValue("hq", unit.hq); - json.writeValue("ace", unit.ace); - writeTile(json, "tile", unitTile); - json.writeObjectEnd(); - if (move != null) writeMove(json, "move", move); - if (engagement != null) writeEngagement(json, "engagement", engagement); - } - - private void writeMove(Json json, String key, Move m) - { - json.writeObjectStart(key); - json.writeValue("type", move.type); - writeTile(json, "from", move.from); - writeTile(json, "to", move.to); - json.writeValue("orientation", move.orientation.r()); - writeTiles(json, "path", move.tiles); - json.writeObjectEnd(); - } - - private void writeEngagement(Json json, String key, Engagement e) - { - json.writeObjectStart(key); - writeUnit(json, "attacker", e.attacker); - writeUnit(json, "defender", e.defender); - writeUnits(json, "assists", e.assists); - json.writeObjectStart("dice"); - json.writeValue("d1", e.d1); - json.writeValue("d2", e.d2); - json.writeValue("d3", e.d3); - json.writeValue("d4", e.d4); - json.writeObjectEnd(); - json.writeObjectStart("results"); - json.writeValue("success", e.success); - json.writeValue("attackSum", e.attackSum); - json.writeValue("defenseSum", e.defenseSum); - json.writeObjectEnd(); - json.writeObjectEnd(); - } - - private void writeUnit(Json json, String key, Unit u) - { - if (key != null) json.writeObjectStart(key); - else json.writeObjectStart(); - json.writeValue("id", u.id); - json.writeValue("ace", u.ace); - json.writeValue("army", u.getArmy()); - writeTile(json, "tile", u.getTile()); - json.writeObjectEnd(); - } - - private void writeUnits(Json json, String key, Collection units) - { - json.writeArrayStart(key); - for (Unit u : units) - writeUnit(json, null, u); - json.writeArrayEnd(); - } - - private void writeTile(Json json, String key, Tile t) - { - if (t == null) return; - if (key != null) json.writeObjectStart(key); - else json.writeObjectStart(); - json.writeValue("col", t.getCol()); - json.writeValue("row", t.getRow()); - json.writeObjectEnd(); - } - - private void writeTiles(Json json, String key, Collection tiles) - { - json.writeArrayStart(key); - for (Tile t : tiles) - writeTile(json, null, t); - json.writeArrayEnd(); - } - - @Override - public void read(Json json, JsonValue jsonMap) - { - // FIXME Command.read(Json, JsonValue); - } -} diff --git a/core/src/ch/asynk/rustanddust/game/Map.java b/core/src/ch/asynk/rustanddust/game/Map.java index 4484327..2386f5d 100644 --- a/core/src/ch/asynk/rustanddust/game/Map.java +++ b/core/src/ch/asynk/rustanddust/game/Map.java @@ -8,9 +8,9 @@ import ch.asynk.rustanddust.engine.SelectedTile; import ch.asynk.rustanddust.engine.Meteorology; import ch.asynk.rustanddust.game.Unit; import ch.asynk.rustanddust.game.Engagement; -import ch.asynk.rustanddust.game.map.Map4Commands; +import ch.asynk.rustanddust.game.map.Map4Orders; -public abstract class Map extends Map4Commands +public abstract class Map extends Map4Orders { protected final Meteorology meteorology; @@ -39,7 +39,7 @@ public abstract class Map extends Map4Commands RustAndDust.debug("TurnDone", String.format(" Processed Commands : %d", commandsSize())); // FIXME must emit the turn commands - commandsClear(); + ordersClear(); } @Override diff --git a/core/src/ch/asynk/rustanddust/game/Order.java b/core/src/ch/asynk/rustanddust/game/Order.java new file mode 100644 index 0000000..07fa19c --- /dev/null +++ b/core/src/ch/asynk/rustanddust/game/Order.java @@ -0,0 +1,219 @@ +package ch.asynk.rustanddust.game; + +import com.badlogic.gdx.utils.Pool; +import com.badlogic.gdx.utils.Disposable; +import com.badlogic.gdx.utils.Json; +import com.badlogic.gdx.utils.JsonValue; + +import ch.asynk.rustanddust.engine.Move; +import ch.asynk.rustanddust.engine.Tile; +import ch.asynk.rustanddust.engine.util.Collection; + +public class Order implements Disposable, Pool.Poolable, Json.Serializable, Comparable +{ + public enum OrderType + { + NONE, + MOVE, + ENGAGE, + PROMOTE, + END_OF_TURN; + } + + private static final Pool orderPool = new Pool() + { + @Override + protected Order newObject() { + return new Order(); + } + }; + + public static void clearPool() + { + orderPool.clear(); + } + + public static Order get(Player player) + { + Order c = orderPool.obtain(); + c.player = player; + return c; + } + + public OrderType type; + public Player player; + public Unit unit; + public Unit.UnitId unitId; + public Unit.UnitType unitType; + public Hex unitHex; + public Move move; + public Engagement engagement; + + private Order() + { + reset(); + } + + @Override + public void dispose() + { + orderPool.free(this); + } + + @Override + public void reset() + { + this.type = OrderType.NONE; + this.player = null; + this.unit = null; + if (this.move != null) { + this.move.dispose(); + this.move = null; + } + if (this.engagement != null) { + this.engagement.dispose(); + this.engagement = null; + } + } + + @Override + public int compareTo(Unit other) + { + if (unit == other) + return 0; + return 1; + } + + public boolean isA(OrderType type) + { + return (type == this.type); + } + + @Override + public String toString() + { + return String.format("%s : %s", type, unit.id); + } + + public void setMove(Unit unit, Move move) + { + this.type = OrderType.MOVE; + this.move = move; + setUnit(unit); + } + + public void setPromote(Unit unit) + { + this.type = OrderType.PROMOTE; + setUnit(unit); + } + + public void setEngage(Unit unit, Unit target) + { + this.type = OrderType.ENGAGE; + this.engagement = Engagement.get(unit, target); + setUnit(unit); + } + + private void setUnit(Unit unit) + { + this.unit = unit; + this.unitId = unit.id; + this.unitType = unit.type; + this.unitHex = unit.getHex(); + } + + @Override + public void write(Json json) + { + json.writeValue("type", type); + json.writeObjectStart("player"); + json.writeValue("army", player.getName()); + json.writeValue("turn", player.getCurrentTurn()); + json.writeValue("aps", player.getAp()); + json.writeObjectEnd(); + json.writeObjectStart("unit"); + json.writeValue("id", unitId); + json.writeValue("type", unitType); + json.writeValue("hq", unit.hq); + json.writeValue("ace", unit.ace); + writeHex(json, "tile", unitHex); + json.writeObjectEnd(); + if (move != null) writeMove(json, "move", move); + if (engagement != null) writeEngagement(json, "engagement", engagement); + } + + private void writeMove(Json json, String key, Move m) + { + json.writeObjectStart(key); + json.writeValue("type", move.type); + writeHex(json, "from", (Hex) move.from); + writeHex(json, "to", (Hex) move.to); + json.writeValue("orientation", move.orientation.r()); + writeTiles(json, "path", move.tiles); + json.writeObjectEnd(); + } + + private void writeEngagement(Json json, String key, Engagement e) + { + json.writeObjectStart(key); + writeUnit(json, "attacker", e.attacker); + writeUnit(json, "defender", e.defender); + writeUnits(json, "assists", e.assists); + json.writeObjectStart("dice"); + json.writeValue("d1", e.d1); + json.writeValue("d2", e.d2); + json.writeValue("d3", e.d3); + json.writeValue("d4", e.d4); + json.writeObjectEnd(); + json.writeObjectStart("results"); + json.writeValue("success", e.success); + json.writeValue("attackSum", e.attackSum); + json.writeValue("defenseSum", e.defenseSum); + json.writeObjectEnd(); + json.writeObjectEnd(); + } + + private void writeUnit(Json json, String key, Unit u) + { + if (key != null) json.writeObjectStart(key); + else json.writeObjectStart(); + json.writeValue("id", u.id); + json.writeValue("ace", u.ace); + json.writeValue("army", u.getArmy()); + writeHex(json, "tile", u.getHex()); + json.writeObjectEnd(); + } + + private void writeUnits(Json json, String key, Collection units) + { + json.writeArrayStart(key); + for (Unit u : units) + writeUnit(json, null, u); + json.writeArrayEnd(); + } + + private void writeHex(Json json, String key, Hex t) + { + if (t == null) return; + if (key != null) json.writeObjectStart(key); + else json.writeObjectStart(); + json.writeValue("col", t.getCol()); + json.writeValue("row", t.getRow()); + json.writeObjectEnd(); + } + + private void writeTiles(Json json, String key, Collection tiles) + { + json.writeArrayStart(key); + for (Tile t : tiles) + writeHex(json, null, (Hex) t); + json.writeArrayEnd(); + } + + @Override + public void read(Json json, JsonValue jsonMap) + { + // FIXME Order.read(Json, JsonValue); + } +} diff --git a/core/src/ch/asynk/rustanddust/game/OrderList.java b/core/src/ch/asynk/rustanddust/game/OrderList.java new file mode 100644 index 0000000..d6c5b5f --- /dev/null +++ b/core/src/ch/asynk/rustanddust/game/OrderList.java @@ -0,0 +1,79 @@ +package ch.asynk.rustanddust.game; + +import java.util.Iterator; + +import com.badlogic.gdx.utils.Json; +import com.badlogic.gdx.utils.JsonValue; +import com.badlogic.gdx.utils.JsonWriter.OutputType; + +import ch.asynk.rustanddust.engine.util.IterableArray; + +public class OrderList extends IterableArray implements Json.Serializable +{ + public OrderList(int capacity) + { + super(capacity); + } + + public Order get(Unit unit, Order.OrderType type) + { + for (Order o : this) { + if ((o.compareTo(unit) == 0) && (o.isA(type))) + return o; + } + return null; + } + + public void dispose(Unit unit) + { + Iterator it = iterator(); + while (it.hasNext()) { + Order order = it.next(); + if (order.compareTo(unit) == 0) { + it.remove(); + order.dispose(); + } + } + } + + public void dispose(Unit unit, Order.OrderType type) + { + Iterator it = iterator(); + while (it.hasNext()) { + Order order = it.next(); + if ((order.compareTo(unit) == 0) && (order.isA(type))) { + it.remove(); + order.dispose(); + } + } + } + + public void dispose() + { + for (Order o : this) + o.dispose(); + clear(); + } + + public String toJson() + { + Json json = new Json(); + json.setOutputType(OutputType.json); + return json.toJson(this); + } + + @Override + public void write(Json json) + { + json.writeArrayStart("commands"); + for (Order o : this) + json.writeValue(o); + json.writeArrayEnd(); + } + + @Override + public void read(Json json, JsonValue jsonMap) + { + // TODO read(Json json, JsonValue jsonMap) + } +} diff --git a/core/src/ch/asynk/rustanddust/game/map/Map4Commands.java b/core/src/ch/asynk/rustanddust/game/map/Map4Commands.java deleted file mode 100644 index 2335475..0000000 --- a/core/src/ch/asynk/rustanddust/game/map/Map4Commands.java +++ /dev/null @@ -1,239 +0,0 @@ -package ch.asynk.rustanddust.game.map; - -import com.badlogic.gdx.graphics.Texture; - -import ch.asynk.rustanddust.RustAndDust; -import ch.asynk.rustanddust.engine.Move; -import ch.asynk.rustanddust.engine.SelectedTile; -import ch.asynk.rustanddust.engine.OrderList; -import ch.asynk.rustanddust.engine.Orientation; -import ch.asynk.rustanddust.game.Hex; -import ch.asynk.rustanddust.game.Unit; -import ch.asynk.rustanddust.game.Player; -import ch.asynk.rustanddust.game.Command; -import ch.asynk.rustanddust.game.Engagement; -import ch.asynk.rustanddust.game.Battle; - -public abstract class Map4Commands extends Map3Animations -{ - private final Battle battle; - private final OrderList commands; - - protected abstract int engagementCost(Engagement e); - protected abstract void resolveEngagement(Engagement e); - - public Map4Commands(final RustAndDust game, Texture map, SelectedTile hex) - { - super(game, map, hex); - - this.battle = game.ctrl.battle; - this.commands = new OrderList(10); - } - - @Override - public void dispose() - { - super.dispose(); - commands.dispose(); - Command.clearPool(); - Engagement.clearPool(); - } - - protected int commandsSize() { return commands.size(); } - protected void commandsClear() { commands.dispose(); } - - // STATES ENTRY -> - - public void showOnBoard(final Unit unit, Hex to, Orientation o) - { - setPawnOnto(unit, to, o); - } - - public boolean setOnBoard(final Unit unit, Hex to, Orientation entry) - { - commands.dispose(unit); - return (process(getMoveCommand(unit, Move.getSet(unit, to, entry))) == 1); - } - - public boolean enterBoard(final Unit unit, Hex to, int allowedMoves) - { - Orientation entry = findBestEntry(unit, to, allowedMoves); - if (entry == Orientation.KEEP) - return false; - - return (process(getMoveCommand(unit, Move.getEnter(unit, to, entry))) == 1); - } - - public int exitBoard(final Unit unit) - { - return process(getMoveCommand(unit, paths.getExitMove())); - } - - public int moveUnit(final Unit unit) - { - return process(getMoveCommand(unit, paths.getMove())); - } - - public void revertMoves() - { - for (Unit unit: activatedUnits) { - RustAndDust.debug(" revertMove() " + unit); - revertLastPawnMove(unit, ((Command) commands.get(unit, Command.CommandType.MOVE)).move); - commands.dispose(unit, Command.CommandType.MOVE); - } - activatedUnits.clear(); - } - - public void revertEnter(final Unit unit) - { - RustAndDust.debug(" revertEnter() "+ unit); - - revertclaim(unit, unit.getHex()); - removePawn(unit); - battle.getPlayer().revertUnitEntry(unit); - commands.dispose(unit); - unit.reset(); - } - - public boolean engageUnit(final Unit unit, final Unit target) - { - attack(unit, target, true); - - Command cmd = Command.get(battle.getPlayer()); - cmd.setEngage(unit, target); - return (process(cmd) == 1); - } - - public void promoteUnit(final Unit unit) - { - Command cmd = Command.get(battle.getPlayer()); - cmd.setPromote(unit); - process(cmd); - } - - // STATES ENTRY <- - - private Command getMoveCommand(Unit unit, Move move) - { - Command cmd = Command.get(battle.getPlayer()); - cmd.setMove(unit, move); - return cmd; - } - - private void initMove(Unit unit) - { - moveableUnits.remove(unit); - activatedUnits.add(unit); - playMoveSound(unit); - } - - private int promoteUnit(final Unit unit, final Player player) - { - activatedUnits.add(unit); - addPromoteAnimation(unit, player, new Runnable() { - @Override - public void run() { - player.promote(unit); - } - }); - return 1; - } - - private int process(Command cmd) - { - RustAndDust.debug("Command", cmd.toString()); - - int r = 1; - - switch(cmd.type) { - case MOVE: - r = process(cmd.unit, cmd.move); - break; - case PROMOTE: - r = promoteUnit(cmd.unit, cmd.player); - break; - case ENGAGE: - r = doEngagement(cmd.engagement); - break; - default: - System.err.println(String.format("process wrong Command type %s", cmd.type)); - r = -1; - break; - } - - if (r != -1) - commands.add(cmd); - - return r; - } - - private int process(Unit unit, Move move) - { - RustAndDust.debug(" Move", String.format("%s %s", move.type, move.toString())); - - int r = 1; - - switch(move.type) { - case REGULAR: - initMove(unit); - movePawn(unit, move, this); - r = moveableUnits.size(); - break; - case EXIT: - initMove(unit); - movePawn(unit, move, this); - battle.getPlayer().unitWithdraw(unit); - r = moveableUnits.size(); - break; - case SET: - setPawnOnto(unit, move); - battle.getPlayer().unitEntry(unit); - claim(unit, move.to); - break; - case ENTER: - enterPawn(unit, move); - battle.getPlayer().unitEntry(unit); - claim(unit, move.to); - break; - default: - System.err.println(String.format("process wrong Move type %s", move.type)); - r = -1; - break; - } - - return r; - } - - private int doEngagement(Engagement e) - { - resolveEngagement(e); - - breakthroughUnits.clear(); - activatedUnits.clear(); - - activatedUnits.add(e.attacker); - for (Unit u : e.assists) - activatedUnits.add(u); - - for (Unit u : activatedUnits) { - u.engage(); - if (u.canBreak()) - breakthroughUnits.add(u); - } - - if (e.success) { - unclaim(e.defender, e.defender.getHex()); - removePawn(e.defender); - addDestroyAnimation(e.defender); - } - - game.ctrl.hud.engagementSummary(e); - addEngagementAnimation(e.defender); - - if (engagementCost(e) == 0) - activatedUnits.clear(); - - return (e.success ? 1 : 0); - } - -} diff --git a/core/src/ch/asynk/rustanddust/game/map/Map4Orders.java b/core/src/ch/asynk/rustanddust/game/map/Map4Orders.java new file mode 100644 index 0000000..3aa0bbf --- /dev/null +++ b/core/src/ch/asynk/rustanddust/game/map/Map4Orders.java @@ -0,0 +1,239 @@ +package ch.asynk.rustanddust.game.map; + +import com.badlogic.gdx.graphics.Texture; + +import ch.asynk.rustanddust.RustAndDust; +import ch.asynk.rustanddust.engine.Move; +import ch.asynk.rustanddust.engine.SelectedTile; +import ch.asynk.rustanddust.engine.Orientation; +import ch.asynk.rustanddust.game.Hex; +import ch.asynk.rustanddust.game.Unit; +import ch.asynk.rustanddust.game.Player; +import ch.asynk.rustanddust.game.Order; +import ch.asynk.rustanddust.game.OrderList; +import ch.asynk.rustanddust.game.Engagement; +import ch.asynk.rustanddust.game.Battle; + +public abstract class Map4Orders extends Map3Animations +{ + private final Battle battle; + private final OrderList orders; + + protected abstract int engagementCost(Engagement e); + protected abstract void resolveEngagement(Engagement e); + + public Map4Orders(final RustAndDust game, Texture map, SelectedTile hex) + { + super(game, map, hex); + + this.battle = game.ctrl.battle; + this.orders = new OrderList(10); + } + + @Override + public void dispose() + { + super.dispose(); + orders.dispose(); + Order.clearPool(); + Engagement.clearPool(); + } + + protected int ordersSize() { return orders.size(); } + protected void ordersClear() { orders.dispose(); } + + // STATES ENTRY -> + + public void showOnBoard(final Unit unit, Hex to, Orientation o) + { + setPawnOnto(unit, to, o); + } + + public boolean setOnBoard(final Unit unit, Hex to, Orientation entry) + { + orders.dispose(unit); + return (process(getMoveOrder(unit, Move.getSet(unit, to, entry))) == 1); + } + + public boolean enterBoard(final Unit unit, Hex to, int allowedMoves) + { + Orientation entry = findBestEntry(unit, to, allowedMoves); + if (entry == Orientation.KEEP) + return false; + + return (process(getMoveOrder(unit, Move.getEnter(unit, to, entry))) == 1); + } + + public int exitBoard(final Unit unit) + { + return process(getMoveOrder(unit, paths.getExitMove())); + } + + public int moveUnit(final Unit unit) + { + return process(getMoveOrder(unit, paths.getMove())); + } + + public void revertMoves() + { + for (Unit unit: activatedUnits) { + RustAndDust.debug(" revertMove() " + unit); + revertLastPawnMove(unit, ((Order) orders.get(unit, Order.OrderType.MOVE)).move); + orders.dispose(unit, Order.OrderType.MOVE); + } + activatedUnits.clear(); + } + + public void revertEnter(final Unit unit) + { + RustAndDust.debug(" revertEnter() "+ unit); + + revertclaim(unit, unit.getHex()); + removePawn(unit); + battle.getPlayer().revertUnitEntry(unit); + orders.dispose(unit); + unit.reset(); + } + + public boolean engageUnit(final Unit unit, final Unit target) + { + attack(unit, target, true); + + Order cmd = Order.get(battle.getPlayer()); + cmd.setEngage(unit, target); + return (process(cmd) == 1); + } + + public void promoteUnit(final Unit unit) + { + Order cmd = Order.get(battle.getPlayer()); + cmd.setPromote(unit); + process(cmd); + } + + // STATES ENTRY <- + + private Order getMoveOrder(Unit unit, Move move) + { + Order cmd = Order.get(battle.getPlayer()); + cmd.setMove(unit, move); + return cmd; + } + + private void initMove(Unit unit) + { + moveableUnits.remove(unit); + activatedUnits.add(unit); + playMoveSound(unit); + } + + private int promoteUnit(final Unit unit, final Player player) + { + activatedUnits.add(unit); + addPromoteAnimation(unit, player, new Runnable() { + @Override + public void run() { + player.promote(unit); + } + }); + return 1; + } + + private int process(Order cmd) + { + RustAndDust.debug("Order", cmd.toString()); + + int r = 1; + + switch(cmd.type) { + case MOVE: + r = process(cmd.unit, cmd.move); + break; + case PROMOTE: + r = promoteUnit(cmd.unit, cmd.player); + break; + case ENGAGE: + r = doEngagement(cmd.engagement); + break; + default: + System.err.println(String.format("process wrong Order type %s", cmd.type)); + r = -1; + break; + } + + if (r != -1) + orders.add(cmd); + + return r; + } + + private int process(Unit unit, Move move) + { + RustAndDust.debug(" Move", String.format("%s %s", move.type, move.toString())); + + int r = 1; + + switch(move.type) { + case REGULAR: + initMove(unit); + movePawn(unit, move, this); + r = moveableUnits.size(); + break; + case EXIT: + initMove(unit); + movePawn(unit, move, this); + battle.getPlayer().unitWithdraw(unit); + r = moveableUnits.size(); + break; + case SET: + setPawnOnto(unit, move); + battle.getPlayer().unitEntry(unit); + claim(unit, move.to); + break; + case ENTER: + enterPawn(unit, move); + battle.getPlayer().unitEntry(unit); + claim(unit, move.to); + break; + default: + System.err.println(String.format("process wrong Move type %s", move.type)); + r = -1; + break; + } + + return r; + } + + private int doEngagement(Engagement e) + { + resolveEngagement(e); + + breakthroughUnits.clear(); + activatedUnits.clear(); + + activatedUnits.add(e.attacker); + for (Unit u : e.assists) + activatedUnits.add(u); + + for (Unit u : activatedUnits) { + u.engage(); + if (u.canBreak()) + breakthroughUnits.add(u); + } + + if (e.success) { + unclaim(e.defender, e.defender.getHex()); + removePawn(e.defender); + addDestroyAnimation(e.defender); + } + + game.ctrl.hud.engagementSummary(e); + addEngagementAnimation(e.defender); + + if (engagementCost(e) == 0) + activatedUnits.clear(); + + return (e.success ? 1 : 0); + } + +} -- cgit v1.1-2-g2b99