Creando las superficies del mapa

This commit is contained in:
2022-09-07 14:00:02 +02:00
parent 4193414f10
commit 2039b2f8db
4 changed files with 268 additions and 27 deletions

View File

@@ -6,6 +6,12 @@
// Constructor
Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset *asset, ItemTracker *itemTracker, int *items, Debug *debug)
{
// Inicializa variables
tileSize = 8;
mapWidth = 32;
mapHeight = 16;
tilesetWidth = 20;
// Copia los punteros a objetos
this->renderer = renderer;
this->asset = asset;
@@ -19,6 +25,12 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
texture = new LTexture(renderer, asset->get(tileset));
itemSound = JA_LoadSound(asset->get("item.wav").c_str());
// Calcula las superficies
setBottomSurfaces();
setTopSurfaces();
setLeftSurfaces();
setRightSurfaces();
// 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);
if (mapTexture == NULL)
@@ -29,12 +41,6 @@ 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
@@ -434,6 +440,32 @@ void Room::fillMapTexture()
texture->render(renderer, x * 8, y * 8, &clip);
}
// ****
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
for (auto l : bottomSurfaces)
{
SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
}
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
for (auto l : topSurfaces)
{
SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
}
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
for (auto l : leftSurfaces)
{
SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
}
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (auto l : rightSurfaces)
{
SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
}
// ****
SDL_SetRenderTarget(renderer, nullptr);
}
@@ -542,6 +574,42 @@ tile_e Room::getTile(SDL_Point point)
return tile;
}
// Devuelve el tipo de tile que hay en ese indice
tile_e Room::getTile(int index)
{
const int maxTile = mapWidth * mapHeight;
tile_e tile = t_empty;
if (index < maxTile)
{
// Las filas 0-7 son de tiles t_wall
if ((tilemap[index] > 0) && (tilemap[index] < 8 * tilesetWidth))
{
return t_wall;
}
// La fila 8 es de tiles t_slope_r
else if ((tilemap[index] >= 8 * tilesetWidth) && (tilemap[index] < 9 * tilesetWidth))
{
return t_slope_r;
}
// La fila 9 es de tiles t_slope_l
else if ((tilemap[index] >= 9 * tilesetWidth) && (tilemap[index] < 10 * tilesetWidth))
{
return t_slope_l;
}
// Las filas 10-14 son de tiles t_passable
if ((tilemap[index] >= 10 * tilesetWidth) && (tilemap[index] < 15 * tilesetWidth))
{
return t_passable;
}
}
return tile;
}
// Indica si hay colision con un enemigo a partir de un rectangulo
bool Room::enemyCollision(SDL_Rect &rect)
{
@@ -625,4 +693,78 @@ int Room::getSlopeHeight(SDL_Point p, tile_e slope)
}
return base;
}
// Calcula las superficies inferiores
void Room::setBottomSurfaces()
{
std::vector<int> tile;
// Busca todos los tiles de tipo wall (excepto los de la última fila) que debajo
// tienen un tile de tipo vacio
for (int i = 0; i < tilemap.size() - mapWidth; i++)
{
if (getTile(i) == t_wall && getTile(i + mapWidth) == t_empty)
{
tile.push_back(i);
}
}
// Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies
int i = 0;
while (i < tile.size())
{
h_line_t line;
line.x1 = (tile[i] % mapWidth) * tileSize;
line.y = ((tile[i] / mapWidth) * tileSize) + tileSize - 1;
while (tile[i] + 1 == tile[i + 1])
{
i++;
}
line.x2 = ((tile[i] % mapWidth) * tileSize) + tileSize - 1;
bottomSurfaces.push_back(line);
i++;
}
}
// Calcula las superficies superiores
void Room::setTopSurfaces()
{
std::vector<int> tile;
// Busca todos los tiles de tipo wall (excepto los de la primera fila) que encima
// tienen un tile de tipo vacio
for (int i = mapWidth; i < tilemap.size(); i++)
{
if (getTile(i) == t_wall && getTile(i - mapWidth) == t_empty)
{
tile.push_back(i);
}
}
// Recorre el vector de tiles buscando tiles consecutivos para localizar las superficies
int i = 0;
while (i < tile.size())
{
h_line_t line;
line.x1 = (tile[i] % mapWidth) * tileSize;
line.y = ((tile[i] / mapWidth) * tileSize);
while (tile[i] + 1 == tile[i + 1])
{
i++;
}
line.x2 = ((tile[i] % mapWidth) * tileSize) + tileSize - 1;
topSurfaces.push_back(line);
i++;
}
}
// Calcula las superficies laterales izquierdas
void Room::setLeftSurfaces()
{
}
// Calcula las superficies laterales derechas
void Room::setRightSurfaces()
{
}