forked from TrevorAPCS/AsteroidsGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceship.pde
246 lines (240 loc) · 5.72 KB
/
Spaceship.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
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
239
240
241
242
243
244
245
246
class Spaceship extends Floater{
protected boolean accelerating, reversing, thrusters;
protected double health, mass, acceleration, turning;
protected int myDefaultColor, thrusterTimer, thrusterTimerRequirement, sHealth;
protected String name;
public Spaceship(){
myCenterX = width / 2;
myCenterY = height / 2;
myPointDirection = 0;
myColor = (#0C9CE8);
myDefaultColor = myColor;
myXspeed = 0;
myYspeed = 0;
accelerating = false;
reversing = false;
thrusters = true;
thrusterTimer = 0;
thrusterTimerRequirement = 5;
name = "Classic";
corners = 3;
xCorners = new int[]{10, -10, -10};
yCorners = new int[]{0, 5, -5};
health = 350;
mass = 1.2;
acceleration = 0.20;
turning = 1.6;
sHealth = (int)health;
}
public Spaceship(int type){
myCenterX = width / 2;
myCenterY = height / 2;
myPointDirection = 0;
myColor = (#0C9CE8);
myDefaultColor = myColor;
myXspeed = 0;
myYspeed = 0;
accelerating = false;
reversing = false;
thrusters = true;
thrusterTimer = 0;
thrusterTimerRequirement = 5;
if(type == 2){
name = "Speed";
corners = 4;
xCorners = new int[]{10, -10, -5, -10};
yCorners = new int[]{0, 5, 0, -5};
health = 300;
mass = 1;
acceleration = 0.25;
turning = 2;
}
else if(type == 3){
name = "Tank";
corners = 6;
xCorners = new int[]{10, 0, -10, -5, -10, 0};
yCorners = new int[]{0, 5, 5, 0, -5, -5};
health = 600;
mass = 2;
acceleration = 0.15;
turning = 1;
}
else{
name = "Classic";
corners = 3;
xCorners = new int[]{10, -10, -10};
yCorners = new int[]{0, 5, -5};
health = 350;
mass = 1.2;
acceleration = 0.20;
turning = 1.6;
}
sHealth = (int)health;
}
public void hyperSpace(){
myXspeed = 0;
myYspeed = 0;
myPointDirection = Math.random() * 360;
myCenterX = Math.random() * 500;
myCenterY = Math.random() * 500;
}
public double getX(){
return myCenterX;
}
public double getY(){
return myCenterY;
}
public void setX(double x){
myCenterX = x;
}
public void setY(double y){
myCenterY = y;
}
public String getName(){
return name;
}
public double getDirection(){
return myPointDirection;
}
public void setDirection(int d){
myPointDirection = d;
}
public double getXspeed(){
return myXspeed;
}
public double getYspeed(){
return myYspeed;
}
public double getMass(){
return mass;
}
public void setXspeed(double s){
myXspeed = s;
}
public void setYspeed(double s){
myYspeed = s;
}
public void setAccelerating(boolean a){
accelerating = a;
if(thrusters == false){
accelerating = false;
}
}
public void setReversing(boolean r){
reversing = r;
if(thrusters == false){
reversing = false;
}
}
public double getHealth(){
return health;
}
public double getHealthPercentage(){
return health / sHealth;
}
public void setHealth(double h){
health = h;
}
public void setThrusters(boolean b){
thrusters = b;
}
public boolean getThrusters(){
return thrusters;
}
public void accelerate(double amount){
if(thrusters){
super.accelerate(amount);
}
}
public void setColor(int c){
myColor = c;
}
public void setDefaultColor(){
myColor = myDefaultColor;
}
public void show (){
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);
if(accelerating){
fill(255, 0, 0);
beginShape();
vertex(-5, 0);
vertex(-8, 3);
vertex(-10, 0);
vertex(-8, -3);
vertex(-5, 0);
endShape(CLOSE);
}
if(reversing){
stroke(255, 0, 0);
line(5, 3, 7, 4);
line(5, -3, 7, -4);
stroke(255);
}
//"unrotate" and "untranslate" in reverse order
rotate(-1*dRadians);
translate(-1*(float)myCenterX, -1*(float)myCenterY);
stroke(0);
}
public void accelerate(boolean forward){
if(forward){
accelerating = true;
super.accelerate(acceleration);
}
else{
reversing = false;
super.accelerate(-(acceleration * 1/3));
}
}
public void turn(boolean clockwise){
if(clockwise){
super.turn(turning);
}
else{
super.turn(-turning);
}
}
public void move(){
super.move();
if(thrusters == false){
thrusterTimer++;
if(thrusterTimer % 2 != 0){
myColor = #FF0000;
}
else{
myColor = myDefaultColor;
}
if(thrusterTimer == thrusterTimerRequirement){
thrusters = true;
thrusterTimer = 0;
myColor = myDefaultColor;
}
}
}
public void calculateCollisionSpeed(double others, double otherm, double dir){
double msi = Math.sqrt(Math.pow(myXspeed, 2) + Math.pow(myYspeed, 2));
double mso = ((mass - otherm)/(mass + otherm)) * msi + ((2 * otherm) / (mass + otherm)) * others;
myXspeed = Math.cos(dir) * mso;
myYspeed = Math.sin(dir) * mso;
health -= (otherm * (msi + 1) * 2);
thrusterTimer = 0;
setThrusters(false);
}
public void unIntrude(double amount, double dir){
myCenterX += amount * Math.cos(dir) * 1.10;
myCenterY += amount * Math.sin(dir) * 1.10;
}
}