-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceship.cpp
254 lines (221 loc) · 7.76 KB
/
Spaceship.cpp
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
247
248
249
250
251
252
253
254
/*
* Spaceship.cpp
* Allenroids
*
* Created by Allen Smith on Sun Jun 20 2004.
*
*/
#include "Spaceship.h"
/****************************************************
* constructor
***************************************************/
Spaceship::Spaceship(){
shieldCoveragePercentage = 0.75; // 3/4 is the default.
};
/****************************************************
* draw
* paints the spaceship, the shield bubble (if any),
* and the rocket thrust
***************************************************/
void Spaceship::draw() const{
float angle; //for creating a circle for shields
int spriteTimeRemaining; //how much longer fading effects like shields and thrust have left
float spriteAlpha; //the *percentage* alpha (transparency) for the thrust and shields
int currentTime = glutGet( GLUT_ELAPSED_TIME );
bool shieldsOverloading;
glRotatef(rotationAngle, 0,0,1);
/* Basic Ship Model:
glVertex2i( x, y+20);
glVertex2i( x-10, y-10*sqrt(3) );
glVertex2i( x, y-15);
glVertex2i( x+10, y-10*sqrt(3) );
*/
//rocket thrust; drawn first so it appears behind the ship.
spriteTimeRemaining = drawThrustUntil - currentTime;
if(thrusting == true || spriteTimeRemaining > 0){
if(thrusting == true)
spriteAlpha = 1.0; //100%
else
spriteAlpha = spriteTimeRemaining / (float) FADEOUTS;
glBegin(GL_POLYGON);
if(strength <= maxStrength)
glColor4ub( 255 * strength/maxStrength,
0, 0,
(GLubyte) (255*spriteAlpha) );
else //overloading
glColor4ub( 255,
255*(strength%maxStrength)/maxStrength,
255*(strength%maxStrength)/maxStrength,
(GLubyte) (255*spriteAlpha) );
glVertex2i( 0, 0); //centerpoint; to complete the filled-in appearance
glColor4ub( 128, 0, 0, (GLubyte) (255*spriteAlpha) );
glVertex2f( (radius*4/5)*cos(5*PI/6), //port blast
(radius*4/5)*sin(5*PI/6));
if(strength <= maxStrength)
glColor4ub( 255 * strength/maxStrength,
0,
0,
(GLubyte) (255*spriteAlpha) );
else //overloading
glColor4ub( 255,
255*(strength % maxStrength)/maxStrength,
255*(strength % maxStrength)/maxStrength,
(GLubyte) (255*spriteAlpha));
glVertex2f( (radius*1.5)*cos(1*PI/1), //center blast
(radius*1.5)*sin(1*PI/1));
glColor4ub( 128, 0, 0, (GLubyte) (255*spriteAlpha) );
glVertex2f( (radius*4/5)*cos(7*PI/6), //starboard blast
(radius*4/5)*sin(7*PI/6));
glEnd();
}//end if
//the spaceship
glColor3ubv(color);
glBegin(GL_POLYGON);
glVertex2f( radius, //nose point
0);
glVertex2f( radius*cos(5*PI/6), //port fin
radius*sin(5*PI/6));
glVertex2f( (radius*3/5)*cos(1*PI/1), //center point
(radius*3/5)*sin(1*PI/1));
glVertex2f( radius*cos(7*PI/6), //starboard fin
radius*sin(7*PI/6));
glEnd();
//A Border for the ship
glColor3ub( (GLubyte) (color[0]*0.25),
(GLubyte) (color[1]*0.25),
(GLubyte) (color[2]*0.25) ); //darker color
glLineWidth(2.0);
glBegin(GL_LINE_LOOP);
glVertex2f( radius, //nose point
0);
glVertex2f( radius*cos(5*PI/6), //port fin
radius*sin(5*PI/6));
glVertex2f( (radius*3/5)*cos(1*PI/1), //center point
(radius*3/5)*sin(1*PI/1));
glVertex2f( radius*cos(7*PI/6), //starboard fin
radius*sin(7*PI/6));
glEnd();
//Draw Shields
shieldsOverloading = false; //assume they aren't overloading; then check the assumption. Computer players can't overload
if(playerNumber > 0){ //not a computer player
if(strength > maxStrength)
shieldsOverloading = true;
}
if(drawShieldsTimeToLive > 0 || shieldsOverloading){
if(invinciblityTimeToLive > 0 )
spriteAlpha = 0.3125; // 5/16ths
else if(shieldsOverloading)
spriteAlpha = (float)( strength % maxStrength ) / (maxStrength*OVERLOAD_FACTOR);
else
spriteAlpha = drawShieldsTimeToLive / (float) FADEOUTS;
//Outer Ring
glLineWidth(2.0);
if(shieldsOverloading)
glColor4ub( (GLubyte) (255*spriteAlpha),
0,
0,
(GLubyte) (128*spriteAlpha) );
else if(playerNumber < 0)
glColor4ub( 102, 255, 102, (GLubyte) (128*spriteAlpha) );
else
glColor4ub( 0, 128, 255, (GLubyte) (128*spriteAlpha) );
glBegin(GL_LINE_LOOP);
for(angle = 0; angle < 2*PI; angle += 2*PI/12){ //twelve-sided shield bubble
glVertex2f( (radius*shieldCoveragePercentage +1)*cos(angle), (radius*shieldCoveragePercentage +1)*sin(angle) );
}
glEnd();
//Central Shild Line!
glLineWidth(3.0);
if(shieldsOverloading)
glColor4ub( (GLubyte) (255*spriteAlpha),
0,
0,
(GLubyte) (255*spriteAlpha) );
else if(playerNumber < 0)
glColor4ub( 102, 255, 102, (GLubyte) (255*spriteAlpha));
else
glColor4ub( 0, 128, 255, (GLubyte) (255*spriteAlpha));
glBegin(GL_LINE_LOOP);
for(angle = 0; angle < 2*PI; angle += 2*PI/12){
glVertex2f( (radius*shieldCoveragePercentage)*cos(angle), (radius*shieldCoveragePercentage)*sin(angle) );
}
glEnd();
//Inner Ring
glLineWidth(2.0);
if(shieldsOverloading)
glColor4ub( (GLubyte) (255*spriteAlpha),
0,
0,
(GLubyte) (128*spriteAlpha) );
else if(playerNumber < 0)
glColor4ub( 102, 255, 102, (GLubyte) (128*spriteAlpha) );
else
glColor4ub( 0, 128, 255, (GLubyte) (128*spriteAlpha) );
glBegin(GL_LINE_LOOP);
for(angle = 0; angle < 2*PI; angle += 2*PI/12){
glVertex2f( (radius*shieldCoveragePercentage -1)*cos(angle), (radius*shieldCoveragePercentage -1)*sin(angle) );
}
glEnd();
}
}
/****************************************************
* drawHealthMeter
* a little rectangle indicating a player's health
***************************************************/
void Spaceship::drawHealthMeter(int hudWidth, int hudHeight, bool growsRight) const{
float overloadFactor;
//create the status bar for ship strength. It changes color based on health
if( strength > maxStrength ){
overloadFactor = (float)( strength % maxStrength ) / (maxStrength*OVERLOAD_FACTOR);
glColor4ub( (GLubyte) (255*overloadFactor ),
(GLubyte) (255*(1-overloadFactor) ),
0,
128); //Overload Red
}
else if(strength > maxStrength*3/4)
glColor4ub(0, 255, 0, 128); //Green
else if(strength > maxStrength*1/2)
glColor4ub(128, 255, 0, 128); //Lime
else if(strength > maxStrength*1/4)
glColor4ub(255, 255, 0, 128); //Yellow
else // strength > 0
glColor4ub(255, 0, 0, 128); //Red
if(growsRight){
glBegin(GL_POLYGON); //draw it counterclockwise for avoiding back-face culling
glVertex2i(0, 0 );
glVertex2i(strength * hudWidth/maxStrength, 0 );
glVertex2i(strength * hudWidth/maxStrength, hudHeight);
glVertex2i(0, hudHeight);
glEnd();
}
else{
//right-justified meter
glBegin(GL_POLYGON); //draw it counterclockwise for avoiding back-face culling
glVertex2i(hudWidth, 0);
glVertex2i(hudWidth, hudHeight);
glVertex2i(hudWidth - strength * hudWidth/maxStrength, hudHeight);
glVertex2i(hudWidth - strength * hudWidth/maxStrength, 0);
glEnd();
}
//Create an outline around the status bar, which identifies it by color.
glColor3ubv(color);
glLineWidth(2.0);
glBegin(GL_LINE_LOOP);
glVertex2i(0, 0 );
glVertex2i(hudWidth, 0 );
glVertex2i(hudWidth, hudHeight);
glVertex2i(0, hudHeight);
glEnd();
}
/****************************************************
* intersectsWith
* overrides GameObject function so as to shrink the impact radius for this spaceship
***************************************************/
bool Spaceship::intersectsWith(const GameObject *otherObject, bool twoWayCompare){
bool intersects;
float originalRadius = radius;
radius *= shieldCoveragePercentage; //shrink down the spaceship radius for the comparison
intersects = GameObject::intersectsWith(otherObject); //now do the regular calculations, which will be affected by the shrunken radius
radius = originalRadius; //now restore the radius to its proper size
return intersects;
}