-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduleActions.java
More file actions
69 lines (52 loc) · 2.21 KB
/
Copy pathScheduleActions.java
File metadata and controls
69 lines (52 loc) · 2.21 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
import processing.core.PImage;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
public abstract class ScheduleActions extends Entity{
protected double animationPeriod;
protected double actionPeriod;
protected ScheduleActions(String id, Point position, List<PImage> images, double animationPeriod, double actionPeriod){
super(id, position, images);
this.actionPeriod = actionPeriod;
this.animationPeriod = animationPeriod;
}
// public double getAnimationPeriod()
// //TODO trash
// {
// return this.animationPeriod;
// }
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 Optional<Entity> findNearest(WorldModel world, Point pos, List<String> names) {
List<Entity> ofType = new LinkedList<>();
for (String name : names) {
for (Entity entity : world.entities) {
if (String.valueOf(entity.getClass().getName()).equals(name)) {
ofType.add(entity);
}
}
}
return nearestEntity(ofType, pos);
}
private Optional<Entity> nearestEntity(List<Entity> entities, Point pos) {
if (entities.isEmpty()) {
return Optional.empty();
} else {
Entity nearest = entities.get(0);
int nearestDistance = nearest.position.distanceSquared(pos);
for (Entity other : entities) {
int otherDistance = other.position.distanceSquared(pos);
if (otherDistance < nearestDistance) {
nearest = other;
nearestDistance = otherDistance;
}
}
return Optional.of(nearest);
}
}
protected abstract void executeActivity(WorldModel world, ImageStore imageStore, EventScheduler scheduler);
}