Make PathExecutor return PathProgress
This commit is contained in:
@@ -10,10 +10,10 @@ public class FollowPathAI implements AI {
|
||||
private final NPCPath path;
|
||||
|
||||
public FollowPathAI(NPC npc) {
|
||||
this(npc, true);
|
||||
this(npc, null);
|
||||
}
|
||||
|
||||
public FollowPathAI(NPC npc, boolean repeat) {
|
||||
public FollowPathAI(NPC npc, Integer repeat) {
|
||||
var path = new NPCPath();
|
||||
this.executor = new PathExecutor<>(npc, repeat, path);
|
||||
this.path = path;
|
||||
|
||||
@@ -20,14 +20,18 @@ public class MoveSegment<T extends Movable> implements PathSegment<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean perform(T movable, ObjectLayer layer, float dt) {
|
||||
public PathProgress perform(T movable, ObjectLayer layer, float dt) {
|
||||
if (movable.isMoving()) {
|
||||
return PathProgress.ONGOING;
|
||||
}
|
||||
|
||||
var movement = movable.prepareMovement(direction);
|
||||
|
||||
if (ignore || layer.isTileReachable(movement.getTo())) {
|
||||
layer.pushMovement(movement);
|
||||
return true;
|
||||
return PathProgress.SEGMENT_DONE;
|
||||
}
|
||||
|
||||
return false;
|
||||
return PathProgress.SEGMENT_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,32 +9,42 @@ import java.util.Objects;
|
||||
public class PathExecutor<T extends Movable> {
|
||||
protected final List<PathSegment<T>> path;
|
||||
private final T movable;
|
||||
private final boolean repeat;
|
||||
private final Integer repeat;
|
||||
|
||||
private int current = 0;
|
||||
private int iteration = 0;
|
||||
private boolean updated = false;
|
||||
|
||||
public PathExecutor(T movable, boolean repeat, Path<T> path) {
|
||||
public PathExecutor(T movable, Integer repeat, Path<T> path) {
|
||||
this.movable = movable;
|
||||
this.repeat = repeat;
|
||||
this.path = Objects.requireNonNull(path).getPath();
|
||||
}
|
||||
|
||||
public boolean execute(ObjectLayer layer, float dt) {
|
||||
if (!repeat && isRetired()) {
|
||||
return false;
|
||||
public PathProgress execute(ObjectLayer layer, float dt) {
|
||||
var size = path.size();
|
||||
|
||||
if (current == size - 1 && !updated) {
|
||||
++iteration;
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (!movable.isMoving()) {
|
||||
var item = path.get(current % path.size());
|
||||
if (item.perform(movable, layer, dt)) {
|
||||
if (current == size) {
|
||||
updated = false;
|
||||
|
||||
if (repeat != null && iteration >= repeat) {
|
||||
return PathProgress.DONE;
|
||||
} else {
|
||||
current = 0;
|
||||
return PathProgress.REPEAT;
|
||||
}
|
||||
}
|
||||
|
||||
var result = path.get(current).perform(movable, layer, dt);
|
||||
if (result == PathProgress.SEGMENT_DONE) {
|
||||
++current;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isRetired() {
|
||||
return current == path.size() - 1;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.bartlomiejpluta.base.api.util.path;
|
||||
|
||||
public enum PathProgress {
|
||||
ONGOING,
|
||||
SEGMENT_DONE,
|
||||
SEGMENT_FAILED,
|
||||
REPEAT,
|
||||
DONE,
|
||||
}
|
||||
@@ -4,5 +4,5 @@ import com.bartlomiejpluta.base.api.game.entity.Movable;
|
||||
import com.bartlomiejpluta.base.api.game.map.layer.object.ObjectLayer;
|
||||
|
||||
public interface PathSegment<T extends Movable> {
|
||||
boolean perform(T movable, ObjectLayer layer, float dt);
|
||||
PathProgress perform(T movable, ObjectLayer layer, float dt);
|
||||
}
|
||||
|
||||
@@ -13,8 +13,8 @@ public class RunSegment<T extends Movable> implements PathSegment<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean perform(T movable, ObjectLayer layer, float dt) {
|
||||
public PathProgress perform(T movable, ObjectLayer layer, float dt) {
|
||||
runnable.run();
|
||||
return true;
|
||||
return PathProgress.SEGMENT_DONE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,8 @@ public class TurnSegment<T extends NPC> implements PathSegment<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean perform(T npc, ObjectLayer layer, float dt) {
|
||||
public PathProgress perform(T npc, ObjectLayer layer, float dt) {
|
||||
npc.setFaceDirection(direction);
|
||||
return true;
|
||||
return PathProgress.SEGMENT_DONE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ public class WaitSegment<T extends Movable> implements PathSegment<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean perform(T movable, ObjectLayer layer, float dt) {
|
||||
public PathProgress perform(T movable, ObjectLayer layer, float dt) {
|
||||
accumulator += dt;
|
||||
|
||||
if (accumulator > seconds) {
|
||||
accumulator = 0.0f;
|
||||
return true;
|
||||
return PathProgress.SEGMENT_DONE;
|
||||
}
|
||||
|
||||
return false;
|
||||
return PathProgress.ONGOING;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user