Empezando a trabajar con tiles animados

This commit is contained in:
2022-09-25 00:14:41 +02:00
parent 86b7b5e122
commit a5d79a0323
4 changed files with 99 additions and 14 deletions

View File

@@ -13,6 +13,7 @@ Room::Room(std::string file, SDL_Renderer *renderer, Screen *screen, Asset *asse
paused = false;
itemColor1 = stringToColor("magenta");
itemColor2 = stringToColor("yellow");
counter = 0;
// Copia los punteros a objetos
this->renderer = renderer;
@@ -38,6 +39,9 @@ Room::Room(std::string file, SDL_Renderer *renderer, Screen *screen, Asset *asse
setLeftSlopes();
setRightSlopes();
// Busca los tiles animados
setAnimatedTiles();
// 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 == nullptr)
@@ -70,6 +74,11 @@ Room::~Room()
{
delete item;
}
for (auto a: animatedTiles)
{
delete a;
}
}
// Carga las variables desde un fichero de mapa
@@ -522,7 +531,7 @@ void Room::fillMapTexture()
clip.x = (tilemap[index] % tilesetWidth) * tileSize;
clip.y = (tilemap[index] / tilesetWidth) * tileSize;
texture->render(renderer, x * tileSize, y * tileSize, &clip);
// ****
if (debug->getEnabled())
{
@@ -611,10 +620,14 @@ void Room::fillMapTexture()
// Dibuja el mapa en pantalla
void Room::renderMap()
{
SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
// Dibuja la textura con el mapa en pantalla
SDL_RenderCopy(renderer, mapTexture, &rect, nullptr);
SDL_RenderCopy(renderer, mapTexture, nullptr, nullptr);
// Dibuja los tiles animados
for (auto a:animatedTiles)
{
a->render();
}
}
// Dibuja los enemigos en pantalla
@@ -643,6 +656,12 @@ void Room::update()
return;
}
// Actualiza el contador
counter++;
// Actualiza los tiles animados
updateAnimatedTiles();
for (auto enemy : enemies)
{ // Actualiza los enemigos
enemy->update();
@@ -713,12 +732,18 @@ tile_e Room::getTile(int index)
return t_slope_l;
}
// Las filas 10-18 son de tiles t_passable
if ((tilemap[index] >= 10 * tilesetWidth) && (tilemap[index] < 19 * tilesetWidth))
// Las filas 10-17 son de tiles t_passable
if ((tilemap[index] >= 10 * tilesetWidth) && (tilemap[index] < 18 * tilesetWidth))
{
return t_passable;
}
// La fila 18 es de tiles t_animated
if ((tilemap[index] >= 18 * tilesetWidth) && (tilemap[index] < 29 * tilesetWidth))
{
return t_animated;
}
// Las fila 19 es de tiles t_kill
if ((tilemap[index] >= 19 * tilesetWidth) && (tilemap[index] < 20 * tilesetWidth))
{
@@ -1061,6 +1086,53 @@ void Room::setRightSlopes()
}
}
// Localiza todos los tiles animados de la habitación
void Room::setAnimatedTiles()
{
// Recorre la habitación entera por filas buscando tiles de tipo t_animated
for (int i = 0; i < (int)tilemap.size(); ++i)
{
if (getTile(i) == t_animated)
{
// la i me da la ubicación
int x = (i % mapWidth) * tileSize;
int y = (i / mapWidth) * tileSize;
// tilemap[i] me da el tile a poner
int xc = (tilemap[i] % tilesetWidth) * tileSize;
int yc = (tilemap[i] / tilesetWidth) * tileSize;
Sprite *sp = new Sprite(x, y, 8, 8, texture, renderer);
sp->setSpriteClip(xc, yc, 8, 8);
animatedTiles.push_back(sp);
}
}
}
// Actualiza los tiles animados
void Room::updateAnimatedTiles()
{
const int frame = (counter / 8) % 4;
for (auto &a : animatedTiles)
{ // Frame vale 0, 1, 2 o 3. La x se aumenta de 8 en 8, salvo cuando frame vale 0 que ha de volver 3*8
SDL_Rect rect = a->getSpriteClip();
if (frame == 0)
rect.x -= 24;
else
rect.x += 8;
a->setSpriteClip(rect);
}
}
// Pinta los tiles animados en pantalla
void Room::renderAnimatedTiles()
{
for (auto a : animatedTiles)
{
a->render();
}
}
// Comprueba las colisiones
int Room::checkRightSurfaces(SDL_Rect *rect)
{