-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchristianR.cpp
More file actions
679 lines (665 loc) · 16.8 KB
/
christianR.cpp
File metadata and controls
679 lines (665 loc) · 16.8 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
//Author: Christian Russell
//June 20, 2017
//-Created Control Manager
//--Interprets keyboard
//--Drunk swerve calculation based on drunkness level
//--Reads keyboard and applies controls:
//--Turn left, right, and speed up, and slow down
//--Bounds checking to make sure you don't swerve off map
//--Send you to beginning of road once a certain point is reached
//---to create seamless loop
//--Makes light follow car like a headlight
//--Animation function for when an object is hit
//-Added to Game class
//--Converter for camera speed to MPH
//--Converter for distance traveled to Miles
//-Road Obstacle Class
//--This is a class that can serve as a parent class
//---type for all obstacles. Effects will be overrided
//---virtual functions
//-Other Changes
//--Added debug info screen to display important variables
//--Added function to blur the screen for a period of time
#include <math.h>
#include <stdio.h>
#include <ctime>
#include <string>
#include <sstream>
#include <unistd.h>
#include <vector>
#include <map>
#include "game.h"
#include "fonts.h"
#include "ppm.h"
#ifdef __APPLE__
#else
#include <malloc.h>
#endif
#define BASIC_MOVEMENT 0.1
#define MAX_MOVEMENT 1
#define MIN_MOVEMENT 0.1
#define ROAD_WIDTH 4
#define SPEED_TO_MPH_MULT 100
#define FPS 50
#define OBSTACLE_DEPTH 1.0
#define OBSTACLE_WIDTH 1.0
#define OBSTACLE_HEIGHT 2
#define OBSTACLE_RENDER_DIST 100
#define CAMERA_HEIGHT 1
#define COOLDOWN_DRINK 5
#define COOLDOWN_BAC .000008
#define BAC_PER_BEER .025
#define TURN_MAX 0.1
#define TURN_SPEED 0.01
#define SPEED_MOD_FASTER .001
#define SPEED_MOD_SLOWER .005
extern std::list<RoadObstacle*> obstacles;
bool ControlManager::movingLeft,
ControlManager::movingRight,
ControlManager::slowingDown,
ControlManager::speedingUp,
ControlManager::hittingObject;
double ControlManager::applyDrunkSwerve(Game& g)
{
double swerveMovement = ControlManager::calculateSwerveModifier(g);
if (ControlManager::movingLeft) {
if (swerveMovement < 0) {
swerveMovement = -1 * BASIC_MOVEMENT / 2;
}
} else if (ControlManager::movingRight) {
if (swerveMovement > 0) {
swerveMovement = BASIC_MOVEMENT / 2;
}
} else {
}
return swerveMovement;
}
void ControlManager::moveForward(Game& g)
{
if (g.gameState != UNPAUSED) {
return;
}
//TODO Two Headlights as light source
g.cameraPosition[2] -= g.speed;
g.distanceTraveled += g.speed;
g.lightPosition[2] = g.cameraPosition[2];
displayHitAnimation(g);
//Turn
double totalTurning = 0;
if (movingLeft && !hittingObject) {
totalTurning -= 0.1;
}
if (movingRight && !hittingObject) {
totalTurning += 0.1;
}
totalTurning += ControlManager::applyDrunkSwerve(g);
g.cameraPosition[0] += totalTurning;
//Pivot Camera
if (!hittingObject) {
if (totalTurning < 0) {
g.up[0] += TURN_SPEED;
} else if (totalTurning > 0) {
g.up[0] -= TURN_SPEED;
} else {
if (g.up[0] > 0) {
g.up[0] -= TURN_SPEED / 2;
if (g.up[0] < 0) {
g.up[0] = 0;
}
} else if (g.up[0] < 0) {
g.up[0] += TURN_SPEED / 2;
if (g.up[0] > 0) {
g.up[0] = 0;
}
}
}
if (g.up[0] > TURN_MAX) {
g.up[0] = TURN_MAX;
}
if (g.up[0] < -TURN_MAX) {
g.up[0] = -TURN_MAX;
}
}
//Speed
if (speedingUp && !hittingObject) {
if (g.speed < MAX_MOVEMENT) {
g.speed += SPEED_MOD_FASTER;
}
}
if (slowingDown && !hittingObject) {
if (g.speed > MIN_MOVEMENT) {
g.speed -= SPEED_MOD_SLOWER;
}
}
if (g.cameraPosition[2] <= -100) {
g.cameraPosition[2] = 2;
}
}
double ControlManager::calculateSwerveModifier(Game& g)
{
static bool currentlySwerving = false;
static int inebLevel = g.getInebriationLevel();
if (hittingObject) {
//Cannot use tires in the air
currentlySwerving = false;
return 0.0;
}
if (currentlySwerving) {
//Don't interrupt current swerve upon higher drunkenness
//printf("Already swerving\n");
} else {
//No swerve in progress; update value
if (inebLevel != g.getInebriationLevel()) {
printf("Disregarding previous level: %d\n", inebLevel);
inebLevel = g.getInebriationLevel();
printf("Recording new level: %d\n", inebLevel);
}
}
switch (inebLevel) {
case 1:
//Random jerking movements
//printf("Calculating swerve for: %d\n", inebLevel);
static double progress = 0;
static bool backwards = false;
if (currentlySwerving) {
if (progress >= (3/2) * PI) {
//swerve is over
progress = 0;
currentlySwerving = false;
return 0;
} else {
//continue swerve
progress += .1;
if (backwards) {
return -1 * (sin(progress) / 30);
} else {
return sin(progress) / 30;
}
}
} else {
//Random chance to start swerve
int randNum = rand() % 1000;
//printf("Rand num: %d\n", randNum);
if (randNum < 5) {
currentlySwerving = true;
if (rand() % 2 == 1) {
//printf("Start swerving to the right\n");
backwards = false;
} else {
//printf("Start swerving to the left\n");
backwards = true;
}
} else {
//printf("Won't start swerving\n");
}
return 0;
}
break;
case 2:
//Consistent bobbing and weaving
return sin(g.cameraPosition[2] / 6) / 30;
break;
case 3:
//Consistent bobbing and weaving and fade to black
return sin(g.cameraPosition[2] / 4.5) / 30;
break;
case 4:
//Random blackouts
if (int randNum = rand() % 1000 < 5) {
blackoutScreen(g, randNum);
}
return sin(g.cameraPosition[2] / 4.5) / 30;
break;
case 5:
//Longer blackouts
if (int randNum = rand() % 1000 < 8) {
blackoutScreen(g, randNum);
}
return sin(g.cameraPosition[2] / 4.5) / 30;
break;
case 6:
//Crash into side; you are dead
static bool right = (rand() % 2 == 1) ? true : false;
if (right) {
return 0.5;
} else {
return -0.5;
}
return 0;
break;
default:
//No modifier
return 0;
break;
}
}
void ControlManager::drinkBeer(Game& g)
{
g.bloodAlcoholContent += BAC_PER_BEER;
g.minimumBAC += BAC_PER_BEER * .33;
g.cooldownDrink = COOLDOWN_DRINK;
}
void ControlManager::applyControls(Game& g, int key, bool isPress)
{
switch (key) {
case XK_Right:
case XK_d:
ControlManager::movingRight = isPress;
break;
case XK_Left:
case XK_a:
ControlManager::movingLeft = isPress;
break;
case XK_Up:
case XK_w:
ControlManager::speedingUp = isPress;
break;
case XK_Down:
case XK_s:
ControlManager::slowingDown = isPress;
break;
case XK_j:
if (isPress && g.gameState == UNPAUSED) {
if (g.cooldownDrink == 0) {
printf("Key is pressed: %s (%d)\n", "Drinking a nice, cold bev", key);
drinkBeer(g);
}
}
break;
case XK_p:
if (isPress) {
if (g.gameState == PAUSED) {
g.gameState = UNPAUSED;
} else if (g.gameState == UNPAUSED) {
g.gameState = PAUSED;
}
}
break;
case XK_Escape:
printf("Key is pressed: %s (%d)\n", "Escape", key);
g.done = 1;
break;
#ifdef DEBUG
case XK_h:
if (isPress && g.gameState == UNPAUSED) {
ControlManager::playAnimationHit();
}
break;
case XK_r:
reset(&g);
break;
case XK_n:
if (isPress) {
if (g.timeOfDay == DAY) {
g.timeOfDay = NIGHT;
} else {
g.timeOfDay = DAY;
}
}
break;
#endif
}
}
void ControlManager::checkBounds(Game& g)
{
if (g.gameState != UNPAUSED) {
return;
}
if (g.cameraPosition[0] > ROAD_WIDTH) {
g.cameraPosition[0] = ROAD_WIDTH;
ControlManager::movingLeft = ControlManager::movingRight = false;
#ifndef DEBUG
g.gameState = GAMEOVER;
//printf("Gameover!\n");
#endif
}
if (g.cameraPosition[0] < -1 * ROAD_WIDTH) {
g.cameraPosition[0] = -1 * ROAD_WIDTH;
ControlManager::movingLeft = ControlManager::movingRight = false;
#ifndef DEBUG
g.gameState = GAMEOVER;
//printf("Gameover!\n");
#endif
}
}
void ControlManager::playAnimationHit()
{
//Called by external functions to begin playing the animation
if (hittingObject) {
//printf("Hit animation already in progress\n");
return;
} else {
//printf("Playing hit animation\n");
hittingObject = true;
}
}
void ControlManager::displayHitAnimation(Game& g, bool restart)
{
//Called every frame to display any hit animations
static double progress = 0.0;
if (restart) {
progress = 0;
hittingObject = false;
}
if (hittingObject) {
if (progress >= 3) {
progress = 0.0;
hittingObject = false;
g.up[1] = 0;
g.cameraPosition[1] = CAMERA_HEIGHT;
return;
} else {
g.cameraPosition[1] = CAMERA_HEIGHT * 1.5;
}
progress += .1;
//g.cameraPosition[1] = CAMERA_HEIGHT + (sin(progress) / 2);
g.up[1] = -0.2 * sin(progress);
g.cameraPosition[1] = CAMERA_HEIGHT + (sin(progress) * .65);
}
}
void ControlManager::reset(Game* g)
{
for (std::list<RoadObstacle*>::iterator it= obstacles.begin();
it != obstacles.end(); ++it) {
(*it)->isActive = true;
}
movingLeft =
movingRight =
slowingDown =
speedingUp = false;
g->up[0] = 0;
g->up[1] = 0;
g->up[2] = 1;
MakeVector(0.0, 1.0, 0.0, g->cameraPosition);
g->speed = 0.1;
g->distanceTraveled = 0;
g->bloodAlcoholContent = g->minimumBAC = 0;
g->cooldownDrink = 0;
displayHitAnimation(*g, true);
}
double Game::getMPH()
{
return speed * SPEED_TO_MPH_MULT;
}
double Game::getDistanceMiles()
{
return distanceTraveled / SPEED_TO_MPH_MULT / FPS;
}
int Game::getInebriationLevel()
{
if (bloodAlcoholContent < 0.04) {
return 0;
} else if (bloodAlcoholContent < 0.08) {
return 1;
} else if (bloodAlcoholContent < .1) {
return 2;
} else if (bloodAlcoholContent < .14) {
return 3;
} else if (bloodAlcoholContent < .2) {
return 4;
} else if (bloodAlcoholContent < .3) {
return 5;
} else {
return 6;
}
}
std::string Game::getInebriationDescription()
{
std::string desc;
switch (getInebriationLevel()) {
case 0:
desc = "Sober";
break;
case 1:
desc = "Buzzed";
break;
case 2:
desc = "Legally Drunk";
break;
case 3:
desc = "90's Drunk";
break;
case 4:
desc = "Fucked Up";
break;
case 5:
desc = "Blackout Drunk";
break;
case 6:
desc = "Dead";
break;
default:
desc = "Error";
}
return desc;
}
void Game::updateCooldowns()
{
if (gameState == PAUSED) {
return;
}
//Called every frame -- reduces each cooldown by one
if (cooldownDrink > 0) {
cooldownDrink--;
}
if (bloodAlcoholContent > minimumBAC) {
bloodAlcoholContent -= COOLDOWN_BAC;
}
}
std::map<std::string, Ppmimage*> textures;
RoadObstacle::RoadObstacle(double roadPosLR, double roadPosDistance,
std::string spriteLoc, int frameWidth, int frameHeight)
{
isActive = true;
roadPositionLR = roadPosLR;
roadPositionDistance = roadPosDistance;
frameColumns = frameWidth;
frameRows = frameHeight;
if (textures.find(spriteLoc) != textures.end()) {
//Already in map
sprite = textures[spriteLoc];
} else {
//Not in map
sprite = ppm6GetImage(spriteLoc.c_str());
textures.insert(textures.begin(),
std::pair<std::string,Ppmimage*>(spriteLoc,sprite));
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
unsigned char *texData = buildAlphaData(sprite);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sprite->width, sprite->height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, texData);
free(texData);
}
bool RoadObstacle::isCameraInside(Game& g)
{
if (g.distanceTraveled > roadPositionDistance + OBSTACLE_DEPTH) {
return false;
}
if (g.distanceTraveled < roadPositionDistance - OBSTACLE_DEPTH) {
return false;
}
if (g.cameraPosition[0] < roadPositionLR - (OBSTACLE_WIDTH/2.0)) {
return false;
}
if (g.cameraPosition[0] > roadPositionLR + (OBSTACLE_WIDTH/2.0)) {
return false;
}
return true;
}
void RoadObstacle::triggerHitEffects()
{
ControlManager::playAnimationHit();
}
void RoadObstacle::render(Game& g)
{
//This function renders the object on the road. Can be called on all
//Objects, because it tests if they’re in the road
if (g.distanceTraveled > roadPositionDistance) {
//Object has been passed
return;
}
if (g.distanceTraveled < roadPositionDistance - OBSTACLE_RENDER_DIST) {
//Object is too far ahead to see
return;
}
//Calculate distance
double distanceAhead = roadPositionDistance - g.distanceTraveled;
//Render
glPushMatrix();
//glNormal3f(0.0f, 0.0f, 1.0f);
glColor3f(1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0f);
glColor4ub(255,255,255,255);
//static std::time_t lastTime = time(nullptr);
static int framesPassed = 0;
static int walkFrame = 0;
int ix = walkFrame % frameColumns;
int iy = (walkFrame / frameColumns) % frameRows;
float ty = (float)iy / frameColumns;
float tx = (float)ix;
//if (lastTime < time(nullptr)) {
if (framesPassed > 20) {
walkFrame++;
framesPassed = 0;
}
framesPassed++;
glBegin(GL_QUADS);
//Corner 1
glTexCoord2f(tx, ty+(1.0/frameRows));
glVertex3f(roadPositionLR + OBSTACLE_WIDTH,OBSTACLE_HEIGHT,
g.cameraPosition[2] - distanceAhead);
//Corner 2
glTexCoord2f(tx, ty);
glVertex3f(roadPositionLR - OBSTACLE_WIDTH,OBSTACLE_HEIGHT,
g.cameraPosition[2] - distanceAhead);
//Corner 3
glTexCoord2f(tx+(1.0/frameColumns), ty);
glVertex3f(roadPositionLR - OBSTACLE_WIDTH,0,
g.cameraPosition[2] - distanceAhead);
//Corner 4
glTexCoord2f(tx+(1.0/frameColumns), ty+(1.0/frameRows));
glVertex3f(roadPositionLR + OBSTACLE_WIDTH,0,
g.cameraPosition[2] - distanceAhead);
glEnd();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_ALPHA_TEST);
}
void blackoutScreen(Game& g, float secs)
{
if (g.gameState == PAUSED) {
return;
}
//Progress starts at 0 and ends at Pi;
static double progress = 0;
static double seconds = 1;
static bool finished = true;
static double progressPerCall;
if (progress >= PI) {
//Free the function up for further use
finished = true;
progress = 0;
return;
} else if (!finished) {
//Continue blackout
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0.0, 0.0, 0.0, sin(progress));
glTranslated(g.xres, g.yres, 0);
glBegin(GL_QUADS);
glVertex2i(-g.xres, -g.yres);
glVertex2i(-g.xres, g.yres);
glVertex2i(g.xres, g.yres);
glVertex2i(g.xres, -g.yres);
glEnd();
glDisable(GL_BLEND);
glPopMatrix();
progress += progressPerCall;
return;
} else if (secs > 0) {
//Start new blur
progressPerCall = PI / (seconds * FPS);
finished = false;
seconds = secs;
return;
}
}
void drawPauseMenu(Game& g)
{
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(0,0,0,0.25);
glTranslated(g.xres, g.yres, 0);
glBegin(GL_QUADS);
double w = g.xres;
double h = g.yres;
glBegin(GL_QUADS);
glVertex2i(-w,-h);
glVertex2i(-w, h);
glVertex2i(w, h);
glVertex2i(w,-h);
glEnd();
glDisable(GL_BLEND);
glPopMatrix();
}
#ifdef DEBUG
void drawDebugInfo(Game& g)
{
Rect debugStats;
debugStats.bot = g.yres - 20;
debugStats.left = g.xres - (175);
debugStats.center = 0;
char buffer[50];
//Speed
sprintf(buffer, "Speed: %02.3f", g.speed);
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
sprintf(buffer, "Speed: %2.1f MPH", g.getMPH());
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//Distance Traveled
sprintf(buffer, "Distance Traveled: %.3f", g.distanceTraveled);
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
sprintf(buffer, "Distance Traveled: %.1f Miles", g.getDistanceMiles());
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//Left/Right position
sprintf(buffer, "Road Position (R/L): %.3f", g.cameraPosition[0]);
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//Camera Position
sprintf(buffer, "Road Position (Dist): %.3f", g.cameraPosition[2]);
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//Vertical position
sprintf(buffer, "Road Position (Vertical): %.3f", g.cameraPosition[1]);
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//Inebriation Level
sprintf(buffer, "Drunkness Level: %d", g.getInebriationLevel());
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//Inebriation Level
sprintf(buffer, "You are: %s", g.getInebriationDescription().c_str());
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//BAC
sprintf(buffer, "BAC: %.3f", g.bloodAlcoholContent);
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//BAC
sprintf(buffer, "Minimum BAC: %.3f", g.minimumBAC);
ggprint8b(&debugStats, 16, 0x00FFFF00, buffer);
//Drinking Cooldown
sprintf(buffer, "Cooldown (Drinking): %d", g.cooldownDrink);
ggprint8b(&debugStats, 16, 0x00FFA500, buffer);
//Controls
ggprint8b(&debugStats, 16, 0x0000FF00, "W/Up - Speed Up");
ggprint8b(&debugStats, 16, 0x0000FF00, "S/Down - Slow Down");
ggprint8b(&debugStats, 16, 0x0000FF00, "A/Left - Speed Up");
ggprint8b(&debugStats, 16, 0x0000FF00, "D/Right - Turn Right");
ggprint8b(&debugStats, 16, 0x0000FF00, "H - Hit Animation Test");
ggprint8b(&debugStats, 16, 0x0000FF00, "J - Drink a beer");
ggprint8b(&debugStats, 16, 0x0000FF00, "P - Pause");
ggprint8b(&debugStats, 16, 0x0000FF00, "R - Restart");
ggprint8b(&debugStats, 16, 0x0000FF00, "N - Day/Night Mode");
}
#endif