-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plant.java
187 lines (171 loc) · 4.97 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
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
public class Plant implements Organism{
// Constants (specific to this animal type)
static final String type = "Plant";
ArrayList<String> preyTypes = null;
static final double maxHealth = 30;
static final double healthLostPerGameTick = -0.02; // Plant gains health per game tick
static final double probabilityGivingBirth = 0.00002;
static final int avgNumBabies = 2;
// Variables (to be set)
int id;
int X, Y;
double health;
int state = 0;
Dimension D;
// Constructor ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Plant(int id, Point2D.Double randomPoint, Dimension D){
// Create a plant at a location X,Y
this.id = id;
this.X = (int)randomPoint.x;
this.Y = (int)randomPoint.y;
this.D = D;
// set initial health
this.health = generateRandomInitialHealth();
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~ Control Methods: To be called by AnimalSimulator ~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// Step 1: Update Health
public void updateHealthTime(){
if(this.health < this.maxHealth){
this.health = this.health - this.healthLostPerGameTick; // adds health
if(this.health > this.maxHealth){
this.health = this.maxHealth;
}
}
}
// Step 2: Update State
public void updateState(ArrayList<Organism> organisms) {
// Plants can't move,
// they never changes state(int)
}
// Step 3: Move
public void move() {
//plant doesn't move
//
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~ Utility methods - called by control methods ~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
public Point2D.Double randomWalk(){
//do nothing. plants don't move!
return (new Point2D.Double(this.X, this.Y));
}
public Point2D.Double hunt(){
//plant doesnt move
return (new Point2D.Double(this.X, this.Y));
}
public Point2D.Double escape(){
// Plants can't escape. The just die.
return (new Point2D.Double(this.X, this.Y));
}
public ArrayList<Organism> getOrganismsWithinSightRadius(ArrayList<Organism> organisms){
// Plants can't see
return null;
}
public ArrayList<Organism> getNearbyPrey(ArrayList<Organism> organisms){
// Plants don't have prey
return null;
}
public Organism getClosestPrey(ArrayList<Organism> nearbyPrey){
// Plants don't have prey
return null;
}
public ArrayList<Organism> getNearbyPredators(ArrayList<Organism> nearbyOrganisms){
// plant can't see Predators, so who cares?
return null;
}
public Organism getClosestPredator(ArrayList<Organism> nearbyPrey){
// plant can't run away from predators, so who cares?
return null;
}
public void eatPrey(){
// Plants dont eat prey
//
}
public boolean isPointWithinBoundary(Point2D.Double point){
/* Check to see if a point is within screen boundary */
int x = (int)point.x;
int y = (int)point.y;
int screenWidth = D.width;
int screenHeight = D.height;
if(x >= 0 && y >= 0 && x < screenWidth && y < screenHeight){
return true;
}
else{
return false;
}
}
public double generateRandomInitialHealth(){ // Called by constructor
// generate a starting health point value between 0.5*maxHealth and maxHealth
Random r = new Random();
double rangeMin = (maxHealth/2.0);
double range = maxHealth - rangeMin + 1.0;
double randomHealth = rangeMin + (range) * r.nextDouble();
return randomHealth;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* ~~~~~~~~~~ Get and Set (These should be the same for all organisms - except for plant) ~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// Get
public int getID(){
return this.id;
}
public String getType(){
return this.type;
}
public int getX(){
return this.X;
}
public int getY(){
return this.Y;
}
public Point2D.Double getXY(){
return new Point2D.Double(this.X, this.Y);
}
public double getHealth(){
return this.health;
}
public double getMaxHealth(){
return this.maxHealth;
}
public int getSightRadius(){
return 0;
}
public int getState(){
return state;
}
public boolean isGivingBirth(){
if(Math.random() <= probabilityGivingBirth){
return true;
}
return false;
}
public int getNumBabiesProduced(){
return (int)RandTool.gaussian(avgNumBabies, 1);
}
// Set
public void setX(int x){
this.X = x;
}
public void setY(int y){
this.Y = y;
}
public void setXY(Point2D.Double point){
this.X = (int)point.x;
this.Y = (int)point.y;
}
public void setTargetLocation(Point2D.Double point){
// Plants don't have a target location.
}
public void setHealth(double health) {
this.health = health;
}
// To String ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public String toString(){
return this.type + " " + this.id;
}
}