-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShip.java
More file actions
202 lines (176 loc) · 6.68 KB
/
Ship.java
File metadata and controls
202 lines (176 loc) · 6.68 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
import java.awt.*;
import java.awt.geom.Point2D.Double;
/**
* The Ship class holds data regarding where the player is
* located within the Universe and performs any transformations
* necessary to move/manipulate the player.
*
* @author Herman Lin
* @author Devin Zhu
*/
public class Ship extends Polygon {
// Coordinate data for the Ship
private static final int[] xcoords = {0, 10, 0, -10};
private static final int[] ycoords = {0, -25, -20, -25};
private static final int nVertex = 4;
// Position variables for the Ship, used in translation
private double positionx;
private double positiony;
// Centroid variables for the Ship, used in rotation and positioning
private double centroidx;
private double centroidy;
private double t_centroidx;
private double t_centroidy;
// The direction the Ship is facing
private double facing = 180; // starts facing up
private double moveFacing = 270;
// Velocity of the ship, starts at 0 in both x/y direction
private double velocityx = 0;
private double velocityy = 0;
// Describes the color of the ship
private Color color;
/**
* @param color the color of the ship
*/
Ship(Color color) {
super(xcoords, ycoords, nVertex);
this.color = color;
// set default starting position
positionx = SpaceRangers.SCREEN_WIDTH/2;
positiony = SpaceRangers.SCREEN_HEIGHT/2;
computeCentroid();
computeTranslatedCentroid();
}
/**
* @param color the color of the Ship
* @param posx the x coordinate of the Ship
* @param posy the y coordinate of the Ship
*/
Ship(Color color, double posx, double posy, double deg) {
super(xcoords, ycoords, nVertex);
this.color = color;
positionx = posx;
positiony = posy;
facing = deg;
computeCentroid();
computeTranslatedCentroid();
}
// Getters and Setters
public Polygon getShip() { return this; }
public Color getColor() { return color; }
public void setColor(Color color) { this.color = color; }
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 double getTranslatedCentroidX() { return t_centroidx; }
public double getTranslatedCentroidY() { return t_centroidy; }
public void setCentroidX(double xcoord) { centroidx = xcoord; }
public void setCentroidY(double ycoord) { centroidy = ycoord; }
public void setTranslatedCentroidX(double xcoord) { t_centroidx = xcoord; }
public void setTranslatedCentroidY(double ycoord) { t_centroidy = ycoord; }
public double getFacing() { return facing; }
public void setFacing(double dirAngle) { facing = dirAngle; }
public double getMoveFacing() { return moveFacing; }
public void setMoveFacing(double dirAngle) { moveFacing = dirAngle; }
public double getVelocityX() { return velocityx; }
public double getVelocityY() { return velocityy; }
public void setVelocityX(double velx) { velocityx = velx; }
public void setVelocityY(double vely) { velocityy = vely; }
/**
* Calculates the centroid of the Ship that is used
* when rotating the ship.
*
* @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);
}
/**
* Calculate the centroid of the ship relative to the ship's
* translated location
*/
public void computeTranslatedCentroid() {
t_centroidx = centroidx + positionx;
t_centroidy = centroidy + positiony;
}
/**
* Rotate the Ship by 5 degrees clockwise
*/
public void rotateRight() {
if (facing >= 355) { facing = 0; }
else { facing += 5; }
}
/**
* Rotate the Ship by 5 degrees counter-clockwise
*/
public void rotateLeft() {
if (facing <= 0) { facing = 355; }
else { facing -= 5; }
}
/**
* Increase the ship's velocity based on the direction
* it is facing. This is multiplied by a base speed of
* 0.03 units.
*/
public void increaseVelocity() {
moveFacing = facing + 90;
velocityx += 0.01 * Math.cos(Math.toRadians(moveFacing));
velocityy += 0.01 * Math.sin(Math.toRadians(moveFacing));
}
/**
* Decrease the ship's velocity based on the direction
* it is facing. This is multiplied by a base speed of
* 0.03 units.
*/
public void decreaseVelocity() {
moveFacing = facing + 90;
velocityx -= 0.01 * Math.cos(Math.toRadians(moveFacing));
velocityy -= 0.01 * Math.sin(Math.toRadians(moveFacing));
}
/**
* Move the ship according to it's velocity values.
*/
public void move() {
// Cap the Ship's velocity at 3.0 units per unit of time
if (velocityx > 3.0) { velocityx = 3.0; }
else if (velocityx < -3.0) { velocityx = -3.0; }
if (velocityy > 3.0) { velocityy = 3.0; }
else if (velocityy < -3.0) { velocityy = -3.0; }
// update the Ship's location
positionx += velocityx;
positiony += velocityy;
// if the Ship goes off screen,
// circle back to the other side
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 color.getRed() + " " + color.getGreen() + " " + color.getBlue() +
" " + positionx + " " + positiony + " " + facing + " ";
}
public String toString() {
return "Ship | Color: " + color +
", X: " + positionx + ", Y: " + positiony +
", cX: " + centroidx + ", cY: " + centroidy +
", Facing: " + facing + " degrees";
}
}