Files
jail_engine/units/movingsprite.cpp

424 lines
7.3 KiB
C++

#include "movingsprite.h"
#include <iostream>
// Constructor
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture, SDL_Renderer *renderer)
{
//std::cout << "Construido MovingSprite" << std::endl;
// Copia los punteros
this->texture = texture;
this->renderer = renderer;
// Establece el alto y el ancho del sprite
this->w = w;
this->h = h;
// Establece la posición X,Y del sprite
this->x = x;
this->y = y;
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;
// 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;
currentFlipH = false;
currentFlipV = false;
};
// Destructor
MovingSprite::~MovingSprite()
{
//std::cout << "Destruido MovingSprite" << std::endl;
}
// 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()
{
if (enabled)
{
xPrev = x;
yPrev = y;
x += vx;
y += vy;
vx += ax;
vy += ay;
}
}
// Muestra el sprite por pantalla
void MovingSprite::render()
{
if (enabled)
{
texture->render(renderer, (int)x, (int)y, &spriteClip, zoomW, zoomH, angle, center, currentFlip);
}
}
// Obtiene el valor de la variable
float MovingSprite::getPosX()
{
return x;
}
// Obtiene el valor de la variable
float MovingSprite::getPosY()
{
return y;
}
// Obtiene el valor de la variable
float MovingSprite::getVelX()
{
return vx;
}
// Obtiene el valor de la variable
float MovingSprite::getVelY()
{
return vy;
}
// Obtiene el valor de la variable
float MovingSprite::getAccelX()
{
return ax;
}
// Obtiene el valor de la variable
float MovingSprite::getAccelY()
{
return ay;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomW()
{
return zoomW;
}
// Obtiene el valor de la variable
float MovingSprite::getZoomH()
{
return zoomH;
}
// Obtiene el valor de la variable
double MovingSprite::getAngle()
{
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 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()
{
return rotateEnabled;
}
// Obtiene el valor de la variable
Uint16 MovingSprite::getRotateSpeed()
{
return rotateSpeed;
}
// Establece la rotacion
void MovingSprite::rotate()
{
if (enabled)
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)
{
if (value < 1)
{
rotateSpeed = 1;
}
else
{
rotateSpeed = 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();
if (enabled)
{
++counter %= 60000;
}
}
// Cambia el sentido de la rotación
void MovingSprite::switchRotate()
{
rotateAmount *= -1;
}
// Actualiza el valor de la variable
void MovingSprite::updateCurrentFlip()
{
if (currentFlipH && currentFlipV)
{
currentFlip = SDL_RendererFlip(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL);
}
else if (currentFlipH && !currentFlipV)
{
currentFlip = SDL_FLIP_HORIZONTAL;
}
else if (!currentFlipH && currentFlipV)
{
currentFlip = SDL_FLIP_VERTICAL;
}
else if (!currentFlipH && !currentFlipV)
{
currentFlip = SDL_FLIP_NONE;
}
}
// Establece el valor de la variable
void MovingSprite::setFlipH(bool flip)
{
currentFlipH = flip;
updateCurrentFlip();
}
// Gira el sprite horizontalmente
void MovingSprite::flipH()
{
currentFlipH = !currentFlipH;
updateCurrentFlip();
}
// Establece el valor de la variable
void MovingSprite::setFlipV(bool flip)
{
currentFlipV = flip;
updateCurrentFlip();
}
// Voltea el sprite verticalmente
void MovingSprite::flipV()
{
currentFlipV = !currentFlipV;
updateCurrentFlip();
}
// Obtiene el valor de la variable
SDL_RendererFlip MovingSprite::getFlip()
{
return currentFlip;
}
// Obtiene el valor de la variable
bool MovingSprite::getFlipH()
{
return currentFlipH;
}
// Obtiene el valor de la variable
bool MovingSprite::getFlipV()
{
return currentFlipV;
}
// 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;
}