diff --git a/source/core/rendering/screen.hpp b/source/core/rendering/screen.hpp index 625c32c..2a22b2d 100644 --- a/source/core/rendering/screen.hpp +++ b/source/core/rendering/screen.hpp @@ -67,6 +67,7 @@ class Screen { auto getRendererSurface() -> std::shared_ptr; auto getBorderSurface() -> std::shared_ptr; [[nodiscard]] auto getText() const -> std::shared_ptr { return text_; } + [[nodiscard]] auto getGameSurfaceDstRect() const -> SDL_FRect { return game_surface_dstrect_; } private: // Estructuras diff --git a/source/game/entities/player.cpp b/source/game/entities/player.cpp index ca7d68d..cc43f52 100644 --- a/source/game/entities/player.cpp +++ b/source/game/entities/player.cpp @@ -887,4 +887,22 @@ auto Player::getProjection(Direction direction, float displacement) -> SDL_FRect .w = 0.0F, .h = 0.0F}; } -} \ No newline at end of file +} + +#ifdef _DEBUG +// Establece la posición del jugador directamente (debug) +void Player::setDebugPosition(float x, float y) { + x_ = x; + y_ = y; + syncSpriteAndCollider(); +} + +// Fija estado ON_GROUND, velocidades a 0, actualiza last_grounded_position_ (debug) +void Player::finalizeDebugTeleport() { + vx_ = 0.0F; + vy_ = 0.0F; + state_ = State::ON_GROUND; + last_grounded_position_ = static_cast(y_); + syncSpriteAndCollider(); +} +#endif \ No newline at end of file diff --git a/source/game/entities/player.hpp b/source/game/entities/player.hpp index 7c9db3a..1002cf8 100644 --- a/source/game/entities/player.hpp +++ b/source/game/entities/player.hpp @@ -103,6 +103,12 @@ class Player { [[nodiscard]] auto isAlive() const -> bool { return is_alive_; } // Comprueba si el jugador esta vivo void setPaused(bool value) { is_paused_ = value; } // Pone el jugador en modo pausa +#ifdef _DEBUG + // --- Funciones de debug --- + void setDebugPosition(float x, float y); // Establece la posición del jugador directamente (debug) + void finalizeDebugTeleport(); // Fija estado ON_GROUND, velocidades a 0, actualiza last_grounded_position_ (debug) +#endif + private: // --- Constantes --- static constexpr int WIDTH = 8; // Ancho del jugador diff --git a/source/game/scenes/game.cpp b/source/game/scenes/game.cpp index 74d7e33..db35a75 100644 --- a/source/game/scenes/game.cpp +++ b/source/game/scenes/game.cpp @@ -2,6 +2,7 @@ #include +#include // Para std::sqrt, std::min #include #include // Para vector @@ -166,7 +167,17 @@ void Game::updatePlaying(float delta_time) { room_->update(delta_time); switch (mode_) { case Mode::GAME: +#ifdef _DEBUG + // Maneja el arrastre del jugador con el ratón (debug) + handleDebugMouseDrag(delta_time); + + // Si estamos arrastrando, no ejecutar la física normal del jugador + if (!debug_dragging_player_) { + player_->update(delta_time); + } +#else player_->update(delta_time); +#endif checkPlayerIsOnBorder(); checkPlayerAndItems(); checkPlayerAndEnemies(); @@ -439,6 +450,60 @@ void Game::handleDebugEvents(const SDL_Event& event) { } } } + +// Maneja el arrastre del jugador con el ratón (debug) +void Game::handleDebugMouseDrag(float delta_time) { + // Solo funciona si Debug está habilitado + if (!Debug::get()->isEnabled()) { + return; + } + + // Obtener estado del ratón + float mouse_x = 0.0F; + float mouse_y = 0.0F; + SDL_MouseButtonFlags buttons = SDL_GetMouseState(&mouse_x, &mouse_y); + + // Convertir coordenadas de ventana a coordenadas de juego + SDL_FRect dst_rect = Screen::get()->getGameSurfaceDstRect(); + + // Calcular posición en coordenadas de juego (256x192) + float game_x = (mouse_x - dst_rect.x) * (static_cast(PlayArea::WIDTH) / dst_rect.w); + float game_y = (mouse_y - dst_rect.y) * (static_cast(PlayArea::HEIGHT) / dst_rect.h); + + // Verificar si el botón izquierdo está presionado + bool left_button_pressed = (buttons & SDL_BUTTON_LMASK) != 0; + + if (left_button_pressed) { + // Obtener posición actual del jugador + SDL_FRect player_rect = player_->getRect(); + float player_x = player_rect.x; + float player_y = player_rect.y; + + // Calcular distancia al objetivo + float dx = game_x - player_x; + float dy = game_y - player_y; + float distance = std::sqrt(dx * dx + dy * dy); + + // Velocidad de movimiento hacia el cursor (pixels/segundo) + constexpr float DRAG_SPEED = 300.0F; + + if (distance > 1.0F) { + // Calcular el movimiento con aceleración (interpolación suave) + float move_factor = std::min(1.0F, DRAG_SPEED * delta_time / distance); + float new_x = player_x + dx * move_factor; + float new_y = player_y + dy * move_factor; + + // Mover el jugador hacia la posición del cursor + player_->setDebugPosition(new_x, new_y); + } + + debug_dragging_player_ = true; + } else if (debug_dragging_player_) { + // Botón soltado después de arrastrar: finalizar teleport + player_->finalizeDebugTeleport(); + debug_dragging_player_ = false; + } +} #endif // Escribe el nombre de la pantalla diff --git a/source/game/scenes/game.hpp b/source/game/scenes/game.hpp index b2adeea..b6f5683 100644 --- a/source/game/scenes/game.hpp +++ b/source/game/scenes/game.hpp @@ -95,6 +95,7 @@ class Game { void updateDebugInfo(); // Pone la información de debug en pantalla static void renderDebugInfo(); // Pone la información de debug en pantalla void handleDebugEvents(const SDL_Event& event); // Comprueba los eventos + void handleDebugMouseDrag(float delta_time); // Maneja el arrastre del jugador con el ratón (debug) #endif // --- Variables miembro --- @@ -125,4 +126,9 @@ class Game { // Variables de efectos visuales SDL_FRect room_name_rect_; // Rectangulo donde pintar la textura con el nombre de la habitación float jail_restore_time_{0.0F}; // Tiempo acumulado para restauración de vidas en la Jail + +#ifdef _DEBUG + // Variables de debug para arrastre con ratón + bool debug_dragging_player_{false}; // Indica si estamos arrastrando al jugador con el ratón +#endif }; \ No newline at end of file