From ce8a4a80509c1d42b215fb215aa3f4a8338abd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Mon, 5 Sep 2022 23:22:49 +0200 Subject: [PATCH] Ya sube cuestas pero no las baja --- media/tilesets/standard.png | Bin 2347 -> 2316 bytes source/director.cpp | 2 + source/game.cpp | 9 +++-- source/player.cpp | 73 ++++++++++++++++++++++++++++++------ source/player.h | 4 ++ source/room.cpp | 65 +++++++++++++++++++++++++------- source/room.h | 46 +++++++++++++++-------- 7 files changed, 156 insertions(+), 43 deletions(-) diff --git a/media/tilesets/standard.png b/media/tilesets/standard.png index 1560a4999ea6f6234789daf936d248dcf744b7d3..b354a2ccc3a90abae6978c298473ae6430cea794 100644 GIT binary patch literal 2316 zcmb7GdpHwp8#iMa$+--B9V2-e@;c@;=akljN*XEhg^{sjV>ClWIpk0X6>^wk8x2#@ zBwFdsh&jC*yBmj@@ny7iESK*BA*J2ZIwyO zl(vlF`B<(r7kAco45%I;wa8m)Jb#7qIZ@$W-Mcwh1l%j-;PlcaRUNRPFB%P6>W1T? zvNqV3d1W>*rkoxF z%JRo+dik&7ANT&|fU}ddHucEpCrRAA4PN+skR^>KJ&Dq?y(Uv)*yp&20$4}yj4olnU#&l$*pN~!`RPA7=R1Ubzj z-S0L!i*9evQwO)_lIW6~J3HIYV)q>eQyk9~Osy)M-?7LKEWnP-!j87_`0Q;O%o(ev z+0K4Iyby<$N4AZ5xJL>dMeO@T&f4Ls*KmYZ(5Yi5w1U2*J6Xt+8gZAP8>MT-kr?f1 zb@~ptEED%SMC^hXX8aZ*RIsPesZB{QZGMJ;`;`kgKT5Kduon)}fT{ADDsO*k#(Y%cOT#vc6ri zp+XKO9pmrEm(Xv_iAy`9V36K zU2N}1jhT+EnPr4c-YejzT#RVQ7Jcu zWDus5qYkJ6uk5x|{OJK(Ji6la1wmmOuR7{%1|k`8Y?%*6Yh&1O2L7tv4eE#9Nl74M zP37wGiLX%OYTf~>v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+hqKuyUQz1$u*9 z$xVySx&dNooh|gB$(0<|cmMP{$Szi1$m{ITabZ&-^eq=O-$pXuSBh~!*?b5=y_vwb zdBE=zka{L3bw;}5rb|bpaLNZ#3MW_j%n*Wd;%s8EMPfht)Q)H^|F+c%HpB@rK3CaM z-|LYUh;M~4>VS9$ZyacO!K4+?rr7>;h#g<5dn@T7i17bnnTU+%f7*~@q$*P*;?Pkb z)A*;^`yTz>6TFfuw?Ys^7-%D6)lB0_Yij}_h?HuAl2o3+jNcc_SY(38_=eQ9s4^uY zt|Rh)DbVg`{wcse!@r}pVSC(Ujv>hOKlLr5YxU8qJ{Z21BB;levSgg94HkXZpV42c z9*dcf(*uic7;TXfML6UM$k;)iVIT7bm@giI6AYa$@^V4(#S^wZp0c%VS25>{PqlzY gj7}lUjoadd("/data/room/03.room", room); asset->add("/data/room/04.room", room); asset->add("/data/room/05.room", room); + asset->add("/data/room/06.room", room); asset->add("/data/room/01.tmx", room); asset->add("/data/room/02.tmx", room); asset->add("/data/room/03.tmx", room); asset->add("/data/room/04.tmx", room); asset->add("/data/room/05.tmx", room); + asset->add("/data/room/06.tmx", room); asset->add("/media/tilesets/standard.png", bitmap); diff --git a/source/game.cpp b/source/game.cpp index a16dff6..365b7d9 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -3,10 +3,13 @@ // Constructor Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input) { - // Inicia variables + // Inicia algunas variables clock = SDL_GetTicks(); currentRoom = "01.room"; - spawnPoint = {2 * 8, 12 * 8, 0, 0, 0, s_standing, SDL_FLIP_NONE}; + spawnPoint = {16, 96, 0, 0, 0, s_standing, SDL_FLIP_NONE}; + + currentRoom = "06.room"; + spawnPoint = {240, 96, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL}; // Copia los punteros this->renderer = renderer; @@ -24,7 +27,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input) debugText = new Text(asset->get("debug.png"), asset->get("debug.txt"), renderer); music = JA_LoadMusic(asset->get("game.ogg").c_str()); - // Inicializa variables + // Inicializa el resto de variables ticks = 0; ticksSpeed = 15; playerLives = 9; diff --git a/source/player.cpp b/source/player.cpp index a0fcfbd..1d1c704 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -2,8 +2,6 @@ #include #include -// CAUTION!!!!! si no se gasta al final, quitar la referencia a la habitación - // Constructor Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Renderer *renderer, Asset *asset, Input *input, Room *room) { @@ -40,12 +38,15 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren sprite->setHeight(16); sprite->setFlip(ini.flip); + sprite->setCurrentAnimation("walk"); + sprite->animate(); lastPosition = getRect(); colliderBox = getRect(); const SDL_Point p = {0, 0}; colliderPoints.insert(colliderPoints.end(), {p, p, p, p, p, p, p, p}); underFeet.insert(underFeet.end(), {p, p}); + feet.insert(feet.end(), {p, p}); } // Destructor @@ -236,6 +237,23 @@ void Player::move() } } + // Comprueba colisiones con rampas + else + { + const tile_e slope = checkSlopes(); + // Se mueve hacia la derecha y cuesta hacia la derecha + if (sprite->getFlip() == SDL_FLIP_NONE && slope == t_slope_r) + { // Recoloca + y = -h + room->getSlopeHeight(feet[1], t_slope_r); + } + + // Se mueve hacia la izquierda y cuesta hacia la izquierda + if (sprite->getFlip() == SDL_FLIP_HORIZONTAL && slope == t_slope_l) + { // Recoloca + y = -h + room->getSlopeHeight(feet[0], t_slope_l); + } + } + y += vy; if (checkWalls()) { @@ -309,6 +327,12 @@ void Player::move() vy = 0.0f; } } + // EXPERIMENTAL + else if (checkSlopes()) + { + state = s_standing; + vy = 0.0f; + } } } @@ -320,16 +344,10 @@ void Player::move() // Establece la animación del jugador void Player::animate() { - // Establece la animación if (vx != 0) { - sprite->setCurrentAnimation("walk"); + sprite->animate(); } - else - { - sprite->setCurrentAnimation("stand"); - } - sprite->animate(); } // Comprueba si ha finalizado el salto al alcanzar la altura de inicio @@ -353,7 +371,8 @@ bool Player::isOnFloor() for (auto f : underFeet) { - onFloor |= ((room->getTile(f) == TILE_SOLID) || (room->getTile(f) == TILE_TRAVESSABLE)); + const tile_e tile = (room->getTile(f)); + onFloor |= (tile == t_wall || tile == t_passable || tile == t_slope_l || tile == t_slope_r); } return onFloor; @@ -370,12 +389,41 @@ bool Player::checkWalls() for (auto c : colliderPoints) { - wall |= (room->getTile(c) == TILE_SOLID); + wall |= (room->getTile(c) == t_wall); } return wall; } +// Comprueba si el jugador está en una rampa +tile_e Player::checkSlopes() +{ + // Actualiza los puntos de colisión + updateFeet(); + + // Comprueba si ha colisionado con una rampa + bool slope_l = false; + bool slope_r = false; + + for (auto f : feet) + { + slope_l |= (room->getTile(f) == t_slope_l); + slope_r |= (room->getTile(f) == t_slope_r); + } + + if (slope_l) + { + return t_slope_l; + } + + if (slope_r) + { + return t_slope_r; + } + + return t_empty; +} + // Obtiene algunos parametros del jugador player_t Player::getSpawnParams() { @@ -425,6 +473,9 @@ void Player::updateFeet() underFeet[0] = {p.x, p.y + h}; underFeet[1] = {p.x + 7, p.y + h}; + + feet[0] = {p.x, p.y + h - 1}; + feet[1] = {p.x + 7, p.y + h - 1}; } // Obtiene el valor de la variable diff --git a/source/player.h b/source/player.h index 2fd0269..a1ffdbb 100644 --- a/source/player.h +++ b/source/player.h @@ -51,6 +51,7 @@ public: SDL_Rect colliderBox; // Caja de colisión con los enemigos u objetos std::vector colliderPoints; // Puntos de colisión con el mapa std::vector underFeet; // Contiene los puntos que hay bajo cada pie del jugador + std::vector feet; // Contiene los puntos que hay en el pie del jugador state_e state; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo bool onBorder; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla int border; // Indica en cual de los cuatro bordes se encuentra @@ -89,6 +90,9 @@ public: // Comprueba que el jugador no atraviese ninguna pared bool checkWalls(); + // Comprueba si el jugador está en una rampa + tile_e checkSlopes(); + // Actualiza los puntos de colisión void updateColliderPoints(); diff --git a/source/room.cpp b/source/room.cpp index c449f99..7a4b7bd 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -28,6 +28,12 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset // Establece el color del borde screen->setBorderColor(borderColor); + + // Inicializa variables + tileSize = 8; + mapWidth = 32; + mapHeight = 16; + tilesetWidth = 20; } // Destructor @@ -499,29 +505,36 @@ std::string Room::getRoom(int border) } // Devuelve el tipo de tile que hay en ese pixel -int Room::getTile(SDL_Point point) +tile_e Room::getTile(SDL_Point point) { - int pos = ((point.y / 8) * 32) + (point.x / 8); - int tile = TILE_EMPTY; + const int maxTile = mapWidth * mapHeight; + const int pos = ((point.y / tileSize) * mapWidth) + (point.x / tileSize); + tile_e tile = t_empty; - if (pos < 512) + if (pos < maxTile) { - // Los tiles entre el 1 y el 80 son solidos - if ((tilemap[pos] > 0) && (tilemap[pos] < 201)) + // Las filas 0-7 son de tiles t_wall + if ((tilemap[pos] > 0) && (tilemap[pos] < 8 * tilesetWidth)) { - return TILE_SOLID; + return t_wall; } - // Los tiles mayores de 80 son atravesables - if ((tilemap[pos] > 200) && (tilemap[pos] < 381)) + // La fila 8 es de tiles t_slope_r + else if ((tilemap[pos] >= 8 * tilesetWidth) && (tilemap[pos] < 9 * tilesetWidth)) { - return TILE_TRAVESSABLE; + return t_slope_r; } - // Los tiles mayores de 80 son atravesables - if ((tilemap[pos] > 380) && (tilemap[pos] < 400)) + // La fila 9 es de tiles t_slope_l + else if ((tilemap[pos] >= 9 * tilesetWidth) && (tilemap[pos] < 10 * tilesetWidth)) { - return TILE_KILL; + return t_slope_l; + } + + // Las filas 10-14 son de tiles t_passable + if ((tilemap[pos] >= 10 * tilesetWidth) && (tilemap[pos] < 15 * tilesetWidth)) + { + return t_passable; } } @@ -581,4 +594,30 @@ void Room::reLoadTexture() int Room::getTileSize() { return 8; +} + +// Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile +int Room::getSlopeHeight(SDL_Point p, tile_e slope) +{ + // Calcula la base del tile + int base = ((p.y / tileSize) * tileSize) + tileSize; + printf("base %i\n", base); + + // Calcula cuanto se ha entrado en el tile horizontalmente + const int pos = (p.x % tileSize); // esto da un valor entre 0 y 7 + printf("pos %i\n", base); + + // Se resta a la base la cantidad de pixeles pos en funcion de la rampa + if (slope == t_slope_r) + { + base -= pos+1; + printf("base_R %i\n", base); + } + else + { + base -= (tileSize - pos); + printf("base_L %i\n", base); + } + + return base; } \ No newline at end of file diff --git a/source/room.h b/source/room.h index 6017d90..3e2d65b 100644 --- a/source/room.h +++ b/source/room.h @@ -15,24 +15,29 @@ #ifndef ROOM_H #define ROOM_H -#define TILE_EMPTY 0 -#define TILE_SOLID 1 -#define TILE_TRAVESSABLE 2 -#define TILE_KILL 3 - /* Cada habitación se crea y destruye cada vez que se entra o sale de la misma -Cada habitacion si que tendra lo siguiente: -ID (numerico) -NOMBRE (texto) -COLOR DE FONDO (texto) -SET DE TILES (texto, hace referencia a un png de la colección) -LIMITE SUPERIOR (ID de la habitación superior), INFERIOR, IZQUIERDO y DERECHO -MAPA DE TILES (array con los indices de los tiles a utilizar) <-- hay que decidir si cada tile del set ya - tierne propiedades o se ponen en un mapa aparte -LISTADO DE ENEMIGOS (tipo, posicion, dx, dy) -LISTADO DE ITEMS (tipo, posicion) +Cada habitacion tiene lo siguiente: + - ID (numerico) + - NOMBRE (texto) + - COLOR DE FONDO (texto) + - COLOR DEL BORDE (texto) + - SET DE TILES (texto, hace referencia a un png de la colección) + - LIMITE SUPERIOR (ID de la habitación superior), INFERIOR, IZQUIERDO y DERECHO + - MAPA DE TILES (array con los indices de los tiles a utilizar) <-- hay que decidir si cada tile del set ya + tierne propiedades o se ponen en un mapa aparte + - LISTADO DE ENEMIGOS (tipo, posicion, dx, dy) + - LISTADO DE ITEMS (tipo, posicion) */ +enum tile_e +{ + t_empty, + t_wall, + t_passable, + t_slope_l, + t_slope_r, + t_death +}; // Clase Room class Room @@ -58,6 +63,11 @@ private: JA_Sound itemSound; // Sonido producido al coger un objeto int *itemsPicked; // Puntero a la cantidad de items recogidos que lleva el juego + int tileSize; // Ancho del tile en pixels + int mapWidth; // Ancho del mapa en tiles + int mapHeight; // Alto del mapa en tiles + int tilesetWidth; // Ancho del tileset en tiles + // Carga las variables desde un fichero bool load(std::string file_path); @@ -102,7 +112,7 @@ public: std::string getRoom(int border); // Devuelve el tipo de tile que hay en ese pixel - int getTile(SDL_Point point); + tile_e getTile(SDL_Point point); // Indica si hay colision con un enemigo a partir de un rectangulo bool enemyCollision(SDL_Rect &rect); @@ -115,6 +125,10 @@ public: // Obten el tamaño del tile int getTileSize(); + + // Obten la coordenada de la cuesta a partir de un punto perteneciente a ese tile + int getSlopeHeight(SDL_Point p, tile_e slope); + }; #endif