-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVein.java
More file actions
81 lines (69 loc) · 2.23 KB
/
Vein.java
File metadata and controls
81 lines (69 loc) · 2.23 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
import processing.core.PImage;
import java.util.List;
import java.util.Random;
public class Vein
extends Actor
{
private static final int DEFAULT_DISTANCE = 1;
private int resourceDistance;
private static final int ORE_CORRUPT_MIN = 20000;
private static final int ORE_CORRUPT_MAX = 30000;
private static final Random rand = new Random();
public Vein(String name, Point position, int rate, int resourceDistance,
List<PImage> imgs)
{
super(name, position, rate, imgs);
this.resourceDistance = resourceDistance;
}
public Vein(String name, Point position, int rate, List<PImage> imgs)
{
this(name, position, rate, DEFAULT_DISTANCE, imgs);
}
public String toString()
{
return String.format("vein %s %d %d %d %d", this.getName(),
this.getPosition().x, this.getPosition().y, this.getRate(),
this.resourceDistance);
}
public Action createAction(WorldModel world, ImageStore imageStore)
{
Action[] action = { null };
action[0] = ticks -> {
removePendingAction(action[0]);
Point openPt = findOpenAround(world, getPosition(), resourceDistance);
if (openPt != null)
{
Ore ore = createOre(world, "ore - " + getName() + " - " + ticks,
openPt, ticks, imageStore);
world.addEntity(ore);
}
scheduleAction(world, this, createAction(world, imageStore),
ticks + getRate());
};
return action[0];
}
private Ore createOre(WorldModel world, String name, Point pt,
long ticks, ImageStore imageStore)
{
Ore ore = new Ore(name, pt,
ORE_CORRUPT_MIN + rand.nextInt(ORE_CORRUPT_MAX - ORE_CORRUPT_MIN),
imageStore.get("ore"));
ore.schedule(world, ticks, imageStore);
return ore;
}
private Point findOpenAround(WorldModel world, Point pt, int distance)
{
for (int dy = -distance; dy <= distance; dy++)
{
for (int dx = -distance; dx <= distance; dx++)
{
Point newPt = new Point(pt.x + dx, pt.y + dy);
if (world.withinBounds(newPt) && !world.isOccupied(newPt))
{
return newPt;
}
}
}
return null;
}
}