Files
projecte_2026/source/game/gameplay/tile_collider.cpp
2026-04-06 21:51:40 +02:00

183 lines
6.3 KiB
C++

#include "game/gameplay/tile_collider.hpp"
#include <algorithm> // Para std::min, std::max
#include <cmath> // Para std::ceil
#include "utils/defines.hpp"
TileCollider::TileCollider(const std::vector<int>& 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<Tile>(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<float>((tile_y + 1) * TS - 1);
float x_in_tile = px - static_cast<float>(tile_x * TS);
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) {
// \ descendente de izquierda a derecha
return tile_bottom - (static_cast<float>(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<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 static_cast<float>((col + 1) * TS);
}
}
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 static_cast<float>(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<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) {
if (isSolid(col, top_row)) {
return static_cast<float>((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<int>(foot_y_current));
int end_row = toTile(static_cast<int>(foot_y_new));
int left_col = toTile(static_cast<int>(x));
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) {
auto tile = getTileAt(col, row);
float floor_y = Collision::NONE;
if (tile == Tile::WALL) {
floor_y = static_cast<float>(row * TS);
} else if (tile == Tile::PASSABLE) {
float tile_top = static_cast<float>(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<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;
}
// --- 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<int>(foot_y));
int left_col = toTile(static_cast<int>(x));
int right_col = toTile(static_cast<int>(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 {};
}