Arreglades les herencies de Sprite

Abans de llevar mil coses que sobren i replantejar-se estes 4 classes
This commit is contained in:
2024-10-13 10:01:07 +02:00
parent 33ea8d90ca
commit b060f21696
17 changed files with 1204 additions and 284 deletions

View File

@@ -3,31 +3,39 @@
// 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)
: 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;
zoom_w_ = 1;
zoom_h_ = 1;
// Establece los valores de rotacion
rotateEnabled_ = false;
rotateSpeed_ = 0;
rotateAmount_ = (double)0;
rotate_.enabled = false;
rotate_.speed = 0;
rotate_.angle = 0.0f;
rotate_.amount = 0.0f;
rotate_.center = nullptr;
// 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;
sprite_clip_ = (SDL_Rect){0, 0, w, h};
// Establece el tipo de volteado
currentFlip_ = SDL_FLIP_NONE;
flip_ = SDL_FLIP_NONE;
};
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture)
: Sprite(texture)
{
clear();
};
// Reinicia todas las variables
@@ -42,17 +50,21 @@ 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
zoomW_ = 1.0f; // Zoom aplicado a la anchura
zoomH_ = 1.0f; // Zoom aplicado a la altura
zoom_w_ = 1.0f; // Zoom aplicado a la anchura
zoom_h_ = 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
rotate_.enabled = false; // Indica si ha de rotar
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
currentFlip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
counter_ = 0; // Contador interno
flip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
setPos((SDL_Rect){0, 0, 0, 0});
setSpriteClip((SDL_Rect){0, 0, 0, 0});
}
// Mueve el sprite
@@ -68,10 +80,7 @@ void MovingSprite::move()
// Muestra el sprite por pantalla
void MovingSprite::render()
{
if (enabled_)
{
texture_->render((int)x_, (int)y_, &spriteClip_, zoomW_, zoomH_, angle_, center_, currentFlip_);
}
texture_->render((int)x_, (int)y_, &sprite_clip_, zoom_w_, zoom_h_, (double)rotate_.angle, rotate_.center, flip_);
}
// Obtiene el valor de la variable
@@ -113,28 +122,28 @@ float MovingSprite::getAccelY() const
// Obtiene el valor de la variable
float MovingSprite::getZoomW() const
{
return zoomW_;
return zoom_w_;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomH() const
{
return zoomH_;
return zoom_h_;
}
// Obtiene el valor de la variable
double MovingSprite::getAngle() const
float MovingSprite::getAngle() const
{
return angle_;
return rotate_.angle;
}
// Establece la posición y_ el tamaño del objeto
void MovingSprite::setRect(SDL_Rect rect)
void MovingSprite::setPos(SDL_Rect rect)
{
x_ = (float)rect.x;
y_ = (float)rect.y;
w_ = rect.w;
h_ = rect.h;
pos_ = rect;
}
// Establece el valor de las variables
@@ -142,18 +151,23 @@ void MovingSprite::setPos(float x, float y)
{
x_ = x;
y_ = y;
pos_.x = (int)x;
pos_.y = (int)y;
}
// Establece el valor de la variable
void MovingSprite::setPosX(float value)
{
x_ = value;
pos_.x = (int)x_;
}
// Establece el valor de la variable
void MovingSprite::setPosY(float value)
{
y_ = value;
pos_.y = (int)y_;
}
// Establece el valor de la variable
@@ -183,53 +197,53 @@ void MovingSprite::setAccelY(float value)
// Establece el valor de la variable
void MovingSprite::setZoomW(float value)
{
zoomW_ = value;
zoom_w_ = value;
}
// Establece el valor de la variable
void MovingSprite::setZoomH(float value)
{
zoomH_ = value;
zoom_h_ = value;
}
// Establece el valor de la variable
void MovingSprite::setAngle(double value)
{
angle_ = value;
rotate_.angle = value;
}
// Incrementa el valor de la variable
void MovingSprite::incAngle(double value)
{
angle_ += value;
rotate_.angle += value;
}
// Decrementa el valor de la variable
void MovingSprite::decAngle(double value)
{
angle_ -= value;
rotate_.angle -= value;
}
// Obtiene el valor de la variable
bool MovingSprite::getRotate() const
{
return rotateEnabled_;
return rotate_.enabled;
}
// Obtiene el valor de la variable
Uint16 MovingSprite::getRotateSpeed() const
{
return rotateSpeed_;
return rotate_.speed;
}
// Establece la rotacion
void MovingSprite::rotate()
{
if (rotateEnabled_)
if (rotate_.enabled)
{
if (counter_ % rotateSpeed_ == 0)
if (counter_ % rotate_.speed == 0)
{
incAngle(rotateAmount_);
incAngle(rotate_.amount);
}
}
}
@@ -237,65 +251,62 @@ void MovingSprite::rotate()
// Establece el valor de la variable
void MovingSprite::setRotate(bool value)
{
rotateEnabled_ = value;
rotate_.enabled = value;
}
// Establece el valor de la variable
void MovingSprite::setRotateSpeed(int value)
{
rotateSpeed_ = (value < 1) ? 1 : value;
rotate_.speed = (value < 1) ? 1 : value;
}
// Establece el valor de la variable
void MovingSprite::setRotateAmount(double value)
{
rotateAmount_ = value;
rotate_.amount = value;
}
// Establece el valor de la variable
void MovingSprite::disableRotate()
{
rotateEnabled_ = false;
angle_ = (double)0;
rotate_.enabled = false;
rotate_.angle = 0.0f;
}
// Actualiza las variables internas del objeto
void MovingSprite::update()
{
if (enabled_)
{
move();
rotate();
++counter_ %= 60000;
}
move();
rotate();
++counter_ %= 60000;
}
// Cambia el sentido de la rotación
void MovingSprite::switchRotate()
{
rotateAmount_ *= -1;
rotate_.amount *= -1;
}
// Establece el valor de la variable
void MovingSprite::setFlip(SDL_RendererFlip flip)
{
currentFlip_ = flip;
flip_ = flip;
}
// Gira el sprite horizontalmente
void MovingSprite::flip()
{
currentFlip_ = (currentFlip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
}
// Obtiene el valor de la variable
SDL_RendererFlip MovingSprite::getFlip()
{
return currentFlip_;
return flip_;
}
// Devuelve el rectangulo donde está el sprite
SDL_Rect MovingSprite::getRect()
SDL_Rect MovingSprite::getPos() const
{
return (SDL_Rect){(int)x_, (int)y_, w_, h_};
return (SDL_Rect){(int)x_, (int)y_, pos_.w, pos_.h};
}