diff options
Diffstat (limited to 'core/src/ch/asynk/gdx')
-rw-r--r-- | core/src/ch/asynk/gdx/boardgame/animations/MoveAnimation.java | 111 |
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); + } +} |