From 7e93b3150f1bdb732889580377fe9f1db28504c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Sat, 20 Aug 2022 09:15:51 +0200 Subject: [PATCH] Terminados los tiles atravesables --- data/map/01_8.tmx | 34 +++++++++++++------------- source/game.cpp | 17 ++++++++++++- source/map.cpp | 12 +++++++--- source/map.h | 9 ++++--- source/player.cpp | 61 +++++++++++++++++++++++++++++++---------------- source/player.h | 27 +++++++++++---------- 6 files changed, 102 insertions(+), 58 deletions(-) diff --git a/data/map/01_8.tmx b/data/map/01_8.tmx index c87ee56..e40b3f3 100644 --- a/data/map/01_8.tmx +++ b/data/map/01_8.tmx @@ -12,23 +12,23 @@ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,159,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,191,192,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,0,0,257,258,0,0,0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,265,266,0,0,0,0,0,0,0,0,0,0, -0,263,264,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,297,298,0,0,0,0,0,0,0,0,0,0, -0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,263,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,263,0,0,0,0, -0,0,0,0,239,240,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,265,266,0,0,0,0,0,0,0,0,0,0, -0,0,0,257,258,257,258,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,93,94,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,289,290,289,290,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,125,126,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,609,610,611,612,613,614,0,0,0,0,0,0,0,0,157,158,0,0,0,0,263,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,189,190,0,0,0,0,0,0,0,0,0,0,189,190,0,0,0,0,0,0,0,0,0,0,0,0,0, -263,264,0,0,0,0,0,193,194,0,0,159,160,189,190,0,0,0,0,0,0,0,281,282,277,278,279,280,0,0,0,0,0,0,0,0,0,0,0,0, -295,296,0,0,0,0,0,225,226,0,0,191,192,189,190,0,0,281,275,275,275,275,313,314,309,310,311,312,0,0,0,0,0,0,0,0,0,0,0,0, -265,264,263,264,263,264,263,264,263,264,263,264,263,264,263,264,263,264,263,266,266,266,283,284,266,265,266,264,263,264,263,264,263,264,263,264,263,264,263,264, -295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,295,296,315,316,298,297,298,296,295,296,295,296,295,296,295,296,295,296,295,296 +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,159,160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,191,192,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,0,0,0, +0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,0,0,257,258,0,0,0,0,0,0,0,0,0,263,264,0,0,0,0,0,0,259,0,0,0, +0,0,0,0,0,0,0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,265,266,0,0,0,0,0,0,259,0,0,0, +0,263,264,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,259,0,0,0, +0,295,296,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0, +0,0,0,0,239,240,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,265,266,0,0,0,259,0,0,259,0,0,0, +0,0,0,257,258,257,258,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0, +0,0,0,289,290,289,290,0,0,0,0,577,578,579,580,581,582,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,259,0,0,259,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,577,578,579,580,581,582,0,0,0,0,0,0,0,0,0,0,0,0,0, +263,264,0,0,0,0,0,193,194,0,0,0,0,0,0,0,0,0,0,0,0,0,281,282,277,278,279,280,0,0,0,0,0,0,0,0,0,0,0,0, +295,296,0,0,0,0,0,225,226,0,0,0,0,0,0,0,0,281,275,275,275,275,313,314,309,310,311,312,0,0,0,0,0,0,0,0,0,0,0,0, +265,264,263,264,263,264,263,264,263,0,263,264,263,264,263,264,263,264,263,266,266,266,283,284,266,265,266,264,263,264,263,264,263,264,263,264,263,264,263,264, +295,296,295,296,295,296,295,296,295,0,295,296,295,296,295,296,295,296,295,296,295,296,315,316,298,297,298,296,295,296,295,296,295,296,295,296,295,296,295,296 diff --git a/source/game.cpp b/source/game.cpp index 7a0ef42..7567210 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -118,6 +118,18 @@ void Game::renderDebugInfo() return; } + // Pinta la rejilla + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 16); + for (int i = 0; i < 240; i += 8) + { + SDL_RenderDrawLine(renderer, 0, i, 320, i); + } + for (int i = 0; i < 320; i += 8) + { + SDL_RenderDrawLine(renderer, i, 0, i, 240); + } + + // Pinta el texto int line = 0; std::string text = ""; @@ -136,7 +148,10 @@ void Game::renderDebugInfo() text = "isOnFloor " + std::to_string(player->isOnFloor()); debugText->write(0, line += 6, text, -1); - text = "getTile " + std::to_string(player->map->getTile(player->collider[0])); + const std::string foot_x = std::to_string(player->underFeet[0].x); + const std::string foot_y = std::to_string(player->underFeet[0].y); + const std::string gettile = std::to_string(player->map->getTile(player->underFeet[0])); + text = "getTile(" + foot_x + "," + foot_y + ") = " + gettile; debugText->write(0, line += 6, text, -1); text = "state " + std::to_string(player->state); diff --git a/source/map.cpp b/source/map.cpp index 44ad4d8..951a9b7 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -226,13 +226,13 @@ void Map::render() t_tile_map Map::getTile(SDL_Point p) { const int tile = tilemap[((p.y / tile_size) * map_width) + (p.x / tile_size)]; - const int png_width = 16 * 2; + const int png_width = 32; - if (tile >= 0 && tile < 4 * 2 * png_width) + if (tile >= 0 && tile < 8 * png_width) { return nothing; } - else if (tile >= (4 * 2 * png_width) && tile < 8 * 2 * png_width) + else if (tile >= (8 * png_width) && tile < 16 * png_width) { return wall; } @@ -246,4 +246,10 @@ t_tile_map Map::getTile(SDL_Point p) int Map::getTileSize() { return tile_size; +} + +// Devuelve el indice del tile correspondiente a un punto del mapa +int Map::getTileIndex(SDL_Point p) +{ + return tilemap[((p.y / tile_size) * map_width) + (p.x / tile_size)]; } \ No newline at end of file diff --git a/source/map.h b/source/map.h index 771ffd5..b5c27cb 100644 --- a/source/map.h +++ b/source/map.h @@ -36,9 +36,9 @@ private: LTexture *texture_bg; // Textura con los graficos de fondo de la habitación SDL_Texture *map_texture; // Textura para dibujar el mapa de la habitación - int tile_size; // Ancho del tile en pixels - int map_width; // Ancho del mapa en tiles - int map_height; // Alto del mapa en tiles + int tile_size; // Ancho del tile en pixels + int map_width; // Ancho del mapa en tiles + int map_height; // Alto del mapa en tiles int tileset_width; // Ancho del tileset en tiles // Carga las variables desde un fichero @@ -68,6 +68,9 @@ public: // Devuelve el valor de la variable int getTileSize(); + + // Devuelve el indice del tile correspondiente a un punto del mapa + int getTileIndex(SDL_Point p); }; #endif diff --git a/source/player.cpp b/source/player.cpp index 6fe8ec9..fb30586 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -23,6 +23,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map) y = 168; vx = 0; vy = 0; + lastPosition = {(int)x, (int)y}; const SDL_Rect rect = {(int)x, (int)y, 16, 24}; sprite->setPos(rect); sprite->setCurrentAnimation("stand"); @@ -64,7 +65,7 @@ Player::~Player() void Player::update() { checkInput(); - addGravity(); + // addGravity(); move(); animate(); } @@ -136,13 +137,10 @@ void Player::checkInput() // Aplica la gravedad void Player::addGravity() { - if (!isOnFloor()) + // *** Falta ver pq la gravedad empuja al muñeco hacia abajo en los tiles atravesables + if (state != standing) { vy = std::min(vy += gravity, maxVY); - if (state == standing) - { - state = falling; - } } } @@ -198,48 +196,74 @@ bool Player::checkMapCollisions() // Mueve al jugador en función de la velocidad/desplazamiento void Player::move() { - const int tile = map->getTileSize(); + const int tileSize = map->getTileSize(); + lastPosition = {(int)x, (int)y}; + addGravity(); + // Mueve en el eje X y comprueba colisiones con muros x += vx; if (checkMapCollisions()) { // Recoloca if (vx > 0) { - x -= ((int)x + w) % tile; + x -= ((int)x + w) % tileSize; } else { - x += tile - ((int)x % tile); + x += tileSize - ((int)x % tileSize); } vx = 0.0f; } - const bool wasOnFloor = isOnFloor(); + // Mueve en el eje Y y comprueba colisiones con muros y += vy; if (checkMapCollisions()) { // Recoloca if (vy > 0.0f) { - y -= ((int)y + h) % tile; + y -= ((int)y + h) % tileSize; state = standing; } else { - y += tile - ((int)y % tile); + y += tileSize - ((int)y % tileSize); state = falling; } vy = 0.0f; } - else if ((!wasOnFloor) && (isOnFloor()) && (vy > 0.0f)) + + // Si no hay colisiones con los muros en el eje Y, comprueba no haya atravesado + // el suelo de un tile atravesble + else { - state = standing; - vy = 0.0f; - y -= ((int)y + h) % tile; + const int a = (lastPosition.y + h) / tileSize; + const int b = ((int)y + h) / tileSize; + const bool tile_change = a != b; + + const bool going_down = vy >= 0.0f; + const bool tile_aligned = ((int)y + h) % tileSize == 0; + + if (((going_down) && (tile_aligned)) || ((going_down) && (tile_change))) + { + // Tiene uno de los pies sobre una superficie + if (isOnFloor()) + { + state = standing; + vy = 0.0f; + y -= ((int)y + h) % tileSize; + } + // Tiene ambos pies sobre el vacío + else + { + state = falling; + } + } } + // Actualiza la posición del sprite sprite->setPosX(x); sprite->setPosY(y); } @@ -273,11 +297,6 @@ bool Player::isOnFloor() updateFeet(); - if (underFeet[0].y % map->getTileSize() != 0) - { - return false; - } - for (auto f : underFeet) { onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable)); diff --git a/source/player.h b/source/player.h index 9198506..81505c5 100644 --- a/source/player.h +++ b/source/player.h @@ -28,19 +28,20 @@ public: LTexture *texture; // Textura con los graficos del jugador Map *map; // Objeto con el mapa - float x; // Posición del jugador en el eje X - float y; // Posición del jugador en el eje Y - float vx; // Velocidad/desplazamiento del jugador en el eje X - float vy; // Velocidad/desplazamiento del jugador en el eje Y - bool jumpPressed; // Indica si esta pulsada la tecla de salto - bool enabled; // Si está habilitado - bool invulnerable; // Indica si se encuentra en estado invulnerable - int coins; // Cantidad de monedas - int cooldown; // Tiempo de inhabilitación - int lives; // Cantidad de vidas - int w; // Ancho del jugador - int h; // ALto del jugador - t_player_state state; // Estado actual del jugador + float x; // Posición del jugador en el eje X + float y; // Posición del jugador en el eje Y + float vx; // Velocidad/desplazamiento del jugador en el eje X + float vy; // Velocidad/desplazamiento del jugador en el eje Y + bool jumpPressed; // Indica si esta pulsada la tecla de salto + bool enabled; // Si está habilitado + bool invulnerable; // Indica si se encuentra en estado invulnerable + int coins; // Cantidad de monedas + int cooldown; // Tiempo de inhabilitación + int lives; // Cantidad de vidas + int w; // Ancho del jugador + int h; // ALto del jugador + t_player_state state; // Estado actual del jugador + SDL_Point lastPosition; // Posición anterior // Variables que afectan a la inercia del movimiento float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar