7ee359b910
Sweep final del naming a CamelCase/camelBack/lower_case:
Fitxers renombrats:
- effects/gestor_puntuacio_flotant.{hpp,cpp} -> floating_score_manager.{hpp,cpp}
- effects/puntuacio_flotant.hpp -> floating_score.hpp
Tipus (CamelCase):
- GestorPuntuacioFlotant -> FloatingScoreManager
- PuntuacioFlotant -> FloatingScore
- ConfigStage -> StageConfig
- ConfigSistemaStages -> StageSystemConfig
- NauTitol -> TitleShip
- EstatNau -> ShipState
Metodes publics (camelBack):
- obte_renderer -> getRenderer
- get_num_actius -> getActiveCount
- calcular_direccio_explosio -> computeExplosionDirection
- trobar_slot_lliure -> findFreeSlot
- explotar -> explode
- reiniciar -> reset
- es_valida -> isValid
- parsejar_fitxer -> parseFile
- carregar -> load
- crear_explosio -> createExplosion
- registrar_puntuacio -> registerScore
- construir_marcador -> buildScoreboard
- render_centered -> renderCentered
Camps struct publics (snake_case):
- actiu/actius -> active
- rotacio -> rotation, rotacio_visual -> visual_rotation
- acceleracio -> acceleration
- velocitat -> velocity
- escala/escala_inicial/objectiu/actual -> scale/initial_scale/...
- posicio/posicio_inicial/objectiu/actual -> position/initial_position/...
- fase_oscilacio -> oscillation_phase
- temps_estat -> state_time
- jugador_id -> player_id
- estat -> state
- brillantor -> brightness
- tipus -> type
Camps privats (sufix _):
- naus_ -> ships_, orni_ -> enemies_, bales_ -> bullets_
- gestor_puntuacio_ -> floating_score_manager_
- punt_mort_ -> death_position_, punt_spawn_ -> spawn_position_
- itocado_per_jugador_ -> hit_timer_per_player_
- vides_per_jugador_ -> lives_per_player_
- puntuacio_per_jugador_ -> score_per_player_
- estat_game_over_ -> game_over_state_
- continues_usados_ -> continues_used_
Constants:
- MARGE_ESQ/DRET/DALT/BAIX -> MARGIN_LEFT/RIGHT/TOP/BOTTOM
Variables locals i parametres comuns (snake_case):
- nau -> ship, enemic -> enemy, bala -> bullet
- forma -> shape, punt(s) -> point(s)
- jugador -> player, partida -> match
- temps -> time, missatge -> message
Diff: 59 fitxers, +1000/-1000 (simetric). Compila i enllaça.
Pendents per a futures fases (no bloquejants):
- Comentaris de capçalera en catala -> castella
- Variables locals/parametres minoritaris en catala
- Include guards (queden alguns #ifndef en lloc de #pragma once)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
162 lines
8.4 KiB
C++
162 lines
8.4 KiB
C++
#pragma once
|
|
|
|
#include <SDL3/SDL.h> // Para SDL_Scancode, SDL_GamepadButton, SDL_JoystickID, SDL_CloseGamepad, SDL_Gamepad, SDL_GetGamepadJoystick, SDL_GetGamepadName, SDL_GetGamepadPath, SDL_GetJoystickID, Sint16, Uint8, SDL_Event
|
|
|
|
#include <array> // Para array
|
|
#include <memory> // Para shared_ptr
|
|
#include <span> // Para span
|
|
#include <string> // Para string, basic_string
|
|
#include <unordered_map> // Para unordered_map
|
|
#include <utility> // Para pair
|
|
#include <vector> // Para vector
|
|
|
|
#include "core/input/input_types.hpp" // for InputAction
|
|
|
|
// --- Clase Input: gestiona la entrada de teclado y mandos (singleton) ---
|
|
class Input {
|
|
public:
|
|
// --- Constantes ---
|
|
static constexpr bool ALLOW_REPEAT = true; // Permite repetición
|
|
static constexpr bool DO_NOT_ALLOW_REPEAT = false; // No permite repetición
|
|
static constexpr bool CHECK_KEYBOARD = true; // Comprueba teclado
|
|
static constexpr bool DO_NOT_CHECK_KEYBOARD = false; // No comprueba teclado
|
|
static constexpr int TRIGGER_L2_AS_BUTTON = 100; // L2 como botón
|
|
static constexpr int TRIGGER_R2_AS_BUTTON = 101; // R2 como botón
|
|
|
|
// --- Tipos ---
|
|
using Action = InputAction; // Alias para mantener compatibilidad
|
|
|
|
// --- Estructuras ---
|
|
struct KeyState {
|
|
Uint8 scancode{0}; // Scancode asociado
|
|
bool is_held{false}; // Está pulsada ahora mismo
|
|
bool just_pressed{false}; // Se acaba de pulsar en este fotograma
|
|
};
|
|
|
|
struct ButtonState {
|
|
int button{static_cast<int>(SDL_GAMEPAD_BUTTON_INVALID)}; // GameControllerButton asociado
|
|
bool is_held{false}; // Está pulsada ahora mismo
|
|
bool just_pressed{false}; // Se acaba de pulsar en este fotograma
|
|
bool axis_active{false}; // Estado del eje
|
|
bool trigger_active{false}; // Estado del trigger como botón digital
|
|
};
|
|
|
|
struct Keyboard {
|
|
std::unordered_map<Action, KeyState> bindings; // Mapa de acciones a estados de tecla
|
|
};
|
|
|
|
struct Gamepad {
|
|
SDL_Gamepad* pad{nullptr}; // Puntero al gamepad SDL
|
|
SDL_JoystickID instance_id{0}; // ID de instancia del joystick
|
|
std::string name; // Nombre del gamepad
|
|
std::string path; // Ruta del dispositivo
|
|
std::unordered_map<Action, ButtonState> bindings; // Mapa de acciones a estados de botón
|
|
|
|
explicit Gamepad(SDL_Gamepad* gamepad)
|
|
: pad(gamepad),
|
|
instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(gamepad))),
|
|
name(std::string(SDL_GetGamepadName(gamepad))),
|
|
path(std::string(SDL_GetGamepadPath(pad))),
|
|
bindings{
|
|
// Movimiento y acciones del player
|
|
{Action::LEFT, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_DPAD_LEFT)}},
|
|
{Action::RIGHT, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_DPAD_RIGHT)}},
|
|
{Action::THRUST, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_WEST)}},
|
|
{Action::SHOOT, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_SOUTH)}}} {}
|
|
|
|
~Gamepad() {
|
|
if (pad != nullptr) {
|
|
SDL_CloseGamepad(pad);
|
|
}
|
|
}
|
|
|
|
// Reasigna un botón a una acción
|
|
void rebindAction(Action action, SDL_GamepadButton new_button) {
|
|
bindings[action].button = static_cast<int>(new_button);
|
|
}
|
|
};
|
|
|
|
// --- Tipos ---
|
|
using Gamepads = std::vector<std::shared_ptr<Gamepad>>; // Vector de gamepads
|
|
|
|
// --- Singleton ---
|
|
static void init(const std::string& game_controller_db_path);
|
|
static void destroy();
|
|
static auto get() -> Input*;
|
|
|
|
// --- Actualización del sistema ---
|
|
void update(); // Actualiza estados de entrada
|
|
|
|
// --- Configuración de controles ---
|
|
void bindKey(Action action, SDL_Scancode code);
|
|
void applyKeyboardBindingsFromOptions();
|
|
void applyGamepadBindingsFromOptions();
|
|
|
|
// Configuración por player (Orni - dos jugadores)
|
|
void applyPlayer1BindingsFromOptions();
|
|
void applyPlayer2BindingsFromOptions();
|
|
|
|
static void bindGameControllerButton(const std::shared_ptr<Gamepad>& gamepad, Action action, SDL_GamepadButton button);
|
|
static void bindGameControllerButton(const std::shared_ptr<Gamepad>& gamepad, Action action_target, Action action_source);
|
|
|
|
// --- Consulta de entrada ---
|
|
auto checkAction(Action action, bool repeat = true, bool check_keyboard = true, const std::shared_ptr<Gamepad>& gamepad = nullptr) -> bool;
|
|
auto checkAnyInput(bool check_keyboard = true, const std::shared_ptr<Gamepad>& gamepad = nullptr) -> bool;
|
|
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> bool;
|
|
void resetInputStates();
|
|
|
|
// Consulta por player (Orni - dos jugadores)
|
|
auto checkActionPlayer1(Action action, bool repeat = true) -> bool;
|
|
auto checkActionPlayer2(Action action, bool repeat = true) -> bool;
|
|
|
|
// Check if any player pressed any action from a list
|
|
auto checkAnyPlayerAction(const std::span<const InputAction>& actions, bool repeat = DO_NOT_ALLOW_REPEAT) -> bool;
|
|
|
|
// --- Gestión de gamepads ---
|
|
[[nodiscard]] auto gameControllerFound() const -> bool;
|
|
[[nodiscard]] auto getNumGamepads() const -> int;
|
|
[[nodiscard]] auto getGamepad(SDL_JoystickID id) const -> std::shared_ptr<Gamepad>;
|
|
[[nodiscard]] auto getGamepadByName(const std::string& name) const -> std::shared_ptr<Input::Gamepad>;
|
|
[[nodiscard]] auto getGamepads() const -> const Gamepads& { return gamepads_; }
|
|
auto findAvailableGamepadByName(const std::string& gamepad_name) -> std::shared_ptr<Gamepad>;
|
|
static auto getControllerName(const std::shared_ptr<Gamepad>& gamepad) -> std::string;
|
|
[[nodiscard]] auto getControllerNames() const -> std::vector<std::string>;
|
|
[[nodiscard]] static auto getControllerBinding(const std::shared_ptr<Gamepad>& gamepad, Action action) -> SDL_GamepadButton;
|
|
void printConnectedGamepads() const;
|
|
|
|
// --- Eventos ---
|
|
auto handleEvent(const SDL_Event& event) -> std::string;
|
|
|
|
private:
|
|
// --- Constantes ---
|
|
static constexpr Sint16 AXIS_THRESHOLD = 30000; // Umbral para ejes analógicos
|
|
static constexpr Sint16 TRIGGER_THRESHOLD = 16384; // Umbral para triggers (50% del rango)
|
|
static constexpr std::array<Action, 1> BUTTON_INPUTS = {Action::SHOOT}; // Inputs que usan botones
|
|
|
|
// --- Métodos ---
|
|
explicit Input(std::string game_controller_db_path);
|
|
~Input() = default;
|
|
|
|
void initSDLGamePad();
|
|
static auto checkAxisInput(Action action, const std::shared_ptr<Gamepad>& gamepad, bool repeat) -> bool;
|
|
static auto checkTriggerInput(Action action, const std::shared_ptr<Gamepad>& gamepad, bool repeat) -> bool;
|
|
auto addGamepad(int device_index) -> std::string;
|
|
auto removeGamepad(SDL_JoystickID id) -> std::string;
|
|
void addGamepadMappingsFromFile();
|
|
void discoverGamepads();
|
|
|
|
// --- Variables miembro ---
|
|
static Input* instance; // Instancia única del singleton
|
|
|
|
Gamepads gamepads_; // Lista de gamepads conectados
|
|
Keyboard keyboard_{}; // Estado del teclado (solo acciones globales)
|
|
std::string gamepad_mappings_file_; // Ruta al archivo de mappings
|
|
|
|
// Referencias cacheadas a gamepads por player (Orni)
|
|
std::shared_ptr<Gamepad> player1_gamepad_;
|
|
std::shared_ptr<Gamepad> player2_gamepad_;
|
|
|
|
// Mapas de bindings separados por player (Orni - dos jugadores)
|
|
std::unordered_map<Action, KeyState> player1_keyboard_bindings_;
|
|
std::unordered_map<Action, KeyState> player2_keyboard_bindings_;
|
|
}; |