-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.java
138 lines (124 loc) · 3.6 KB
/
Entity.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
import java.util.Random;
/**
* An entity is a super class that all entities represented on the simulation share.
* This includes the disease, animals, plants etc...
*
* @author Syraj Alkhalil and Cosmo Colman
* @version 2022.02.27 (2)
*/
public class Entity {
private int ageInSteps; // get the age of the entity in steps
private Field field; // The field occupied by the entity
private boolean isAlive; // Is the entity currently alive
private Location location; // The location of the entity
private static final Random rand = Randomizer.getRandom(); // The randomness all entities share
private final EntityStats entityStats; // The entity stats these are subject to change
/**
* Constructor for objects of class Entity
*/
public Entity(EntityStats stats ,Field field, Location initLocation) {
// initialise instance variables
this.entityStats = stats;
this.ageInSteps = 0;
this.isAlive = true;
this.field = field;
setLocation(initLocation);
}
/**
* A simple method to return the age of the entity in days instead of steps
*
* @return the current age of the entity
*/
public double getAgeInDays() {
double value = getAgeInSteps() / 24.0;
return Math.floor(value * 100) / 100;
}
/**
* A simple getter method to get the age field
*
* @return the current age of the entity
*/
public int getAgeInSteps() {
return ageInSteps;
}
/**
* A simple setter method to set the field
*
* @param field the field we want the entity to act in.
*/
public void setField(Field field) {
this.field = field;
}
/**
* Return the animal's field.
*
* @return The animal's field.
*/
public Field getField() {
return field;
}
/**
* A simple setter method to set the age of the entity
*
* @param ageInSteps the age we want the entity to have
*/
public void setAgeInSteps(int ageInSteps) {
this.ageInSteps = ageInSteps;
}
/**
* Return the animal's location.
*
* @return The animal's location.
*/
public Location getLocation() {
return location;
}
/**
* Place the animal at the new location in the given field.
*
* @param newLocation The animal's new location.
*/
protected void setLocation(Location newLocation) {
if(location != null) {
getField().clear(location);
}
location = newLocation;
getField().place(this, newLocation);
}
/**
* A simple getter method to get the isAlive field
*
* @return true if the entity is alive
*/
protected boolean getIsAlive() {
return isAlive;
}
/**
* return the rand field which contains a Random object
*
* @return the random object we have
*/
public static Random getRand() {
return rand;
}
/**
* Indicate that the animal is no longer alive.
* It is removed from the field.
*/
protected void setDead() {
isAlive = false;
if(location != null) {
getField().clear(getLocation());
location = null;
setField(null);
}
}
/**
* A simple getter method that returns the EntityStats field
*
* @return the entity statistics
*/
public EntityStats getStats() {
return entityStats;
}
}