#include "s_moving_sprite.h" #include "surface.h" // for Surface // Constructor SMovingSprite::SMovingSprite(std::shared_ptr surface, SDL_Rect pos, SDL_RendererFlip flip) : SSprite(surface, pos), x_(pos.x), y_(pos.y), flip_(flip) { SSprite::pos_ = pos; } SMovingSprite::SMovingSprite(std::shared_ptr surface, SDL_Rect pos) : SSprite(surface, pos), x_(pos.x), y_(pos.y), flip_(SDL_FLIP_NONE) { SSprite::pos_ = pos; } SMovingSprite::SMovingSprite(std::shared_ptr surface) : SSprite(surface), x_(0.0f), y_(0.0f), flip_(SDL_FLIP_NONE) { SSprite::clear(); } // Reinicia todas las variables void SMovingSprite::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 flip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite SSprite::clear(); } // Mueve el sprite void SMovingSprite::move() { x_ += vx_; y_ += vy_; vx_ += ax_; vy_ += ay_; pos_.x = static_cast(x_); pos_.y = static_cast(y_); } // Actualiza las variables internas del objeto void SMovingSprite::update() { move(); } // Muestra el sprite por pantalla void SMovingSprite::render() { surface_->render(pos_.x, pos_.y, &clip_, flip_); } // Muestra el sprite por pantalla void SMovingSprite::render(Uint8 source_color, Uint8 target_color) { surface_->renderWithColorReplace(pos_.x, pos_.y, source_color, target_color, &clip_, flip_); } // Establece la posición y_ el tamaño del objeto void SMovingSprite::setPos(SDL_Rect rect) { x_ = static_cast(rect.x); y_ = static_cast(rect.y); pos_ = rect; } // Establece el valor de las variables void SMovingSprite::setPos(float x, float y) { x_ = x; y_ = y; pos_.x = static_cast(x_); pos_.y = static_cast(y_); } // Establece el valor de la variable void SMovingSprite::setPosX(float value) { x_ = value; pos_.x = static_cast(x_); } // Establece el valor de la variable void SMovingSprite::setPosY(float value) { y_ = value; pos_.y = static_cast(y_); }