Add support for finishing Animation when it encounters obstacle or finishes its path
This commit is contained in:
@@ -24,11 +24,13 @@ public interface Animation extends Placeable, Movable, Renderable, Updatable {
|
||||
|
||||
void setRepeat(Integer repeat);
|
||||
|
||||
void followPath(Path<Animation> path, boolean repeat);
|
||||
void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail);
|
||||
|
||||
void setSpeed(float speed);
|
||||
|
||||
void onAdd(Layer layer);
|
||||
|
||||
void finish();
|
||||
|
||||
boolean finished();
|
||||
}
|
||||
|
||||
@@ -160,8 +160,8 @@ public abstract class AnimationDelegate implements Animation {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void followPath(Path<Animation> path, boolean repeat) {
|
||||
animation.followPath(path, repeat);
|
||||
public void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) {
|
||||
animation.followPath(path, repeat, finishOnEnd, finishOnFail);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -174,6 +174,11 @@ public abstract class AnimationDelegate implements Animation {
|
||||
animation.onAdd(layer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
animation.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||
animation.render(screen, camera, shaderManager);
|
||||
|
||||
@@ -14,6 +14,9 @@ import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import org.joml.Vector2fc;
|
||||
|
||||
import static com.bartlomiejpluta.base.api.util.path.PathProgress.DONE;
|
||||
import static com.bartlomiejpluta.base.api.util.path.PathProgress.SEGMENT_FAILED;
|
||||
|
||||
public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
private final Vector2fc[] frames;
|
||||
private final int lastFrameIndex;
|
||||
@@ -26,8 +29,12 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
@Setter
|
||||
private Integer repeat = 1;
|
||||
|
||||
private boolean forcedFinish = false;
|
||||
|
||||
@Getter
|
||||
private PathExecutor<Animation> pathExecutor;
|
||||
private boolean finishOnEnd;
|
||||
private boolean finishOnFail;
|
||||
private Layer layer;
|
||||
private boolean isObjectLayer = false;
|
||||
|
||||
@@ -63,8 +70,10 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void followPath(Path<Animation> path, boolean repeat) {
|
||||
public void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) {
|
||||
pathExecutor = new PathExecutor<>(this, repeat, path);
|
||||
this.finishOnEnd = finishOnEnd;
|
||||
this.finishOnFail = finishOnFail;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,7 +86,10 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
super.update(dt);
|
||||
|
||||
if (pathExecutor != null && isObjectLayer) {
|
||||
pathExecutor.execute((ObjectLayer) layer, dt);
|
||||
var pathProgress = pathExecutor.execute((ObjectLayer) layer, dt);
|
||||
if ((pathProgress == DONE && finishOnEnd) || (pathProgress == SEGMENT_FAILED && finishOnFail)) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,8 +99,17 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
||||
this.isObjectLayer = layer instanceof ObjectLayer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
this.forcedFinish = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean finished() {
|
||||
if (forcedFinish) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (repeat == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user