Primera implementacion de tiles atravesables. No funcionará si estan apilados

This commit is contained in:
2022-08-19 12:33:42 +02:00
parent 76696f9eb7
commit 0624cff845
6 changed files with 71 additions and 52 deletions

View File

@@ -3,6 +3,12 @@
// Constructor
Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset)
{
// Inicializa variables
tile_size = 8;
map_width = 40;
map_height = 26;
tileset_width = 32;
// Copia los punteros a objetos
this->asset = asset;
this->renderer = renderer;
@@ -21,11 +27,6 @@ Map::Map(std::string file, SDL_Renderer *renderer, Asset *asset)
// Pinta el mapa de la habitación en la textura
fillMapTexture();
// Inicializa variables
tile_width = 16/2;
map_width = 20*2;
map_height = 13*2;
}
// Destructor
@@ -160,16 +161,6 @@ bool Map::setVars(std::string var, std::string value)
{
room_right = value;
}
else if (var == "tilemap")
{
// Se introducen los valores separados por comas en un vector
std::stringstream ss(value);
std::string tmp;
while (getline(ss, tmp, ','))
{
tilemap.push_back(std::stoi(tmp));
}
}
else if (var == "")
{
}
@@ -194,18 +185,13 @@ void Map::fillMapTexture()
texture_bg->render(renderer, 0, 0, &clip);
// Dibuja el mapeado de tiles
const int tile_size = 16/2;
const int tileset_width_in_tiles = 16*2;
const int map_width_in_tiles = 20*2;
const int map_height_in_tiles = 13*2;
clip = {0, 0, tile_size, tile_size};
for (int y = 0; y < map_height_in_tiles; y++)
for (int x = 0; x < map_width_in_tiles; x++)
for (int y = 0; y < map_height; y++)
for (int x = 0; x < map_width; x++)
{
clip.x = ((tilemap[(y * map_width_in_tiles) + x] - 1) % tileset_width_in_tiles) * tile_size;
clip.y = ((tilemap[(y * map_width_in_tiles) + x] - 1) / tileset_width_in_tiles) * tile_size;
clip.x = ((tilemap[(y * map_width) + x] - 1) % tileset_width) * tile_size;
clip.y = ((tilemap[(y * map_width) + x] - 1) / tileset_width) * tile_size;
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
}
@@ -239,19 +225,25 @@ void Map::render()
// Devuelve el tipo de tile que hay en un punto
t_tile_map Map::getTile(SDL_Point p)
{
const int tile = tilemap[((p.y / tile_width) * map_width) + (p.x / tile_width)];
const int png_width = 16*2;
const int tile = tilemap[((p.y / tile_size) * map_width) + (p.x / tile_size)];
const int png_width = 16 * 2;
if (tile >= 0 && tile < 4*2 * png_width)
if (tile >= 0 && tile < 4 * 2 * png_width)
{
return nothing;
}
else if (tile >= (4*2 * png_width) && tile < 8*2 * png_width)
else if (tile >= (4 * 2 * png_width) && tile < 8 * 2 * png_width)
{
return wall;
}
else
{
return travessable;
return passable;
}
}
// Devuelve el valor de la variable
int Map::getTileWidth()
{
return tile_size;
}