-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.java
201 lines (179 loc) · 4.84 KB
/
Robot.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
/**
* The Robot class creates a PVP robot that a player controls that has a certain name
* health, speed, and color. The player moves this Robot around the frame and can control
* when it shoots weapons.
*
* @author Akshara Ganapathi, Mukund Ramachandran, Sanjeet Verma
* Collaborators: None
* Teacher Name: Ms. Bailey
* Period: 03/05
* Due Date: 05-19-22
*/
import java.awt.*;
public class Robot implements Comparable<Robot> {
private static final int RIGHT_BOUND = 950;
private static final int LEFT_BOUND = 40;
private static final int UPPER_BOUND = 150;
private static final int LOWER_BOUND = 720;
private static final int SUBSTRING = 14;
private final String name;
private final int maxHealth;
private int currentHealth;
private final int speed;
private final Color robotColor;
private boolean freeze;
private int x;
private int y;
private boolean canShoot;
/**
* Creates a robot object
* @param name given name
* @param maxHealth given health
* @param speed given speed
* @param color given Color
*/
public Robot(String name, int maxHealth, int speed, Color color) {
this.name = name;
this.maxHealth = maxHealth;
this.currentHealth = maxHealth;
this.speed = speed;
this.robotColor = color;
this.freeze = true;
this.x = 0;
this.y = 0;
canShoot = true;
}
/**
* Reduces current health of robot
* @param change amount change
*/
public void subtractCurrentHealth(int change) {
if ((this.currentHealth - change) < 0)
currentHealth = 0;
else
this.currentHealth -= change;
}
/**
* Returns name of Robot
* @return String name
*/
public String getName() {
return this.name;
}
/**
* Returns if Robot can shoot a weapon
* @return boolean representation
*/
public boolean canShoot() {
return canShoot;
}
/**
* Determines if Robot can shoot or not
* @param canShoot boolean that determines
*/
public void setCanShoot(boolean canShoot) {
this.canShoot = canShoot;
}
/**
* Returns current health
* @return current health
*/
public int getCurrentHealth() {
return currentHealth;
}
/**
* Returns Robot speed
* @return Robot speed
*/
public int getSpeed() {
return speed;
}
/**
* Returns Color
* @return Color object
*/
public Color getRobotColor() {
return robotColor;
}
/**
* Returns if Robot can use freeze powerup
* @return boolean representation
*/
public boolean canUseFreeze() {
return this.freeze;
}
/**
* Determines if Robot can use freeze powerup
* @param freeze boolean that determines
*/
public void setFreeze(boolean freeze) {
this.freeze = freeze;
}
/**
* Returns x-value of Robot
* @return x-value
*/
public int getX() {
return this.x;
}
/**
* Returns y-value of Robot
* @return y-value
*/
public int getY() {
return this.y;
}
/**
* Sets x-value and y-value of Robot
* @param xNew new x-value
* @param yNew new y-value
*/
public void setLocation(int xNew, int yNew) {
this.x = xNew;
this.y = yNew;
}
/**
* Changes x-value and y-value of Robot
* @param xAmt x increment
* @param yAmt y increment
*/
public void changeLocation(int xAmt, int yAmt) {
this.x += xAmt;
if (x > RIGHT_BOUND)
x = RIGHT_BOUND;
else if (x < LEFT_BOUND)
x = LEFT_BOUND;
this.y += yAmt;
if (y > LOWER_BOUND)
y = LOWER_BOUND;
else if (y < UPPER_BOUND)
y = UPPER_BOUND;
}
/**
* Displays the Robot in String format
* @return String format
*/
@Override
public String toString() {
String part1 = "Name: " + this.name;
String part2 = ", Health: " + this.maxHealth;
String part3 = ", Speed: " + this.speed;
String part4 = ", Color: " + displayColor();
return part1 + part2 + part3 + part4;
}
/**
* Displays color of robot
* @return String representation
*/
public String displayColor() {
return this.robotColor.toString().substring(SUBSTRING);
}
/**
* Compares Robot to other Robots
* @param other other Robot
* @return whether its greater or not based on health
*/
public int compareTo(Robot other) {
return this.maxHealth - other.maxHealth;
}
}