From e68539575aa9d536d92190243aecb415b056bed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Mon, 22 Mar 2021 12:35:42 +0100 Subject: [PATCH] Add offset properties to Simple and Random Animations Runners --- .../animation/RandomAnimationsRunner.java | 13 +++++++-- .../game/animation/SimpleAnimationRunner.java | 27 +++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/RandomAnimationsRunner.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/RandomAnimationsRunner.java index e5844587..0bc4c260 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/RandomAnimationsRunner.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/RandomAnimationsRunner.java @@ -34,6 +34,9 @@ public class RandomAnimationsRunner implements AnimationRunner { private float rotation = 0f; private RealDistribution rotationDistribution; + private float offsetX = 0; + private float offsetY = 0; + public RandomAnimationsRunner(int count) { this.count = max(count, 0); } @@ -119,15 +122,21 @@ public class RandomAnimationsRunner implements AnimationRunner { return this; } + public RandomAnimationsRunner offset(float x, float y) { + this.offsetX = x; + this.offsetY = y; + return this; + } + @Override public void run(Context context, Layer layer, Vector2fc origin) { for (int i = 0; i < count; ++i) { var animation = context.createAnimation(animationUids.get(random.nextInt(animationUids.size()))); if (rangeDistribution != null) { - animation.setPosition(origin.x() + (float) rangeDistribution.sample(), origin.y() + (float) rangeDistribution.sample()); + animation.setPosition(origin.x() + (float) rangeDistribution.sample() + offsetX, origin.y() + (float) rangeDistribution.sample() + offsetY); } else { - animation.setPosition(origin.x() + rangeX, origin.y() + rangeY); + animation.setPosition(origin.x() + rangeX + offsetX, origin.y() + rangeY + offsetY); } animation.setScale(scaleDistribution != null ? (float) scaleDistribution.sample() : scale); diff --git a/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/SimpleAnimationRunner.java b/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/SimpleAnimationRunner.java index 822d1c57..7b1b8843 100644 --- a/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/SimpleAnimationRunner.java +++ b/api/src/main/java/com/bartlomiejpluta/base/api/game/animation/SimpleAnimationRunner.java @@ -15,9 +15,11 @@ public class SimpleAnimationRunner implements AnimationRunner { private float speed = 0.05f; private float rotation = 0f; private Path path; - private Integer pathRepeat; + private Integer repeatPath; private boolean finishOnPathEnd; private boolean finishOnPathFail; + private float offsetX = 0; + private float offsetY = 0; public SimpleAnimationRunner(String animationUid) { this.animationUid = animationUid; @@ -48,14 +50,29 @@ public class SimpleAnimationRunner implements AnimationRunner { return this; } + public SimpleAnimationRunner speed(float speed) { + this.speed = speed; + return this; + } + public SimpleAnimationRunner rotation(float rotation) { this.rotation = rotation; return this; } - public SimpleAnimationRunner path(Path path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) { + public SimpleAnimationRunner offset(float x, float y) { + this.offsetX = x; + this.offsetY = y; + return this; + } + + public SimpleAnimationRunner repeatPath(int n) { + this.repeatPath = n; + return this; + } + + public SimpleAnimationRunner path(Path path, boolean finishOnEnd, boolean finishOnFail) { this.path = path; - this.pathRepeat = repeat; this.finishOnPathEnd = finishOnEnd; this.finishOnPathFail = finishOnFail; return this; @@ -64,7 +81,7 @@ public class SimpleAnimationRunner implements AnimationRunner { @Override public void run(Context context, Layer layer, Vector2fc origin) { var animation = new DelayedAnimation(context.createAnimation(animationUid), delay); - animation.setPosition(origin); + animation.setPosition(origin.x() + offsetX, origin.y() + offsetY); animation.setScale(scale); animation.setAnimationSpeed(animationSpeed); animation.setSpeed(speed); @@ -72,7 +89,7 @@ public class SimpleAnimationRunner implements AnimationRunner { animation.setRepeat(repeat); if (path != null) { - animation.followPath(path, pathRepeat, finishOnPathEnd, finishOnPathFail); + animation.followPath(path, repeatPath, finishOnPathEnd, finishOnPathFail); } layer.pushAnimation(animation);