diff --git a/source/room.cpp b/source/room.cpp index 6c2b45c..e0cae06 100644 --- a/source/room.cpp +++ b/source/room.cpp @@ -30,6 +30,8 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset setTopSurfaces(); setLeftSurfaces(); setRightSurfaces(); + setLeftSlopes(); + setRightSlopes(); // Crea la textura para el mapa de tiles de la habitación mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT); @@ -458,7 +460,6 @@ void Room::fillMapTexture() // TopSurfaces if (true) { - SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF); for (auto l : topSurfaces) { SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); @@ -469,7 +470,6 @@ void Room::fillMapTexture() // LeftSurfaces if (true) { - SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); for (auto l : leftSurfaces) { SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); @@ -480,13 +480,32 @@ void Room::fillMapTexture() // RightSurfaces if (true) { - SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); for (auto l : rightSurfaces) { SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2); } } + + // LeftSlopes + if (true) + { + for (auto l : leftSlopes) + { + SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_RenderDrawLine(renderer, l.x1, l.y1, l.x2, l.y2); + } + } + + // RightSlopes + if (true) + { + for (auto l : rightSlopes) + { + SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); + SDL_RenderDrawLine(renderer, l.x1, l.y1, l.x2, l.y2); + } + } } // **** @@ -781,7 +800,7 @@ void Room::setTopSurfaces() { h_line_t line; line.x1 = (tile[i] % mapWidth) * tileSize; - line.y = ((tile[i] / mapWidth) * tileSize); + line.y = (tile[i] / mapWidth) * tileSize; while (tile[i] + 1 == tile[i + 1]) { i++; @@ -856,7 +875,7 @@ void Room::setRightSurfaces() while (i < tile.size()) { v_line_t line; - line.x = (tile[i] % mapWidth) * tileSize + tileSize - 1; + line.x = ((tile[i] % mapWidth) * tileSize) + tileSize - 1; line.y1 = ((tile[i] / mapWidth) * tileSize); while (tile[i] + mapWidth == tile[i + 1]) { @@ -869,6 +888,88 @@ void Room::setRightSurfaces() } } +// Encuentra todas las rampas que suben hacia la izquierda +void Room::setLeftSlopes() +{ + // Recorre la habitación entera por filas buscando tiles de tipo t_slope_l + std::vector found; + for (int i = 0; i < tilemap.size(); ++i) + { + if (getTile(i) == t_slope_l) + { + found.push_back(i); + } + } + + // El primer elemento es el inicio de una rampa. Se añade ese elemento y se buscan los siguientes, + // que seran i + mapWidth + 1. Conforme se añaden se eliminan y se vuelve a escudriñar el vector de + // tiles encontrados hasta que esté vacío + + while (found.size() > 0) + { + line_t line; + line.x1 = (found[0] % mapWidth) * tileSize; + line.y1 = (found[0] / mapWidth) * tileSize; + int lookingFor = found[0] + mapWidth + 1; + int lastOneFound = found[0]; + found.erase(found.begin()); + for (int i = 0; i < found.size(); i++) + { + if (found[i] == lookingFor) + { + lastOneFound = lookingFor; + lookingFor += mapWidth + 1; + found.erase(found.begin() + i); + --i; + } + } + line.x2 = ((lastOneFound % mapWidth) * tileSize) + tileSize - 1; + line.y2 = ((lastOneFound / mapWidth) * tileSize) + tileSize - 1; + leftSlopes.push_back(line); + } +} + +// Encuentra todas las rampas que suben hacia la derecha +void Room::setRightSlopes() +{ + // Recorre la habitación entera por filas buscando tiles de tipo t_slope_r + std::vector found; + for (int i = 0; i < tilemap.size(); ++i) + { + if (getTile(i) == t_slope_r) + { + found.push_back(i); + } + } + + // El primer elemento es el inicio de una rampa. Se añade ese elemento y se buscan los siguientes, + // que seran i + mapWidth - 1. Conforme se añaden se eliminan y se vuelve a escudriñar el vector de + // tiles encontrados hasta que esté vacío + + while (found.size() > 0) + { + line_t line; + line.x1 = ((found[0] % mapWidth) * tileSize) + tileSize - 1; + line.y1 = (found[0] / mapWidth) * tileSize; + int lookingFor = found[0] + mapWidth - 1; + int lastOneFound = found[0]; + found.erase(found.begin()); + for (int i = 0; i < found.size(); i++) + { + if (found[i] == lookingFor) + { + lastOneFound = lookingFor; + lookingFor += mapWidth - 1; + found.erase(found.begin() + i); + --i; + } + } + line.x2 = (lastOneFound % mapWidth) * tileSize; + line.y2 = ((lastOneFound / mapWidth) * tileSize) + tileSize - 1; + leftSlopes.push_back(line); + } +} + // Comprueba las colisiones int Room::checkRightSurfaces(SDL_Rect *rect) { diff --git a/source/room.h b/source/room.h index 47f79f6..759bb56 100644 --- a/source/room.h +++ b/source/room.h @@ -68,6 +68,8 @@ private: std::vector topSurfaces; // Lista con las superficies superiores de la habitación std::vector leftSurfaces; // Lista con las superficies laterales de la parte izquierda de la habitación std::vector rightSurfaces; // Lista con las superficies laterales de la parte derecha de la habitación + std::vector leftSlopes; // Lista con todas las rampas que suben hacia la izquierda + std::vector rightSlopes; // Lista con todas las rampas que suben hacia la derecha int tileSize; // Ancho del tile en pixels int mapWidth; // Ancho del mapa en tiles @@ -101,6 +103,12 @@ private: // Calcula las superficies laterales derechas void setRightSurfaces(); + // Encuentra todas las rampas que suben hacia la izquierda + void setLeftSlopes(); + + // Encuentra todas las rampas que suben hacia la derecha + void setRightSlopes(); + // Devuelve el tipo de tile que hay en ese indice tile_e getTile(int index); diff --git a/source/utils.h b/source/utils.h index b3f2900..6a72185 100644 --- a/source/utils.h +++ b/source/utils.h @@ -46,6 +46,12 @@ struct v_line_t int x, y1, y2; }; +// Estructura para definir una linea +struct line_t +{ + int x1, x2, y1, y2; +}; + // Estructura para definir un color struct color_t {