diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-21 10:04:46 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-21 10:04:46 +0200 |
commit | 8d43e91dcf005cf076fb3cd571f6125df5837c8d (patch) | |
tree | 2dcf066b4cd50acd25140f865be4b07f375e484a /core | |
parent | e9f641b93dd669cc609c23b105acfaf6369a6e04 (diff) | |
download | RustAndDust-8d43e91dcf005cf076fb3cd571f6125df5837c8d.zip RustAndDust-8d43e91dcf005cf076fb3cd571f6125df5837c8d.tar.gz |
MapNode->Board TileOverlays->Tile PawnImage->Pawn
Diffstat (limited to 'core')
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/Board.java (renamed from core/src/ch/asynk/tankontank/engine/MapNode.java) | 60 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/Map.java | 47 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/Pawn.java | 72 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/PawnImage.java | 70 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/Tile.java | 101 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/TileOverlays.java | 101 |
6 files changed, 163 insertions, 288 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/MapNode.java b/core/src/ch/asynk/tankontank/engine/Board.java index bf9928a..16a27d2 100644 --- a/core/src/ch/asynk/tankontank/engine/MapNode.java +++ b/core/src/ch/asynk/tankontank/engine/Board.java @@ -4,7 +4,7 @@ import java.util.Vector; import java.util.Iterator; import java.util.LinkedHashSet; -import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Batch; @@ -20,9 +20,23 @@ import ch.asynk.tankontank.engine.gfx.Animation; import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation; -public class MapNode extends Image implements Map +public abstract class Board extends Image implements Disposable { - private Map.Config cfg; + public static class Config + { + public int cols; + public int rows; + public int x0; // bottom left x offset + public int y0; // bottom left y offset + public int w; // hex width + public int dw; // half hex : w/2 + public int s; // hex side + public float dh; // hex top : s/2 + public float h; // square height : s + dh + public float slope; // north-west side slope : (dh / (float) dw) + } + + private Config cfg; private int cols; private int rows; private Tile[][] board; @@ -31,15 +45,12 @@ public class MapNode extends Image implements Map private Matrix4 prevTransform; private Matrix4 nextTransform; - private Pawn currentPawn; - private GridPoint2 currentHex = new GridPoint2(-1, -1); - private final Vector<Animation> animations = new Vector<Animation>(2); private final Vector<Animation> nextAnimations = new Vector<Animation>(2); private final LinkedHashSet<Tile> tilesToDraw = new LinkedHashSet<Tile>(); private final LinkedHashSet<Pawn> pawnsToDraw = new LinkedHashSet<Pawn>(); - public MapNode(Map.Config cfg, Tile[][] board, Texture texture) + public Board(Config cfg, Tile[][] board, Texture texture) { super(texture); this.cfg = cfg; @@ -67,7 +78,6 @@ public class MapNode extends Image implements Map nextAnimations.add(seq); } - @Override public void animate(float delta) { Iterator<Animation> iter = animations.iterator(); @@ -126,7 +136,6 @@ public class MapNode extends Image implements Map debugShapes.setTransformMatrix(prevTransform); } - @Override public Pawn getTopPawnAt(GridPoint2 cell) { return getTopPawnAt(cell.x, cell.y); @@ -153,7 +162,6 @@ public class MapNode extends Image implements Map return n; } - @Override public Vector2 getHexCenterAt(GridPoint2 cell) { float x = cfg.x0 + ((cell.x * cfg.w) + (cfg.w / 2)); @@ -162,7 +170,6 @@ public class MapNode extends Image implements Map return new Vector2(x, y); } - @Override public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 cell) { return getPawnPosAt(pawn, cell.x, cell.y); @@ -176,7 +183,6 @@ public class MapNode extends Image implements Map return new Vector2(x, y); } - @Override public void setPawnAt(final Pawn pawn, final int col, final int row, Pawn.Orientation o) { Vector2 pos = getPawnPosAt(pawn, col, row); @@ -184,20 +190,17 @@ public class MapNode extends Image implements Map pushPawnAt(pawn, col, row); } - @Override public void movePawnTo(Pawn pawn, Vector3 coords) { GridPoint2 hex = getHexAt(null, coords.x, coords.y); movePawnTo(pawn, hex.x, hex.y, Pawn.Orientation.KEEP); } - @Override public void movePawnTo(Pawn pawn, GridPoint2 hex) { movePawnTo(pawn, hex.x, hex.y, Pawn.Orientation.KEEP); } - @Override public void movePawnTo(final Pawn pawn, final int col, final int row, Pawn.Orientation o) { GridPoint2 prev = getHexAt(pawn.getLastPosition()); @@ -226,7 +229,6 @@ public class MapNode extends Image implements Map return getHexAt(null, v.x, v.y); } - @Override public GridPoint2 getHexAt(GridPoint2 hex, float cx, float cy) { if (hex == null) hex = new GridPoint2(); @@ -280,31 +282,5 @@ public class MapNode extends Image implements Map return hex; } - - @Override - public boolean drag(float dx, float dy) - { - if (currentPawn == null) return false; - currentPawn.translate(dx, dy); - return true; - } - - @Override - public void touchDown(float x, float y) - { - getHexAt(currentHex, x, y); - if (currentHex.x != -1) { - currentPawn = getTopPawnAt(currentHex); - } - } - - @Override - public void touchUp(float x, float y) - { - getHexAt(currentHex, x, y); - if (currentPawn != null) { - movePawnTo(currentPawn, currentHex); - } - } } diff --git a/core/src/ch/asynk/tankontank/engine/Map.java b/core/src/ch/asynk/tankontank/engine/Map.java deleted file mode 100644 index 253edf0..0000000 --- a/core/src/ch/asynk/tankontank/engine/Map.java +++ /dev/null @@ -1,47 +0,0 @@ -package ch.asynk.tankontank.engine; - -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.math.GridPoint2; -import com.badlogic.gdx.utils.Disposable; - -import ch.asynk.tankontank.engine.gfx.Drawable; - -public interface Map extends Drawable, Disposable -{ - public void animate(float delta); - - public GridPoint2 getHexAt(GridPoint2 hex, float x, float y); - - public Pawn getTopPawnAt(GridPoint2 hex); - - public Vector2 getHexCenterAt(GridPoint2 hex); - - public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 hex); - - public void setPawnAt(Pawn pawn, int col, int row, Pawn.Orientation o); - - public void movePawnTo(Pawn pawn, Vector3 coords); - - public void movePawnTo(Pawn pawn, GridPoint2 hex); - - public void movePawnTo(Pawn pawn, int col, int row, Pawn.Orientation o); - - public class Config - { - public int cols; - public int rows; - public int x0; // bottom left x offset - public int y0; // bottom left y offset - public int w; // hex width - public int dw; // half hex : w/2 - public int s; // hex side - public float dh; // hex top : s/2 - public float h; // square height : s + dh - public float slope; // north-west side slope : (dh / (float) dw) - } - - public void touchUp(float x, float y); - public void touchDown(float x, float y); - public boolean drag(float dx, float dy); -} diff --git a/core/src/ch/asynk/tankontank/engine/Pawn.java b/core/src/ch/asynk/tankontank/engine/Pawn.java index 0198302..204bc97 100644 --- a/core/src/ch/asynk/tankontank/engine/Pawn.java +++ b/core/src/ch/asynk/tankontank/engine/Pawn.java @@ -1,21 +1,20 @@ package ch.asynk.tankontank.engine; -import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.utils.Disposable; - -import ch.asynk.tankontank.engine.gfx.Drawable; -import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; +import java.util.ArrayDeque; -public interface Pawn extends Drawable, Disposable -{ - public Vector3 getLastPosition(); +import com.badlogic.gdx.utils.Disposable; - public void moveDone(); +import com.badlogic.gdx.graphics.g2d.TextureRegion; - public void pushMove(float x, float y, Pawn.Orientation o); +import com.badlogic.gdx.math.Vector3; - public AnimationSequence getResetMovesAnimation(); +import ch.asynk.tankontank.engine.gfx.Image; +import ch.asynk.tankontank.engine.gfx.animations.MoveToAnimation; +import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation; +import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; +public abstract class Pawn extends Image implements Disposable +{ public enum Orientation { KEEP(0), @@ -29,4 +28,55 @@ public interface Pawn extends Drawable, Disposable public final int v; Orientation(int v) { this.v = v; } } + + private static final float MOVE_TIME = 0.3f; + + private ArrayDeque<Vector3> path = new ArrayDeque<Vector3>(); + + public Pawn(TextureRegion region) + { + super(region); + } + + public Vector3 getLastPosition() + { + if ((path == null) || (path.size() == 0)) return null; + return path.getFirst(); + } + + public void moveDone() + { + Vector3 v = path.pop(); + path.clear(); + path.push(v); + } + + public void pushMove(float x, float y, Pawn.Orientation o) + { + float r = ((o == Pawn.Orientation.KEEP) ? getRotation() : o.v); + setPosition(x, y, r); + Vector3 v = new Vector3(x, y, r); + if ((path.size() == 0) || (!v.equals(path.getFirst()))) + path.push(new Vector3(x, y, r)); + } + + public AnimationSequence getResetMovesAnimation() + { + final Vector3 finalPos = path.getLast(); + + AnimationSequence seq = AnimationSequence.get(path.size() + 1); + + while(path.size() != 0) { + seq.addAnimation(MoveToAnimation.get(this, path.pop(), MOVE_TIME)); + } + + seq.addAnimation(RunnableAnimation.get(this, new Runnable() { + @Override + public void run() { + path.push(finalPos); + } + })); + + return seq; + } } diff --git a/core/src/ch/asynk/tankontank/engine/PawnImage.java b/core/src/ch/asynk/tankontank/engine/PawnImage.java deleted file mode 100644 index 4a16cd5..0000000 --- a/core/src/ch/asynk/tankontank/engine/PawnImage.java +++ /dev/null @@ -1,70 +0,0 @@ -package ch.asynk.tankontank.engine; - -import java.util.ArrayDeque; - -import com.badlogic.gdx.graphics.g2d.TextureRegion; - -import com.badlogic.gdx.math.Vector3; - -import ch.asynk.tankontank.engine.gfx.Image; -import ch.asynk.tankontank.engine.gfx.animations.MoveToAnimation; -import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation; -import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; - -public class PawnImage extends Image implements Pawn -{ - private static final float MOVE_TIME = 0.3f; - - private ArrayDeque<Vector3> path = new ArrayDeque<Vector3>(); - - public PawnImage(TextureRegion region) - { - super(region); - } - - @Override - public Vector3 getLastPosition() - { - if ((path == null) || (path.size() == 0)) return null; - return path.getFirst(); - } - - @Override - public void moveDone() - { - Vector3 v = path.pop(); - path.clear(); - path.push(v); - } - - @Override - public void pushMove(float x, float y, Pawn.Orientation o) - { - float r = ((o == Pawn.Orientation.KEEP) ? getRotation() : o.v); - setPosition(x, y, r); - Vector3 v = new Vector3(x, y, r); - if ((path.size() == 0) || (!v.equals(path.getFirst()))) - path.push(new Vector3(x, y, r)); - } - - @Override - public AnimationSequence getResetMovesAnimation() - { - final Vector3 finalPos = path.getLast(); - - AnimationSequence seq = AnimationSequence.get(path.size() + 1); - - while(path.size() != 0) { - seq.addAnimation(MoveToAnimation.get(this, path.pop(), MOVE_TIME)); - } - - seq.addAnimation(RunnableAnimation.get(this, new Runnable() { - @Override - public void run() { - path.push(finalPos); - } - })); - - return seq; - } -} diff --git a/core/src/ch/asynk/tankontank/engine/Tile.java b/core/src/ch/asynk/tankontank/engine/Tile.java index 7c43bd4..f7122fe 100644 --- a/core/src/ch/asynk/tankontank/engine/Tile.java +++ b/core/src/ch/asynk/tankontank/engine/Tile.java @@ -1,27 +1,17 @@ package ch.asynk.tankontank.engine; import java.util.List; +import java.util.ArrayDeque; + +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import ch.asynk.tankontank.engine.gfx.BasicDrawable; +import ch.asynk.tankontank.engine.gfx.StackedImages; -public interface Tile extends BasicDrawable +public abstract class Tile implements BasicDrawable { - public int push(Pawn pawn); - - public int remove(Pawn pawn); - - public Pawn getTopPawn(); - - public boolean mustBeDrawn(); - - public boolean occupied(); - - public boolean hasOverlayEnabled(); - - public void enableOverlay(int i, boolean enable); - - public List<Tile> adjacents(); - public enum Side { WEST(1), @@ -34,4 +24,81 @@ public interface Tile extends BasicDrawable public final int v; Side(int v) { this.v = v; } } + + private StackedImages overlays; + private ArrayDeque<Pawn> stack; + + public Tile(TextureAtlas atlas) + { + this.stack = null; + this.overlays = new StackedImages(atlas); + } + + public int push(Pawn pawn) + { + if (stack == null) stack = new ArrayDeque<Pawn>(); + stack.push(pawn); + return stack.size(); + } + + public int remove(Pawn pawn) + { + stack.remove(pawn); + return stack.size(); + } + + public Pawn getTopPawn() + { + if ((stack == null) || (stack.size() == 0)) return null; + return stack.getFirst(); + } + + public boolean mustBeDrawn() + { + if (occupied()) return true; + return hasOverlayEnabled(); + } + + public boolean occupied() + { + return (stack.size() != 0); + } + + public boolean hasOverlayEnabled() + { + return overlays.isEnabled(); + } + + public void enableOverlay(int i, boolean enable) + { + overlays.enable(i, enable); + } + + public List<Tile> adjacents() + { + // FIXME + System.err.println("adjacents() Not implemented yet"); + return null; + } + + public void setPosition(float x, float y, float z) + { + overlays.setPosition(x, y, z); + } + + public void draw(Batch batch, float parentAlpha) + { + overlays.draw(batch, parentAlpha); + Pawn pawn = getTopPawn(); + if (pawn != null) + pawn.draw(batch, parentAlpha); + } + + public void drawDebug(ShapeRenderer debugShapes) + { + overlays.drawDebug(debugShapes); + Pawn pawn = getTopPawn(); + if (pawn != null) + pawn.drawDebug(debugShapes); + } } diff --git a/core/src/ch/asynk/tankontank/engine/TileOverlays.java b/core/src/ch/asynk/tankontank/engine/TileOverlays.java deleted file mode 100644 index 48d755d..0000000 --- a/core/src/ch/asynk/tankontank/engine/TileOverlays.java +++ /dev/null @@ -1,101 +0,0 @@ -package ch.asynk.tankontank.engine; - -import java.util.List; -import java.util.ArrayDeque; - -import com.badlogic.gdx.graphics.g2d.Batch; -import com.badlogic.gdx.graphics.g2d.TextureAtlas; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; - -import ch.asynk.tankontank.engine.gfx.StackedImages; - -public abstract class TileOverlays implements Tile -{ - private StackedImages overlays; - private ArrayDeque<Pawn> stack; - - public TileOverlays(TextureAtlas atlas) - { - this.stack = null; - this.overlays = new StackedImages(atlas); - } - - @Override - public int push(Pawn pawn) - { - if (stack == null) stack = new ArrayDeque<Pawn>(); - stack.push(pawn); - return stack.size(); - } - - @Override - public int remove(Pawn pawn) - { - stack.remove(pawn); - return stack.size(); - } - - @Override - public Pawn getTopPawn() - { - if ((stack == null) || (stack.size() == 0)) return null; - return stack.getFirst(); - } - - @Override - public boolean mustBeDrawn() - { - if (occupied()) return true; - return hasOverlayEnabled(); - } - - @Override - public boolean occupied() - { - return (stack.size() != 0); - } - - @Override - public boolean hasOverlayEnabled() - { - return overlays.isEnabled(); - } - - @Override - public void enableOverlay(int i, boolean enable) - { - overlays.enable(i, enable); - } - - @Override - public List<Tile> adjacents() - { - // FIXME - System.err.println("adjacents() Not implemented yet"); - return null; - } - - @Override - public void setPosition(float x, float y, float z) - { - overlays.setPosition(x, y, z); - } - - @Override - public void draw(Batch batch, float parentAlpha) - { - overlays.draw(batch, parentAlpha); - Pawn pawn = getTopPawn(); - if (pawn != null) - pawn.draw(batch, parentAlpha); - } - - @Override - public void drawDebug(ShapeRenderer debugShapes) - { - overlays.drawDebug(debugShapes); - Pawn pawn = getTopPawn(); - if (pawn != null) - pawn.drawDebug(debugShapes); - } -} |