-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1366 lines (1183 loc) · 35.8 KB
/
main.cpp
File metadata and controls
1366 lines (1183 loc) · 35.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <GLUT/gl.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#endif
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <time.h>
#include <string>
#include "Vector3.h"
#include "QuadFace.h"
#include "GameObject.h"
#include "ObjImporter.h"
using namespace std;
////////////////////////////////////////////////////VARIABLES///////////////////////////////////////////////////////
int timeHour = 12; //peak time
ObjImporter *objImporter;
bool update = false;
bool debug = true;
static float gravity = 0.981f;
int winX = 800;
int winY = 600;
//////////////////////////PLAYER//////////////////////////////////
GameObject* player = new GameObject(1,10,1);
int w = 0;
int s = 0;
int a = 0;
int d = 0;
int aup = 0;
int aleft = 0;
int aright = 0;
int adown = 0;
// angle of rotation for the camera direction
float angle_x=0.0;
float angle_y=0;
float room = 200.0f;
// for mouse
int deltaX;
int deltaY;
// player stats
int maxHealth = 100;
int health = 100;
int maxExperience = 1000;
int experience = 0;
int score = 0;
float fogColor[4] = {0.3, 0.3, 0.3, 0.7};
float fogNear = 150;
float fogFar = 300;
int waveNumber = 0;
bool gameOver = false;
bool playerDead = false;
float deathAlpha = 0.0;
// game variables
vector<GameObject> enemies;
vector<GameObject> environment;
GLfloat mat_diffuse[] = {0.5,0.5,0.5,1.0};
GLfloat tmp_diffuse[] = {0.3,0.3,0.3,1.0};
GLfloat light_position[] = { -40, 30, 0, 0.0 };
GLfloat light_diffuse[] = {1,1,1,1};
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_diffuseTarget[] = {0.917,0.917,0.917};
vector<GameObject> enemies_template;
vector<GameObject> environment_template;
vector< vector<float> > attackList;
double start[] ={0,0,0}, end[]={1,1,1};
// textures
GLuint textures[5];
int width, height, maxImg;
GLubyte* groundTexture;
GLubyte* batTexture;
GLubyte* snailTexture;
GLubyte* rockTexture;
GLubyte* treeTexture;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* LoadPPM -- loads the specified ppm file, and returns the image data as a GLubyte
* (unsigned byte) array. Also returns the width and height of the image, and the
* maximum colour value by way of arguments
*/
GLubyte* LoadPPM(char const* file, int* width, int* height, int* maxImg)
{
GLubyte* img;
FILE *fd;
int n, m;
int k, nm;
char c;
int i;
char b[100];
float s;
int red, green, blue;
/* first open file and check if it's an ASCII PPM (indicated by P3 at the start) */
fd = fopen(file, "r");
fscanf(fd,"%[^\n] ",b);
if(b[0]!='P'|| b[1] != '3')
{
printf("%s is not a PPM file!\n",file);
exit(0);
}
fscanf(fd, "%c",&c);
/* next, skip past the comments - any line starting with #*/
while(c == '#')
{
fscanf(fd, "%[^\n] ", b);
fscanf(fd, "%c",&c);
}
ungetc(c,fd);
/* now get the dimensions and max colour value from the image */
fscanf(fd, "%d %d %d", &n, &m, &k);
//printf("%d rows %d columns max value= %d\n",n,m,k);
/* calculate number of pixels and allocate storage for this */
nm = n*m;
img = (GLubyte*)malloc(3*sizeof(GLuint)*nm);
s=255.0/k;
//s = 1023.0/k;
/* for every pixel, grab the read green and blue values, storing them in the image data array */
for(i=0;i<nm;i++)
{
fscanf(fd,"%d %d %d",&red, &green, &blue );
img[3*nm-3*i-3]=red*s;
img[3*nm-3*i-2]=green*s;
img[3*nm-3*i-1]=blue*s;
}
/* finally, set the "return parameters" (width, height, max) and return the image array */
*width = n;
*height = m;
*maxImg = k;
return img;
}
// removes an enemy from the vector, and give player appropriate reward
void killEnemy(int enemy){
if (enemies.size() > 0){
enemies.erase(enemies.begin()+enemy);
score += 50;
experience += 50;
if (experience >= maxExperience){
experience = 0;
maxExperience += 1000;
}
}
}
//function which preforms intersection test
bool Intersect(){
int x = winX/2;
int y = winY/2;
for (size_t i = 0; i < enemies.size(); i++){
GameObject *g = &enemies.at(i);
glPushMatrix();
glTranslatef(g->position->x, g->position->y, g->position->z);
//allocate matricies memory
double matModelView[16], matProjection[16];
int viewport[4];
//grab the matricies
glGetDoublev(GL_MODELVIEW_MATRIX, matModelView);
glGetDoublev(GL_PROJECTION_MATRIX, matProjection);
glGetIntegerv(GL_VIEWPORT, viewport);
//unproject the values
double winX = (double)x;
double winY = viewport[3] - (double)y;
// get point on the 'near' plane (third param is set to 0.0)
gluUnProject(winX, winY, 0.0, matModelView, matProjection,
viewport, &start[0], &start[1], &start[2]);
// get point on the 'far' plane (third param is set to 1.0)
gluUnProject(winX, winY, 1.0, matModelView, matProjection,
viewport, &end[0], &end[1], &end[2]);
// check for intersection against spherical body of enemy
double A, B, C;
double R0x, R0y, R0z;
double Rdx, Rdy, Rdz;
R0x = start[0];
R0y = start[1];
R0z = start[2];
Rdx = end[0] - start[0];
Rdy = end[1] - start[1];
Rdz = end[2] - start[2];
//magnitude!
double M = sqrt(Rdx*Rdx + Rdy*Rdy + Rdz* Rdz);
//unit vector!
Rdx /= M;
Rdy /= M;
Rdz /= M;
//A = Rd dot Rd
A = Rdx*Rdx + Rdy*Rdy + Rdz*Rdz;
double Btempx, Btempy, Btempz;
int changeInY;
// adjust for different heights of enemies
if (g->returnType() == 3){
changeInY = 2;
}
else{
changeInY = 1;
}
Btempx = R0x;
Btempz = R0z;
Btempy = R0y-changeInY;
B = Btempx * Rdx + Btempy * Rdy + Btempz *Rdz;
B *= 2.0;
C = R0x*R0x + (R0y-changeInY)*(R0y-changeInY) + R0z* R0z - (g->bounds->b->x/2 * g->bounds->b->x/2);
double sq = B*B - 4*A*C;
double t0 = 0, t1 = 0;
if(sq < 0){
}else{
// if intersect then kill enemy
t0 = ((-1) * B + sqrt(sq))/(2*A);
t1 = ((-1) * B - sqrt(sq))/(2*A);
killEnemy(i);
return true;
}
glPopMatrix();
}
return false; //else returns false
}
// initialize the game
void init(){
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// set up player bounds
player->rotation->z = -1;
player->bounds = new Bounds(new Vector3(1,5,1),0);
// set up the textures
glEnable(GL_TEXTURE_2D);
glGenTextures(5, textures);
// load textures
groundTexture = LoadPPM("data/grass.ppm", &width, &height, &maxImg);
glBindTexture(GL_TEXTURE_2D, textures[0]);
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, width, height, GL_RGB, GL_UNSIGNED_BYTE, groundTexture );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
rockTexture = LoadPPM("data/rock.ppm", &width, &height, &maxImg);
glBindTexture(GL_TEXTURE_2D, textures[1]);
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, width, height, GL_RGB, GL_UNSIGNED_BYTE, rockTexture );
treeTexture = LoadPPM("data/Tree.ppm", &width, &height, &maxImg);
glBindTexture(GL_TEXTURE_2D, textures[2]);
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, width, height, GL_RGB, GL_UNSIGNED_BYTE, treeTexture );
batTexture = LoadPPM("data/Bat.ppm", &width, &height, &maxImg);
glBindTexture(GL_TEXTURE_2D, textures[3]);
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, width, height, GL_RGB, GL_UNSIGNED_BYTE, batTexture );
snailTexture = LoadPPM("data/Snail.ppm", &width, &height, &maxImg);
glBindTexture(GL_TEXTURE_2D, textures[4]);
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, width, height, GL_RGB, GL_UNSIGNED_BYTE, snailTexture );
// load the objects using the obj importer
objImporter = new ObjImporter();
GameObject* g = new GameObject();
objImporter->import("data/Bat.obj");
g->setVertexList(objImporter->getVertices());
g->setFaceList(objImporter->getFaces());
g->setNormalList(objImporter->getVertexNormals());
g->setTextureList(objImporter->getVertexTextures());
g->setDiffuse(objImporter->getDiffuse());
g->setAmbient(objImporter->getAmbient());
g->isActive = true;
enemies_template.push_back(g);
GameObject* g2 = new GameObject();
objImporter->import("data/Snail.obj");
g2->setVertexList(objImporter->getVertices());
g2->setFaceList(objImporter->getFaces());
g2->setNormalList(objImporter->getVertexNormals());
g2->setTextureList(objImporter->getVertexTextures());
g2->setDiffuse(objImporter->getDiffuse());
g2->setAmbient(objImporter->getAmbient());
g2->isActive = true;
enemies_template.push_back(g2);
GameObject* g3 = new GameObject();
objImporter->import("data/Rock.obj");
g3->setVertexList(objImporter->getVertices());
g3->setFaceList(objImporter->getFaces());
g3->setNormalList(objImporter->getVertexNormals());
g3->setTextureList(objImporter->getVertexTextures());
g3->setDiffuse(objImporter->getDiffuse());
g3->setAmbient(objImporter->getAmbient());
g3->isActive = true;
environment_template.push_back(g3);
GameObject* g4 = new GameObject();
objImporter->import("data/Tree.obj");
g4->setVertexList(objImporter->getVertices());
g4->setFaceList(objImporter->getFaces());
g4->setNormalList(objImporter->getVertexNormals());
g4->setTextureList(objImporter->getVertexTextures());
g4->setDiffuse(objImporter->getDiffuse());
g4->setAmbient(objImporter->getAmbient());
g4->isActive = true;
environment_template.push_back(g4);
// enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
// spawn x enemies where x = num, and of a certain type
// 3 = bat, 4 = snail
void spawnEnemies(int num, int type){
for (int eNum= 0; eNum < num; eNum++){
// randomize spawn location, but don't let it spawn too close to player
int tx = rand()% (int)(room*2-40) - room+20;
int tz = rand()% (int)(room*2-40) - room+20;
while(tx > player->position->x -20 && tx < player->position->x + 20)
tx = rand()% (int)(room*2-40) - room+20;
while(tz > player->position->z -20 && tz < player->position->z + 20)
tz = rand()% (int)(room*2-40) - room+20;
// randomize a starting direction for the enemy
float dx, dz;
if (tx < player->position->x)
dx = rand()% 100 / 100.0;
else
dx = -1*(rand()% 100 / 100.0);
if (tz < player->position->z)
dz = rand()% 100 / 100.0;
else
dz = -1*(rand()% 100 / 100.0);
// add the enemy to the enemies vector
if (type == 3){
enemies.push_back(new GameObject(enemies_template.at(0)));
enemies.at(enemies.size()-1).setType(3);
}
else if (type == 4){
enemies.push_back(new GameObject(enemies_template.at(1)));
enemies.at(enemies.size()-1).setType(4);
}
// adjust the height values
enemies.at(enemies.size()-1).position->x = tx;
if (enemies.at(enemies.size()-1).type == 3){
enemies.at(enemies.size()-1).position->y = 5;
}
else if (enemies.at(enemies.size()-1).type == 4){
enemies.at(enemies.size()-1).position->y = 0;
}
enemies.at(enemies.size()-1).position->z = tz;
enemies.at(enemies.size()-1).setDirection(new Vector3(dx, 0, dz));
}
}
// push an attack onto the array list
void enemyAttack(int i){
vector<float> att;
att.push_back(enemies.at(i).position->x);
att.push_back(enemies.at(i).position->y + 1);
att.push_back(enemies.at(i).position->z);
float xVec = player->position->x - enemies.at(i).position->x;
float zVec = player->position->z - enemies.at(i).position->z;
// normalize
float vLen = sqrt(xVec*xVec + zVec*zVec);
att.push_back(xVec/vLen);
att.push_back(0.0);
att.push_back(zVec/vLen);
// bats do 1 damage, snails do 2
if (enemies[i].returnType() == 3){
att.push_back(1.0);
}
else if (enemies[i].returnType() == 4){
att.push_back(2.0);
}
attackList.push_back(att);
}
// start the game
void startGame(){
// add trees and rocks
for (int i = 0; i < 100; i++){
// randomize location of the trees and rocks, but
// dont let them spawn near the player
int rx = rand()% (int)room*2 - room;
int rz = rand()% (int)room*2 - room;
while(rx > player->position->x -20 && rx < player->position->x + 20)
rx = rand()% (int)(room*2-40) - room+20;
while(rz > player->position->z -20 && rz < player->position->z + 20)
rz = rand()% (int)(room*2-40) - room+20;
// scale the trees and rocks randomly
float scale;
if (i < 50){
scale = (float)rand()/(float)(RAND_MAX) * 1.8 + 1;
environment.push_back(new GameObject(environment_template.at(0)));
environment.at(environment.size()-1).setType(1);
}
else if (i >= 50){
scale = (float)rand()/(float)(RAND_MAX) * 2 + 1;
environment.push_back(new GameObject(environment_template.at(1)));
environment.at(environment.size()-1).setType(2);
}
environment.at(environment.size()-1).position->x = rx;
environment.at(environment.size()-1).position->y = 0;
environment.at(environment.size()-1).position->z = rz;
environment.at(environment.size()-1).scale->x = scale;
environment.at(environment.size()-1).scale->y = scale;
environment.at(environment.size()-1).scale->z = scale;
}
}
// update positions of the enemy attacks
void moveEnemyAttacks(){
bool deleteAttack;
for (int i = 0; i < attackList.size(); i++){
deleteAttack = false;
attackList[i][0] += attackList[i][3];
attackList[i][1] += attackList[i][4];
attackList[i][2] += attackList[i][5];
// remove it if it collides with a tree or rock
for (int e = 0; e < environment.size(); e++){
// rock
if (environment.at(e).returnType() == 1){
if (attackList[i][0] > environment.at(e).position->x - environment.at(e).scale->x - 2 && attackList[i][0] < environment.at(e).position->x +environment.at(e).scale->x + 2 && attackList[i][2] > environment.at(e).position->z - environment.at(e).scale->z - 2 && attackList[i][2] < environment.at(e).position->z + environment.at(e).scale->z + 2 && attackList[i][1] < 5 + environment.at(e).scale->y){
deleteAttack = true;
}
}
// trees
if (environment.at(e).returnType() == 2){
if (attackList[i][0] > environment.at(e).position->x - environment.at(e).scale->x - 1.5 && attackList[i][0] < environment.at(e).position->x + environment.at(e).scale->x + 1.5 &&
attackList[i][2] > environment.at(e).position->z - environment.at(e).scale->z -1.5 && attackList[i][2] < environment.at(e).position->z + environment.at(e).scale->z + 1.5 && attackList[i][1] < 15 + environment.at(e).scale->y){
deleteAttack = true;
}
}
}
// also do attack with player collision here
if (attackList[i][0] > player->position->x - 2 && attackList[i][0] < player->position->x + 2 && attackList[i][2] > player->position->z -2 && attackList[i][2] < player->position->z + 2 && attackList[i][1] < 20){
// remove the attack and lower health
health -= attackList[i][6];
deleteAttack = true;
}
// remove it if it exits the playing area
if (attackList[i][0] > room || attackList[i][0] < -room || attackList[i][2] > room || attackList[i][2] < -room){
deleteAttack = true;
}
if (deleteAttack)
attackList.erase(attackList.begin() + i);
}
}
// draw the enemy attacks using spheres
void renderEnemyAttacks(){
glDisable(GL_TEXTURE_2D);
for (int i = 0; i < attackList.size(); i++){
glPushMatrix();
glTranslatef(attackList[i][0], attackList[i][1], attackList[i][2]);
glutSolidSphere(0.3, 5, 5);
glPopMatrix();
}
glEnable(GL_TEXTURE_2D);
}
// the AI for the enemies
void enemyAI(){
float x = player->position->x;
float y = player->position->y;
float z = player->position->z;
// for each enemy...
for (int i = 0; i < enemies.size(); i ++){
// move the enemy based on current direction
if (enemies[i].returnType() == 3){
enemies[i].position->x += enemies[i].direction->x /2;
enemies[i].position->z += enemies[i].direction->z /2;
}
else if (enemies[i].returnType() == 4){
enemies[i].position->x += enemies[i].direction->x /4;
enemies[i].position->z += enemies[i].direction->z /4;
}
// adjust the direction based on where the player is, with a randomized value
// in the algorithm so they move differently from other enemies
float adjustmentFactor = rand()%400 + 600;
if (enemies[i].position->x <= player->position->x)
enemies[i].direction->x += (player->position->x - enemies[i].position->x) /adjustmentFactor;
else if (enemies[i].position->x > player->position->x)
enemies[i].direction->x -= (enemies[i].position->x - player->position->x) /adjustmentFactor;
if (enemies[i].position->z <= player->position->z)
enemies[i].direction->z += (player->position->z - enemies[i].position->z) /adjustmentFactor;
else if (enemies[i].position->z > player->position->z)
enemies[i].direction->z -= (enemies[i].position->z - player->position->z) /adjustmentFactor;
float dx = player->position->x - enemies[i].position->x;
float dz = player->position->z - enemies[i].position->z;
float m = sqrt(dx*dx + dz*dz);
dx /= m;
dz /= m;
float roty;
roty = (float) atan2(dx, dz) * 180 / 3.14;
enemies[i].rotation->y = roty;
// normalize the vector
float len = sqrt(enemies[i].direction->x *enemies[i].direction->x + enemies[i].direction->z*enemies[i].direction->z);
enemies[i].direction->x /= len;
enemies[i].direction->z /= len;
// bounce off the walls
if (enemies[i].position->x <= -room + 10){
enemies[i].direction->x *= -1;
}
else if (enemies[i].position->x >= room-10 ){
enemies[i].direction->x *= -1;
}
if (enemies[i].position->z <= -room +10 ){
enemies[i].direction->z *= -1;
}
else if (enemies[i].position->z >= room-10){
enemies[i].direction->z *= -1;
}
// decide whether to attack or not
int aRand = rand()%200;
// bats attack faster than snails
if (enemies[i].returnType() == 3){
if (aRand < 2){
enemyAttack(i);
}
}
else if (enemies[i].returnType() == 4){
if (aRand == 0){
enemyAttack(i);
}
}
}
// ground the enemies
for (size_t i = 0; i < enemies.size(); i++){
enemies.at(i).update();
if (enemies.at(i).isGrounded){
Vector3* v = new Vector3(0,200,0);
enemies.at(i).addForce(v);
delete v;
}
}
}
//Camera Control
// move player based on what angle camera is at
void movementListener(){
float x = 0;
float y = 0;
float z = 0;
// set it so that the angle is always between -2pi rad and 2pi rad
if (angle_x > 6.28319 || angle_x < -6.28319){
angle_x = 0;
}
float fraction;
fraction = 4;
if ((angle_x >= 0 && angle_x <= 3.14159/2) || (angle_x > 3.14159 && angle_x <= 3.14159 + 3.14159/2) || (angle_x < 0 && angle_x >= -3.14159/2) || (angle_x < -3.14159 && angle_x >= -3.14159 - 3.14159/2)){
if (w){
z -= fraction * cos(angle_x + 3.14/2);
x += fraction*sin(angle_x + 3.14/2);
}
if (s){
z += fraction * cos(angle_x + 3.14/2);
x -= fraction*sin(angle_x + 3.14/2);
}
if (a){
x -= fraction * cos(angle_x+ 3.14/2);
z -= fraction * sin(angle_x + 3.14/2);
}
if (d){
x += fraction * cos(angle_x + 3.14/2);
z += fraction * sin(angle_x + 3.14/2);
}
}
else if ((angle_x > 3.14159/2 && angle_x <= 3.14159)|| (angle_x > 3.14159 + 3.14159/2 && angle_x < 6.28319)||(angle_x < -3.14159/2 && angle_x > -3.14159) || (angle_x < -3.14159 - 3.14159/2 && angle_x > -6.28319)) {
if (w){
z -= fraction * cos(angle_x + 3.14/2);
x += fraction*sin(angle_x + 3.14/2);
}
if (s){
z+= fraction * cos(angle_x + 3.14/2);
x -= fraction*sin(angle_x + 3.14/2);
}
if (a){
z -= fraction * cos(angle_x);
x += fraction * sin(angle_x);
}
if (d){
z += fraction * cos(angle_x);
x -= fraction * sin(angle_x);
}
}
player->acceleration->x = x;
player->acceleration->y = y;
player->acceleration->z = z;
// make sure player stays within room bounds
if (player->position->x - 5 < -room)
player->position->x = -room+5;
else if (player->position->x + 5 > room)
player->position->x = room-5;
if (player->position->z - 5 < -room)
player->position->z = -room+5;
else if (player->position->z + 5 > room)
player->position->z = room - 5;
}
// rotate the screen based on how the mouse moved
void rotateScreen(){
angle_x -= (float)(deltaX * 0.001);
angle_y += (float)(deltaY * 0.001);
float lx = sin(angle_x + (3.14)/2) * 0.001;
float lz = -cos(angle_x+ (3.14)/2) * 0.001;
float ly = angle_y * 0.001;
if (deltaX != 0 || deltaY != 0)
glutWarpPointer(winX/2, winY/2);
player->rotation->x = lx;
player->rotation->y = ly;
player->rotation->z = lz;
}
//Drawing the room
void renderScene(void) {
// Draw ground
glColor3f(0.9f, 0.9f, 0.9f);
glTexImage2D(GL_TEXTURE_2D, 9,GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, groundTexture);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glBegin(GL_QUADS);
glNormal3f(0,1,0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glTexCoord2f(0, 0);
glVertex3f(-room, 0.0f, -room);
glTexCoord2f(100, 0);
glVertex3f(-room, 0.0f, room);
glTexCoord2f(100, 100);
glVertex3f( room, 0.0f, room);
glTexCoord2f(0,100);
glVertex3f( room, 0.0f, -room);
glEnd();
// draw walls
glTexImage2D(GL_TEXTURE_2D, 9,GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, rockTexture);
glBindTexture(GL_TEXTURE_2D, textures[1]);
glBegin(GL_QUADS);
tmp_diffuse[0] = mat_diffuse[0] - 0.1;
tmp_diffuse[1] = mat_diffuse[1] - 0.1;
tmp_diffuse[2] = mat_diffuse[2] - 0.1;
glMaterialfv(GL_FRONT, GL_DIFFUSE, tmp_diffuse);
glTexCoord2f(0, 0);
glVertex3f(-room, 0, -room);
glTexCoord2f(0, 20);
glVertex3f(-room, room*2, -room);
glTexCoord2f(20, 20);
glVertex3f(-room, room*2, room);
glTexCoord2f(20, 0);
glVertex3f(-room, 0, room);
tmp_diffuse[0] -= 0.1;
tmp_diffuse[1] -= 0.1;
tmp_diffuse[2] -= 0.1;
glMaterialfv(GL_FRONT, GL_DIFFUSE, tmp_diffuse);
glTexCoord2f(0, 0);
glVertex3f(-room, 0, room);
glTexCoord2f(0, 20);
glVertex3f(-room, room*2, room);
glTexCoord2f(20, 20);
glVertex3f(room, room*2, room);
glTexCoord2f(20, 0);
glVertex3f(room, 0, room);
tmp_diffuse[0] += 0.1;
tmp_diffuse[1] += 0.1;
tmp_diffuse[2] += 0.1;
glMaterialfv(GL_FRONT, GL_DIFFUSE, tmp_diffuse);
glTexCoord2f(0, 0);
glVertex3f(room, 0, room);
glTexCoord2f(0, 20);
glVertex3f(room, room*2, room);
glTexCoord2f(20, 20);
glVertex3f(room, room*2, -room);
glTexCoord2f(20, 0);
glVertex3f(room, 0, -room);
tmp_diffuse[0] += 0.2;
tmp_diffuse[1] += 0.2;
tmp_diffuse[2] += 0.2;
glMaterialfv(GL_FRONT, GL_DIFFUSE, tmp_diffuse);
glTexCoord2f(0, 0);
glVertex3f(room, 0, -room);
glTexCoord2f(0, 20);
glVertex3f(room, room*2, -room);
glTexCoord2f(20, 20);
glVertex3f(-room, room*2, -room);
glTexCoord2f(20, 0);
glVertex3f(-room, 0, -room);
glEnd();
for (int i = 0; i < environment.size(); i++){
// bind tree texture (only need to bind tree since rock takes the texture of the wall)
if (environment.at(i).returnType() == 2){
glTexImage2D(GL_TEXTURE_2D, 9,GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, treeTexture);
glBindTexture(GL_TEXTURE_2D, textures[2]);
}
environment.at(i).draw();
}
}
// draw the enemies
void renderEnemies(){
for (size_t i = 0; i < enemies.size(); i++){
// bind bat texture
if (enemies.at(i).returnType() == 3){
glTexImage2D(GL_TEXTURE_2D, 9,GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, batTexture);
glBindTexture(GL_TEXTURE_2D, textures[3]);
}
// bind snail texture
else if (enemies.at(i).returnType() == 4){
glTexImage2D(GL_TEXTURE_2D, 9,GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, snailTexture);
glBindTexture(GL_TEXTURE_2D, textures[4]);
}
enemies.at(i).draw();
}
}
// check for collision between objects
void checkCollision(){
// when 2 enemies get too close, adjust their directions so they move away from each other
if (enemies.size() > 1)
for (size_t i = 0; i < enemies.size(); i++){
for (size_t j = i+1; j < enemies.size(); j++){
if (j != i){
Vector3* a = enemies.at(i).position;
Vector3* b = enemies.at(j).position;
float dist = sqrt((a->x - b->x)*(a->x - b->x) + (a->y - b->y)*(a->y - b->y) + (a->y - b->y)*(a->y - b->y));
a = enemies.at(i).bounds->b;
b = enemies.at(j).bounds->b;
float minDist = a->x + b->x; + 20; //radius + distance where they start slowing down
if (minDist > dist){ //two objects are too close
if(enemies[i].direction->x <= 0)
enemies[i].direction->x += 0.002;
else if (enemies[i].direction->x > 0)
enemies[i].direction->x -= 0.002;
if(enemies[i].direction->z <= 0)
enemies[i].direction->z += 0.002;
else if (enemies[i].direction->z > 0)
enemies[i].direction->z -= 0.002;
}
}
}
}
// don't let the player walk into trees or rocks
Vector3* pp = player->position;
Vector3* pb = player->bounds->b;
for (size_t p = 0; p < environment.size(); p++){
Vector3* b = environment.at(p).position;
float dist = sqrt((pp->x - b->x)*(pp->x - b->x) + (pp->z - b->z)*(pp->z - b->z));
Vector3* bb = environment.at(p).bounds->b;
float minDist = pb->x + bb->x + environment.at(p).scale->x;
if (minDist > dist){ //Too close
player->position->x = pp->x - player->velocity->x *1.1 ;
player->position->z = pp->z - player->velocity->z*1.1 ;
//direction from player to that object
float xdir = b->x - pp->x;
float zdir = b->z - pp->z;
float m = sqrt((xdir*xdir) + (zdir*zdir));
//unit vector of normal
xdir /= m;
zdir /= m;
player->velocity->x = -player->velocity->x * 0.5;
player->velocity->z = -player->velocity->z * 0.5;
}
}
}
// draws the HUD for the player
void drawOverlay(void){
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, 800, 600, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glDisable(GL_DEPTH_TEST);
// player is dead if health <= 0
if(health <= 0){
health = 0;
playerDead = true;
}
// add a shade of red to the screen based on how low player hp is
if (!playerDead){
if (health < 50)
deathAlpha = 0.002*(50-health);
}
//if game over, slowly fill the screen with red
else if (playerDead){
if (deathAlpha<0.6)
deathAlpha += 0.01;
else{
gameOver = true;
}
}
glColor4f(1.0, 0, 0, deathAlpha);
glBegin(GL_POLYGON);
glVertex2f(0, 0);
glVertex2f(0, winY);
glVertex2f(winX, winY);
glVertex2f(winX, 0);
glEnd();
// draw basic background squares for HUD
glColor3f(0.7, 0.7, 0.7);
glBegin(GL_POLYGON);
glVertex2f(20, 20);
glVertex2f(20, 80);
glVertex2f(230, 80);
glVertex2f(230, 20);
glEnd();
glBegin(GL_POLYGON);
glVertex2f(780, 20);
glVertex2f(780, 70);
glVertex2f(730, 70);
glVertex2f(730, 20);
glEnd();
glColor3f(1, 0, 0);
glLineWidth(1);
// draw targeting reticle
glBegin(GL_LINE_LOOP);
for(int i = 0; i <10; i++)
{
float theta = 2.0f * 3.1415926f * float(i) / float(10);//get the current angle
float x = 20 * cosf(theta);//calculate the x component
float y = 20 * sinf(theta);//calculate the y component
glVertex2f(x + 400, y + 300);//output vertex
}
glEnd();
glBegin(GL_LINES);
glVertex2f(400, 270);
glVertex2f(400, 330);
glEnd();
glBegin(GL_LINES);
glVertex2f(370, 300);
glVertex2f(430, 300);
glEnd();
glColor3f(0, 1, 0);
float drawnHp = health*2 + 25;
glLineWidth(10);
glBegin(GL_LINES);
glVertex2f(25, 40);
glVertex2f(drawnHp, 40);
glEnd();
glColor3f(1, 1, 0);
float drawnExp = 200*((float)experience/maxExperience) + 25;
glBegin(GL_LINES);
glVertex2f(25, 60);
glVertex2f(drawnExp, 60);
glEnd();
glLineWidth(3);
// draw a sun or a moon depending on time hour
// daytime, draw a sun
if (timeHour > 5 && timeHour < 19)
glColor3f(1, 1, 0);
// nighttime, draw a moon
else
glColor3f(0.9, 0.9, 0.9);
glBegin(GL_POLYGON);
glVertex2f(755, 30);
glVertex2f(750, 32.5);
glVertex2f(745, 35);
glVertex2f(742.5, 40);
glVertex2f(740, 45);
glVertex2f(742.5, 50);
glVertex2f(745, 55);
glVertex2f(750, 57.5);
glVertex2f(755, 60);
glVertex2f(760, 57.5);
glVertex2f(765, 55);
glVertex2f(767.5, 50);
glVertex2f(770, 45);
glVertex2f(767.5, 40);
glVertex2f(765, 35);
glVertex2f(760, 32.5);
glEnd();
// show score at bottom of screen
glColor3f(1, 1, 0);
// show wave number
char waveDisplay1 [5];
strcpy(waveDisplay1, "Wave ");
char waveDisplay2 [3];
itoa(waveNumber, waveDisplay2, 10);
glPushMatrix();
glTranslatef(30, winY-30, 0);
glScalef(0.15, -0.15, 1);
for (int i = 0; i < strlen(waveDisplay1); i++)
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, waveDisplay1[i]);
for (int i = 0; i < strlen(waveDisplay2); i++)
glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, waveDisplay2[i]);
glPopMatrix();