-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShootingGallery.cpp
363 lines (316 loc) · 10.7 KB
/
ShootingGallery.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "ShootingGallery.h"
ShootingGallery::ShootingGallery(GLuint _ShadeProg, Sound* _sound) {
ShadeProg = _ShadeProg;
// Create a wall in the back
gallery = new Object(shapes, materials, ShadeProg);
gallery->load(GALLERY_FILE_NAME);
gallery->setPos(glm::vec3(0.0f, 2.5f, 5.0f));
gallery->scale(glm::vec3(20.0f, 20.0f, 20.0f));
gallery->rotate(180.0f, glm::vec3(0.0f, 1.0f, 0.0f));
gallery->setTexture(textures[TEX_WOOD_RED]);
gallery->setShadows(false, 0.0, 0.0);
backdrop = new Object(shapes, materials, ShadeProg);
backdrop->load(BACKDROP_FILE_NAME);
backdrop->setPos(glm::vec3(0.0f, 5.0f, 10.0f));
backdrop->scale(glm::vec3(50.0f, 75.0f, 1.0f));
backdrop->setTexture(textures[TEX_SKY_DAY]);
// Create a gun to shoot from
gun = new Object(shapes, materials, ShadeProg);
gun->load(RIFLE_FILE_NAME);
gun->setPos(glm::vec3(0.0, 1.0, 1.0));
gun->setShadows(false, 0.0, 0.0);
gun->scale(glm::vec3(4.0f, 4.0f, 1.0f));
gun->setTexture(textures[TEX_STEEL]);
tempFix = new Object(shapes, materials, ShadeProg);
tempFix->load(TARGET_FILE_NAME);
tempFix->setShadows(false, 0.0, 0.0);
tempFix->setPos(glm::vec3(0.0, 0.0, -1.0));
xRotation = yRotation = 0.0f;
sound = _sound;
gameStart = gameOver = false;
score = 0;
elapsedTime = 0.0;
gameOver = false;
doneTimer = -1;
difficulty = 0;
changeDifficulty(0);
}
void ShootingGallery::newTarget(){
// Initialize a target
Object* object = new Object(shapes, materials, ShadeProg);
object->load(TARGET_FILE_NAME);
object->setShadows(false, 0.0, 0.0);
// Pick a random spot from 8 places
float x = 2.0 * (int)(Util::randF() * COLS) - 3.0;
float y = 3.0 * (int)(Util::randF() * ROWS) + 1.0 - 10.0;
float z = DEPTH;
object->setPos(glm::vec3(x, y, z));
object->rotate(-90, glm::vec3(0.0, 1.0, 0.0));
object->setTexture(textures[TEX_TARGET]);
// Have target pop in from bottom of screen
object->setDir(glm::vec3(0.0, 1.0, 0.0));
object->setSpeed(newTargetSpeed);
object->setAccel(newTargetAccel);
object->setChangeDir(true);
// Add the target to the list
targets.push_back(object);
}
ShootingGallery::~ShootingGallery() {
for (int i = 0; i < targets.size(); ++i) {
delete targets[i];
}
for(int i = 0; i < bullets.size(); ++i){
delete bullets[i];
}
delete gallery;
delete backdrop;
delete gun;
delete tempFix;
}
void ShootingGallery::printInstructions() {
ifstream instrFile;
instrFile.open("sginstr.txt");
string line;
float yPos = .55;
float yInc;
fontEngine->useFont("chunk5", 35);
yInc = fontEngine->getTextHeight("blank") * 1.3;
if (instrFile.is_open()) {
while (getline(instrFile, line)) {
if (line[0] == '\n') {
yPos -= yInc;
}
yPos -= yInc;
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), line.c_str(), 0-fontEngine->getTextWidth(line.c_str())/2.0, yPos);
}
}
else {
printf("file 'sginstr.txt' was not available or could not be opened\n");
}
}
void ShootingGallery::step(Window* window) {
tempFix->draw();
gallery->draw();
backdrop->draw();
gun->draw();
if (!gameStart) {
printInstructions();
char diff1[40];
sprintf(diff1, "Difficulty Mode:");
fontEngine->useFont("chunk5", 35);
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), diff1, 0-fontEngine->getTextWidth(diff1)/2.0, .85);
char diff2[20];
if (difficulty == 0) {
sprintf(diff2, "Easy");
}
else if (difficulty == 1) {
sprintf(diff2, "Intermediate");
}
else if (difficulty == 2) {
sprintf(diff2, "Hard");
}
else {
sprintf(diff2, "Extreme");
}
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), diff2, 0-fontEngine->getTextWidth(diff2)/2.0, .85-fontEngine->getTextHeight(diff2)*1.3);
return;
}
if (gameOver) {
float yPos = .3;
float yInc;
char ln1[30];
if (ammo == 0) {
sprintf(ln1, "You're out of ammo!");
}
else {
sprintf(ln1, "You're out of time!");
}
fontEngine->useFont("chunk5", 40);
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), ln1, 0-fontEngine->getTextWidth(ln1)/2.0, yPos);
yInc = fontEngine->getTextHeight(ln1);
yPos -= (yInc * 2);
char ln2[15];
sprintf(ln2, "Score: %d", score);
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), ln2, 0-fontEngine->getTextWidth(ln2)/2.0, yPos);
yPos -= (2 * yInc);
char ln3[20];
sprintf(ln3, "Press ENTER to exit.");
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), ln3, 0-fontEngine->getTextWidth(ln3)/2.0, yPos);
return;
}
if (ammo <= 0) {
global_points += score * targetPoints;
gameOver = true;
}
// Decrement the time limit if not set to unlimited (< 0)
else if (timeLimit > 0) {
timeLimit -= window->dt;
// If the time limit ran out, begin leaving the minigame
if (timeLimit <= 0) {
gameOver = true;
global_points += score * targetPoints;
}
}
// Adds a new target every amount of time
if (window->time - elapsedTime >= newTargetTime) {
newTarget();
elapsedTime = window->time;
}
// Draw the targets
for (int i = 0; i < targets.size(); ++i) {
if (targets[i] == NULL) {
targets.erase(targets.begin() + i);
--i;
continue;
}
targets[i]->setPos(targets[i]->calculateNewPos(window->dt));
targets[i]->draw();
}
// Draw the bullets
for(int i = 0; i< bullets.size(); ++i){
// If the bullet hasn't gone past the targets OR hasn't fallen off screen, do stuff with it
if (bullets[i]->getPos().z <= 10.0 && bullets[i]->getPos().y >= -10){
bullets[i]->setPos(bullets[i]->calculateNewPos(window->dt));
bullets[i]->draw();
// Check collision of bullet against targets
// TODO use spatial data structure
for (int j = 0; j < targets.size(); ++j) {
if (bullets[i]->collidedWithObj(*targets[j], window->dt)) {
sound->playThwackSound();
// Increment score
score++;
// Remove the target
targets.erase(targets.begin() + j);
--j;
// Remove the bullet
bullets.erase(bullets.begin() + i);
--i;
break;
}
}
} else {
// Remove the bullet if it has gone past the targets OR fallen off screen
bullets.erase(bullets.begin() + i);
--i;
}
}
textStep(window);
}
void ShootingGallery::textStep(Window* window) {
float yPos = .9;
float yInc;
int timeLim = (int)timeLimit;
char time[40];
sprintf(time, "Time remaining: 0:%d", timeLim);
fontEngine->useFont("chunk5", 25);
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), time, 0-fontEngine->getTextWidth(time)/2.0, yPos);
yInc = fontEngine->getTextHeight(time) * 1.3;
char diff[25];
if (difficulty == 0) {
sprintf(diff, "Easy Mode");
}
else if (difficulty == 1) {
sprintf(diff, "Intermediate Mode");
}
else if (difficulty == 2) {
sprintf(diff, "Hard Mode");
}
else {
sprintf(diff, "Extreme Mode");
}
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), diff, 0-fontEngine->getTextWidth(diff)/2.0, yPos - yInc);
char scrStr[15];
sprintf(scrStr, "Score: %d", score);
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), scrStr, 1-fontEngine->getTextWidth(scrStr)-.07, yPos);
char ammoStr[15];
sprintf(ammoStr, "Ammo: %d", ammo);
fontEngine->display(glm::vec4(1.0, 1.0, 1.0, 1.0), ammoStr, 1-fontEngine->getTextWidth(ammoStr)-.07, yPos - yInc);
}
void ShootingGallery::mouseClick(glm::vec3 direction, glm::vec4 point) {
// Only shoot a bullet if we have the ammo
if (ammo <= 0 || !gameStart) {
return;
}
glm::vec3 gunPos = gun->getPos();
glm::vec3 clickPt = glm::vec3(point.x, point.y - 7.5, DEPTH);
glm::vec3 gunDir = clickPt - gunPos;
// Shoot a bullet
Object* bullet = new Object(shapes, materials, ShadeProg);
bullet->load("sphere.obj");
// bullet->setPos(glm::vec3(gunPos.x + xRotation/57.0, gunPos.y - yRotation/33.0, gunPos.z + 1.0));
bullet->setPos(glm::vec3(point.x, point.y-7.5, 0.0));
bullet->setDir(direction);
bullet->setTexture(textures[TEX_WOOD_WALL]);
bullet->setShadows(false, 0.0, 0.0);
bullet->setSpeed(30.0f);
bullet->setAccel(0.0f);
bullet->scale(glm::vec3(0.25, 0.25, 0.25));
bullets.push_back(bullet);
// Decrement ammo
ammo--;
}
/**
* The game is finished. Set a delay timer for kicking the player out of the minigame.
*/
void ShootingGallery::finished() {
doneTimer = 2.0f; // 2 seconds
// Tell the user the game is done and we will be exiting
}
void ShootingGallery::exit() {
gameOver = true;
// TODO record score for buying prizes later?
}
void ShootingGallery::mouseMove(double xpos, double ypos, int width, int height) {
// TODO move gun
// Measure mouse x position from center -1.0 to 1.0
double x = (xpos - (width / 2.0)) / (width / 2.0);
// Measure mouse y position from near bottom 0.0 to 1.0
double y = 1.0 - (ypos / (0.8 * height));
// Rotate gun to follow mouse (binded to 45 degrees)
gun->iterativeRotate(0.0f, glm::vec3(0.0, 0.0, 0.0));
xRotation = (-45.0 * x);
gun->iterativeRotate(xRotation, glm::vec3(0.0, 1.0, 0.0));
yRotation = -30.0 * y;
gun->iterativeRotate(yRotation, glm::vec3(1.0, 0.0, 0.0));
}
void ShootingGallery::changeDifficulty(int delta) {
int newDiff = difficulty + delta;
if (gameStart || newDiff < 0 || newDiff > 3)
return;
difficulty = newDiff;
printf("difficulty is now %d\n", difficulty);
switch (difficulty) {
case 0: // EASY
ammo = 20;
timeLimit = 30.0f;
newTargetTime = 3.0f;
newTargetSpeed = 10.0f;
newTargetAccel = -4.5f;
targetPoints = 10;
break;
case 1: // MEDIUM
ammo = 30;
timeLimit = 45.0f;
newTargetTime = 2.0f;
newTargetSpeed = 15.0f;
newTargetAccel = -10.5f;
targetPoints = 20;
break;
case 2: // HARD
ammo = 60;
timeLimit = 60.0f;
newTargetTime = 1.0f;
newTargetSpeed = 20.0f;
newTargetAccel = -18.0f;
targetPoints = 50;
break;
case 3: // EXTREME
ammo = 60;
timeLimit = 30.0f;
newTargetTime = 0.5f;
newTargetSpeed = 20.0f;
newTargetAccel = -18.0f;
targetPoints = 50;
break;
}
}