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

@@ -5,8 +5,8 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
{
// Inicia algunas variables
board.iniClock = SDL_GetTicks();
currentRoom = "01.room";
spawnPoint = {16, 96, 0, 0, 0, s_standing, SDL_FLIP_NONE};
currentRoom = "03.room";
spawnPoint = {15, 96, 0, 0, 0, s_standing, SDL_FLIP_NONE};
// Copia los punteros
this->renderer = renderer;
@@ -24,10 +24,10 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
//currentRoom = "06.room";
//spawnPoint = {14 * 8, 9 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
currentRoom = "60.room";
const int x = 16;
const int y = 13;
spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
//currentRoom = "60.room";
//const int x = 16;
//const int y = 13;
//spawnPoint = {x * 8, y * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
// ****
// Crea los objetos

View File

@@ -20,7 +20,7 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren
color = stringToColor("white");
onBorder = false;
border = BORDER_TOP;
invincible = false;
invincible = true;
alive = true;
maxFallHeight = BLOCK * 4;
paused = false;

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)
{

View File

@@ -6,6 +6,7 @@
#include "screen.h"
#include "enemy.h"
#include "item.h"
#include "sprite.h"
#include "item_tracker.h"
#include "const.h"
#include "jail_audio.h"
@@ -36,7 +37,8 @@ enum tile_e
t_passable,
t_slope_l,
t_slope_r,
t_kill
t_kill,
t_animated
};
// Clase Room
@@ -73,6 +75,8 @@ private:
std::vector<v_line_t> rightSurfaces; // Lista con las superficies laterales de la parte derecha de la habitación
std::vector<d_line_t> leftSlopes; // Lista con todas las rampas que suben hacia la izquierda
std::vector<d_line_t> rightSlopes; // Lista con todas las rampas que suben hacia la derecha
int counter; // Contador para lo que haga falta
std::vector<Sprite *> animatedTiles; // Vector con los indices de tiles animados
int tileSize; // Ancho del tile en pixels
int mapWidth; // Ancho del mapa en tiles
@@ -115,6 +119,15 @@ private:
// Encuentra todas las rampas que suben hacia la derecha
void setRightSlopes();
// Localiza todos los tiles animados de la habitación
void setAnimatedTiles();
// Actualiza los tiles animados
void updateAnimatedTiles();
// Pinta los tiles animados en pantalla
void renderAnimatedTiles();
// Devuelve el tipo de tile que hay en ese indice
tile_e getTile(int index);