Mes recomanacions de cppcheck

This commit is contained in:
2024-10-13 19:26:27 +02:00
parent 46540ad7c3
commit babf02226c
22 changed files with 291 additions and 369 deletions

View File

@@ -1,14 +1,15 @@
#include "game.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_keycode.h> // for SDLK_1, SDLK_2, SDLK_3, SDLK_h
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_rwops.h> // for SDL_RWFromFile, SDL_RWclose, SDL_R...
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <SDL2/SDL_video.h> // for SDL_WINDOWEVENT_FOCUS_GAINED, SDL_...
#include <stdlib.h> // for rand
#include <algorithm> // for min
#include <fstream> // for basic_ifstream
#include <iostream> // for char_traits, basic_istream, ifstream
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND
#include <SDL2/SDL_keycode.h> // for SDLK_1, SDLK_2, SDLK_3, SDLK_h
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_rwops.h> // for SDL_RWFromFile, SDL_RWclose, SDL_R...
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <SDL2/SDL_video.h> // for SDL_WINDOWEVENT_FOCUS_GAINED, SDL_...
#include <stdlib.h> // for rand
#include <algorithm> // for min
#include <fstream> // for basic_ifstream
#include <iostream> // for char_traits, basic_istream, ifstream
#include <numeric>
#include "asset.h" // for Asset
#include "background.h" // for Background
#include "balloon.h" // for Balloon, BALLOON_SPEED_1, BALLOON_...
@@ -133,13 +134,13 @@ void Game::init(int player_id)
players_.push_back(std::move(player2));
// Obtiene mediante "playerID" el jugador que va a empezar jugar
auto player = getPlayer(player_id);
auto main_player = getPlayer(player_id);
// Cambia el estado del jugador seleccionado
player->setStatusPlaying(PlayerStatus::PLAYING);
main_player->setStatusPlaying(PlayerStatus::PLAYING);
// Como es el principio del juego, empieza sin inmunidad
player->setInvulnerable(false);
main_player->setInvulnerable(false);
// Variables relacionadas con la dificultad
switch (difficulty_)
@@ -243,9 +244,9 @@ void Game::init(int player_id)
// Activa o no al otro jugador
if (rand() % 2 == 0)
{
const auto otherPlayer = player_id == 1 ? 2 : 1;
auto player = getPlayer(otherPlayer);
player->setStatusPlaying(PlayerStatus::PLAYING);
const auto other_player_id = player_id == 1 ? 2 : 1;
auto other_player = getPlayer(other_player_id);
other_player->setStatusPlaying(PlayerStatus::PLAYING);
}
for (auto &player : players_)
@@ -707,7 +708,7 @@ bool Game::saveDemoFile(const std::string &file_path)
#endif // RECORDING
// Crea una formación de enemigos
void Game::deployEnemyFormation()
void Game::deployBalloonFormation()
{
// Solo despliega una formación enemiga si ha pasado cierto tiempo desde la última
if (balloon_deploy_counter_ == 0)
@@ -1587,14 +1588,8 @@ void Game::killPlayer(std::shared_ptr<Player> &player)
// Calcula y establece el valor de amenaza en funcion de los globos activos
void Game::evaluateAndSetMenace()
{
menace_current_ = 0;
for (auto &balloon : balloons_)
{
if (balloon->isEnabled())
{
menace_current_ += balloon->getMenace();
}
}
menace_current_ = std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon)
{ return sum + (balloon->isEnabled() ? balloon->getMenace() : 0); });
}
// Obtiene el valor de la variable
@@ -1645,11 +1640,11 @@ void Game::updateTimeStoppedCounter()
}
// Actualiza la variable enemyDeployCounter
void Game::updateEnemyDeployCounter()
void Game::updateBalloonDeployCounter()
{
if (balloon_deploy_counter_ > 0)
{
balloon_deploy_counter_--;
--balloon_deploy_counter_;
}
}
@@ -1750,7 +1745,7 @@ void Game::update()
// Actualiza los contadores de estado y efectos
updateTimeStoppedCounter();
updateEnemyDeployCounter();
updateBalloonDeployCounter();
// Actualiza el ayudante
updateHelper();
@@ -1872,7 +1867,7 @@ void Game::updateMenace()
if (menace_current_ < menace_threshold_)
{
// Crea una formación de enemigos
deployEnemyFormation();
deployBalloonFormation();
// Recalcula el nivel de amenaza con el nuevo globo
evaluateAndSetMenace();
@@ -2248,28 +2243,14 @@ void Game::run()
// Indica si se puede crear una powerball
bool Game::canPowerBallBeCreated()
{
if ((!power_ball_enabled_) && (calculateScreenPower() > POWERBALL_SCREENPOWER_MINIMUM) && (power_ball_counter_ == 0))
{
return true;
}
return false;
return (!power_ball_enabled_) && (calculateScreenPower() > POWERBALL_SCREENPOWER_MINIMUM) && (power_ball_counter_ == 0);
}
// Calcula el poder actual de los globos en pantalla
int Game::calculateScreenPower()
{
auto power = 0;
for (auto &balloon : balloons_)
{
if (balloon->isEnabled())
{
power += balloon->getPower();
}
}
return power;
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon)
{ return sum + (balloon->isEnabled() ? balloon->getPower() : 0); });
}
// Inicializa las variables que contienen puntos de ruta para mover objetos
@@ -2354,7 +2335,7 @@ void Game::updateHelper()
// Solo ofrece ayuda cuando la amenaza es elevada
if (menace_current_ > 15)
{
for (auto &player : players_)
for (const auto &player : players_)
{
helper_.need_coffee = (player->getCoffees() == 0);
helper_.need_coffee_machine = (!player->isPowerUp());
@@ -2370,7 +2351,7 @@ void Game::updateHelper()
bool Game::allPlayersAreWaitingOrGameOver()
{
auto success = true;
for (auto &player : players_)
for (const auto &player : players_)
{
success &= player->isWaiting() || player->isGameOver();
}
@@ -2382,7 +2363,7 @@ bool Game::allPlayersAreWaitingOrGameOver()
bool Game::allPlayersAreGameOver()
{
auto success = true;
for (auto &player : players_)
for (const auto &player : players_)
{
success &= player->isGameOver();
}
@@ -2394,7 +2375,7 @@ bool Game::allPlayersAreGameOver()
bool Game::allPlayersAreNotPlaying()
{
auto success = true;
for (auto &player : players_)
for (const auto &player : players_)
{
success &= !player->isPlaying();
}
@@ -2566,7 +2547,7 @@ void Game::reloadTextures()
// Actualiza el marcador
void Game::updateScoreboard()
{
for (auto &player : players_)
for (const auto &player : players_)
{
scoreboard_->setScore(player->getScoreBoardPanel(), player->getScore());
scoreboard_->setMult(player->getScoreBoardPanel(), player->getScoreMultiplier());
@@ -2590,7 +2571,7 @@ void Game::pause(bool value)
}
// Añade una puntuación a la tabla de records
void Game::addScoreToScoreBoard(std::string name, int score)
void Game::addScoreToScoreBoard(const std::string &name, int score)
{
const auto entry = (HiScoreEntry){trim(name), score};
auto manager = std::make_unique<ManageHiScoreTable>(&options.game.hi_score_table);
@@ -2635,7 +2616,7 @@ void Game::checkPlayersStatusPlaying()
// Obtiene un jugador a partir de su "id"
std::shared_ptr<Player> Game::getPlayer(int id)
{
for (auto &player : players_)
for (const auto &player : players_)
{
if (player->getId() == id)
{