From 138eb26249cafb7d2744c6a8e344f47946fc4bee Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Thu, 9 Apr 2026 21:54:18 +0200 Subject: [PATCH] fix: les rampes s'acabaven de trencar --- source/game/entities/player.cpp | 16 ++++++++-------- source/game/gameplay/tile_collider.hpp | 17 ++++++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/source/game/entities/player.cpp b/source/game/entities/player.cpp index 645ef7f..a01b719 100644 --- a/source/game/entities/player.cpp +++ b/source/game/entities/player.cpp @@ -202,9 +202,9 @@ void Player::handleJumpAndDrop() { 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; + int foot_row = tc.toTile(static_cast(foot_y)); + int left_col = tc.toTile(static_cast(x_)); + int right_col = tc.toTile(static_cast(x_ + WIDTH - 1)); for (int col = left_col; col <= right_col; ++col) { if (tc.getTileAt(col, foot_row) == TileCollider::Tile::PASSABLE) { @@ -267,9 +267,9 @@ void Player::followSlope() { 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; + // Comprobar si hemos salido del tile actual (coordenadas del mapa extendido) + int foot_tile_x = tc.toTile(static_cast(foot_x)); + int foot_tile_y = tc.toTile(static_cast(y_ + HEIGHT)); if (foot_tile_x != slope_tile_x_ || foot_tile_y != slope_tile_y_) { // Buscar slope en el tile calculado y en el de abajo (la escalera de slopes @@ -302,8 +302,8 @@ void Player::exitSlope() { for (int check = 0; check <= 1; ++check) { float check_y = foot_y + check; if (tc.hasGroundBelow(x_, check_y, WIDTH)) { - int row = static_cast(check_y) / Tile::SIZE; - y_ = static_cast(row * Tile::SIZE) - HEIGHT; + int row = tc.toTile(static_cast(check_y)); + y_ = tc.toPixel(row) - HEIGHT; transitionToState(State::ON_GROUND); return; } diff --git a/source/game/gameplay/tile_collider.hpp b/source/game/gameplay/tile_collider.hpp index 32069b2..03564fe 100644 --- a/source/game/gameplay/tile_collider.hpp +++ b/source/game/gameplay/tile_collider.hpp @@ -53,6 +53,14 @@ class TileCollider { // Devuelve true si el rectángulo del jugador solapa algún tile KILL [[nodiscard]] auto touchesKillTile(float x, float y, float w, float h) const -> bool; + // Convierte píxeles en room-space a índice de tile en el mapa extendido. + [[nodiscard]] auto toTile(int px) const -> int { return (px + border_px_) / TS; } + + // Convierte índice de tile del mapa extendido a píxeles en room-space. + [[nodiscard]] auto toPixel(int tile) const -> float { + return static_cast((tile * TS) - border_px_); + } + private: static constexpr int TS = ::Tile::SIZE; @@ -61,13 +69,4 @@ class TileCollider { int border_px_; // Offset en píxeles (CollisionBorder::PX) const std::vector& tile_map_; - - // Convierte píxeles en room-space a índice de tile en el mapa extendido. - // Nota: asume que px >= -border_px_ (el jugador no puede estar más allá del borde). - [[nodiscard]] auto toTile(int px) const -> int { return (px + border_px_) / TS; } - - // Convierte índice de tile del mapa extendido a píxeles en room-space. - [[nodiscard]] auto toPixel(int tile) const -> float { - return static_cast((tile * TS) - border_px_); - } };