diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-08 16:37:08 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2018-10-08 16:37:08 +0200 |
commit | bd891a4be89144c795752e6480c5efbd73ea1444 (patch) | |
tree | 2ea31d14a85819924c29536c8ca16847582ff5bb /core/src/ch/asynk/gdx/boardgame | |
parent | 247f2e0ccf2563d5d67081608b870bd08a24c93c (diff) | |
download | gdx-boardgame-bd891a4be89144c795752e6480c5efbd73ea1444.zip gdx-boardgame-bd891a4be89144c795752e6480c5efbd73ea1444.tar.gz |
add animations
Diffstat (limited to 'core/src/ch/asynk/gdx/boardgame')
4 files changed, 215 insertions, 0 deletions
diff --git a/core/src/ch/asynk/gdx/boardgame/animations/Animation.java b/core/src/ch/asynk/gdx/boardgame/animations/Animation.java new file mode 100644 index 0000000..52c0889 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/animations/Animation.java @@ -0,0 +1,11 @@ +package ch.asynk.gdx.boardgame.animations; + +import com.badlogic.gdx.utils.Disposable; + +import ch.asynk.gdx.boardgame.Drawable; + +public interface Animation extends Drawable, Disposable +{ + public boolean completed(); + public boolean animate(float delta); +} diff --git a/core/src/ch/asynk/gdx/boardgame/animations/AnimationSequence.java b/core/src/ch/asynk/gdx/boardgame/animations/AnimationSequence.java new file mode 100644 index 0000000..4732c53 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/animations/AnimationSequence.java @@ -0,0 +1,82 @@ +package ch.asynk.gdx.boardgame.animations; + +import com.badlogic.gdx.utils.Pool; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.glutils.ShapeRenderer; + +import ch.asynk.gdx.boardgame.utils.IterableArray; + +public class AnimationSequence implements Animation, Pool.Poolable +{ + private static final Pool<AnimationSequence> animationSequencePool = new Pool<AnimationSequence>() + { + @Override protected AnimationSequence newObject() + { + return new AnimationSequence(); + } + }; + + public static AnimationSequence get(int capacity) + { + AnimationSequence seq = animationSequencePool.obtain(); + if (seq.animations == null) { + seq.animations = new IterableArray<Animation>(capacity); + } else { + seq.animations.ensureCapacity(capacity); + } + + return seq; + } + + private IterableArray<Animation> animations; + + private AnimationSequence() + { + } + + @Override public void reset() + { + for (Animation a : animations) { + a.dispose(); + } + animations.clear(); + } + + @Override public void dispose() + { + animationSequencePool.free(this); + } + + public void addAnimation(Animation animation) + { + animations.add(animation); + } + + @Override public boolean completed() + { + return (animations.size() == 0); + } + + @Override public boolean animate(float delta) + { + if (animations.isEmpty()) return true; + + Animation animation = animations.get(0); + if (animation.animate(delta)) { + animations.remove(0); + animation.dispose(); + } + + return (animations.isEmpty()); + } + + @Override public void draw(Batch batch) + { + animations.get(0).draw(batch); + } + + @Override public void drawDebug(ShapeRenderer debugShapes) + { + animations.get(0).drawDebug(debugShapes); + } +} diff --git a/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java new file mode 100644 index 0000000..3149e8d --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/animations/BounceAnimation.java @@ -0,0 +1,73 @@ +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.graphics.glutils.ShapeRenderer; + +import ch.asynk.gdx.boardgame.Scalable; + +public class BounceAnimation extends TimedAnimation +{ + private static final Pool<BounceAnimation> bounceAnimationPool = new Pool<BounceAnimation>() + { + @Override protected BounceAnimation newObject() + { + return new BounceAnimation(); + } + }; + + public static BounceAnimation get(Scalable scalable, float duration, float bounceFactor) + { + BounceAnimation a = bounceAnimationPool.obtain(); + + a.scalable = scalable; + a.duration = duration; + a.bounceFactor = bounceFactor; + + return a; + } + + private Scalable scalable; + private float bounceFactor; + + private BounceAnimation() + { + } + + public BounceAnimation(Scalable scalable, float duration, float bounceFactor) + { + this.scalable = scalable; + this.duration = duration; + this.bounceFactor = bounceFactor; + } + + @Override public void dispose() + { + bounceAnimationPool.free(this); + } + + @Override protected void begin() + { + } + + @Override protected void end() + { + scalable.setScale(1f); + } + + @Override protected void update(float percent) + { + scalable.setScale(1 + bounceFactor * (float) Math.sin(percent * Math.PI)); + } + + @Override public void draw(Batch batch) + { + scalable.draw(batch); + } + + @Override public void drawDebug(ShapeRenderer debugShapes) + { + } +} diff --git a/core/src/ch/asynk/gdx/boardgame/animations/TimedAnimation.java b/core/src/ch/asynk/gdx/boardgame/animations/TimedAnimation.java new file mode 100644 index 0000000..f66cb98 --- /dev/null +++ b/core/src/ch/asynk/gdx/boardgame/animations/TimedAnimation.java @@ -0,0 +1,49 @@ +package ch.asynk.gdx.boardgame.animations; + +import com.badlogic.gdx.utils.Pool; + +public abstract class TimedAnimation implements Animation, Pool.Poolable +{ + private float time; + private boolean started; + private boolean completed; + protected float duration; + + abstract protected void begin(); + abstract protected void end(); + abstract protected void update(float percent); + + @Override public void reset() + { + time = 0f; + started = false; + completed = false; + } + + @Override public boolean completed() + { + return completed; + } + + @Override public boolean animate(float delta) + { + if (completed) return true; + + if (!started) { + begin(); + started = true; + } + + time += delta; + + if (time >= duration) { + completed = true; + update(1); + end(); + } else { + update(time / duration); + } + + return completed; + } +} |