/
FollowPathGoal.java
67 lines (56 loc) · 1.84 KB
/
FollowPathGoal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package net.citizensnpcs.api.ai.goals;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.Location;
import net.citizensnpcs.api.ai.Goal;
import net.citizensnpcs.api.ai.tree.Behavior;
import net.citizensnpcs.api.ai.tree.BehaviorGoalAdapter;
import net.citizensnpcs.api.ai.tree.BehaviorStatus;
import net.citizensnpcs.api.npc.NPC;
/**
* A sample {@link Goal}/{@link Behavior} that simply moves an {@link NPC} through a list of {@link Location}s.
*/
public class FollowPathGoal extends BehaviorGoalAdapter {
private int idx;
private final NPC npc;
private final List<MoveToGoal> path;
public FollowPathGoal(NPC npc, List<MoveToGoal> path) {
this.npc = npc;
this.path = path;
}
@Override
public void reset() {
npc.getNavigator().cancelNavigation();
idx = 0;
}
@Override
public BehaviorStatus run() {
if (idx >= path.size())
return BehaviorStatus.SUCCESS;
if (!npc.getNavigator().isNavigating()) {
setPath();
}
BehaviorStatus status = path.get(idx).run();
if (status == BehaviorStatus.SUCCESS) {
idx++;
setPath();
return BehaviorStatus.RUNNING;
}
return status;
}
private void setPath() {
if (idx >= path.size())
return;
path.get(idx).shouldExecute();
}
@Override
public boolean shouldExecute() {
return !npc.getNavigator().isNavigating() && path != null;
}
public static FollowPathGoal create(NPC npc, List<MoveToGoal> path) {
return new FollowPathGoal(npc, path);
}
public static FollowPathGoal createFromLocations(NPC npc, List<Location> path) {
return new FollowPathGoal(npc, path.stream().map(loc -> new MoveToGoal(npc, loc)).collect(Collectors.toList()));
}
}