-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
472 lines (397 loc) · 11.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
#define _USE_MATH_DEFINES
#include <cmath>
#include <utility>
#include <vector>
#include <windows.h>
#include <GL/gl.h>
#include "glut.h"
#include "creature.h"
#include "pac.h"
#include "ghost.h"
#include "gameboard.h"
#include "scrnsave.h"
#include <iostream>
#include <string>
using namespace std;
static const int ghosts_count = 4;
//Rules and resources for Pacman
//http://gameinternals.com/understanding-pac-man-ghost-behavior
// board game:
GameBoard *board;
// mr. pacman
Pac *pacman;
bool pacFollowed = false;
// ghosts
Ghost *ghosts[ghosts_count];
float ghostsColors[] = { 1.0, 0.0, 0.0, // Blinky
1.0, 0.753, 0.796, // Pinky
0.0, 1.0, 1.0, // Inky
1.0, 0.647, 0.0 // Clyde
};
// basic game information
std::string infoText[] =
{
"Coins left: ",
"Lives: "
};
// zooming - max Z distance and min Z distance
float maxZ = 15;
float minZ = 8;
// Camera up/down maximums:
float camUpDownMax = 0.8;
// Camera left/right maximums:
float camLRMax = 0.8;
// Camera rotation angles:
double theta = 0;
double phi = 0;
// Initial camera looking target
GLdouble centerX = GameBoard::CENTER_X;
GLdouble centerY = GameBoard::CENTER_Y;
GLdouble centerZ = GameBoard::CENTER_Z;
GLdouble centerDistance = 15;
// Camera (i.e. observer) coordinates
GLdouble eyeX = 0;
GLdouble eyeY = 0;
GLdouble eyeZ = 0;
// Lighting:
float lightIntensity = 1;
void init()
{
// Resources for lighting & texture in openGL
// https://www.youtube.com/watch?v=g_0yV7jZvGg
// https://www.youtube.com/watch?v=gFZqzVQrw84
// https://www.youtube.com/watch?v=oVwH8KV1xnY
// General OpenGL configirations:
glEnable(GL_DEPTH_TEST); // intialization of 3D rendering
glEnable(GL_COLOR_MATERIAL); // object material properties enabled
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); // set material properties which will be assigned by glColor
glEnable(GL_NORMALIZE); // Automatically normalize normals (vectors of surfaces )
glEnable(GL_LIGHTING); // general lighting enabled
glEnable(GL_LIGHT0);
// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { GameBoard::CENTER_X, GameBoard::CENTER_Y, 4.0f, 1.0f };
// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
//glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0);
//glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.6);
//glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);
// Add global ambient light
//GLfloat ambientColor[] = { 0.5, 0.5, 0.5, 1.0 };
//glLightModelfv( GL_LIGHT_MODEL_AMBIENT, ambientColor); // ambient lights everywhere with the same amount
GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
//glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient );
//glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
//glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 10.0 );
glShadeModel(GL_SMOOTH); // smooth shading
glDepthFunc( GL_LESS );
}
void DrawInfo()
{
// buffer for storing coinsCount
static const int buf_length = 4;
static char count_buffer[buf_length];
std::sprintf(count_buffer,"%d",pacman->lives);
std::sprintf(count_buffer + 1,"%d",board->coinsCount);
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,500,0,500);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3d(1.0, 1.0, 1.0);
//std::cout << static_cast <char>(board->coinsCount) << std::endl;
// text display functionality
for (int i = 0; i < sizeof(infoText) / sizeof(infoText[0]); i++)
{
glRasterPos2i(10, 500 - 20 - i * 18);
for (std::string::iterator j = infoText[i].begin(); j != infoText[i].end(); ++j)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,*j);
}
if (i == 0)
{ // display coinsCount
for (int j = 1; j < buf_length; j++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, count_buffer[j]);
}
}
if (i == 1)
{ // display lives
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, count_buffer[0]);
}
}
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
//added this
glEnable(GL_TEXTURE_2D);
}
void display()
{
float orthoOffset = 2.0f;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 90, 1, 5, 30.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// lighting stuff:
glPushMatrix();
GLfloat lightPos0[] = { pacman->x, pacman->y, 4.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
glPopMatrix();
// camera movement stuff:
// http://gamedev.stackexchange.com/questions/43588/how-to-rotate-camera-centered-around-the-cameras-position
if (pacFollowed)
{ // look at pacman
centerX = pacman->x;
centerY = pacman->y;
centerZ = pacman->z;
}
else
{ // look at the middle of the gameboard
centerX = GameBoard::CENTER_X;
centerY = GameBoard::CENTER_Y;
centerZ = GameBoard::CENTER_Z;
}
eyeX = centerX + 5 * sin(theta);
eyeY = centerY + centerDistance * sin(phi) + 5 * sin(theta);
eyeZ = centerZ + centerDistance * cos(phi);
gluLookAt( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, sin(theta), cos(theta), 0 );
// move the pacman
std::pair<int,int> pacmanCord = pacman->Move();
// wall check stops movement
pacman->WallCheck();
// coins eating
if (pacman->Consume())
board->coinsCount--;
// ghosts movement algorithms:
// Blinky:
if (ghosts[0]->chase)
{
// Blinky targets packman current tile coordinates while in chase mode
ghosts[0]->targetTileX = pacman->tileX;
ghosts[0]->targetTileY = pacman->tileY;
}
std::pair<int, int> ghostCord0 = ghosts[0]->Move();
// Pinky:
if (ghosts[1]->chase)
{
// Pinky targets packman current tile + 4 ahead coordinates while in chase mode
ghosts[1]->targetTileX = pacman->getNextTileX(4);
ghosts[1]->targetTileY = pacman->getNextTileY(4);
}
std::pair<int, int> ghostCord1 = ghosts[1]->Move();
// Inky:
if (ghosts[2]->chase)
{
// Uses pacman +2 next coordinates and blinky's position
ghosts[2]->targetTileX = pacman->getNextTileX(2) + (pacman->getNextTileX(2) - ghosts[0]->tileX);
ghosts[2]->targetTileY = pacman->getNextTileY(2) + (pacman->getNextTileY(2) - ghosts[0]->tileY);
}
std::pair<int, int> ghostCord2 = ghosts[2]->Move();
// Clyde:
if (ghosts[3]->chase)
{
// If distance from pac > 8 then target him. Else go where Clyde's scatter points is.
int distSquared = abs(ghosts[3]->tileX - pacman->tileX) * abs(ghosts[3]->tileX - pacman->tileX)
+ abs(ghosts[3]->tileY - pacman->tileY) * abs(ghosts[3]->tileY - pacman->tileY);
//std::cout << "Clyde dist to pacman: " << distSquared << std::endl;
if (distSquared >= 64)
{
ghosts[3]->targetTileX = pacman->tileX;
ghosts[3]->targetTileY = pacman->tileY;
}
else
{
ghosts[3]->targetTileX = ghosts[3]->scatterTileX;
ghosts[3]->targetTileY = ghosts[3]->scatterTileY;
}
}
std::pair<int, int> ghostCord3 = ghosts[3]->Move();
vector<pair<int, int>> cords = { ghostCord0, ghostCord1 , ghostCord2, ghostCord3};
for (int i = 0;i < 4;i++) {
if (pacmanCord.first == cords[i].first && pacmanCord.second == cords[i].second) {
cout << "Collision with Ghost at ("<< pacmanCord.first <<","<< pacmanCord.second <<")\n";
pacman->lives -= 1;
cout << "Lives left: " << pacman->lives << "\n";
pacman->tileX = 15;
pacman->tileY = 2;
pacman->Reassign(15,2);
for (int j = 0;j < 4;j++) {
ghosts[j]->Reassign(13 + j, 10);
ghosts[j]->tileX = 13 + j;
ghosts[j]->tileY = 10;
}
if (pacman->lives == 0) {
cout << "\nGame Over\n";
exit(0);
}
break;
}
}
if (board->coinsCount == 0) {
cout << "Winner!!\n";
exit(0);
}
// actual pacman, ghosts and board drawing
board->Draw();
pacman->Draw();
ghosts[0]->Draw(phi); // Blinky
ghosts[1]->Draw(phi); // Pinky
ghosts[2]->Draw(phi); // Inky
ghosts[3]->Draw(phi); // Clyde
// screen information
DrawInfo();
glFlush();
glutSwapBuffers();
}
void reshape(GLsizei w, GLsizei h)
{
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
GLdouble aspect = w /( GLdouble ) h;
gluPerspective( 90, 1, 5, 30.0 );
}
void keyboard(unsigned char key, int x, int y)
{
static float step = 0.1;
static float stepZoom = 1;
// Camera / zooming
switch (key)
{
case 'a':
// left
if (theta < -camLRMax)
return;
theta -= step;
break;
case 'w':
// up
if (phi > camUpDownMax)
return;
phi += step;
break;
case 'd':
// right
if (theta > camLRMax)
return;
theta += step;
break;
case 's':
// down
if (phi < -camUpDownMax)
return;
phi -= step;
break;
case 'r':
// zoom in
if (centerDistance < minZ)
return;
centerDistance -= stepZoom;
//positionz += stepZoom;
break;
case 'f':
// zoom out
if (centerDistance > maxZ)
return;
centerDistance += stepZoom;
//positionz -= stepZoom;
break;
case 'c':
//follow camera mode
pacFollowed = pacFollowed ? false : true;
centerDistance = (pacFollowed)? (maxZ - 4) : maxZ;
if(!pacFollowed)
{
// reset angles when returning from following mode
phi = 0;
theta = 0;
}
break;
case 't':
break;
case 'g':
break;
}
}
// Mouse handling
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_RIGHT_BUTTON) {
exit(0);
}
}
// Special keys handling (arrows)
void special( int key, int x, int y )
{
switch( key )
{
case GLUT_KEY_LEFT:
pacman->Turn(180);
break;
case GLUT_KEY_RIGHT:
pacman->Turn(0);
break;
case GLUT_KEY_UP:
pacman->Turn(90);
break;
case GLUT_KEY_DOWN:
pacman->Turn(270);
break;
}
reshape( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
}
void timer(int v)
{
// maybe some visual effects?
}
int main(int argc, char** argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition( 100, 10 );
glutInitWindowSize( 1000, 1000 );
glutCreateWindow( "PackMan" );
//glutGameModeString( "800x600:16@60" );
//glutEnterGameMode();
glutSetCursor(GLUT_CURSOR_NONE);
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutIdleFunc(display);
glutTimerFunc(40, timer, 1);
glutSpecialFunc(special);
glutKeyboardFunc(keyboard);
// OpenGL objects initialization:
pacman = new Pac(15,2);
board = new GameBoard();
// Ghosts initial configuration:
for (int i = 0; i < ghosts_count; i++)
{
ghosts[i] = new Ghost(ghostsColors[3*i],ghostsColors[3*i + 1],ghostsColors[3*i + 2], 13 + i, 10, 2);
}
// Blinky:
ghosts[0]->scatterTileX = 0;
ghosts[0]->scatterTileY = GameBoard::DIM_Y + 2;
// Pinky:
ghosts[1]->scatterTileX = GameBoard::DIM_X;
ghosts[1]->scatterTileY = GameBoard::DIM_Y + 2;
// Inky:
ghosts[1]->scatterTileX = 0;
ghosts[1]->scatterTileY = -2;
// Clyde:
ghosts[3]->scatterTileX = 0;
ghosts[3]->scatterTileY = -2;
init();
glutMainLoop();
return 0;
}