-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsteroid.java
More file actions
238 lines (212 loc) · 8.69 KB
/
Asteroid.java
File metadata and controls
238 lines (212 loc) · 8.69 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
import java.awt.*;
import java.awt.geom.Area;
import java.util.Random;
import java.util.ArrayList;
/**
* The Asteroid class contains data on a specific Asteroid within the
* Universe. The class also holds an ArrayList containing all the
* Asteroids within the Universe.
*
* @author Herman Lin
* @author Devin Zhu
*/
class Asteroid extends Polygon {
public Random random = new Random();
// An ArrayList that contains all the asteroids within the
// world. Asteroids maintain their own presence in the universe
transient ArrayList<Asteroid> asteroids;
// 4 different shapes of Asteroids where each consecutive
// pair of indices represents the x/y coordinates of an
// asteroid shape
private final int[][] coordinates = {
new int[] {0,1,3,3,1,-1,-3,-3,-1,-1,0},
new int[] {2,3,1,-1,-3,-3,-1,0,0,3,2},
new int[] {0,1,3,2,2,-1,-3,-2,-3,-1},
new int[] {2,3,1,0,-2,-3,-1,0,1,3},
new int[] {0,2,3,2,2,0,-2,-3,-2,0},
new int[] {2,2,0,-1,-2,-3,-2,0,2,3},
new int[] {0,2,3,2,0,-1,-2,-2,-3,-2,-1},
new int[] {2,3,1,-2,-2,-3,-2,-1,0,2,3}
};
// Represents the number of points for each asteroid shape
private final int[] nVertex = {11,10,10,11};
// Represents how large the asteroid should be
private int scale;
// Position variables for the Asteroid, used in translation
private double positionx;
private double positiony;
// Centroid variables for the Asteroid, used in rotation
private double centroidx;
private double centroidy;
// Direction the Asteroid moves
private double direction;
// Velocities of the Asteroid
private double velocityx = 1.5;
private double velocityy = 1.5;
// Type of the asteroids
private int type;
// Boolean for determining when the asteroid is destroyed
private boolean alive = true;
/**
* Constructor for creating a default large Asteroid
*/
Asteroid(ArrayList<Asteroid> asteroids) {
super();
// randomize different aspects of the asteroid
type = random.nextInt(4);
direction = 360 * random.nextDouble();
this.randomLocation();
this.npoints = nVertex[type];
scale = 10;
// copy the coordinate data and process
int[][] temp_array = {new int[npoints], new int[npoints]};
System.arraycopy(coordinates[type * 2], 0, temp_array[0], 0, nVertex[type]);
System.arraycopy(coordinates[type * 2 + 1], 0, temp_array[1], 0, nVertex[type]);
for (int i = 0; i < npoints; i++) {
temp_array[0][i] *= scale;
temp_array[1][i] *= scale;
}
this.xpoints = temp_array[0];
this.ypoints = temp_array[1];
this.asteroids = asteroids;
computeCentroid();
}
/**
* Constructor for creating asteroids of specific
* qualities and location
*
* @param posX the x coordinate of the Asteroid
* @param posY the y coordinate of the Asteroid
* @param dir the direction the Asteroid moves in
* @param velX the velocity the Asteroid moves in the X direction
* @param velY the velocity the Asteroid moves in the Y direciton
* @param type the type of the Asteroid
* @param scale the size of the Asteroid
* @param asteroids an ArrayList of Asteroids within the Universe
*/
Asteroid(double posX, double posY, double dir, double velX, double velY,
int type, int scale, ArrayList<Asteroid> asteroids) {
super();
this.type = type;
this.npoints = nVertex[type];
positionx = posX; positiony = posY;
direction = dir;
velocityx = velX; velocityy = velY;
// copy the coordinate data and process
int[][] temp_array = {new int[npoints], new int[npoints]};
System.arraycopy(coordinates[type * 2], 0, temp_array[0], 0, nVertex[type]);
System.arraycopy(coordinates[type * 2 + 1], 0, temp_array[1], 0, nVertex[type]);
for (int i = 0; i < npoints; i++) {
temp_array[0][i] *= scale;
temp_array[1][i] *= scale;
}
this.xpoints = temp_array[0];
this.ypoints = temp_array[1];
this.scale = scale;
this.asteroids = asteroids;
computeCentroid();
}
// Getters and Setters
public Asteroid getAsteroid() { return this; }
public int getType() { return type; }
public void setType(int newType) { type = newType; }
public int getScale() { return scale; }
public void setScale(int newScale) { type = newScale; }
public double getPositionX() { return positionx; }
public double getPositionY() { return positiony; }
public void setPositionX(double xcoord) { positionx = xcoord; }
public void setPositionY(double ycoord) { positiony = ycoord; }
public double getCentroidX() { return centroidx; }
public double getCentroidY() { return centroidy; }
public void setCentroidX(double xcoord) { centroidx = xcoord; }
public void setCentroidY(double ycoord) { centroidy = ycoord; }
public double getDirection() { return direction; }
public void setDirection(double angle) { direction = angle; }
public double getVelocityX() { return velocityx; }
public double getVelocityY() { return velocityy; }
public void setVelocityX(double velX) { velocityx = velX; }
public void setVelocityY(double velY) { velocityy = velY; }
public boolean isAlive() { return alive; }
public void destroy() { alive = false; }
public Rectangle getBounds() {
return new Rectangle((int)positionx - (2 * scale), (int)positiony,
4 * scale, 4 * scale);
}
/**
* Calculates the centroid of the asteroid shape that is used
* when rotating the Asteroid.
*
* @see https://en.wikipedia.org/wiki/Centroid#Of_a_polygon
*/
public void computeCentroid() {
centroidx = 0.0; centroidy = 0.0;
double x0, x1, y0, y1, a, signedArea = 0.0;
for (int i = 0; i < npoints; i++) {
x0 = this.xpoints[i];
y0 = this.ypoints[i];
x1 = this.xpoints[(i+1) % this.npoints];
y1 = this.ypoints[(i+1) % this.npoints];
a = x0*y1 - x1*y0;
signedArea += a;
centroidx += (x0 + x1) * a;
centroidy += (y0 + y1) * a;
}
signedArea *= 0.5;
centroidx /= (6.0 * signedArea);
centroidy /= (6.0 * signedArea);
}
/**
* Randomize the starting location of the Asteroid
*/
public void randomLocation() {
// determine which side of the screen the Asteroid will spawn
int side = random.nextInt(4);
if (side == 0) { // top of screen
positionx = random.nextDouble() * SpaceRangers.SCREEN_WIDTH;
positiony = 0.0;
} else if (side == 1) { // bottom of screen
positionx = random.nextDouble() * SpaceRangers.SCREEN_WIDTH;
positiony = SpaceRangers.SCREEN_HEIGHT;
} else if (side == 2) { // left of screen
positionx = 0.0;
positiony = random.nextDouble() * SpaceRangers.SCREEN_HEIGHT;
} else if (side == 3) { // right of screen
positionx = SpaceRangers.SCREEN_WIDTH;
positiony = random.nextDouble() * SpaceRangers.SCREEN_HEIGHT;
}
}
/**
* Check whether or not the asteroid collides with a projectile
*
* @param p the projectile to check collision with
*/
public boolean collidesWith(Projectile p) {
Area aBounds = new Area(this.getBounds());
Area pBounds = new Area(p.getBounds());
pBounds.intersect(aBounds);
return !pBounds.isEmpty();
}
/**
* Move the Asteroid according to the velocity and direction values
*/
public void move() {
positionx += velocityx * Math.cos(Math.toRadians(direction + 90));
positiony += velocityy * Math.sin(Math.toRadians(direction + 90));
if (positionx < 0) { positionx = SpaceRangers.SCREEN_WIDTH; }
else if (positionx > SpaceRangers.SCREEN_WIDTH) { positionx = 0; }
if (positiony < 0) { positiony = SpaceRangers.SCREEN_HEIGHT; }
else if (positiony > SpaceRangers.SCREEN_HEIGHT) { positiony = 0; }
}
public String dataString() {
return type + " " + scale +
" " + positionx + " " + positiony +
" " + velocityx + " " + velocityy +
" " + direction + " ";
}
public String toString() {
return "Asteroid | Type: " + type + ", Scale: " + scale +
", X: " + positionx + ", Y: " + positiony +
", vX: " + velocityx + ", vY: " + velocityy +
", Direction: " + direction + " degrees";
}
}