Commit pa que Mon arregle el codi mentre em dutxe

This commit is contained in:
2024-10-09 21:48:01 +02:00
parent 3c1dcad3ab
commit f2fa216b0d
34 changed files with 862 additions and 1218 deletions

View File

@@ -2,294 +2,258 @@
#include "texture.h" // for Texture
// Constructor
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture)
: x(x), y(y)
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)
{
// Copia los punteros
this->texture = texture;
// Establece el alto y el ancho del sprite
this->w = w;
this->h = h;
// Establece la posición X,Y del sprite
xPrev = x;
yPrev = y;
// Establece la velocidad X,Y del sprite
vx = velx;
vy = vely;
// Establece la aceleración X,Y del sprite
ax = accelx;
ay = accely;
// Establece el zoom W,H del sprite
zoomW = 1;
zoomH = 1;
zoomW_ = 1;
zoomH_ = 1;
// Establece el angulo con el que se dibujará
angle = (double)0;
angle_ = (double)0;
// Establece los valores de rotacion
rotateEnabled = false;
rotateSpeed = 0;
rotateAmount = (double)0;
rotateEnabled_ = false;
rotateSpeed_ = 0;
rotateAmount_ = (double)0;
// Contador interno
counter = 0;
counter_ = 0;
// Establece el rectangulo de donde coger la imagen
spriteClip = {0, 0, w, h};
spriteClip_ = {0, 0, w_, h_};
// Establece el centro de rotación
center = nullptr;
center_ = nullptr;
// Establece el tipo de volteado
currentFlip = SDL_FLIP_NONE;
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
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
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
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
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
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
currentFlip_ = SDL_FLIP_NONE; // Establece como se ha de voltear el sprite
}
// Mueve el sprite
void MovingSprite::move()
{
if (enabled)
{
xPrev = x;
yPrev = y;
x_ += vx_;
y_ += vy_;
x += vx;
y += vy;
vx += ax;
vy += ay;
}
vx_ += ax_;
vy_ += ay_;
}
// 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_, &spriteClip_, zoomW_, zoomH_, angle_, center_, currentFlip_);
}
// Obtiene el valor de la variable
float MovingSprite::getPosX()
float MovingSprite::getPosX() const
{
return x;
return x_;
}
// Obtiene el valor de la variable
float MovingSprite::getPosY()
float MovingSprite::getPosY() const
{
return y;
return y_;
}
// Obtiene el valor de la variable
float MovingSprite::getVelX()
float MovingSprite::getVelX() const
{
return vx;
return vx_;
}
// Obtiene el valor de la variable
float MovingSprite::getVelY()
float MovingSprite::getVelY() const
{
return vy;
return vy_;
}
// Obtiene el valor de la variable
float MovingSprite::getAccelX()
float MovingSprite::getAccelX() const
{
return ax;
return ax_;
}
// Obtiene el valor de la variable
float MovingSprite::getAccelY()
float MovingSprite::getAccelY() const
{
return ay;
return ay_;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomW()
float MovingSprite::getZoomW() const
{
return zoomW;
return zoomW_;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomH()
float MovingSprite::getZoomH() const
{
return zoomH;
return zoomH_;
}
// Obtiene el valor de la variable
double MovingSprite::getAngle()
double MovingSprite::getAngle() const
{
return angle;
return angle_;
}
// Establece la posición y el tamaño del objeto
// 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;
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)
{
this->x = x;
this->y = y;
x_ = x;
y_ = y;
}
// Establece el valor de la variable
void MovingSprite::setPosX(float value)
{
x = value;
x_ = value;
}
// Establece el valor de la variable
void MovingSprite::setPosY(float value)
{
y = value;
y_ = value;
}
// Establece el valor de la variable
void MovingSprite::setVelX(float value)
{
vx = value;
vx_ = value;
}
// Establece el valor de la variable
void MovingSprite::setVelY(float value)
{
vy = value;
vy_ = value;
}
// Establece el valor de la variable
void MovingSprite::setAccelX(float value)
{
ax = value;
ax_ = value;
}
// Establece el valor de la variable
void MovingSprite::setAccelY(float value)
{
ay = value;
ay_ = value;
}
// Establece el valor de la variable
void MovingSprite::setZoomW(float value)
{
zoomW = value;
zoomW_ = value;
}
// Establece el valor de la variable
void MovingSprite::setZoomH(float value)
{
zoomH = value;
zoomH_ = value;
}
// Establece el valor de la variable
void MovingSprite::setAngle(double value)
{
angle = value;
angle_ = value;
}
// Incrementa el valor de la variable
void MovingSprite::incAngle(double value)
{
angle += value;
angle_ += value;
}
// Decrementa el valor de la variable
void MovingSprite::decAngle(double value)
{
angle -= value;
angle_ -= value;
}
// Obtiene el valor de la variable
bool MovingSprite::getRotate()
bool MovingSprite::getRotate() const
{
return rotateEnabled;
return rotateEnabled_;
}
// Obtiene el valor de la variable
Uint16 MovingSprite::getRotateSpeed()
Uint16 MovingSprite::getRotateSpeed() const
{
return rotateSpeed;
return rotateSpeed_;
}
// Establece la rotacion
void MovingSprite::rotate()
{
if (enabled)
if (rotateEnabled)
if (rotateEnabled_)
{
if (counter_ % rotateSpeed_ == 0)
{
if (counter % rotateSpeed == 0)
{
incAngle(rotateAmount);
}
incAngle(rotateAmount_);
}
}
}
// Establece el valor de la variable
void MovingSprite::setRotate(bool value)
{
rotateEnabled = value;
rotateEnabled_ = value;
}
// Establece el valor de la variable
void MovingSprite::setRotateSpeed(int value)
{
if (value < 1)
{
rotateSpeed = 1;
}
else
{
rotateSpeed = value;
}
rotateSpeed_ = (value < 1) ? 1 : value;
}
// Establece el valor de la variable
void MovingSprite::setRotateAmount(double value)
{
rotateAmount = value;
rotateAmount_ = value;
}
// Establece el valor de la variable
void MovingSprite::disableRotate()
{
rotateEnabled = false;
angle = (double)0;
rotateEnabled_ = false;
angle_ = (double)0;
}
// Actualiza las variables internas del objeto
@@ -297,71 +261,35 @@ void MovingSprite::update()
{
move();
rotate();
if (enabled)
{
++counter %= 60000;
}
++counter_ %= 60000;
}
// Cambia el sentido de la rotación
void MovingSprite::switchRotate()
{
rotateAmount *= -1;
rotateAmount_ *= -1;
}
// Establece el valor de la variable
void MovingSprite::setFlip(SDL_RendererFlip flip)
{
currentFlip = flip;
currentFlip_ = flip;
}
// Gira el sprite horizontalmente
void MovingSprite::flip()
{
currentFlip = (currentFlip == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
currentFlip_ = (currentFlip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
}
// Obtiene el valor de la variable
SDL_RendererFlip MovingSprite::getFlip()
{
return currentFlip;
return currentFlip_;
}
// Devuelve el rectangulo donde está el sprite
SDL_Rect MovingSprite::getRect()
{
const SDL_Rect rect = {(int)x, (int)y, w, h};
return rect;
}
// Deshace el último movimiento
void MovingSprite::undoMove()
{
x = xPrev;
y = yPrev;
}
// Deshace el último movimiento en el eje X
void MovingSprite::undoMoveX()
{
x = xPrev;
}
// Deshace el último movimiento en el eje Y
void MovingSprite::undoMoveY()
{
y = yPrev;
}
// Pone a cero las velocidades de desplacamiento
void MovingSprite::clearVel()
{
vx = vy = 0.0f;
}
// Devuelve el incremento en el eje X en pixels
int MovingSprite::getIncX()
{
return (int)x - (int)xPrev;
return (SDL_Rect){(int)x_, (int)y_, w_, h_};
}