From 25a2753b13684a285621a9d3a4695305741ea274 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sat, 5 Oct 2024 23:53:42 +0200 Subject: [PATCH] =?UTF-8?q?Canviats=20defines=20per=20constexpr=20i=20enum?= =?UTF-8?q?=20class=20Canviats=20punters=20a=20unique=5Fptr=20Afegit=20con?= =?UTF-8?q?st=20a=20alguns=20metodes=20de=20classse=20fix:=20el=20segon=20?= =?UTF-8?q?jugador=20no=20podia=20unirse=20a=20la=20partida=20new:=20Quan?= =?UTF-8?q?=20els=20dos=20jugadors=20han=20decidit=20no=20continuar,=20ja?= =?UTF-8?q?=20no=20poden=20continuar=20i=20el=20marcador=20aix=C3=AD=20ho?= =?UTF-8?q?=20reflectix=20fix:=20al=20posar=20el=20nom=20per=20segona=20ve?= =?UTF-8?q?gada=20en=20la=20mateixa=20partida,=20no=20es=20reseteja=20la?= =?UTF-8?q?=20posici=C3=B3=20del=20selector=20fix:=20el=20fade=20venetian?= =?UTF-8?q?=20no=20netejava=20la=20textura=20i=20de=20vegades=20eixien=20g?= =?UTF-8?q?r=C3=A0fics=20corruptes=20fix:=20ara=20grava=20a=20disco=20cada?= =?UTF-8?q?=20vegada=20que=20es=20posa=20nom=20al=20morir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/enter_name.cpp | 16 ++-- source/enter_name.h | 9 ++- source/fade.cpp | 1 + source/game.cpp | 94 +++++++++++++-------- source/game.h | 3 + source/player.cpp | 184 +++++++++++++++++++++++------------------- source/player.h | 175 ++++++++++++++++++++------------------- source/scoreboard.cpp | 45 ++++++++--- source/scoreboard.h | 32 ++++---- source/title.cpp | 1 - 10 files changed, 322 insertions(+), 238 deletions(-) diff --git a/source/enter_name.cpp b/source/enter_name.cpp index a9e7de2..a6c3174 100644 --- a/source/enter_name.cpp +++ b/source/enter_name.cpp @@ -3,12 +3,17 @@ // Constructor EnterName::EnterName() +{ + init(); +} + +// Inicializa el objeto +void EnterName::init() { // Obtiene el puntero al nombre name = "A"; // Inicia la lista de caracteres permitidos - // characterList = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; characterList = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+-*/=?¿<>!\"#$%&/()"; pos = 0; numCharacters = (int)characterList.size(); @@ -20,11 +25,6 @@ EnterName::EnterName() updateName(); } -// Destructor -EnterName::~EnterName() -{ -} - // Incrementa la posición void EnterName::incPos() { @@ -101,13 +101,13 @@ int EnterName::findIndex(char character) } // Obtiene el nombre -std::string EnterName::getName() +std::string EnterName::getName() const { return name; } // Obtiene la posición que se está editando -int EnterName::getPos() +int EnterName::getPos() const { return pos; } \ No newline at end of file diff --git a/source/enter_name.h b/source/enter_name.h index 24c83f0..114f42c 100644 --- a/source/enter_name.h +++ b/source/enter_name.h @@ -36,7 +36,10 @@ public: EnterName(); // Destructor - ~EnterName(); + ~EnterName() = default; + + // Inicializa el objeto + void init(); // Incrementa la posición void incPos(); @@ -51,8 +54,8 @@ public: void decIndex(); // Obtiene el nombre - std::string getName(); + std::string getName() const; // Obtiene la posición que se está editando - int getPos(); + int getPos() const; }; \ No newline at end of file diff --git a/source/fade.cpp b/source/fade.cpp index 11f08f9..65ffdaa 100644 --- a/source/fade.cpp +++ b/source/fade.cpp @@ -270,6 +270,7 @@ void Fade::activate() case FADE_VENETIAN: { + cleanBackbuffer(0, 0, 0, 0); rect1 = {0, 0, param.game.width, 0}; square.clear(); a = 255; diff --git a/source/game.cpp b/source/game.cpp index 724c1ad..08ccc2d 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -24,7 +24,7 @@ #include "manage_hiscore_table.h" // for ManageHiScoreTable #include "options.h" // for options #include "param.h" // for param -#include "player.h" // for Player, PLAYER_STATUS_PLAYING, PLA... +#include "player.h" // for Player, playerStatus::PLAYING, PLA... #include "scoreboard.h" // for Scoreboard, scoreboard_modes_e #include "screen.h" // for Screen #include "smart_sprite.h" // for SmartSprite @@ -155,7 +155,7 @@ void Game::init(int playerID) Player *player = getPlayer(playerID); // Cambia el estado del jugador seleccionado - player->setStatusPlaying(PLAYER_STATUS_PLAYING); + player->setStatusPlaying(playerStatus::PLAYING); // Como es el principio del juego, empieza sin inmunidad player->setInvulnerable(false); @@ -164,25 +164,31 @@ void Game::init(int playerID) switch (difficulty) { case DIFFICULTY_EASY: + { defaultEnemySpeed = BALLOON_SPEED_1; difficultyScoreMultiplier = 0.5f; difficultyColor = difficultyEasyColor; scoreboard->setColor(difficultyColor); break; + } case DIFFICULTY_NORMAL: + { defaultEnemySpeed = BALLOON_SPEED_1; difficultyScoreMultiplier = 1.0f; difficultyColor = difficultyNormalColor; scoreboard->setColor(scoreboardColor); break; + } case DIFFICULTY_HARD: + { defaultEnemySpeed = BALLOON_SPEED_5; difficultyScoreMultiplier = 1.5f; difficultyColor = difficultyHardColor; scoreboard->setColor(difficultyColor); break; + } default: break; @@ -195,10 +201,10 @@ void Game::init(int playerID) scoreboard->setName(player->getScoreBoardPanel(), player->getName()); if (player->isWaiting()) { - scoreboard->setMode(player->getScoreBoardPanel(), SCOREBOARD_MODE_GAME_OVER); + scoreboard->setMode(player->getScoreBoardPanel(), scoreboardMode::WAITING); } } - scoreboard->setMode(SCOREBOARD_CENTER_PANEL, SCOREBOARD_MODE_STAGE_INFO); + scoreboard->setMode(SCOREBOARD_CENTER_PANEL, scoreboardMode::STAGE_INFO); // Resto de variables hiScore.score = options.game.hiScoreTable[0].score; @@ -258,7 +264,7 @@ void Game::init(int playerID) { const int otherPlayer = playerID == 1 ? 2 : 1; Player *player = getPlayer(otherPlayer); - player->setStatusPlaying(PLAYER_STATUS_PLAYING); + player->setStatusPlaying(playerStatus::PLAYING); } for (auto player : players) @@ -277,8 +283,8 @@ void Game::init(int playerID) JA_EnableSound(false); // Configura los marcadores - scoreboard->setMode(SCOREBOARD_LEFT_PANEL, SCOREBOARD_MODE_DEMO); - scoreboard->setMode(SCOREBOARD_RIGHT_PANEL, SCOREBOARD_MODE_DEMO); + scoreboard->setMode(SCOREBOARD_LEFT_PANEL, scoreboardMode::DEMO); + scoreboard->setMode(SCOREBOARD_RIGHT_PANEL, scoreboardMode::DEMO); } initPaths(); @@ -859,12 +865,13 @@ void Game::updatePlayers() player->update(); if (player->isPlaying()) - { // Comprueba la colisión entre el jugador y los globos + { + // Comprueba la colisión entre el jugador y los globos if (checkPlayerBalloonCollision(player)) { killPlayer(player); - if (demo.enabled && allPlayersAreWaiting()) + if (demo.enabled && allPlayersAreNotPlaying()) { fade->setType(FADE_RANDOM_SQUARE); fade->activate(); @@ -947,8 +954,18 @@ void Game::updateStage() // Actualiza el estado de fin de la partida void Game::updateGameOver() { - // Comprueba si todos los jugadores estan muertos + // Comprueba si todos los jugadores estan esperando if (allPlayersAreWaiting()) + { + // Entonces los pone en estado de Game Over + for (auto player : players) + { + player->setStatusPlaying(playerStatus::GAME_OVER); + } + } + + // Si todos estan en estado de Game Over + if (allPlayersAreGameOver()) { if (gameOverCounter > 0) { @@ -1745,7 +1762,7 @@ void Game::killPlayer(Player *player) JA_PlaySound(playerCollisionSound); screen->shake(); JA_PlaySound(coffeeOutSound); - player->setStatusPlaying(PLAYER_STATUS_DYING); + player->setStatusPlaying(playerStatus::DYING); if (!demo.enabled) { // En el modo DEMO ni se para la musica ni se añade la puntuación a la tabla allPlayersAreNotPlaying() ? JA_StopMusic() : JA_ResumeMusic(); @@ -2237,16 +2254,12 @@ void Game::checkInput() } #endif } - else if (player->isContinue()) + else if (player->isContinue() || player->isWaiting()) { // Si no está jugando, el botón de start le permite continuar jugando if (input->checkInput(input_start, INPUT_DO_NOT_ALLOW_REPEAT, options.controller[controllerIndex].deviceType, options.controller[controllerIndex].index)) { - // Si no ha entrado ya en el estado de game over, entonces se puede continuar - if (gameOverCounter == GAME_OVER_COUNTER) - { - player->setStatusPlaying(PLAYER_STATUS_PLAYING); - } + player->setStatusPlaying(playerStatus::PLAYING); } // Si está continuando, los botones de fuego hacen decrementar el contador @@ -2269,7 +2282,7 @@ void Game::checkInput() { player->setInput(input_start); addScoreToScoreBoard(player->getRecordName(), player->getScore()); - player->setStatusPlaying(PLAYER_STATUS_CONTINUE); + player->setStatusPlaying(playerStatus::CONTINUE); } else { @@ -2296,7 +2309,7 @@ void Game::checkInput() { player->setInput(input_start); addScoreToScoreBoard(player->getRecordName(), player->getScore()); - player->setStatusPlaying(PLAYER_STATUS_CONTINUE); + player->setStatusPlaying(playerStatus::CONTINUE); } } } @@ -2403,7 +2416,7 @@ void Game::checkMusicStatus() if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) { // Si se ha completado el juego o los jugadores han terminado, detiene la música - gameCompleted || allPlayersAreWaiting() ? JA_StopMusic() : JA_PlayMusic(music); + gameCompleted || allPlayersAreGameOver() ? JA_StopMusic() : JA_PlayMusic(music); } } @@ -2589,6 +2602,18 @@ bool Game::allPlayersAreWaiting() return success; } +// Comprueba si todos los jugadores han terminado de jugar +bool Game::allPlayersAreGameOver() +{ + bool success = true; + for (auto player : players) + { + success &= player->isGameOver(); + } + + return success; +} + // Comprueba si todos los jugadores han terminado de jugar bool Game::allPlayersAreNotPlaying() { @@ -2793,6 +2818,7 @@ void Game::addScoreToScoreBoard(std::string name, int score) const hiScoreEntry_t entry = {trim(name), score}; ManageHiScoreTable *manager = new ManageHiScoreTable(&options.game.hiScoreTable); manager->add(entry); + manager->saveToFile(asset->get("score.bin")); delete manager; } @@ -2808,36 +2834,40 @@ void Game::checkPlayersStatusPlaying() { switch (player->getStatusPlaying()) { - case PLAYER_STATUS_PLAYING: - scoreboard->setMode(player->getScoreBoardPanel(), SCOREBOARD_MODE_SCORE); + case playerStatus::PLAYING: + scoreboard->setMode(player->getScoreBoardPanel(), scoreboardMode::SCORE); break; - case PLAYER_STATUS_CONTINUE: - scoreboard->setMode(player->getScoreBoardPanel(), SCOREBOARD_MODE_CONTINUE); + case playerStatus::CONTINUE: + scoreboard->setMode(player->getScoreBoardPanel(), scoreboardMode::CONTINUE); scoreboard->setContinue(player->getScoreBoardPanel(), player->getContinueCounter()); break; - case PLAYER_STATUS_WAITING: - scoreboard->setMode(player->getScoreBoardPanel(), SCOREBOARD_MODE_GAME_OVER); + case playerStatus::WAITING: + scoreboard->setMode(player->getScoreBoardPanel(), scoreboardMode::WAITING); break; - case PLAYER_STATUS_ENTERING_NAME: - scoreboard->setMode(player->getScoreBoardPanel(), SCOREBOARD_MODE_ENTER_NAME); + case playerStatus::ENTERING_NAME: + scoreboard->setMode(player->getScoreBoardPanel(), scoreboardMode::ENTER_NAME); scoreboard->setRecordName(player->getScoreBoardPanel(), player->getRecordName()); scoreboard->setSelectorPos(player->getScoreBoardPanel(), player->getRecordNamePos()); break; - case PLAYER_STATUS_DYING: + case playerStatus::DYING: break; - case PLAYER_STATUS_DIED: + case playerStatus::DIED: { - const int nextPlayerStatus = IsEligibleForHighScore(player->getScore()) ? PLAYER_STATUS_ENTERING_NAME : PLAYER_STATUS_CONTINUE; - demo.enabled ? player->setStatusPlaying(PLAYER_STATUS_WAITING) : player->setStatusPlaying(nextPlayerStatus); + const playerStatus nextPlayerStatus = IsEligibleForHighScore(player->getScore()) ? playerStatus::ENTERING_NAME : playerStatus::CONTINUE; + demo.enabled ? player->setStatusPlaying(playerStatus::WAITING) : player->setStatusPlaying(nextPlayerStatus); // addScoreToScoreBoard(player->getName(), player->getScore()); break; } + case playerStatus::GAME_OVER: + scoreboard->setMode(player->getScoreBoardPanel(), scoreboardMode::GAME_OVER); + break; + default: break; } diff --git a/source/game.h b/source/game.h index 889ed32..02d2dd5 100644 --- a/source/game.h +++ b/source/game.h @@ -411,6 +411,9 @@ private: // Comprueba si todos los jugadores han terminado de jugar bool allPlayersAreWaiting(); + // Comprueba si todos los jugadores han terminado de jugar + bool allPlayersAreGameOver(); + // Comprueba si todos los jugadores han terminado de jugar bool allPlayersAreNotPlaying(); diff --git a/source/player.cpp b/source/player.cpp index 066c02e..4c7cccb 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -4,19 +4,18 @@ #include // for rand #include // for max, min #include "animated_sprite.h" // for AnimatedSprite -#include "enter_name.h" -#include "input.h" // for inputs_e -#include "param.h" // for param -#include "texture.h" // for Texture +#include "input.h" // for inputs_e +#include "param.h" // for param +#include "texture.h" // for Texture // Constructor Player::Player(int id, float x, int y, SDL_Rect *playArea, std::vector texture, std::vector *> animations) { // Reserva memoria para los objetos - playerSprite = new AnimatedSprite(texture[0], "", animations[0]); - powerSprite = new AnimatedSprite(texture[1], "", animations[1]); + playerSprite = std::unique_ptr(new AnimatedSprite(texture[0], "", animations[0])); + powerSprite = std::unique_ptr(new AnimatedSprite(texture[1], "", animations[1])); powerSprite->getTexture()->setAlpha(224); - enterName = new EnterName(); + enterName = std::unique_ptr(new EnterName()); // Rectangulo con la zona de juego this->playArea = playArea; @@ -31,32 +30,21 @@ Player::Player(int id, float x, int y, SDL_Rect *playArea, std::vectorid = id; - statusPlaying = PLAYER_STATUS_WAITING; + statusPlaying = playerStatus::WAITING; scoreBoardPanel = 0; name = ""; setRecordName(enterName->getName()); init(); } -// Destructor -Player::~Player() -{ - delete playerSprite; - delete powerSprite; - if (enterName) - { - delete enterName; - } -} - // Iniciador void Player::init() { // Inicializa variables de estado posX = defaultPosX; posY = defaultPosY; - statusWalking = PLAYER_STATUS_WALKING_STOP; - statusFiring = PLAYER_STATUS_FIRING_NO; + statusWalking = playerStatus::WALKING_STOP; + statusFiring = playerStatus::FIRING_NO; invulnerable = true; invulnerableCounter = PLAYER_INVULNERABLE_COUNTER; powerUp = false; @@ -90,13 +78,16 @@ void Player::setInput(int input) { switch (statusPlaying) { - case PLAYER_STATUS_PLAYING: + case playerStatus::PLAYING: setInputPlaying(input); break; - case PLAYER_STATUS_ENTERING_NAME: + case playerStatus::ENTERING_NAME: setInputEnteringName(input); break; + + default: + break; } } @@ -107,29 +98,29 @@ void Player::setInputPlaying(int input) { case input_left: velX = -baseSpeed; - setWalkingStatus(PLAYER_STATUS_WALKING_LEFT); + setWalkingStatus(playerStatus::WALKING_LEFT); break; case input_right: velX = baseSpeed; - setWalkingStatus(PLAYER_STATUS_WALKING_RIGHT); + setWalkingStatus(playerStatus::WALKING_RIGHT); break; case input_fire_center: - setFiringStatus(PLAYER_STATUS_FIRING_UP); + setFiringStatus(playerStatus::FIRING_UP); break; case input_fire_left: - setFiringStatus(PLAYER_STATUS_FIRING_LEFT); + setFiringStatus(playerStatus::FIRING_LEFT); break; case input_fire_right: - setFiringStatus(PLAYER_STATUS_FIRING_RIGHT); + setFiringStatus(playerStatus::FIRING_RIGHT); break; default: velX = 0; - setWalkingStatus(PLAYER_STATUS_WALKING_STOP); + setWalkingStatus(playerStatus::WALKING_STOP); break; } } @@ -156,7 +147,7 @@ void Player::setInputEnteringName(int input) break; case input_start: - recordName = enterName->getName(); + setRecordName(enterName->getName()); break; default: @@ -204,7 +195,7 @@ void Player::move() // Si el cadaver abandona el area de juego por abajo if (playerSprite->getPosY() > param.game.playArea.rect.h) { - setStatusPlaying(PLAYER_STATUS_DIED); + setStatusPlaying(playerStatus::DIED); } } } @@ -220,11 +211,12 @@ void Player::render() } } - playerSprite->render(); + if (isRenderable()) + playerSprite->render(); } // Establece el estado del jugador cuando camina -void Player::setWalkingStatus(int status) +void Player::setWalkingStatus(playerStatus status) { // Si cambiamos de estado, reiniciamos la animación if (statusWalking != status) @@ -234,7 +226,7 @@ void Player::setWalkingStatus(int status) } // Establece el estado del jugador cuando dispara -void Player::setFiringStatus(int status) +void Player::setFiringStatus(playerStatus status) { // Si cambiamos de estado, reiniciamos la animación if (statusFiring != status) @@ -247,16 +239,16 @@ void Player::setFiringStatus(int status) void Player::setAnimation() { // Crea cadenas de texto para componer el nombre de la animación - const std::string aWalking = statusWalking == PLAYER_STATUS_WALKING_STOP ? "stand" : "walk"; - const std::string aFiring = statusFiring == PLAYER_STATUS_FIRING_UP ? "centershoot" : "sideshoot"; + const std::string aWalking = statusWalking == playerStatus::WALKING_STOP ? "stand" : "walk"; + const std::string aFiring = statusFiring == playerStatus::FIRING_UP ? "centershoot" : "sideshoot"; - const SDL_RendererFlip flipWalk = statusWalking == PLAYER_STATUS_WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; - const SDL_RendererFlip flipFire = statusFiring == PLAYER_STATUS_FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; + const SDL_RendererFlip flipWalk = statusWalking == playerStatus::WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; + const SDL_RendererFlip flipFire = statusFiring == playerStatus::FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE; // Establece la animación a partir de las cadenas if (isPlaying()) { - if (statusFiring == PLAYER_STATUS_FIRING_NO) + if (statusFiring == playerStatus::FIRING_NO) { // No esta disparando playerSprite->setCurrentAnimation(aWalking); playerSprite->setFlip(flipWalk); @@ -282,31 +274,31 @@ void Player::setAnimation() } // Obtiene el valor de la variable -int Player::getPosX() +int Player::getPosX() const { return int(posX); } // Obtiene el valor de la variable -int Player::getPosY() +int Player::getPosY() const { return posY; } // Obtiene el valor de la variable -int Player::getWidth() +int Player::getWidth() const { return width; } // Obtiene el valor de la variable -int Player::getHeight() +int Player::getHeight() const { return height; } // Indica si el jugador puede disparar -bool Player::canFire() +bool Player::canFire() const { // Si el contador a llegado a cero, podemos disparar. En caso contrario decrementamos el contador return cooldown > 0 ? false : true; @@ -331,7 +323,7 @@ void Player::updateCooldown() } else { - setFiringStatus(PLAYER_STATUS_FIRING_NO); + setFiringStatus(playerStatus::FIRING_NO); } } @@ -348,7 +340,7 @@ void Player::update() } // Obtiene la puntuación del jugador -int Player::getScore() +int Player::getScore() const { return score; } @@ -369,73 +361,89 @@ void Player::addScore(int score) } // Indica si el jugador está jugando -bool Player::isPlaying() +bool Player::isPlaying() const { - return statusPlaying == PLAYER_STATUS_PLAYING; + return statusPlaying == playerStatus::PLAYING; } // Indica si el jugador está continuando -bool Player::isContinue() +bool Player::isContinue() const { - return statusPlaying == PLAYER_STATUS_CONTINUE; + return statusPlaying == playerStatus::CONTINUE; } // Indica si el jugador está esperando -bool Player::isWaiting() +bool Player::isWaiting() const { - return statusPlaying == PLAYER_STATUS_WAITING; + return statusPlaying == playerStatus::WAITING; } // Indica si el jugador está introduciendo su nombre -bool Player::isEnteringName() +bool Player::isEnteringName() const { - return statusPlaying == PLAYER_STATUS_ENTERING_NAME; + return statusPlaying == playerStatus::ENTERING_NAME; } // Indica si el jugador está muriendose -bool Player::isDying() +bool Player::isDying() const { - return statusPlaying == PLAYER_STATUS_DYING; + return statusPlaying == playerStatus::DYING; } // Indica si el jugador ha terminado de morir -bool Player::hasDied() +bool Player::hasDied() const { - return statusPlaying == PLAYER_STATUS_DIED; + return statusPlaying == playerStatus::DIED; +} + +// Indica si el jugador ya ha terminado de jugar +bool Player::isGameOver() const +{ + return statusPlaying == playerStatus::GAME_OVER; } // Establece el estado del jugador en el juego -void Player::setStatusPlaying(int value) +void Player::setStatusPlaying(playerStatus value) { statusPlaying = value; switch (statusPlaying) { - case PLAYER_STATUS_PLAYING: - statusPlaying = PLAYER_STATUS_PLAYING; + case playerStatus::PLAYING: + { + statusPlaying = playerStatus::PLAYING; init(); break; + } - case PLAYER_STATUS_CONTINUE: + case playerStatus::CONTINUE: + { // Inicializa el contador de continuar continueTicks = SDL_GetTicks(); continueCounter = 9; + enterName->init(); + break; + } + + case playerStatus::WAITING: break; - case PLAYER_STATUS_WAITING: + case playerStatus::ENTERING_NAME: break; - case PLAYER_STATUS_ENTERING_NAME: - break; - - case PLAYER_STATUS_DYING: + case playerStatus::DYING: + { // Activa la animación de morir playerSprite->setAccelY(0.2f); playerSprite->setVelY(-6.6f); rand() % 2 == 0 ? playerSprite->setVelX(3.3f) : playerSprite->setVelX(-3.3f); break; + } - case PLAYER_STATUS_DIED: + case playerStatus::DIED: + break; + + case playerStatus::GAME_OVER: break; default: @@ -444,13 +452,13 @@ void Player::setStatusPlaying(int value) } // Obtiene el estado del jugador en el juego -int Player::getStatusPlaying() +playerStatus Player::getStatusPlaying() const { return statusPlaying; } // Obtiene el valor de la variable -float Player::getScoreMultiplier() +float Player::getScoreMultiplier() const { return scoreMultiplier; } @@ -476,7 +484,7 @@ void Player::decScoreMultiplier() } // Obtiene el valor de la variable -bool Player::isInvulnerable() +bool Player::isInvulnerable() const { return invulnerable; } @@ -489,7 +497,7 @@ void Player::setInvulnerable(bool value) } // Obtiene el valor de la variable -int Player::getInvulnerableCounter() +int Player::getInvulnerableCounter() const { return invulnerableCounter; } @@ -519,7 +527,7 @@ void Player::updateInvulnerable() } // Obtiene el valor de la variable -bool Player::isPowerUp() +bool Player::isPowerUp() const { return powerUp; } @@ -532,7 +540,7 @@ void Player::setPowerUp() } // Obtiene el valor de la variable -int Player::getPowerUpCounter() +int Player::getPowerUpCounter() const { return powerUpCounter; } @@ -558,7 +566,7 @@ void Player::updatePowerUpCounter() } // Obtiene el valor de la variable -bool Player::hasExtraHit() +bool Player::hasExtraHit() const { return extraHit; } @@ -600,7 +608,7 @@ void Player::disableInput() } // Devuelve el número de cafes actuales -int Player::getCoffees() +int Player::getCoffees() const { return coffees; } @@ -626,7 +634,7 @@ void Player::setPlayerTextures(std::vector texture) } // Obtiene el valor de la variable -int Player::getContinueCounter() +int Player::getContinueCounter() const { return continueCounter; } @@ -634,7 +642,7 @@ int Player::getContinueCounter() // Actualiza el contador de continue void Player::updateContinueCounter() { - if (statusPlaying == PLAYER_STATUS_CONTINUE) + if (statusPlaying == playerStatus::CONTINUE) { const Uint32 ticksSpeed = 1000; @@ -652,7 +660,7 @@ void Player::setScoreBoardPanel(int panel) } // Obtiene el valor de la variable -int Player::getScoreBoardPanel() +int Player::getScoreBoardPanel() const { return scoreBoardPanel; } @@ -664,7 +672,7 @@ void Player::decContinueCounter() continueCounter--; if (continueCounter < 0) { - setStatusPlaying(PLAYER_STATUS_WAITING); + setStatusPlaying(playerStatus::WAITING); } } @@ -681,19 +689,19 @@ void Player::setRecordName(std::string recordName) } // Obtiene el nombre del jugador -std::string Player::getName() +std::string Player::getName() const { return name; } // Obtiene el nombre del jugador para la tabla de mejores puntuaciones -std::string Player::getRecordName() +std::string Player::getRecordName() const { return recordName; } // Obtiene la posici´´on que se está editando del nombre del jugador para la tabla de mejores puntuaciones -int Player::getRecordNamePos() +int Player::getRecordNamePos() const { if (enterName) { @@ -710,13 +718,19 @@ void Player::setController(int index) } // Obtiene el mando que usa para ser controlado -int Player::getController() +int Player::getController() const { return controllerIndex; } // Obtiene el "id" del jugador -int Player::getId() +int Player::getId() const { return id; +} + +// Indica si el jugador se puede dibujar +bool Player::isRenderable() const +{ + return isPlaying() || isDying(); } \ No newline at end of file diff --git a/source/player.h b/source/player.h index 67a0254..53628f4 100644 --- a/source/player.h +++ b/source/player.h @@ -5,73 +5,78 @@ #include // for string, basic_string #include // for vector #include "utils.h" // for circle_t +#include "enter_name.h" +#include class AnimatedSprite; -class EnterName; class Texture; // Estados del jugador -#define PLAYER_STATUS_WALKING_LEFT 0 -#define PLAYER_STATUS_WALKING_RIGHT 1 -#define PLAYER_STATUS_WALKING_STOP 2 +enum class playerStatus +{ + WALKING_LEFT, + WALKING_RIGHT, + WALKING_STOP, -#define PLAYER_STATUS_FIRING_UP 0 -#define PLAYER_STATUS_FIRING_LEFT 1 -#define PLAYER_STATUS_FIRING_RIGHT 2 -#define PLAYER_STATUS_FIRING_NO 3 + FIRING_UP, + FIRING_LEFT, + FIRING_RIGHT, + FIRING_NO, -#define PLAYER_STATUS_PLAYING 0 -#define PLAYER_STATUS_CONTINUE 1 -#define PLAYER_STATUS_WAITING 2 -#define PLAYER_STATUS_ENTERING_NAME 3 -#define PLAYER_STATUS_DYING 4 -#define PLAYER_STATUS_DIED 5 + PLAYING, + CONTINUE, + WAITING, + ENTERING_NAME, + DYING, + DIED, + GAME_OVER, +}; // Variables del jugador -#define PLAYER_INVULNERABLE_COUNTER 200 -#define PLAYER_POWERUP_COUNTER 1500 +constexpr int PLAYER_INVULNERABLE_COUNTER = 200; +constexpr int PLAYER_POWERUP_COUNTER = 1500; // Clase Player class Player { private: // Objetos y punteros - AnimatedSprite *playerSprite; // Sprite para dibujar el jugador - AnimatedSprite *powerSprite; // Sprite para dibujar el aura del jugador con el poder a tope - SDL_Rect *playArea; // Rectangulo con la zona de juego - EnterName *enterName; + std::unique_ptr playerSprite; // Sprite para dibujar el jugador + std::unique_ptr powerSprite; // Sprite para dibujar el aura del jugador con el poder a tope + std::unique_ptr enterName; // Clase utilizada para introducir el nombre + SDL_Rect *playArea; // Rectangulo con la zona de juego // Variables - int id; // Numero de identificación para el jugador - float posX; // Posicion en el eje X - int posY; // Posicion en el eje Y - float defaultPosX; // Posición inicial para el jugador - int defaultPosY; // Posición inicial para el jugador - int width; // Anchura - int height; // Altura - float velX; // Cantidad de pixeles a desplazarse en el eje X - int velY; // Cantidad de pixeles a desplazarse en el eje Y - float baseSpeed; // Velocidad base del jugador - int cooldown; // Contador durante el cual no puede disparar - int score; // Puntos del jugador - float scoreMultiplier; // Multiplicador de puntos - int statusWalking; // Estado del jugador al moverse - int statusFiring; // Estado del jugador al disparar - int statusPlaying; // Estado del jugador en el juego - bool invulnerable; // Indica si el jugador es invulnerable - int invulnerableCounter; // Contador para la invulnerabilidad - bool extraHit; // Indica si el jugador tiene un toque extra - int coffees; // Indica cuantos cafes lleva acumulados - bool powerUp; // Indica si el jugador tiene activo el modo PowerUp - int powerUpCounter; // Temporizador para el modo PowerUp - int powerUpDespX; // Desplazamiento del sprite de PowerUp respecto al sprite del jugador - bool input; // Indica si puede recibir ordenes de entrada - circle_t collider; // Circulo de colisión del jugador - int continueCounter; // Contador para poder continuar - Uint32 continueTicks; // Variable para poder cambiar el contador de continue en función del tiempo - int scoreBoardPanel; // Panel del marcador asociado al jugador - std::string name; // Nombre del jugador - std::string recordName; // Nombre del jugador para l atabla de mejores puntuaciones - int controllerIndex; // Indice del array de mandos que utilizará para moverse + int id; // Numero de identificación para el jugador + float posX; // Posicion en el eje X + int posY; // Posicion en el eje Y + float defaultPosX; // Posición inicial para el jugador + int defaultPosY; // Posición inicial para el jugador + int width; // Anchura + int height; // Altura + float velX; // Cantidad de pixeles a desplazarse en el eje X + int velY; // Cantidad de pixeles a desplazarse en el eje Y + float baseSpeed; // Velocidad base del jugador + int cooldown; // Contador durante el cual no puede disparar + int score; // Puntos del jugador + float scoreMultiplier; // Multiplicador de puntos + playerStatus statusWalking; // Estado del jugador al moverse + playerStatus statusFiring; // Estado del jugador al disparar + playerStatus statusPlaying; // Estado del jugador en el juego + bool invulnerable; // Indica si el jugador es invulnerable + int invulnerableCounter; // Contador para la invulnerabilidad + bool extraHit; // Indica si el jugador tiene un toque extra + int coffees; // Indica cuantos cafes lleva acumulados + bool powerUp; // Indica si el jugador tiene activo el modo PowerUp + int powerUpCounter; // Temporizador para el modo PowerUp + int powerUpDespX; // Desplazamiento del sprite de PowerUp respecto al sprite del jugador + bool input; // Indica si puede recibir ordenes de entrada + circle_t collider; // Circulo de colisión del jugador + int continueCounter; // Contador para poder continuar + Uint32 continueTicks; // Variable para poder cambiar el contador de continue en función del tiempo + int scoreBoardPanel; // Panel del marcador asociado al jugador + std::string name; // Nombre del jugador + std::string recordName; // Nombre del jugador para l atabla de mejores puntuaciones + int controllerIndex; // Indice del array de mandos que utilizará para moverse // Actualiza el circulo de colisión a la posición del jugador void shiftColliders(); @@ -82,12 +87,15 @@ private: // Actualiza el contador de continue void updateContinueCounter(); + // Indica si el jugador se puede dibujar + bool isRenderable() const; + public: // Constructor Player(int id, float x, int y, SDL_Rect *playArea, std::vector texture, std::vector *> animations); // Destructor - ~Player(); + ~Player() = default; // Iniciador void init(); @@ -114,28 +122,28 @@ public: void move(); // Establece el estado del jugador - void setWalkingStatus(int status); + void setWalkingStatus(playerStatus status); // Establece el estado del jugador - void setFiringStatus(int status); + void setFiringStatus(playerStatus status); // Establece la animación correspondiente al estado void setAnimation(); // Obtiene el valor de la variable - int getPosX(); + int getPosX() const; // Obtiene el valor de la variable - int getPosY(); + int getPosY() const; // Obtiene el valor de la variable - int getWidth(); + int getWidth() const; // Obtiene el valor de la variable - int getHeight(); + int getHeight() const; // Indica si el jugador puede disparar - bool canFire(); + bool canFire() const; // Establece el valor de la variable void setFireCooldown(int time); @@ -144,7 +152,7 @@ public: void updateCooldown(); // Obtiene la puntuación del jugador - int getScore(); + int getScore() const; // Asigna un valor a la puntuación del jugador void setScore(int score); @@ -153,31 +161,34 @@ public: void addScore(int score); // Indica si el jugador está jugando - bool isPlaying(); + bool isPlaying() const; // Indica si el jugador está continuando - bool isContinue(); + bool isContinue() const; // Indica si el jugador está esperando - bool isWaiting(); + bool isWaiting() const; // Indica si el jugador está introduciendo su nombre - bool isEnteringName(); + bool isEnteringName() const; // Indica si el jugador está muriendose - bool isDying(); + bool isDying() const; // Indica si el jugador ha terminado de morir - bool hasDied(); + bool hasDied() const; + + // Indica si el jugador ya ha terminado de jugar + bool isGameOver() const; // Establece el estado del jugador en el juego - void setStatusPlaying(int value); + void setStatusPlaying(playerStatus value); // Obtiene el estado del jugador en el juego - int getStatusPlaying(); + playerStatus getStatusPlaying() const; // Obtiene el valor de la variable - float getScoreMultiplier(); + float getScoreMultiplier() const; // Establece el valor de la variable void setScoreMultiplier(float value); @@ -189,25 +200,25 @@ public: void decScoreMultiplier(); // Obtiene el valor de la variable - bool isInvulnerable(); + bool isInvulnerable() const; // Establece el valor del estado void setInvulnerable(bool value); // Obtiene el valor de la variable - int getInvulnerableCounter(); + int getInvulnerableCounter() const; // Establece el valor de la variable void setInvulnerableCounter(int value); // Obtiene el valor de la variable - bool isPowerUp(); + bool isPowerUp() const; // Establece el valor de la variable a verdadero void setPowerUp(); // Obtiene el valor de la variable - int getPowerUpCounter(); + int getPowerUpCounter() const; // Establece el valor de la variable void setPowerUpCounter(int value); @@ -216,7 +227,7 @@ public: void updatePowerUpCounter(); // Obtiene el valor de la variable - bool hasExtraHit(); + bool hasExtraHit() const; // Concede un toque extra al jugador void giveExtraHit(); @@ -231,19 +242,19 @@ public: void disableInput(); // Devuelve el número de cafes actuales - int getCoffees(); + int getCoffees() const; // Obtiene el circulo de colisión circle_t &getCollider(); // Obtiene el valor de la variable - int getContinueCounter(); + int getContinueCounter() const; // Le asigna un panel en el marcador al jugador void setScoreBoardPanel(int panel); // Obtiene el valor de la variable - int getScoreBoardPanel(); + int getScoreBoardPanel() const; // Decrementa el contador de continuar void decContinueCounter(); @@ -255,20 +266,20 @@ public: void setRecordName(std::string recordName); // Obtiene el nombre del jugador - std::string getName(); + std::string getName() const; // Obtiene el nombre del jugador para la tabla de mejores puntuaciones - std::string getRecordName(); + std::string getRecordName() const; // Obtiene la posici´´on que se está editando del nombre del jugador para la tabla de mejores puntuaciones - int getRecordNamePos(); + int getRecordNamePos() const; // Establece el mando que usará para ser controlado void setController(int index); // Obtiene el mando que usa para ser controlado - int getController(); + int getController() const; // Obtiene el "id" del jugador - int getId(); + int getId() const; }; diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index efc15c0..9fea7bc 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -37,9 +37,9 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer) hiScoreName = ""; color = {0, 0, 0}; rect = {0, 0, 320, 40}; - panel[SCOREBOARD_LEFT_PANEL].mode = SCOREBOARD_MODE_SCORE; - panel[SCOREBOARD_RIGHT_PANEL].mode = SCOREBOARD_MODE_SCORE; - panel[SCOREBOARD_CENTER_PANEL].mode = SCOREBOARD_MODE_STAGE_INFO; + panel[SCOREBOARD_LEFT_PANEL].mode = scoreboardMode::SCORE; + panel[SCOREBOARD_RIGHT_PANEL].mode = scoreboardMode::SCORE; + panel[SCOREBOARD_CENTER_PANEL].mode = scoreboardMode::STAGE_INFO; ticks = SDL_GetTicks(); counter = 0; @@ -248,7 +248,8 @@ void Scoreboard::fillPanelTextures() switch (panel[i].mode) { - case SCOREBOARD_MODE_SCORE: + case scoreboardMode::SCORE: + { // SCORE textScoreBoard->writeCentered(slot4_1.x, slot4_1.y, name[i]); textScoreBoard->writeCentered(slot4_2.x, slot4_2.y, updateScoreText(score[i])); @@ -257,26 +258,45 @@ void Scoreboard::fillPanelTextures() textScoreBoard->writeCentered(slot4_3.x, slot4_3.y, lang::getText(55)); textScoreBoard->writeCentered(slot4_4.x, slot4_4.y, std::to_string(mult[i]).substr(0, 3)); break; + } - case SCOREBOARD_MODE_DEMO: + case scoreboardMode::DEMO: + { + // DEMO MODE textScoreBoard->writeCentered(slot4_1.x, slot4_1.y + 4, lang::getText(101)); + + // PRESS START TO PLAY if (counter % 10 < 8) { textScoreBoard->writeCentered(slot4_3.x, slot4_3.y - 2, lang::getText(103)); textScoreBoard->writeCentered(slot4_4.x, slot4_4.y - 2, lang::getText(104)); } break; + } - case SCOREBOARD_MODE_GAME_OVER: + case scoreboardMode::WAITING: + { + // GAME OVER textScoreBoard->writeCentered(slot4_1.x, slot4_1.y + 4, lang::getText(102)); + + // PRESS START TO PLAY if (counter % 10 < 8) { textScoreBoard->writeCentered(slot4_3.x, slot4_3.y - 2, lang::getText(103)); textScoreBoard->writeCentered(slot4_4.x, slot4_4.y - 2, lang::getText(104)); } break; + } - case SCOREBOARD_MODE_STAGE_INFO: + case scoreboardMode::GAME_OVER: + { + // GAME OVER + textScoreBoard->writeCentered(slot4_1.x, slot4_1.y + 4, lang::getText(102)); + break; + } + + case scoreboardMode::STAGE_INFO: + { // STAGE textScoreBoard->writeCentered(slot4_1.x, slot4_1.y, lang::getText(57) + std::to_string(stage)); @@ -290,8 +310,10 @@ void Scoreboard::fillPanelTextures() textScoreBoard->writeCentered(slot4_3.x, slot4_3.y, lang::getText(56)); textScoreBoard->writeCentered(slot4_4.x, slot4_4.y, hiScoreName + " - " + updateScoreText(hiScore)); break; + } - case SCOREBOARD_MODE_CONTINUE: + case scoreboardMode::CONTINUE: + { // SCORE textScoreBoard->writeCentered(slot4_1.x, slot4_1.y, name[i]); textScoreBoard->writeCentered(slot4_2.x, slot4_2.y, updateScoreText(score[i])); @@ -300,8 +322,9 @@ void Scoreboard::fillPanelTextures() textScoreBoard->writeCentered(slot4_3.x, slot4_3.y, lang::getText(105)); textScoreBoard->writeCentered(slot4_4.x, slot4_4.y, std::to_string(continueCounter[i])); break; + } - case SCOREBOARD_MODE_ENTER_NAME: + case scoreboardMode::ENTER_NAME: { // SCORE textScoreBoard->writeCentered(slot4_1.x, slot4_1.y, name[i]); @@ -314,7 +337,7 @@ void Scoreboard::fillPanelTextures() for (int j = 0; j < (int)recordName[i].size(); ++j) { if (j == selectorPos[i]) - {// La letra seleccionada se pinta de forma intermitente + { // La letra seleccionada se pinta de forma intermitente if (counter % 3 > 0) { SDL_RenderDrawLine(renderer, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h); @@ -413,7 +436,7 @@ void Scoreboard::recalculateAnchors() } // Establece el modo del marcador -void Scoreboard::setMode(int index, scoreboard_modes_e mode) +void Scoreboard::setMode(int index, scoreboardMode mode) { panel[index].mode = mode; } diff --git a/source/scoreboard.h b/source/scoreboard.h index 5613fd2..f74caa1 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -12,29 +12,29 @@ class Text; class Texture; // Defines -#define SCOREBOARD_LEFT_PANEL 0 -#define SCOREBOARD_CENTER_PANEL 1 -#define SCOREBOARD_RIGHT_PANEL 2 -#define SCOREBOARD_MAX_PANELS 3 - -#define SCOREBOARD_TICK_SPEED 100 +constexpr int SCOREBOARD_LEFT_PANEL = 0; +constexpr int SCOREBOARD_CENTER_PANEL = 1; +constexpr int SCOREBOARD_RIGHT_PANEL = 2; +constexpr int SCOREBOARD_MAX_PANELS = 3; +constexpr int SCOREBOARD_TICK_SPEED = 100; // Enums -enum scoreboard_modes_e +enum class scoreboardMode { - SCOREBOARD_MODE_SCORE, - SCOREBOARD_MODE_STAGE_INFO, - SCOREBOARD_MODE_CONTINUE, - SCOREBOARD_MODE_GAME_OVER, - SCOREBOARD_MODE_DEMO, - SCOREBOARD_MODE_ENTER_NAME, - SCOREBOARD_MODE_NUM_MODES, + SCORE, + STAGE_INFO, + CONTINUE, + WAITING, + GAME_OVER, + DEMO, + ENTER_NAME, + NUM_MODES, }; // Structs struct panel_t { - scoreboard_modes_e mode; // Modo en el que se encuentra el panel + scoreboardMode mode; // Modo en el que se encuentra el panel SDL_Rect pos; // Posición donde dibujar el panel dentro del marcador }; @@ -146,5 +146,5 @@ public: void setPos(SDL_Rect rect); // Establece el modo del marcador - void setMode(int index, scoreboard_modes_e mode); + void setMode(int index, scoreboardMode mode); }; diff --git a/source/title.cpp b/source/title.cpp index 5e4b03e..7408460 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -378,7 +378,6 @@ void Title::swapControllers() for (int i = 0; i < MAX_CONTROLLERS; ++i) { const int index = playerControllerIndex[i]; - //if (options.controller[index].name != "NO NAME") if (options.controller[index].plugged) { text[i] = "Jugador " + std::to_string(i + 1) + ": " + options.controller[index].name;