-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.pde
More file actions
116 lines (102 loc) · 3.06 KB
/
Tile.pde
File metadata and controls
116 lines (102 loc) · 3.06 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Tile {
int x, y;
Stack<Integer> colorStack = new Stack<Integer>();
ArrayList<Pheromone> pheromones = new ArrayList<Pheromone>();
float foodLevel = 0;
TileAgent agent;
Tile(int x, int y) {
this.x = x;
this.y = y;
colorStack.push(#FFFFFF);
}
void reset() {
colorStack.clear();
colorStack.push(#FFFFFF);
pheromones.clear();
foodLevel = 0;
agent = null;
}
void draw() {
if (selectedAgent != null && agent == selectedAgent)
selectedTile = this;
stroke(0);
fill(colorStack.peek());
square(x * tileSize, y * tileSize, tileSize);
if (pheromones.size() > 0) {
Pheromone pheromone = pheromones.get(pheromones.size() - 1);
color c = pheromone.colony.antColor;
fill(color(
red(c),
green(c),
blue(c),
float(pheromone.strength) / pheromoneLength * 255
));
noStroke();
square(
x * tileSize + tileSize / 4,
y * tileSize + tileSize / 4,
tileSize / 2
);
}
}
void frame() {
for (Pheromone pheromone : pheromones) {
pheromone.frame();
}
pheromones.removeIf(p->p.strength <= 0);
}
void drawOutline(color stroke) {
stroke(stroke);
noFill();
square(x * tileSize, y * tileSize, tileSize);
}
void pushColor(color col) { colorStack.push(col); }
color popColor() {
if (colorStack.size() == 1) {
// new Exception().printStackTrace();
}
return colorStack.pop();
}
void pushFood(float level) {
foodLevel = level;
if (level > 0) {
colorMode(HSB, 360, 100, 100);
pushColor(color(101, 100, foodLevel * 50 + 50));
colorMode(RGB, 255, 255, 255);
}
}
ArrayList<Tile> neighbors() {
ArrayList<Tile> tiles = new ArrayList<Tile>();
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (!(i == 0 && j == 0)) {
Tile relTile = getRelativeTile(i, j);
if (relTile != this) {
tiles.add(relTile);
}
}
}
}
return tiles;
}
Tile getRelativeTile(int x, int y) {
if (wrapAround) {
return grid.get(
(this.x + x + grid.width) % grid.width,
(this.y + y + grid.height) % grid.height
); // Wrap around
} else {
return grid.get(
clamp(this.x + x, 0, grid.width - 1),
clamp(this.y + y, 0, grid.height - 1)
); // stop at the edges
}
}
boolean containsPheromone(Colony colony, int type) {
for (Pheromone pheromone : pheromones) {
if (pheromone.colony == colony && pheromone.type == type)
return true;
}
return false;
}
}