Add CompletableFuture for animations
This commit is contained in:
@@ -6,6 +6,8 @@ import com.bartlomiejpluta.base.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.internal.render.Renderable;
|
||||
import com.bartlomiejpluta.base.util.path.Path;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface Animation extends Movable, Animated, Renderable, Updatable {
|
||||
|
||||
Integer getRepeat();
|
||||
@@ -18,6 +20,8 @@ public interface Animation extends Movable, Animated, Renderable, Updatable {
|
||||
|
||||
void onFinish(Layer layer);
|
||||
|
||||
CompletableFuture<Animation> getFuture();
|
||||
|
||||
void finish();
|
||||
|
||||
boolean finished();
|
||||
|
||||
@@ -15,6 +15,8 @@ import org.joml.Matrix4fc;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public abstract class AnimationDelegate implements Animation {
|
||||
private final Animation animation;
|
||||
|
||||
@@ -272,6 +274,11 @@ public abstract class AnimationDelegate implements Animation {
|
||||
animation.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Animation> getFuture() {
|
||||
return animation.getFuture();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
animation.render(screen, camera, shaderManager);
|
||||
|
||||
@@ -5,8 +5,10 @@ import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.move.Movable;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
public interface AnimationRunner {
|
||||
void run(Context context, Layer layer, Vector2fc origin);
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
void run(Context context, Layer layer, Movable origin);
|
||||
public interface AnimationRunner {
|
||||
CompletableFuture<Void> run(Context context, Layer layer, Vector2fc origin);
|
||||
|
||||
CompletableFuture<Void> run(Context context, Layer layer, Movable origin);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import lombok.NonNull;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2i;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
@@ -126,7 +127,7 @@ public class BulletAnimationRunner implements AnimationRunner {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Context context, Layer layer, Vector2fc origin) {
|
||||
public CompletableFuture<Void> run(Context context, Layer layer, Vector2fc origin) {
|
||||
var animation = new BulletAnimation(context.createAnimation(animationUid), delay, direction, null, onHit, onMiss);
|
||||
|
||||
animation.setPosition(origin);
|
||||
@@ -140,10 +141,12 @@ public class BulletAnimationRunner implements AnimationRunner {
|
||||
animation.followPath(path, range, true, true);
|
||||
|
||||
layer.pushAnimation(animation);
|
||||
|
||||
return animation.getFuture().thenApply(a -> null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Context context, Layer layer, Movable origin) {
|
||||
public CompletableFuture<Void> run(Context context, Layer layer, Movable origin) {
|
||||
var animation = new BulletAnimation(context.createAnimation(animationUid), delay, direction, origin, onHit, onMiss);
|
||||
|
||||
animation.setCoordinates(origin.getCoordinates());
|
||||
@@ -157,6 +160,8 @@ public class BulletAnimationRunner implements AnimationRunner {
|
||||
animation.followPath(path, range, true, true);
|
||||
|
||||
layer.pushAnimation(animation);
|
||||
|
||||
return animation.getFuture().thenApply(a -> null);
|
||||
}
|
||||
|
||||
private static class BulletAnimation extends DelayedAnimation {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.bartlomiejpluta.base.lib.animation;
|
||||
|
||||
import com.bartlomiejpluta.base.api.animation.Animation;
|
||||
import com.bartlomiejpluta.base.api.context.Context;
|
||||
import com.bartlomiejpluta.base.api.map.layer.base.Layer;
|
||||
import com.bartlomiejpluta.base.api.move.Movable;
|
||||
@@ -9,8 +10,10 @@ import org.apache.commons.math3.distribution.UniformRealDistribution;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
|
||||
@@ -130,7 +133,9 @@ public class RandomAnimationsRunner implements AnimationRunner {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Context context, Layer layer, Vector2fc origin) {
|
||||
public CompletableFuture<Void> run(Context context, Layer layer, Vector2fc origin) {
|
||||
var futures = new CompletableFuture[count];
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
var animation = context.createAnimation(animationUids.get(random.nextInt(animationUids.size())));
|
||||
|
||||
@@ -146,11 +151,17 @@ public class RandomAnimationsRunner implements AnimationRunner {
|
||||
animation.setRotation(rotationDistribution != null ? (float) rotationDistribution.sample() : rotation);
|
||||
|
||||
layer.pushAnimation(new DelayedAnimation(animation, (int) (delayDistribution != null ? delayDistribution.sample() : delay)));
|
||||
|
||||
futures[i] = animation.getFuture();
|
||||
}
|
||||
|
||||
return CompletableFuture.allOf(futures);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Context context, Layer layer, Movable origin) {
|
||||
public CompletableFuture<Void> run(Context context, Layer layer, Movable origin) {
|
||||
var futures = new CompletableFuture[count];
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
var animation = context.createAnimation(animationUids.get(random.nextInt(animationUids.size())));
|
||||
|
||||
@@ -169,7 +180,10 @@ public class RandomAnimationsRunner implements AnimationRunner {
|
||||
animation.setRotation(rotationDistribution != null ? (float) rotationDistribution.sample() : rotation);
|
||||
|
||||
layer.pushAnimation(new DelayedAnimation(animation, (int) (delayDistribution != null ? delayDistribution.sample() : delay)));
|
||||
|
||||
futures[i] = animation.getFuture();
|
||||
}
|
||||
|
||||
return CompletableFuture.allOf(futures);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import com.bartlomiejpluta.base.api.move.Movable;
|
||||
import com.bartlomiejpluta.base.util.path.Path;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class SimpleAnimationRunner implements AnimationRunner {
|
||||
private final String animationUid;
|
||||
|
||||
@@ -81,7 +83,7 @@ public class SimpleAnimationRunner implements AnimationRunner {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Context context, Layer layer, Vector2fc origin) {
|
||||
public CompletableFuture<Void> run(Context context, Layer layer, Vector2fc origin) {
|
||||
var animation = new DelayedAnimation(context.createAnimation(animationUid), delay);
|
||||
|
||||
animation.setPosition(origin);
|
||||
@@ -97,10 +99,11 @@ public class SimpleAnimationRunner implements AnimationRunner {
|
||||
}
|
||||
|
||||
layer.pushAnimation(animation);
|
||||
return animation.getFuture().thenApply(a -> null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(Context context, Layer layer, Movable origin) {
|
||||
public CompletableFuture<Void> run(Context context, Layer layer, Movable origin) {
|
||||
var animation = new DelayedAnimation(context.createAnimation(animationUid), delay);
|
||||
|
||||
animation.setCoordinates(origin.getCoordinates());
|
||||
@@ -117,5 +120,6 @@ public class SimpleAnimationRunner implements AnimationRunner {
|
||||
|
||||
layer.pushAnimation(animation);
|
||||
|
||||
return animation.getFuture().thenApply(a -> null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ import lombok.Setter;
|
||||
import org.joml.Vector2f;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static com.bartlomiejpluta.base.util.path.PathProgress.DONE;
|
||||
import static com.bartlomiejpluta.base.util.path.PathProgress.SEGMENT_FAILED;
|
||||
|
||||
@@ -44,6 +46,9 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
private Layer layer;
|
||||
private boolean isObjectLayer = false;
|
||||
|
||||
@Getter
|
||||
private final CompletableFuture<Animation> future = new CompletableFuture<>();
|
||||
|
||||
public DefaultAnimation(Mesh mesh, Material material, @NonNull Vector2fc[] frames) {
|
||||
super(mesh, material);
|
||||
this.frames = frames;
|
||||
|
||||
@@ -54,6 +54,7 @@ public abstract class BaseLayer implements Layer, Updatable {
|
||||
|
||||
if (animation.finished()) {
|
||||
animations.remove(animation);
|
||||
animation.getFuture().complete(animation);
|
||||
animation.onFinish(this);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user