295 lines
5.5 KiB
C++
295 lines
5.5 KiB
C++
#include "moving_sprite.h"
|
|
#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)
|
|
{
|
|
// Establece el zoom W,H del sprite
|
|
zoomW_ = 1;
|
|
zoomH_ = 1;
|
|
|
|
// Establece el angulo con el que se dibujará
|
|
angle_ = (double)0;
|
|
|
|
// Establece los valores de rotacion
|
|
rotateEnabled_ = false;
|
|
rotateSpeed_ = 0;
|
|
rotateAmount_ = (double)0;
|
|
|
|
// Contador interno
|
|
counter_ = 0;
|
|
|
|
// Establece el rectangulo de donde coger la imagen
|
|
spriteClip_ = {0, 0, w_, h_};
|
|
|
|
// Establece el centro de rotación
|
|
center_ = nullptr;
|
|
|
|
// Establece el tipo de volteado
|
|
currentFlip_ = SDL_FLIP_NONE;
|
|
};
|
|
|
|
// 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
|
|
|
|
zoomW_ = 1.0f; // Zoom aplicado a la anchura
|
|
zoomH_ = 1.0f; // Zoom aplicado a la altura
|
|
|
|
angle_ = 0.0; // Angulo para dibujarlo
|
|
rotateEnabled_ = false; // Indica si ha de rotar
|
|
center_ = nullptr; // Centro de rotación
|
|
rotateSpeed_ = 0; // Velocidad de giro
|
|
rotateAmount_ = 0.0; // Cantidad de grados a girar en cada iteración
|
|
counter_ = 0; // Contador interno
|
|
|
|
currentFlip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
|
|
}
|
|
|
|
// Mueve el sprite
|
|
void MovingSprite::move()
|
|
{
|
|
x_ += vx_;
|
|
y_ += vy_;
|
|
|
|
vx_ += ax_;
|
|
vy_ += ay_;
|
|
}
|
|
|
|
// Muestra el sprite por pantalla
|
|
void MovingSprite::render()
|
|
{
|
|
texture_->render((int)x_, (int)y_, &spriteClip_, zoomW_, zoomH_, angle_, center_, currentFlip_);
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getPosX() const
|
|
{
|
|
return x_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getPosY() const
|
|
{
|
|
return y_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getVelX() const
|
|
{
|
|
return vx_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getVelY() const
|
|
{
|
|
return vy_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getAccelX() const
|
|
{
|
|
return ax_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getAccelY() const
|
|
{
|
|
return ay_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getZoomW() const
|
|
{
|
|
return zoomW_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
float MovingSprite::getZoomH() const
|
|
{
|
|
return zoomH_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
double MovingSprite::getAngle() const
|
|
{
|
|
return angle_;
|
|
}
|
|
|
|
// Establece la posición y_ el tamaño del objeto
|
|
void MovingSprite::setRect(SDL_Rect rect)
|
|
{
|
|
x_ = (float)rect.x;
|
|
y_ = (float)rect.y;
|
|
w_ = rect.w;
|
|
h_ = rect.h;
|
|
}
|
|
|
|
// Establece el valor de las variables
|
|
void MovingSprite::setPos(float x, float y)
|
|
{
|
|
x_ = x;
|
|
y_ = y;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setPosX(float value)
|
|
{
|
|
x_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setPosY(float value)
|
|
{
|
|
y_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setVelX(float value)
|
|
{
|
|
vx_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setVelY(float value)
|
|
{
|
|
vy_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setAccelX(float value)
|
|
{
|
|
ax_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setAccelY(float value)
|
|
{
|
|
ay_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setZoomW(float value)
|
|
{
|
|
zoomW_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setZoomH(float value)
|
|
{
|
|
zoomH_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setAngle(double value)
|
|
{
|
|
angle_ = value;
|
|
}
|
|
|
|
// Incrementa el valor de la variable
|
|
void MovingSprite::incAngle(double value)
|
|
{
|
|
angle_ += value;
|
|
}
|
|
|
|
// Decrementa el valor de la variable
|
|
void MovingSprite::decAngle(double value)
|
|
{
|
|
angle_ -= value;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
bool MovingSprite::getRotate() const
|
|
{
|
|
return rotateEnabled_;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
Uint16 MovingSprite::getRotateSpeed() const
|
|
{
|
|
return rotateSpeed_;
|
|
}
|
|
|
|
// Establece la rotacion
|
|
void MovingSprite::rotate()
|
|
{
|
|
if (rotateEnabled_)
|
|
{
|
|
if (counter_ % rotateSpeed_ == 0)
|
|
{
|
|
incAngle(rotateAmount_);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setRotate(bool value)
|
|
{
|
|
rotateEnabled_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setRotateSpeed(int value)
|
|
{
|
|
rotateSpeed_ = (value < 1) ? 1 : value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setRotateAmount(double value)
|
|
{
|
|
rotateAmount_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::disableRotate()
|
|
{
|
|
rotateEnabled_ = false;
|
|
angle_ = (double)0;
|
|
}
|
|
|
|
// Actualiza las variables internas del objeto
|
|
void MovingSprite::update()
|
|
{
|
|
move();
|
|
rotate();
|
|
++counter_ %= 60000;
|
|
}
|
|
|
|
// Cambia el sentido de la rotación
|
|
void MovingSprite::switchRotate()
|
|
{
|
|
rotateAmount_ *= -1;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void MovingSprite::setFlip(SDL_RendererFlip flip)
|
|
{
|
|
currentFlip_ = flip;
|
|
}
|
|
|
|
// Gira el sprite horizontalmente
|
|
void MovingSprite::flip()
|
|
{
|
|
currentFlip_ = (currentFlip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
SDL_RendererFlip MovingSprite::getFlip()
|
|
{
|
|
return currentFlip_;
|
|
}
|
|
|
|
// Devuelve el rectangulo donde está el sprite
|
|
SDL_Rect MovingSprite::getRect()
|
|
{
|
|
return (SDL_Rect){(int)x_, (int)y_, w_, h_};
|
|
} |