Make Path Executor reusable

This commit is contained in:
2021-04-04 22:37:19 +02:00
parent b0655a0bb1
commit 7c777dca01
3 changed files with 34 additions and 10 deletions

View File

@@ -9,8 +9,18 @@ import com.bartlomiejpluta.base.util.path.PathExecutor;
public class FollowPathAI<T extends NPC> implements AI {
private final PathExecutor<T> executor;
public FollowPathAI(T npc, Integer repeat, Path<T> path) {
this.executor = new PathExecutor<>(npc, repeat, path);
public FollowPathAI(T npc) {
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

View File

@@ -4,21 +4,35 @@ import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
import com.bartlomiejpluta.base.api.move.Movable;
import java.util.List;
import java.util.Objects;
import static java.util.Collections.emptyList;
public class PathExecutor<T extends Movable> {
protected final List<PathSegment<T>> path;
private final T movable;
private final Integer repeat;
private List<PathSegment<T>> path = emptyList();
private Integer repeat;
private int current = 0;
private int iteration = 0;
private boolean updated = false;
public PathExecutor(T movable, Integer repeat, Path<T> path) {
public PathExecutor(T 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.path = Objects.requireNonNull(path).getPath();
this.current = 0;
this.iteration = 0;
return this;
}
public PathProgress execute(ObjectLayer layer, float dt) {