-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFairy.java
More file actions
91 lines (75 loc) · 3.43 KB
/
Fairy.java
File metadata and controls
91 lines (75 loc) · 3.43 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import processing.core.PImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class Fairy extends ActivityEntity implements Move, PathingStrategy {
public Fairy(String id, Point position, double animationPeriod, double actionPeriod, List<PImage> images) {
super(id, position, animationPeriod, actionPeriod, images);
}
@Override
public boolean moveTo(WorldModel world, Entity target, EventScheduler scheduler) {
if (this.getPosition().adjacent(target.getPosition())) {
world.removeEntity(scheduler, target);
return true;
} else {
Point nextPos = this.nextPosition(world, target.getPosition());
if (!this.getPosition().equals(nextPos)) {
world.moveEntity(scheduler, this, nextPos);
}
return false;
}
}
@Override
public Point nextPosition(WorldModel world, Point destPos) {
Predicate<Point> canPassThrough = p -> world.withinBounds(p)
&& (world.getOccupant(p).isEmpty() || world.getOccupant(getPosition()).get().getClass().equals(House.class));
BiPredicate<Point, Point> withinReach = Point::adjacent;
PathingStrategy strategy = new AStarPathingStrategy();
List<Point> path = strategy.computePath(
getPosition(),
destPos,
canPassThrough,
withinReach,
PathingStrategy.CARDINAL_NEIGHBORS
);
if (path == null || path.isEmpty()) {
return getPosition();
}
return path.getFirst();
}
// int horiz = Integer.signum(destPos.x - getPosition().x);
// Point newPos = new Point(getPosition().x + horiz, getPosition().y);
//
// if (horiz == 0 || world.getOccupant(newPos).isPresent() && world.getOccupant(newPos).get().getClass() != House.class) {
// int vert = Integer.signum(destPos.y - getPosition().y);
// newPos = new Point(getPosition().x, getPosition().y + vert);
//
// if (vert == 0 || world.getOccupant(newPos).isPresent() && world.getOccupant(newPos).get().getClass() != House.class) {
// newPos = getPosition();
// }
// }
//
// return newPos;
// }
@Override
public void executeActivity(WorldModel world, ImageStore imageStore, EventScheduler scheduler) {
Optional<Entity> fairyTarget = world.findNearest(getPosition(), new ArrayList<>(List.of(Stump.class)));
if (fairyTarget.isPresent()) {
Point tgtPos = fairyTarget.get().getPosition();
if (this.moveTo(world, fairyTarget.get(), scheduler)) {
Sapling sapling = Factory.createSapling(WorldLoader.SAPLING_KEY + "_" + fairyTarget.get().getId(), tgtPos, imageStore.getImageList(WorldLoader.SAPLING_KEY));
world.tryAddEntity(sapling);
sapling.scheduleActions(scheduler, world, imageStore);
}
}
scheduler.scheduleEvent(this, Factory.createActivityAction(this, world, imageStore), getActionPeriod());
}
@Override
public List<Point> computePath(Point start, Point end, Predicate<Point> canPassThrough, BiPredicate<Point, Point> withinReach, Function<Point, Stream<Point>> potentialNeighbors) {
return null;
}
}