From e612a7e78883c91b008d97585f1e4f73d773d490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Thu, 11 Oct 2018 17:47:25 +0200 Subject: BounceAnimation : support optional rotation movement --- .../gdx/boardgame/animations/BounceAnimation.java | 30 +++++++++++++++++++--- .../asynk/gdx/boardgame/test/AnimationsScreen.java | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java index 22aa470..271728c 100644 --- a/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java +++ b/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java @@ -18,29 +18,43 @@ public class BounceAnimation extends TimedAnimation } }; - public static BounceAnimation get(Piece piece, float duration, float bounceFactor) + public static BounceAnimation get(Piece piece, float duration, float bounceFactor, int rotations) { BounceAnimation a = bounceAnimationPool.obtain(); a.piece = piece; a.bounceFactor = bounceFactor; a.setDuration(duration); + a.computeRotationDegrees(rotations); return a; } private Piece piece; + private float startScale; + private float startRotation; private float bounceFactor; + private float rotationDegrees; private BounceAnimation() { } - public BounceAnimation(Piece piece, float duration, float bounceFactor) + public BounceAnimation(Piece piece, float duration, float bounceFactor, int rotations) { this.piece = piece; this.bounceFactor = bounceFactor; this.setDuration(duration); + this.computeRotationDegrees(rotations); + } + + private void computeRotationDegrees(int rotations) + { + if (rotations == 0) { + rotationDegrees = 0f; + } else { + rotationDegrees = (360 * rotations); + } } @Override public void dispose() @@ -50,16 +64,24 @@ public class BounceAnimation extends TimedAnimation @Override protected void begin() { + this.startScale = piece.getScale(); + this.startRotation = piece.getRotation(); } @Override protected void end() { - piece.setScale(1f); + piece.setScale(this.startScale); + if (rotationDegrees != 0f) { + piece.setRotation(this.startRotation); + } } @Override protected void update(float percent) { - piece.setScale(1 + bounceFactor * (float) Math.sin(percent * Math.PI)); + piece.setScale(this.startScale + this.bounceFactor * (float) Math.sin(percent * Math.PI)); + if (rotationDegrees != 0f) { + piece.setRotation(this.startRotation + (percent * this.rotationDegrees)); + } } @Override public void draw(Batch batch) diff --git a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java index b21da4d..010506e 100644 --- a/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java +++ b/test/src/ch/asynk/gdx/boardgame/test/AnimationsScreen.java @@ -61,7 +61,7 @@ public class AnimationsScreen extends AbstractScreen { switch (state) { case BOUNCE: - animation = BounceAnimation.get(panzer, 2f, 3f); + animation = BounceAnimation.get(panzer, 2f, 3f, -1); case DONE: app.switchToMenu(); } -- cgit v1.1-2-g2b99