-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsteroid.java
executable file
·91 lines (77 loc) · 2.74 KB
/
Asteroid.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
package asteroids;
/**
Name: Chris Drury
Class: CSc 2310: Introduction to programming
Filename: Asteroid.java
Date written: April, 19, 2011
Description:
This class controls how our asteroids work.
*/
import java.util.Random;
import javax.vecmath.Vector2f;
import processing.core.PApplet;
public class Asteroid extends AbstractEntity{
//Public because I'm the only one coding this game
//And its quicker than making helper methods.
public Vector2f driftSpeed;
public int size;
public int health;
private float rotateSpeed;
//Default constructor of our asteroid
public Asteroid(PApplet aParent, Vector2f aPosition, int aSize)
{
//Tell our abstract entity parent the information it needs
super(aParent, aPosition);
//Psudeo random number generator. Used for our random
//Asteroid creation
Random ran = new Random();
//Random rotation speed
rotateSpeed = (float)((Math.random()*100) - 50);
size = aSize;
//Minimum number of sides is 5
//Triangles don't look like asteroids
numberOfSides = (int)(ran.nextDouble() * 10)+5;
//Health per asteroid is 3
health = 3;
//Random angle for our new asteroid to drift
double randomAngle = ran.nextDouble()*360;
//set the drift speed of our asteroid
//The speed varies from 0-180 pixel/s
driftSpeed = new Vector2f((float)(Math.cos(Math.toRadians(randomAngle))*ran.nextDouble()*180), (float)(Math.sin(Math.toRadians(randomAngle))*ran.nextDouble()*180));
//Randomly create our asteroid. We do 360 - one angle of our side
//This is because we dont need to remake our 360 degree point as its
//our 0 point as well. Then using the number of sides in our asteroid
//Loop in a circle around our point creating random distances
//for each angle.
for (int x = 0; x < 360-(360/(numberOfSides)); x+= (360/(numberOfSides)))
{
//Distance from center is created randomly between
//30 and the largest size it could be - 10
double randomSize = (ran.nextDouble() * size-10)+30;
//Array lists only store objects, not primitives
Double mySize = new Double(randomSize);
//Add distance to my array of distances.
verticiesSizes.add(mySize);
}
}
public void update(double aDelta)
{
//Update our parent to update our position information
super.update(aDelta);
//Drift the asteroid. This is set
//To drift speed * delta to make a smooth move
//Then * difficulty making it harder later because
//The asteroids move faster.
position.x += (float)(driftSpeed.x * Main.difficulty * aDelta);
position.y += (float)(driftSpeed.y * Main.difficulty * aDelta);
//rotate the asteroid
rotation += rotateSpeed * aDelta;
}
public void draw()
{
//No special drawing attributes to our asteroids
//Just draw our verticies. Our parent does this
//for us.
super.draw();
}
}