Ya se detectan las rampas en el mapa

This commit is contained in:
2022-09-09 19:02:36 +02:00
parent 36fc848779
commit 2ccb02258c
3 changed files with 120 additions and 5 deletions

View File

@@ -30,6 +30,8 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
setTopSurfaces(); setTopSurfaces();
setLeftSurfaces(); setLeftSurfaces();
setRightSurfaces(); setRightSurfaces();
setLeftSlopes();
setRightSlopes();
// Crea la textura para el mapa de tiles de la habitación // 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); mapTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
@@ -458,7 +460,6 @@ void Room::fillMapTexture()
// TopSurfaces // TopSurfaces
if (true) if (true)
{ {
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
for (auto l : topSurfaces) for (auto l : topSurfaces)
{ {
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF);
@@ -469,7 +470,6 @@ void Room::fillMapTexture()
// LeftSurfaces // LeftSurfaces
if (true) if (true)
{ {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
for (auto l : leftSurfaces) for (auto l : leftSurfaces)
{ {
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF);
@@ -480,13 +480,32 @@ void Room::fillMapTexture()
// RightSurfaces // RightSurfaces
if (true) if (true)
{ {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (auto l : rightSurfaces) for (auto l : rightSurfaces)
{ {
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF); SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF);
SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2); 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; h_line_t line;
line.x1 = (tile[i] % mapWidth) * tileSize; 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]) while (tile[i] + 1 == tile[i + 1])
{ {
i++; i++;
@@ -856,7 +875,7 @@ void Room::setRightSurfaces()
while (i < tile.size()) while (i < tile.size())
{ {
v_line_t line; 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); line.y1 = ((tile[i] / mapWidth) * tileSize);
while (tile[i] + mapWidth == tile[i + 1]) 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<int> 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<int> 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 // Comprueba las colisiones
int Room::checkRightSurfaces(SDL_Rect *rect) int Room::checkRightSurfaces(SDL_Rect *rect)
{ {

View File

@@ -68,6 +68,8 @@ private:
std::vector<h_line_t> topSurfaces; // Lista con las superficies superiores de la habitación std::vector<h_line_t> topSurfaces; // Lista con las superficies superiores de la habitación
std::vector<v_line_t> leftSurfaces; // Lista con las superficies laterales de la parte izquierda de la habitación std::vector<v_line_t> leftSurfaces; // Lista con las superficies laterales de la parte izquierda de la habitación
std::vector<v_line_t> rightSurfaces; // Lista con las superficies laterales de la parte derecha de la habitación std::vector<v_line_t> rightSurfaces; // Lista con las superficies laterales de la parte derecha de la habitación
std::vector<line_t> leftSlopes; // Lista con todas las rampas que suben hacia la izquierda
std::vector<line_t> rightSlopes; // Lista con todas las rampas que suben hacia la derecha
int tileSize; // Ancho del tile en pixels int tileSize; // Ancho del tile en pixels
int mapWidth; // Ancho del mapa en tiles int mapWidth; // Ancho del mapa en tiles
@@ -101,6 +103,12 @@ private:
// Calcula las superficies laterales derechas // Calcula las superficies laterales derechas
void setRightSurfaces(); 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 // Devuelve el tipo de tile que hay en ese indice
tile_e getTile(int index); tile_e getTile(int index);

View File

@@ -46,6 +46,12 @@ struct v_line_t
int x, y1, y2; int x, y1, y2;
}; };
// Estructura para definir una linea
struct line_t
{
int x1, x2, y1, y2;
};
// Estructura para definir un color // Estructura para definir un color
struct color_t struct color_t
{ {