-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPacman.cpp
More file actions
299 lines (256 loc) · 7.4 KB
/
Pacman.cpp
File metadata and controls
299 lines (256 loc) · 7.4 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
#include <cstdlib>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <stdio.h>
#include "strstream"
#include <GL/glut.h>
using std::strstreambuf;
using std::istrstream;
using std::ostrstream;
using std::strstream;
using std::fstream;
using namespace std;
#include "glFrame.h"
#include "Pacman.h"
#include "Eight.h"
Pacman::Pacman( GLfloat xPos,GLfloat yPos,GLfloat zPos): Sprite()
{
this->x=xPos;
this->y=yPos;
this->z=zPos;
this->rotation=0.0;
this->loadTexture();
this->eight = new Eight(0.0f,0.3f,-2.0f);
this->worldsEdgeX = 28*2;
this->worldsEdgeZ = 28*2;
this->health = 100;
this->dead = 0;
this->pelletPower = 0;
}
Pacman::~Pacman()
{
//dtor
}
void Pacman::turnLeft()
{
this->rotation=this->rotation+90;
if(this->rotation==360)//scale back factor.
this->rotation=0;
}
void Pacman::turnRight()
{
//If the roation is zero then set the rotation to 360 and subtract.
//This keeps the rotation value [0,360] and is used to calculate direction of movement.
if(this->rotation==0)
this->rotation=360;
this->rotation=this->rotation-90;
}
void Pacman::walkForward(char* canMove)
{
if(this->rotation == 0 && canMove[3]=='1') // facing south, make sure there is no wall
{
this->z++;
this->camera.SetForwardVector(0.0,0.0,1.0);
}
else if(this->rotation == 90 && canMove[0]=='1') // facing east
{
this->x++;
this->camera.SetForwardVector(1.0,0.0,0.0);
}
else if(this->rotation == 180 && canMove[1]=='1') //facing north
{
this->z--;
this->camera.SetForwardVector(0.0,0.0,-1.0);
}
else if(this->rotation == 270 && canMove[2]=='1') //facing west
{
this->x--;
this->camera.SetForwardVector(-1.0,0.0,0.0);
}
else
{
// cout << "Ouch! Watch it!" << endl;
}
}
void Pacman::walkBackward(char* canMove)
{
if(this->rotation == 0 && canMove[1]=='1') // facing south //Check north.
{
this->z--;
}
else if(this->rotation == 90 && canMove[2]=='1') // facing east, check behid-> west
{
this->x--;
}
else if(this->rotation == 180 && canMove[3]=='1') //facing north
{
this->z++;
}
else if(this->rotation == 270 && canMove[0]=='1') //facing west, check behind ->east
{
this->x++;
}
else
{
// cout << "Ouch! Watch it!" << endl;
}
}
GLint Pacman::takeHit()
{
this->health = this->health-20;
cout<<"Pacman hit! OUCH! Health: "<<this->health<<endl;
if(this->health<=0)
{
cout<<"Pacman killed! GAME OVER!"<<endl;
this->dead = 1;
}
return this->health;
}
GLint Pacman::isAlive()
{
return !this->dead;
}
void Pacman::takePowerPellet()
{
this->pelletPower = 1;
this->pelletPowerTime = 100;
}
GLint Pacman::hasPelletPower()
{
return this->pelletPower;
}
void Pacman::decreasePelletPowerTime()
{
this->pelletPowerTime--;
if(this->pelletPowerTime==0)
{
cout<<"Power Pellet Time is up! Defend yourself."<<endl;
this->pelletPower=0;
}
}
int Pacman::loadTexture()
{
// Load Bitmaps And Convert To Texture
this->textureBank[0] = SOIL_load_OGL_texture
(
// "/home/angela/Documents/Comp371Project/data/pacman1.bmp",
"data/pacman1.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
this->textureBank[1] = SOIL_load_OGL_texture
(
//"/home/angela/Documents/Comp371Project/data/pacman2.bmp",
"data/pacman2.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
this->textureBank[2] = SOIL_load_OGL_texture
(
//"/home/angela/Documents/Comp371Project/data/pacman3.bmp",
"data/pacman3.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
if(this->textureBank[0] == 0 || this->textureBank[1] == 0 || this->textureBank[3]== 0 )
{
printf( "SOIL loading error: '%s'\n", SOIL_last_result());
return false;
}
//Set current texture
this->currentTextureIndex = 2;
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, this->textureBank[this->currentTextureIndex]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
return true; // Return Success
}
int Pacman::switchTexture()
{
this->currentTextureIndex = ++this->currentTextureIndex%3;
cout << "Changed Pacman's Texture" << this->currentTextureIndex << endl;
return true;
}
void Pacman::drawHalf(GLfloat r)
{
GLfloat off[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat yellow[] = { 1.0f, 1.0, 0.0, 1.0 };
GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat polished[] = { 100.0 };
glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow);
glMaterialfv(GL_FRONT, GL_AMBIENT, off);
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialfv(GL_FRONT, GL_SHININESS, polished);
GLdouble eqn[4] = {0.0, 1.0, 0.0, 0.0};
glClipPlane (GL_CLIP_PLANE0, eqn);
glEnable (GL_CLIP_PLANE0);
glRotatef(180.0f, 1.0f, 0.0f,0.0f);//Flip sphere so we do not see texture seem.
if(this->pelletPower==1)//Power pellet mode
glBindTexture(GL_TEXTURE_2D, this->textureBank[1]);
else
glBindTexture(GL_TEXTURE_2D, this->textureBank[this->currentTextureIndex]);
GLUquadricObj* p_poQuadric = gluNewQuadric();
gluQuadricDrawStyle(p_poQuadric, GLU_FILL);
gluQuadricNormals(p_poQuadric, GLU_SMOOTH);
gluQuadricTexture(p_poQuadric, GL_TRUE);
gluSphere(p_poQuadric,r,24,24);
gluDeleteQuadric(p_poQuadric);
glDisable (GL_CLIP_PLANE0);
}
void Pacman::draw()
{
GLfloat r = 0.8f;
const GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
const GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
const GLfloat polished[] = { 100.0 };
glPushMatrix();
move();
glRotatef(this->rotation, 0.0f, 1.0f, 0.0f);
glPushMatrix(); //Top
drawHalf(r);
glPopMatrix();
glPushMatrix(); //Bottom
glRotatef(200.0,1.0f,0.0f,0.0f);
drawHalf(r); //Bottom
glPopMatrix();
glPushMatrix(); //Eye balls.
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, black);
glMaterialfv(GL_FRONT, GL_SPECULAR, white);
glMaterialfv(GL_FRONT, GL_SHININESS, polished);
glTranslatef(-0.3f,0.6f,0.7f);
glutSolidSphere(0.15,24,24);
glTranslatef(0.6f,0.0f,0.0f);
glutSolidSphere(0.15,24,24);
glPopMatrix();
//Eight
glPushMatrix();
glRotatef(90.0f,1.0f,0.0f,0.0f);
glScalef(0.5f,0.5f,0.5f);
glTranslatef(-0.5f,0.3f,-2.0f);
eight->draw();
glPopMatrix();
glPopMatrix();
}
GLfloat Pacman::getX()
{
return this->x;
}
GLfloat Pacman::getY()
{
return this->y;
}
GLfloat Pacman::getZ()
{
return this->z;
}
GLfloat Pacman::getRotation()
{
return this->rotation;
}