From ab76defa6334d83ba2d73a3adfad1d697061c1e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 21 Jan 2015 10:52:33 +0100 Subject: Objective[Set]: move from game to engine package --- core/src/ch/asynk/tankontank/engine/Objective.java | 49 ++++++++++++ .../ch/asynk/tankontank/engine/ObjectiveSet.java | 73 ++++++++++++++++++ core/src/ch/asynk/tankontank/game/Map.java | 71 +++++++++++++++-- core/src/ch/asynk/tankontank/game/Objective.java | 48 ------------ .../src/ch/asynk/tankontank/game/ObjectiveSet.java | 90 ---------------------- 5 files changed, 185 insertions(+), 146 deletions(-) create mode 100644 core/src/ch/asynk/tankontank/engine/Objective.java create mode 100644 core/src/ch/asynk/tankontank/engine/ObjectiveSet.java delete mode 100644 core/src/ch/asynk/tankontank/game/Objective.java delete mode 100644 core/src/ch/asynk/tankontank/game/ObjectiveSet.java diff --git a/core/src/ch/asynk/tankontank/engine/Objective.java b/core/src/ch/asynk/tankontank/engine/Objective.java new file mode 100644 index 0000000..1686122 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/Objective.java @@ -0,0 +1,49 @@ +package ch.asynk.tankontank.engine; + +public class Objective +{ + protected Faction curFaction; + protected Faction prevFaction; + private boolean persistent; + + public Objective(Faction faction, boolean persistent) + { + this.curFaction = faction; + this.prevFaction = faction; + this.persistent = persistent; + } + + public boolean is(Faction faction) + { + return (curFaction == faction); + } + + public Faction faction() + { + return curFaction; + } + + public boolean set(Faction faction) + { + if (faction == curFaction) + return false; + + prevFaction = curFaction; + curFaction = faction; + return true; + } + + public boolean unset() + { + if (persistent) + return false; + revert(); + return true; + } + + public Faction revert() + { + curFaction = prevFaction; + return curFaction; + } +} diff --git a/core/src/ch/asynk/tankontank/engine/ObjectiveSet.java b/core/src/ch/asynk/tankontank/engine/ObjectiveSet.java new file mode 100644 index 0000000..221fbc8 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/ObjectiveSet.java @@ -0,0 +1,73 @@ +package ch.asynk.tankontank.engine; + +import java.util.Collection; +import java.util.HashMap; + +public class ObjectiveSet extends HashMap +{ + public interface ObjectiveCb + { + public void showObjective(Tile tile, Faction faction); + } + + private final Board board; + private final HashMap modified; + + public ObjectiveSet(Board board, int n) + { + super(n); + this.board = board; + this.modified = new HashMap(10); + } + + public void add(Tile tile, Faction faction, boolean persistent) + { + put(tile, new Objective(faction, persistent)); + } + + public int count(Faction faction) + { + int n = 0; + for (Objective objective : values()) { + if (objective.is(faction)) + n += 1; + } + return n; + } + + public Faction claim(Tile tile, Faction faction) + { + Objective objective = get(tile); + if (objective == null) + return null; + + if (objective.set(faction)) + modified.put(objective, tile); + return objective.faction(); + } + + public Faction unclaim(Tile tile) + { + Objective objective = get(tile); + if (objective == null) + return null; + + if (objective.unset()) + modified.remove(objective); + return objective.faction(); + } + + public void forget() + { + modified.clear(); + } + + public void revert(ObjectiveCb cb) + { + for (Objective objective : modified.keySet()) { + objective.revert(); + cb.showObjective(modified.get(objective), objective.faction()); + } + modified.clear(); + } +} diff --git a/core/src/ch/asynk/tankontank/game/Map.java b/core/src/ch/asynk/tankontank/game/Map.java index d0b461b..cd958a2 100644 --- a/core/src/ch/asynk/tankontank/game/Map.java +++ b/core/src/ch/asynk/tankontank/game/Map.java @@ -11,11 +11,15 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import ch.asynk.tankontank.TankOnTank; import ch.asynk.tankontank.engine.Pawn; import ch.asynk.tankontank.engine.Board; +import ch.asynk.tankontank.engine.Tile; +import ch.asynk.tankontank.engine.Faction; import ch.asynk.tankontank.engine.Move; import ch.asynk.tankontank.engine.SelectedTile; +import ch.asynk.tankontank.engine.ObjectiveSet; import ch.asynk.tankontank.engine.Orientation; import ch.asynk.tankontank.engine.Meteorology; import ch.asynk.tankontank.engine.PathBuilder; +import ch.asynk.tankontank.engine.gfx.Moveable; import ch.asynk.tankontank.engine.gfx.Animation; import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; import ch.asynk.tankontank.engine.gfx.animations.DiceAnimation; @@ -26,10 +30,11 @@ import ch.asynk.tankontank.engine.gfx.animations.PromoteAnimation; import ch.asynk.tankontank.engine.gfx.animations.DestroyAnimation; import ch.asynk.tankontank.engine.gfx.animations.SoundAnimation; import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation; +import ch.asynk.tankontank.engine.gfx.animations.MoveToAnimation.MoveToAnimationCb; import ch.asynk.tankontank.ui.Position; -public abstract class Map extends Board +public abstract class Map extends Board implements MoveToAnimationCb, ObjectiveSet.ObjectiveCb { private final Ctrl ctrl; @@ -173,12 +178,29 @@ public abstract class Map extends Board public void addObjective(int col, int row, Army army) { - objectives.add(getHex(col, row), army, true); + addObjective(col, row, army, true); } public void addHoldObjective(int col, int row, Army army) { - objectives.add(getHex(col, row), army, false); + addObjective(col, row, army, false); + } + + private void addObjective(int col, int row, Army army, boolean persistent) + { + Hex hex = getHex(col, row); + objectives.add(hex, army, persistent); + showObjective(hex, army, !persistent); + } + + private void claim(Hex hex, Army army) + { + showObjective(hex, objectives.claim(hex, army)); + } + + private void unclaim(Hex hex) + { + showObjective(hex, objectives.unclaim(hex)); } public int collectPossibleMoves(Unit unit) @@ -260,6 +282,27 @@ public abstract class Map extends Board return 0; } + // -> implement MoveToAnimationCb + + @Override + public void moveToAnimationEnter(Moveable moveable, float x, float y, float r) + { + claim(getHexAt(x, y), (Army) moveable.getFaction()); + } + + @Override + public void moveToAnimationLeave(Moveable moveable, float x, float y, float r) + { + unclaim(getHexAt(x, y)); + } + + @Override + public void moveToAnimationDone(Moveable moveable, float x, float y, float r) + { + } + + // <- implement MoveToAnimationCb + private int process(Unit unit, Move move) { TankOnTank.debug("Process", String.format("%s %s", move.type, move.toString())); @@ -278,13 +321,13 @@ public abstract class Map extends Board // FIXME SET -> activatedUnits.add(unit); ?? setPawnOnto(unit, move); ctrl.player.unitEntry(unit); - objectives.claim((Hex) move.to, unit.getArmy()); + claim((Hex) move.to, unit.getArmy()); break; case ENTER: // FIXME ENTER -> activatedUnits.add(unit); ?? enterPawn(unit, move); ctrl.player.unitEntry(unit); - objectives.claim((Hex) move.to, unit.getArmy()); + claim((Hex) move.to, unit.getArmy()); break; default: System.err.println(String.format("process wrong type %s", move.type)); @@ -360,14 +403,14 @@ public abstract class Map extends Board revertLastPawnMove(unit, notifyDoneAnimation(unit)); } activatedUnits.clear(); - objectives.revert(); + objectives.revert(this); } public void revertEnter(Unit unit) { unit.reset(); removePawn(unit); - objectives.revert(); + objectives.revert(this); ctrl.player.revertUnitEntry(unit); } @@ -519,7 +562,7 @@ public abstract class Map extends Board activatedUnits.clear(); if (success) { - objectives.unclaim(target.getHex()); + unclaim(target.getHex()); removePawn(target); destroy.set(2f, target); addAnimation(destroy); @@ -580,8 +623,20 @@ public abstract class Map extends Board enableOverlayOn(hex, Hex.OBJECTIVE, true); } + + // -> implement ObjectiveSet.ObjectiveCb + + public void showObjective(Tile tile, Faction faction) + { + showObjective((Hex) tile, (Army) faction); + } + + // <- implement MoveToAnimationCb + public void showObjective(Hex hex, Army army) { + if (army == null) + army = Army.NONE; switch(army) { case GE: enableOverlayOn(hex, Hex.OBJECTIVE_GE, true); diff --git a/core/src/ch/asynk/tankontank/game/Objective.java b/core/src/ch/asynk/tankontank/game/Objective.java deleted file mode 100644 index f7a6c3b..0000000 --- a/core/src/ch/asynk/tankontank/game/Objective.java +++ /dev/null @@ -1,48 +0,0 @@ -package ch.asynk.tankontank.game; - -public class Objective -{ - protected Army curArmy; - protected Army prevArmy; - private boolean persistent; - - public Objective(Army army, boolean persistent) - { - this.curArmy = army; - this.prevArmy = army; - this.persistent = persistent; - } - - public boolean is(Army army) - { - return (curArmy == army); - } - - public Army army() - { - return curArmy; - } - - public boolean set(Army army) - { - if (army == curArmy) - return false; - prevArmy = curArmy; - curArmy = army; - return true; - } - - public boolean unset() - { - if (persistent) - return false; - revert(); - return true; - } - - public Army revert() - { - curArmy = prevArmy; - return curArmy; - } -} diff --git a/core/src/ch/asynk/tankontank/game/ObjectiveSet.java b/core/src/ch/asynk/tankontank/game/ObjectiveSet.java deleted file mode 100644 index ab827f6..0000000 --- a/core/src/ch/asynk/tankontank/game/ObjectiveSet.java +++ /dev/null @@ -1,90 +0,0 @@ -package ch.asynk.tankontank.game; - -import java.util.Collection; -import java.util.HashMap; - -import ch.asynk.tankontank.engine.gfx.Moveable; -import ch.asynk.tankontank.engine.gfx.animations.MoveToAnimation.MoveToAnimationCb; - -public class ObjectiveSet extends HashMap implements MoveToAnimationCb -{ - private final Map map; - private final HashMap modified; - - public ObjectiveSet(Map map, int n) - { - super(n); - this.map = map; - this.modified = new HashMap(10); - } - - public void add(Hex hex, Army army, boolean persistent) - { - put(hex, new Objective(army, persistent)); - map.showObjective(hex, army, !persistent); - } - - public int count(Army army) - { - int n = 0; - for (Objective objective : values()) { - if (objective.is(army)) - n += 1; - } - return n; - } - - @Override - public void moveToAnimationEnter(Moveable moveable, float x, float y, float r) - { - claim(map.getHexAt(x, y), (Army) moveable.getFaction()); - } - - @Override - public void moveToAnimationLeave(Moveable moveable, float x, float y, float r) - { - unclaim(map.getHexAt(x, y)); - } - - @Override - public void moveToAnimationDone(Moveable moveable, float x, float y, float r) - { - } - - public Army claim(Hex hex, Army army) - { - Objective objective = get(hex); - if (objective == null) - return Army.NONE; - if (objective.set(army)) { - modified.put(objective, hex); - map.showObjective(hex, army); - } - return army; - } - - public void unclaim(Hex hex) - { - Objective objective = get(hex); - if (objective == null) - return; - if (objective.unset()) { - modified.remove(objective); - map.showObjective(hex, objective.army()); - } - } - - public void forget() - { - modified.clear(); - } - - public void revert() - { - for (Objective objective : modified.keySet()) { - objective.revert(); - map.showObjective(modified.get(objective), objective.army()); - } - modified.clear(); - } -} -- cgit v1.1-2-g2b99