Files
coffee_crisis_arcade_edition/source/smart_sprite.cpp

157 lines
2.7 KiB
C++

#include "smart_sprite.h"
#include "moving_sprite.h" // for MovingSprite
class Texture;
// Constructor
SmartSprite::SmartSprite(std::shared_ptr<Texture> texture)
{
// Copia punteros
setTexture(texture);
init();
}
// Inicializa el objeto
void SmartSprite::init()
{
finishedCounter_ = 0;
onDestination_ = false;
destX_ = 0;
destY_ = 0;
finished_ = false;
enabled_ = false;
}
// Actualiza la posición y comprueba si ha llegado a su destino
void SmartSprite::update()
{
if (enabled_)
{
MovingSprite::update();
checkMove();
checkFinished();
}
}
// Establece el valor de la variable
void SmartSprite::setFinishedCounter(int value)
{
finishedCounter_ = value;
}
// Establece el valor de la variable
void SmartSprite::setDestX(int x)
{
destX_ = x;
}
// Establece el valor de la variable
void SmartSprite::setDestY(int y)
{
destY_ = y;
}
// Obtiene el valor de la variable
int SmartSprite::getDestX() const
{
return destX_;
}
// Obtiene el valor de la variable
int SmartSprite::getDestY() const
{
return destY_;
}
// Comprueba el movimiento
void SmartSprite::checkMove()
{
// Comprueba si se desplaza en el eje X hacia la derecha
if (getAccelX() > 0 || getVelX() > 0)
{
// Comprueba si ha llegado al destino
if (getPosX() > destX_)
{
// Lo coloca en posición
setPosX(destX_);
// Lo detiene
setVelX(0.0f);
setAccelX(0.0f);
}
}
// Comprueba si se desplaza en el eje X hacia la izquierda
else if (getAccelX() < 0 || getVelX() < 0)
{
// Comprueba si ha llegado al destino
if (getPosX() < destX_)
{
// Lo coloca en posición
setPosX(destX_);
// Lo detiene
setVelX(0.0f);
setAccelX(0.0f);
}
}
// Comprueba si se desplaza en el eje Y hacia abajo
if (getAccelY() > 0 || getVelY() > 0)
{
// Comprueba si ha llegado al destino
if (getPosY() > destY_)
{
// Lo coloca en posición
setPosY(destY_);
// Lo detiene
setVelY(0.0f);
setAccelY(0.0f);
}
}
// Comprueba si se desplaza en el eje Y hacia arriba
else if (getAccelY() < 0 || getVelY() < 0)
{
// Comprueba si ha llegado al destino
if (getPosY() < destY_)
{
// Lo coloca en posición
setPosY(destY_);
// Lo detiene
setVelY(0.0f);
setAccelY(0.0f);
}
}
}
// Comprueba si ha terminado
void SmartSprite::checkFinished()
{
// Comprueba si ha llegado a su destino
onDestination_ = (getPosX() == destX_ && getPosY() == destY_) ? true : false;
if (onDestination_)
{
if (finishedCounter_ == 0)
{
finished_ = true;
}
else
{
--finishedCounter_;
}
}
}
// Obtiene el valor de la variable
bool SmartSprite::isOnDestination() const
{
return onDestination_;
}
// Obtiene el valor de la variable
bool SmartSprite::hasFinished() const
{
return finished_;
}