diff --git a/data/config/param_320x256.txt b/data/config/param_320x256.txt index 040e23b..39506e0 100644 --- a/data/config/param_320x256.txt +++ b/data/config/param_320x256.txt @@ -7,6 +7,7 @@ game.play_area.rect.y 0 # Rectangulo con la posición de la zona de juego game.play_area.rect.w 320 # Rectangulo con la posición de la zona de juego game.play_area.rect.h 216 # Rectangulo con la posición de la zona de juego game.enter_name_seconds 30 # Duración en segundos para introducir el nombre al finalizar la partida +game.game_text.dest_y ## FADE fade.num_squares_width 160 diff --git a/source/director.cpp b/source/director.cpp index dea9797..735ca64 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -90,7 +90,7 @@ Director::Director(int argc, const char *argv[]) // Carga el fichero de puntuaciones { - auto manager = std::make_unique(&options.game.hi_score_table); + auto manager = std::make_unique(options.game.hi_score_table); if (overrides.clear_hi_score_table) { manager->clear(); diff --git a/source/game.cpp b/source/game.cpp index 6bd7585..2cc4680 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -64,8 +64,8 @@ Game::Game(int player_id, int current_stage, bool demo) explosions_ = std::make_unique(); balloon_formations_ = std::make_unique(); - // Carga los recursos - loadMedia(); + // Asigna los recursos a variables privadas del objeto + setResources(); // Inicializa los vectores con los datos para la demo if (demo_.enabled) @@ -86,39 +86,6 @@ Game::Game(int player_id, int current_stage, bool demo) canvas_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.play_area.rect.w, param.game.play_area.rect.h); SDL_SetTextureBlendMode(canvas_, SDL_BLENDMODE_BLEND); - // Inicializa las variables necesarias para la sección 'Game' - init(player_id); -} - -Game::~Game() -{ - // Guarda las puntuaciones en un fichero - if (!demo_.enabled) - { - auto manager = std::make_unique(&options.game.hi_score_table); - manager->saveToFile(asset_->get("score.bin")); - } -#ifdef RECORDING - saveDemoFile(Asset::get()->get("demo1.bin"), demo_.data.at(0)); -#endif - - // Elimina todos los objetos contenidos en vectores - deleteAllVectorObjects(); - - // Libera los recursos - unloadMedia(); - - Scoreboard::destroy(); - - SDL_DestroyTexture(canvas_); -} - -// Inicializa las variables necesarias para la sección 'Game' -void Game::init(int player_id) -{ - // Elimina qualquier jugador que hubiese antes de crear los nuevos - players_.clear(); - // Crea los dos jugadores auto player1 = std::make_unique(1, (param.game.play_area.first_quarter_x * ((0 * 2) + 1)) - 11, param.game.play_area.rect.h - 30, demo_.enabled, ¶m.game.play_area.rect, player_textures_[0], player_animations_); player1->setScoreBoardPanel(SCOREBOARD_LEFT_PANEL); @@ -134,14 +101,12 @@ void Game::init(int player_id) player2->setController(controller2); players_.push_back(std::move(player2)); - // Obtiene mediante "playerID" el jugador que va a empezar jugar - auto main_player = getPlayer(player_id); - - // Cambia el estado del jugador seleccionado - main_player->setStatusPlaying(PlayerStatus::PLAYING); - - // Como es el principio del juego, empieza sin inmunidad - main_player->setInvulnerable(false); + // Activa el jugador que coincide con el "player_id" + { + auto player = getPlayer(player_id); + player->setStatusPlaying(PlayerStatus::PLAYING); + player->setInvulnerable(false); + } // Variables relacionadas con la dificultad switch (difficulty_) @@ -150,8 +115,7 @@ void Game::init(int player_id) { default_balloon_speed_ = BALLOON_SPEED_1; difficulty_score_multiplier_ = 0.5f; - difficulty_color_ = difficulty_easy_color; - scoreboard_->setColor(difficulty_color_); + scoreboard_->setColor(scoreboard_easy_color); break; } @@ -159,8 +123,7 @@ void Game::init(int player_id) { default_balloon_speed_ = BALLOON_SPEED_1; difficulty_score_multiplier_ = 1.0f; - difficulty_color_ = difficulty_normal_color; - scoreboard_->setColor(scoreboard_color); + scoreboard_->setColor(scoreboard_normal_color); break; } @@ -168,8 +131,7 @@ void Game::init(int player_id) { default_balloon_speed_ = BALLOON_SPEED_5; difficulty_score_multiplier_ = 1.5f; - difficulty_color_ = difficulty_hard_color; - scoreboard_->setColor(difficulty_color_); + scoreboard_->setColor(scoreboard_hard_color); break; } @@ -299,11 +261,26 @@ void Game::init(int player_id) smart_sprites_.clear(); } -// Carga los recursos necesarios para la sección 'Game' -void Game::loadMedia() +Game::~Game() { - unloadMedia(); + // Guarda las puntuaciones en un fichero + if (!demo_.enabled) + { + auto manager = std::make_unique(options.game.hi_score_table); + manager->saveToFile(asset_->get("score.bin")); + } +#ifdef RECORDING + saveDemoFile(Asset::get()->get("demo1.bin"), demo_.data.at(0)); +#endif + Scoreboard::destroy(); + + SDL_DestroyTexture(canvas_); +} + +// Asigna los recursos a variables privadas del objeto +void Game::setResources() +{ // Texturas { bullet_texture_ = Resource::get()->getTexture("bullet.png"); @@ -403,23 +380,6 @@ void Game::loadMedia() } } -// Libera los recursos previamente cargados -void Game::unloadMedia() -{ - // Texturas - game_text_textures_.clear(); - balloon_textures_.clear(); - explosions_textures_.clear(); - item_textures_.clear(); - player_textures_.clear(); - - // Animaciones - player_animations_.clear(); - balloon_animations_.clear(); - explosions_animations_.clear(); - item_animations_.clear(); -} - // Crea una formación de enemigos void Game::deployBalloonFormation() { @@ -1945,16 +1905,6 @@ void Game::checkEvents() } } -// Elimina todos los objetos contenidos en vectores -void Game::deleteAllVectorObjects() -{ - players_.clear(); - balloons_.clear(); - bullets_.clear(); - items_.clear(); - smart_sprites_.clear(); -} - // Recarga las texturas void Game::reloadTextures() { @@ -2014,8 +1964,8 @@ void Game::pause(bool value) // Añade una puntuación a la tabla de records void Game::addScoreToScoreBoard(const std::string &name, int score) { - const auto entry = (HiScoreEntry){trim(name), score}; - auto manager = std::make_unique(&options.game.hi_score_table); + const auto entry = HiScoreEntry(trim(name), score); + auto manager = std::make_unique(options.game.hi_score_table); manager->add(entry); manager->saveToFile(asset_->get("score.bin")); } diff --git a/source/game.h b/source/game.h index 57759ff..5e9be12 100644 --- a/source/game.h +++ b/source/game.h @@ -76,6 +76,12 @@ private: int item_clock_odds; // Probabilidad de aparición del objeto int item_coffee_odds; // Probabilidad de aparición del objeto int item_coffee_machine_odds; // Probabilidad de aparición del objeto + + // Constructor con valores predeterminados + Helper(bool nc = false, bool ncm = false, bool npb = false, int c = 0, int ido = 0, int igo = 0, int ipo = 0, int ico = 0, int icoffo = 0, int icmo = 0) + : need_coffee(nc), need_coffee_machine(ncm), need_power_ball(npb), counter(c), + item_disk_odds(ido), item_gavina_odds(igo), item_pacmar_odds(ipo), item_clock_odds(ico), + item_coffee_odds(icoffo), item_coffee_machine_odds(icmo) {} }; // Constantes @@ -164,7 +170,6 @@ private: int game_completed_counter_; // Contador para el tramo final, cuando se ha completado la partida y ya no aparecen más enemigos GameDifficulty difficulty_; // Dificultad del juego float difficulty_score_multiplier_; // Multiplicador de puntos en función de la dificultad - Color difficulty_color_; // Color asociado a la dificultad int last_stage_reached_; // Contiene el número de la última pantalla que se ha alcanzado Demo demo_; // Variable con todas las variables relacionadas con el modo demo int total_power_to_complete_game_; // La suma del poder necesario para completar todas las fases @@ -183,14 +188,8 @@ private: // Comprueba los eventos que hay en cola void checkEvents(); - // Inicializa las variables necesarias para la sección 'Game' - void init(int player_id); - - // Carga los recursos necesarios para la sección 'Game' - void loadMedia(); - - // Libera los recursos previamente cargados - void unloadMedia(); + // Asigna los recursos a variables privadas del objeto + void setResources(); // Crea una formación de enemigos void deployBalloonFormation(); @@ -366,9 +365,6 @@ private: // Comprueba si todos los jugadores han terminado de jugar bool allPlayersAreNotPlaying(); - // Elimina todos los objetos contenidos en vectores - void deleteAllVectorObjects(); - // Recarga las texturas void reloadTextures(); diff --git a/source/manage_hiscore_table.cpp b/source/manage_hiscore_table.cpp index a6efc2f..80ecf96 100644 --- a/source/manage_hiscore_table.cpp +++ b/source/manage_hiscore_table.cpp @@ -6,42 +6,38 @@ #include // for basic_ostream, char_traits, operator<< #include "utils.h" // for HiScoreEntry -// Constructor -ManageHiScoreTable::ManageHiScoreTable(std::vector *table) - : table_(table) {} - // Resetea la tabla a los valores por defecto void ManageHiScoreTable::clear() { // Limpia la tabla - table_->clear(); + table_.clear(); // Añade 10 entradas predefinidas - table_->push_back({"Bry", 1000000}); - table_->push_back({"Usufondo", 500000}); - table_->push_back({"G.Lucas", 100000}); - table_->push_back({"P.Delgat", 50000}); - table_->push_back({"P.Arrabalera", 10000}); - table_->push_back({"Pelechano", 5000}); - table_->push_back({"Sahuquillo", 1000}); - table_->push_back({"Bacteriol", 500}); - table_->push_back({"Pepe", 200}); - table_->push_back({"Rosita", 100}); + table_.push_back({"Bry", 1000000}); + table_.push_back({"Usufondo", 500000}); + table_.push_back({"G.Lucas", 100000}); + table_.push_back({"P.Delgat", 50000}); + table_.push_back({"P.Arrabalera", 10000}); + table_.push_back({"Pelechano", 5000}); + table_.push_back({"Sahuquillo", 1000}); + table_.push_back({"Bacteriol", 500}); + table_.push_back({"Pepe", 200}); + table_.push_back({"Rosita", 100}); } // Añade un elemento a la tabla void ManageHiScoreTable::add(HiScoreEntry entry) { // Añade la entrada a la tabla - table_->push_back(entry); + table_.push_back(entry); // Ordena la tabla sort(); // Deja solo las 10 primeras entradas - if (static_cast(table_->size()) > 10) + if (static_cast(table_.size()) > 10) { - table_->resize(10); + table_.resize(10); } } @@ -53,7 +49,7 @@ void ManageHiScoreTable::sort() bool operator()(const HiScoreEntry &a, const HiScoreEntry &b) const { return a.score > b.score; } } custom_less; - std::sort(table_->begin(), table_->end(), custom_less); + std::sort(table_.begin(), table_.end(), custom_less); } // Carga la tabla con los datos de un fichero @@ -68,7 +64,7 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path) const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1); std::cout << "Reading file: " << file_name << std::endl; - for (auto &entry : *table_) + for (auto &entry : table_) { int nameSize = 0; @@ -116,12 +112,12 @@ bool ManageHiScoreTable::saveToFile(const std::string &file_path) if (file) { // Guarda los datos - for (int i = 0; i < (int)table_->size(); ++i) + for (int i = 0; i < (int)table_.size(); ++i) { - SDL_RWwrite(file, &table_->at(i).score, sizeof(int), 1); - const int nameSize = (int)table_->at(i).name.size(); + SDL_RWwrite(file, &table_.at(i).score, sizeof(int), 1); + const int nameSize = (int)table_.at(i).name.size(); SDL_RWwrite(file, &nameSize, sizeof(int), 1); - SDL_RWwrite(file, table_->at(i).name.c_str(), nameSize, 1); + SDL_RWwrite(file, table_.at(i).name.c_str(), nameSize, 1); } std::cout << "Writing file: " << file_name.c_str() << std::endl; diff --git a/source/manage_hiscore_table.h b/source/manage_hiscore_table.h index b75720f..76a79be 100644 --- a/source/manage_hiscore_table.h +++ b/source/manage_hiscore_table.h @@ -17,14 +17,15 @@ class ManageHiScoreTable { private: // Variables - std::vector *table_; // Tabla con los records + std::vector &table_; // Tabla con los records // Ordena la tabla void sort(); public: // Constructor - explicit ManageHiScoreTable(std::vector *table); + explicit ManageHiScoreTable(std::vector &table) + : table_(table) {} // Inicialización en el constructor // Destructor ~ManageHiScoreTable() = default; diff --git a/source/utils.cpp b/source/utils.cpp index 8770cf4..b572a79 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -16,10 +16,9 @@ const Color bg_color = {0x27, 0x27, 0x36}; const Color no_color = {0xFF, 0xFF, 0xFF}; const Color shdw_txt_color = {0x43, 0x43, 0x4F}; const Color separator_color = {0x0D, 0x1A, 0x2B}; -const Color scoreboard_color = {0x2E, 0x3F, 0x47}; -const Color difficulty_easy_color = {0x4B, 0x69, 0x2F}; -const Color difficulty_normal_color = {0xFF, 0x7A, 0x00}; -const Color difficulty_hard_color = {0x76, 0x42, 0x8A}; +const Color scoreboard_easy_color = {0x4B, 0x69, 0x2F}; +const Color scoreboard_normal_color = {0x2E, 0x3F, 0x47}; +const Color scoreboard_hard_color = {0x76, 0x42, 0x8A}; const Color flash_color = {0xFF, 0xFF, 0xFF}; const Color fade_color = {0x27, 0x27, 0x36}; const Color orange_color = {0xFF, 0x7A, 0x00}; diff --git a/source/utils.h b/source/utils.h index d0de72a..5300c35 100644 --- a/source/utils.h +++ b/source/utils.h @@ -71,6 +71,10 @@ struct HiScoreEntry { std::string name; // Nombre int score; // Puntuación + +// Constructor + HiScoreEntry(const std::string &n = "", int s = 0) + : name(n), score(s) {} }; struct DemoKeys @@ -96,6 +100,14 @@ struct Demo int counter; // Contador para el modo demo DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo std::vector data; // Vector con diferentes sets de datos con los movimientos para la demo + + // Constructor por defecto + Demo() + : enabled(false), recording(false), counter(0), keys(), data() {} + + // Constructor con parámetros + Demo(bool e, bool r, int c, const DemoKeys &k, const std::vector &d) + : enabled(e), recording(r), counter(c), keys(k), data(d) {} }; // Estructura para las opciones de la ventana @@ -308,10 +320,9 @@ extern const Color bg_color; extern const Color no_color; extern const Color shdw_txt_color; extern const Color separator_color; -extern const Color scoreboard_color; -extern const Color difficulty_easy_color; -extern const Color difficulty_normal_color; -extern const Color difficulty_hard_color; +extern const Color scoreboard_easy_color; +extern const Color scoreboard_normal_color; +extern const Color scoreboard_hard_color; extern const Color flash_color; extern const Color fade_color; extern const Color orange_color; \ No newline at end of file