-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
785 lines (675 loc) · 21.1 KB
/
main.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
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
#include "globals.h"
using std::cin;
using std::cout;
std::vector<Building*> buildings; // must be a pointer so that we dont try to allocated GL things before it has been inited
std::vector<Target*> targets;
double camMove_forward = 0;
double camMove_strafe = 0;
double camMove_vert = 0;
const double camMove_speed = 0.25 / 2.0;
double tankAccel = 0;
double tankScale = 0;
double tankBaseRotate = 0;
double tankTurretRotate = 0;
double tankCannonRotate = 0;
bool laserOn = true;
int cameraMode = 0;
Tank * tank;
bool orthoView = false;
bool aerial = false;
Skybox * skybox;
Groundbox * groundbox;
bool captureTime = 0;
int oldTime=0.0;
float actualfps, fps=0.0;
int currentTime;
double timeRemaining = TIME_LIMIT;
std::vector<AI_Tank *> ai_tanks;
std::vector<Projectile*> projectiles;
void mouseButtons(int but,int state,int x,int y){
//scaleMouse(x,y);
//this is still needed since we are expecting to measure from the bottom left with how i set things up but the mouse is reported from the top left
y=GLOBAL.WINDOW_MAX_Y-y;
if(but==0 && state==GLUT_DOWN){
//left mouse button
tank->shoot();
}else if(but==2 && state==GLUT_DOWN){
//right mouse button
}else if(but==3 && state==GLUT_DOWN){
//scroll up
}else if(but==4 && state==GLUT_DOWN){
//scroll down
}else{
if(state == GLUT_DOWN)printf("Unknown Mouse Button %d\n",but);
}
}
bool youLose()
{
return ((GLOBAL.gameOver) || (timeRemaining <= 0));
}
void passiveMouseMovement(int x,int y){
//x and y are window cordinates
//it is up to us to get deltas
if(!youLose())
{
cameraMovement(x,y,tank->center,cameraMode);
tank->turretFollowMouse(x, y,cameraMode);
}
}
void mouseMovement(int x,int y){
//x and y are window cordinates
//it is up to us to get deltas
// FPS_CameraMovement(x,y);
}
void gameEngine(){
if(tank->health == 0)
GLOBAL.gameOver = true;
if(!GLOBAL.gameOver)
{
for(int x=0; x<buildings.size(); x++)
buildings[x]->update();
for(int x=0; x<targets.size();x++)
targets[x]->update();
GLOBAL.CAMERA_POS.z += camMove_vert;
GLOBAL.CAMERA_POS.x += camMove_forward * cos(GLOBAL.CAMERA_ANGLE_HORIZONTAL*PI/180.0);
GLOBAL.CAMERA_POS.y += camMove_forward * -sin(GLOBAL.CAMERA_ANGLE_HORIZONTAL*PI/180.0);
GLOBAL.CAMERA_POS.x += camMove_strafe * sin(GLOBAL.CAMERA_ANGLE_HORIZONTAL*PI/180.0);
GLOBAL.CAMERA_POS.y += camMove_strafe * cos(GLOBAL.CAMERA_ANGLE_HORIZONTAL*PI/180.0);
//iterate tank properties
tank->update(tankBaseRotate, tankTurretRotate, tankCannonRotate, cameraMode, tankAccel); // the things below need to be moved into this function
for(int x=0; x<ai_tanks.size();x++){
ai_tanks[x]->updateTank();
ai_tanks[x]->nearbyTarget(tank);
}
skybox->update();
GLOBAL.LIGHTS[0].position[0]=tank->center.x;
GLOBAL.LIGHTS[0].position[1]=tank->center.y;
GLOBAL.LIGHTS[0].position[2]=tank->center.z+5;
GLOBAL.LIGHTS[1].position[0]=tank->center.x;
GLOBAL.LIGHTS[1].position[1]=tank->center.y;
GLOBAL.LIGHTS[2].position[0]=tank->center.x;
GLOBAL.LIGHTS[2].position[1]=tank->center.y;
// GLOBAL.LIGHTS[2].position[2]=tank->center.z+10;
for(int i=projectiles.size()-1; i >=0 ; i--){
projectiles[i]->update();
if(projectiles[i]->state==Projectile::DEAD)
projectiles.erase(projectiles.begin()+i);
}
}
}
void drawGameOver(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // reset the projection style
gluOrtho2D(0.0,100.0,100.0,0.0); // simple ortho
glLineWidth(5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
int i, len;
char label[] = "GAME OVER";
void *font = GLUT_STROKE_ROMAN;
glTranslatef(82, 90, 0);
glScalef(0.5, 0.5, 0.5);
glPushMatrix();
glColor3f(1.0,1.0,0.0);
glRotatef(180.0,1.0,0.0,0.0);
glScalef(0.105, 0.105, 0.105);
len = (int) strlen(label);
glTranslatef(-1530.0, 1575, 0);
for(i = 0;i<len;i++)
glutStrokeCharacter(font, label[i]);
glPopMatrix();
char label2[] = "Nice Try But";
glPushMatrix();
glColor3f(0.0,1.0,0.0);
glRotatef(180.0,1.0,0.0,0.0);
glScalef(0.105, 0.105, 0.105);
len = (int) strlen(label2);
glTranslatef(-1100.0, 1150, 0);
for(i = 0;i<len;i++)
glutStrokeCharacter(font, label2[i]);
glPopMatrix();
char label3[] = "YOU LOST!";
glPushMatrix();
glColor3f(0.0,1.0,1.0);
glRotatef(180.0,1.0,0.0,0.0);
glScalef(0.105, 0.105, 0.105);
len = (int) strlen(label3);
glTranslatef(-755.0, 725, 0);
for(i = 0;i<len;i++)
glutStrokeCharacter(font, label3[i]);
glPopMatrix();
char label4[] = "Your score was: ";
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glRotatef(180.0,1.0,0.0,0.0);
glScalef(0.08, 0.08, 0.08);
len = (int) strlen(label4);
glTranslatef(-1700.0, 400, 0);
for(i = 0;i<len;i++)
glutStrokeCharacter(font, label4[i]);
std::ostringstream printNum;
std::string printy;
printNum << GLOBAL.score;
printy = printNum.str();
len = (int) strlen(&printy[0]);
for(i = 0;i<len;i++)
glutStrokeCharacter(font,printy[i]);
printNum.str("");
glPopMatrix();
char label5[] = "Press 'q' to exit or 'r' to restart";
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glRotatef(180.0,1.0,0.0,0.0);
glScalef(0.06, 0.06, 0.06);
len = (int) strlen(label5);
glTranslatef(-2140.0, 300, 0);
for(i = 0;i<len;i++)
glutStrokeCharacter(font, label5[i]);
glPopMatrix();
glPopMatrix();
glLineWidth(1);
}
void drawTime() {
glPushMatrix();
int i, len;
char label[] = "Time Remaining: ";
void *font = GLUT_STROKE_ROMAN;
glTranslatef(82, 90, 0);
glScalef(0.15, 0.15, 0.15);
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glRotatef(180.0,1.0,0.0,0.0);
glScalef(0.125,0.125,0.125);
glTranslatef(-550.0, 300, 0);
len = (int) strlen(label);
for(i = 0;i<len;i++)
glutStrokeCharacter(font, label[i]);
std::ostringstream printNum;
std::string printy;
timeRemaining = TIME_LIMIT - difftime(time(0) ,GLOBAL.timeStart);
printNum << timeRemaining;
printy = printNum.str();
len = (int) strlen(&printy[0]);
for(i = 0;i<len;i++)
glutStrokeCharacter(font,printy[i]);
printNum.str("");
glPopMatrix();
glPopMatrix();
}
void drawHud() {//to draw the 2d hud on 3d scene
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // reset the projection style
gluOrtho2D(0.0,100.0,100.0,0.0); // simple ortho
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//draw 2D stuff
drawHealthBar(*tank);
drawCooldownBar(*tank);
drawScore();
drawTime();
showFPS(fps, oldTime, actualfps);
}
void drawWorld(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // reset the values
double aspect = (GLOBAL.WINDOW_MAX_X/(double)GLOBAL.WINDOW_MAX_Y);
if(!orthoView){
gluPerspective(90,aspect,0.1,1000);
{
double temp[3]={
GLOBAL.CAMERA_POS.x + GLOBAL.CAMERA_LOOK_VECTOR.x,
GLOBAL.CAMERA_POS.y + GLOBAL.CAMERA_LOOK_VECTOR.y,
GLOBAL.CAMERA_POS.z + GLOBAL.CAMERA_LOOK_VECTOR.z
};
gluLookAt(
GLOBAL.CAMERA_POS.x,GLOBAL.CAMERA_POS.y,GLOBAL.CAMERA_POS.z,
temp[0],temp[1],temp[2],
0,0,1
);
}
}
else{
// Herron: sets orthographic view
glOrtho( 500.0, -500.0, 500.0, -500.0, 0.1, 1000);
gluLookAt(450.0, 450.0, 800.0, 450.0 , 450.0, 0.0, 0.0, -1.0, -1.0);
}
glMatrixMode(GL_MODELVIEW);
if(!orthoView){
for(int x=0; x<buildings.size(); x++)
buildings[x]->draw();
for(int x=0; x<targets.size(); x++)
targets[x]->draw();
}else{
for(int x=0; x<buildings.size(); x++)
buildings[x]->draw_simple();
for(int x=0; x<targets.size(); x++)
targets[x]->draw_map_marker();
}
tank->draw();
for(int x=0; x<ai_tanks.size();x++)
ai_tanks[x]->tank->draw();
for(int i=0; i<projectiles.size();i++){
projectiles[i]->draw();
}
skybox->draw();
groundbox->draw();
//tank->drawHealthBar(tank->health);
}
void drawBoundingBoxes(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // reset the values
double aspect = (GLOBAL.WINDOW_MAX_X/(double)GLOBAL.WINDOW_MAX_Y);
if(!orthoView){
gluPerspective(90,aspect,0.1,1000);
{
double temp[3]={
GLOBAL.CAMERA_POS.x + GLOBAL.CAMERA_LOOK_VECTOR.x,
GLOBAL.CAMERA_POS.y + GLOBAL.CAMERA_LOOK_VECTOR.y,
GLOBAL.CAMERA_POS.z + GLOBAL.CAMERA_LOOK_VECTOR.z
};
gluLookAt(
GLOBAL.CAMERA_POS.x,GLOBAL.CAMERA_POS.y,GLOBAL.CAMERA_POS.z,
temp[0],temp[1],temp[2],
0,0,1
);
}
}
else{
glOrtho(-500.0, 500.0, -500.0, 500.0, 0.1, 1000);{
gluLookAt(450.0, 450.0, 800.0, 450.0 , 450.0, 0.0, 0.0, -1.0, -1.0);
}
}
glMatrixMode(GL_MODELVIEW);
// for(int x=0; x<buildings.size(); x++)
// buildings[x]->draw();
// tank->draw();
// ai_tank->tank->draw();
// for(int i=0; i<projectiles.size();i++){
// projectiles[i]->draw();
// }
for(int x=0; x<targets.size(); x++){
auto box = targets[x]->getBoundingBox();
for(int y=0; y<box.size(); y++){
box[y].drawLines();
}
}
{
auto box = tank->getBoundingBox();
for(int y=0; y<box.size(); y++){
box[y].setColor(255,255,255);
box[y].drawLines();
}
}
}
void drawMinimap(){
double height,width;
height = 100;
width = GLOBAL.WINDOW_MAX_X/(double)GLOBAL.WINDOW_MAX_Y * height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // reset the projection style
glOrtho(-width,width , -height,height , -5.0,500.0); // simple ortho - left,right,bottom,top,near,far
gluLookAt(
tank->center.x,tank->center.y,400,
tank->center.x,tank->center.y,0,
0,1,0
);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
{//Make a black polygon to draw our world on top of so the regular world does not bleed through
double &x=tank->center.x;
double &y=tank->center.y;
glColor3ub(0,0,0);
glBegin(GL_POLYGON);
glVertex3d(x-width,y-height,-50);
glVertex3d(x+width,y-height,-50);
glVertex3d(x+width,y+height,-50);
glVertex3d(x-width,y+height,-50);
glEnd();
}
for(int x=0; x<buildings.size(); x++)
buildings[x]->draw_simple();
for(int x=0; x<targets.size(); x++)
targets[x]->draw_map_marker();
tank->draw();
for(int x=0; x<ai_tanks.size();x++)
ai_tanks[x]->tank->draw();
}
void display(){
//remove this clear for game over cast over moment of failure
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,GLOBAL.WINDOW_MAX_X,GLOBAL.WINDOW_MAX_Y);
if(!youLose()) {
if(!orthoView)glEnable(GL_LIGHTING);
updateLights();
drawWorld();
collisionTest();
//===============================================================================
glDisable(GL_LIGHTING);
glClear(GL_DEPTH_BUFFER_BIT);
drawHud();
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0,0,GLOBAL.WINDOW_MAX_X/4,GLOBAL.WINDOW_MAX_Y/4);
if(!orthoView)drawMinimap();
}
else {
glDisable(GL_LIGHTING);
drawGameOver();
}
glFlush();
glutSwapBuffers();
if(captureTime == 1)
{
screencapture(GLOBAL.step);
GLOBAL.step++;
}
glutPostRedisplay(); //always say we want a redraws
}
void keyboardButtons(unsigned char key, int x, int y){
if(key == 'q' || key == 'Q'){
exit(0);
}else if(key == 'w' || key == 'W'){
if(cameraMode==0)camMove_forward += camMove_speed;
else tankAccel += 0.005;
}else if(key == 's' || key == 'S'){
if(cameraMode==0)camMove_forward -= camMove_speed;
else tankAccel -= 0.005;
}else if(key == 'a' || key == 'A'){
if(cameraMode==0)camMove_strafe += camMove_speed;
else tankBaseRotate += 2;
}else if(key == 'd' || key == 'D'){
if(cameraMode==0)camMove_strafe -= camMove_speed;
else tankBaseRotate -= 2;
}else if(key == 'n' || key == 'N'){
tankScale -= 0.05;
}else if(key == 'm' || key == 'M'){
tankScale += 0.05;
}else if(key == 'z' || key == 'Z'){
if(cameraMode>=2){//currently looking at three camera modes that we switch between
cameraMode = 0;
}
else{
cameraMode++;
}
}else if(key == '-' || key == '_'){
tankCannonRotate -= 2;
}else if(key == '=' || key == '+'){
tankCannonRotate += 2;
}else if(key == '8'){
tank->laser = !tank->laser;
//end of tank controls
}else if(key == 'c' || key == 'C'){
camMove_vert += camMove_speed;
}else if(key == ' '){
camMove_vert -= camMove_speed;
}else if(key == 'x' || key == 'X'){
tank->shoot();
}else if(key == 'y' || key == 'Y'){
if(tank->health >= 10)
tank->health = tank->health-10;
//glutPostRedisplay();
//printf("%d\n", tank->health);
}else if(key == 'v' || key == 'V' ){
// Herron: sets to top down orthographic view
orthoView = !orthoView;
}else if(key == 'r' || key == 'R'){
if(GLOBAL.gameOver)//when game over, r to restart game
{
delete tank;
for(int x=ai_tanks.size()-1; x>=0; x--){
delete ai_tanks[x];
ai_tanks.pop_back();
}
tank = new Tank(Point(0, Building::maxBuildingWidth/2.0 + Building::streetWidth/2.0, 0));
for(int x=0; x<NUM_AI_TANKS; x++){
int dx = rand()%NUM_BLOCKS_WIDE * Building::distanceBetweenBuildings;
int dy = rand()%NUM_BLOCKS_WIDE * Building::distanceBetweenBuildings;
AI_Tank * ai_tank = new AI_Tank(new Tank(
Point(Building::maxBuildingWidth/2.0 + Building::streetWidth/2.0 + dx,
Building::maxBuildingWidth/2.0 + Building::streetWidth/2.0 + dy,
0)
));
ai_tanks.push_back(ai_tank);
}
GLOBAL.reset();
}
}else{
// printf("Unknown Key Down %d\n",key);
}
if(camMove_forward > camMove_speed)
camMove_forward = camMove_speed;
if(camMove_forward < -1 * camMove_speed)
camMove_forward = -1 * camMove_speed;
if(camMove_strafe > camMove_speed)
camMove_strafe = camMove_speed;
if(camMove_strafe < -1 * camMove_speed)
camMove_strafe = -1 * camMove_speed;
if(camMove_vert > camMove_speed)
camMove_vert = camMove_speed;
if(camMove_vert < -1 * camMove_speed)
camMove_vert = -1 * camMove_speed;
}
void keyboardButtonsUp(unsigned char key, int x, int y){
if(key == 'q' || key == 'Q'){
exit(0);
}else if(key == 'w' || key == 'W'){
if(cameraMode==0)camMove_forward -= camMove_speed;
else tankAccel -= 0.005;
}else if(key == 's' || key == 'S'){
if(cameraMode==0) camMove_forward += camMove_speed;
else tankAccel += 0.005;
}else if(key == 'a' || key == 'A'){
if(cameraMode==0) camMove_strafe -= camMove_speed;
else tankBaseRotate -= 2;
}else if(key == 'd' || key == 'D'){
if(cameraMode==0) camMove_strafe += camMove_speed;
else tankBaseRotate += 2;
}else if(key == 'n' || key == 'N'){
}else if(key == 'm' || key == 'M'){
}else if(key == '-' || key == '_'){
tankCannonRotate += 2;
}else if(key == '=' || key == '+'){
tankCannonRotate -= 2;
//end of tank controls
}else if(key == 'c' || key == 'C'){
camMove_vert -= camMove_speed;
}else if(key == ' '){
camMove_vert += camMove_speed;
}else if(key == 'x' || key == 'X' || key == 'y' || key == 'Y'){
//do nothing, but stop printing unknown key
}else{
// printf("Unknown Key Up %d\n",key); This is kind of annoying
}
if(camMove_forward > camMove_speed)
camMove_forward = camMove_speed;
if(camMove_forward < -1 * camMove_speed)
camMove_forward = -1 * camMove_speed;
if(camMove_strafe > camMove_speed)
camMove_strafe = camMove_speed;
if(camMove_strafe < -1 * camMove_speed)
camMove_strafe = -1 * camMove_speed;
if(camMove_vert > camMove_speed)
camMove_vert = camMove_speed;
if(camMove_vert < -1 * camMove_speed)
camMove_vert = -1 * camMove_speed;
}
void keyboardButtons_special(int key,int x,int y){
if(key == GLUT_KEY_UP){
}else if(key == GLUT_KEY_DOWN){
}else if(key == GLUT_KEY_LEFT){
}else if(key == GLUT_KEY_RIGHT){
}else if(key == GLUT_KEY_PAGE_UP){
}else if(key == GLUT_KEY_PAGE_DOWN){
}else{
printf("Unknown Special Key Down %d\n",key);
}
}
void keyboardButtonsUp_special(int key,int x,int y){
if(key == GLUT_KEY_UP){
}else if(key == GLUT_KEY_DOWN){
}else if(key == GLUT_KEY_LEFT){
}else if(key == GLUT_KEY_RIGHT){
}else if(key == GLUT_KEY_PAGE_UP){
}else if(key == GLUT_KEY_PAGE_DOWN){
}else{
printf("Unknown Special Key Up %d\n",key);
}
}
void joystickControls(unsigned int buttonMask, int x, int y, int z)
{
//This was for figuring out all the buttonmasks and stuff from the joystick control.
/*unsigned int a = buttonMask;
for(int i = 0; i < 32; i++)
{
printf("%d", a & 1);
a >>=1;
}
printf(" %d %d %d \n", x, y, z);*/
//Mapped to fire button, and mapped to trigger for pointer finger. Bitshifting and bitwise and.
if( buttonMask & 1 || (buttonMask >> 1) & 1)
tank->shoot();
//Left hand pointer finger controls base movement.
if((buttonMask >> 20) & 1)
{
tankBaseRotate = -2;
}
else if((buttonMask >> 22) & 1)
{
tankBaseRotate = 2;
}
else
{
tankBaseRotate = 0;
}
//It was really easy go up to 200, so I set a large deadzone for staying at zero. Technically, at exactly 200, nothing happens.
if(abs(z) < 200)
{
tankAccel = 0;
}
else if(z < 200)
{
tankAccel = 0.005;
}
else if(z > -200)
{
tankAccel = -0.005;
}
//We want to check if either are above, otherwise it locks controls to just on coordinate and not the other.
if(abs(x) > 50 || abs(y) > 50)
{
//Fun ternary stayements to check and set the delta x or y values.
int xMove = abs(x) > 50 ? (x > 0 ? 2:-2) : 0;
int yMove = abs(y) > 50 ? (y > 0 ? -2:2) : 0;
//We're pretending to be the mouse here. Where as we would read from the mouse and move the camera
//we instead pretend the mouse has moved by added our x and y movements to the center, where the mouse
//ordinarily would be. This makes it seem as though the mouse moved, when its just the joystick.
cameraMovement(GLOBAL.WINDOW_MAX_X/2 + xMove, GLOBAL.WINDOW_MAX_Y/2 + yMove,tank->center,cameraMode);
tank->turretFollowMouse(GLOBAL.WINDOW_MAX_X/2 + xMove, GLOBAL.WINDOW_MAX_Y/2 + yMove,cameraMode);
}
}
int main(int argc,char** args){
if(argc > 1)
captureTime=1;
glutInit(&argc, args);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_ALPHA);
glutInitWindowPosition(0,0);
glutInitWindowSize(GLOBAL.WINDOW_MAX_X,GLOBAL.WINDOW_MAX_Y);
glutCreateWindow("Pendulum");
glClearColor(0,0,0,0);
GLOBAL.step=0;
glutReshapeFunc(windowResize);
glutDisplayFunc(display);
glutIdleFunc(gameEngine);
glutPassiveMotionFunc(passiveMouseMovement);
glutMotionFunc(mouseMovement);
glutMouseFunc(mouseButtons);
glutKeyboardFunc(keyboardButtons);
glutKeyboardUpFunc(keyboardButtonsUp);
glutSpecialFunc(keyboardButtons_special);
glutSpecialUpFunc(keyboardButtonsUp_special);
glutJoystickFunc(joystickControls, 1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
// glEnable(GL_LIGHT2);
// glEnable(GL_LIGHT3);
// glEnable(GL_LIGHT4);
// glEnable(GL_LIGHT5);
// glEnable(GL_LIGHT6);
// glEnable(GL_LIGHT7);
glShadeModel(GL_SMOOTH);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); // make the lighting track the color of objects
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
//let people use random numbers without worrying about how to seed things
srand(time(NULL));
// enable blending to have translucent materials
// you must draw objects back to front to get proper blending
//glEnable(GL_BLEND);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ONE);
for(int x=0;x<NUM_BLOCKS_WIDE;x++){
for(int y=0;y<NUM_BLOCKS_WIDE;y++){
buildings.push_back(new Building(Point(
Building::distanceBetweenBuildings*x,
Building::distanceBetweenBuildings*y,
0)
));
int randSide = (rand()/(double)RAND_MAX) * 4;
double randomHeight = rand() / (double)RAND_MAX;
double maxHeight = buildings[buildings.size()-1]->getBoundingBox()[0][0].z;
double targetCenter = randomHeight * (3.0*maxHeight/4.0) + (maxHeight/8.0); //randomly spawns in the middle 3/4 of the building
if(randSide == 0) {//"north" wall
targets.push_back(new Target(Point(
Building::distanceBetweenBuildings*x,
Building::distanceBetweenBuildings*y - (Building::maxBuildingWidth)/2.0 - 0.55,
targetCenter)
));
} else if(randSide == 1) {//"west"
Target *tDawg = new Target(Point(
Building::distanceBetweenBuildings*x + (Building::maxBuildingWidth)/2.0 + 0.55,
Building::distanceBetweenBuildings*y - (Building::maxBuildingWidth)/32.0,
targetCenter));
(*tDawg).setRotation(90.0);
targets.push_back(tDawg);
} else if(randSide == 2) {//"south
Target *tDawg = new Target(Point(
Building::distanceBetweenBuildings*x,
Building::distanceBetweenBuildings*y + (Building::maxBuildingWidth)/2.0 + 0.55,
targetCenter));
(*tDawg).setRotation(180.0);
targets.push_back(tDawg);
} else if(randSide == 3) {//"east"
Target *tDawg = new Target(Point(
Building::distanceBetweenBuildings*x - (Building::maxBuildingWidth)/2.0 - 0.55,
Building::distanceBetweenBuildings*y - (Building::maxBuildingWidth)/32.0,
targetCenter));
(*tDawg).setRotation(-90.0);
targets.push_back(tDawg);
} else {
std::cout << "SOMETHING HAS GONE HORRIBLY WRONG" << std::endl;
exit(0);
}
}
}
tank = new Tank(Point(0, Building::maxBuildingWidth/2.0 + Building::streetWidth/2.0, 0));
for(int x=0; x<NUM_AI_TANKS; x++){
int dx = rand()%NUM_BLOCKS_WIDE * Building::distanceBetweenBuildings;
int dy = rand()%NUM_BLOCKS_WIDE * Building::distanceBetweenBuildings;
AI_Tank * ai_tank = new AI_Tank(new Tank(
Point(Building::maxBuildingWidth/2.0 + Building::streetWidth/2.0 + dx,
Building::maxBuildingWidth/2.0 + Building::streetWidth/2.0 + dy,
0)
));
ai_tanks.push_back(ai_tank);
}
oldTime = glutGet(GLUT_ELAPSED_TIME);
skybox = new Skybox();
groundbox = new Groundbox(Building::maxBuildingWidth, Building::streetWidth, Building:: sidewalkWidth);
glutMainLoop();
return 0;
}