Si no poses cap nom, tria un nom de jailer al azar

This commit is contained in:
2025-03-10 19:59:21 +01:00
parent e0c10f83d6
commit c88a277cba
6 changed files with 46 additions and 36 deletions

View File

@@ -13,7 +13,7 @@ EnterName::EnterName()
void EnterName::init(const std::string &name) void EnterName::init(const std::string &name)
{ {
// No se pasa ningún nombre // No se pasa ningún nombre
if (name == "") if (name.empty())
{ {
name_ = "A"; name_ = "A";
position_ = 0; position_ = 0;
@@ -24,7 +24,7 @@ void EnterName::init(const std::string &name)
{ {
name_ = name; name_ = name;
position_ = name_.length(); position_ = name_.length();
position_overflow_ = position_ >= NAME_LENGHT ? true : false; position_overflow_ = position_ >= MAX_NAME_LENGHT ? true : false;
} }
// Inicializa el vector de indices con el nombre y espacios // Inicializa el vector de indices con el nombre y espacios
@@ -42,15 +42,15 @@ void EnterName::incPosition()
++position_; ++position_;
if (position_ >= NAME_LENGHT) if (position_ >= MAX_NAME_LENGHT)
{ {
position_ = NAME_LENGHT; // Mantenemos en el índice máximo válido. position_ = MAX_NAME_LENGHT; // Mantenemos en el índice máximo válido.
position_overflow_ = true; // Activamos el flag de overflow. position_overflow_ = true; // Activamos el flag de overflow.
} }
else else
{ {
// Copiamos el índice del carácter anterior si es posible. // Copiamos el índice del carácter anterior si es posible.
if (position_ > 0 && position_ < NAME_LENGHT) if (position_ > 0 && position_ < MAX_NAME_LENGHT)
{ {
character_index_[position_] = character_index_[position_ - 1]; character_index_[position_] = character_index_[position_ - 1];
} }
@@ -71,7 +71,7 @@ void EnterName::decPosition()
{ {
// Si estaba en overflow, lo desactivamos y mantenemos position_ en el máximo. // Si estaba en overflow, lo desactivamos y mantenemos position_ en el máximo.
position_overflow_ = false; position_overflow_ = false;
position_ = NAME_LENGHT - 1; position_ = MAX_NAME_LENGHT - 1;
} }
else else
{ {
@@ -80,7 +80,7 @@ void EnterName::decPosition()
--position_; --position_;
// Limpiamos el carácter siguiente si el índice es válido. // Limpiamos el carácter siguiente si el índice es válido.
if (position_ + 1 < NAME_LENGHT) if (position_ + 1 < MAX_NAME_LENGHT)
{ {
character_index_[position_ + 1] = 0; character_index_[position_ + 1] = 0;
} }
@@ -93,7 +93,7 @@ void EnterName::decPosition()
} }
// Si position_ es menor que NAME_LENGHT, aseguramos que el overflow esté desactivado. // Si position_ es menor que NAME_LENGHT, aseguramos que el overflow esté desactivado.
if (position_ < NAME_LENGHT) if (position_ < MAX_NAME_LENGHT)
{ {
position_overflow_ = false; position_overflow_ = false;
} }
@@ -102,7 +102,6 @@ void EnterName::decPosition()
updateNameFromCharacterIndex(); updateNameFromCharacterIndex();
} }
// Incrementa el índice // Incrementa el índice
void EnterName::incIndex() void EnterName::incIndex()
{ {
@@ -139,7 +138,7 @@ void EnterName::decIndex()
void EnterName::updateNameFromCharacterIndex() void EnterName::updateNameFromCharacterIndex()
{ {
name_.clear(); name_.clear();
for (int i = 0; i < NAME_LENGHT; ++i) for (int i = 0; i < MAX_NAME_LENGHT; ++i)
{ {
name_.push_back(character_list_[character_index_[i]]); name_.push_back(character_list_[character_index_[i]]);
} }
@@ -150,13 +149,13 @@ void EnterName::updateNameFromCharacterIndex()
void EnterName::initCharacterIndex(const std::string &name) void EnterName::initCharacterIndex(const std::string &name)
{ {
// Rellena de espacios // Rellena de espacios
for (size_t i = 0; i < NAME_LENGHT; ++i) for (size_t i = 0; i < MAX_NAME_LENGHT; ++i)
{ {
character_index_[i] = 0; character_index_[i] = 0;
} }
// Coloca los índices en función de los caracteres que forman el nombre // Coloca los índices en función de los caracteres que forman el nombre
for (size_t i = 0; i < name.substr(0, NAME_LENGHT).size(); ++i) for (size_t i = 0; i < name.substr(0, MAX_NAME_LENGHT).size(); ++i)
{ {
character_index_[i] = findIndex(name.at(i)); character_index_[i] = findIndex(name.at(i));
} }
@@ -166,7 +165,11 @@ void EnterName::initCharacterIndex(const std::string &name)
int EnterName::findIndex(char character) const int EnterName::findIndex(char character) const
{ {
for (size_t i = 0; i < character_list_.size(); ++i) for (size_t i = 0; i < character_list_.size(); ++i)
{
if (character == character_list_.at(i)) if (character == character_list_.at(i))
{
return i; return i;
}
}
return 0; return 0;
} }

View File

@@ -3,7 +3,7 @@
#include <string> #include <string>
#include "utils.h" #include "utils.h"
constexpr int NAME_LENGHT = 6; constexpr int MAX_NAME_LENGHT = 6;
/* /*
Un array, "characterList", contiene la lista de caracteres Un array, "characterList", contiene la lista de caracteres
@@ -21,7 +21,7 @@ private:
std::string name_; // Nombre introducido std::string name_; // Nombre introducido
int position_ = 0; // Posición a editar del nombre int position_ = 0; // Posición a editar del nombre
bool position_overflow_ = false; // Indica si hemos incrementado la posición más allá del límite bool position_overflow_ = false; // Indica si hemos incrementado la posición más allá del límite
int character_index_[NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre int character_index_[MAX_NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre
// Actualiza el nombre a partir de la lista de índices // Actualiza el nombre a partir de la lista de índices
void updateNameFromCharacterIndex(); void updateNameFromCharacterIndex();

View File

@@ -3,6 +3,7 @@
#include <SDL2/SDL_rect.h> // Para SDL_Rect, SDL_Point #include <SDL2/SDL_rect.h> // Para SDL_Rect, SDL_Point
#include <SDL2/SDL_render.h> // Para SDL_RendererFlip #include <SDL2/SDL_render.h> // Para SDL_RendererFlip
#include <memory> // Para shared_ptr #include <memory> // Para shared_ptr
#include <algorithm>
#include "sprite.h" // Para Sprite #include "sprite.h" // Para Sprite
class Texture; // lines 8-8 class Texture; // lines 8-8

View File

@@ -149,6 +149,11 @@ void Player::setInputEnteringName(InputType input)
break; break;
case InputType::START: case InputType::START:
last_enter_name_ = getRecordName(); last_enter_name_ = getRecordName();
if (last_enter_name_.empty())
{
const std::array<std::string, 8> NAMES = {"BAL1", "TABE", "DOC", "MON", "SAM1", "JORDI", "JDES", "PEPE"};
last_enter_name_ = NAMES.at(rand() % NAMES.size());
}
break; break;
default: default:
break; break;
@@ -166,9 +171,9 @@ void Player::move()
pos_x_ += vel_x_; pos_x_ += vel_x_;
// Si el jugador abandona el area de juego por los laterales, restaura su posición // Si el jugador abandona el area de juego por los laterales, restaura su posición
const float min_x = play_area_.x - 5; const float MIN_X = play_area_.x - 5;
const float max_x = play_area_.w + 5 - WIDTH_; const float MAX_X = play_area_.w + 5 - WIDTH_;
pos_x_ = std::clamp(pos_x_, min_x, max_x); pos_x_ = std::clamp(pos_x_, MIN_X, MAX_X);
shiftSprite(); shiftSprite();
break; break;
@@ -176,12 +181,12 @@ void Player::move()
case PlayerState::DYING: case PlayerState::DYING:
{ {
// Si el cadaver abandona el area de juego por los laterales lo hace rebotar // Si el cadaver abandona el area de juego por los laterales lo hace rebotar
const int x = player_sprite_->getPosX(); const int X = player_sprite_->getPosX();
const int min_x = play_area_.x; const int MIN_X = play_area_.x;
const int max_x = play_area_.x + play_area_.w - WIDTH_; const int MAX_X = play_area_.x + play_area_.w - WIDTH_;
if ((x < min_x) || (x > max_x)) if ((X < MIN_X) || (X > MAX_X))
{ {
player_sprite_->setPosX(std::clamp(x, min_x, max_x)); player_sprite_->setPosX(std::clamp(X, MIN_X, MAX_X));
player_sprite_->setVelX(-player_sprite_->getVelX()); player_sprite_->setVelX(-player_sprite_->getVelX());
playRandomBubbleSound(); playRandomBubbleSound();
} }
@@ -287,7 +292,6 @@ void Player::move()
{ {
pos_x_ = param.game.game_area.rect.w - WIDTH_; pos_x_ = param.game.game_area.rect.w - WIDTH_;
vel_x_ *= -1; vel_x_ *= -1;
// setInputPlaying(InputType::LEFT);
} }
} }
else else
@@ -297,7 +301,6 @@ void Player::move()
{ {
pos_x_ = param.game.game_area.rect.x; pos_x_ = param.game.game_area.rect.x;
vel_x_ *= -1; vel_x_ *= -1;
// setInputPlaying(InputType::RIGHT);
} }
} }
@@ -356,17 +359,20 @@ void Player::setAnimation()
// Establece la animación a partir de las cadenas // Establece la animación a partir de las cadenas
if (firing_state_ == PlayerState::FIRING_NONE) if (firing_state_ == PlayerState::FIRING_NONE)
{ // No esta disparando {
// No esta disparando
player_sprite_->setCurrentAnimation(a_walking); player_sprite_->setCurrentAnimation(a_walking);
player_sprite_->setFlip(flip_walk); player_sprite_->setFlip(flip_walk);
} }
else if (isCooling()) else if (isCooling())
{ // Acaba de disparar {
// Acaba de disparar
player_sprite_->setCurrentAnimation(a_walking + "-" + a_cooling + "-cooldown"); player_sprite_->setCurrentAnimation(a_walking + "-" + a_cooling + "-cooldown");
player_sprite_->setFlip(flip_cooling); player_sprite_->setFlip(flip_cooling);
} }
else else
{ // Está disparando {
// Está disparando
player_sprite_->setCurrentAnimation(a_walking + "-" + a_firing); player_sprite_->setCurrentAnimation(a_walking + "-" + a_firing);
// Si dispara de lado, invierte el sprite segun hacia donde dispara // Si dispara de lado, invierte el sprite segun hacia donde dispara
// Si dispara recto, invierte el sprite segun hacia donde camina // Si dispara recto, invierte el sprite segun hacia donde camina

View File

@@ -263,7 +263,7 @@ void Scoreboard::fillPanelTextures()
SDL_Rect rect = {enter_name_pos_.x, enter_name_pos_.y, 5, 7}; SDL_Rect rect = {enter_name_pos_.x, enter_name_pos_.y, 5, 7};
// Recorre todos los slots de letras del nombre // Recorre todos los slots de letras del nombre
for (size_t j = 0; j < NAME_LENGHT; ++j) for (size_t j = 0; j < MAX_NAME_LENGHT; ++j)
{ {
// Selecciona el color // Selecciona el color
const Color color = j < selector_pos_[i] ? orange_soft_color.lighten() : Color(0xFF, 0xFF, 0xEB); const Color color = j < selector_pos_[i] ? orange_soft_color.lighten() : Color(0xFF, 0xFF, 0xEB);
@@ -380,7 +380,7 @@ void Scoreboard::recalculateAnchors()
slot4_4_ = {col, row4}; slot4_4_ = {col, row4};
// Primer cuadrado para poner el nombre de record // Primer cuadrado para poner el nombre de record
const int enter_name_lenght = text_scoreboard_->lenght(std::string(NAME_LENGHT, 'A')); const int enter_name_lenght = text_scoreboard_->lenght(std::string(MAX_NAME_LENGHT, 'A'));
enter_name_pos_.x = col - (enter_name_lenght / 2); enter_name_pos_.x = col - (enter_name_lenght / 2);
enter_name_pos_.y = row4; enter_name_pos_.y = row4;

View File

@@ -34,7 +34,7 @@ public:
void update() override; void update() override;
// Dibuja el sprite // Dibuja el sprite
void render(); void render() override;
// Getters // Getters
int getDestX() const { return dest_x_; } int getDestX() const { return dest_x_; }