Add support for finishing Animation when it encounters obstacle or finishes its path

This commit is contained in:
2021-03-21 10:48:48 +01:00
parent 3b22655bc6
commit 11664d809a
3 changed files with 33 additions and 5 deletions

View File

@@ -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;
}