Files
coffee_crisis_arcade_edition/source/moving_sprite.h

132 lines
3.7 KiB
C++

#pragma once
#include <SDL2/SDL_rect.h> // for SDL_Rect, SDL_Point
#include <SDL2/SDL_render.h> // for SDL_RendererFlip
#include <SDL2/SDL_stdinc.h> // for Uint16
#include <memory> // for shared_ptr
#include "sprite.h" // for Sprite
class Texture;
// Clase MovingSprite. Añade posicion y velocidad en punto flotante
class MovingSprite : public Sprite
{
protected:
struct Rotate
{
bool enabled; // Indica si ha de rotar
int speed; // Velocidad de giro
float angle; // Angulo para dibujarlo
float amount; // Cantidad de grados a girar en cada iteración
SDL_Point *center; // Centro de rotación
};
float x_; // Posición en el eje X
float y_; // Posición en el eje Y
float vx_; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
float vy_; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
float ax_; // Aceleración en el eje X. Variación de la velocidad
float ay_; // Aceleración en el eje Y. Variación de la velocidad
float zoom_w_; // Zoom aplicado a la anchura
float zoom_h_; // Zoom aplicado a la altura
int counter_; // Contador interno
Rotate rotate_; // Variables usada para controlar la rotación del sprite
SDL_RendererFlip flip_; // Indica como se voltea el sprite
public:
// Constructor
explicit MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, std::shared_ptr<Texture> texture = nullptr);
explicit MovingSprite(std::shared_ptr<Texture> texture = nullptr);
// Destructor
virtual ~MovingSprite() = default;
// Mueve el sprite
void move();
// Rota el sprite
void rotate();
// Actualiza las variables internas del objeto
virtual void update();
// Reinicia todas las variables
void clear();
// Muestra el sprite por pantalla
void render() override;
// Obten el valor de la variable
float getPosX() const;
float getPosY() const;
// Obten el valor de la variable
float getVelX() const;
float getVelY() const;
// Obten el valor de la variable
float getAccelX() const;
float getAccelY() const;
// Obten el valor de la variable
float getZoomW() const;
float getZoomH() const;
// Obten el valor de la variable
float getAngle() const;
bool getRotate() const;
Uint16 getRotateSpeed() const;
// Establece la posición del objeto
void setPos(SDL_Rect rect) override;
void setPos(float x, float y);
// Devuelve el rectangulo donde está el sprite
SDL_Rect getPos() const override;
// Establece el valor de la variable
void setPosX(float value);
void setPosY(float value);
// Establece el valor de la variable
void setVelX(float value);
void setVelY(float value);
// Establece el valor de la variable
void setAccelX(float value);
void setAccelY(float value);
// Establece el valor de la variable
void setZoomW(float value);
void setZoomH(float value);
// Establece el valor de la variable
void setAngle(double vaue);
void incAngle(double value);
void decAngle(double value);
// Establece el valor de la variable
void setRotate(bool value);
void setRotateSpeed(int value);
void setRotateAmount(double value);
// Quita el efecto de rotación y deja el sprite en su angulo inicial.
void disableRotate();
// Cambia el sentido de la rotación
void switchRotate();
// Establece el valor de la variable
void setFlip(SDL_RendererFlip flip);
// Gira el sprite horizontalmente
void flip();
// Obtiene el valor de la variable
SDL_RendererFlip getFlip();
};