summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/engine
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2015-01-21 10:52:33 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2015-01-21 10:52:33 +0100
commitab76defa6334d83ba2d73a3adfad1d697061c1e4 (patch)
tree240e0d56b9bc230eff4603eaf1337d59563ad0bf /core/src/ch/asynk/tankontank/engine
parent773e6c2b462eb5390d7b3c5f8a85bef95562575e (diff)
downloadRustAndDust-ab76defa6334d83ba2d73a3adfad1d697061c1e4.zip
RustAndDust-ab76defa6334d83ba2d73a3adfad1d697061c1e4.tar.gz
Objective[Set]: move from game to engine package
Diffstat (limited to 'core/src/ch/asynk/tankontank/engine')
-rw-r--r--core/src/ch/asynk/tankontank/engine/Objective.java49
-rw-r--r--core/src/ch/asynk/tankontank/engine/ObjectiveSet.java73
2 files changed, 122 insertions, 0 deletions
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<Tile, Objective>
+{
+ public interface ObjectiveCb
+ {
+ public void showObjective(Tile tile, Faction faction);
+ }
+
+ private final Board board;
+ private final HashMap<Objective, Tile> modified;
+
+ public ObjectiveSet(Board board, int n)
+ {
+ super(n);
+ this.board = board;
+ this.modified = new HashMap<Objective, Tile>(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();
+ }
+}