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.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.util.path.Path;
|
import com.bartlomiejpluta.base.util.path.Path;
|
||||||
import com.bartlomiejpluta.base.util.path.PathExecutor;
|
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 {
|
public class FollowPathAI<T extends NPC> implements AI {
|
||||||
private final PathExecutor<T> executor;
|
private final PathExecutor<T> executor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private PathProgress progress = PathProgress.NOT_STARTED;
|
||||||
|
|
||||||
public FollowPathAI(T npc) {
|
public FollowPathAI(T npc) {
|
||||||
this.executor = new PathExecutor<>(npc);
|
this.executor = new PathExecutor<>(npc);
|
||||||
}
|
}
|
||||||
@@ -23,8 +28,13 @@ public class FollowPathAI<T extends NPC> implements AI {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FollowPathAI<T> reset() {
|
||||||
|
executor.reset();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void nextActivity(ObjectLayer layer, float dt) {
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Path<T> insertPath(Path<T> path) {
|
||||||
|
this.path.addAll(path.getPath());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public Path<T> move(Direction direction) {
|
public Path<T> move(Direction direction) {
|
||||||
path.add(new MoveSegment<>(direction));
|
path.add(new MoveSegment<>(direction));
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -35,6 +35,11 @@ public class CharacterPath<T extends Character> implements Path<T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CharacterPath<T> insertPath(Path<T> path) {
|
||||||
|
this.path.addAll(path.getPath());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public CharacterPath<T> move(Direction direction) {
|
public CharacterPath<T> move(Direction direction) {
|
||||||
path.add(new MoveSegment<>(direction));
|
path.add(new MoveSegment<>(direction));
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -10,10 +10,13 @@ import java.util.function.Supplier;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class CompletableFutureSegment<T extends Movable> implements PathSegment<T> {
|
public class CompletableFutureSegment<T extends Movable> implements PathSegment<T> {
|
||||||
private final Supplier<CompletableFuture<?>> futureSupplier;
|
private final Supplier<CompletableFuture<?>> futureSupplier;
|
||||||
|
private CompletableFuture<?> future;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PathProgress perform(T movable, ObjectLayer layer, float dt) {
|
public PathProgress perform(T movable, ObjectLayer layer, float dt) {
|
||||||
var future = futureSupplier.get();
|
if (future == null) {
|
||||||
|
future = futureSupplier.get();
|
||||||
|
}
|
||||||
|
|
||||||
if (future.isCancelled() || future.isCompletedExceptionally()) {
|
if (future.isCancelled() || future.isCompletedExceptionally()) {
|
||||||
return PathProgress.SEGMENT_FAILED;
|
return PathProgress.SEGMENT_FAILED;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.util.path;
|
package com.bartlomiejpluta.base.util.path;
|
||||||
|
|
||||||
public enum PathProgress {
|
public enum PathProgress {
|
||||||
|
NOT_STARTED,
|
||||||
ONGOING,
|
ONGOING,
|
||||||
SEGMENT_DONE,
|
SEGMENT_DONE,
|
||||||
SEGMENT_FAILED,
|
SEGMENT_FAILED,
|
||||||
|
|||||||
Reference in New Issue
Block a user