forked from APCSLowell/AsteroidsGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Floater.pde
76 lines (68 loc) · 2.42 KB
/
Floater.pde
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
class Floater //Do NOT modify the Floater class! Make changes in the Spaceship class
{
protected int corners; //the number of corners, a triangular floater has 3
protected int[] xCorners;
protected int[] yCorners;
protected int myColor;
protected double myCenterX, myCenterY; //holds center coordinates
protected double myXspeed, myYspeed; //holds the speed of travel in the x and y directions
protected double myPointDirection; //holds current direction the ship is pointing in degrees
//Accelerates the floater in the direction it is pointing (myPointDirection)
public void accelerate (double dAmount)
{
//convert the current direction the floater is pointing to radians
double dRadians =myPointDirection*(Math.PI/180);
//change coordinates of direction of travel
myXspeed += ((dAmount) * Math.cos(dRadians));
myYspeed += ((dAmount) * Math.sin(dRadians));
}
public void turn (double degreesOfRotation)
{
//rotates the floater by a given number of degrees
myPointDirection+=degreesOfRotation;
}
public void move () //move the floater in the current direction of travel
{
//change the x and y coordinates by myXspeed and myYspeed
myCenterX += myXspeed;
myCenterY += myYspeed;
//wrap around screen
if(myCenterX >width)
{
myCenterX = 0;
}
else if (myCenterX<0)
{
myCenterX = width;
}
if(myCenterY >height)
{
myCenterY = 0;
}
else if (myCenterY < 0)
{
myCenterY = height;
}
}
public void show () //Draws the floater at the current position
{
fill(myColor);
stroke(myColor);
//translate the (x,y) center of the ship to the correct position
translate((float)myCenterX, (float)myCenterY);
//convert degrees to radians for rotate()
float dRadians = (float)(myPointDirection*(Math.PI/180));
//rotate so that the polygon will be drawn in the correct direction
rotate(dRadians);
//draw the polygon
beginShape();
for (int nI = 0; nI < corners; nI++)
{
vertex(xCorners[nI], yCorners[nI]);
}
endShape(CLOSE);
//"unrotate" and "untranslate" in reverse order
rotate(-1*dRadians);
translate(-1*(float)myCenterX, -1*(float)myCenterY);
}
}