Make Path Executor reusable
This commit is contained in:
@@ -9,8 +9,18 @@ import com.bartlomiejpluta.base.util.path.PathExecutor;
|
|||||||
public class FollowPathAI<T extends NPC> implements AI {
|
public class FollowPathAI<T extends NPC> implements AI {
|
||||||
private final PathExecutor<T> executor;
|
private final PathExecutor<T> executor;
|
||||||
|
|
||||||
public FollowPathAI(T npc, Integer repeat, Path<T> path) {
|
public FollowPathAI(T npc) {
|
||||||
this.executor = new PathExecutor<>(npc, repeat, path);
|
this.executor = new PathExecutor<>(npc);
|
||||||
|
}
|
||||||
|
|
||||||
|
public FollowPathAI<T> setPath(Path<T> path) {
|
||||||
|
executor.setPath(path);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FollowPathAI<T> setRepeat(Integer repeat) {
|
||||||
|
executor.setRepeat(repeat);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -4,21 +4,35 @@ import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
|||||||
import com.bartlomiejpluta.base.api.move.Movable;
|
import com.bartlomiejpluta.base.api.move.Movable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
|
|
||||||
public class PathExecutor<T extends Movable> {
|
public class PathExecutor<T extends Movable> {
|
||||||
protected final List<PathSegment<T>> path;
|
|
||||||
private final T movable;
|
private final T movable;
|
||||||
private final Integer repeat;
|
|
||||||
|
private List<PathSegment<T>> path = emptyList();
|
||||||
|
private Integer repeat;
|
||||||
|
|
||||||
private int current = 0;
|
private int current = 0;
|
||||||
private int iteration = 0;
|
private int iteration = 0;
|
||||||
private boolean updated = false;
|
private boolean updated = false;
|
||||||
|
|
||||||
public PathExecutor(T movable, Integer repeat, Path<T> path) {
|
public PathExecutor(T movable) {
|
||||||
this.movable = movable;
|
this.movable = movable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PathExecutor<T> setPath(Path<T> path) {
|
||||||
|
this.path = path != null ? path.getPath() : emptyList();
|
||||||
|
this.current = 0;
|
||||||
|
this.iteration = 0;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PathExecutor<T> setRepeat(Integer repeat) {
|
||||||
this.repeat = repeat;
|
this.repeat = repeat;
|
||||||
this.path = Objects.requireNonNull(path).getPath();
|
this.current = 0;
|
||||||
|
this.iteration = 0;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PathProgress execute(ObjectLayer layer, float dt) {
|
public PathProgress execute(ObjectLayer layer, float dt) {
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
private boolean enabled = true;
|
private boolean enabled = true;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private PathExecutor<Animation> pathExecutor;
|
private PathExecutor<Animation> pathExecutor = new PathExecutor<>(this);
|
||||||
private boolean finishOnEnd;
|
private boolean finishOnEnd;
|
||||||
private boolean finishOnFail;
|
private boolean finishOnFail;
|
||||||
private Layer layer;
|
private Layer layer;
|
||||||
@@ -95,7 +95,7 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) {
|
public void followPath(Path<Animation> path, Integer repeat, boolean finishOnEnd, boolean finishOnFail) {
|
||||||
pathExecutor = new PathExecutor<>(this, repeat, path);
|
pathExecutor.setPath(path).setRepeat(repeat);
|
||||||
this.finishOnEnd = finishOnEnd;
|
this.finishOnEnd = finishOnEnd;
|
||||||
this.finishOnFail = finishOnFail;
|
this.finishOnFail = finishOnFail;
|
||||||
}
|
}
|
||||||
@@ -104,7 +104,7 @@ public class DefaultAnimation extends MovableSprite implements Animation {
|
|||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
super.update(dt);
|
super.update(dt);
|
||||||
|
|
||||||
if (pathExecutor != null && isObjectLayer) {
|
if (isObjectLayer) {
|
||||||
var pathProgress = pathExecutor.execute((ObjectLayer) layer, dt);
|
var pathProgress = pathExecutor.execute((ObjectLayer) layer, dt);
|
||||||
if ((pathProgress == DONE && finishOnEnd) || (pathProgress == SEGMENT_FAILED && finishOnFail)) {
|
if ((pathProgress == DONE && finishOnEnd) || (pathProgress == SEGMENT_FAILED && finishOnFail)) {
|
||||||
finish();
|
finish();
|
||||||
|
|||||||
Reference in New Issue
Block a user