-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.hpp
More file actions
308 lines (263 loc) · 11 KB
/
Copy pathEnemy.hpp
File metadata and controls
308 lines (263 loc) · 11 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
//
// Created by diego on 13/3/21.
//
#include "Character.hpp"
#include <random>
#include <chrono>
#include <list>
#ifndef ALLEGRO5TUTORIAL_ENEMY_HPP
#define ALLEGRO5TUTORIAL_ENEMY_HPP
#define MIN 60
#define MAX 120
enum Estado {
CIELO, INGAME
};
class Enemy : public Character {
bool changingGroundPower = false;
bool helpingPower = false;
int randMoveTimer = 0;
int randMovePeriod;
Estado estado = CIELO;
bool colisionado = false;
protected:
int IA; //0 CLASICO, 1 MEDIO, 2 DIFICIL
public:
bool isCoily = false;
//de todos los sprites, el primer sprite, o el sprite a la izquierda de la pareja, tiene ancho leftSprite
//usado porque Coily tiene su primera pareja de tamaño 14, el resto es de tamaño 16
int leftSprite = 16;
/* Constructor */
Enemy(Piramide piramide, std::string nom, int i, int j, int xRespectCube, int yRespectCube, int IA=0) :
Character(piramide, nom, i, j, DOWNRIGHT, xRespectCube, yRespectCube) {
Enemy::setSourceX(16);
this->IA = IA;
std::mt19937 mt(std::chrono::system_clock::now().time_since_epoch().count());
//
std::uniform_int_distribution<int> dist(MIN, MAX);
Enemy::randMovePeriod;
switch (piramide.getLevel()) {
case 6:
randMovePeriod = 23;
break;
case 7:
randMovePeriod = 20;
break;
case 8:
randMovePeriod = 17;
break;
case 9:
randMovePeriod = 15;
break;
default:
randMovePeriod = 25;
break;
}
if(nom != "Ugg" && nom != "WrongWay"){
Character::y = piramide.map[i][j].y -32-10-32;
}
if (nom == "GreenBlob") {
setHelpingPower(true);
}
if(nom == "Slick" || nom == "Sam"){
ALLEGRO_SAMPLE *musica1 = al_load_sample(("../sounds/" + nom + "1.ogg").c_str());
ALLEGRO_SAMPLE *musica2 = al_load_sample(("../sounds/" + nom + "2.ogg").c_str());
playOnce(musica1);
playOnce(musica2);
}
}
virtual void randomMovement(int iQBert, int jQBert) {
bool aleatorio;
std::random_device rd;
std::mt19937 mt(std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> dist(1, 2);
if(IA == 0) aleatorio = true;
else if(IA == 1){ // 50% aleatorio 50% IA --> MEDIO
if(dist(mt) == 1) aleatorio = true;
else aleatorio = false;
}
else if(IA == 2) {
aleatorio = false;
}
Direction d;
if(aleatorio){
if (dist(mt) == 1) d = DOWNRIGHT;
else d = DOWNLEFT;
}
else {
if(getJ()>=jQBert){
d = DOWNLEFT;
}
else {
d = DOWNRIGHT;
}
}
setDir(d);
assignIJ();
Enemy::setJumping(true);
Enemy::setSourceX(getSourceX() - 16);
}
int distanciaQbert(int qberti, int qbertj){
int distanciaQbert = abs(getJ() - qbertj);
if(getI() == qberti){
//está en la misma fila, siempre habrá el mismo movimiento hacia vertical que lateral
distanciaQbert *= 2;
}else if(distanciaQbert == 0){
//solo hay diferencia vertical
distanciaQbert = abs(getI() - qberti);
}else{//estamos difente tanto en fila como en columna
if((getJ() > qbertj && getI() < qberti) || (getJ() < qbertj && getI() > qberti)) {
//si vamos hacia abajo a la izquierda o arriba a la derecha, necesitamos un movimiento más hacia abajo o arriba
distanciaQbert += distanciaQbert + 1;
}else if((getJ() < qbertj && getI() < qberti) || (getJ() > qbertj && getI() > qberti)){
//si vamos hacia arriba a la izquierda o abajo a la derecha, necesitamos un movimiento menos hacia abajo o arriba
distanciaQbert += distanciaQbert - 1;
}
}
return distanciaQbert;
}
int distanciaQbert(int qberti, int qbertj, int i, int j){
int distanciaAQbert = abs(j - qbertj);
if(i == qberti){
//está en la misma fila, siempre habrá el mismo movimiento hacia vertical que lateral
distanciaAQbert *= 2;
}else if(distanciaAQbert == 0){
//solo hay diferencia vertical
distanciaAQbert = abs(i - qberti);
}else{//estamos difente tanto en fila como en columna
if((j > qbertj && i < qberti) || (j < qbertj && i > qberti)) {
//si vamos hacia abajo a la izquierda o arriba a la derecha, necesitamos un movimiento más hacia abajo o arriba
distanciaAQbert += distanciaAQbert + 1;
}else if((j < qbertj && i < qberti) || (j > qbertj && i > qberti)){
//si vamos hacia arriba a la izquierda o abajo a la derecha, necesitamos un movimiento menos hacia abajo o arriba
distanciaAQbert += distanciaAQbert - 1;
}
}
return distanciaAQbert;
}
virtual void assignIJ() {
if (getDir() == TOPLEFT) setI(getI() - 1), setJ(getJ() - 1);
else if (getDir() == DOWNRIGHT) setI(getI() + 1), setJ(getJ() + 1);
else if (getDir() == TOPRIGHT) setI(getI() - 1);
else if (getDir() == DOWNLEFT) setI(getI() + 1);
}
virtual void movement(Piramide *piramide, int WIDTH, int HEIGHT, int &puntuacion, Character *qbert, std::_List_iterator<Enemy *> it,
std::list<Enemy*> & borrarEnemies) {
if (estado == INGAME) { // Esta en el campo de juego
if (isJumping()) {
timerplusplus();
if (!isFalling()) {
if (getTimer() < airTime / 2) {
//GO UP AND DIRECTION
if (getDir() == TOPRIGHT || getDir() == DOWNRIGHT)
setX(movementX + getX());
else if(getDir() == TOPLEFT || getDir() == DOWNLEFT)
setX(getX() - movementX);
if (getDir() == TOPRIGHT || getDir() == TOPLEFT)
setY(getY() - movementY);
} else if (getTimer() > airTime / 2 && getTimer() < airTime) {
//GO DOWN AND DIRECTION
if (getDir() == TOPRIGHT || getDir() == DOWNRIGHT)
setX(movementX + getX());
else if(getDir() == TOPLEFT || getDir() == DOWNLEFT)
setX(getX() - movementX);
if (getDir() == DOWNRIGHT || getDir() == DOWNLEFT)
setY(getY() + movementY);
} else if (getTimer() > airTime) {
if ((getJ() < 0 || getJ() > getI() || getI() > 6) ||
(nom == "Ugg" && (getJ() <= 0 || getJ() > getI() || getI() > 6)) ||
(nom == "WrongWay" && (getJ() < 0 || getJ() >= getI() || getI() > 6)) ){
setFalling(true);
if(nom == "coilyBola") {
puntuacion+=500;
ALLEGRO_SAMPLE *coilyCae = al_load_sample("../sounds/snake-fall.ogg");
playOnce(coilyCae);
}
if (getSourceX() != 0) {
setSourceX(getSourceX() + 16);
}
} else {
//WE LANDED
playOnce(getJumpSound());
if (hasChangingGroundPower()) {
int none = 0;
piramide->changeCubeInverse(getI(), getJ());
}
setX(piramide->map[getI()][getJ()].x + getXRespectCube());
setY(piramide->map[getI()][getJ()].y + getYRespectCube());
setTimer(0);
setJumping(false);
setSourceX(getSourceX() + leftSprite);
if (getI() == qbert->getI() && getJ() == qbert->getJ()) {
colisionado = true;
}
}
}
} else { //se cae de la piramide
if(nom == "Ugg" && getX() >= WIDTH/2-200){
setX(getX() - movementX);
}
else if(nom == "WrongWay" && getX() <= WIDTH/2+200){
setX(getX() + movementX);
}
else if(nom != "WrongWay" && nom != "Ugg" && getY() <= HEIGHT){
setY(getY() + movementY);
}
else{//si hemos llegado al suelo
borrarEnemies.push_back(it.operator*());//lo borramos
}
}
}
} else { // Esta bajando del cielo, CIELO
timerplusplus();
setX(piramide->map[getI()][getJ()].x + getXRespectCube());
if (getY() < piramide->map[getI()][getJ()].y + getYRespectCube()) {
setY(getY() + movementY);
} else {
estado = INGAME;
if(nom == "Slick" || nom == "Sam"){
piramide->changeCubeInverse(getI(),getJ());
}
if (getI() == qbert->getI() && getJ() == qbert->getJ()) {
colisionado = true;
}
playOnce(getJumpSound());
setX(piramide->map[getI()][getJ()].x + getXRespectCube());
setY(piramide->map[getI()][getJ()].y + getYRespectCube());
setTimer(0);
}
}
}
/*************************
* GETTER'S AND SETTER'S *
*************************/
bool haColisionado() {
return colisionado;
}
void setHaColisionado(bool col) {
colisionado = col;
}
void setChangingGroundPower(bool changingGroundPower) {
Enemy::changingGroundPower = changingGroundPower;
}
bool hasHelpingPower() const {
return helpingPower;
}
void setHelpingPower(bool helpingPower) {
Enemy::helpingPower = helpingPower;
}
int getRandMoveTimer() const { return randMoveTimer; }
void randomMoveTimerplusplus() { Enemy::randMoveTimer++; }
void resetRandomMoveTimer() { Enemy::randMoveTimer = 0; }
int getRandMovePeriod() const { return randMovePeriod; }
bool hasChangingGroundPower() const { return changingGroundPower; }
Estado getEstado() const {
return estado;
}
void setEstado(Estado estado) {
Enemy::estado = estado;
}
bool estaCielo() {
return estado == CIELO;
}
};
#endif //ALLEGRO5TUTORIAL_ENEMY_HPP