94 lines
2.4 KiB
C++
94 lines
2.4 KiB
C++
#include "sprite/surface_moving_sprite.h"
|
|
|
|
#include "surface.h" // Para Surface
|
|
|
|
// Constructor
|
|
SMovingSprite::SMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos, SDL_FlipMode flip)
|
|
: SSprite(surface, pos),
|
|
x_(pos.x),
|
|
y_(pos.y),
|
|
flip_(flip) { SSprite::pos_ = pos; }
|
|
|
|
SMovingSprite::SMovingSprite(std::shared_ptr<Surface> surface, SDL_FRect pos)
|
|
: SSprite(surface, pos),
|
|
x_(pos.x),
|
|
y_(pos.y),
|
|
flip_(SDL_FLIP_NONE) { SSprite::pos_ = pos; }
|
|
|
|
SMovingSprite::SMovingSprite(std::shared_ptr<Surface> 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<int>(x_);
|
|
pos_.y = static_cast<int>(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_FRect rect) {
|
|
x_ = static_cast<float>(rect.x);
|
|
y_ = static_cast<float>(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<int>(x_);
|
|
pos_.y = static_cast<int>(y_);
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void SMovingSprite::setPosX(float value) {
|
|
x_ = value;
|
|
pos_.x = static_cast<int>(x_);
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void SMovingSprite::setPosY(float value) {
|
|
y_ = value;
|
|
pos_.y = static_cast<int>(y_);
|
|
} |