-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDudeNotFull.java
More file actions
63 lines (46 loc) · 2.16 KB
/
Copy pathDudeNotFull.java
File metadata and controls
63 lines (46 loc) · 2.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
import processing.core.PImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class DudeNotFull extends Dude implements Transform, Move {
private int resourceCount;
public DudeNotFull(String id, Point position, List<PImage> images, double animationPeriod, double actionPeriod, int resourceLimit){
super(id, position, images, animationPeriod, actionPeriod, resourceLimit);
this.resourceCount = 0;
//TODO should resource count be parameter or start as 0?
}
public void executeActivity(WorldModel world, ImageStore imageStore, EventScheduler scheduler) {
Optional<Entity> target = findNearest(world, this.position, new ArrayList<>(Arrays.asList("Tree", "Sapling")));
if (target.isEmpty() || !this.moveTo(world, target.get(), scheduler) ||
!transform(world, scheduler, imageStore)) {
Activity activity = new Activity(this, world, imageStore);
scheduler.scheduleEvent(this, activity, this.actionPeriod);
}
}
@Override
public boolean transform(WorldModel world, EventScheduler scheduler, ImageStore imageStore) {
if (this.resourceCount >= this.resourceLimit) {
Dude dude = new DudeFull(this.id, this.position, this.images, this.animationPeriod, this.actionPeriod, this.resourceLimit);
world.removeEntity(scheduler, this);
scheduler.unscheduleAllEvents(this);
world.addEntity(dude);
dude.scheduleActions(scheduler, world, imageStore);
return true;
}
return false;
}
public boolean moveTo(WorldModel world, Entity target, EventScheduler scheduler) {
if (this.position.adjacent(target.position)) {
this.resourceCount += 1;
((Growable)target).health--;
return true;
} else {
Point nextPos = nextPosition(world, target.position);
if (!this.position.equals(nextPos)) {
world.moveEntity(scheduler, this, nextPos);
}
return false;
}
}
}