summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2015-12-28 18:38:37 +0100
committerJérémy Zurcher <jeremy@asynk.ch>2015-12-28 18:38:37 +0100
commitec8d22ee6a21b8aaf56b051600e7d9bb94a1d788 (patch)
treefed0f439885aeb333ffe5dfddf491248d206dd49 /core/src/ch/asynk/rustanddust/engine
parentd76dd1fa80df0f89e33c3d3c5ab40e48c4f82d77 (diff)
downloadRustAndDust-ec8d22ee6a21b8aaf56b051600e7d9bb94a1d788.zip
RustAndDust-ec8d22ee6a21b8aaf56b051600e7d9bb94a1d788.tar.gz
Objective: Tile swallows Objective, ObjectiveSet is destroyed, Board does the trick
Diffstat (limited to 'core/src/ch/asynk/rustanddust/engine')
-rw-r--r--core/src/ch/asynk/rustanddust/engine/Board.java49
-rw-r--r--core/src/ch/asynk/rustanddust/engine/Objective.java54
-rw-r--r--core/src/ch/asynk/rustanddust/engine/ObjectiveSet.java89
-rw-r--r--core/src/ch/asynk/rustanddust/engine/PathBuilder.java2
-rw-r--r--core/src/ch/asynk/rustanddust/engine/Tile.java105
5 files changed, 148 insertions, 151 deletions
diff --git a/core/src/ch/asynk/rustanddust/engine/Board.java b/core/src/ch/asynk/rustanddust/engine/Board.java
index 83dbd86..6664d47 100644
--- a/core/src/ch/asynk/rustanddust/engine/Board.java
+++ b/core/src/ch/asynk/rustanddust/engine/Board.java
@@ -16,6 +16,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Matrix4;
+import ch.asynk.rustanddust.engine.gfx.Moveable;
import ch.asynk.rustanddust.engine.gfx.Animation;
import ch.asynk.rustanddust.engine.gfx.animations.AnimationSequence;
import ch.asynk.rustanddust.engine.gfx.animations.RunnableAnimation;
@@ -66,7 +67,6 @@ public abstract class Board implements Disposable, Animation
protected SelectedTile selectedTile;
abstract protected Config getConfig();
- abstract protected boolean isObjectiveFor(Tile tile, Pawn pawn);
protected Board(int cols, int rows)
{
@@ -392,6 +392,46 @@ public abstract class Board implements Disposable, Animation
return entry.opposite();
}
+ public int objectivesCount(Faction faction)
+ {
+ int n = 0;
+ for (Tile tile : tiles) {
+ if (tile.isOwnedObjective(faction))
+ n += 1;
+ }
+ return n;
+ }
+
+ public void claim(Moveable moveable, Tile tile)
+ {
+ int o = tile.belongsTo().overlay();
+ if (tile.claim(moveable.getFaction())) {
+ if (tile.isObjective()) {
+ tile.enableOverlay(o, false);
+ tile.enableOverlay(moveable.getFaction().overlay(), true);
+ tilesToDraw.add(tile);
+ }
+ }
+ }
+
+ public void unclaim(Moveable moveable, Tile tile)
+ {
+ if (tile.unclaim()) {
+ if (tile.isObjective()) {
+ tile.enableOverlay(moveable.getFaction().overlay(), false);
+ tile.enableOverlay(tile.belongsTo().overlay(), true);
+ tilesToDraw.add(tile);
+ }
+ }
+ }
+
+ public void revertclaim(Pawn pawn, Tile tile)
+ {
+ int o = pawn.getTile().revertClaim().overlay();
+ tile.enableOverlay(pawn.getFaction().overlay(), false);
+ enableOverlayOn(tile ,o, true);
+ }
+
public void enableOverlayOn(Tile tile, int i, boolean enable)
{
if(tile.enableOverlay(i, enable))
@@ -467,10 +507,15 @@ public abstract class Board implements Disposable, Animation
setPawnOnto(pawn, move.to, move.orientation);
}
- protected void revertLastPawnMove(final Pawn pawn)
+ protected void revertLastPawnMove(final Pawn pawn, final Move move)
{
removePawn(pawn);
+ revertclaim(pawn, move.to);
+ for (Tile tile : move.tiles)
+ revertclaim(pawn, tile);
+ claim(pawn, move.from);
+
addAnimation(RunnableAnimation.get(pawn, new Runnable() {
@Override
public void run() {
diff --git a/core/src/ch/asynk/rustanddust/engine/Objective.java b/core/src/ch/asynk/rustanddust/engine/Objective.java
deleted file mode 100644
index d10f446..0000000
--- a/core/src/ch/asynk/rustanddust/engine/Objective.java
+++ /dev/null
@@ -1,54 +0,0 @@
-package ch.asynk.rustanddust.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 persistent()
- {
- return persistent;
- }
-
- 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/rustanddust/engine/ObjectiveSet.java b/core/src/ch/asynk/rustanddust/engine/ObjectiveSet.java
deleted file mode 100644
index 61feba6..0000000
--- a/core/src/ch/asynk/rustanddust/engine/ObjectiveSet.java
+++ /dev/null
@@ -1,89 +0,0 @@
-package ch.asynk.rustanddust.engine;
-
-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 boolean isObjectiveFor(Tile tile, Pawn pawn)
- {
- Objective objective = get(tile);
- if (objective == null)
- return false;
-
- if (objective.faction() == pawn.getFaction())
- return false;
-
- return (objective.persistent());
- }
-
- 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 int modifiedCount()
- {
- return modified.size();
- }
-
- 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/rustanddust/engine/PathBuilder.java b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java
index 0f18ae0..6097d87 100644
--- a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java
+++ b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java
@@ -157,7 +157,7 @@ public class PathBuilder implements Disposable
int n = next.costFrom(pawn, o);
boolean r = next.road(o);
int f = (fitness + 1 + (r ? 0 : 1));
- if (board.isObjectiveFor(next, pawn)) f -= 4;
+ if (next.isObjectiveFor(pawn)) f -= 4;
int m = (mvtLeft - n);
r &= roadMarch;
diff --git a/core/src/ch/asynk/rustanddust/engine/Tile.java b/core/src/ch/asynk/rustanddust/engine/Tile.java
index c865b31..4bf1e29 100644
--- a/core/src/ch/asynk/rustanddust/engine/Tile.java
+++ b/core/src/ch/asynk/rustanddust/engine/Tile.java
@@ -18,6 +18,14 @@ public abstract class Tile implements Drawable, Disposable, Iterable<Pawn>
{
}
+ public enum Objective
+ {
+ NONE,
+ PERSISTENT,
+ VERSATILE,
+ FINAL
+ }
+
protected int col;
protected int row;
protected float x;
@@ -25,6 +33,10 @@ public abstract class Tile implements Drawable, Disposable, Iterable<Pawn>
private StackedImages overlays;
protected ArrayDeque<Pawn> stack;
+ protected Faction curFaction;
+ protected Faction prevFaction;
+ protected Objective objective;
+
public abstract int defense();
public abstract int exitCost();
public abstract int costFrom(Pawn pawn, Orientation side);
@@ -35,20 +47,21 @@ public abstract class Tile implements Drawable, Disposable, Iterable<Pawn>
public abstract boolean atLeastOneMove(Pawn pawn);
public abstract boolean blockLineOfSight(Tile from, Tile to);
- protected Tile(int col, int row)
+ protected Tile(int col, int row, Faction defaultFaction)
{
this.col = col;
this.row = row;
this.stack = new ArrayDeque<Pawn>();
+ this.curFaction = defaultFaction;
+ this.prevFaction = defaultFaction;
+ this.objective = Objective.NONE;
}
- public Tile(float x, float y, int col, int row, TextureAtlas atlas)
+ public Tile(float x, float y, int col, int row, TextureAtlas atlas, Faction defaultFaction)
{
- this.stack = new ArrayDeque<Pawn>();
+ this(col, row, defaultFaction);
this.x = x;
this.y = y;
- this.col = col;
- this.row = row;
this.overlays = new StackedImages(atlas);
this.overlays.centerOn(x, y);
}
@@ -76,6 +89,8 @@ public abstract class Tile implements Drawable, Disposable, Iterable<Pawn>
overlays.dispose();
}
+ // STACK
+
public boolean isEmpty()
{
return stack.isEmpty();
@@ -116,6 +131,86 @@ public abstract class Tile implements Drawable, Disposable, Iterable<Pawn>
return false;
}
+ // OBJECTIVE
+
+ public void setObjective(Faction faction, Objective objective)
+ {
+ this.curFaction = faction;
+ this.prevFaction = faction;
+ this.objective = objective;
+ }
+
+ public boolean isPersistent()
+ {
+ return ((objective == Objective.PERSISTENT) || (objective == Objective.FINAL));
+ }
+
+ public boolean isFinal()
+ {
+ return (objective == Objective.FINAL);
+ }
+
+ public Faction belongsTo()
+ {
+ return curFaction;
+ }
+
+ public boolean belongsTo(Faction faction)
+ {
+ return (faction == curFaction);
+ }
+
+ public boolean isObjective()
+ {
+ return (objective != Objective.NONE);
+ }
+
+ public boolean isOwnedObjective(Faction faction)
+ {
+ return (isObjective() && belongsTo(faction));
+ }
+
+ public boolean isObjectiveFor(Pawn pawn)
+ {
+ if (!isObjective())
+ return false;
+
+ if (belongsTo(pawn.getFaction()))
+ return false;
+
+ return isPersistent();
+ }
+
+
+ public boolean claim(Faction faction)
+ {
+ if (belongsTo(faction))
+ return false;
+
+ if (isFinal() && (curFaction != prevFaction))
+ return false;
+
+ prevFaction = curFaction;
+ curFaction = faction;
+ return true;
+ }
+
+ public boolean unclaim()
+ {
+ if (isPersistent())
+ return false;
+ revertClaim();
+ return true;
+ }
+
+ public Faction revertClaim()
+ {
+ curFaction = prevFaction;
+ return curFaction;
+ }
+
+ // OVERLAYS
+
public boolean mustBeDrawn()
{
if (!isEmpty()) return true;