-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFairy.java
More file actions
70 lines (55 loc) · 2.67 KB
/
Copy pathFairy.java
File metadata and controls
70 lines (55 loc) · 2.67 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
import processing.core.PImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class Fairy extends ScheduleActions implements Move {
public Fairy(String id, Point position, List<PImage> images, double animationPeriod, double actionPeriod){
super(id, position, images, animationPeriod, actionPeriod);
}
// protected void scheduleActions(EventScheduler scheduler, WorldModel world, ImageStore imageStore) {
// Activity activity = new Activity(this, world, imageStore);
// Animation animation = new Animation(this, 0);
// scheduler.scheduleEvent(this, activity, this.actionPeriod);
// scheduler.scheduleEvent(this, animation, this.animationPeriod);
//
// }
protected Point nextPosition(WorldModel world, Point destPos) {
int horiz = Integer.signum(destPos.x - this.position.x);
Point newPos = new Point(this.position.x + horiz, this.position.y);
if (horiz == 0 || world.isOccupied(newPos)) {
int vert = Integer.signum(destPos.y - this.position.y);
newPos = new Point(this.position.x, this.position.y + vert);
if (vert == 0 || world.isOccupied(newPos)) {
newPos = this.position;
}
}
return newPos;
}
public boolean moveTo(WorldModel world, Entity target, EventScheduler scheduler) {
if (this.position.adjacent(target.position)) {
world.removeEntity(scheduler, target);
return true;
} else {
Point nextPos = nextPosition(world, target.position);
if (!this.position.equals(nextPos)) {
world.moveEntity(scheduler, this, nextPos);
}
return false;
}
}
protected void executeActivity(WorldModel world, ImageStore imageStore, EventScheduler scheduler) {
Optional<Entity> fairyTarget = findNearest(world, this.position, new ArrayList<>(List.of("Stump")));
if (fairyTarget.isPresent()) {
Point tgtPos = fairyTarget.get().position;
if (moveTo(world, fairyTarget.get(), scheduler)) {
Sapling sapling = new Sapling(Sapling.SAPLING_KEY + "_" +
fairyTarget.get().id, tgtPos, ImageStore.getImageList(imageStore, Sapling.SAPLING_KEY), 0, Sapling.SAPLING_HEALTH_LIMIT);
world.addEntity(sapling);
sapling.scheduleActions(scheduler, world, imageStore);
}
}
Activity activity = new Activity(this, world, imageStore);
scheduler.scheduleEvent(this, activity, this.actionPeriod);
}
}