Files

241 lines
9.3 KiB
C++

#include "game/gameplay/tile_collider.hpp"
#include <algorithm> // Para std::min, std::max, std::clamp
#include "utils/defines.hpp"
TileCollider::TileCollider(const std::vector<int>& extended_tile_map, int width, int height, int border_px)
: width_(width),
height_(height),
border_px_(border_px),
tile_map_(extended_tile_map) {}
// --- Queries básicas ---
auto TileCollider::getTileAt(int tile_x, int tile_y) const -> Tile {
if (tile_x < 0 || tile_x >= width_ || tile_y < 0 || tile_y >= height_) {
return Tile::EMPTY;
}
int value = tile_map_[(tile_y * width_) + tile_x];
if (value >= 0 && value <= 5) {
return static_cast<Tile>(value);
}
return Tile::EMPTY;
}
auto TileCollider::isSolid(int tile_x, int tile_y) const -> bool {
return getTileAt(tile_x, tile_y) == Tile::WALL;
}
// Calcula la Y de la superficie de una slope en un pixel X concreto.
// Las slopes son de 45° y ocupan un tile de 8x8:
// SLOPE_L (\): alto a la izquierda, bajo a la derecha. surface = bottom - (7 - x_in_tile)
// SLOPE_R (/): alto a la derecha, bajo a la izquierda. surface = bottom - x_in_tile
auto TileCollider::getSlopeY(int tile_x, int tile_y, float px) const -> float {
float tile_bottom = toPixel(tile_y + 1) - 1;
float x_in_tile = px - toPixel(tile_x);
x_in_tile = std::clamp(x_in_tile, 0.0F, static_cast<float>(TS - 1));
auto tile = getTileAt(tile_x, tile_y);
if (tile == Tile::SLOPE_L) {
return tile_bottom - (static_cast<float>(TS - 1) - x_in_tile);
}
if (tile == Tile::SLOPE_R) {
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<int>(x) - 1);
int top_row = toTile(static_cast<int>(y));
int bot_row = toTile(static_cast<int>(y + h - 2));
for (int row = top_row; row <= bot_row; ++row) {
if (isSolid(col, row)) {
return toPixel(col + 1);
}
}
return Collision::NONE;
}
auto TileCollider::checkWallRight(float x, float y, float w, float h) const -> float {
int col = toTile(static_cast<int>(x + w));
int top_row = toTile(static_cast<int>(y));
int bot_row = toTile(static_cast<int>(y + h - 2));
for (int row = top_row; row <= bot_row; ++row) {
if (isSolid(col, row)) {
return toPixel(col);
}
}
return Collision::NONE;
}
// --- Colisión con techo ---
auto TileCollider::checkCeiling(float x, float y, float w) const -> float {
int top_row = toTile(static_cast<int>(y));
int left_col = toTile(static_cast<int>(x));
int right_col = toTile(static_cast<int>(x + w - 1));
for (int col = left_col; col <= right_col; ++col) {
auto tile = getTileAt(col, top_row);
// Slopes actúan como techo (no se atraviesan desde abajo)
if (tile == Tile::WALL || tile == Tile::SLOPE_L || tile == Tile::SLOPE_R) {
return toPixel(top_row + 1);
}
}
return Collision::NONE;
}
// --- Colisión con suelo (landing) ---
// Busca suelo entre foot_y_current y foot_y_new (rango de caída del frame).
// WALL: bloquea si los pies estaban por encima (como PASSABLE).
// PASSABLE: solo si los pies estaban por encima del borde superior del tile.
// SLOPE: siempre bloquea (las slopes son sólidas, como muros en diagonal).
// Calcula la y del suelo per a un tile concret. Retorna Collision::NONE si el tile
// no actua com a suelo en aquest cas (segons el tipus i el rang del moviment).
auto TileCollider::floorYForTile(Tile tile, int col, int row, float x, float w, float foot_y_current, float foot_y_new) const -> float {
if (tile == Tile::WALL || tile == Tile::PASSABLE) {
const float TILE_TOP = toPixel(row);
// WALL/PASSABLE només compten si els peus estaven per damunt abans del moviment
return (foot_y_current <= TILE_TOP) ? TILE_TOP : Collision::NONE;
}
if (tile == Tile::SLOPE_L || tile == Tile::SLOPE_R) {
const float CHECK_X = (tile == Tile::SLOPE_L) ? x : x + w - 1;
const float SLOPE_Y = getSlopeY(col, row, CHECK_X);
// Slopes són sòlides: aterrar sempre que els peus arriben a la superfície
return (foot_y_new >= SLOPE_Y) ? SLOPE_Y : Collision::NONE;
}
return Collision::NONE;
}
auto TileCollider::checkFloor(float x, float foot_y_current, float w, float foot_y_new) const -> FloorHit {
const int START_ROW = toTile(static_cast<int>(foot_y_current));
const int END_ROW = toTile(static_cast<int>(foot_y_new));
const int LEFT_COL = toTile(static_cast<int>(x));
const int RIGHT_COL = toTile(static_cast<int>(x + w - 1));
FloorHit best;
for (int row = START_ROW; row <= END_ROW; ++row) {
for (int col = LEFT_COL; col <= RIGHT_COL; ++col) {
const auto TILE = getTileAt(col, row);
const float FLOOR_Y = floorYForTile(TILE, col, row, x, w, foot_y_current, foot_y_new);
if (FLOOR_Y == Collision::NONE) { continue; }
if (best.y == Collision::NONE || FLOOR_Y < best.y) {
best = {.y = FLOOR_Y, .type = TILE, .tile_x = col, .tile_y = 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<int>(foot_y));
int left_col = toTile(static_cast<int>(x));
int right_col = toTile(static_cast<int>(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;
}
// --- Comprueba si el jugador está parcialmente dentro de algún slope ---
// Devuelve true si algún pie del jugador está POR DEBAJO de la superficie de algún
// tile de slope que solape. Esto indica que el jugador está "dentro" de una slope
// (por ejemplo, tras hacer drop-through o al saltar desde abajo).
// Cuando esto ocurre, checkFloor bloquea el aterrizaje en slopes para evitar que
// el jugador se quede pegado encima de una slope que está atravesando.
auto TileCollider::isInsideAnySlope(float x, float foot_y, float w) const -> bool {
int foot_row = toTile(static_cast<int>(foot_y));
int left_col = toTile(static_cast<int>(x));
int right_col = toTile(static_cast<int>(x + w - 1));
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 || 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 > slope_y) {
return true;
}
}
}
}
return false;
}
// --- Detección de slope debajo (transición ground→slope) ---
// Busca una slope directamente debajo del jugador (para transición ground→slope).
// Escanea la fila de los pies Y la fila superior: las slopes en escalera siempre
// tienen el tile de entrada una fila arriba del suelo desde el que se accede.
auto TileCollider::checkSlopeBelow(float x, float foot_y, float w) const -> SlopeInfo {
int foot_row = toTile(static_cast<int>(foot_y));
int left_col = toTile(static_cast<int>(x));
int right_col = toTile(static_cast<int>(x + w - 1));
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) {
// SLOPE_L (\): alta a la izquierda → siempre pie izquierdo
float slope_y = getSlopeY(col, row, x);
if (slope_y <= foot_y && slope_y >= foot_y - TS) {
return {.on_slope = true, .type = Tile::SLOPE_L, .tile_x = col, .tile_y = row, .surface_y = slope_y};
}
}
if (tile == Tile::SLOPE_R) {
// SLOPE_R (/): alta a la derecha → siempre pie derecho
float slope_y = getSlopeY(col, row, x + w - 1);
if (slope_y <= foot_y && slope_y >= foot_y - TS) {
return {.on_slope = true, .type = Tile::SLOPE_R, .tile_x = col, .tile_y = row, .surface_y = slope_y};
}
}
}
}
return {};
}
// --- Detección de kill tiles ---
// Devuelve true si el rectángulo del jugador solapa algún tile KILL.
auto TileCollider::touchesKillTile(float x, float y, float w, float h) const -> bool {
int top_row = toTile(static_cast<int>(y));
int bot_row = toTile(static_cast<int>(y + h - 1));
int left_col = toTile(static_cast<int>(x));
int right_col = toTile(static_cast<int>(x + w - 1));
for (int row = top_row; row <= bot_row; ++row) {
for (int col = left_col; col <= right_col; ++col) {
if (getTileAt(col, row) == Tile::KILL) {
return true;
}
}
}
return false;
}