diff --git a/source/director.cpp b/source/director.cpp index 4a8fb84..bcdf70c 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -9,7 +9,6 @@ #include // for Uint32 #include // for SDL_GetTicks #include // for errno, EACCES, EEXIST, ENAMETOO... -#include // for getpwuid, passwd #include // for printf, perror, size_t #include // for strcmp #include // for stat, mkdir, S_IRWXU @@ -34,6 +33,10 @@ #include "title.h" // for Title #include "utils.h" // for music_file_t, sound_file_t, opt... +#ifndef _WIN32 +#include // for getpwuid, passwd +#endif + // Constructor Director::Director(int argc, char *argv[]) { diff --git a/source/enter_name.cpp b/source/enter_name.cpp index ac38004..2da0118 100644 --- a/source/enter_name.cpp +++ b/source/enter_name.cpp @@ -1,13 +1,17 @@ #include "enter_name.h" -#include // for max, min +#include // for max, min // Constructor EnterName::EnterName(std::string *name) { - characterList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + // Obtiene el puntero al nombre this->name = name; + + // Inicia la lista de caracteres permitidos + characterList = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; pos = 0; numCharacters = (int)characterList.size(); + for (int i = 0; i < NAME_LENGHT; ++i) { characterIndex[i] = 0; @@ -33,13 +37,32 @@ void EnterName::decPos() pos = std::max(pos, 0); } +// Incrementa el índice +void EnterName::incIndex() +{ + ++characterIndex[pos]; + if (characterIndex[pos] >= numCharacters) + { + characterIndex[pos] = 0; + } +} + +// Decrementa el índice +void EnterName::decIndex() +{ + --characterIndex[pos]; + if (characterIndex[pos] < 0) + { + characterIndex[pos] = numCharacters - 1; + } +} + // Actualiza la variable void EnterName::updateName() { name->clear(); for (int i = 0; i < NAME_LENGHT; ++i) { - name->append("a"); - //name->append(characterIndex[i] = 0; + name->push_back(characterList[characterIndex[i]]); } -} \ No newline at end of file +} diff --git a/source/enter_name.h b/source/enter_name.h index 7c367ab..fcc256c 100644 --- a/source/enter_name.h +++ b/source/enter_name.h @@ -4,12 +4,20 @@ #define NAME_LENGHT 8 +/* + Un array, "characterList", contiene la lista de caracteres + Un segundo array, "characterIndex", contiene el indice a "characterList" de cada una de las letras que conforman el nombre + "pos" es la posición de "characterIndex" que se está modificando + Izquierda o derecha modifican "pos", arriba o abajo modifican el índice de "characterIndex[pos]" + Pulsar cualquier botón, mueve "pos" a la derecha. Al pulsar el botón en la ´´ultima posición se finaliza la introducción de nombres +*/ + // Clase EnterName class EnterName { private: std::string characterList; // Lista de todos los caracteres permitidos - std::string *name; // Nombre introducido + std::string *name; // Nombre introducido int pos; // Posición a editar del nombre int numCharacters; // Cantidad de caracteres de la lista de caracteres int characterIndex[NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre @@ -20,6 +28,12 @@ private: // Decrementa la posición void decPos(); + // Incrementa el índice + void incIndex(); + + // Decrementa el índice + void decIndex(); + // Actualiza la variable void updateName(); diff --git a/source/game.cpp b/source/game.cpp index fe4dbce..9156b30 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,34 +1,34 @@ #include "game.h" -#include // for SDL_BLENDMODE_BLEND -#include // for SDLK_1, SDLK_2, SDLK_3, SDLK_h -#include // for SDL_PIXELFORMAT_RGBA8888 -#include // for SDL_RWFromFile, SDL_RWclose, SDL_R... -#include // for SDL_GetTicks -#include // for SDL_WINDOWEVENT_FOCUS_GAINED, SDL_... -#include // for rand -#include // for min -#include // for basic_ifstream -#include // for char_traits, basic_istream, ifstream -#include "asset.h" // for Asset -#include "background.h" // for Background -#include "balloon.h" // for Balloon, BALLOON_SPEED_1, BALLOON_... -#include "bullet.h" // for Bullet, BULLET_LEFT, BULLET_RIGHT -#include "enemy_formations.h" // for stage_t, EnemyFormations, enemyIni... -#include "explosions.h" // for Explosions -#include "fade.h" // for Fade, FADE_RANDOM_SQUARE, FADE_VEN... -#include "input.h" // for inputs_e, Input, INPUT_DO_NOT_ALLO... -#include "item.h" // for Item, ITEM_COFFEE_MACHINE, ITEM_CLOCK -#include "jail_audio.h" // for JA_PlaySound, JA_DeleteSound, JA_L... -#include "lang.h" // for getText -#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 "scoreboard.h" // for Scoreboard, scoreboard_modes_e -#include "screen.h" // for Screen -#include "smart_sprite.h" // for SmartSprite -#include "text.h" // for Text, TXT_CENTER -#include "texture.h" // for Texture +#include // for SDL_BLENDMODE_BLEND +#include // for SDLK_1, SDLK_2, SDLK_3, SDLK_h +#include // for SDL_PIXELFORMAT_RGBA8888 +#include // for SDL_RWFromFile, SDL_RWclose, SDL_R... +#include // for SDL_GetTicks +#include // for SDL_WINDOWEVENT_FOCUS_GAINED, SDL_... +#include // for rand +#include // for min +#include // for basic_ifstream +#include // for char_traits, basic_istream, ifstream +#include "asset.h" // for Asset +#include "background.h" // for Background +#include "balloon.h" // for Balloon, BALLOON_SPEED_1, BALLOON_... +#include "bullet.h" // for Bullet, BULLET_LEFT, BULLET_RIGHT +#include "enemy_formations.h" // for stage_t, EnemyFormations, enemyIni... +#include "explosions.h" // for Explosions +#include "fade.h" // for Fade, FADE_RANDOM_SQUARE, FADE_VEN... +#include "input.h" // for inputs_e, Input, INPUT_DO_NOT_ALLO... +#include "item.h" // for Item, ITEM_COFFEE_MACHINE, ITEM_CLOCK +#include "jail_audio.h" // for JA_PlaySound, JA_DeleteSound, JA_L... +#include "lang.h" // for getText +#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 "scoreboard.h" // for Scoreboard, scoreboard_modes_e +#include "screen.h" // for Screen +#include "smart_sprite.h" // for SmartSprite +#include "text.h" // for Text, TXT_CENTER +#include "texture.h" // for Texture struct JA_Music_t; struct JA_Sound_t; @@ -1744,11 +1744,10 @@ void Game::killPlayer(Player *player) JA_PlaySound(playerCollisionSound); screen->shake(); JA_PlaySound(coffeeOutSound); - demo.enabled ? player->setStatusPlaying(PLAYER_STATUS_WAITING) : player->setStatusPlaying(PLAYER_STATUS_CONTINUE); + player->setStatusPlaying(PLAYER_STATUS_DYING); if (!demo.enabled) { // En el modo DEMO ni se para la musica ni se añade la puntuación a la tabla - allPlayersAreWaiting() ? JA_StopMusic() : JA_ResumeMusic(); - addScoreToScoreBoard(player->getName(), player->getScore()); + allPlayersAreNotPlaying() ? JA_StopMusic() : JA_ResumeMusic(); } } } @@ -2584,6 +2583,18 @@ bool Game::allPlayersAreWaiting() return success; } +// Comprueba si todos los jugadores han terminado de jugar +bool Game::allPlayersAreNotPlaying() +{ + bool success = true; + for (auto player : players) + { + success &= !player->isPlaying(); + } + + return success; +} + // Comprueba los eventos que hay en cola void Game::checkEvents() { @@ -2809,12 +2820,33 @@ void Game::checkPlayersStatusPlaying() scoreboard->setMode(player->getScoreBoardPanel(), SCOREBOARD_MODE_GAME_OVER); break; + case PLAYER_STATUS_ENTERING_NAME: + scoreboard->setMode(player->getScoreBoardPanel(), SCOREBOARD_MODE_ENTER_NAME); + break; + + case PLAYER_STATUS_DYING: + break; + + case PLAYER_STATUS_DIED: + { + const int nextPlayerStatus = IsEligibleForHighScore(player->getScore()) ? PLAYER_STATUS_ENTERING_NAME : PLAYER_STATUS_CONTINUE; + demo.enabled ? player->setStatusPlaying(PLAYER_STATUS_WAITING) : player->setStatusPlaying(nextPlayerStatus); + // addScoreToScoreBoard(player->getName(), player->getScore()); + break; + } + default: break; } } } +// Comprueba si la puntuación entra en la tabla de mejores puntuaciones +bool Game::IsEligibleForHighScore(int score) +{ + return score > options.game.hiScoreTable.back().score; +} + // Obtiene un jugador a partir de su "id" Player *Game::getPlayer(int id) { diff --git a/source/game.h b/source/game.h index e667614..7f150e6 100644 --- a/source/game.h +++ b/source/game.h @@ -1,12 +1,12 @@ #pragma once -#include // for SDL_Event -#include // for SDL_Renderer, SDL_Texture -#include // for Uint32 -#include // for string -#include // for vector -#include "section.h" // for options_e -#include "utils.h" // for demoKeys_t, color_t, hiScoreEntry_t +#include // for SDL_Event +#include // for SDL_Renderer, SDL_Texture +#include // for Uint32 +#include // for string +#include // for vector +#include "section.h" // for options_e +#include "utils.h" // for demoKeys_t, color_t, hiScoreEntry_t class Asset; class Background; class Balloon; @@ -410,6 +410,9 @@ private: // Comprueba si todos los jugadores han terminado de jugar bool allPlayersAreWaiting(); + // Comprueba si todos los jugadores han terminado de jugar + bool allPlayersAreNotPlaying(); + // Carga las animaciones void loadAnimations(std::string filePath, std::vector *buffer); @@ -434,6 +437,9 @@ private: // Añade una puntuación a la tabla de records void addScoreToScoreBoard(std::string name, int score); + // Comprueba si la puntuación entra en la tabla de mejores puntuaciones + bool IsEligibleForHighScore(int score); + // Comprueba el estado de juego de los jugadores void checkPlayersStatusPlaying(); diff --git a/source/player.cpp b/source/player.cpp index b2b0ab8..b6a0aa5 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -1,12 +1,12 @@ #include "player.h" -#include // for SDL_FLIP_HORIZONTAL, SDL_FLIP_NONE, SDL... -#include // for SDL_GetTicks -#include // for rand -#include // for max, min -#include "animated_sprite.h" // for AnimatedSprite -#include "input.h" // for inputs_e -#include "param.h" // for param -#include "texture.h" // for Texture +#include // for SDL_FLIP_HORIZONTAL, SDL_FLIP_NONE, SDL... +#include // for SDL_GetTicks +#include // for rand +#include // for max, min +#include "animated_sprite.h" // for AnimatedSprite +#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) @@ -58,7 +58,7 @@ void Player::init() coffees = 0; input = true; continueTicks = 0; - continueCounter = 9; + continueCounter = 20; width = 30; height = 30; collider.r = 9; @@ -133,7 +133,7 @@ void Player::move() powerSprite->setPosX(getPosX() - powerUpDespX); } - else + else if (isDying()) { playerSprite->update(); @@ -147,6 +147,12 @@ void Player::move() // Rebota playerSprite->setVelX(-vx); } + + // Si el cadaver abandona el area de juego por abajo + if (playerSprite->getPosY() > param.game.playArea.rect.h) + { + setStatusPlaying(PLAYER_STATUS_DIED); + } } } @@ -327,6 +333,24 @@ bool Player::isWaiting() return statusPlaying == PLAYER_STATUS_WAITING; } +// Indica si el jugador está introduciendo su nombre +bool Player::isEnteringName() +{ + return statusPlaying == PLAYER_STATUS_ENTERING_NAME; +} + +// Indica si el jugador está muriendose +bool Player::isDying() +{ + return statusPlaying == PLAYER_STATUS_DYING; +} + +// Indica si el jugador ha terminado de morir +bool Player::hasDied() +{ + return statusPlaying == PLAYER_STATUS_DIED; +} + // Establece el estado del jugador en el juego void Player::setStatusPlaying(int value) { @@ -340,18 +364,25 @@ void Player::setStatusPlaying(int value) break; case PLAYER_STATUS_CONTINUE: - // 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); - // Inicializa el contador de continuar continueTicks = SDL_GetTicks(); continueCounter = 9; break; case PLAYER_STATUS_WAITING: + break; + case PLAYER_STATUS_ENTERING_NAME: + break; + + case PLAYER_STATUS_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: break; default: @@ -578,7 +609,6 @@ void Player::decContinueCounter() { continueTicks = SDL_GetTicks(); continueCounter--; - if (continueCounter < 0) { setStatusPlaying(PLAYER_STATUS_WAITING); diff --git a/source/player.h b/source/player.h index 993489f..4d1838c 100644 --- a/source/player.h +++ b/source/player.h @@ -1,10 +1,10 @@ #pragma once -#include // for SDL_Rect -#include // for Uint32 -#include // for string, basic_string -#include // for vector -#include "utils.h" // for circle_t +#include // for SDL_Rect +#include // for Uint32 +#include // for string, basic_string +#include // for vector +#include "utils.h" // for circle_t class AnimatedSprite; class Texture; @@ -21,6 +21,9 @@ class Texture; #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 // Variables del jugador #define PLAYER_INVULNERABLE_COUNTER 200 @@ -149,6 +152,15 @@ public: // Indica si el jugador está esperando bool isWaiting(); + // Indica si el jugador está introduciendo su nombre + bool isEnteringName(); + + // Indica si el jugador está muriendose + bool isDying(); + + // Indica si el jugador ha terminado de morir + bool hasDied(); + // Establece el estado del jugador en el juego void setStatusPlaying(int value); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 9dc3927..9b2dc80 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -287,6 +287,10 @@ void Scoreboard::fillPanelTextures() textScoreBoard->writeCentered(slot4_4.x, slot4_4.y, std::to_string(continueCounter[i])); break; + case SCOREBOARD_MODE_ENTER_NAME: + textScoreBoard->writeCentered(slot4_1.x, slot4_1.y, "ENTER NAME"); + break; + default: break; } diff --git a/source/scoreboard.h b/source/scoreboard.h index 42232d5..03e5dd8 100644 --- a/source/scoreboard.h +++ b/source/scoreboard.h @@ -27,6 +27,7 @@ enum scoreboard_modes_e SCOREBOARD_MODE_CONTINUE, SCOREBOARD_MODE_GAME_OVER, SCOREBOARD_MODE_DEMO, + SCOREBOARD_MODE_ENTER_NAME, SCOREBOARD_MODE_NUM_MODES, };