Commit de vesprà tirada a la brossa
This commit is contained in:
@@ -2,34 +2,26 @@
|
||||
#include "texture.h" // for Texture
|
||||
|
||||
// Constructor
|
||||
MovingSprite::MovingSprite(float x, float y, int w, int h, float vx, float vy, float ax, float ay, std::shared_ptr<Texture> texture)
|
||||
: Sprite((int)x, (int)y, w, h, texture),
|
||||
x_(x),
|
||||
y_(y),
|
||||
vx_(vx),
|
||||
vy_(vy),
|
||||
ax_(ax),
|
||||
ay_(ay),
|
||||
zoom_w_(1),
|
||||
zoom_h_(1),
|
||||
counter_(0),
|
||||
flip_(SDL_FLIP_NONE)
|
||||
{
|
||||
// Establece los valores de rotacion
|
||||
rotate_.enabled = false;
|
||||
rotate_.speed = 0;
|
||||
rotate_.angle = 0.0f;
|
||||
rotate_.amount = 0.0f;
|
||||
rotate_.center = nullptr;
|
||||
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture, SDL_Rect pos, Rotate rotate, float zoom_w, float zoom_h, SDL_RendererFlip flip)
|
||||
: Sprite(texture, pos),
|
||||
rotate_(rotate),
|
||||
zoom_w_(zoom_w),
|
||||
zoom_h_(zoom_h),
|
||||
flip_(flip) {}
|
||||
|
||||
sprite_clip_ = (SDL_Rect){0, 0, w, h};
|
||||
};
|
||||
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture, SDL_Rect pos)
|
||||
: Sprite(texture, pos),
|
||||
rotate_({false, 0, 0, 0.0f, 0.0f, nullptr}),
|
||||
zoom_w_(1.0f),
|
||||
zoom_h_(1.0f),
|
||||
flip_(SDL_FLIP_NONE) {}
|
||||
|
||||
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture)
|
||||
: Sprite(texture)
|
||||
{
|
||||
clear();
|
||||
};
|
||||
: Sprite(texture),
|
||||
rotate_({false, 0, 0, 0.0f, 0.0f, nullptr}),
|
||||
zoom_w_(1.0f),
|
||||
zoom_h_(1.0f),
|
||||
flip_(SDL_FLIP_NONE) {}
|
||||
|
||||
// Reinicia todas las variables
|
||||
void MovingSprite::clear()
|
||||
@@ -43,16 +35,15 @@ void MovingSprite::clear()
|
||||
ax_ = 0.0f; // Aceleración en el eje X. Variación de la velocidad
|
||||
ay_ = 0.0f; // Aceleración en el eje Y. Variación de la velocidad
|
||||
|
||||
zoom_w_ = 1.0f; // Zoom aplicado a la anchura
|
||||
zoom_h_ = 1.0f; // Zoom aplicado a la altura
|
||||
|
||||
rotate_.enabled = false; // Indica si ha de rotar
|
||||
rotate_.counter = 0; // Contador
|
||||
rotate_.speed = 0; // Velocidad de giro
|
||||
rotate_.angle = 0.0f; // Angulo para dibujarlo
|
||||
rotate_.amount = 0.0f; // Cantidad de grados a girar en cada iteración
|
||||
rotate_.center = nullptr; // Centro de rotación
|
||||
|
||||
counter_ = 0; // Contador interno
|
||||
zoom_w_ = 1.0f; // Zoom aplicado a la anchura
|
||||
zoom_h_ = 1.0f; // Zoom aplicado a la altura
|
||||
|
||||
flip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
|
||||
|
||||
@@ -70,10 +61,135 @@ void MovingSprite::move()
|
||||
vy_ += ay_;
|
||||
}
|
||||
|
||||
// Actualiza las variables internas del objeto
|
||||
void MovingSprite::update()
|
||||
{
|
||||
move();
|
||||
rotate();
|
||||
}
|
||||
|
||||
// Muestra el sprite por pantalla
|
||||
void MovingSprite::render()
|
||||
{
|
||||
texture_->render((int)x_, (int)y_, &sprite_clip_, zoom_w_, zoom_h_, (double)rotate_.angle, rotate_.center, flip_);
|
||||
texture_->render(pos_.x, pos_.y, &sprite_clip_, zoom_w_, zoom_h_, rotate_.angle, rotate_.center, flip_);
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getZoomW() const
|
||||
{
|
||||
return zoom_w_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getZoomH() const
|
||||
{
|
||||
return zoom_h_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
double MovingSprite::getAngle() const
|
||||
{
|
||||
return rotate_.angle;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setZoomW(float value)
|
||||
{
|
||||
zoom_w_ = value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setZoomH(float value)
|
||||
{
|
||||
zoom_h_ = value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setAngle(double value)
|
||||
{
|
||||
rotate_.angle = value;
|
||||
}
|
||||
|
||||
// Incrementa el valor del ángulo
|
||||
void MovingSprite::updateAngle()
|
||||
{
|
||||
rotate_.angle += rotate_.amount;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool MovingSprite::isRotating() const
|
||||
{
|
||||
return rotate_.enabled;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
int MovingSprite::getRotateSpeed() const
|
||||
{
|
||||
return rotate_.speed;
|
||||
}
|
||||
|
||||
// Establece la rotacion
|
||||
void MovingSprite::rotate()
|
||||
{
|
||||
if (rotate_.enabled)
|
||||
{
|
||||
++rotate_.counter;
|
||||
if (rotate_.counter % rotate_.speed == 0)
|
||||
{
|
||||
updateAngle();
|
||||
rotate_.counter = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::enableRotate()
|
||||
{
|
||||
rotate_.enabled = true;
|
||||
rotate_.counter = 0;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::disableRotate()
|
||||
{
|
||||
rotate_.enabled = false;
|
||||
rotate_.counter = 0;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setRotateSpeed(int value)
|
||||
{
|
||||
rotate_.speed = std::max(1, value);
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setRotateAmount(double value)
|
||||
{
|
||||
rotate_.amount = value;
|
||||
}
|
||||
|
||||
// Cambia el sentido de la rotación
|
||||
void MovingSprite::switchRotate()
|
||||
{
|
||||
rotate_.amount *= -1;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setFlip(SDL_RendererFlip flip)
|
||||
{
|
||||
flip_ = flip;
|
||||
}
|
||||
|
||||
// Gira el sprite horizontalmente
|
||||
void MovingSprite::flip()
|
||||
{
|
||||
flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
SDL_RendererFlip MovingSprite::getFlip()
|
||||
{
|
||||
return flip_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
@@ -112,24 +228,6 @@ float MovingSprite::getAccelY() const
|
||||
return ay_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getZoomW() const
|
||||
{
|
||||
return zoom_w_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getZoomH() const
|
||||
{
|
||||
return zoom_h_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
float MovingSprite::getAngle() const
|
||||
{
|
||||
return rotate_.angle;
|
||||
}
|
||||
|
||||
// Establece la posición y_ el tamaño del objeto
|
||||
void MovingSprite::setPos(SDL_Rect rect)
|
||||
{
|
||||
@@ -186,120 +284,3 @@ void MovingSprite::setAccelY(float value)
|
||||
{
|
||||
ay_ = value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setZoomW(float value)
|
||||
{
|
||||
zoom_w_ = value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setZoomH(float value)
|
||||
{
|
||||
zoom_h_ = value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setAngle(double value)
|
||||
{
|
||||
rotate_.angle = value;
|
||||
}
|
||||
|
||||
// Incrementa el valor de la variable
|
||||
void MovingSprite::incAngle(double value)
|
||||
{
|
||||
rotate_.angle += value;
|
||||
}
|
||||
|
||||
// Decrementa el valor de la variable
|
||||
void MovingSprite::decAngle(double value)
|
||||
{
|
||||
rotate_.angle -= value;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
bool MovingSprite::getRotate() const
|
||||
{
|
||||
return rotate_.enabled;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
Uint16 MovingSprite::getRotateSpeed() const
|
||||
{
|
||||
return rotate_.speed;
|
||||
}
|
||||
|
||||
// Establece la rotacion
|
||||
void MovingSprite::rotate()
|
||||
{
|
||||
if (rotate_.enabled)
|
||||
{
|
||||
if (counter_ % rotate_.speed == 0)
|
||||
{
|
||||
incAngle(rotate_.amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setRotate(bool value)
|
||||
{
|
||||
rotate_.enabled = value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setRotateSpeed(int value)
|
||||
{
|
||||
rotate_.speed = (value < 1) ? 1 : value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setRotateAmount(double value)
|
||||
{
|
||||
rotate_.amount = value;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::disableRotate()
|
||||
{
|
||||
rotate_.enabled = false;
|
||||
rotate_.angle = 0.0f;
|
||||
}
|
||||
|
||||
// Actualiza las variables internas del objeto
|
||||
void MovingSprite::update()
|
||||
{
|
||||
move();
|
||||
rotate();
|
||||
++counter_ %= 60000;
|
||||
}
|
||||
|
||||
// Cambia el sentido de la rotación
|
||||
void MovingSprite::switchRotate()
|
||||
{
|
||||
rotate_.amount *= -1;
|
||||
}
|
||||
|
||||
// Establece el valor de la variable
|
||||
void MovingSprite::setFlip(SDL_RendererFlip flip)
|
||||
{
|
||||
flip_ = flip;
|
||||
}
|
||||
|
||||
// Gira el sprite horizontalmente
|
||||
void MovingSprite::flip()
|
||||
{
|
||||
flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
SDL_RendererFlip MovingSprite::getFlip()
|
||||
{
|
||||
return flip_;
|
||||
}
|
||||
|
||||
// Devuelve el rectangulo donde está el sprite
|
||||
SDL_Rect MovingSprite::getPos() const
|
||||
{
|
||||
return (SDL_Rect){(int)x_, (int)y_, pos_.w, pos_.h};
|
||||
}
|
||||
Reference in New Issue
Block a user