collision tile

This commit is contained in:
2026-04-06 21:51:40 +02:00
parent 98715ef3a7
commit 5393a861d1
11 changed files with 781 additions and 790 deletions

View File

@@ -2,26 +2,24 @@
#include <SDL3/SDL.h>
#include <array> // Para array
#include <limits> // Para numeric_limits
#include <memory> // Para shared_ptr, __shared_ptr_access
#include <memory> // Para shared_ptr
#include <string> // Para string
#include <utility>
#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite
#include "core/rendering/sprite/animated_sprite.hpp" // Para AnimatedSprite
#include "game/gameplay/room.hpp"
#include "game/options.hpp" // Para Cheat, Options, options
#include "utils/defines.hpp" // Para BORDER_TOP, BLOCK
#include "utils/utils.hpp" // Para Color
struct JA_Sound_t; // lines 13-13
#include "game/gameplay/tile_collider.hpp" // Para TileCollider::Tile
#include "game/options.hpp" // Para Cheat, Options
#include "utils/defines.hpp" // Para PlayArea, Tile, Flip
struct JA_Sound_t;
class Player {
public:
// --- Enums y Structs ---
enum class State {
ON_GROUND, // En suelo plano o conveyor belt
ON_SLOPE, // En rampa/pendiente
ON_AIR, // En el aire (saltando, cayendo o caminando al vacío)
ON_GROUND,
ON_SLOPE,
ON_AIR,
};
enum class Direction {
@@ -32,13 +30,13 @@ class Player {
NONE
};
// --- Constantes de física (públicas para permitir cálculos en structs) ---
static constexpr float HORIZONTAL_VELOCITY = 60.0F; // Velocidad horizontal objetivo en pixels/segundo
static constexpr float HORIZONTAL_ACCEL = 500.0F; // Aceleración/deceleración horizontal en pixels/segundo² (inercia ligera)
static constexpr float MAX_VY = 160.0F; // Velocidad vertical máxima en pixels/segundo
static constexpr float JUMP_VELOCITY = -178.5F; // Velocidad inicial del salto en pixels/segundo
static constexpr float GRAVITY_FORCE = 360.0F; // Fuerza de gravedad en pixels/segundo²
static constexpr float LOW_JUMP_GRAVITY_MULT = 3.0F; // Multiplicador de gravedad al soltar el botón de salto (salto variable)
// --- Constantes de física ---
static constexpr float HORIZONTAL_VELOCITY = 60.0F;
static constexpr float HORIZONTAL_ACCEL = 500.0F;
static constexpr float MAX_VY = 160.0F;
static constexpr float JUMP_VELOCITY = -178.5F;
static constexpr float GRAVITY_FORCE = 360.0F;
static constexpr float LOW_JUMP_GRAVITY_MULT = 3.0F;
struct SpawnData {
float x = 0;
@@ -60,130 +58,98 @@ class Player {
explicit Player(const Data& player);
~Player() = default;
// --- Funciones ---
void render(); // Pinta el enemigo en pantalla
void update(float delta_time); // Actualiza las variables del objeto
[[nodiscard]] auto isOnBorder() const -> bool { return border_ != Room::Border::NONE; } // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
[[nodiscard]] auto getBorder() const -> Room::Border { return border_; } // Indica en cual de los cuatro bordes se encuentra
void switchBorders(); // Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
auto getRect() -> SDL_FRect { return {.x = x_, .y = y_, .w = WIDTH, .h = HEIGHT}; } // Obtiene el rectangulo que delimita al jugador
auto getCollider() -> SDL_FRect& { return collider_box_; } // Obtiene el rectangulo de colision del jugador
auto getSpawnParams() -> SpawnData { return {.x = x_, .y = y_, .vx = vx_, .vy = vy_, .last_grounded_position = last_grounded_position_, .state = state_, .flip = sprite_->getFlip()}; } // Obtiene el estado de reaparición del jugador
void setColor(Uint8 color = 0); // Establece el color del jugador (0 = automático según cheats)
void setSkin(const std::string& skin_name); // Cambia la skin del jugador en caliente ("default" o nombre de enemigo)
static auto skinToAnimationPath(const std::string& skin_name) -> std::string; // Resuelve nombre de skin a fichero de animación
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); } // Establece la habitación en la que se encuentra el jugador
//[[nodiscard]] auto isAlive() const -> bool { return is_alive_ || (Options::cheats.invincible == Options::Cheat::State::ENABLED); } // Comprueba si el jugador esta vivo
[[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
void setIgnoreInput(bool value) { ignore_input_ = value; } // Ignora inputs del jugador (física sigue activa)
// --- Interfaz pública ---
void render();
void update(float delta_time);
[[nodiscard]] auto isOnBorder() const -> bool { return border_ != Room::Border::NONE; }
[[nodiscard]] auto getBorder() const -> Room::Border { return border_; }
void switchBorders();
auto getRect() -> SDL_FRect { return {.x = x_, .y = y_, .w = WIDTH, .h = HEIGHT}; }
auto getCollider() -> SDL_FRect& { return collider_box_; }
auto getSpawnParams() -> SpawnData { return {.x = x_, .y = y_, .vx = vx_, .vy = vy_, .last_grounded_position = last_grounded_position_, .state = state_, .flip = sprite_->getFlip()}; }
void setColor(Uint8 color = 0);
void setSkin(const std::string& skin_name);
static auto skinToAnimationPath(const std::string& skin_name) -> std::string;
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); }
[[nodiscard]] auto isAlive() const -> bool { return is_alive_; }
void setPaused(bool value) { is_paused_ = value; }
void setIgnoreInput(bool value) { ignore_input_ = value; }
[[nodiscard]] auto getIgnoreInput() const -> bool { return ignore_input_; }
#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)
void setDebugPosition(float x, float y);
void finalizeDebugTeleport();
#endif
private:
// --- Constantes ---
static constexpr int WIDTH = 12; // Ancho del jugador
static constexpr int HEIGHT = 24; // Alto del jugador
static constexpr int MAX_FALLING_HEIGHT = Tile::SIZE * 4; // Altura maxima permitida de caída en pixels
static constexpr int WIDTH = 12;
static constexpr int HEIGHT = 24;
// --- Objetos y punteros ---
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
std::unique_ptr<AnimatedSprite> sprite_; // Sprite del jugador
std::shared_ptr<Room> room_;
std::unique_ptr<AnimatedSprite> sprite_;
// --- Variables de posición y física ---
float x_ = 0.0F; // Posición del jugador en el eje X
float y_ = 0.0F; // Posición del jugador en el eje Y
float y_prev_ = 0.0F; // Posición Y del frame anterior (para detectar hitos de distancia en sonidos)
float vx_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje X
float vy_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje Y
// --- Posición y física ---
float x_ = 0.0F;
float y_ = 0.0F;
float vx_ = 0.0F;
float vy_ = 0.0F;
Direction wanna_go_ = Direction::NONE;
bool wanna_jump_ = false;
bool wanna_down_ = false;
bool jump_held_ = false; // true mientras la tecla de salto esté pulsada (para detectar flanco)
bool jump_held_ = false;
// --- Variables de estado ---
State state_ = State::ON_GROUND; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
State previous_state_ = State::ON_GROUND; // Estado previo en el que se encontraba el jugador
// --- Estado ---
State state_ = State::ON_GROUND;
State previous_state_ = State::ON_GROUND;
// --- Variables de colisión ---
SDL_FRect collider_box_{}; // Caja de colisión con los enemigos u objetos
std::array<SDL_FPoint, 12> collider_points_{}; // Puntos de colisión con el mapa (3 columnas × 4 filas, gap ≤ 8px)
SDL_FPoint under_left_foot_ = {.x = 0.0F, .y = 0.0F}; // El punto bajo la esquina inferior izquierda del jugador
SDL_FPoint under_right_foot_ = {.x = 0.0F, .y = 0.0F}; // El punto bajo la esquina inferior derecha del jugador
const LineDiagonal* current_slope_{nullptr}; // Rampa actual sobe la que está el jugador
// --- Colisión ---
SDL_FRect collider_box_{};
int slope_tile_x_{0};
int slope_tile_y_{0};
TileCollider::Tile slope_type_{TileCollider::Tile::EMPTY};
// --- Variables de juego ---
bool is_alive_ = true; // Indica si el jugador esta vivo o no
bool is_paused_ = false; // Indica si el jugador esta en modo pausa
bool ignore_input_ = false; // Ignora inputs pero mantiene la física activa
bool auto_movement_ = false; // Indica si esta siendo arrastrado por una superficie automatica
Room::Border border_ = Room::Border::TOP; // Indica en cual de los cuatro bordes se encuentra
int last_grounded_position_ = 0; // Ultima posición en Y en la que se estaba en contacto con el suelo (hace doble función: tracking de caída + altura inicial del salto)
bool is_alive_ = true;
bool is_paused_ = false;
bool ignore_input_ = false;
Room::Border border_ = Room::Border::TOP;
int last_grounded_position_ = 0;
// --- Variables de renderizado y sonido ---
Uint8 color_ = 0; // Color del jugador
JA_Sound_t* jump_sound_ = nullptr; // Sonido al iniciar el salto
JA_Sound_t* land_sound_ = nullptr; // Sonido al aterrizar en el suelo
// --- Renderizado y sonido ---
Uint8 color_ = 0;
JA_Sound_t* jump_sound_ = nullptr;
JA_Sound_t* land_sound_ = nullptr;
void handleConveyorBelts();
void handleShouldFall();
void updateState(float delta_time);
// --- Pipeline de update ---
void handleInput();
void updateVelocity(float delta_time);
void applyGravity(float delta_time);
void handleJumpAndDrop();
void moveHorizontal(float delta_time);
void moveVertical(float delta_time);
void followSlope();
void exitSlope();
void detectSlopeEntry();
void checkFalling();
// --- Métodos de actualización por estado ---
void updateOnGround(float delta_time); // Actualización lógica estado ON_GROUND
void updateOnSlope(float delta_time); // Actualización lógica estado ON_SLOPE
void updateOnAir(float delta_time); // Actualización lógica estado ON_AIR
// --- Gestión de estado ---
void transitionToState(State state);
void startJump();
// --- Métodos de movimiento por estado ---
void moveOnGround(float delta_time); // Movimiento físico estado ON_GROUND
void moveOnSlope(float delta_time); // Movimiento físico estado ON_SLOPE
void moveOnAir(float delta_time); // Movimiento físico estado ON_AIR
// --- Geometría y renderizado ---
void syncSpriteAndCollider();
void placeSprite();
void animate(float delta_time);
auto handleBorders() -> Room::Border;
// --- Funciones de inicialización ---
void initSprite(const std::string& animations_path); // Inicializa el sprite del jugador
void initSounds(); // Inicializa los sonidos de salto y caida
void applySpawnValues(const SpawnData& spawn); // Aplica los valores de spawn al jugador
// --- Inicialización ---
void initSprite(const std::string& animations_path);
void initSounds();
void applySpawnValues(const SpawnData& spawn);
// --- Funciones de procesamiento de entrada ---
void handleInput(); // Comprueba las entradas y modifica variables
// --- Funciones de gestión de estado ---
void transitionToState(State state); // Cambia el estado del jugador
void startJump(); // Inicia el salto: velocidad inicial + sonido + transición a ON_AIR
// --- Funciones de física ---
void applyGravity(float delta_time); // Aplica gravedad al jugador
// --- Funciones de movimiento y colisión ---
void move(float delta_time); // Orquesta el movimiento del jugador
auto getProjection(Direction direction, float displacement) -> SDL_FRect; // Devuelve el rectangulo de proyeccion
void applyHorizontalMovement(float delta_time); // Aplica movimiento horizontal con colisión de muros
auto handleLandingFromAir(float displacement, const SDL_FRect& projection) -> bool; // Detecta aterrizaje en superficies y rampas
// --- Funciones de detección de superficies ---
auto isOnFloor() -> bool; // Comprueba si el jugador tiene suelo debajo de los pies
auto isOnTopSurface() -> bool; // Comprueba si el jugador está sobre una superficie
auto isOnConveyorBelt() -> bool; // Comprueba si el jugador esta sobre una cinta transportadora
auto isOnSlope() -> bool; // Comprueba si el jugador está sobre una rampa
auto isLeftSlope() -> bool; // Comprueba si current_slope_ es una rampa izquierda (ascendente a la izquierda)
void updateCurrentSlope(); // Actualiza current_slope_ con la rampa correcta y muestra debug info
// --- Funciones de actualización de geometría ---
void syncSpriteAndCollider(); // Actualiza collider_box y collision points
void updateColliderPoints(); // Actualiza los puntos de colisión
void updateFeet(); // Actualiza los puntos de los pies
void placeSprite(); // Coloca el sprite en la posición del jugador
// --- Funciones de finalización ---
void animate(float delta_time); // Establece la animación del jugador
auto handleBorders() -> Room::Border; // Comprueba si se halla en alguno de los cuatro bordes
auto handleKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
void updateVelocity(float delta_time); // Calcula la velocidad en x con inercia ligera
void markAsDead(); // Marca al jugador como muerto
};
// --- Utilidad ---
void markAsDead();
};