-
Notifications
You must be signed in to change notification settings - Fork 0
/
iFloater.pde
84 lines (80 loc) · 2.9 KB
/
iFloater.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
77
78
79
80
81
82
83
84
abstract class iFloater //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 myDirectionX, myDirectionY; //holds x and y coordinates of the vector for direction of travel
protected double myPointDirection; //holds current direction the ship is pointing in degrees
abstract public void setX(int x);
abstract public int getX();
abstract public void setY(int y);
abstract public int getY();
abstract public void setDirectionX(double x);
abstract public double getDirectionX();
abstract public void setDirectionY(double y);
abstract public double getDirectionY();
abstract public void setPointDirection(int degrees);
abstract public double getPointDirection();
//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
myDirectionX += ((dAmount) * Math.cos(dRadians));
myDirectionY += ((dAmount) * Math.sin(dRadians));
}
public void turn (int nDegreesOfRotation)
{
//rotates the floater by a given number of degrees
myPointDirection+=nDegreesOfRotation;
}
public void move () //move the floater in the current direction of travel
{
//change the x and y coordinates by myDirectionX and myDirectionY
myCenterX += myDirectionX;
myCenterY += myDirectionY;
//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);
}
}