-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pheromone.java
54 lines (50 loc) · 1.39 KB
/
Pheromone.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* Pheromones are dropped by ants when they want to communicate something to
* other ants.
*
* @author Michael Kölling
* @version 1.1
*/
public class Pheromone extends Actor
{
private final static int MAX_INTENSITY = 180;
private int intensity;
/**
* Create a new drop of pheromone with full intensity.
*/
public Pheromone()
{
intensity = MAX_INTENSITY;
updateImage();
}
/**
* The pheromone decreases the intensity. When the intensity reaches zero, it disappears.
*/
public void act()
{
intensity -= 1;
if (intensity <= 0) {
getWorld().removeObject(this);
}
else {
if ((intensity % 4) == 0) { // every four steps...
updateImage();
}
}
}
/**
* Make the image. The size and transparency are proportional to the intensity.
*/
private void updateImage()
{
int size = intensity / 3 + 5;
GreenfootImage image = new GreenfootImage(size + 1, size + 1);
int alpha = intensity / 3;
image.setColor(new Color(255, 255, 255, alpha));
image.fillOval(0, 0, size, size);
image.setColor(Color.DARK_GRAY);
image.fillRect(size / 2, size / 2, 2, 2); // small dot in the middle
setImage(image);
}
}