-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.java
More file actions
249 lines (220 loc) · 5.28 KB
/
Enemy.java
File metadata and controls
249 lines (220 loc) · 5.28 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import java.awt.Color;
/**
* This class lets you create an Enemy object
* @author elan
*
*/
public class Enemy extends Entity
{
/**
* these Class variables are here as they are the same for all enemies on the screen at any given time
* they must also change together and they are not final as they do change
*/
private static double verticalDisplacement = 0, horizontalDisplacement = 0;
private static boolean movingDown = false, movingRight = true;
/**
* Constructor for my Enemy
* @param x x co-ordinate where it will be created
* @param y y co-ordinate where it will be created
* @param a angle it will start with
* @param i unique identifier
*/
public Enemy(double x, double y, double a, int i)
{
super(x, y, a, i);
}
/**
* Constructor for my Enemy
* @param x x co-ordinate where it will be created
* @param y y co-ordinate where it will be created
* @param a angle it will start with
* @param i unique identifier
* @param hp hit points it will start with
*/
public Enemy(double x, double y, double a, int i, int hp)
{
super(x, y, a, i, hp);
}
/**
* calls all updates that the Enemy needs to do
*/
public void update()
{
checkCollision();
if(dead)
{
try
{
Controller.enemies.remove(index);
}
catch(IndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
/**
* checks if the Enemy has come into contact with the player or a bullet, or reached the bottom of the screen
*/
private void checkCollision()
{
if(inRadius(Controller.player, Controller.enemies.get(index)))
{
Controller.gameOver();
}
if(Controller.enemies.get(index).getY() < States.enemySize/(double)1.95)
{
Controller.gameOver();
}
for (int j = 0; j < Controller.bullets.size(); j++)
{
if(inRadius(Controller.bullets.get(j), Controller.enemies.get(index)))
{
collideBulletAndEnemy(j);
}
}
}
/**
* Collides an Enemy with a bullet and destroys the bullet,
* if the enemy's hit points is 0 or less then it is also destroyed,
* and power Up spawns with a random chance
* @param bulletIndex unique identifier of the bullet hit
*/
private void collideBulletAndEnemy(int bulletIndex)
{
if(this.damage(Controller.bullets.get(bulletIndex)))
{
if(Level.powerUpChance())
Controller.powerUpBoxes.add(new PowerUpBox(this));
dead = true;
}
try
{
Controller.bullets.remove(bulletIndex);
}
catch(IndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
/**
* @return Returns true if Enemy can move left
*/
public boolean canMoveLeft()
{
if(xPos < States.enemySize*0.6)
return false;
return true;
}
/**
* @return Returns true if Enemy can move right
*/
public boolean canMoveRight()
{
if(xPos > States.WINDOW_RESOLUTION - States.enemySize*0.6)
return false;
return true;
}
/**
* Draws the Enemy
*/
public void render()
{
String type = "";
if(States.enemyHealth > 23)
type = "Enemy4.png";
else if(States.enemyHealth > 18)
type = "Enemy3.png";
else if(States.enemyHealth > 13)
type = "Enemy2.png";
else
type = "Enemy1.png";
StdDraw.picture(xPos, yPos, type, States.enemySize, States.enemySize, angle);
if(HP < States.enemyHealth)
renderHPbar();
}
/**
* draws the Enemy's Health bar
*/
public void renderHPbar()
{
double HPBarWidth = States.enemySize*0.7;
double HPBarHeight = States.enemySize/(double)10;
StdDraw.setPenColor(Color.RED);
StdDraw.filledRectangle(xPos, yPos + States.enemySize/(double)2, HPBarWidth/(double)2, HPBarHeight/(double)2);
StdDraw.setPenColor(Color.GREEN);
StdDraw.filledRectangle(xPos, yPos + States.enemySize/(double)2, HPBarWidth/(double)2 - (1 - HP/(double)States.enemyHealth)*(HPBarWidth/(double)2), HPBarHeight/(double)2);
}
/**
* @return Returns the distance the Enemy has moved in this direction
*/
public static double getVerticalDisplacement()
{
return verticalDisplacement;
}
/**
* increases the vertical displacement
* @param speed this is added to the vertical displacement
*/
public static void IncreaseVerticalDisplacement(double speed)
{
verticalDisplacement += speed;
}
/**
* resets the vertical displacement to 0
*/
public static void resetVerticalDisplacement()
{
verticalDisplacement = 0;
}
/**
* @return Returns the distance the Enemy has moved in this direction
*/
public static double getHorizontalDisplacement()
{
return horizontalDisplacement;
}
/**
* increases the horizontal displacement
* @param speed this is added to the horizontal displacement
*/
public static void IncreaseHorizontalDisplacement(double speed)
{
horizontalDisplacement += speed;
}
/**
* resets the horizontal displacement to 0
*/
public static void resetHorizontalDisplacement()
{
horizontalDisplacement = 0;
}
/**
* @return Returns true if Enemy is busy moving down
*/
public static boolean isMovingDown()
{
return movingDown;
}
/**
* Starts enemy moving down if it was not, stops it if it was
*/
public static void startOrStopVerticalMotion()
{
movingDown = !movingDown;
}
/**
* @return Returns true if Enemy is moving Right
*/
public static boolean isMovingRight()
{
return movingRight;
}
/**
* Makes the enemy move in the opposite direction
*/
public static void invertHorizontalMotion()
{
movingRight = !movingRight;
}
}