afegida la classe Explosions
This commit is contained in:
@@ -12,28 +12,79 @@ Explosions::~Explosions()
|
|||||||
{
|
{
|
||||||
for (auto explosion : explosions)
|
for (auto explosion : explosions)
|
||||||
{
|
{
|
||||||
|
if (explosion)
|
||||||
|
{
|
||||||
|
delete explosion;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza la lógica de la clase
|
// Actualiza la lógica de la clase
|
||||||
void Explosions::update()
|
void Explosions::update()
|
||||||
{
|
{
|
||||||
|
for (auto explosion : explosions)
|
||||||
|
{
|
||||||
|
explosion->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vacia el vector de elementos finalizados
|
||||||
|
freeExplosions();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuja el objeto en pantalla
|
// Dibuja el objeto en pantalla
|
||||||
void Explosions::render()
|
void Explosions::render()
|
||||||
{
|
{
|
||||||
|
for (auto explosion : explosions)
|
||||||
|
{
|
||||||
|
explosion->render();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Añade texturas al objetp
|
// Añade texturas al objetp
|
||||||
void Explosions::addTexture(Texture *texture)
|
void Explosions::addTexture(int size, Texture *texture, std::vector<std::string> *animation)
|
||||||
{
|
{
|
||||||
textures.push_back(texture);
|
explosion_texture_t temp;
|
||||||
|
temp.size = size;
|
||||||
|
temp.texture = texture;
|
||||||
|
temp.animation = animation;
|
||||||
|
textures.push_back(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Añade una explosión
|
// Añade una explosión
|
||||||
void Explosions::add(int x, int y, int size)
|
void Explosions::add(int x, int y, int size)
|
||||||
{
|
{
|
||||||
AnimatedSprite *sprite = new AnimatedSprite(textures[0]);
|
const int index = getIndexBySize(size);
|
||||||
sprite->setPos({x,y});
|
AnimatedSprite *sprite = new AnimatedSprite(textures[index].texture, "", textures[index].animation);
|
||||||
|
sprite->setPos({x, y});
|
||||||
|
explosions.push_back(sprite);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vacia el vector de elementos finalizados
|
||||||
|
void Explosions::freeExplosions()
|
||||||
|
{
|
||||||
|
if (explosions.empty() == false)
|
||||||
|
{
|
||||||
|
for (int i = explosions.size() - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
|
if (explosions[i]->animationIsCompleted())
|
||||||
|
{
|
||||||
|
delete explosions[i];
|
||||||
|
explosions.erase(explosions.begin() + i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Busca una textura a partir del tamaño
|
||||||
|
int Explosions::getIndexBySize(int size)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < (int)textures.size();++i)
|
||||||
|
{
|
||||||
|
if (size == textures[i].size)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -5,27 +5,26 @@
|
|||||||
#include "common/animatedsprite.h"
|
#include "common/animatedsprite.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
struct explosion_t
|
struct explosion_texture_t
|
||||||
{
|
{
|
||||||
AnimatedSprite *sprite; // Sprite para dibujar la explosión
|
Texture *texture; // Textura para la explosión
|
||||||
bool ended; // Indica si la explosión ha terminado
|
std::vector<std::string> *animation; // Animación para la textura
|
||||||
};
|
int size; // Tamaño de la explosión
|
||||||
|
|
||||||
struct explision_texture_t
|
|
||||||
{
|
|
||||||
Texture *texture; // Textura para la explosión
|
|
||||||
int size; // Tamaño de la explosión
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Clase explosions
|
// Clase explosions
|
||||||
class Explosions
|
class Explosions
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
// Objetos y punteros
|
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
std::vector<Texture *> textures; // Vector con las texturas a utilizar
|
std::vector<explosion_texture_t> textures; // Vector con las texturas a utilizar
|
||||||
std::vector<explosion_t> explosions; // Lista con todas las explosiones
|
std::vector<AnimatedSprite *> explosions; // Lista con todas las explosiones
|
||||||
|
|
||||||
|
// Vacia el vector de elementos finalizados
|
||||||
|
void freeExplosions();
|
||||||
|
|
||||||
|
// Busca una textura a partir del tamaño
|
||||||
|
int getIndexBySize(int size);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
@@ -41,7 +40,7 @@ public:
|
|||||||
void render();
|
void render();
|
||||||
|
|
||||||
// Añade texturas al objetp
|
// Añade texturas al objetp
|
||||||
void addTexture(Texture *texture);
|
void addTexture(int size, Texture *texture, std::vector<std::string> *animation);
|
||||||
|
|
||||||
// Añade una explosión
|
// Añade una explosión
|
||||||
void add(int x, int y, int size);
|
void add(int x, int y, int size);
|
||||||
|
|||||||
@@ -49,6 +49,11 @@ Game::Game(int playerID, int currentStage, SDL_Renderer *renderer, Screen *scree
|
|||||||
n2500Sprite = new SmartSprite(gameTextTexture);
|
n2500Sprite = new SmartSprite(gameTextTexture);
|
||||||
n5000Sprite = new SmartSprite(gameTextTexture);
|
n5000Sprite = new SmartSprite(gameTextTexture);
|
||||||
|
|
||||||
|
explosions->addTexture(1, explosionsTextures[0], explosionsAnimations[0]);
|
||||||
|
explosions->addTexture(2, explosionsTextures[1], explosionsAnimations[1]);
|
||||||
|
explosions->addTexture(3, explosionsTextures[2], explosionsAnimations[2]);
|
||||||
|
explosions->addTexture(4, explosionsTextures[3], explosionsAnimations[3]);
|
||||||
|
|
||||||
canvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, playArea.w, playArea.h);
|
canvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, playArea.w, playArea.h);
|
||||||
SDL_SetTextureBlendMode(canvas, SDL_BLENDMODE_BLEND);
|
SDL_SetTextureBlendMode(canvas, SDL_BLENDMODE_BLEND);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user