-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticle.cpp
More file actions
231 lines (190 loc) · 4.24 KB
/
Copy pathparticle.cpp
File metadata and controls
231 lines (190 loc) · 4.24 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
class Particle{
public:
Particle(float r, float g, float b){
this->r = r;
this->g = g;
this->b = b;
this->state = QUIET;
this->new_direction = -1;
this->current_direction = -1;
this->canGoOut = false;
this->lastInBox = true;
this->pacmanDegreeVisivility = 45;
}
~Particle(){}
void SetPosition(int x, int y)
{
this->x = x;
this->y = y;
}
void SetDegreeVisivility(int x)
{
this->pacmanDegreeVisivility = x;
}
int GetX()
{
return x;
}
int GetY()
{
return y;
}
void SetNewDirection(int direction)
{
this->new_direction = direction;
}
int GetNewDirection()
{
return this->new_direction;
}
void SetCurrentDirection(int direction)
{
this->current_direction = direction;
}
int GetCurrentDirection()
{
return this->current_direction;
}
void InitMovement(int destinationX, int destinationY, int duration)
{
vx = (destinationX - x)/duration;
vy = (destinationY - y)/duration;
state = MOVE;
time_remaining = duration;
}
int GetState()
{
return state;
}
bool CanGoOut(){
return canGoOut;
}
void GoOut(){
canGoOut = true;
}
void Out(){
canGoOut = false;
lastInBox = true;
}
bool LastInBox(){
return this->lastInBox;
}
void OutBox(){
this->lastInBox = false;
}
void Integrate(long t)
{
if(state == MOVE && t < time_remaining)
{
x = x + vx*t;
y = y + vy*t;
time_remaining -= t;
}
else if(state == MOVE && t >= time_remaining)
{
x = round(x + vx*time_remaining);
y = round(y + vy*time_remaining);
state = QUIET;
}
}
void drawLight(int particle){
GLenum lights;
float colorValue = 0.8;
int degree = 20;
if(particle == 0){
colorValue = 0.8;
degree = this->pacmanDegreeVisivility;
}
GLint position[4];
GLfloat color[4];
GLfloat dir[3];
switch(current_direction){
case UP:
dir[0]=0;dir[1]=0;dir[2]=-1;
break;
case DOWN:
dir[0]=0;dir[1]=0;dir[2]=1;
break;
case RIGHT:
dir[0]=1;dir[1]=0;dir[2]=0;
break;
case LEFT:
dir[0]=-1;dir[1]=0;dir[2]=0;
break;
}
switch (particle){
case 0:
lights = GL_LIGHT2;
break;
case 1:
lights = GL_LIGHT3;
break;
case 2:
lights = GL_LIGHT4;
break;
case 3:
lights = GL_LIGHT5;
break;
}
glLightfv(lights,GL_SPOT_DIRECTION,dir);
glLightf(lights,GL_SPOT_CUTOFF,degree);
glLightf(lights,GL_SPOT_EXPONENT,10);
position[0]=(x+0.5)*cellWidth; position[1]=Y+radiParticle; position[2]=(y+0.5)*cellHeight; position[3]=1;
glLightiv(lights,GL_POSITION,position);
color[0]=colorValue; color[1]=colorValue; color[2]=colorValue; color[3]=1;
glLightfv(lights,GL_DIFFUSE,color);
glLightf(lights,GL_CONSTANT_ATTENUATION,1.0);
glLightf(lights,GL_LINEAR_ATTENUATION,0.0);
glLightf(lights,GL_QUADRATIC_ATTENUATION,0.0);
glEnable(lights);
}
void draw()
{
GLfloat material[4];
//glColor3f(r,g,b);
material[0]=r; material[1]=g; material[2]=b; material[3]=1.0;
glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,material);
GLUquadricObj *quadric;
quadric = gluNewQuadric();
gluQuadricDrawStyle(quadric, GLU_FILL);
glPushMatrix();
glTranslatef((x+0.5)*cellWidth,Y+radiParticle,(y+0.5)*cellHeight);
gluSphere(quadric,radiParticle,36,18);
glPopMatrix();
gluDeleteQuadric(quadric);
/*glBegin(GL_QUADS);
glVertex2i((x+GHOST)*cellWidth, (y+GHOST)*cellHeight);
glVertex2i((x+1-GHOST)*cellWidth, (y+GHOST)*cellHeight);
glVertex2i((x+1-GHOST)*cellWidth, (y+1-GHOST)*cellHeight);
glVertex2i((x+GHOST)*cellWidth, (y+1-GHOST)*cellHeight);
glEnd();*/
}
int getNextX(){
if(state == QUIET) return x;
switch(current_direction){
case RIGHT:
return x + 1;
case LEFT:
return x - 1;
}
return x;
}
int getNextY(){
if(state == QUIET) return y;
switch(current_direction){
case UP:
return y + 1;
case DOWN:
return y - 1;
}
return y;
}
private:
float x, y;
float vx, vy;
int state, pacmanDegreeVisivility;
long time_remaining;
float r, g, b; //RGB color
int new_direction, current_direction;
bool canGoOut, lastInBox;
};