Improve Paths and fix some path issues
- implement insertPath() method which should be especially useful to insert some automatically generated path with pathfinder algorithms - fix CompletableFutureSegment - the supplier should be invoked only once and then it should be cached instead of triggering it over and over again - make FollowPathAI aware of the path progress - also expose it with getter
This commit is contained in:
@@ -5,10 +5,15 @@ import com.bartlomiejpluta.base.api.ai.NPC;
|
||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||
import com.bartlomiejpluta.base.util.path.Path;
|
||||
import com.bartlomiejpluta.base.util.path.PathExecutor;
|
||||
import com.bartlomiejpluta.base.util.path.PathProgress;
|
||||
import lombok.Getter;
|
||||
|
||||
public class FollowPathAI<T extends NPC> implements AI {
|
||||
private final PathExecutor<T> executor;
|
||||
|
||||
@Getter
|
||||
private PathProgress progress = PathProgress.NOT_STARTED;
|
||||
|
||||
public FollowPathAI(T npc) {
|
||||
this.executor = new PathExecutor<>(npc);
|
||||
}
|
||||
@@ -23,8 +28,13 @@ public class FollowPathAI<T extends NPC> implements AI {
|
||||
return this;
|
||||
}
|
||||
|
||||
public FollowPathAI<T> reset() {
|
||||
executor.reset();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void nextActivity(ObjectLayer layer, float dt) {
|
||||
executor.execute(layer, dt);
|
||||
progress = executor.execute(layer, dt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,11 @@ public class BasePath<T extends Movable> implements Path<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Path<T> insertPath(Path<T> path) {
|
||||
this.path.addAll(path.getPath());
|
||||
return this;
|
||||
}
|
||||
|
||||
public Path<T> move(Direction direction) {
|
||||
path.add(new MoveSegment<>(direction));
|
||||
return this;
|
||||
|
||||
@@ -35,6 +35,11 @@ public class CharacterPath<T extends Character> implements Path<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public CharacterPath<T> insertPath(Path<T> path) {
|
||||
this.path.addAll(path.getPath());
|
||||
return this;
|
||||
}
|
||||
|
||||
public CharacterPath<T> move(Direction direction) {
|
||||
path.add(new MoveSegment<>(direction));
|
||||
return this;
|
||||
|
||||
@@ -10,10 +10,13 @@ import java.util.function.Supplier;
|
||||
@RequiredArgsConstructor
|
||||
public class CompletableFutureSegment<T extends Movable> implements PathSegment<T> {
|
||||
private final Supplier<CompletableFuture<?>> futureSupplier;
|
||||
private CompletableFuture<?> future;
|
||||
|
||||
@Override
|
||||
public PathProgress perform(T movable, ObjectLayer layer, float dt) {
|
||||
var future = futureSupplier.get();
|
||||
if (future == null) {
|
||||
future = futureSupplier.get();
|
||||
}
|
||||
|
||||
if (future.isCancelled() || future.isCompletedExceptionally()) {
|
||||
return PathProgress.SEGMENT_FAILED;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartlomiejpluta.base.util.path;
|
||||
|
||||
public enum PathProgress {
|
||||
NOT_STARTED,
|
||||
ONGOING,
|
||||
SEGMENT_DONE,
|
||||
SEGMENT_FAILED,
|
||||
|
||||
Reference in New Issue
Block a user