-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinerNotFull.java
138 lines (117 loc) · 5.17 KB
/
MinerNotFull.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import processing.core.PImage;
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 MinerNotFull extends MinerEntity
{
public MinerNotFull(String id, int resourceLimit, Point position,
int actionPeriod, int animationPeriod, List<PImage> images)
{
super(id, resourceLimit, position, actionPeriod, animationPeriod, images);
}
public void executeActivity(ActiveEntity entity, WorldModel world,
ImageStore imageStore, EventScheduler scheduler)
{
Optional<WorldEntity> notFullTarget = world.findNearest(entity.getPosition(),
"Ore");
if (!notFullTarget.isPresent() ||
!moveTo((MobileEntity) entity, world, notFullTarget.get(), scheduler) ||
!transform((MinerEntity) entity, world, scheduler, imageStore))
{
scheduler.scheduleEvent(entity,
new Activity(entity, world, imageStore),
entity.getActionPeriod());
}
}
public Point nextPosition(MobileEntity entity, WorldModel world,
Point destPos)
{
int horiz = Integer.signum(destPos.getX() - entity.getPosition().getX());
Point newPos = new Point(entity.getPosition().getX() + horiz,
entity.getPosition().getY());
if (horiz == 0 || world.isOccupied(newPos))
{
int vert = Integer.signum(destPos.getY()
- entity.getPosition().getY());
newPos = new Point(entity.getPosition().getX(),
entity.getPosition().getY() + vert);
if (vert == 0 || world.isOccupied(newPos))
{
newPos = entity.getPosition();
}
}
return newPos;
}
public boolean moveTo(MobileEntity miner, WorldModel world,
WorldEntity target, EventScheduler scheduler)
{
if (miner.getPosition().adjacent(target.getPosition()))
{
((MinerEntity)miner).setResourceCount(((MinerEntity)miner).getResourceCount() + 1);
world.removeEntity(target);
scheduler.unscheduleAllEvents(target);
return true;
}
else
{
/* int horiz = Integer.signum(target.getPosition().getX() - miner.getPosition().getX());
Point nextPos = new Point(miner.getPosition().getX() + horiz,
miner.getPosition().getY());
if (horiz == 0 || world.isOccupied(nextPos))
{
int vert = Integer.signum(target.getPosition().getY()
- miner.getPosition().getY());
nextPos = new Point(miner.getPosition().getX(),
miner.getPosition().getY() + vert);
if (vert == 0 || world.isOccupied(nextPos))
{
nextPos = miner.getPosition();
}
}*/
Predicate<Point> canPassThrough = (Point p) -> !(world.isOccupied(p));
BiPredicate<Point, Point> withinReach = (p1,p2) -> (
((p1.getX() == p2.getX())
&& ((p1.getY() == p2.getY()-1) || (p1.getY() == p2.getY()+1)))
|| ((p1.getY() == p2.getY())
&& ((p1.getX() == p2.getX()-1) || (p1.getX() == p2.getX()+1))));
Function<Point, Stream<Point>> potentialNeighbors = PathingStrategy.CARDINAL_NEIGHBORS;
PathingStrategy s = new AStarPathingStrategy();
List<Point> path = s.computePath(miner.getPosition(), target.getPosition(), canPassThrough, withinReach, potentialNeighbors);
if(path.size() > 0)
{
Point nextPos = path.get(0);
if (!miner.getPosition().equals(nextPos)) {
Optional<WorldEntity> occupant = world.getOccupant(nextPos);
if (occupant.isPresent()) {
scheduler.unscheduleAllEvents(occupant.get());
}
world.moveEntity(miner, nextPos);
}
}
return false;
}
}
public boolean transform(MinerEntity entity, WorldModel world,
EventScheduler scheduler, ImageStore imageStore)
{
if (entity.getResourceCount() >= entity.getResourceLimit())
{
MinerFull miner = new MinerFull(entity.getId(),
entity.getResourceLimit(), entity.getPosition(),
entity.getActionPeriod(), entity.getAnimationPeriod(),
entity.getImages());
world.removeEntity(entity);
scheduler.unscheduleAllEvents(entity);
world.addEntity(miner);
miner.scheduleActions(world, imageStore, scheduler);
return true;
}
return false;
}
public <Boolean> Boolean accept(EntityVisitor<Boolean> visitor) {
return visitor.visit(this);
}
}