diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-19 09:49:38 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-19 09:49:38 +0200 |
commit | 80b5a3ee896d450525ac129c3442f0ec792523d9 (patch) | |
tree | e76d7dd103b66cef89de6f0c57030b87810ea7c8 /core | |
parent | abab7886b47873cec83f064625e11f5eac8d205e (diff) | |
download | RustAndDust-80b5a3ee896d450525ac129c3442f0ec792523d9.zip RustAndDust-80b5a3ee896d450525ac129c3442f0ec792523d9.tar.gz |
MapImage->MapNode, PawnImage->PawnNode, add Layer
Diffstat (limited to 'core')
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/Layer.java | 119 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/Map.java | 11 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/MapNode.java (renamed from core/src/ch/asynk/tankontank/engine/MapImage.java) | 50 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/Pawn.java | 27 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/engine/PawnNode.java (renamed from core/src/ch/asynk/tankontank/engine/PawnImage.java) | 50 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/game/GameFactory.java | 6 | ||||
-rw-r--r-- | core/src/ch/asynk/tankontank/game/Unit.java | 4 |
7 files changed, 207 insertions, 60 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/Layer.java b/core/src/ch/asynk/tankontank/engine/Layer.java new file mode 100644 index 0000000..76f99c0 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/Layer.java @@ -0,0 +1,119 @@ +package ch.asynk.tankontank.engine; + +import java.util.LinkedList; +import java.util.Vector; +import java.util.Iterator; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Camera; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.utils.viewport.Viewport; + +import ch.asynk.tankontank.engine.gfx.Node; +import ch.asynk.tankontank.engine.gfx.Animation; + +public class Layer +{ + public boolean visible; + private final Viewport viewport; + private final Batch batch; + private final Vector<Animation> animations = new Vector<Animation>(5); + private final Vector<Animation> nextAnimations = new Vector<Animation>(2); + private final LinkedList<Node> nodes = new LinkedList<Node>(); + + public Layer(Viewport viewport) + { + this.visible = true; + this.viewport = viewport; + this.batch = new SpriteBatch(); + viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true); + } + + public void resize(int width, int height) + { + viewport.update(width, height); + } + + public void addNode(Node node) + { + node.setLayer(this); + nodes.add(node); + } + + public boolean removeNode(Node node) + { + node.setLayer(null); + return nodes.remove(node); + } + + public void goOnTop(Node node) + { + nodes.remove(node); + node.setLayer(this); + nodes.add(node); + } + + public void addAnimation(Animation animation) + { + nextAnimations.add(animation); + } + + public void act() + { + act(Math.min(Gdx.graphics.getDeltaTime(), (1 / 30f))); + } + + public void act(float delta) + { + Iterator<Animation> iter; + + iter = animations.iterator(); + while (iter.hasNext()) { + Animation a = iter.next(); + Node n = a.getNode(); + if (n != null) + goOnTop(n); + if (a.act(delta)) iter.remove(); + } + + for (int i = 0, n = nodes.size(); i < n; i++) + nodes.get(i).act(delta); + + for (int i = 0, n = nextAnimations.size(); i < n; i++) + animations.add(nextAnimations.get(i)); + nextAnimations.clear(); + } + + public void draw() + { + Camera camera = viewport.getCamera(); + camera.update(); + if (!visible) return; + Batch batch = this.batch; + if (batch != null) { + batch.setProjectionMatrix(camera.combined); + batch.begin(); + for (int i = 0, n = nodes.size(); i < n; i++) + nodes.get(i).draw(batch, 1); + batch.end(); + } + } + + public void clear() + { + for (int i = 0, n = nodes.size(); i < n; i++) + nodes.get(i).clear(); + nodes.clear(); + + for (int i = 0, n = animations.size(); i < n; i++) + animations.get(i).free(); + animations.clear(); + + for (int i = 0, n = nextAnimations.size(); i < n; i++) + nextAnimations.get(i).free(); + nextAnimations.clear(); + + batch.dispose(); + } +} diff --git a/core/src/ch/asynk/tankontank/engine/Map.java b/core/src/ch/asynk/tankontank/engine/Map.java index 771e2c9..7364c9d 100644 --- a/core/src/ch/asynk/tankontank/engine/Map.java +++ b/core/src/ch/asynk/tankontank/engine/Map.java @@ -4,15 +4,10 @@ import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.GridPoint2; -public interface Map -{ - // libgdx - - public float getWidth(); - public float getHeight(); - - // game +import ch.asynk.tankontank.engine.gfx.Node; +public interface Map extends Node +{ public GridPoint2 getHexAt(GridPoint2 hex, float x, float y); public Pawn getTopPawnAt(GridPoint2 hex); diff --git a/core/src/ch/asynk/tankontank/engine/MapImage.java b/core/src/ch/asynk/tankontank/engine/MapNode.java index 2b80974..10b5ad5 100644 --- a/core/src/ch/asynk/tankontank/engine/MapImage.java +++ b/core/src/ch/asynk/tankontank/engine/MapNode.java @@ -3,21 +3,25 @@ package ch.asynk.tankontank.engine; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.scenes.scene2d.ui.Image; +import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.GridPoint2; -public class MapImage extends Image implements Map +import ch.asynk.tankontank.engine.gfx.TextureDrawable; +import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; +import ch.asynk.tankontank.engine.gfx.animations.RunnableAnimation; + +public class MapNode extends TextureDrawable implements Map { + private Layer layer; private Map.Config cfg; private int cols; private int rows; private Tile[][] board; - @SuppressWarnings("unchecked") - public MapImage(Map.Config cfg, Tile[][] board, Texture texture) + public MapNode(Map.Config cfg, Tile[][] board, Texture texture) { super(texture); this.cfg = cfg; @@ -26,6 +30,24 @@ public class MapImage extends Image implements Map this.rows = cfg.rows - 1; } + @Override + public void setLayer(Layer layer) + { + this.layer = layer; + } + + @Override + public void clear() + { + dispose(); + } + + @Override + public void act(float delta) + { + } + + @Override public Pawn getTopPawnAt(GridPoint2 cell) { return getTopPawnAt(cell.x, cell.y); @@ -33,22 +55,20 @@ public class MapImage extends Image implements Map private Pawn getTopPawnAt(int col, int row) { - // if ((col < 0) || (row < 0)) throw new (); return board[row][col].getTop(); } private int pushPawnAt(Pawn pawn, int col, int row) { - // if ((col < 0) || (row < 0)) throw new (); return board[row][col].push(pawn); } private void removePawnFrom(Pawn pawn, int col, int row) { - // if ((col < 0) || (row < 0)) throw new (); board[row][col].remove(pawn); } + @Override public Vector2 getHexCenterAt(GridPoint2 cell) { float x = cfg.x0 + ((cell.x * cfg.w) + (cfg.w / 2)); @@ -57,6 +77,7 @@ public class MapImage extends Image implements Map return new Vector2(x, y); } + @Override public Vector2 getPawnPosAt(Pawn pawn, GridPoint2 cell) { return getPawnPosAt(pawn, cell.x, cell.y); @@ -70,12 +91,14 @@ public class MapImage extends Image implements Map return new Vector2(x, y); } + @Override public void movePawnTo(Pawn pawn, Vector3 coords) { GridPoint2 p = getHexAt(null, coords.x, coords.y); movePawnTo(pawn, p.x, p.y, Pawn.Orientation.KEEP); } + @Override public void setPawnAt(final Pawn pawn, final int col, final int row, Pawn.Orientation o) { int z = pushPawnAt(pawn, col, row); @@ -83,21 +106,22 @@ public class MapImage extends Image implements Map pawn.pushMove(pos.x, pos.y, z, o); } + @Override public void movePawnTo(final Pawn pawn, final int col, final int row, Pawn.Orientation o) { GridPoint2 prev = getHexAt(pawn.getLastPosition()); - // if (prev == null) throw new (); removePawnFrom(pawn, prev.x, prev.y); if ((col < 0) || (row < 0)) { - pawn.resetMoves(new Runnable() { + AnimationSequence seq = pawn.getResetMovesAnimation(); + seq.addAnimation(RunnableAnimation.get(new Runnable() { @Override public void run() { GridPoint2 hex = getHexAt(pawn.getLastPosition()); - pawn.setZIndex(pushPawnAt(pawn, hex.x, hex.y)); + pushPawnAt(pawn, hex.x, hex.y); } - }); - return; + })); + layer.addAnimation(seq); } else { int z = pushPawnAt(pawn, col, row); Vector2 pos = getPawnPosAt(pawn, col, row); @@ -111,6 +135,7 @@ public class MapImage 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(); @@ -165,3 +190,4 @@ public class MapImage extends Image implements Map return hex; } } + diff --git a/core/src/ch/asynk/tankontank/engine/Pawn.java b/core/src/ch/asynk/tankontank/engine/Pawn.java index bcd1966..3d2c782 100644 --- a/core/src/ch/asynk/tankontank/engine/Pawn.java +++ b/core/src/ch/asynk/tankontank/engine/Pawn.java @@ -2,35 +2,30 @@ package ch.asynk.tankontank.engine; import com.badlogic.gdx.math.Vector3; -public interface Pawn -{ - // libgdx - - public float getWidth(); - public float getHeight(); - public void setZIndex(int z); - - // game +import ch.asynk.tankontank.engine.gfx.Node; +import ch.asynk.tankontank.engine.gfx.animations.AnimationSequence; +public interface Pawn extends Node +{ public Vector3 getLastPosition(); public void moveBy(float x, float y); public void pushMove(float x, float y, int z, Pawn.Orientation o); - public void resetMoves(Runnable cb); + public AnimationSequence getResetMovesAnimation(); public void moveDone(); public enum Orientation { KEEP(0), - WEST(-90), - NORTH_WEST(-30), - NORTH_EAST (30), - EAST(90), - SOUTH_EAST(150), - SOUTH_WEST(-150); + WEST(180), + NORTH_WEST(120), + NORTH_EAST (60), + EAST(0), + SOUTH_EAST(-60), + SOUTH_WEST(-120); public final int v; Orientation(int v) { this.v = v; } diff --git a/core/src/ch/asynk/tankontank/engine/PawnImage.java b/core/src/ch/asynk/tankontank/engine/PawnNode.java index d092028..bf3287b 100644 --- a/core/src/ch/asynk/tankontank/engine/PawnImage.java +++ b/core/src/ch/asynk/tankontank/engine/PawnNode.java @@ -3,23 +3,42 @@ package ch.asynk.tankontank.engine; import java.util.ArrayDeque; import com.badlogic.gdx.graphics.g2d.TextureRegion; -import com.badlogic.gdx.scenes.scene2d.ui.Image; -import com.badlogic.gdx.scenes.scene2d.actions.Actions; -import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction; import com.badlogic.gdx.math.Vector3; -public class PawnImage extends Image implements Pawn +import ch.asynk.tankontank.engine.gfx.TextureRegionDrawable; +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 PawnNode extends TextureRegionDrawable implements Pawn { private static final float MOVE_TIME = 0.3f; private static final float ROTATE_TIME = 0.2f; + private Layer layer; private ArrayDeque<Vector3> path = new ArrayDeque<Vector3>(); - public PawnImage(TextureRegion region) + public PawnNode(TextureRegion region) { super(region); - setOrigin((getWidth() / 2.f), (getHeight() / 2.f)); + } + + @Override + public void setLayer(Layer layer) + { + this.layer = layer; + } + + @Override + public void clear() + { + dispose(); + } + + @Override + public void act(float delta) + { } public Vector3 getLastPosition() @@ -30,38 +49,31 @@ public class PawnImage extends Image implements Pawn public void pushMove(float x, float y, int z, Pawn.Orientation r) { - setPosition(x, y); + setCoords(x, y, z); if (r != Pawn.Orientation.KEEP) setRotation(r.v); - setZIndex(z); Vector3 v = new Vector3(x, y, r.v); if ((path.size() == 0) || (!v.equals(path.getFirst()))) path.push(new Vector3(x, y, r.v)); } - public void resetMoves(Runnable cb) + public AnimationSequence getResetMovesAnimation() { final Vector3 finalPos = path.getLast(); - SequenceAction seq = new SequenceAction(); + AnimationSequence seq = AnimationSequence.get(path.size() + 1); while(path.size() != 0) { - Vector3 v = path.pop(); - seq.addAction(Actions.moveTo(v.x, v.y, MOVE_TIME)); - if (v.z != Pawn.Orientation.KEEP.v) - seq.addAction(Actions.rotateTo(v.z, ROTATE_TIME)); + seq.addAnimation(MoveToAnimation.get(this, path.pop(), MOVE_TIME)); } - seq.addAction( Actions.run(new Runnable() { + seq.addAnimation(RunnableAnimation.get(new Runnable() { @Override public void run() { path.push(finalPos); } })); - // the map must finalize this move - seq.addAction(Actions.run(cb)); - - addAction(seq); + return seq; } public void moveDone() diff --git a/core/src/ch/asynk/tankontank/game/GameFactory.java b/core/src/ch/asynk/tankontank/game/GameFactory.java index f5c113c..8716c49 100644 --- a/core/src/ch/asynk/tankontank/game/GameFactory.java +++ b/core/src/ch/asynk/tankontank/game/GameFactory.java @@ -6,7 +6,7 @@ import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import ch.asynk.tankontank.engine.Map; -import ch.asynk.tankontank.engine.MapImage; +import ch.asynk.tankontank.engine.MapNode; import ch.asynk.tankontank.engine.Tile; public class GameFactory @@ -139,10 +139,10 @@ public class GameFactory Map m = null; switch(t) { case MAP_A: - m = new MapImage(config(), board, manager.get("images/map_a.png", Texture.class)); + m = new MapNode(config(), board, manager.get("images/map_a.png", Texture.class)); break; case MAP_B: - m = new MapImage(config(), board, manager.get("images/map_b.png", Texture.class)); + m = new MapNode(config(), board, manager.get("images/map_b.png", Texture.class)); break; } diff --git a/core/src/ch/asynk/tankontank/game/Unit.java b/core/src/ch/asynk/tankontank/game/Unit.java index 61833eb..310ead9 100644 --- a/core/src/ch/asynk/tankontank/game/Unit.java +++ b/core/src/ch/asynk/tankontank/game/Unit.java @@ -2,9 +2,9 @@ package ch.asynk.tankontank.game; import com.badlogic.gdx.graphics.g2d.TextureRegion; -import ch.asynk.tankontank.engine.PawnImage; +import ch.asynk.tankontank.engine.PawnNode; -public class Unit extends PawnImage +public class Unit extends PawnNode { public int rng; public int def; |