diff --git a/CLAUDE.md b/CLAUDE.md index ce9a41b..3b519bc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -11,9 +11,9 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## Cambios de diseño previstos respecto al juego original 1. **Habitaciones más grandes** — aprovechar el espacio del marcador (scoreboard) para ampliar el área jugable. Esto implica revisar la resolución de canvas, el layout de la HUD y el tamaño de los tilemaps. -2. **Nueva física del jugador** — menos arcaica, más moderna/fluida. Reemplazar la lógica actual de `Player::update()`. -3. **Motor de colisiones por tiles** — eliminar el sistema actual basado en listas de superficies (`bottom_floors_`, `top_floors_`, `left_walls_`, `right_walls_`, `left_slopes_`, `right_slopes_`, `conveyor_belt_floors_` en `Room`). Sustituir por detección de colisiones tile-based directa sobre el mapa. -4. **Transición de pantalla animada** — al cambiar de habitación, en lugar de warpear al jugador del borde opuesto, mostrar una animación/transición de cambio de pantalla. +2. ~~**Nueva física del jugador**~~ ✅ **HECHO** — Player reescrito con pipeline de 6 fases, colisiones tile-based, ~20 métodos eliminados. +3. ~~**Motor de colisiones por tiles**~~ ✅ **HECHO** — `TileCollider` con queries directas al grid. Collision tilemap editado desde el editor (tecla 7). Sistema antiguo de superficies preservado pero no usado. Pendiente: tiles kill (5) y conveyor (6). +4. ~~**Transición de pantalla animada**~~ ✅ **HECHO** — Scroll con easing `cubicInOut` (0.5s), ambas rooms visibles, enemigos activos, jugador puede moverse durante la transición. 5. **Paleta Amstrad CPC** — subir de la paleta actual (8-bit indexada limitada) a la paleta del Amstrad CPC (27 colores / más colores que la actual). Afecta a `PaletteManager` y a todos los assets de color. ## Estado del renombrado @@ -49,6 +49,43 @@ Se han renombrado las referencias de `JailDoctor's Dilemma` → `Projecte 2026` ### Otros - Añadidos `desktop.ini` y `Thumbs.db` al `.gitignore`. +### Transiciones de pantalla (sesión abril 2026) +- **Cambio de pantalla por punto central:** `Player::handleBorders()` usa el centro del rectángulo del jugador para detectar cambio de pantalla (antes usaba bordes). +- **Conservación de momento:** `Player::switchBorders()` conserva velocidad y estado al cambiar de pantalla (antes forzaba ON_GROUND y reseteaba vy_). +- **Transición animada con scroll:** Al cambiar de habitación, ambas rooms se desplazan con easing `cubicInOut` durante 0.5s. + - Render offset global añadido a `Screen` (`setRenderOffset`) aplicado en los 6 métodos de `Surface::render()`. + - Enemigos de ambas habitaciones se actualizan durante la transición. + - El jugador puede moverse durante la transición. + - Estado en `Game`: `transitioning_`, `transition_timer_`, `transition_old_room_`, `transition_direction_`. + - Ficheros: `screen.hpp/cpp`, `surface.cpp`, `game.hpp/cpp`. + +### Impulso de salto +- `JUMP_VELOCITY` incrementado 5%: de -170.0 a -178.5. + +### Collision tilemap (sesión abril 2026) +- **Formato YAML nuevo:** `tilemap:` tiene dos sub-mapas: `draw:` (tilemap de dibujo, el original) y `collision:` (mapa de colisiones). `RoomLoader::parseTilemap()` lee ambos con fallback al formato antiguo. +- **`Room::Data::collision_tile_map`** — vector con tipos: 0=vacío, 1=muro, 2=plataforma, 3=slope_l, 4=slope_r, 5=kill, 6=conveyor. +- **CollisionMap migrado:** `getTile()` lee directamente del `collision_tile_map` (antes deducía el tipo por rangos de índice del tileset de dibujo). Constructor ya no necesita `tile_set_width`. +- **Editor de colisiones:** Tecla 7 o `EDIT DRAW`/`EDIT COLLISION` alterna entre editar el tilemap de dibujo y el de colisiones. En modo collision se superpone el `collision.gif` (7 tiles) sobre el mapa de dibujo. Right-click abre el tile picker del tileset correspondiente. `RoomSaver` guarda ambos sub-mapas. +- **`collision.gif`** registrado en `assets.yaml` (7 tiles: vacío, muro, plataforma, slope_l, slope_r, kill, conveyor). + +### Nuevo motor de colisiones tile-based (sesión abril 2026) +- **Clase `TileCollider`** (`source/game/gameplay/tile_collider.hpp/cpp`): queries directas contra el grid de tiles sin listas de superficies intermedias. API: `checkWallLeft/Right`, `checkCeiling`, `checkFloor` (con FloorHit struct), `hasGroundBelow`, `checkSlopeBelow` (con SlopeInfo struct), `getSlopeY`. +- Integrado en `CollisionMap` (miembro + getter) y expuesto via `Room::getTileCollider()`. +- **Player reescrito** con pipeline de 6 fases claras: + 1. `handleInput()` — leer input + 2. `updateVelocity()` + `applyGravity()` — calcular velocidades + 3. `handleJumpAndDrop()` — salto + drop-through (plataformas y slopes con DOWN) + 4. `moveHorizontal()` + `moveVertical()` — movimiento con colisión tile-based + 5. `checkFalling()` — detectar caída + 6. `syncSpriteAndCollider()` + `animate()` + `handleBorders()` +- **~20 métodos eliminados** del Player antiguo. Slopes gestionadas por tile (slope_tile_x/y/type) en vez de puntero a LineDiagonal. +- **Sistema antiguo de superficies preservado** en CollisionMap (no eliminado), simplemente ya no usado por Player. +- **Pendiente:** tiles 5 (kill) y 6 (conveyor) no soportados aún en el nuevo motor. + +### Otros +- Añadidos `desktop.ini` y `Thumbs.db` al `.gitignore`. + --- ## Overview (legacy) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67fbe56..d1144b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ set(APP_SOURCES # Game - Gameplay source/game/gameplay/cheevos.cpp source/game/gameplay/collision_map.cpp + source/game/gameplay/tile_collider.cpp source/game/gameplay/enemy_manager.cpp source/game/gameplay/item_manager.cpp source/game/gameplay/item_tracker.cpp diff --git a/data/tilesets/collision.gif b/data/tilesets/collision.gif index a2fa7be..b12a903 100644 Binary files a/data/tilesets/collision.gif and b/data/tilesets/collision.gif differ diff --git a/source/game/entities/player.cpp b/source/game/entities/player.cpp index 4ca431e..6376a77 100644 --- a/source/game/entities/player.cpp +++ b/source/game/entities/player.cpp @@ -3,22 +3,24 @@ #include // Para max, min #include // Para ceil, abs -#include -#include // Para std::ranges::any_of #include "core/audio/audio.hpp" // Para Audio #include "core/input/input.hpp" // Para Input, InputAction -#include "core/rendering/sprite/animated_sprite.hpp" // Para SAnimatedSprite +#include "core/rendering/sprite/animated_sprite.hpp" // Para AnimatedSprite #include "core/resources/resource_cache.hpp" // Para Resource -#include "game/gameplay/room.hpp" // Para Room, TileType -#include "game/options.hpp" // Para Cheat, Options, options -#include "utils/defines.hpp" // Para RoomBorder::BOTTOM, RoomBorder::LEFT, RoomBorder::RIGHT +#include "game/gameplay/room.hpp" // Para Room +#include "game/gameplay/tile_collider.hpp" // Para TileCollider +#include "game/options.hpp" // Para Cheat, Options +#include "utils/defines.hpp" // Para PlayArea, Collision #ifdef _DEBUG #include "core/system/debug.hpp" // Para Debug #endif +// ============================================================================ // Constructor +// ============================================================================ + Player::Player(const Data& player) : room_(player.room) { initSprite(player.animations_path); @@ -26,64 +28,48 @@ Player::Player(const Data& player) applySpawnValues(player.spawn_data); placeSprite(); initSounds(); - previous_state_ = state_; } -// Pinta el jugador en pantalla +// ============================================================================ +// Render +// ============================================================================ + void Player::render() { sprite_->render(1, color_); -#ifdef _DEBUG - if (Debug::get()->isEnabled()) { - Screen::get()->getRendererSurface()->putPixel(under_right_foot_.x, under_right_foot_.y, 8); - Screen::get()->getRendererSurface()->putPixel(under_left_foot_.x, under_left_foot_.y, 8); - } -#endif } -// Actualiza las variables del objeto +// ============================================================================ +// Update — pipeline principal (6 fases) +// ============================================================================ + void Player::update(float delta_time) { - if (!is_paused_) { - handleInput(); - updateState(delta_time); - move(delta_time); - handleKillingTiles(); // Los collider_points_ están actualizados por syncSpriteAndCollider() dentro de move() - animate(delta_time); - border_ = handleBorders(); - } -} + if (is_paused_) { return; } -// Comprueba las entradas y modifica variables -void Player::handleInput() { - if (ignore_input_) { return; } - if (Input::get()->checkAction(InputAction::LEFT)) { - wanna_go_ = Direction::LEFT; - } else if (Input::get()->checkAction(InputAction::RIGHT)) { - wanna_go_ = Direction::RIGHT; - } else { - wanna_go_ = Direction::NONE; + // 1. Leer input + handleInput(); + + // 2. Calcular velocidades + updateVelocity(delta_time); + if (state_ == State::ON_AIR) { + applyGravity(delta_time); } - const bool JUMP_PRESSED = Input::get()->checkAction(InputAction::JUMP); - wanna_jump_ = JUMP_PRESSED && !jump_held_; // Solo en el flanco de pulsación - jump_held_ = JUMP_PRESSED; - wanna_down_ = Input::get()->checkAction(InputAction::DOWN); -} + // 3. Saltar o caer a través de plataformas/slopes + handleJumpAndDrop(); + + // 4. Aplicar movimiento con colisión + moveHorizontal(delta_time); + moveVertical(delta_time); + + // 5. Detectar caída + checkFalling(); + + // 6. Finalizar + syncSpriteAndCollider(); + animate(delta_time); + border_ = handleBorders(); -// La lógica de movimiento está distribuida en move -void Player::move(float delta_time) { - switch (state_) { - case State::ON_GROUND: - moveOnGround(delta_time); - break; - case State::ON_SLOPE: - moveOnSlope(delta_time); - break; - case State::ON_AIR: - moveOnAir(delta_time); - break; - } - syncSpriteAndCollider(); // Actualiza la posición del sprite y las colisiones #ifdef _DEBUG Debug::get()->set("P.X", std::to_string(static_cast(x_))); Debug::get()->set("P.Y", std::to_string(static_cast(y_))); @@ -102,23 +88,322 @@ void Player::move(float delta_time) { #endif } -void Player::handleConveyorBelts() { - if (!auto_movement_ and isOnConveyorBelt() and wanna_go_ == Direction::NONE) { - auto_movement_ = true; +// ============================================================================ +// Fase 1: Input +// ============================================================================ + +void Player::handleInput() { + if (ignore_input_) { return; } + if (Input::get()->checkAction(InputAction::LEFT)) { + wanna_go_ = Direction::LEFT; + } else if (Input::get()->checkAction(InputAction::RIGHT)) { + wanna_go_ = Direction::RIGHT; + } else { + wanna_go_ = Direction::NONE; } - if (auto_movement_ and !isOnConveyorBelt()) { - auto_movement_ = false; + const bool JUMP_PRESSED = Input::get()->checkAction(InputAction::JUMP); + wanna_jump_ = JUMP_PRESSED && !jump_held_; + jump_held_ = JUMP_PRESSED; + wanna_down_ = Input::get()->checkAction(InputAction::DOWN); +} + +// ============================================================================ +// Fase 2: Velocidades +// ============================================================================ + +void Player::updateVelocity(float delta_time) { + float target = 0.0F; + switch (wanna_go_) { + case Direction::LEFT: + target = -HORIZONTAL_VELOCITY; + break; + case Direction::RIGHT: + target = HORIZONTAL_VELOCITY; + break; + default: + target = 0.0F; + break; + } + + // Orientación del sprite + if (target > 0.0F) { + sprite_->setFlip(Flip::RIGHT); + } else if (target < 0.0F) { + sprite_->setFlip(Flip::LEFT); + } + + // Inercia: aire = gradual ambas direcciones, suelo = instantáneo arranque, gradual frenada + const float STEP = HORIZONTAL_ACCEL * delta_time; + if (state_ == State::ON_AIR) { + if (vx_ < target) { + vx_ = std::min(vx_ + STEP, target); + } else if (vx_ > target) { + vx_ = std::max(vx_ - STEP, target); + } + } else { + if (target != 0.0F) { + vx_ = target; + } else if (vx_ > 0.0F) { + vx_ = std::max(vx_ - STEP, 0.0F); + } else if (vx_ < 0.0F) { + vx_ = std::min(vx_ + STEP, 0.0F); + } } } -void Player::handleShouldFall() { - if (!isOnFloor() and (state_ == State::ON_GROUND || state_ == State::ON_SLOPE)) { +void Player::applyGravity(float delta_time) { + const float GRAVITY = (vy_ < 0.0F && !jump_held_) + ? GRAVITY_FORCE * LOW_JUMP_GRAVITY_MULT + : GRAVITY_FORCE; + vy_ += GRAVITY * delta_time; + vy_ = std::min(vy_, MAX_VY); +} + +// ============================================================================ +// Fase 3: Saltar y drop-through +// ============================================================================ + +void Player::handleJumpAndDrop() { + if (state_ == State::ON_AIR) { return; } + + if (wanna_jump_) { + startJump(); + return; + } + + // Drop-through: slope + if (wanna_down_ && state_ == State::ON_SLOPE) { + y_ += 1.0F; + vy_ = 0.0F; + transitionToState(State::ON_AIR); + return; + } + + // Drop-through: plataforma passable + if (wanna_down_ && state_ == State::ON_GROUND) { + const auto& tc = room_->getTileCollider(); + float foot_y = y_ + HEIGHT; + int foot_row = static_cast(foot_y) / Tile::SIZE; + int left_col = static_cast(x_) / Tile::SIZE; + int right_col = static_cast(x_ + WIDTH - 1) / Tile::SIZE; + + for (int col = left_col; col <= right_col; ++col) { + if (tc.getTileAt(col, foot_row) == TileCollider::Tile::PASSABLE) { + y_ += 1.0F; + vy_ = 0.0F; + transitionToState(State::ON_AIR); + return; + } + } + } +} + +void Player::startJump() { + vy_ = JUMP_VELOCITY; + last_grounded_position_ = y_; + Audio::get()->playSound(jump_sound_, Audio::Group::GAME); + transitionToState(State::ON_AIR); +} + +// ============================================================================ +// Fase 4a: Movimiento horizontal +// ============================================================================ + +void Player::moveHorizontal(float delta_time) { + if (vx_ == 0.0F) { + // Aunque no haya movimiento horizontal, si estamos en slope hay que seguirla + // (por si la gravedad nos ha movido o algo) + return; + } + + const auto& tc = room_->getTileCollider(); + float new_x = x_ + vx_ * delta_time; + + // Colisión con paredes + if (vx_ < 0.0F) { + float wall = tc.checkWallLeft(new_x, y_, WIDTH, HEIGHT); + if (wall != Collision::NONE) { + new_x = wall; + } + } else { + float wall = tc.checkWallRight(new_x, y_, WIDTH, HEIGHT); + if (wall != Collision::NONE) { + new_x = wall - WIDTH; + } + } + + x_ = new_x; + + // Si estamos en una slope, ajustar Y para seguirla + if (state_ == State::ON_SLOPE) { + followSlope(); + } + + // Si estamos en suelo plano, detectar entrada a slope + if (state_ == State::ON_GROUND) { + detectSlopeEntry(); + } +} + +void Player::followSlope() { + const auto& tc = room_->getTileCollider(); + + // Seleccionar pie según tipo de slope + float foot_x = (slope_type_ == TileCollider::Tile::SLOPE_L) ? x_ : x_ + WIDTH - 1; + + // Calcular Y en la slope actual + float surface_y = tc.getSlopeY(slope_tile_x_, slope_tile_y_, foot_x); + y_ = surface_y - HEIGHT; + + // Comprobar si hemos salido del tile actual + int foot_tile_x = static_cast(foot_x) / Tile::SIZE; + int foot_tile_y = static_cast(y_ + HEIGHT) / Tile::SIZE; + + if (foot_tile_x != slope_tile_x_ || foot_tile_y != slope_tile_y_) { + // Mirar si el nuevo tile también es slope + auto new_tile = tc.getTileAt(foot_tile_x, foot_tile_y); + if (new_tile == TileCollider::Tile::SLOPE_L || new_tile == TileCollider::Tile::SLOPE_R) { + slope_tile_x_ = foot_tile_x; + slope_tile_y_ = foot_tile_y; + slope_type_ = new_tile; + surface_y = tc.getSlopeY(slope_tile_x_, slope_tile_y_, foot_x); + y_ = surface_y - HEIGHT; + } else { + exitSlope(); + } + } +} + +void Player::exitSlope() { + const auto& tc = room_->getTileCollider(); + + // Corrección de 1px al salir por arriba de la slope + y_ += 1.0F; + float foot_y = y_ + HEIGHT; + + if (tc.hasGroundBelow(x_, foot_y, WIDTH)) { + transitionToState(State::ON_GROUND); + } else { vy_ = 0.0F; transitionToState(State::ON_AIR); } } +void Player::detectSlopeEntry() { + const auto& tc = room_->getTileCollider(); + float foot_y = y_ + HEIGHT; + + auto slope = tc.checkSlopeBelow(x_, foot_y, WIDTH); + + // LOG: siempre mostrar lo que ve detectSlopeEntry + int left_col = static_cast(x_) / Tile::SIZE; + int right_col = static_cast(x_ + WIDTH - 1) / Tile::SIZE; + int row = static_cast(foot_y) / Tile::SIZE; + auto tile_l = tc.getTileAt(left_col, row); + auto tile_r = tc.getTileAt(right_col, row); + SDL_Log("detectSlopeEntry: foot_y=%.1f row=%d cols=[%d,%d] tiles=[%d,%d] slope.on=%d", + foot_y, + row, + left_col, + right_col, + static_cast(tile_l), + static_cast(tile_r), + slope.on_slope ? 1 : 0); + + if (slope.on_slope) { + SDL_Log(" -> ENTERING slope type=%d tile=(%d,%d) surface_y=%.1f new_y=%.1f", + static_cast(slope.type), + slope.tile_x, + slope.tile_y, + slope.surface_y, + slope.surface_y - HEIGHT); + y_ = slope.surface_y - HEIGHT; + slope_tile_x_ = slope.tile_x; + slope_tile_y_ = slope.tile_y; + slope_type_ = slope.type; + transitionToState(State::ON_SLOPE); + } +} + +// ============================================================================ +// Fase 4b: Movimiento vertical +// ============================================================================ + +void Player::moveVertical(float delta_time) { + if (state_ != State::ON_AIR) { return; } + + const auto& tc = room_->getTileCollider(); + float displacement = vy_ * delta_time; + + if (vy_ < 0.0F) { + // Subiendo: comprobar techo + float new_y = y_ + displacement; + float ceiling = tc.checkCeiling(x_, new_y, WIDTH); + if (ceiling != Collision::NONE) { + y_ = ceiling; + vy_ = 0.0F; + } else { + y_ = new_y; + } + } else if (vy_ > 0.0F) { + // Bajando: comprobar suelo + float foot_y = y_ + HEIGHT; + float new_foot_y = foot_y + displacement; + auto hit = tc.checkFloor(x_, foot_y, WIDTH, new_foot_y); + + if (hit.y != Collision::NONE) { + y_ = hit.y - HEIGHT; + if (hit.type == TileCollider::Tile::SLOPE_L || hit.type == TileCollider::Tile::SLOPE_R) { + slope_tile_x_ = hit.tile_x; + slope_tile_y_ = hit.tile_y; + slope_type_ = hit.type; + transitionToState(State::ON_SLOPE); + } else { + transitionToState(State::ON_GROUND); + } + } else { + y_ += displacement; +#ifdef _DEBUG + if (y_ > PlayArea::BOTTOM + HEIGHT) { y_ = PlayArea::TOP + 2; } +#endif + } + } +} + +// ============================================================================ +// Fase 5: Detección de caída +// ============================================================================ + +void Player::checkFalling() { + if (state_ == State::ON_AIR) { return; } + + const auto& tc = room_->getTileCollider(); + + if (state_ == State::ON_SLOPE) { + // Verificar que el tile de slope sigue existiendo + auto tile = tc.getTileAt(slope_tile_x_, slope_tile_y_); + if (tile != TileCollider::Tile::SLOPE_L && tile != TileCollider::Tile::SLOPE_R) { + vy_ = 0.0F; + transitionToState(State::ON_AIR); + } + return; + } + + // ON_GROUND: comprobar si sigue habiendo suelo + float foot_y = y_ + HEIGHT; + bool ground = tc.hasGroundBelow(x_, foot_y, WIDTH); + if (!ground) { + SDL_Log("checkFalling: NO ground at foot_y=%.1f x=%.1f -> ON_AIR", foot_y, x_); + vy_ = 0.0F; + transitionToState(State::ON_AIR); + } +} + +// ============================================================================ +// Gestión de estado +// ============================================================================ + void Player::transitionToState(State state) { previous_state_ = state_; state_ = state; @@ -129,303 +414,70 @@ void Player::transitionToState(State state) { if (previous_state_ == State::ON_AIR) { Audio::get()->playSound(land_sound_, Audio::Group::GAME); } - current_slope_ = nullptr; break; case State::ON_SLOPE: vy_ = 0; if (previous_state_ == State::ON_AIR) { Audio::get()->playSound(land_sound_, Audio::Group::GAME); } - updateCurrentSlope(); - if (current_slope_ == nullptr) { - // Los pies no coinciden con ninguna rampa: tratar como suelo plano - state_ = State::ON_GROUND; - } break; case State::ON_AIR: last_grounded_position_ = static_cast(y_); - current_slope_ = nullptr; break; } } -void Player::updateState(float delta_time) { - switch (state_) { - case State::ON_GROUND: - updateOnGround(delta_time); - break; - case State::ON_SLOPE: - updateOnSlope(delta_time); - break; - case State::ON_AIR: - updateOnAir(delta_time); - break; - } -} +// ============================================================================ +// Bordes de pantalla +// ============================================================================ -// Inicia un salto desde ON_GROUND/ON_SLOPE: velocidad inicial hacia arriba + sonido + transición -void Player::startJump() { - vy_ = JUMP_VELOCITY; - last_grounded_position_ = y_; - Audio::get()->playSound(jump_sound_, Audio::Group::GAME); - transitionToState(State::ON_AIR); -} - -// Actualización lógica del estado ON_GROUND -void Player::updateOnGround(float delta_time) { - (void)delta_time; // No usado en este método, pero se mantiene por consistencia - handleConveyorBelts(); // Gestiona las cintas transportadoras - - // El salto tiene prioridad sobre la caída por falta de suelo - if (wanna_jump_) { - startJump(); - return; - } - handleShouldFall(); // Verifica si debe caer (no tiene suelo) -} - -// Actualización lógica del estado ON_SLOPE -void Player::updateOnSlope(float delta_time) { - (void)delta_time; // No usado en este método, pero se mantiene por consistencia - - if (wanna_jump_) { - startJump(); - return; - } - // DOWN: dejarse caer atravesando la rampa - if (wanna_down_) { - y_ += 1.0F; - vy_ = 0.0F; - transitionToState(State::ON_AIR); - return; - } - handleShouldFall(); -} - -// Actualización lógica del estado ON_AIR -void Player::updateOnAir(float delta_time) { - (void)delta_time; - auto_movement_ = false; // Desactiva el movimiento automático en el aire -} - -// Movimiento físico del estado ON_GROUND -void Player::moveOnGround(float delta_time) { - // Determinama cuál debe ser la velocidad a partir de automovement o de wanna_go_ - updateVelocity(delta_time); - - if (vx_ == 0.0F) { return; } - - // Movimiento horizontal y colision con muros - applyHorizontalMovement(delta_time); - - // Comprueba colision con rampas, corrige y cambia estado - const int SIDE_X = vx_ < 0.0F ? static_cast(x_) : static_cast(x_) + WIDTH - 1; - const LineVertical SIDE = { - .x = SIDE_X, - .y1 = static_cast(y_) + HEIGHT - 2, - .y2 = static_cast(y_) + HEIGHT - 1}; - - // Comprueba la rampa correspondiente según la dirección - const int SLOPE_Y = vx_ < 0.0F ? room_->checkLeftSlopes(SIDE) : room_->checkRightSlopes(SIDE); - if (SLOPE_Y != Collision::NONE) { - // Hay rampa: sube al jugador para pegarlo a la rampa - y_ = SLOPE_Y - HEIGHT; - transitionToState(State::ON_SLOPE); - } -#ifdef _DEBUG - Debug::get()->set("sl.detect_y", SLOPE_Y != Collision::NONE ? std::to_string(SLOPE_Y) : "-"); -#endif - - // Comprueba si está sobre una rampa - if (isOnSlope()) { transitionToState(State::ON_SLOPE); } -} - -// Movimiento físico del estado ON_SLOPE -void Player::moveOnSlope(float delta_time) { - // Determinama cuál debe ser la velocidad a partir de automovement o de wanna_go_ - updateVelocity(delta_time); - - // Verificar rampa válida antes de comprobar velocidad: si no hay rampa siempre caer, - // independientemente de si hay o no input (evita bloqueo con vx_=0 y slope null) - if (current_slope_ == nullptr) { - vy_ = 0.0F; - transitionToState(State::ON_AIR); - return; - } - - if (vx_ == 0.0F) { return; } - - // Determinar el tipo de rampa - const bool IS_LEFT_SLOPE = isLeftSlope(); - - // Movimiento horizontal con colisión lateral - applyHorizontalMovement(delta_time); - - // Seleccionar el pie apropiado según el tipo de rampa - // Left slopes (forma \) colisionan con el pie izquierdo - // Right slopes (forma /) colisionan con el pie derecho - const int X = IS_LEFT_SLOPE ? x_ : x_ + WIDTH - 1; - - // Calcular la Y basada en la ecuación de la rampa (45 grados) - // Left slope (\): y aumenta con x -> y = y1 + (x - x1) - // Right slope (/): y disminuye con x -> y = y1 - (x - x1) - if (IS_LEFT_SLOPE) { - y_ = current_slope_->y1 + (X - current_slope_->x1) - HEIGHT; - } else { - y_ = current_slope_->y1 - (X - current_slope_->x1) - HEIGHT; - } - - // Verificar si el pie ha salido de los límites horizontales de la rampa - // Usar min/max porque LEFT slopes tienen x1x2 - const int MIN_X = std::min(current_slope_->x1, current_slope_->x2); - const int MAX_X = std::max(current_slope_->x1, current_slope_->x2); - const bool OUT_OF_BOUNDS = (X < MIN_X) || (X > MAX_X); - -#ifdef _DEBUG - Debug::get()->set("sl.foot", std::to_string(X)); - Debug::get()->set("sl.y_c", std::to_string(static_cast(y_))); - Debug::get()->set("sl.oob", OUT_OF_BOUNDS ? "YES" : "ok"); -#endif - - if (OUT_OF_BOUNDS) { - // Determinar si estamos saliendo por arriba o por abajo de la rampa - const bool EXITING_DOWNWARD = (X > current_slope_->x2 && IS_LEFT_SLOPE) || - (X < current_slope_->x1 && !IS_LEFT_SLOPE); - const bool EXITING_UPWARD = (X < current_slope_->x1 && IS_LEFT_SLOPE) || - (X > current_slope_->x2 && !IS_LEFT_SLOPE); -#ifdef _DEBUG - Debug::get()->set("sl.oob", EXITING_DOWNWARD ? "DOWN" : "UP"); -#endif - - if (EXITING_DOWNWARD) { - // Salida por abajo: no hacer nada - // y_ += 1.0F; - } - - if (EXITING_UPWARD) { - // Salida por arriba: bajar un pixel ya que ha subido 1 de mas al salirse de la recta - y_ += 1.0F; - } - - // Verificar si hay soporte debajo (suelo plano o conveyor belt) - if (isOnTopSurface() || isOnConveyorBelt()) { - // Hay soporte: transición a ON_GROUND (podría ser superficie o conveyor belt) - transitionToState(State::ON_GROUND); - } else { - // Sin soporte: empezar a caer - vy_ = 0.0F; - transitionToState(State::ON_AIR); - } - return; - } - - // Verificar transición a superficie plana - /*if (isOnTopSurface()) { - transitionToState(State::ON_GROUND); - return; - }*/ -} - -// Movimiento físico del estado ON_AIR -// El jugador puede moverse horizontalmente en el aire y la gravedad siempre actúa. -void Player::moveOnAir(float delta_time) { - // Movimiento horizontal libre según wanna_go_ (permite girar en el aire) - updateVelocity(delta_time); - applyHorizontalMovement(delta_time); - - // Gravedad - applyGravity(delta_time); - - const float DISPLACEMENT_Y = vy_ * delta_time; - - // Subiendo: comprobar techo - if (vy_ < 0.0F) { - const SDL_FRect PROJECTION = getProjection(Direction::UP, DISPLACEMENT_Y); - const int POS = room_->checkBottomSurfaces(PROJECTION); - if (POS == Collision::NONE) { - y_ += DISPLACEMENT_Y; - } else { - // Choque con techo: se pega por debajo y empieza a caer - y_ = POS + 1; - vy_ = 0.0F; - } - return; - } - - // Bajando: comprobar aterrizaje en superficies y rampas - if (vy_ > 0.0F) { - const SDL_FRect PROJECTION = getProjection(Direction::DOWN, DISPLACEMENT_Y); - handleLandingFromAir(DISPLACEMENT_Y, PROJECTION); - } -} - -// Comprueba si el punto central del jugador ha sobrepasado alguno de los bordes auto Player::handleBorders() -> Room::Border { const float CENTER_X = x_ + WIDTH / 2.0F; const float CENTER_Y = y_ + HEIGHT / 2.0F; - if (CENTER_X < PlayArea::LEFT) { - return Room::Border::LEFT; - } - - if (CENTER_X > PlayArea::RIGHT) { - return Room::Border::RIGHT; - } - - if (CENTER_Y < PlayArea::TOP) { - return Room::Border::TOP; - } - - if (CENTER_Y > PlayArea::BOTTOM) { - return Room::Border::BOTTOM; - } - + if (CENTER_X < PlayArea::LEFT) { return Room::Border::LEFT; } + if (CENTER_X > PlayArea::RIGHT) { return Room::Border::RIGHT; } + if (CENTER_Y < PlayArea::TOP) { return Room::Border::TOP; } + if (CENTER_Y > PlayArea::BOTTOM) { return Room::Border::BOTTOM; } return Room::Border::NONE; } -// Cambia al jugador de un borde al opuesto conservando velocidad y estado. -// Usa el punto central para calcular la posición en la pantalla destino. void Player::switchBorders() { switch (border_) { case Room::Border::TOP: y_ += PlayArea::HEIGHT; last_grounded_position_ = static_cast(y_); break; - case Room::Border::BOTTOM: y_ -= PlayArea::HEIGHT; last_grounded_position_ = static_cast(y_); break; - case Room::Border::RIGHT: x_ -= PlayArea::WIDTH; break; - case Room::Border::LEFT: x_ += PlayArea::WIDTH; break; - default: break; } - border_ = Room::Border::NONE; syncSpriteAndCollider(); } -// Aplica gravedad al jugador -void Player::applyGravity(float delta_time) { - if (state_ == State::ON_AIR) { - // Si está subiendo y ha soltado el botón de salto, gravedad aumentada para cortar el salto - const float GRAVITY = (vy_ < 0.0F && !jump_held_) - ? GRAVITY_FORCE * LOW_JUMP_GRAVITY_MULT - : GRAVITY_FORCE; - vy_ += GRAVITY * delta_time; - vy_ = std::min(vy_, MAX_VY); - } +// ============================================================================ +// Geometría y renderizado +// ============================================================================ + +void Player::syncSpriteAndCollider() { + placeSprite(); + collider_box_ = getRect(); +} + +void Player::placeSprite() { + sprite_->setPos(x_, y_); } -// Establece la animación del jugador void Player::animate(float delta_time) { // NOLINT(readability-make-member-function-const) if (state_ == State::ON_AIR) { sprite_->setCurrentAnimation("jump"); @@ -437,218 +489,40 @@ void Player::animate(float delta_time) { // NOLINT(readability-make-member-func } } -// Comprueba si el jugador tiene suelo debajo de los pies -auto Player::isOnFloor() -> bool { - bool on_top_surface = false; - bool on_conveyor_belt = false; - updateFeet(); +// ============================================================================ +// Color y skin +// ============================================================================ - // Comprueba las superficies - on_top_surface |= room_->checkTopSurfaces(under_left_foot_); - on_top_surface |= room_->checkTopSurfaces(under_right_foot_); - - // Comprueba las cintas transportadoras - on_conveyor_belt |= room_->checkConveyorBelts(under_left_foot_); - on_conveyor_belt |= room_->checkConveyorBelts(under_right_foot_); - - // Comprueba las rampas - auto on_slope_l = room_->checkLeftSlopes(under_left_foot_); - auto on_slope_r = room_->checkRightSlopes(under_right_foot_); - - return on_top_surface || on_conveyor_belt || on_slope_l || on_slope_r; -} - -// Comprueba si el jugador está sobre una superficie -auto Player::isOnTopSurface() -> bool { - bool on_top_surface = false; - updateFeet(); - - // Comprueba las superficies - on_top_surface |= room_->checkTopSurfaces(under_left_foot_); - on_top_surface |= room_->checkTopSurfaces(under_right_foot_); - - return on_top_surface; -} - -// Comprueba si el jugador esta sobre una cinta transportadora -auto Player::isOnConveyorBelt() -> bool { - bool on_conveyor_belt = false; - updateFeet(); - - // Comprueba las superficies - on_conveyor_belt |= room_->checkConveyorBelts(under_left_foot_); - on_conveyor_belt |= room_->checkConveyorBelts(under_right_foot_); - - return on_conveyor_belt; -} - -// Comprueba si el jugador está sobre una rampa -// Retorna true SOLO si un pie está en rampa Y el otro pie está volando (sin soporte) -auto Player::isOnSlope() -> bool { - updateFeet(); - - // Verificar qué pie está en qué tipo de rampa - const bool LEFT_FOOT_ON_LEFT_SLOPE = room_->checkLeftSlopes(under_left_foot_); - const bool RIGHT_FOOT_ON_RIGHT_SLOPE = room_->checkRightSlopes(under_right_foot_); - - // Verificar si cada pie está "volando" (sin soporte: ni top surface ni conveyor belt) - const bool LEFT_FOOT_FLYING = !(room_->checkTopSurfaces(under_left_foot_) || - room_->checkConveyorBelts(under_left_foot_)); - const bool RIGHT_FOOT_FLYING = !(room_->checkTopSurfaces(under_right_foot_) || - room_->checkConveyorBelts(under_right_foot_)); - - // Retornar true si UN pie en rampa Y el OTRO volando - return (LEFT_FOOT_ON_LEFT_SLOPE && RIGHT_FOOT_FLYING) || - (RIGHT_FOOT_ON_RIGHT_SLOPE && LEFT_FOOT_FLYING); -} - -// Comprueba si current_slope_ es una rampa izquierda (ascendente a la izquierda) -// Las rampas izquierdas tienen forma \ con x1 < x2 (x aumenta de izq a der) -auto Player::isLeftSlope() -> bool { - if (current_slope_ == nullptr) { - return false; - } - // Left slopes (\): x1 < x2 (x aumenta de izquierda a derecha) - // Right slopes (/): x1 > x2 (x decrece de izquierda a derecha) - return current_slope_->x1 < current_slope_->x2; -} - -// Actualiza current_slope_ con la rampa correcta según el pie que toca -void Player::updateCurrentSlope() { - updateFeet(); - - // Left slopes (\) ascendentes a izquierda tocan el pie izquierdo - if (room_->checkLeftSlopes(under_left_foot_)) { - current_slope_ = room_->getSlopeAtPoint(under_left_foot_); - } - // Right slopes (/) ascendentes a derecha tocan el pie derecho - else if (room_->checkRightSlopes(under_right_foot_)) { - current_slope_ = room_->getSlopeAtPoint(under_right_foot_); - } - // Fallback para casos edge - else { - current_slope_ = room_->getSlopeAtPoint(under_left_foot_); - if (current_slope_ == nullptr) { - current_slope_ = room_->getSlopeAtPoint(under_right_foot_); - } - } - -#ifdef _DEBUG - if (current_slope_ != nullptr) { - Debug::get()->set("sl.type", isLeftSlope() ? "L\\" : "R/"); - Debug::get()->set("sl.p1", std::to_string(current_slope_->x1) + "," + std::to_string(current_slope_->y1)); - Debug::get()->set("sl.p2", std::to_string(current_slope_->x2) + "," + std::to_string(current_slope_->y2)); - } else { - Debug::get()->set("sl.type", "null"); - Debug::get()->unset("sl.p1"); - Debug::get()->unset("sl.p2"); - } -#endif -} - -// Comprueba que el jugador no toque ningun tile de los que matan -auto Player::handleKillingTiles() -> bool { - // Comprueba si hay contacto con algún tile que mata - if (std::ranges::any_of(collider_points_, [this](const auto& c) -> bool { - return room_->getTile(c) == Room::Tile::KILL; - })) { - markAsDead(); // Mata al jugador inmediatamente - return true; // Retorna en cuanto se detecta una colisión - } - - return false; // No se encontró ninguna colisión -} - -// Establece el color del jugador (0 = automático según options) void Player::setColor(Uint8 color) { if (color != 0) { color_ = color; return; } - - // Color personalizado desde opciones if (Options::game.player_color >= 0) { color_ = static_cast(Options::game.player_color); } else { color_ = 14; } - - // Si el color coincide con el fondo de la habitación, usar fallback if (room_ != nullptr && color_ == room_->getBGColor()) { - color_ = (room_->getBGColor() != 14) - ? 14 - : 1; + color_ = (room_->getBGColor() != 14) ? 14 : 1; } } -// Actualiza los puntos de colisión -void Player::updateColliderPoints() { - // 3 columnas × 4 filas: garantiza que cada tile de 8×8 que solape el jugador tenga al menos un punto - const float L = x_; - const float M = x_ + (WIDTH / 2); - const float R = x_ + WIDTH - 1; - const float Y0 = y_; - const float Y1 = y_ + 8; - const float Y2 = y_ + 16; - const float Y3 = y_ + HEIGHT - 1; - collider_points_[0] = {.x = L, .y = Y0}; - collider_points_[1] = {.x = M, .y = Y0}; - collider_points_[2] = {.x = R, .y = Y0}; - collider_points_[3] = {.x = L, .y = Y1}; - collider_points_[4] = {.x = M, .y = Y1}; - collider_points_[5] = {.x = R, .y = Y1}; - collider_points_[6] = {.x = L, .y = Y2}; - collider_points_[7] = {.x = M, .y = Y2}; - collider_points_[8] = {.x = R, .y = Y2}; - collider_points_[9] = {.x = L, .y = Y3}; - collider_points_[10] = {.x = M, .y = Y3}; - collider_points_[11] = {.x = R, .y = Y3}; -} - -// Actualiza los puntos de los pies -void Player::updateFeet() { - under_left_foot_ = { - .x = x_, - .y = y_ + HEIGHT}; - under_right_foot_ = { - .x = x_ + WIDTH - 1, - .y = y_ + HEIGHT}; -} - -// Inicializa los sonidos de salto y aterrizaje -void Player::initSounds() { // NOLINT(readability-convert-member-functions-to-static) - jump_sound_ = Resource::Cache::get()->getSound("jump.wav"); - land_sound_ = Resource::Cache::get()->getSound("land.wav"); -} - -// Aplica los valores de spawn al jugador -void Player::applySpawnValues(const SpawnData& spawn) { - x_ = spawn.x; - y_ = spawn.y; - y_prev_ = spawn.y; // Inicializar y_prev_ igual a y_ para evitar saltos en primer frame - vx_ = spawn.vx; - vy_ = spawn.vy; - last_grounded_position_ = spawn.last_grounded_position; - state_ = spawn.state; - sprite_->setFlip(spawn.flip); -} - -// Resuelve nombre de skin a fichero de animación auto Player::skinToAnimationPath(const std::string& skin_name) -> std::string { - if (skin_name == "default") { - return "player.yaml"; - } + if (skin_name == "default") { return "player.yaml"; } return skin_name + ".yaml"; } -// Cambia la skin del jugador en caliente preservando la orientación actual void Player::setSkin(const std::string& skin_name) { const auto FLIP = sprite_->getFlip(); initSprite(skinToAnimationPath(skin_name)); sprite_->setFlip(FLIP); } -// Inicializa el sprite del jugador +// ============================================================================ +// Inicialización +// ============================================================================ + void Player::initSprite(const std::string& animations_path) { // NOLINT(readability-convert-member-functions-to-static) const auto& animation_data = Resource::Cache::get()->getAnimationData(animations_path); sprite_ = std::make_unique(animation_data); @@ -657,185 +531,44 @@ void Player::initSprite(const std::string& animations_path) { // NOLINT(readabi sprite_->setCurrentAnimation("default"); } -// Actualiza la posición del sprite y las colisiones -void Player::syncSpriteAndCollider() { - placeSprite(); // Coloca el sprite en la posición del jugador - collider_box_ = getRect(); // Actualiza el rectangulo de colisión - updateColliderPoints(); // Actualiza los puntos de colisión -#ifdef _DEBUG - updateFeet(); -#endif +void Player::initSounds() { // NOLINT(readability-convert-member-functions-to-static) + jump_sound_ = Resource::Cache::get()->getSound("jump.wav"); + land_sound_ = Resource::Cache::get()->getSound("land.wav"); } -// Coloca el sprite en la posición del jugador -void Player::placeSprite() { - sprite_->setPos(x_, y_); +void Player::applySpawnValues(const SpawnData& spawn) { + x_ = spawn.x; + y_ = spawn.y; + vx_ = spawn.vx; + vy_ = spawn.vy; + last_grounded_position_ = spawn.last_grounded_position; + state_ = spawn.state; + sprite_->setFlip(spawn.flip); } -// Calcula la velocidad en x con inercia ligera (interpolación hacia vel. objetivo) -void Player::updateVelocity(float delta_time) { - float target = 0.0F; - if (auto_movement_) { - // La cinta transportadora tiene el control - target = HORIZONTAL_VELOCITY * room_->getConveyorBeltDirection(); - } else { - switch (wanna_go_) { - case Direction::LEFT: - target = -HORIZONTAL_VELOCITY; - break; - case Direction::RIGHT: - target = HORIZONTAL_VELOCITY; - break; - default: - target = 0.0F; - break; - } - } +// ============================================================================ +// Utilidades +// ============================================================================ - // Orientación del sprite según la dirección deseada (sin cambiar cuando target=0) - if (target > 0.0F) { - sprite_->setFlip(Flip::RIGHT); - } else if (target < 0.0F) { - sprite_->setFlip(Flip::LEFT); - } - - // Inercia: - // - En el aire: inercia completa (arranque y frenada graduales) - // - En suelo/rampa: arranque instantáneo, frenada gradual - const float STEP = HORIZONTAL_ACCEL * delta_time; - if (state_ == State::ON_AIR) { - if (vx_ < target) { - vx_ = std::min(vx_ + STEP, target); - } else if (vx_ > target) { - vx_ = std::max(vx_ - STEP, target); - } - } else { - if (target != 0.0F) { - vx_ = target; // Arranque instantáneo - } else if (vx_ > 0.0F) { - vx_ = std::max(vx_ - STEP, 0.0F); // Frenada gradual - } else if (vx_ < 0.0F) { - vx_ = std::min(vx_ + STEP, 0.0F); // Frenada gradual - } - } -} - -// Aplica movimiento horizontal con colisión de muros -void Player::applyHorizontalMovement(float delta_time) { - if (vx_ == 0.0F) { return; } - - const float DISPLACEMENT = vx_ * delta_time; - if (vx_ < 0.0F) { - const SDL_FRect PROJECTION = getProjection(Direction::LEFT, DISPLACEMENT); - const int POS = room_->checkRightSurfaces(PROJECTION); - if (POS == Collision::NONE) { - x_ += DISPLACEMENT; - } else { - x_ = POS + 1; - } - } else { - const SDL_FRect PROJECTION = getProjection(Direction::RIGHT, DISPLACEMENT); - const int POS = room_->checkLeftSurfaces(PROJECTION); - if (POS == Collision::NONE) { - x_ += DISPLACEMENT; - } else { - x_ = POS - WIDTH; - } - } -} - -// Detecta aterrizaje en superficies y rampas -auto Player::handleLandingFromAir(float displacement, const SDL_FRect& projection) -> bool { - // Comprueba la colisión con las superficies y las cintas transportadoras - const float POS = std::max(room_->checkTopSurfaces(projection), room_->checkAutoSurfaces(projection)); - if (POS != Collision::NONE) { - // Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie - y_ = POS - HEIGHT; - transitionToState(State::ON_GROUND); - return true; - } - - // Comprueba la colisión con las rampas - auto rect = toSDLRect(projection); - const LineVertical LEFT_SIDE = {.x = rect.x, .y1 = rect.y, .y2 = rect.y + rect.h}; - const LineVertical RIGHT_SIDE = {.x = rect.x + rect.w - 1, .y1 = rect.y, .y2 = rect.y + rect.h}; - const float POINT = std::max(room_->checkRightSlopes(RIGHT_SIDE), room_->checkLeftSlopes(LEFT_SIDE)); - if (POINT != Collision::NONE) { - y_ = POINT - HEIGHT; - transitionToState(State::ON_SLOPE); - return true; - } - - // No hay colisión - y_ += displacement; -#ifdef _DEBUG - // Guarda por si en debug el jugador se sale de la pantalla, para que no esté cayendo infinitamente - if (y_ > PlayArea::BOTTOM + HEIGHT) { y_ = PlayArea::TOP + 2; } -#endif - return false; -} - -// Devuelve el rectangulo de proyeccion -auto Player::getProjection(Direction direction, float displacement) -> SDL_FRect { // NOLINT(readability-convert-member-functions-to-static) - switch (direction) { - case Direction::LEFT: - return { - .x = x_ + displacement, - .y = y_, - .w = std::ceil(std::fabs(displacement)), // Para evitar que tenga una anchura de 0 pixels - .h = HEIGHT - 1}; // -1 para dar ventana de 2px en aperturas de altura exacta - - case Direction::RIGHT: - return { - .x = x_ + WIDTH, - .y = y_, - .w = std::ceil(displacement), // Para evitar que tenga una anchura de 0 pixels - .h = HEIGHT - 1}; // -1 para dar ventana de 2px en aperturas de altura exacta - - case Direction::UP: - return { - .x = x_, - .y = y_ + displacement, - .w = WIDTH, - .h = std::ceil(std::fabs(displacement)) // Para evitar que tenga una altura de 0 pixels - }; - - case Direction::DOWN: - return { - .x = x_, - .y = y_ + HEIGHT, - .w = WIDTH, - .h = std::ceil(displacement) // Para evitar que tenga una altura de 0 pixels - }; - - default: - return { - .x = 0.0F, - .y = 0.0F, - .w = 0.0F, - .h = 0.0F}; - } -} - -// Marca al jugador como muerto void Player::markAsDead() { is_alive_ = (Options::cheats.invincible == Options::Cheat::State::ENABLED); } #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; last_grounded_position_ = static_cast(y_); + slope_tile_x_ = 0; + slope_tile_y_ = 0; + slope_type_ = TileCollider::Tile::EMPTY; transitionToState(State::ON_GROUND); syncSpriteAndCollider(); } -#endif \ No newline at end of file +#endif diff --git a/source/game/entities/player.hpp b/source/game/entities/player.hpp index 0ca6882..81c5217 100644 --- a/source/game/entities/player.hpp +++ b/source/game/entities/player.hpp @@ -2,26 +2,24 @@ #include -#include // Para array -#include // Para numeric_limits -#include // Para shared_ptr, __shared_ptr_access +#include // Para shared_ptr #include // Para string #include -#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_ = 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_ = 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_; // Objeto encargado de gestionar cada habitación del juego - std::unique_ptr sprite_; // Sprite del jugador + std::shared_ptr room_; + std::unique_ptr 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 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 -}; \ No newline at end of file + // --- Utilidad --- + void markAsDead(); +}; diff --git a/source/game/gameplay/collision_map.cpp b/source/game/gameplay/collision_map.cpp index 31d795f..b060ed4 100644 --- a/source/game/gameplay/collision_map.cpp +++ b/source/game/gameplay/collision_map.cpp @@ -10,7 +10,8 @@ // Constructor CollisionMap::CollisionMap(std::vector collision_tile_map, int conveyor_belt_direction) : collision_tile_map_(std::move(collision_tile_map)), - conveyor_belt_direction_(conveyor_belt_direction) { + conveyor_belt_direction_(conveyor_belt_direction), + tile_collider_(collision_tile_map_) { // Inicializa todas las superficies de colisión initializeSurfaces(); } @@ -41,13 +42,20 @@ auto CollisionMap::getTile(int index) const -> Tile { } switch (collision_tile_map_[index]) { - case 1: return Tile::WALL; - case 2: return Tile::PASSABLE; - case 3: return Tile::SLOPE_L; - case 4: return Tile::SLOPE_R; - case 5: return Tile::KILL; - case 6: return Tile::ANIMATED; - default: return Tile::EMPTY; + case 1: + return Tile::WALL; + case 2: + return Tile::PASSABLE; + case 3: + return Tile::SLOPE_L; + case 4: + return Tile::SLOPE_R; + case 5: + return Tile::KILL; + case 6: + return Tile::ANIMATED; + default: + return Tile::EMPTY; } } diff --git a/source/game/gameplay/collision_map.hpp b/source/game/gameplay/collision_map.hpp index 8dc74c5..8817ac8 100644 --- a/source/game/gameplay/collision_map.hpp +++ b/source/game/gameplay/collision_map.hpp @@ -4,8 +4,9 @@ #include // Para vector -#include "utils/defines.hpp" // Para Tile::SIZE, Map::WIDTH, Map::HEIGHT -#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical +#include "game/gameplay/tile_collider.hpp" // Para TileCollider +#include "utils/defines.hpp" // Para Tile::SIZE, Map::WIDTH, Map::HEIGHT +#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical /** * @brief Mapa de colisiones de una habitación @@ -71,6 +72,7 @@ class CollisionMap { // --- Getters --- [[nodiscard]] auto getConveyorBeltDirection() const -> int { return conveyor_belt_direction_; } + [[nodiscard]] auto getTileCollider() const -> const TileCollider& { return tile_collider_; } // Getters para debug visualization [[nodiscard]] auto getBottomFloors() const -> const std::vector& { return bottom_floors_; } @@ -90,6 +92,7 @@ class CollisionMap { // --- Datos de la habitación --- std::vector collision_tile_map_; // Mapa de colisiones por tile int conveyor_belt_direction_; // Dirección de conveyor belts + TileCollider tile_collider_; // Sistema de colisión por tiles // --- Geometría de colisión --- std::vector bottom_floors_; // Superficies inferiores (suelos) diff --git a/source/game/gameplay/room.cpp b/source/game/gameplay/room.cpp index 0cf927c..aa93f02 100644 --- a/source/game/gameplay/room.cpp +++ b/source/game/gameplay/room.cpp @@ -161,6 +161,10 @@ void Room::setPaused(bool value) { item_manager_->setPaused(value); } +auto Room::getTileCollider() const -> const TileCollider& { + return collision_map_->getTileCollider(); +} + // Devuelve la cadena del fichero de la habitación contigua segun el borde auto Room::getRoom(Border border) -> std::string { // NOLINT(readability-convert-member-functions-to-static) switch (border) { diff --git a/source/game/gameplay/room.hpp b/source/game/gameplay/room.hpp index 089b3dc..2193d3c 100644 --- a/source/game/gameplay/room.hpp +++ b/source/game/gameplay/room.hpp @@ -16,6 +16,7 @@ class Surface; // lines 13-13 class EnemyManager; class ItemManager; class CollisionMap; +class TileCollider; class TilemapRenderer; class Room { @@ -102,6 +103,7 @@ class Room { [[nodiscard]] auto getSlopeAtPoint(const SDL_FPoint& p) const -> const LineDiagonal*; // Obtiene puntero a slope en un punto void setPaused(bool value); // Pone el mapa en modo pausa [[nodiscard]] auto getConveyorBeltDirection() const -> int { return conveyor_belt_direction_; } // Obten la direccion de las superficies automaticas + [[nodiscard]] auto getTileCollider() const -> const TileCollider&; // Método de carga de archivos YAML (delegado a RoomLoader) static auto loadYAML(const std::string& file_path, bool verbose = false) -> Data; // Carga habitación desde archivo YAML unificado diff --git a/source/game/gameplay/tile_collider.cpp b/source/game/gameplay/tile_collider.cpp new file mode 100644 index 0000000..aa146a7 --- /dev/null +++ b/source/game/gameplay/tile_collider.cpp @@ -0,0 +1,182 @@ +#include "game/gameplay/tile_collider.hpp" + +#include // Para std::min, std::max +#include // Para std::ceil + +#include "utils/defines.hpp" + +TileCollider::TileCollider(const std::vector& collision_tile_map) + : tile_map_(collision_tile_map) {} + +// --- Queries básicas --- + +auto TileCollider::getTileAt(int tile_x, int tile_y) const -> Tile { + if (tile_x < 0 || tile_x >= MW || tile_y < 0 || tile_y >= MH) { + return Tile::EMPTY; + } + int value = tile_map_[tile_y * MW + tile_x]; + if (value >= 0 && value <= 4) { + return static_cast(value); + } + return Tile::EMPTY; +} + +auto TileCollider::isSolid(int tile_x, int tile_y) const -> bool { + return getTileAt(tile_x, tile_y) == Tile::WALL; +} + +auto TileCollider::getSlopeY(int tile_x, int tile_y, float px) const -> float { + float tile_bottom = static_cast((tile_y + 1) * TS - 1); + float x_in_tile = px - static_cast(tile_x * TS); + x_in_tile = std::clamp(x_in_tile, 0.0F, static_cast(TS - 1)); + + auto tile = getTileAt(tile_x, tile_y); + if (tile == Tile::SLOPE_L) { + // \ descendente de izquierda a derecha + return tile_bottom - (static_cast(TS - 1) - x_in_tile); + } + if (tile == Tile::SLOPE_R) { + // / descendente de derecha a izquierda + return tile_bottom - x_in_tile; + } + return tile_bottom; +} + +// --- Colisión con paredes --- + +auto TileCollider::checkWallLeft(float x, float y, float w, float h) const -> float { + (void)w; + int col = toTile(static_cast(x) - 1); + int top_row = toTile(static_cast(y)); + int bot_row = toTile(static_cast(y + h - 2)); + + for (int row = top_row; row <= bot_row; ++row) { + if (isSolid(col, row)) { + return static_cast((col + 1) * TS); + } + } + return Collision::NONE; +} + +auto TileCollider::checkWallRight(float x, float y, float w, float h) const -> float { + int col = toTile(static_cast(x + w)); + int top_row = toTile(static_cast(y)); + int bot_row = toTile(static_cast(y + h - 2)); + + for (int row = top_row; row <= bot_row; ++row) { + if (isSolid(col, row)) { + return static_cast(col * TS); + } + } + return Collision::NONE; +} + +// --- Colisión con techo --- + +auto TileCollider::checkCeiling(float x, float y, float w) const -> float { + int top_row = toTile(static_cast(y)); + int left_col = toTile(static_cast(x)); + int right_col = toTile(static_cast(x + w - 1)); + + for (int col = left_col; col <= right_col; ++col) { + if (isSolid(col, top_row)) { + return static_cast((top_row + 1) * TS); + } + } + return Collision::NONE; +} + +// --- Colisión con suelo (landing) --- + +auto TileCollider::checkFloor(float x, float foot_y_current, float w, float foot_y_new) const -> FloorHit { + int start_row = toTile(static_cast(foot_y_current)); + int end_row = toTile(static_cast(foot_y_new)); + int left_col = toTile(static_cast(x)); + int right_col = toTile(static_cast(x + w - 1)); + + FloorHit best; + + for (int row = start_row; row <= end_row; ++row) { + for (int col = left_col; col <= right_col; ++col) { + auto tile = getTileAt(col, row); + float floor_y = Collision::NONE; + + if (tile == Tile::WALL) { + floor_y = static_cast(row * TS); + } else if (tile == Tile::PASSABLE) { + float tile_top = static_cast(row * TS); + // Solo cuenta como suelo si los pies estaban por encima antes del movimiento + if (foot_y_current <= tile_top) { + floor_y = tile_top; + } + } else if (tile == Tile::SLOPE_L || tile == Tile::SLOPE_R) { + float check_x = (tile == Tile::SLOPE_L) ? x : x + w - 1; + float slope_y = getSlopeY(col, row, check_x); + if (foot_y_new >= slope_y && foot_y_current <= slope_y + TS) { + floor_y = slope_y; + } + } + + if (floor_y != Collision::NONE && (best.y == Collision::NONE || floor_y < best.y)) { + best = {floor_y, tile, col, row}; + } + } + } + + return best; +} + +// --- Detección de suelo debajo --- + +auto TileCollider::hasGroundBelow(float x, float foot_y, float w) const -> bool { + int row = toTile(static_cast(foot_y)); + int left_col = toTile(static_cast(x)); + int right_col = toTile(static_cast(x + w - 1)); + + for (int col = left_col; col <= right_col; ++col) { + auto tile = getTileAt(col, row); + if (tile == Tile::WALL || tile == Tile::PASSABLE) { + return true; + } + if (tile == Tile::SLOPE_L || tile == Tile::SLOPE_R) { + float check_x = (tile == Tile::SLOPE_L) ? x : x + w - 1; + float slope_y = getSlopeY(col, row, check_x); + if (slope_y <= foot_y + 1) { + return true; + } + } + } + return false; +} + +// --- Detección de slope debajo (transición ground→slope) --- + +auto TileCollider::checkSlopeBelow(float x, float foot_y, float w) const -> SlopeInfo { + int foot_row = toTile(static_cast(foot_y)); + int left_col = toTile(static_cast(x)); + int right_col = toTile(static_cast(x + w - 1)); + + // Comprobar la fila de los pies Y la fila de arriba + // (la slope entry puede estar un tile arriba del nivel de la plataforma) + for (int row = foot_row - 1; row <= foot_row; ++row) { + for (int col = left_col; col <= right_col; ++col) { + auto tile = getTileAt(col, row); + if (tile == Tile::SLOPE_L) { + float foot_x = (col == left_col) ? x : x + w - 1; + float slope_y = getSlopeY(col, row, foot_x); + if (slope_y <= foot_y && slope_y >= foot_y - TS) { + return {true, Tile::SLOPE_L, col, row, slope_y}; + } + } + if (tile == Tile::SLOPE_R) { + float foot_x = (col == right_col) ? x + w - 1 : x; + float slope_y = getSlopeY(col, row, foot_x); + if (slope_y <= foot_y && slope_y >= foot_y - TS) { + return {true, Tile::SLOPE_R, col, row, slope_y}; + } + } + } + } + + return {}; +} diff --git a/source/game/gameplay/tile_collider.hpp b/source/game/gameplay/tile_collider.hpp new file mode 100644 index 0000000..af48bec --- /dev/null +++ b/source/game/gameplay/tile_collider.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include + +#include "utils/defines.hpp" + +class TileCollider { + public: + enum class Tile : int { + EMPTY = 0, + WALL = 1, + PASSABLE = 2, + SLOPE_L = 3, + SLOPE_R = 4 + }; + + struct FloorHit { + float y{-1}; + Tile type{Tile::EMPTY}; + int tile_x{0}; + int tile_y{0}; + }; + + struct SlopeInfo { + bool on_slope{false}; + Tile type{Tile::EMPTY}; + int tile_x{0}; + int tile_y{0}; + float surface_y{0}; + }; + + explicit TileCollider(const std::vector& collision_tile_map); + + // Queries básicas + [[nodiscard]] auto getTileAt(int tile_x, int tile_y) const -> Tile; + [[nodiscard]] auto isSolid(int tile_x, int tile_y) const -> bool; + [[nodiscard]] auto getSlopeY(int tile_x, int tile_y, float px) const -> float; + + // Colisión para el Player + [[nodiscard]] auto checkWallLeft(float x, float y, float w, float h) const -> float; + [[nodiscard]] auto checkWallRight(float x, float y, float w, float h) const -> float; + [[nodiscard]] auto checkCeiling(float x, float y, float w) const -> float; + [[nodiscard]] auto checkFloor(float x, float foot_y_current, float w, float foot_y_new) const -> FloorHit; + [[nodiscard]] auto hasGroundBelow(float x, float foot_y, float w) const -> bool; + [[nodiscard]] auto checkSlopeBelow(float x, float foot_y, float w) const -> SlopeInfo; + + private: + static constexpr int TS = ::Tile::SIZE; + static constexpr int MW = ::Map::WIDTH; + static constexpr int MH = ::Map::HEIGHT; + + const std::vector& tile_map_; + + [[nodiscard]] static auto toTile(int px) -> int { return px / TS; } +};