desacoplament de Player i Options

Player: canviat id de int a enum
migrant input: eliminat Device, keyboard separat de la llista de mandos, llig i guarda configuracions de mandos
falta: definir botons, asignar mandos a jugadors i guardar la asignació
This commit is contained in:
2025-08-03 22:49:28 +02:00
parent de9fb5aa4b
commit 90c080f3e3
19 changed files with 433 additions and 353 deletions

View File

@@ -10,7 +10,6 @@
#include "enter_name.h" // Para EnterName
#include "input.h"
#include "manage_hiscore_table.h" // Para HiScoreEntry
#include "options.h" // Para SettingsOptions, settings
#include "scoreboard.h" // Para Scoreboard
#include "utils.h" // Para Circle
@@ -25,6 +24,14 @@ class Player {
static constexpr int WIDTH = 32; // Anchura
static constexpr int HEIGHT = 32; // Altura
// --- Id para los jugadores ---
enum class Id : int {
NO_PLAYER = -1,
BOTH_PLAYERS = 0,
PLAYER1 = 1,
PLAYER2 = 2
};
// --- Estados posibles del jugador ---
enum class State {
// Estados de movimiento
@@ -68,8 +75,20 @@ class Player {
RESPAWNING, // Tras continuar y volver al juego
};
struct Config {
Id id;
float x;
int y;
bool demo;
SDL_FRect *play_area; // Usamos puntero para mantener la referencia
std::vector<std::shared_ptr<Texture>> texture;
std::vector<std::vector<std::string>> animations;
Table *hi_score_table; // También como puntero para referencia
int *glowing_entry; // Puntero para mantener la referencia
};
// --- Constructor y destructor ---
Player(int id, float x, int y, bool demo, SDL_FRect &play_area, std::vector<std::shared_ptr<Texture>> texture, const std::vector<std::vector<std::string>> &animations);
Player(const Config& config);
~Player() = default;
// --- Inicialización y ciclo de vida ---
@@ -93,9 +112,9 @@ class Player {
void updateCooldown(); // Actualiza el cooldown de disparo
// --- Puntuación y marcador ---
void addScore(int score); // Añade puntos
void incScoreMultiplier(); // Incrementa el multiplicador
void decScoreMultiplier(); // Decrementa el multiplicador
void addScore(int score, int last_hi_score_entry); // Añade puntos
void incScoreMultiplier(); // Incrementa el multiplicador
void decScoreMultiplier(); // Decrementa el multiplicador
// --- Estados de juego ---
void setPlayingState(State state); // Cambia el estado de juego
@@ -128,7 +147,7 @@ class Player {
[[nodiscard]] auto hasExtraHit() const -> bool { return extra_hit_; }
[[nodiscard]] auto isCooling() const -> bool { return firing_state_ == State::COOLING_LEFT || firing_state_ == State::COOLING_UP || firing_state_ == State::COOLING_RIGHT; }
[[nodiscard]] auto isRecoiling() const -> bool { return firing_state_ == State::RECOILING_LEFT || firing_state_ == State::RECOILING_UP || firing_state_ == State::RECOILING_RIGHT; }
[[nodiscard]] auto isEligibleForHighScore() const -> bool { return score_ > Options::settings.hi_score_table.back().score; }
[[nodiscard]] auto qualifiesForHighScore() const -> bool { return qualifies_for_high_score_; }
[[nodiscard]] auto isInvulnerable() const -> bool { return invulnerable_; }
[[nodiscard]] auto isPowerUp() const -> bool { return power_up_; }
auto getCollider() -> Circle & { return collider_; }
@@ -137,7 +156,7 @@ class Player {
[[nodiscard]] auto getContinueCounter() const -> int { return continue_counter_; }
[[nodiscard]] auto getController() const -> int { return controller_index_; }
[[nodiscard]] static auto getHeight() -> int { return HEIGHT; }
[[nodiscard]] auto getId() const -> int { return id_; }
[[nodiscard]] auto getId() const -> Player::Id { return id_; }
[[nodiscard]] auto getInvulnerableCounter() const -> int { return invulnerable_counter_; }
[[nodiscard]] auto getPosX() const -> int { return static_cast<int>(pos_x_); }
[[nodiscard]] auto getPosY() const -> int { return pos_y_; }
@@ -165,11 +184,13 @@ class Player {
void setWalkingState(State state) { walking_state_ = state; }
void addCredit() { ++credits_used_; }
// Setter y getter para gamepad_
void setGamepad(std::shared_ptr<Input::Gamepad> gamepad) { gamepad_ = gamepad; }
[[nodiscard]] std::shared_ptr<Input::Gamepad> getGamepad() const { return gamepad_; }
void setUsesKeyboard(bool value) { uses_keyboard_ = value; }
[[nodiscard]] bool getUsesKeyboard() const { return uses_keyboard_; }
void setHiScoreTable(const Table &table) { hi_score_table_ = table; }
const Table &getHiScoreTable() const { return hi_score_table_; }
void setGlowingEntry(const int &entry) { glowing_entry_ = entry; }
private:
// --- Constantes ---
@@ -186,7 +207,7 @@ class Player {
std::unique_ptr<EnterName> enter_name_; // Clase utilizada para introducir el nombre
// --- Variables de estado ---
int id_; // Número de identificación para el jugador. Player1 = 1, Player2 = 2
Id id_; // Identificador para el jugador
SDL_FRect play_area_; // Rectángulo con la zona de juego
float pos_x_ = 0.0F; // Posición en el eje X
int pos_y_ = 0; // Posición en el eje Y
@@ -200,6 +221,9 @@ class Player {
int cooling_state_counter_ = 0; // Contador para la animación del estado cooling
int score_ = 0; // Puntos del jugador
float score_multiplier_ = 1.0F; // Multiplicador de puntos
bool qualifies_for_high_score_ = false; // Indica si tiene una puntuación que le permite entrar en la tabla de records
Table &hi_score_table_; // Tabla de maximas puntuaciones
int &glowing_entry_; // Entrada de la tabla de puntuaciones para hacerla brillar
State walking_state_ = State::WALKING_STOP; // Estado del jugador al moverse
State firing_state_ = State::FIRING_NONE; // Estado del jugador al disparar
State playing_state_ = State::WAITING; // Estado del jugador en el juego