Files
coffee_crisis_arcade_edition/source/moving_sprite.h
2025-03-27 09:43:19 +01:00

123 lines
4.1 KiB
C++

#pragma once
#include <SDL3/SDL_rect.h> // Para SDL_FPoint, SDL_FRect
#include <SDL3/SDL_surface.h> // Para SDL_FlipMode
#include <algorithm> // Para max
#include <memory> // Para shared_ptr
#include "sprite.h" // Para Sprite
class Texture; // lines 8-8
// Clase MovingSprite. Añade movimiento y efectos de rotación, zoom y flip al sprite
class MovingSprite : public Sprite
{
public:
struct Rotate
{
bool enabled; // Indica si ha de rotar
int counter; // Contador
int speed; // Velocidad de giro
double angle; // Angulo para dibujarlo
float amount; // Cantidad de grados a girar en cada iteración
SDL_FPoint center; // Centro de rotación
Rotate() : enabled(false), counter(0), speed(1), angle(0.0), amount(0.0f), center({0.0f, 0.0f}) {}
};
protected:
float x_ = 0.0f; // Posición en el eje X
float y_ = 0.0f; // Posición en el eje Y
float vx_ = 0.0f; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
float vy_ = 0.0f; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
float ax_ = 0.0f; // Aceleración en el eje X. Variación de la velocidad
float ay_ = 0.0f; // Aceleración en el eje Y. Variación de la velocidad
Rotate rotate_; // Variables usada para controlar la rotación del sprite
float zoom_w_; // Zoom aplicado a la anchura
float zoom_h_; // Zoom aplicado a la altura
SDL_FlipMode flip_; // Indica como se voltea el sprite
// Incrementa el valor del ángulo
void updateAngle() { rotate_.angle += rotate_.amount; }
// Mueve el sprite
void move();
// Rota el sprite
void rotate();
public:
// Constructor
MovingSprite(std::shared_ptr<Texture> texture, SDL_FRect pos, MovingSprite::Rotate rotate, float zoom_w, float zoom_h, SDL_FlipMode flip);
MovingSprite(std::shared_ptr<Texture> texture, SDL_FRect pos);
explicit MovingSprite(std::shared_ptr<Texture> texture);
// Destructor
virtual ~MovingSprite() override = default;
// Actualiza las variables internas del objeto
virtual void update();
// Reinicia todas las variables a cero
void clear() override;
// Muestra el sprite por pantalla
void render() override;
// Obtiene la variable
float getPosX() const { return x_; }
float getPosY() const { return y_; }
float getVelX() const { return vx_; }
float getVelY() const { return vy_; }
float getAccelX() const { return ax_; }
float getAccelY() const { return ay_; }
// Establece la variable
void setVelX(float value) { vx_ = value; }
void setVelY(float value) { vy_ = value; }
void setAccelX(float value) { ax_ = value; }
void setAccelY(float value) { ay_ = value; }
// Obten el valor de la variable
bool isRotating() const { return rotate_.enabled; }
// Establece el valor de la variable
void setZoomW(float value) { zoom_w_ = value; }
void setZoomH(float value) { zoom_h_ = value; }
// Establece el valor de la variable
void setAngle(double value) { rotate_.angle = value; }
void setRotatingCenter(SDL_FPoint point) { rotate_.center = point; }
// Activa o desactiva el efecto de rotación
void setRotate(bool enable);
// Establece el valor de la variable
void setRotateSpeed(int value) { rotate_.speed = std::max(1, value); }
void setRotateAmount(double value) { rotate_.amount = value; }
// Cambia el sentido de la rotación
void switchRotate() { rotate_.amount *= -1; }
// Establece el valor de la variable
void setFlip(SDL_FlipMode flip) { flip_ = flip; }
// Gira el sprite horizontalmente
void flip() { flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL; }
// Obtiene el valor de la variable
SDL_FlipMode getFlip() { return flip_; }
// Establece la posición y_ el tamaño del objeto
void setPos(SDL_FRect rect);
// Establece el valor de las variables
void setPos(float x, float y);
// Establece el valor de la variable
void setPosX(float value);
// Establece el valor de la variable
void setPosY(float value);
};