treball en curs: correccions de tidy
This commit is contained in:
@@ -7,58 +7,58 @@
|
||||
class Texture;
|
||||
|
||||
// Constructor
|
||||
Item::Item(Uint8 kind, float x, float y, Texture *texture, std::vector<std::string> *animation, SDL_Renderer *renderer) {
|
||||
sprite = new AnimatedSprite(texture, renderer, "", animation);
|
||||
Item::Item(Id id, float x, float y, Texture *texture, std::vector<std::string> *animation, SDL_Renderer *renderer) {
|
||||
sprite_ = new AnimatedSprite(texture, renderer, "", animation);
|
||||
|
||||
this->kind = kind;
|
||||
enabled = true;
|
||||
timeToLive = 600;
|
||||
accelX = 0.0F;
|
||||
floorCollision = false;
|
||||
this->id_ = id;
|
||||
enabled_ = true;
|
||||
time_to_live_ = 600;
|
||||
accel_x_ = 0.0F;
|
||||
floor_collision_ = false;
|
||||
|
||||
if (kind == ITEM_COFFEE_MACHINE) {
|
||||
width = 23;
|
||||
height = 29;
|
||||
posX = (((int)x + (PLAY_AREA_WIDTH / 2)) % (PLAY_AREA_WIDTH - width - 5)) + 2;
|
||||
posY = PLAY_AREA_TOP - height;
|
||||
velX = 0.0F;
|
||||
velY = -0.1F;
|
||||
accelY = 0.1F;
|
||||
collider.r = 10;
|
||||
if (id == Item::Id::COFFEE_MACHINE) {
|
||||
width_ = 23;
|
||||
height_ = 29;
|
||||
pos_x_ = (((int)x + (PLAY_AREA_WIDTH / 2)) % (PLAY_AREA_WIDTH - width_ - 5)) + 2;
|
||||
pos_y_ = PLAY_AREA_TOP - height_;
|
||||
vel_x_ = 0.0F;
|
||||
vel_y_ = -0.1F;
|
||||
accel_y_ = 0.1F;
|
||||
collider_.r = 10;
|
||||
} else {
|
||||
width = 16;
|
||||
height = 16;
|
||||
posX = x;
|
||||
posY = y;
|
||||
velX = -1.0F + ((rand() % 5) * 0.5F);
|
||||
velY = -4.0F;
|
||||
accelY = 0.2F;
|
||||
collider.r = width / 2;
|
||||
width_ = 16;
|
||||
height_ = 16;
|
||||
pos_x_ = x;
|
||||
pos_y_ = y;
|
||||
vel_x_ = -1.0F + ((rand() % 5) * 0.5F);
|
||||
vel_y_ = -4.0F;
|
||||
accel_y_ = 0.2F;
|
||||
collider_.r = width_ / 2;
|
||||
}
|
||||
|
||||
sprite->setPosX(posX);
|
||||
sprite->setPosY(posY);
|
||||
sprite_->setPosX(pos_x_);
|
||||
sprite_->setPosY(pos_y_);
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Item::~Item() {
|
||||
delete sprite;
|
||||
delete sprite_;
|
||||
}
|
||||
|
||||
// Centra el objeto en la posición X
|
||||
void Item::allignTo(int x) {
|
||||
posX = float(x - (width / 2));
|
||||
pos_x_ = float(x - (width_ / 2));
|
||||
|
||||
if (posX < PLAY_AREA_LEFT) {
|
||||
posX = PLAY_AREA_LEFT + 1;
|
||||
} else if ((posX + width) > PLAY_AREA_RIGHT) {
|
||||
posX = float(PLAY_AREA_RIGHT - width - 1);
|
||||
if (pos_x_ < PLAY_AREA_LEFT) {
|
||||
pos_x_ = PLAY_AREA_LEFT + 1;
|
||||
} else if ((pos_x_ + width_) > PLAY_AREA_RIGHT) {
|
||||
pos_x_ = float(PLAY_AREA_RIGHT - width_ - 1);
|
||||
}
|
||||
|
||||
// Posición X,Y del sprite
|
||||
sprite->setPosX(int(posX));
|
||||
sprite->setPosY(int(posY));
|
||||
sprite_->setPosX(int(pos_x_));
|
||||
sprite_->setPosY(int(pos_y_));
|
||||
|
||||
// Alinea el circulo de colisión con el objeto
|
||||
shiftColliders();
|
||||
@@ -68,129 +68,129 @@ void Item::allignTo(int x) {
|
||||
void Item::render() {
|
||||
// Mentre quede temps de sobra (>200) es renderitza sempre; quan està a
|
||||
// punt d'expirar, parpalleja alternant 10 frames visibles i 10 invisibles.
|
||||
if (enabled && (timeToLive > 200 || timeToLive % 20 > 10)) {
|
||||
sprite->render();
|
||||
if (enabled_ && (time_to_live_ > 200 || time_to_live_ % 20 > 10)) {
|
||||
sprite_->render();
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición y estados del objeto
|
||||
void Item::move() {
|
||||
floorCollision = false;
|
||||
floor_collision_ = false;
|
||||
|
||||
// Calcula la nueva posición
|
||||
posX += velX;
|
||||
posY += velY;
|
||||
pos_x_ += vel_x_;
|
||||
pos_y_ += vel_y_;
|
||||
|
||||
// Aplica las aceleraciones a la velocidad
|
||||
velX += accelX;
|
||||
velY += accelY;
|
||||
vel_x_ += accel_x_;
|
||||
vel_y_ += accel_y_;
|
||||
|
||||
// Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido
|
||||
if ((posX < PLAY_AREA_LEFT) || (posX + width > PLAY_AREA_RIGHT)) {
|
||||
if ((pos_x_ < PLAY_AREA_LEFT) || (pos_x_ + width_ > PLAY_AREA_RIGHT)) {
|
||||
// Corregir posición
|
||||
posX -= velX;
|
||||
pos_x_ -= vel_x_;
|
||||
|
||||
// Invertir sentido
|
||||
velX = -velX;
|
||||
vel_x_ = -vel_x_;
|
||||
}
|
||||
|
||||
// Si se sale por arriba rebota (excepto la maquina de café)
|
||||
if ((posY < PLAY_AREA_TOP) && !(kind == ITEM_COFFEE_MACHINE)) {
|
||||
if ((pos_y_ < PLAY_AREA_TOP) && !(id_ == Item::Id::COFFEE_MACHINE)) {
|
||||
// Corrige
|
||||
posY -= velY;
|
||||
pos_y_ -= vel_y_;
|
||||
|
||||
// Invierte el sentido
|
||||
velY = -velY;
|
||||
vel_y_ = -vel_y_;
|
||||
}
|
||||
|
||||
// Si el objeto se sale por la parte inferior
|
||||
if (posY + height > PLAY_AREA_BOTTOM) {
|
||||
if (pos_y_ + height_ > PLAY_AREA_BOTTOM) {
|
||||
// Detiene el objeto
|
||||
velY = 0;
|
||||
velX = 0;
|
||||
accelX = 0;
|
||||
accelY = 0;
|
||||
posY = PLAY_AREA_BOTTOM - height;
|
||||
if (kind == ITEM_COFFEE_MACHINE) {
|
||||
floorCollision = true;
|
||||
vel_y_ = 0;
|
||||
vel_x_ = 0;
|
||||
accel_x_ = 0;
|
||||
accel_y_ = 0;
|
||||
pos_y_ = PLAY_AREA_BOTTOM - height_;
|
||||
if (id_ == Item::Id::COFFEE_MACHINE) {
|
||||
floor_collision_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza la posición del sprite
|
||||
sprite->setPosX(int(posX));
|
||||
sprite->setPosY(int(posY));
|
||||
sprite_->setPosX(int(pos_x_));
|
||||
sprite_->setPosY(int(pos_y_));
|
||||
shiftColliders();
|
||||
}
|
||||
|
||||
// Pone a cero todos los valores del objeto
|
||||
void Item::disable() {
|
||||
enabled = false;
|
||||
enabled_ = false;
|
||||
}
|
||||
|
||||
// Actualiza el objeto a su posicion, animación y controla los contadores
|
||||
void Item::update() {
|
||||
move();
|
||||
sprite->animate();
|
||||
sprite_->animate();
|
||||
updateTimeToLive();
|
||||
checkTimeToLive();
|
||||
}
|
||||
|
||||
// Actualiza el contador
|
||||
void Item::updateTimeToLive() {
|
||||
if (timeToLive > 0) {
|
||||
timeToLive--;
|
||||
if (time_to_live_ > 0) {
|
||||
time_to_live_--;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si el objeto sigue vivo
|
||||
void Item::checkTimeToLive() {
|
||||
if (timeToLive == 0) {
|
||||
if (time_to_live_ == 0) {
|
||||
disable();
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
auto Item::getPosX() const -> float {
|
||||
return posX;
|
||||
return pos_x_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
auto Item::getPosY() const -> float {
|
||||
return posY;
|
||||
return pos_y_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
auto Item::getWidth() const -> int {
|
||||
return width;
|
||||
return width_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
auto Item::getHeight() const -> int {
|
||||
return height;
|
||||
return height_;
|
||||
}
|
||||
|
||||
// Obtiene del valor de la variable
|
||||
auto Item::getClass() const -> int {
|
||||
return kind;
|
||||
auto Item::getId() const -> Id {
|
||||
return id_;
|
||||
}
|
||||
|
||||
// Obtiene el valor de la variable
|
||||
auto Item::isEnabled() const -> bool {
|
||||
return enabled;
|
||||
return enabled_;
|
||||
}
|
||||
|
||||
// Obtiene el circulo de colisión
|
||||
auto Item::getCollider() -> Circle & {
|
||||
return collider;
|
||||
return collider_;
|
||||
}
|
||||
|
||||
// Alinea el circulo de colisión con la posición del objeto
|
||||
void Item::shiftColliders() {
|
||||
collider.x = int(posX + (width / 2));
|
||||
collider.y = int(posY + (height / 2));
|
||||
collider_.x = int(pos_x_ + (width_ / 2));
|
||||
collider_.y = int(pos_y_ + (height_ / 2));
|
||||
}
|
||||
|
||||
// Informa si el objeto ha colisionado con el suelo
|
||||
auto Item::isOnFloor() const -> bool {
|
||||
return floorCollision;
|
||||
return floor_collision_;
|
||||
}
|
||||
Reference in New Issue
Block a user