-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlant.java
207 lines (188 loc) · 6.88 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A class representing shared characteristics of all plants.
*
* @author Syraj Alkhalil and Cosmo Colman
* @version 2022.02.27 (2)
*/
public class Plant extends Organism{
// characteristics all plants share
private int currentLevel; // The current level of the plant
private int waterLevel; // The water level of the plant (plants will die if they don't get enough water)
private int sunLightLevel; // The sunlight level of the plant (plants will die if they don't get enough sunlight)
private final PlantStats plantStats; // The statistics of the plant ranges from max level to food level
/**
* Create a plant. A plant can be created as with a field and
* a location
* @param stats the statistics of the plant
* @param field The field currently occupied.
* @param initLocation The location within the field.
*/
public Plant(PlantStats stats, Field field, Location initLocation) {
super(stats, field, initLocation);
this.plantStats = stats;
this.currentLevel = 1;
this.waterLevel = 1;
this.sunLightLevel = 1;
}
/**
* This method is responsible for making a J-panel that will hold all the statistics
* of the plant this ranges from the water level of the plant to the sunlight level
* all the stats will be displayed on the gui IF the user hovers over the plant
*
* @return the statistics of the animal in gui form
*/
public JPanel getInspectPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(new LineBorder(getStats().getColor(), 4));
ArrayList<String> labelItems = new ArrayList<>(Arrays.asList(
"Level:",this.getCurrentLevel() + "/" + ((PlantStats)this.getStats()).getMaxLevel(),
"Water Level:", waterLevel + "",
"Sunlight Level:", sunLightLevel + ""));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
panel.add(new JLabel(this.getStats().getName()), gbc);
gbc.gridwidth = 1;
gbc.gridy++;
for (int index = 0; index < labelItems.size(); index += 2){
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
panel.add(new JLabel(labelItems.get(index)), gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridx = 1;
panel.add(new JLabel(labelItems.get(index+1)), gbc);
gbc.gridy++;
}
return panel;
}
/**
* This is what the plant does most of the time: 'breed', photosynthesise, and grow.
*
* @param newPlants A list to return newly born plants.
* @param isDay is it currently day or night ?
* @param currentWeather the current weather
*/
@Override
public void act(List<Organism> newPlants, boolean isDay, Weather currentWeather) {
if(isDay){
grow(currentWeather);
if(this.currentLevel > 2){
giveBirth(newPlants);
}
}
}
/**
* Allow the plants to grow according to the current weather
* plants may die in this process
*
* @param currentWeather the current weather
*/
private void grow(Weather currentWeather) {
photosynthesis(currentWeather);
transpiration(currentWeather);
// allow growth
if(this.sunLightLevel > 10 && this.waterLevel > 12){
// then increment the level
if(this.currentLevel < ((PlantStats)this.getStats()).getMaxLevel()){
this.currentLevel += 1;
// we should also reduce the resources of plants IFF they grow
this.waterLevel = 3;
this.sunLightLevel = 2;
}
}
}
/**
* This method is responsible for the plants getting the water they need.
* plants get water from rain
*
* @param currentWeather the current weather
*/
private void transpiration(Weather currentWeather){
// neater way of writing if else blocks ew
this.waterLevel = (currentWeather.getActualDownfall() < 10) ? this.waterLevel - 1 : this.waterLevel + 1;
if(this.waterLevel <= 0){
setDead();
}
}
/**
* This method is responsible for the plants getting the sunlight they need
* plants get the sunlight they need depending on the visibility in the current weather
*
* @param currentWeather the current weather
*/
private void photosynthesis(Weather currentWeather){
// neater way of writing if else blocks ew
this.sunLightLevel = (currentWeather.getActualVisibility() < 10) ? this.sunLightLevel - 1 : this.sunLightLevel + 1;
if(this.sunLightLevel <= 0){
setDead();
}
}
/**
* set the plant to dead if the level is not 0 then reduce the level by 1
* plants don't die as normal animals instead if the level of the plant is 0 then we allow the plants to die normally
*/
@Override
protected void setDead() {
if(this.currentLevel <= 0){
super.setDead();
}else{
currentLevel -= 1;
setLocation(getLocation());
}
}
/**
* Check whether this plant is to give birth at this step.
* New births will be made into free adjacent locations.
*
* @param newPlants A list to return newly born plants.
*/
private void giveBirth(List<Organism> newPlants) {
// New plants are born into adjacent locations.
// Get a list of adjacent free locations.
Field field = getField();
List<Location> free = field.getFreeAdjacentLocations(getLocation());
int births = breed();
for(int b = 0; b < births && free.size() > 0; b++) {
Location loc = free.remove(0);
Plant young = new Plant(plantStats, field, loc);
newPlants.add(young);
}
}
/**
* Generate a number representing the number of births,
* if it can breed.
*
* @return The number of births (maybe zero).
*/
private int breed() {
int births = 0;
if(getRand().nextDouble() <= this.getStats().getBreedingProbability()) {
births = getRand().nextInt(4) + 1;
}
return births;
}
/**
* A simple getter method that returns the currentLevel field
*
* @return the current level of the plant
*/
public int getCurrentLevel() {
return currentLevel;
}
/**
* A simple getter method that returns the plantStats field
*
* @return the stats of the plant
*/
public EntityStats getStats() {
return plantStats;
}
}