/
PlayScreen.cpp
260 lines (213 loc) · 5.53 KB
/
PlayScreen.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
255
256
257
258
259
260
/*
* PlayScreen.cpp
* Super Spray
*/
//#include <stdlib.h> // Windows
//#include <gl/glut.h> // Windows
#include <GLUT/glut.h> // Mac OS X
#include "PlayScreen.h"
#include "SoundEffectsLibrary.h"
#include "DrawText.h"
#include "Target.h"
#include "PlayerScore.h"
#include "HighScores.h"
#include "SessionTime.h"
#include "Explosion.h"
// Define the current window state of the game.
#define STATE_PLAY 2
#define STATE_HIGHSCORES 3
#define STATE_GAMEOVER 4
#define MAX_NUM_TARGETS 100
#define WIDTH (double)glutGet(GLUT_SCREEN_WIDTH)
#define HEIGHT (double)glutGet(GLUT_SCREEN_HEIGHT)
extern int* shot;
extern bool isShotFired;
float spawnCounter= 0.0f;
float spawnTime = 5.0f;
Target newTarget; // Used to create new targets to add to the pull of new targets.
Target targets[MAX_NUM_TARGETS]; // Holds all the targets to be displayed that are created.
int cur = 0;
bool explosionFlag = false;
int expX;
int expY;
int shotDuration = 0;
// Draws the reticle -------------------------------------------------------------------------
void drawBlueCircle(float radius, float x, float y, float z = 0)
{
glPushMatrix();
glTranslatef(x, y, z);
int sections = 128;
// Two * Pi
GLfloat twoPi = 2.0f * 3.14159f;
glColor4f(0.0, 0.0, 1.0, 1.0);
glBegin(GL_TRIANGLE_FAN);
for(int i = 0; i <= sections; i++)
{
glVertex3f(radius * cos(i * twoPi / sections),
radius * sin(i * twoPi / sections), 0.0);
}
glEnd();
glPopMatrix();
}
void reticle()
{
glPushMatrix();
glTranslatef(shot[0], shot[1], 0.0f);
drawBlueCircle(rand()/(RAND_MAX/5),rand()/(RAND_MAX/10),rand()/(RAND_MAX/10));
drawBlueCircle(rand()/(RAND_MAX/5),rand()/(RAND_MAX/10),rand()/(RAND_MAX/10));
drawBlueCircle(rand()/(RAND_MAX/5),-rand()/(RAND_MAX/10),rand()/(RAND_MAX/10));
drawBlueCircle(rand()/(RAND_MAX/5),-rand()/(RAND_MAX/10),rand()/(RAND_MAX/10));
drawBlueCircle(rand()/(RAND_MAX/5),rand()/(RAND_MAX/10),-rand()/(RAND_MAX/10));
drawBlueCircle(rand()/(RAND_MAX/5),rand()/(RAND_MAX/10),-rand()/(RAND_MAX/10));
drawBlueCircle(rand()/(RAND_MAX/5),-rand()/(RAND_MAX/10),-rand()/(RAND_MAX/10));
drawBlueCircle(rand()/(RAND_MAX/5),-rand()/(RAND_MAX/10),-rand()/(RAND_MAX/10));
glPopMatrix();
}
// -------------------------------------------------------------------------------------------
// Time, Score, and Ammo ---------------------------------------------------------------------
void drawTime()
{
char timeString[30];
glPushMatrix();
sprintf(timeString, "Time Left: %f", getTime());
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(WIDTH/2 - 165, HEIGHT/2 - 20);
drawSentence(timeString);
glPopMatrix();
}
void drawScore()
{
char scoreString[15];
glPushMatrix();
sprintf(scoreString, "Score: %u", getScore());
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(-WIDTH/2, HEIGHT/2 - 20);
drawSentence(scoreString);
glPopMatrix();
}
void drawAmmo()
{
glPushMatrix();
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2f(-WIDTH/2, -HEIGHT/2 + 10);
drawSentence("Ammo: ");
glColor3f(0.0f, 0.0f, 1.0f);
glRasterPos2f(-WIDTH/2 + 80, -HEIGHT/2 + 10);
for(int i = 0; i < 16/* AMMO AMOUNT */; i++)
drawSentence("<>");
glPopMatrix();
}
// -------------------------------------------------------------------------------------------
// Manage the targets ------------------------------------------------------------------------
void addTarget()
{
newTarget = Target(Vec3(rand() % glutGet(GLUT_SCREEN_WIDTH) - glutGet(GLUT_SCREEN_WIDTH)/2,
rand() % glutGet(GLUT_SCREEN_HEIGHT) - glutGet(GLUT_SCREEN_HEIGHT)/2, 0));
if((double(rand())/RAND_MAX) > .9)
{
newTarget.setExplosionType(true);
newTarget.setColor(Vec3(1.0f,1.0f,0.0f));
}
else
{
newTarget.setColor(Vec3(0, double(rand())/RAND_MAX, double(rand())/RAND_MAX));
}
targets[cur] = newTarget;
cur = (cur + 1) % MAX_NUM_TARGETS;
}
void drawTargets()
{
if(spawnCounter <= 0)
{
addTarget();
spawnCounter = spawnTime;
}
else
spawnCounter -= 0.3;
for(int j = 0; j < MAX_NUM_TARGETS; j++)
{
if(!targets[j].getStatus() && targets[j].getCounter() > 0)
targets[j].drawTarget();
}
}
void checkAllHits()
{
for(int i = 0; i < MAX_NUM_TARGETS; i++)
{
if(!targets[i].getStatus() && (targets[i].getCounter() > 0) && targets[i].checkHit(shot[0], shot[1]))
{
targets[i].decreaseAlpha(0.34);
if(targets[i].getAlpha() < 0)
{
targets[i].setHitStatus(true);
incrementScore(100);
}
}
}
}
void setSpawnTime()
{
if(getTime() < 5.0f)
spawnTime = 0.3f;
else if(getTime() < 10.0f)
spawnTime = 0.6f;
else if(getTime() < 20.0f)
spawnTime = 1.2f;
else if(getTime() < 30.0f)
spawnTime = 2.4f;
else if(getTime() < 40.0f)
spawnTime = 4.8f;
else
spawnTime = 6.0f;
}
void decreaseTargetTimeOnScreen()
{
for(int i = 0; i < MAX_NUM_TARGETS; i++)
{
targets[i].decreaseCounter();
if((targets[i].getCounter() <= 0) && (targets[i].getExplosionType() == true) && !explosionFlag)
{
explosionFlag = true;
Vec3 pos = targets[i].getPosition();
expX = pos.x;
expY = pos.y;
}
}
}
// -------------------------------------------------------------------------------------------
short playDisplay()
{
glDisable(GL_DEPTH_TEST);
setSpawnTime();
drawTargets();
drawTime();
drawScore();
drawAmmo();
if(isShotFired)
shotDuration = 5;
if(shotDuration > 0)
{
reticle();
shotDuration--;
}
if(explosionFlag)
{
glPushMatrix();
glTranslatef(expX, expY, 0);
drawExplosion(0, 0);
glPopMatrix();
}
glEnable(GL_DEPTH_TEST);
if(getTime() > 0)
{
return STATE_PLAY;
}
else if(checkNewScore(getScoreItem()))
{
return STATE_HIGHSCORES;
}
else
{
return STATE_GAMEOVER;
}
}