-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFarmerSearching.java
More file actions
110 lines (90 loc) · 4.16 KB
/
FarmerSearching.java
File metadata and controls
110 lines (90 loc) · 4.16 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import processing.core.PImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class FarmerSearching extends Farmer {
private int resourceCount;
double ActionPeriod = .5;
public FarmerSearching(String id, Point position, double animationPeriod, double actionPeriod, int resourceLimit, List<PImage> images) {
super(id, position, animationPeriod, actionPeriod, resourceLimit, images);
}
@Override
public void executeActivity(WorldModel world, ImageStore imageStore, EventScheduler scheduler) {
Optional<Entity> farmerSearchingTarget = world.findNearest(getPosition(),
new ArrayList<>(List.of(WolfSearching.class, WolfFull.class)));
if (farmerSearchingTarget.isPresent() && (moveTo(world, farmerSearchingTarget.get(), scheduler)
|| transform(world, scheduler, imageStore))) {
farmerSearchingTarget.ifPresent(entity -> ((Wolf) entity).transformWolf(world, scheduler, imageStore));
scheduler.scheduleEvent(this, Factory.createActivityAction(this, world, imageStore), this.ActionPeriod);
if (countWolves(world) < Factory.MAX_WOLVES) {
addNewWolf(world, imageStore, scheduler);
}
} else if (farmerSearchingTarget.isPresent()) {
scheduler.scheduleEvent(this, Factory.createActivityAction(this, world, imageStore), this.ActionPeriod);
}
}
private int countWolves(WorldModel world) {
int count = 0;
for (Entity entity : world.getEntities()) {
if (entity instanceof Wolf) {
count++;
}
}
return count;
}
private void addNewWolf(WorldModel world, ImageStore imageStore, EventScheduler scheduler) {
Random rand = new Random();
int randCols = rand.nextInt(world.getNumCols());
int randRows = rand.nextInt(world.getNumRows());
Point randomPoint = new Point(randCols, randRows);
while (!world.withinBounds(randomPoint) || world.getOccupant(randomPoint).isPresent()) {
randCols = rand.nextInt(world.getNumCols());
randRows = rand.nextInt(world.getNumRows());
randomPoint = new Point(randCols, randRows);
}
WolfSearching wolf = new WolfSearching(this.getId(), randomPoint, getAnimationPeriod(), getActionPeriod(), getResourceLimit(), imageStore.getImageList("wolf"));
world.tryAddEntity(wolf);
scheduler.scheduleEvent(wolf, Factory.createActivityAction(wolf, world, imageStore), wolf.getActionPeriod());
}
@Override
public boolean moveTo(WorldModel world, Entity target, EventScheduler scheduler) {
if (this.getPosition().adjacent(target.getPosition())) {
this.resourceCount += 1;
if (target instanceof Wolf) {
((Wolf) target).decreaseHealth();
}
return true;
} else {
Point nextPos = this.nextPosition(world, target.getPosition());
if (!this.getPosition().equals(nextPos)) {
world.moveEntity(scheduler, this, nextPos);
}
return false;
}
}
@Override
public boolean transform(WorldModel world, EventScheduler scheduler, ImageStore imageStore) {
if (resourceCount >= getResourceLimit()) {
Farmer farmer = Factory.createFarmerFull(getId(), getPosition(), getActionPeriod(), getAnimationPeriod(), getResourceLimit(), getImages());
scheduler.unscheduleAllEvents(this);
world.removeEntity(scheduler, this);
world.tryAddEntity(farmer);
farmer.scheduleActions(scheduler, world, imageStore);
return true;
}
return false;
}
@Override
public int getHealth() {
return 0;
}
@Override
public List<Point> computePath(Point start, Point end, Predicate<Point> canPassThrough, BiPredicate<Point, Point> withinReach, Function<Point, Stream<Point>> potentialNeighbors) {
return null;
}
}