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 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 setSpeed(float speed);
|
||||||
|
|
||||||
void onAdd(Layer layer);
|
void onAdd(Layer layer);
|
||||||
|
|
||||||
|
void finish();
|
||||||
|
|
||||||
boolean finished();
|
boolean finished();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -160,8 +160,8 @@ public abstract class AnimationDelegate implements Animation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void followPath(Path<Animation> path, boolean repeat) {
|
public void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) {
|
||||||
animation.followPath(path, repeat);
|
animation.followPath(path, repeat, finishOnEnd, finishOnFail);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -174,6 +174,11 @@ public abstract class AnimationDelegate implements Animation {
|
|||||||
animation.onAdd(layer);
|
animation.onAdd(layer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finish() {
|
||||||
|
animation.finish();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
public void render(Screen screen, Camera camera, ShaderManager shaderManager) {
|
||||||
animation.render(screen, camera, shaderManager);
|
animation.render(screen, camera, shaderManager);
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ import lombok.NonNull;
|
|||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.joml.Vector2fc;
|
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 {
|
public class DefaultAnimation extends MovableSprite implements Animation {
|
||||||
private final Vector2fc[] frames;
|
private final Vector2fc[] frames;
|
||||||
private final int lastFrameIndex;
|
private final int lastFrameIndex;
|
||||||
@@ -26,8 +29,12 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
@Setter
|
@Setter
|
||||||
private Integer repeat = 1;
|
private Integer repeat = 1;
|
||||||
|
|
||||||
|
private boolean forcedFinish = false;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private PathExecutor<Animation> pathExecutor;
|
private PathExecutor<Animation> pathExecutor;
|
||||||
|
private boolean finishOnEnd;
|
||||||
|
private boolean finishOnFail;
|
||||||
private Layer layer;
|
private Layer layer;
|
||||||
private boolean isObjectLayer = false;
|
private boolean isObjectLayer = false;
|
||||||
|
|
||||||
@@ -63,8 +70,10 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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);
|
pathExecutor = new PathExecutor<>(this, repeat, path);
|
||||||
|
this.finishOnEnd = finishOnEnd;
|
||||||
|
this.finishOnFail = finishOnFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -77,7 +86,10 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
super.update(dt);
|
super.update(dt);
|
||||||
|
|
||||||
if (pathExecutor != null && isObjectLayer) {
|
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;
|
this.isObjectLayer = layer instanceof ObjectLayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void finish() {
|
||||||
|
this.forcedFinish = true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean finished() {
|
public boolean finished() {
|
||||||
|
if (forcedFinish) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (repeat == null) {
|
if (repeat == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user