117 lines
2.9 KiB
C++
117 lines
2.9 KiB
C++
#include "moving_sprite.h"
|
|
|
|
#include "texture.h" // Para Texture
|
|
|
|
// Constructor
|
|
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture, SDL_FRect pos, Rotate rotate, float zoom_w, float zoom_h, SDL_FlipMode flip)
|
|
: Sprite(texture, pos),
|
|
x_(pos.x),
|
|
y_(pos.y),
|
|
rotate_(rotate),
|
|
zoom_w_(zoom_w),
|
|
zoom_h_(zoom_h),
|
|
flip_(flip) {}
|
|
|
|
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture, SDL_FRect pos)
|
|
: Sprite(texture, pos),
|
|
x_(pos.x),
|
|
y_(pos.y),
|
|
|
|
zoom_w_(1.0F),
|
|
zoom_h_(1.0F),
|
|
flip_(SDL_FLIP_NONE) {}
|
|
|
|
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture)
|
|
: Sprite(texture),
|
|
|
|
zoom_w_(1.0F),
|
|
zoom_h_(1.0F),
|
|
flip_(SDL_FLIP_NONE) { Sprite::clear(); }
|
|
|
|
// Reinicia todas las variables
|
|
void MovingSprite::clear() {
|
|
x_ = 0.0F; // Posición en el eje X
|
|
y_ = 0.0F; // Posición en el eje Y
|
|
|
|
vx_ = 0.0F; // Velocidad en el eje X. Cantidad de pixeles a desplazarse
|
|
vy_ = 0.0F; // Velocidad en el eje Y. Cantidad de pixeles a desplazarse
|
|
|
|
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
|
|
|
|
rotate_ = Rotate(); // Inicializa la estructura
|
|
|
|
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
|
|
|
|
Sprite::clear();
|
|
}
|
|
|
|
// Mueve el sprite
|
|
void MovingSprite::move() {
|
|
x_ += vx_;
|
|
y_ += vy_;
|
|
|
|
vx_ += ax_;
|
|
vy_ += ay_;
|
|
|
|
pos_.x = static_cast<int>(x_);
|
|
pos_.y = static_cast<int>(y_);
|
|
}
|
|
|
|
// Actualiza las variables internas del objeto
|
|
void MovingSprite::update() {
|
|
move();
|
|
rotate();
|
|
}
|
|
|
|
// Muestra el sprite por pantalla
|
|
void MovingSprite::render() { texture_->render(pos_.x, pos_.y, &sprite_clip_, zoom_w_, zoom_h_, rotate_.angle, &rotate_.center, flip_); }
|
|
|
|
// Establece la rotacion
|
|
void MovingSprite::rotate() {
|
|
if (rotate_.enabled) {
|
|
++rotate_.counter;
|
|
if (rotate_.counter % rotate_.speed == 0) {
|
|
updateAngle();
|
|
rotate_.counter = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Activa o desactiva el efecto de rotación
|
|
void MovingSprite::setRotate(bool enable) {
|
|
rotate_.enabled = enable;
|
|
rotate_.counter = 0;
|
|
}
|
|
|
|
// Establece la posición y_ el tamaño del objeto
|
|
void MovingSprite::setPos(SDL_FRect rect) {
|
|
x_ = rect.x;
|
|
y_ = rect.y;
|
|
|
|
pos_ = rect;
|
|
}
|
|
|
|
// Establece el valor de las variables
|
|
void MovingSprite::setPos(float pos_x, float pos_y) {
|
|
x_ = pos_x;
|
|
y_ = pos_y;
|
|
|
|
pos_.x = static_cast<int>(x_);
|
|
pos_.y = static_cast<int>(y_);
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setPosX(float pos_x) {
|
|
x_ = pos_x;
|
|
pos_.x = static_cast<int>(x_);
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setPosY(float pos_y) {
|
|
y_ = pos_y;
|
|
pos_.y = static_cast<int>(y_);
|
|
} |