summaryrefslogtreecommitdiffstats
path: root/core/src
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2018-10-17 16:03:26 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2018-10-17 16:03:26 +0200
commita7522b9a4cfa9be0e0cb5f77dacd86bbf04765e7 (patch)
tree5a6a76141bf6b2323fa8af9d30ee67ad45e19cdc /core/src
parentcf85904469c49e881e5a853a1f4d6668c180513d (diff)
downloadgdx-boardgame-a7522b9a4cfa9be0e0cb5f77dacd86bbf04765e7.zip
gdx-boardgame-a7522b9a4cfa9be0e0cb5f77dacd86bbf04765e7.tar.gz
add MoveAnimation
Diffstat (limited to 'core/src')
-rw-r--r--core/src/ch/asynk/gdx/boardgame/animations/MoveAnimation.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/animations/MoveAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/MoveAnimation.java
new file mode 100644
index 0000000..27de101
--- /dev/null
+++ b/core/src/ch/asynk/gdx/boardgame/animations/MoveAnimation.java
@@ -0,0 +1,111 @@
+package ch.asynk.gdx.boardgame.animations;
+
+import java.lang.Math;
+
+import com.badlogic.gdx.utils.Pool;
+import com.badlogic.gdx.graphics.g2d.Batch;
+import com.badlogic.gdx.math.Vector3;
+
+import ch.asynk.gdx.boardgame.pieces.Piece;
+import ch.asynk.gdx.boardgame.Path;
+
+public class MoveAnimation implements Animation, Pool.Poolable
+{
+ private static final Pool<MoveAnimation> moveAnimationPool = new Pool<MoveAnimation>()
+ {
+ @Override protected MoveAnimation newObject()
+ {
+ return new MoveAnimation();
+ }
+ };
+
+ public static MoveAnimation obtain(Piece piece, Path path, float speed)
+ {
+ MoveAnimation a = moveAnimationPool.obtain();
+
+ a.piece = piece;
+ a.path = path;
+ a.speed = speed;
+
+ a.init();
+
+ return a;
+ }
+
+ private Piece piece;
+ private Path path;
+ private float speed;
+ private float dp;
+ private float percent;
+ private Vector3 dst = new Vector3();
+ private Vector3 dt = new Vector3();
+
+ private MoveAnimation()
+ {
+ }
+
+ private void init()
+ {
+ path.iterator();
+ setNextMove();
+ percent = 0f;
+ dp = 1f * speed;
+ }
+
+ private boolean setNextMove()
+ {
+ boolean done = path.nextVector(piece, dst);
+
+ float dr = (dst.z - piece.getRotation());
+ if (dr > 180) {
+ dr -= 360;
+ } else if (dr < -180) {
+ dr += 360;
+ }
+
+ dt.set(
+ (dst.x - piece.getX()) * speed,
+ (dst.y - piece.getY()) * speed,
+ (dr) * speed
+ );
+ return done;
+ }
+
+ @Override public void reset()
+ {
+ this.path = null;
+ this.piece = null;
+ this.dst.set(0, 0, 0);
+ }
+
+ @Override public void dispose()
+ {
+ moveAnimationPool.free(this);
+ }
+
+ @Override public boolean completed()
+ {
+ return (percent >= 1f);
+ }
+
+ @Override public boolean animate(float delta)
+ {
+ piece.translate(dt.x * delta, dt.y * delta);
+ piece.rotate(dt.z * delta);
+
+ percent += (dp * delta);
+ if (percent >= 1f) {
+ piece.setPosition(dst.x, dst.y, dst.z);
+ if (!setNextMove()) {
+ percent = 0f;
+ }
+ }
+
+ return (percent >= 1f);
+ }
+
+ @Override public void draw(Batch batch)
+ {
+ piece.draw(batch);
+ }
+}