Make EntityInstantAnimation returns a CompletableFuture
This commit is contained in:
@@ -7,6 +7,8 @@ import com.bartlomiejpluta.base.api.move.Movable;
|
||||
import com.bartlomiejpluta.base.internal.logic.Updatable;
|
||||
import com.bartlomiejpluta.base.internal.render.Renderable;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface Entity extends Movable, Animated, Renderable, Updatable {
|
||||
|
||||
Direction getFaceDirection();
|
||||
@@ -35,5 +37,7 @@ public interface Entity extends Movable, Animated, Renderable, Updatable {
|
||||
|
||||
void setZIndex(int zIndex);
|
||||
|
||||
void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish);
|
||||
CompletableFuture<Void> performInstantAnimation();
|
||||
|
||||
CompletableFuture<Void> performInstantAnimation(Direction targetFaceDirection);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import org.joml.Matrix4fc;
|
||||
import org.joml.Vector2fc;
|
||||
import org.joml.Vector2ic;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public abstract class EntityDelegate implements Entity {
|
||||
protected final Entity entity;
|
||||
|
||||
@@ -266,8 +268,13 @@ public abstract class EntityDelegate implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish) {
|
||||
entity.performInstantAnimation(targetFaceDirection, onFinish);
|
||||
public CompletableFuture<Void> performInstantAnimation() {
|
||||
return entity.performInstantAnimation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> performInstantAnimation(Direction targetFaceDirection) {
|
||||
return entity.performInstantAnimation(targetFaceDirection);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,6 +21,7 @@ import org.joml.Vector2i;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@@ -119,8 +120,19 @@ public class DefaultEntity extends MovableSprite implements Entity {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void performInstantAnimation(Direction targetFaceDirection, Runnable onFinish) {
|
||||
instantAnimations.add(new EntityInstantAnimation(this, defaultSpriteColumn, targetFaceDirection, onFinish));
|
||||
public CompletableFuture<Void> performInstantAnimation() {
|
||||
var animation = new EntityInstantAnimation(this, defaultSpriteColumn);
|
||||
instantAnimations.add(animation);
|
||||
|
||||
return animation.getFuture();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> performInstantAnimation(Direction targetFaceDirection) {
|
||||
var animation = new EntityInstantAnimation(this, defaultSpriteColumn, targetFaceDirection);
|
||||
instantAnimations.add(animation);
|
||||
|
||||
return animation.getFuture();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.bartlomiejpluta.base.engine.world.entity.model;
|
||||
|
||||
import com.bartlomiejpluta.base.api.move.Direction;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class EntityInstantAnimation {
|
||||
|
||||
@@ -8,15 +11,20 @@ public class EntityInstantAnimation {
|
||||
private final int firstFrame;
|
||||
private final int lastFrame;
|
||||
private final Direction faceDirectionOnFinish;
|
||||
private final Runnable onFinish;
|
||||
private boolean finished = false;
|
||||
|
||||
EntityInstantAnimation(DefaultEntity entity, int firstFrame, Direction faceDirectionOnFinish, Runnable onFinish) {
|
||||
@Getter
|
||||
private final CompletableFuture<Void> future = new CompletableFuture<>();
|
||||
|
||||
EntityInstantAnimation(DefaultEntity entity, int firstFrame) {
|
||||
this(entity, firstFrame, null);
|
||||
}
|
||||
|
||||
EntityInstantAnimation(DefaultEntity entity, int firstFrame, Direction faceDirectionOnFinish) {
|
||||
this.entity = entity;
|
||||
this.firstFrame = firstFrame;
|
||||
this.lastFrame = entity.getMaterial().getTexture().getColumns() - 1;
|
||||
this.faceDirectionOnFinish = faceDirectionOnFinish;
|
||||
this.onFinish = onFinish;
|
||||
}
|
||||
|
||||
public boolean updateFrame() {
|
||||
@@ -30,9 +38,7 @@ public class EntityInstantAnimation {
|
||||
entity.setFaceDirection(faceDirectionOnFinish);
|
||||
}
|
||||
|
||||
if (onFinish != null) {
|
||||
onFinish.run();
|
||||
}
|
||||
future.complete(null);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user