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