From 8d43e91dcf005cf076fb3cd571f6125df5837c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 21 Sep 2014 10:04:46 +0200 Subject: MapNode->Board TileOverlays->Tile PawnImage->Pawn --- core/src/ch/asynk/tankontank/engine/Board.java | 286 +++++++++++++++++++ core/src/ch/asynk/tankontank/engine/Map.java | 47 ---- core/src/ch/asynk/tankontank/engine/MapNode.java | 310 --------------------- core/src/ch/asynk/tankontank/engine/Pawn.java | 72 ++++- core/src/ch/asynk/tankontank/engine/PawnImage.java | 70 ----- core/src/ch/asynk/tankontank/engine/Tile.java | 101 +++++-- .../ch/asynk/tankontank/engine/TileOverlays.java | 101 ------- 7 files changed, 431 insertions(+), 556 deletions(-) create mode 100644 core/src/ch/asynk/tankontank/engine/Board.java delete mode 100644 core/src/ch/asynk/tankontank/engine/Map.java delete mode 100644 core/src/ch/asynk/tankontank/engine/MapNode.java delete mode 100644 core/src/ch/asynk/tankontank/engine/PawnImage.java delete mode 100644 core/src/ch/asynk/tankontank/engine/TileOverlays.java diff --git a/core/src/ch/asynk/tankontank/engine/Board.java b/core/src/ch/asynk/tankontank/engine/Board.java new file mode 100644 index 0000000..16a27d2 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/Board.java @@ -0,0 +1,286 @@ +package ch.asynk.tankontank.engine; + +import java.util.Vector; +import java.util.Iterator; +import java.util.LinkedHashSet; + +import com.badlogic.gdx.utils.Disposable; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; + +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.math.GridPoint2; +import com.badlogic.gdx.math.Matrix4; + +import ch.asynk.tankontank.engine.gfx.Image; +import ch.asynk.tankontank.engine.gfx.Animation; +import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; +import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation; + +public abstract class Board extends Image implements Disposable +{ + 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; + + boolean transform; + private Matrix4 prevTransform; + private Matrix4 nextTransform; + + private final Vector animations = new Vector(2); + private final Vector nextAnimations = new Vector(2); + private final LinkedHashSet tilesToDraw = new LinkedHashSet(); + private final LinkedHashSet pawnsToDraw = new LinkedHashSet(); + + public Board(Config cfg, Tile[][] board, Texture texture) + { + super(texture); + this.cfg = cfg; + this.board = board; + this.cols = cfg.cols - 1; + this.rows = cfg.rows - 1; + } + + @Override + public void setPosition(float x, float y) + { + super.setPosition(x, y); + if ((x != 0.0f) || (y != 0.0f)) { + transform = true; + prevTransform = new Matrix4(); + nextTransform = new Matrix4(); + nextTransform.translate(x, y, 0); + } else + transform = false; + } + + private void addPawnAnimation(Pawn pawn, AnimationSequence seq) + { + pawnsToDraw.add(pawn); + nextAnimations.add(seq); + } + + public void animate(float delta) + { + Iterator iter = animations.iterator(); + while (iter.hasNext()) { + Animation a = iter.next(); + Pawn p = a.getPawn(); + if (a.animate(delta)) { + iter.remove(); + pawnsToDraw.remove(p); + } + } + + for (int i = 0, n = nextAnimations.size(); i < n; i++) + animations.add(nextAnimations.get(i)); + nextAnimations.clear(); + } + + @Override + public void draw(Batch batch, float parentAlpha) + { + super.draw(batch, parentAlpha); + + if (transform) { + prevTransform.set(batch.getTransformMatrix()); + batch.setTransformMatrix(nextTransform); + } + + Iterator tileIter = tilesToDraw.iterator(); + while (tileIter.hasNext()) { + tileIter.next().draw(batch, parentAlpha); + } + + Iterator pawnIter = pawnsToDraw.iterator(); + while (pawnIter.hasNext()) { + pawnIter.next().draw(batch, parentAlpha); + } + + if (transform) + batch.setTransformMatrix(prevTransform); + } + + @Override + public void drawDebug(ShapeRenderer debugShapes) + { + if (transform) { + prevTransform.set(debugShapes.getTransformMatrix()); + debugShapes.setTransformMatrix(nextTransform); + } + + Iterator iter = tilesToDraw.iterator(); + while (iter.hasNext()) { + iter.next().drawDebug(debugShapes); + } + + if (transform) + debugShapes.setTransformMatrix(prevTransform); + } + + public Pawn getTopPawnAt(GridPoint2 cell) + { + return getTopPawnAt(cell.x, cell.y); + } + + private Pawn getTopPawnAt(int col, int row) + { + return board[row][col].getTopPawn(); + } + + private int pushPawnAt(Pawn pawn, int col, int row) + { + Tile tile = board[row][col]; + tilesToDraw.add(tile); + return tile.push(pawn); + } + + private int removePawnFrom(Pawn pawn, int col, int row) + { + Tile tile = board[row][col]; + int n = tile.remove(pawn); + if (!tile.mustBeDrawn()) + tilesToDraw.remove(tile); + return n; + } + + public Vector2 getHexCenterAt(GridPoint2 cell) + { + float x = cfg.x0 + ((cell.x * cfg.w) + (cfg.w / 2)); + float y = cfg.y0 + ((cell.y * cfg.h) + (cfg.s / 2)); + if ((cell.y % 2) == 1) x += cfg.dw; + return new Vector2(x, y); + } + + public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 cell) + { + return getPawnPosAt(pawn, cell.x, cell.y); + } + + private Vector2 getPawnPosAt(Pawn pawn, int col, int row) + { + float x = cfg.x0 + ((col * cfg.w) + ((cfg.w - pawn.getHeight()) / 2)); + float y = cfg.y0 + ((row * cfg.h) + ((cfg.s - pawn.getWidth()) / 2)); + if ((row % 2) == 1) x += cfg.dw; + return new Vector2(x, y); + } + + public void setPawnAt(final Pawn pawn, final int col, final int row, Pawn.Orientation o) + { + Vector2 pos = getPawnPosAt(pawn, col, row); + pawn.pushMove(pos.x, pos.y, o); + pushPawnAt(pawn, col, row); + } + + public void movePawnTo(Pawn pawn, Vector3 coords) + { + GridPoint2 hex = getHexAt(null, coords.x, coords.y); + movePawnTo(pawn, hex.x, hex.y, Pawn.Orientation.KEEP); + } + + public void movePawnTo(Pawn pawn, GridPoint2 hex) + { + movePawnTo(pawn, hex.x, hex.y, Pawn.Orientation.KEEP); + } + + public void movePawnTo(final Pawn pawn, final int col, final int row, Pawn.Orientation o) + { + GridPoint2 prev = getHexAt(pawn.getLastPosition()); + removePawnFrom(pawn, prev.x, prev.y); + + if ((col < 0) || (row < 0)) { + AnimationSequence seq = pawn.getResetMovesAnimation(); + seq.addAnimation(RunnableAnimation.get(pawn, new Runnable() { + @Override + public void run() { + GridPoint2 hex = getHexAt(pawn.getLastPosition()); + pushPawnAt(pawn, hex.x, hex.y); + } + })); + addPawnAnimation(pawn, seq); + } else { + pushPawnAt(pawn, col, row); + Vector2 pos = getPawnPosAt(pawn, col, row); + pawn.pushMove(pos.x, pos.y, o); + } + } + + private GridPoint2 getHexAt(Vector3 v) + { + if (v == null) return null; + return getHexAt(null, v.x, v.y); + } + + public GridPoint2 getHexAt(GridPoint2 hex, float cx, float cy) + { + if (hex == null) hex = new GridPoint2(); + + // compute row + int row; + boolean oddRow = true; + float y = (cy - cfg.y0); + if (y < 0.f) { + row = -1; + } else { + row = (int) (y / cfg.h); + oddRow = ((row % 2) == 1); + } + + // compute col + int col; + float x = (cx - cfg.x0); + if (oddRow) x -= cfg.dw; + if (x < 0.f) { + col = -1; + } else { + col = (int) (x / cfg.w); + } + + // check upper boundaries + float dy = (y - (row * cfg.h)); + if (dy > cfg.s) { + dy -= cfg.s; + float dx = (x - (col * cfg.w)); + if (dx < cfg.dw) { + if ((dx * cfg.slope) < dy) { + row += 1; + if (!oddRow) col -= 1; + oddRow = !oddRow; + } + } else { + if (((cfg.w - dx) * cfg.slope) < dy) { + row += 1; + if (oddRow) col += 1; + oddRow = !oddRow; + } + } + } + + // validate hex + if ((col < 0) || (row < 0) || (row > rows) || (col > cols) || (oddRow && ((col + 1)> cols))) + hex.set(-1, -1); + else + hex.set(col, row); + + return hex; + } +} + 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/MapNode.java b/core/src/ch/asynk/tankontank/engine/MapNode.java deleted file mode 100644 index bf9928a..0000000 --- a/core/src/ch/asynk/tankontank/engine/MapNode.java +++ /dev/null @@ -1,310 +0,0 @@ -package ch.asynk.tankontank.engine; - -import java.util.Vector; -import java.util.Iterator; -import java.util.LinkedHashSet; - -import com.badlogic.gdx.Gdx; - -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Batch; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; - -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.math.GridPoint2; -import com.badlogic.gdx.math.Matrix4; - -import ch.asynk.tankontank.engine.gfx.Image; -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 -{ - private Map.Config cfg; - private int cols; - private int rows; - private Tile[][] board; - - boolean transform; - private Matrix4 prevTransform; - private Matrix4 nextTransform; - - private Pawn currentPawn; - private GridPoint2 currentHex = new GridPoint2(-1, -1); - - private final Vector animations = new Vector(2); - private final Vector nextAnimations = new Vector(2); - private final LinkedHashSet tilesToDraw = new LinkedHashSet(); - private final LinkedHashSet pawnsToDraw = new LinkedHashSet(); - - public MapNode(Map.Config cfg, Tile[][] board, Texture texture) - { - super(texture); - this.cfg = cfg; - this.board = board; - this.cols = cfg.cols - 1; - this.rows = cfg.rows - 1; - } - - @Override - public void setPosition(float x, float y) - { - super.setPosition(x, y); - if ((x != 0.0f) || (y != 0.0f)) { - transform = true; - prevTransform = new Matrix4(); - nextTransform = new Matrix4(); - nextTransform.translate(x, y, 0); - } else - transform = false; - } - - private void addPawnAnimation(Pawn pawn, AnimationSequence seq) - { - pawnsToDraw.add(pawn); - nextAnimations.add(seq); - } - - @Override - public void animate(float delta) - { - Iterator iter = animations.iterator(); - while (iter.hasNext()) { - Animation a = iter.next(); - Pawn p = a.getPawn(); - if (a.animate(delta)) { - iter.remove(); - pawnsToDraw.remove(p); - } - } - - for (int i = 0, n = nextAnimations.size(); i < n; i++) - animations.add(nextAnimations.get(i)); - nextAnimations.clear(); - } - - @Override - public void draw(Batch batch, float parentAlpha) - { - super.draw(batch, parentAlpha); - - if (transform) { - prevTransform.set(batch.getTransformMatrix()); - batch.setTransformMatrix(nextTransform); - } - - Iterator tileIter = tilesToDraw.iterator(); - while (tileIter.hasNext()) { - tileIter.next().draw(batch, parentAlpha); - } - - Iterator pawnIter = pawnsToDraw.iterator(); - while (pawnIter.hasNext()) { - pawnIter.next().draw(batch, parentAlpha); - } - - if (transform) - batch.setTransformMatrix(prevTransform); - } - - @Override - public void drawDebug(ShapeRenderer debugShapes) - { - if (transform) { - prevTransform.set(debugShapes.getTransformMatrix()); - debugShapes.setTransformMatrix(nextTransform); - } - - Iterator iter = tilesToDraw.iterator(); - while (iter.hasNext()) { - iter.next().drawDebug(debugShapes); - } - - if (transform) - debugShapes.setTransformMatrix(prevTransform); - } - - @Override - public Pawn getTopPawnAt(GridPoint2 cell) - { - return getTopPawnAt(cell.x, cell.y); - } - - private Pawn getTopPawnAt(int col, int row) - { - return board[row][col].getTopPawn(); - } - - private int pushPawnAt(Pawn pawn, int col, int row) - { - Tile tile = board[row][col]; - tilesToDraw.add(tile); - return tile.push(pawn); - } - - private int removePawnFrom(Pawn pawn, int col, int row) - { - Tile tile = board[row][col]; - int n = tile.remove(pawn); - if (!tile.mustBeDrawn()) - tilesToDraw.remove(tile); - return n; - } - - @Override - public Vector2 getHexCenterAt(GridPoint2 cell) - { - float x = cfg.x0 + ((cell.x * cfg.w) + (cfg.w / 2)); - float y = cfg.y0 + ((cell.y * cfg.h) + (cfg.s / 2)); - if ((cell.y % 2) == 1) x += cfg.dw; - return new Vector2(x, y); - } - - @Override - public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 cell) - { - return getPawnPosAt(pawn, cell.x, cell.y); - } - - private Vector2 getPawnPosAt(Pawn pawn, int col, int row) - { - float x = cfg.x0 + ((col * cfg.w) + ((cfg.w - pawn.getHeight()) / 2)); - float y = cfg.y0 + ((row * cfg.h) + ((cfg.s - pawn.getWidth()) / 2)); - if ((row % 2) == 1) x += cfg.dw; - 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); - pawn.pushMove(pos.x, pos.y, o); - 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()); - removePawnFrom(pawn, prev.x, prev.y); - - if ((col < 0) || (row < 0)) { - AnimationSequence seq = pawn.getResetMovesAnimation(); - seq.addAnimation(RunnableAnimation.get(pawn, new Runnable() { - @Override - public void run() { - GridPoint2 hex = getHexAt(pawn.getLastPosition()); - pushPawnAt(pawn, hex.x, hex.y); - } - })); - addPawnAnimation(pawn, seq); - } else { - pushPawnAt(pawn, col, row); - Vector2 pos = getPawnPosAt(pawn, col, row); - pawn.pushMove(pos.x, pos.y, o); - } - } - - private GridPoint2 getHexAt(Vector3 v) - { - if (v == null) return null; - return getHexAt(null, v.x, v.y); - } - - @Override - public GridPoint2 getHexAt(GridPoint2 hex, float cx, float cy) - { - if (hex == null) hex = new GridPoint2(); - - // compute row - int row; - boolean oddRow = true; - float y = (cy - cfg.y0); - if (y < 0.f) { - row = -1; - } else { - row = (int) (y / cfg.h); - oddRow = ((row % 2) == 1); - } - - // compute col - int col; - float x = (cx - cfg.x0); - if (oddRow) x -= cfg.dw; - if (x < 0.f) { - col = -1; - } else { - col = (int) (x / cfg.w); - } - - // check upper boundaries - float dy = (y - (row * cfg.h)); - if (dy > cfg.s) { - dy -= cfg.s; - float dx = (x - (col * cfg.w)); - if (dx < cfg.dw) { - if ((dx * cfg.slope) < dy) { - row += 1; - if (!oddRow) col -= 1; - oddRow = !oddRow; - } - } else { - if (((cfg.w - dx) * cfg.slope) < dy) { - row += 1; - if (oddRow) col += 1; - oddRow = !oddRow; - } - } - } - - // validate hex - if ((col < 0) || (row < 0) || (row > rows) || (col > cols) || (oddRow && ((col + 1)> cols))) - hex.set(-1, -1); - else - hex.set(col, row); - - 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/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 path = new ArrayDeque(); + + 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 path = new ArrayDeque(); - - 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 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 stack; + + public Tile(TextureAtlas atlas) + { + this.stack = null; + this.overlays = new StackedImages(atlas); + } + + public int push(Pawn pawn) + { + if (stack == null) stack = new ArrayDeque(); + 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 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 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(); - 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 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); - } -} -- cgit v1.1-2-g2b99