-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlant.java
94 lines (80 loc) · 2.59 KB
/
Plant.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
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
import java.util.List;
import java.util.Random;
/**
* A class defining the behavior of plants.
* Plants grow, spread and die of age.
*
* @author Aymen Berbache and Aleks
* @version 2022.02
*/
public abstract class Plant extends Entity {
//A shared random number generator to control the plant spreading
private static final Random rand = Randomizer.getRandom();
/**
* Create a new plant at a location in the field
* @param field The field currently occupied
* @param location The location within the field
*/
public Plant(Field field, Location location)
{
super(field, location);
}
/**
* @param newPlants A list that holds the newly created plants.
*/
public void act(List<Entity> newPlants) {
incrementAge();
// Plants grow two times more when it's rainy.
if (Simulator.weather.isRainy()) {
incrementAge();
}
// Plants spread randomly
if (isAlive() && rand.nextDouble() <= getWindySpreadingProbability()) {
createPlants(newPlants);
}
}
/**
* A plant has a probability of spreading to an adjacent location in the simulation
* @param newPlants A list of new plants
*/
private void createPlants(List<Entity> newPlants){
Field field = getField();
List<Location> free = field.getFreeAdjacentLocations(getLocation());
for(int newPlant = 0; newPlant < free.size(); newPlant++) {
Location loc = free.remove(0);
Plant newP = newPlantObject(field, loc);
newPlants.add(newP);
}
}
/**
* @return The spreading probability of the plant considering the state of the weather.
* If there is wind, plants are more likely to spread.
*/
private double getWindySpreadingProbability(){
if(Simulator.weather.isWindy()){
return getSPREADING_PROBABILITY()*2;
}else{
return getSPREADING_PROBABILITY();
}
}
//abstract methods
/**
* Creates a new animal object
* @param field The simulation field.
* @param location Location to create the new plant.
* @return A new plant object
*/
protected abstract Plant newPlantObject(Field field, Location location);
/**
* @return The plant's max age
*/
protected abstract int getMAX_AGE();
/**
* @return The plant's food value
*/
protected abstract int getFOOD_VALUE();
/**
* @return The probability that the plant will spread to a neighbouring cell
*/
protected abstract double getSPREADING_PROBABILITY();
}