jugant amb clang-tidy
This commit is contained in:
106
source/tabe.cpp
106
source/tabe.cpp
@@ -2,10 +2,10 @@
|
||||
#include "tabe.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_FlipMode, SDL_GetTicks
|
||||
#include <stdlib.h> // Para rand, abs
|
||||
|
||||
#include <algorithm> // Para max
|
||||
#include <cmath> // Para abs
|
||||
#include <cstdlib> // Para rand, abs
|
||||
#include <string> // Para basic_string
|
||||
|
||||
#include "audio.h" // Para Audio
|
||||
@@ -16,7 +16,7 @@
|
||||
// Constructor
|
||||
Tabe::Tabe()
|
||||
: sprite_(std::make_unique<AnimatedSprite>(Resource::get()->getTexture("tabe.png"), Resource::get()->getAnimation("tabe.ani"))),
|
||||
timer_(TabeTimer(2.5f, 4.0f)) {}
|
||||
timer_(TabeTimer(2.5F, 4.0F)) {}
|
||||
|
||||
// Actualiza la lógica
|
||||
void Tabe::update() {
|
||||
@@ -27,7 +27,7 @@ void Tabe::update() {
|
||||
}
|
||||
|
||||
timer_.update();
|
||||
if (timer_.should_spawn()) {
|
||||
if (timer_.shouldSpawn()) {
|
||||
enable();
|
||||
}
|
||||
}
|
||||
@@ -41,50 +41,50 @@ void Tabe::render() {
|
||||
|
||||
// Mueve el objeto
|
||||
void Tabe::move() {
|
||||
const int x = static_cast<int>(x_);
|
||||
speed_ += accel_;
|
||||
x_ += speed_;
|
||||
fly_distance_ -= std::abs(x - static_cast<int>(x_));
|
||||
const int X = static_cast<int>(x_);
|
||||
speed_ += accel_;
|
||||
x_ += speed_;
|
||||
fly_distance_ -= std::abs(X - static_cast<int>(x_));
|
||||
|
||||
// Comprueba si sale por los bordes
|
||||
const float min_x = param.game.game_area.rect.x - WIDTH_;
|
||||
const float max_x = param.game.game_area.rect.x + param.game.game_area.rect.w;
|
||||
switch (destiny_) {
|
||||
case TabeDirection::TO_THE_LEFT: {
|
||||
if (x_ < min_x) {
|
||||
disable();
|
||||
}
|
||||
if (x_ > param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_ && direction_ == TabeDirection::TO_THE_RIGHT) {
|
||||
setRandomFlyPath(TabeDirection::TO_THE_LEFT, 80);
|
||||
x_ = param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH_;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Comprueba si sale por los bordes
|
||||
const float MIN_X = param.game.game_area.rect.x - WIDTH;
|
||||
const float MAX_X = param.game.game_area.rect.x + param.game.game_area.rect.w;
|
||||
switch (destiny_) {
|
||||
case TabeDirection::TO_THE_LEFT: {
|
||||
if (x_ < MIN_X) {
|
||||
disable();
|
||||
}
|
||||
if (x_ > param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH && direction_ == TabeDirection::TO_THE_RIGHT) {
|
||||
setRandomFlyPath(TabeDirection::TO_THE_LEFT, 80);
|
||||
x_ = param.game.game_area.rect.x + param.game.game_area.rect.w - WIDTH;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case TabeDirection::TO_THE_RIGHT: {
|
||||
if (x_ > max_x) {
|
||||
disable();
|
||||
}
|
||||
if (x_ < param.game.game_area.rect.x && direction_ == TabeDirection::TO_THE_LEFT) {
|
||||
setRandomFlyPath(TabeDirection::TO_THE_RIGHT, 80);
|
||||
x_ = param.game.game_area.rect.x;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
case TabeDirection::TO_THE_RIGHT: {
|
||||
if (x_ > MAX_X) {
|
||||
disable();
|
||||
}
|
||||
if (x_ < param.game.game_area.rect.x && direction_ == TabeDirection::TO_THE_LEFT) {
|
||||
setRandomFlyPath(TabeDirection::TO_THE_RIGHT, 80);
|
||||
x_ = param.game.game_area.rect.x;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (fly_distance_ <= 0) {
|
||||
if (waiting_counter_ > 0) {
|
||||
accel_ = speed_ = 0.0f;
|
||||
--waiting_counter_;
|
||||
accel_ = speed_ = 0.0F;
|
||||
--waiting_counter_;
|
||||
} else {
|
||||
constexpr int CHOICES = 4;
|
||||
const TabeDirection left[CHOICES] = {TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_RIGHT};
|
||||
const TabeDirection right[CHOICES] = {TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_RIGHT, TabeDirection::TO_THE_RIGHT, TabeDirection::TO_THE_RIGHT};
|
||||
const TabeDirection direction = destiny_ == TabeDirection::TO_THE_LEFT ? left[rand() % CHOICES] : right[rand() % CHOICES];
|
||||
setRandomFlyPath(direction, 20 + rand() % 40);
|
||||
const TabeDirection LEFT[CHOICES] = {TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_RIGHT};
|
||||
const TabeDirection RIGHT[CHOICES] = {TabeDirection::TO_THE_LEFT, TabeDirection::TO_THE_RIGHT, TabeDirection::TO_THE_RIGHT, TabeDirection::TO_THE_RIGHT};
|
||||
const TabeDirection DIRECTION = destiny_ == TabeDirection::TO_THE_LEFT ? LEFT[rand() % CHOICES] : RIGHT[rand() % CHOICES];
|
||||
setRandomFlyPath(DIRECTION, 20 + rand() % 40);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,13 +98,13 @@ void Tabe::enable() {
|
||||
has_bonus_ = true;
|
||||
hit_counter_ = 0;
|
||||
number_of_hits_ = 0;
|
||||
y_ = param.game.game_area.rect.y + 20.0f;
|
||||
y_ = param.game.game_area.rect.y + 20.0F;
|
||||
|
||||
// Establece una dirección aleatoria
|
||||
destiny_ = direction_ = rand() % 2 == 0 ? TabeDirection::TO_THE_LEFT : TabeDirection::TO_THE_RIGHT;
|
||||
|
||||
// Establece la posición inicial
|
||||
x_ = (direction_ == TabeDirection::TO_THE_LEFT) ? param.game.game_area.rect.x + param.game.game_area.rect.w : param.game.game_area.rect.x - WIDTH_;
|
||||
x_ = (direction_ == TabeDirection::TO_THE_LEFT) ? param.game.game_area.rect.x + param.game.game_area.rect.w : param.game.game_area.rect.x - WIDTH;
|
||||
|
||||
// Crea una ruta de vuelo
|
||||
setRandomFlyPath(direction_, 60);
|
||||
@@ -119,19 +119,19 @@ void Tabe::setRandomFlyPath(TabeDirection direction, int lenght) {
|
||||
waiting_counter_ = 5 + rand() % 15;
|
||||
Audio::get()->playSound("tabe.wav");
|
||||
|
||||
constexpr float SPEED = 2.0f;
|
||||
constexpr float SPEED = 2.0F;
|
||||
|
||||
switch (direction) {
|
||||
case TabeDirection::TO_THE_LEFT: {
|
||||
speed_ = -1.0f * SPEED;
|
||||
accel_ = -1.0f * (1 + rand() % 10) / 30.0f;
|
||||
sprite_->setFlip(SDL_FLIP_NONE);
|
||||
break;
|
||||
speed_ = -1.0F * SPEED;
|
||||
accel_ = -1.0F * (1 + rand() % 10) / 30.0F;
|
||||
sprite_->setFlip(SDL_FLIP_NONE);
|
||||
break;
|
||||
}
|
||||
|
||||
case TabeDirection::TO_THE_RIGHT: {
|
||||
speed_ = SPEED;
|
||||
accel_ = (1 + rand() % 10) / 30.0f;
|
||||
accel_ = (1 + rand() % 10) / 30.0F;
|
||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
break;
|
||||
}
|
||||
@@ -174,12 +174,12 @@ void Tabe::updateState() {
|
||||
}
|
||||
|
||||
// Intenta obtener el bonus
|
||||
bool Tabe::tryToGetBonus() {
|
||||
if (has_bonus_ && rand() % std::max(1, 15 - number_of_hits_) == 0) {
|
||||
has_bonus_ = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
auto Tabe::tryToGetBonus() -> bool {
|
||||
if (has_bonus_ && rand() % std::max(1, 15 - number_of_hits_) == 0) {
|
||||
has_bonus_ = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Actualiza el temporizador
|
||||
|
||||
Reference in New Issue
Block a user