-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissile.java
More file actions
249 lines (201 loc) · 6.97 KB
/
Missile.java
File metadata and controls
249 lines (201 loc) · 6.97 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
/*
*Partner 1 Name: Evelyn Bailey
*Partner 1 PennKey: ebail
*Partner 1 Recitation #: 215
*
*Partner 2 Name: Stephen Eyerly
*Partner 2 PennKey: seyerly
*Partner 2 Recitation #: 216
*
* Description: The missile class constructs a missile object and has functions
* to move missiles and find the distance between missiles.
*
*/
public class Missile {
// Variables to keep track of the missile's position
private double xBegin;
private double yBegin;
private double xEnd;
private double yEnd;
private double xCurrent;
private double yCurrent;
private boolean isExploding = false;
private int iterationsExploded = 0;
// Variables that store the total change in x and y for the missile
private double dX;
private double dY;
// This variable stores the move constant. It determines the speed at
// which missiles move.
private double moveConstant;
private boolean isEnemy;
private double explosionRadius = 0.1;
public double xBegin() {
return xBegin;
}
public double yBegin() {
return yBegin;
}
public double xEnd() {
return xEnd;
}
public double yEnd() {
return yEnd;
}
public double xCurrent() {
return xCurrent;
}
public double yCurrent() {
return yCurrent;
}
public boolean isExploding() {
return isExploding;
}
public int iterationsExploded() {
return iterationsExploded;
}
public boolean isEnemy() {
return isEnemy;
}
public double explosionRadius() {
return explosionRadius;
}
/*
* Name: Missile
* Description: constructor for defensive missiles
* Inputs: -
* Outputs: -
*/
public Missile(double xBegin, double yBegin, double xEnd,
double yEnd, double moveConstant) {
//the missile is not an enemy missile
isEnemy = false;
//the current x and y start at the x and y beginning coordinates
this.xBegin = xBegin;
xCurrent = xBegin;
this.yBegin = yBegin;
yCurrent = yBegin;
this.xEnd = xEnd;
this.yEnd = yEnd;
this.moveConstant = moveConstant;
this.moveConstant = moveConstant;
double actualDX = xEnd - xBegin;
double actualDY = yEnd - yBegin;
//calculate actual distance to travel
double distance = Math.sqrt(actualDX * actualDX +
actualDY * actualDY);
dX = actualDX / distance;
dY = actualDY / distance;
}
/*
* Name: Missile
* Description: constructor for enemy missiles
* Inputs: double moveConstant
* Outputs: -
*/
public Missile(double moveConstant){
isEnemy = true;
// random lateral starting position
xBegin = Math.random();
xCurrent = xBegin;
// Starts at the top
yBegin = 1;
yCurrent = yBegin;
// random lateral ending position
xEnd = Math.random();
// ends at the bottom
yEnd = 0;
this.moveConstant = moveConstant;
//calculations for total distance to travel
double actualDX = xEnd - xBegin;
double actualDY = yEnd - yBegin;
double distance = Math.sqrt(actualDX * actualDX +
actualDY * actualDY);
dX = actualDX / distance;
dY = actualDY / distance;
}
/*
* Name: move
* Description: moves the missiles by updating the position of the
* missile image
* Inputs: -
* Outputs: -
*/
public void move() {
//if the missile is not exploding, draw it
if (!isExploding) {
xCurrent += dX * moveConstant;
yCurrent += dY * moveConstant;
}
//once the enemy missile is exploding, keep track of how long it has
//been exploding by incrementing its iterationsExploded value
if (isEnemy){
if (yCurrent <= yEnd) {
isExploding = true;
iterationsExploded++;
//retrieve x and y coordinates of the exploding missile
double xCoord = xEnd;
double yCoord = yEnd;
//animate explosion
explode(2, xCoord, yCoord, "explosion.png", 50, 50);
}
}
//once the defensive missile is exploding, keep track of how long it has
//been exploding by incrementing its iterationsExploded value
if (!isEnemy){
if (yCurrent >= yEnd) {
isExploding = true;
iterationsExploded++;
//retrieve x and y coordinates of the exploding missile
double xCoord = xEnd;
double yCoord = yEnd;
//animate explosion
explode(2, xCoord, yCoord, "explosion.png", 50, 50);
}
}
}
/*
* Name: distanceTo
* Description: finds the distance between two missiles
* Inputs: Missile j
* Outputs: double distance
*/
public double distanceTo(Missile j) {
double deltaX = j.xCurrent - this.xCurrent;
double deltaY = j.yCurrent - this.yCurrent;
double d = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
return d;
}
/*
* Name: explode
* Description: animations the explosion of a missile
* Inputs: int numLevels, double xCenter, double yCenter, String filenae,
* double width, double height
* Outputs: -
*/
private void explode (int numLevels, double xCenter, double yCenter,
String filename, double width, double height){
//base case
if (numLevels <= 0) {
return;
}
//draw the image only after the x and y coordinates have been shifted
if (numLevels <= 1){
PennDraw.picture(xCenter, yCenter, filename, width, height);
}
//modify the x and y coordinates for placement
double x1 = xCenter + .05;
double x2 = xCenter - .05;
double y1 = yCenter + .05;
double y2 = yCenter - .05;
//decrease the depth
numLevels--;
//draw upper left picture
explode(numLevels, x2, y1, filename, width, height);
//draw upper right picture
explode(numLevels, x1, y1, filename, width, height);
//draw lower left
explode(numLevels, x2, y2, filename, width, height);
//draw lower right
explode(numLevels, x1, y2, filename, width, height);
}
}