-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.java
More file actions
100 lines (86 loc) · 2.55 KB
/
Worker.java
File metadata and controls
100 lines (86 loc) · 2.55 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
import processing.core.PImage;
import java.util.ArrayList;
import java.util.List;
public class Worker
extends MobileAnimatedActor
{
private static final int QUAKE_ANIMATION_RATE = 100;
public Worker(String name, Point position, int rate,
int animation_rate, List<PImage> imgs)
{
super(name, position, rate, animation_rate, imgs);
}
public String toString()
{
return String.format("moving_blacksmith %s %d %d", this.getName(),
this.getPosition().x, this.getPosition().y);
}
protected boolean canPassThrough(WorldModel world, Point pt)
{
return !world.isOccupied(pt);
}
public Action createAction(WorldModel world, ImageStore imageStore)
{
Action[] action = { null };
action[0] = ticks -> {
removePendingAction(action[0]);
WorldEntity target = world.findNearest(getPosition(), Ship.class);
long nextTime = ticks + getRate();
if (target != null)
{
Point tpt = target.getPosition();
if(move(world,target))
{
Quake quake = createQuake(world, tpt, ticks, imageStore);
world.addEntity(quake);
nextTime = nextTime + getRate();
}
}
else
{
List<Alien> aliens = new ArrayList<>();
for (WorldEntity ent : world.getEntities())
{
if (ent instanceof Alien)
{
Alien alien = (Alien) ent;
aliens.add(alien);
}
}
for(Alien alien : aliens)
{
alien.gonna_die(world, ticks, imageStore);
nextTime = nextTime + getRate();
}
}
scheduleAction(world, this, createAction(world, imageStore),
nextTime);
};
return action[0];
}
protected boolean move(WorldModel world, WorldEntity ship)
{
if (ship == null)
{
return false;
}
if (adjacent(getPosition(), ship.getPosition()))
{
ship.remove(world);
return true;
}
else
{
world.moveEntity(this, nextPosition(world, ship.getPosition()));
return false;
}
}
private Quake createQuake(WorldModel world, Point pt, long ticks,
ImageStore imageStore)
{
Quake quake = new Quake("quake", pt, QUAKE_ANIMATION_RATE,
imageStore.get("quake"));
quake.schedule(world, ticks, imageStore);
return quake;
}
}