-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBunker.java
More file actions
226 lines (201 loc) · 5.33 KB
/
Bunker.java
File metadata and controls
226 lines (201 loc) · 5.33 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
import java.awt.Color;
/**
* This Class lets you create a Bunker object
* @author elan
*
*/
public class Bunker extends Entity
{
/**
* width is private and final as it is only determined once and may not be seen outside of the class
* is moving right is not final the bunker can move right or left and is private as it may not be seen outside of the class
*/
private final double width;
private boolean isMovingRight;
/**
* Constructor for a Bunker object
* @param x x value where it will be created
* @param y x value where it will be created
* @param a angle that it will be created at
* @param i the unique index it will get
* @param hp the hit points it starts with
* @param w the width of the bunker
* @param r if the bunker is moving right initially
*/
public Bunker(double x, double y, double a, int i, int hp, double w, boolean r)
{
super(x, y, a, i, hp);
width = w;
isMovingRight = r;
}
/**
* creates the bunkers at the beginning of each level
*/
public static void initializeBunkers()
{
Controller.bunkers.add(new Bunker(States.WINDOW_CENTRE/(double)2, States.SHOOTER_START_Y * 3 + States.BUNKER_HEIGHT, 0, Controller.bunkers.size(), States.bunkerHP, States.bunkerWidth, true));
Controller.bunkers.add(new Bunker(States.WINDOW_CENTRE/(double)2 + States.WINDOW_CENTRE, States.SHOOTER_START_Y * 3 + States.BUNKER_HEIGHT, 0, Controller.bunkers.size(), States.bunkerHP, States.bunkerWidth, false));
}
/**
* Draws the bunker on the screen
*/
public void render()
{
StdDraw.picture(xPos, yPos, "bunker.png", width, States.BUNKER_HEIGHT, angle);
if(HP < States.bunkerHP)
renderHPbar();
}
/**
* Draws the bullets Health Bar
*/
public void renderHPbar()
{
double HPBarWidth = width*0.7;
double HPBarHeight = States.BUNKER_HEIGHT/(double)10;
StdDraw.setPenColor(Color.RED);
StdDraw.filledRectangle(xPos, yPos + States.BUNKER_HEIGHT/(double)2, HPBarWidth/(double)2, HPBarHeight/(double)2);
StdDraw.setPenColor(Color.GREEN);
StdDraw.filledRectangle(xPos, yPos + States.BUNKER_HEIGHT/(double)2, HPBarWidth/(double)2 - (1 - HP/(double)States.bunkerHP)*(HPBarWidth/(double)2), HPBarHeight/(double)2);
}
/**
* updates the bunker
*/
public void update()
{
updatePos();
checkCollision();
if(dead)
Controller.bunkers.remove(index);
}
/**
* updates the bunkers x co-ordinates
*/
private void updatePos()
{
if(isMovingRight)
{
moveRight(States.BUNKER_SPEED);
}
else
{
moveLeft(States.BUNKER_SPEED);
}
}
/**
* Causes the bunker to move in the opposite direction
*/
public void invertMovement()
{
isMovingRight = !isMovingRight;
}
/**
* Returns the width of the bunker
* @return width of bunker
*/
public double getWidth()
{
return width;
}
/**
* Returns if the bunker can move left or not (if not, inverts its movement)
* @return true if bunker can move left
*/
public boolean canMoveLeft()
{
if(xPos > width/(double)2)
return true;
invertMovement();
return false;
}
/**
* Returns if the bunker can move right or not (if not, inverts its movement)
* @return true if bunker can move right
*/
public boolean canMoveRight()
{
if(xPos < States.WINDOW_RESOLUTION - width/(double)2)
return true;
invertMovement();
return false;
}
/**
* checks the various things a bunker can come in contact with
* if two bunkers touch
* if a bunker touches a Bullet
* if a bunker touches a EnemyBullet
*/
private void checkCollision()
{
for(int j = 0; j < Controller.bunkers.size(); j++)
{
if(index != j)
{
if(inRadius(Controller.bunkers.get(index), Controller.bunkers.get(j)))
{
collideBunkers(index, j);
}
}
}
for(int j = 0; j < Controller.bullets.size(); j++)
{
if(Controller.bunkers.size() < 1)
break;
if(inRadius(Controller.bullets.get(j), Controller.bunkers.get(index)))
{
collideBunkerAndBullet(Controller.bullets.get(j));
Index.updateBulletIndex();
}
}
for(int j = Controller.enemyBullets.size() - 1; j >= 0 ; j--)
{
if(Controller.bunkers.size() < 1)
break;
if(inRadius(Controller.enemyBullets.get(j), Controller.bunkers.get(index)))
{
collideBunkerAndBullet(Controller.enemyBullets.get(j));
Index.updateEnemyBulletIndex();
}
}
}
/**
* Causes the bunkers that collided to invert their movement
* @param bunkIndex1 the unique index of the first bunker
* @param bunkIndex2 the unique index of the second bunker
*/
private void collideBunkers(int bunkIndex1, int bunkIndex2)
{
Controller.bunkers.get(bunkIndex1).invertMovement();
Controller.bunkers.get(bunkIndex1).updatePos();
Controller.bunkers.get(bunkIndex2).invertMovement();
Controller.bunkers.get(bunkIndex2).updatePos();
}
/**
* Collides the bunker with a Bullet or an EnemyBullet and damages the bunker proportionally
* @param bull the Bullet object that the bunker has collided with
*/
private void collideBunkerAndBullet(Bullet bull)
{
if(bull instanceof EnemyBullet)
{
Controller.enemyBullets.remove(bull.getIndex());
if(this.damage(bull))
dead = true;
}
else
{
if(!bull.isPhasable())//gives the bullet a chance to phase through the bunker
{
try
{
Controller.bullets.remove(bull.getIndex());
}
catch(IndexOutOfBoundsException e)
{
e.printStackTrace();
}
if(this.damage(bull))
dead = true;
}
}
}
}