forked from carboned4/CeGue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrog.h
270 lines (217 loc) · 6.22 KB
/
Frog.h
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
#ifndef FROG_H
#define FROG_H
#include <vector>
#include <Windows.h>
#include "glut.h"
#include "DynamicObject.h"
#include "Car.h"
#include "resource.h"
#define FROG_SPEED_MODULE 4.0
#define FROG_DIMENSION_YMIN -0.3
#define FROG_DIMENSION_YMAX 0.3
#define FROG_DIMENSION_XMIN -0.3
#define FROG_DIMENSION_XMAX 0.3
class Frog : public DynamicObject {
private:
int _lives;
Vector3 _platformSpeed;
double _zRotation;
/* Position/Collision Flags */
bool _ground;
bool _water;
bool _logOrTurtle;
int _score;
public:
Frog() : DynamicObject(Box(FROG_DIMENSION_XMIN, FROG_DIMENSION_XMAX, FROG_DIMENSION_YMIN, FROG_DIMENSION_YMAX)) {
_lives = 5;
_zRotation=0;
_platformSpeed = Vector3(0.0, 0.0, 0.0);
_ground = true;
_water = false;
_logOrTurtle = false;
_score = 0;
}
~Frog(){}
int getLives(){
return _lives;
}
void setLives(int lives){
_lives = lives;
}
void setZRotation(double z){ _zRotation = z;}
double getZRotation(){ return _zRotation; }
int getScore(){ return _score; }
void setScore(int score){ _score = score; }
void restartSpeed(){
return;
}
void moveDown(){
setZRotation(180.0);
Vector3 new_speed;
new_speed = _platformSpeed + Vector3(0.0, -FROG_SPEED_MODULE, 0.0);
setSpeed(new_speed);
}
void moveUp(){
setZRotation(0.0);
Vector3 new_speed;
new_speed = _platformSpeed + Vector3(0.0, FROG_SPEED_MODULE, 0.0);
setSpeed(new_speed);
}
void moveLeft(){
setZRotation(90.0);
Vector3 new_speed;
new_speed = _platformSpeed + Vector3(-FROG_SPEED_MODULE, 0.0, 0.0);
setSpeed(new_speed);
}
void moveRight(){
setZRotation(-90.0);
Vector3 new_speed;
new_speed = _platformSpeed + Vector3(FROG_SPEED_MODULE, 0.0, 0.0);
setSpeed(new_speed);
}
void stopMovement(){
setSpeed(_platformSpeed);
}
/* checkIfCollided() - Verifies if frog collided with game objects.*/
int checkIfColided(std::vector <GameObject *> collidable){
std::vector<GameObject* >::iterator iter = collidable.begin();
int colision_type = 0;
_ground = false;
_water = false;
_logOrTurtle = false;
int fate = 0; //Defines what kind of fate the frog had, so a score can be assigned.
for (iter; iter != collidable.end(); iter++){
if ((int) this == (int)*iter) continue; //Collision with itself.
colision_type = (*iter)->checkColisions(getPosition(),getBox());
if (colision_type == 1){ //Crashed with a car/bus.
_lives--;
die("Atropelado!");
_score -= 10; //Points to earn (negative = lose) when the frog is run over
fate = -1;
break;
}
else if (colision_type == 6){ //WIN
win();
_score += 50; //Points to earn when you win the game
fate = 1;
break;
}
else if (colision_type == 4) _ground = true; //It is in the ground.
else if (colision_type == 5){
_water = true; //Fall in water.
fate = -1;
}
else if (colision_type == 2){ //Its is in a turtle or log.
_logOrTurtle = true;
fate = 0;
break;
}
}
if (_ground){
_platformSpeed = Vector3(0.0, 0.0, 0.0);
return fate; //ground keeps the frog safe from the water - 1 is alive
}
else if (_water){
if (!_logOrTurtle){ //the frog will survive the water if there's a log or a turtle
_lives--;
die("Afogado!");
_score -= 10;
return fate; // -1 is dead
}
else{
_platformSpeed = (*iter)->getSpeed();
}
}
return fate;
}
void win(){
setPosition(0.0, 0.0, 0.0);
_zRotation = 0;
_platformSpeed = Vector3(0.0, 0.0, 0.0);
setSpeed(0.0, 0.0, 0.0);
}
void die(char* message){
//printf("%s\n",message);
if (_lives > 0){
setPosition(0.0, 0.0, 0.0);
_zRotation = 0;
}
_platformSpeed = Vector3(0.0, 0.0, 0.0);
setSpeed(0.0, 0.0, 0.0);
PlaySound((LPCWSTR)IDR_WAVE1, NULL, SND_RESOURCE | SND_ASYNC);
}
void update(int delta_t){
double _x, _y;
Vector3 distance = getSpeed();
if(distance.getX()>100.0 || distance.getX()<-100.0){ //arranja um problema que só existe nos PCs da RNL: a distance vem a infinito magicamente
distance = Vector3(0.0, 0.0, 0.0);
}
distance = distance * ((double)delta_t / 1000);
setPosition(getPosition() + distance);
_x = getPosition().getX();
_y = getPosition().getY();
if (_x > 5.5 - FROG_DIMENSION_XMAX){
setPosition(5.5 - FROG_DIMENSION_XMAX, getPosition().getY(), getPosition().getZ());
}
else if (_x < -5.5 - FROG_DIMENSION_XMIN){
setPosition(-5.5 - FROG_DIMENSION_XMIN, getPosition().getY(), getPosition().getZ());
}
if (_y > 12.5 - FROG_DIMENSION_YMAX){
setPosition(getPosition().getX(), 12.5 - FROG_DIMENSION_YMAX, getPosition().getZ());
}
else if (_y < -0.5 - FROG_DIMENSION_YMIN){
setPosition(getPosition().getX(), -0.5 - FROG_DIMENSION_YMIN, getPosition().getZ());
}
}
void draw(){
Vector3 vector = getPosition();
GLfloat mat_specular[] = { 0.1, 0.75, 0.1, 1.0 };
GLfloat mat_ambient[] = { 0.1, 0.5, 0.1, 1.0 };
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.1, 1.0 };
GLfloat shininess = 5;
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient);
glPushMatrix();
glTranslatef(vector.getX(), vector.getY(), vector.getZ()-0.25);
glRotatef(_zRotation, 0, 0, 1.0);
glScalef(0.5, 0.5, 0.5);
glPushMatrix(); //front leg
glColor3f(0.0, 1.0, 0.0);
glTranslatef(0.3, 0.2, -0.45);
//glutSolidCube(0.1);
glutSolidSphere(0.1, 8, 8);
glPopMatrix();
glPushMatrix();
glColor3f(0.0, 1.0, 0.0);
glTranslatef(-0.3, 0.2, -0.45);
//glutSolidCube(0.1);
glutSolidSphere(0.1, 8, 8);
glPopMatrix();
glPushMatrix();
glColor3f(0.0, 1.0, 0.0);
glTranslatef(0.3, -0.25, -0.4);
//glutSolidCube(0.3);
glutSolidSphere(0.2, 12, 12);
glPopMatrix();
glPushMatrix();
glColor3f(0.0, 1.0, 0.0);
glTranslatef(-0.3, -0.25, -0.4);
//glutSolidCube(0.3);
glutSolidSphere(0.2, 12, 12);
glPopMatrix();
glPushMatrix(); // body
glColor3f(0.5, 1.0, 0.5);
glTranslatef(0.0, -0.1, -0.1);
glutSolidSphere(0.3, 16, 16);
glPopMatrix();
glPushMatrix(); //head
glColor3f(0.0, 1.0, 0.0);
glTranslatef(0.0, 0.2, 0.2);
glutSolidSphere(0.2, 12, 12);
glPopMatrix();
glPopMatrix();
}
};
#endif