-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMap.cpp
More file actions
687 lines (578 loc) · 15.9 KB
/
Map.cpp
File metadata and controls
687 lines (578 loc) · 15.9 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
#include "Map.h"
#include <cstdlib>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include "strstream"
#include <bitset>
using std::strstreambuf;
using std::istrstream;
using std::ostrstream;
using std::strstream;
using std::fstream;
using std::bitset;
#define ESC 27
#define M_PI 3.1415926
using namespace std;
Map::Map()
{
//ctor
this->score = 0;
this->autoplay = 0;
this->xUnits = 28;
this->zUnits = 28;
this->worldsEdgeX = 28*2;
this->worldsEdgeZ = 28*2;
this->ghostUnits = 6 ;
this->initTiles();
this->level = 1;
this->initLevel1();
//Draw lights
this->streetLights[0] = new StreetLamp(GL_LIGHT1,0.0,4.0,0.0,4.0,0.0,4.0); //back left
this->streetLights[1] = new StreetLamp(GL_LIGHT2,0.0,4.0,56.0,48.0,0.0,-48.0);//front left
this->streetLights[2] = new StreetLamp(GL_LIGHT3,56.0,4.0,0.0,-48.0,0.0,48.0);//back right
this->streetLights[3] = new StreetLamp(GL_LIGHT4,56.0,4.0,56.0,-48.0,0.0,-48.0);//front right
GLfloat ambientlightColor[] = { 1.0, 1.0, 1.0, 0.5 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientlightColor);
this->pelletsTextureState = 1;
this->masterStreetLightState = 1;
this->allTextureState = true;
}
Map::~Map()
{
//dtor
}
void Map::initLevel1()
{
cout<<"************Level 1***************"<<endl;
this->createMaze();//initialize new map;
this->initPellets();
GLfloat ghostData[this->ghostUnits][2];
ghostData[0][0] = 1.0;
ghostData[0][1] = 1.0;
ghostData[1][0] = 27.0;
ghostData[1][1] = 1.0;
ghostData[2][0] = 27.0;
ghostData[2][1] = 55.0;
ghostData[3][0] = 55.0;
ghostData[3][1] = 55.0;
ghostData[4][0] = 55.0;
ghostData[4][1] = 27.0;
ghostData[5][0] = 1.0;
ghostData[5][1] = 27.0;
this->initGhosts(ghostData);
this->pacman = new Pacman(1.0f,1.1f,1.0f);
this->createMaze();
return;
}
void Map::initLevel2()
{
cout<<"************LEVEL UP!! On to level 2***************"<<endl;
this->initPellets();
GLfloat ghostData[this->ghostUnits][2];
ghostData[0][0] = 27.0;
ghostData[0][1] = 27.0;
ghostData[1][0] = 27.0;
ghostData[1][1] = 29.0;
ghostData[2][0] = 27.0;
ghostData[2][1] = 25.0;
ghostData[3][0] = 25.0;
ghostData[3][1] = 27.0;
ghostData[4][0] = 25.0;
ghostData[4][1] = 25.0;
ghostData[5][0] = 25.0;
ghostData[5][1] = 29.0;
this->initGhosts(ghostData);
this->pacman = new Pacman(27.0f,1.1f,55.0f);
this->createMaze();
return;
}
void Map::draw()
{
if(this->allTextureState)
glEnable( GL_TEXTURE_2D );
else
glDisable( GL_TEXTURE_2D );
if(this->pacman->isAlive()==1)
{
this->pacman->camera.SetOrigin(this->pacman->getX(), this->pacman->getY()+1, this->pacman->getZ());
char* canMove = this->whatDirectionsCanHeMove(this->pacman->getX(), this->pacman->getZ());
if(this->autoplay==1) //Auto play mode. Pacman's random movement.
{
int moveSwitch = rand() % 4;
if(moveSwitch==0)//if he can move choosen direction, then move there!
this->pacman->walkForward(canMove);
else if(moveSwitch==1)
this->pacman->turnLeft();
else if (moveSwitch==2)
this->pacman->turnRight();
else if(moveSwitch==3)//if he can move choosen direction, then move there!
this->pacman->walkBackward(canMove);
}
this->pacmanGhostCollisionDetection();
this->gotPellet();
this->pacman->draw();
//If theere is an active power pellet, decrease it's life span.
if(this->pacman->hasPelletPower()==1)
this->pacman->decreasePelletPowerTime();
}
else
{
this->newLevel();
}
this->drawTiles();
this->drawStreetLights();
this->drawGhosts();
this->drawPellets();
this->drawWalls();
}
void Map::newLevel()
{
if(this->level==1)
{
this->level++;
this->initLevel2();
}
else
{
cout<<"************GAME COMPLETE!**************"<<endl;
cout<<"************FINAL SCORE: "<<this->score<<"**************"<<endl;
}
}
void Map::streetLightSwitch(GLint lightIndex)
{
this->streetLights[lightIndex]->lightSwitch();
}
void Map::masterStreetLightSwitch()
{
this->masterStreetLightState = !this->masterStreetLightState;
for(GLint i=0; i<4; i++)
{
this->streetLights[i]-> lightSwitchTo(this->masterStreetLightState);
}
}
void Map::drawStreetLights()
{
for(GLint i=0; i<4; i++)
{
this->streetLights[i]->draw();
}
}
void Map::drawGhosts()
{
for(GLint i=0; i<this->ghostUnits; i++)
{
if(this->ghosts[i]->isAlive()==1)
{
this->ghosts[i]->camera.SetOrigin(this->ghosts[i]->getX(), this->ghosts[i]->getY()+2.0, this->ghosts[i]->getZ());
//Where can the ghost move:
char* canMove = this->whatDirectionsCanHeMove(this->ghosts[i]->getX(), this->ghosts[i]->getZ());
int moveSwitch = rand() % 3;
if(moveSwitch==0)//if he can move choosen direction, then move there!
this->ghosts[i]->walkForward(canMove);
else if(moveSwitch==1)
this->ghosts[i]->turnLeft();
else if (moveSwitch==2)
this->ghosts[i]->turnRight();
this->ghosts[i]->draw();
}
}
}
void Map::drawPellets()
{
for(GLint i=0; i<this->xUnits; i++)
{
for(GLint j=0; j<this->zUnits; j++)
{
this->pellets[i][j]->draw();
}
}
}
void Map::drawTiles()
{
for(GLint i=0; i<this->xUnits; i++)
{
for(GLint j=0; j<this->zUnits; j++)
{
this->tiles[i][j]->draw();
}
}
}
void Map::drawAxis()
{
glPushMatrix();
// Draw model axes.
glBegin(GL_LINES);
// X axis
glColor3f(1, 0, 0); //Red
glVertex3f(0, 0, 0);
glVertex3f(10, 0, 0);
// Y axis
glColor3f(0, 1, 0); //Blue
glVertex3f(0, 0, 0);
glVertex3f(0, 10, 0);
// Z axis
glColor3f(0, 0, 1); //Gree
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 10);
glEnd();
glPopMatrix();
}
void Map::initGhosts(GLfloat ghostData[][2])
{
for(GLint i=0; i<this->ghostUnits; i++)
{
this->ghosts[i] = new Ghost(ghostData[i][0], 0.1f, ghostData[i][1]);
}
}
void Map::initTiles()
{
GLfloat moveX = 0.0f;
for(GLint i=0; i<this->xUnits; i++)
{
GLfloat moveZ = 0.0f;
for(GLint j=0; j<this->zUnits; j++)
{
this->tiles[i][j]= new Tile(moveX,0.0f,moveZ);
moveZ = moveZ + 2.0f;
}
moveX = moveX + 2.0f;
}
}
void Map::initPellets()
{
GLint numPowerPellets = 6;
GLfloat moveX = 1.0f;
for(GLint i=0; i<this->xUnits; i++)
{
GLfloat moveZ = 1.0f;
for(GLint j=0; j<this->zUnits; j++)
{
this->pellets[i][j]= new Pellet(moveX,0.5f,moveZ, (i+j)%5+1, (i+j)%10+1);
moveZ = moveZ + 2.0f;
}
moveX = moveX + 2.0f;
}
}
void Map::switchPelletsTexture()
{
//Get/set the current pellet texture state.
//this->pelletsTextureState = 0; No Texture
//this->pelletsTextureState = 1; Texture all pellets
//this->pelletsTextureState = 2; Texture only power pellets
this->pelletsTextureState = ++this->pelletsTextureState%3;
for(GLint i=0; i<this->xUnits; i++)
{
for(GLint j=0; j<this->zUnits; j++)
{
if(this->pelletsTextureState == 0)
{
//Turn off texture for all pellets
this->pellets[i][j]->setTextureSwitch(0);
}
else if(this->pelletsTextureState == 1)
{
//Turn texture on for power, off for normal.
if(this->pellets[i][j]->getType()==1)
this->pellets[i][j]->setTextureSwitch(1);
else
this->pellets[i][j]->setTextureSwitch(0);
}
else
{
//Turn on texture for all pellets
this->pellets[i][j]->setTextureSwitch(1);
}
}
}
}
void Map::switchAllTexture()
{
if(this->allTextureState)
{
//Turn off all textures
glDisable( GL_TEXTURE_2D );
this->allTextureState = false;
this->pelletsTextureState = 0;
for(GLint i=0; i<this->xUnits; i++)
{
for(GLint j=0; j<this->zUnits; j++)
{
//Turn off texture for all pellets
this->pellets[i][j]->setTextureSwitch(1);
}
}
}
else
{
glEnable( GL_TEXTURE_2D );
this->allTextureState = true;
this->pelletsTextureState = 2;
for(GLint i=0; i<this->xUnits; i++)
{
for(GLint j=0; j<this->zUnits; j++)
{
//Turn off texture for all pellets
this->pellets[i][j]->setTextureSwitch(0);
}
}
}
}
char* Map::tr(char x) // Transforms Every char into a binary number.
{
// Every '1' corresponds to a wall, considering
// every char is a labyrinth's unit square.
// 1000 = east wall
// 0100 = north
// 0010 = west
// 0001 = south
char* res;
switch(x)
{
case 'a':
res = "0001";
break;
case 'b':
res = "0010";
break;
case 'c':
res = "0011";
break;
case 'd':
res = "0100";
break;
case 'e':
res = "0101";
break;
case 'f':
res = "0110";
break;
case 'g':
res = "0111";
break;
case 'h':
res = "1000";
break;
case 'i':
res = "1001";
break;
case 'j':
res = "1010";
break;
case 'k':
res = "1011";
break;
case 'l':
res = "1100";
break;
case 'm':
res = "1101";
break;
case 'n':
res = "1110";
break;
case 'o':
res = "1111";
break;
default:
res = "0000";
break;
}
return res;
}
void Map::readstr(FILE *f,char *string) // Read char from the file
{
do
{
fgets(string, 28, f);
}
while ((string[0] == '/') || (string[0] == '\n'));
return;
}
char* Map::whereIsHe()
{
float x = this->pacman->getX();
float z = this->pacman->getZ();
return this->walls[(int)x/2][(int)z/2]->getType();
}
void Map::gotPellet()
{
float x = this->pacman->getX();
float z = this->pacman->getZ();
bool onTileX = (int)ceil(x) % 2;
bool onTileZ = (int)ceil(z) % 2;
if(onTileX&&onTileZ)
{
GLint hasBeenEaten = this->pellets[(int)x/2][(int)z/2]->getHasBeenEaten();
if(hasBeenEaten==0)
{
GLint nutritionalValue = this->pellets[(int)x/2][(int)z/2]->eat();
if(this->pacman->hasPelletPower()==1) //Only score in power pellet mode.
{
this->score++;
cout<<"Yum Yum. I Love pellets. Nutritional value = "<<nutritionalValue<<"Score: "<<this->score<<endl;
}
if(nutritionalValue==1)
{
cout<<"Power pellet time!"<<endl;
this->pacman->takePowerPellet();
}
}
}
return;
}
void Map::pacmanGhostCollisionDetection()
{
GLfloat pacmanX = this->pacman->getX();
GLfloat pacmanZ = this->pacman->getZ();
//Check if pacman is at the same place as ghosts are using brute force.
for(GLint i=0; i<this->ghostUnits; i++)
{
GLfloat ghostX = this->ghosts[i]->getX();
GLfloat ghostZ = this->ghosts[i]->getZ();
// cout<<"ghost: "<<i<<" x: "<<ghostX<<" z: "<<ghostZ<<endl;
if(pacmanX==ghostX && pacmanZ==ghostZ && this->ghosts[i]->isAlive()==1)
{
if(this->pacman->hasPelletPower()==1)
{
cout<<"Destroy the ghost! Score: "<<this->score<<endl;
this->ghosts[i]->takeHit();
}
else
{
GLint health = this->pacman->takeHit();
cout<<"hit by ghost! Health: "<<health<<endl;
}
}
}
return;
}
char* Map::whatDirectionsCanHeMove(float x, float z)
{
//Start with the assumption that the sprite can move in all directions
//and then take away directions.
//Represented with a 4 bit strig representing 4 direction, ENWS
char* canMove = new char[4];
canMove[0] ='1';
canMove[1] ='1';
canMove[2] ='1';
canMove[3] ='1';
int wallIndexX = (int)x/2;
int wallIndexZ = (int)z/2;
//ON THE SEAM, BETWEEN 2 TILES
//check even and odd. x and z.
bool onTileX = (int)ceil(x) % 2;
bool onTileZ = (int)ceil(z) % 2;
if(onTileZ==0) //Can only move N S
{
canMove[0] = '0';
canMove[2] = '0';
}
else
{
//Can he move north.
if(wallIndexZ<=0) //on northern edge, cannot go further north
{
canMove[1] = '0';
}
else
{
//Check current tile's N and the northern tile's S
char* northTileWallCode = this->walls[wallIndexX][wallIndexZ-1]->getType();
char* currentTileWallCode = this->walls[wallIndexX][wallIndexZ]->getType();
if(currentTileWallCode[1]=='1' || northTileWallCode[3]=='1')
canMove[1]= '0';
}
//Can he move south.
if(wallIndexZ>=this->zUnits-1)//is on southern edge, cannot go further south
{
canMove[3]= '0';
}
else
{
//Check current tile's S and the southern tile's N
char* southTileWallCode = this->walls[wallIndexX][wallIndexZ+1]->getType();
char* currentTileWallCode = this->walls[wallIndexX][wallIndexZ]->getType();
if(currentTileWallCode[3]=='1' || southTileWallCode[1]=='1')
canMove[3]= '0';
}
}
// even on x plane means you can only move on the x plane , so no N S
if(onTileX==0)
{
canMove[1] = '0';
canMove[3] = '0';
}
else
{
//Can he move east.
if(wallIndexX>=this->xUnits-1) //is on eastern edge, cannot go further east
{
canMove[0]= '0';
}
else
{
//Check current tile's E and the eastern tile's W
char* eastTileWallCode = this->walls[wallIndexX+1][wallIndexZ]->getType();
char* currentTileWallCode = this->walls[wallIndexX][wallIndexZ]->getType();
if(currentTileWallCode[0]=='1' || eastTileWallCode[2]=='1')
canMove[0]= '0';
}
//Can he move west.
if(wallIndexX<=0) //is on western edge, cannot go further west
{
canMove[2]= '0';
}
else
{
//Check current tile's W and the western tile's E
char* westTileWallCode = this->walls[wallIndexX-1][wallIndexZ]->getType();
char* currentTileWallCode = this->walls[wallIndexX][wallIndexZ]->getType();
if(currentTileWallCode[2]=='1' || westTileWallCode[0]=='1')
canMove[2]= '0';
}
}
return canMove;
}
void Map::createMaze()
{
string s;
ifstream infile;
if(this->level == 1)
infile.open("data/Maze2.txt");
if(this->level == 2)
infile.open("data/Maze3.txt");
int i = 0;
if (infile.is_open())
{
while(getline(infile,s) && i<28)
{
for (int j = 0; j < 28; j++)
{
char* wallCode = tr(s[j]);
this->walls[i][j] = new Wall(i*2.0f,0.5f,j*2.0f, wallCode);
}
i++;
}
infile.close();
}
else
{
printf( "Error: can't open maze input file " );
}
}
void Map::drawWalls()
{
glPushMatrix();
for (int i = 0; i < 28; i++) // Draw Walls For Every Square Unit, every tile.
{
for (int j = 0; j < 28; j++)
{
this->walls[i][j]->draw();
}
}
glPopMatrix();
}