arreglos en updateColliderPoints()
This commit is contained in:
@@ -339,16 +339,49 @@ void Player::setColor(Uint8 color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza los puntos de colisión
|
// Actualiza los puntos de colisión
|
||||||
|
// Sistema dinámico que adapta el número de puntos al tamaño del sprite
|
||||||
|
// Para sprites más grandes que los tiles (8px), genera puntos adicionales
|
||||||
|
// evitando que se atraviesen tiles al haber huecos entre colliders
|
||||||
void Player::updateColliderPoints() {
|
void Player::updateColliderPoints() {
|
||||||
const SDL_FRect RECT = getRect();
|
const SDL_FRect RECT = getRect();
|
||||||
collider_points_[0] = {.x = RECT.x, .y = RECT.y};
|
|
||||||
collider_points_[1] = {.x = RECT.x + 7, .y = RECT.y};
|
// Calcular cuántos puntos necesitamos por cada dimensión
|
||||||
collider_points_[2] = {.x = RECT.x + 7, .y = RECT.y + 7};
|
// Regla: un punto cada ~7px (menos que Tile::SIZE=8px) para garantizar detección
|
||||||
collider_points_[3] = {.x = RECT.x, .y = RECT.y + 7};
|
// Como mínimo 2 puntos por borde (esquinas)
|
||||||
collider_points_[4] = {.x = RECT.x, .y = RECT.y + 8};
|
constexpr int MAX_SPACING = 7; // Máxima distancia entre puntos (menor que tile size de 8px)
|
||||||
collider_points_[5] = {.x = RECT.x + 7, .y = RECT.y + 8};
|
|
||||||
collider_points_[6] = {.x = RECT.x + 7, .y = RECT.y + 15};
|
const int points_per_width = std::max(2, (WIDTH / MAX_SPACING) + 1);
|
||||||
collider_points_[7] = {.x = RECT.x, .y = RECT.y + 15};
|
const int points_per_height = std::max(2, (HEIGHT / MAX_SPACING) + 1);
|
||||||
|
|
||||||
|
// Limpiar y reservar espacio para los nuevos puntos
|
||||||
|
collider_points_.clear();
|
||||||
|
collider_points_.reserve(2 * points_per_width + 2 * points_per_height - 4); // Perímetro sin duplicar esquinas
|
||||||
|
|
||||||
|
// Generar puntos distribuidos uniformemente por el perímetro del rectángulo
|
||||||
|
|
||||||
|
// Borde superior (izquierda a derecha)
|
||||||
|
for (int i = 0; i < points_per_width; ++i) {
|
||||||
|
const float offset = (WIDTH - 1) * static_cast<float>(i) / static_cast<float>(points_per_width - 1);
|
||||||
|
collider_points_.push_back({.x = RECT.x + offset, .y = RECT.y});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Borde derecho (arriba a abajo, sin repetir esquina superior derecha)
|
||||||
|
for (int i = 1; i < points_per_height; ++i) {
|
||||||
|
const float offset = (HEIGHT - 1) * static_cast<float>(i) / static_cast<float>(points_per_height - 1);
|
||||||
|
collider_points_.push_back({.x = RECT.x + WIDTH - 1, .y = RECT.y + offset});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Borde inferior (derecha a izquierda, sin repetir esquina inferior derecha)
|
||||||
|
for (int i = 1; i < points_per_width; ++i) {
|
||||||
|
const float offset = (WIDTH - 1) * static_cast<float>(points_per_width - 1 - i) / static_cast<float>(points_per_width - 1);
|
||||||
|
collider_points_.push_back({.x = RECT.x + offset, .y = RECT.y + HEIGHT - 1});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Borde izquierdo (abajo a arriba, sin repetir esquinas)
|
||||||
|
for (int i = 1; i < points_per_height - 1; ++i) {
|
||||||
|
const float offset = (HEIGHT - 1) * static_cast<float>(points_per_height - 1 - i) / static_cast<float>(points_per_height - 1);
|
||||||
|
collider_points_.push_back({.x = RECT.x, .y = RECT.y + offset});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza los puntos de los pies
|
// Actualiza los puntos de los pies
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include <memory> // Para shared_ptr, __shared_ptr_access
|
#include <memory> // Para shared_ptr, __shared_ptr_access
|
||||||
#include <string> // Para string
|
#include <string> // Para string
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <vector> // Para vector
|
||||||
|
|
||||||
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
|
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
|
||||||
#include "game/gameplay/room.hpp"
|
#include "game/gameplay/room.hpp"
|
||||||
@@ -64,6 +65,7 @@ class Player {
|
|||||||
//[[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_ || (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
|
[[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 setPaused(bool value) { is_paused_ = value; } // Pone el jugador en modo pausa
|
||||||
|
auto handleKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
// --- Funciones de debug ---
|
// --- Funciones de debug ---
|
||||||
@@ -107,10 +109,10 @@ class Player {
|
|||||||
State previous_state_ = State::ON_GROUND; // Estado previo en el que se encontraba el jugador
|
State previous_state_ = State::ON_GROUND; // Estado previo en el que se encontraba el jugador
|
||||||
|
|
||||||
// --- Variables de colisión ---
|
// --- Variables de colisión ---
|
||||||
SDL_FRect collider_box_{}; // Caja de colisión con los enemigos u objetos
|
SDL_FRect collider_box_{}; // Caja de colisión con los enemigos u objetos
|
||||||
std::array<SDL_FPoint, 8> collider_points_{}; // Puntos de colisión con el mapa
|
std::vector<SDL_FPoint> collider_points_{}; // Puntos de colisión con el mapa (dinámico, se adapta al tamaño del sprite)
|
||||||
SDL_FPoint under_left_foot_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior izquierda del jugador
|
SDL_FPoint under_left_foot_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior izquierda del jugador
|
||||||
SDL_FPoint under_right_foot_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior derecha del jugador
|
SDL_FPoint under_right_foot_ = {0.0F, 0.0F}; // El punto bajo la esquina inferior derecha del jugador
|
||||||
|
|
||||||
// --- Variables de juego ---
|
// --- Variables de juego ---
|
||||||
bool is_alive_ = true; // Indica si el jugador esta vivo o no
|
bool is_alive_ = true; // Indica si el jugador esta vivo o no
|
||||||
@@ -167,7 +169,6 @@ class Player {
|
|||||||
// --- Funciones de finalización ---
|
// --- Funciones de finalización ---
|
||||||
void animate(float delta_time); // Establece la animación del jugador
|
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 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 aceleración
|
void updateVelocity(float delta_time); // Calcula la velocidad en x con aceleración
|
||||||
void markAsDead(); // Marca al jugador como muerto
|
void markAsDead(); // Marca al jugador como muerto
|
||||||
};
|
};
|
||||||
@@ -175,6 +175,7 @@ void Game::updatePlaying(float delta_time) {
|
|||||||
handlePlayerIsOnBorder();
|
handlePlayerIsOnBorder();
|
||||||
handlePlayerAndItemsCollisions();
|
handlePlayerAndItemsCollisions();
|
||||||
handlePlayerAndEnemiesCollisions();
|
handlePlayerAndEnemiesCollisions();
|
||||||
|
player_->handleKillingTiles();
|
||||||
handleIfPlayerIsAlive();
|
handleIfPlayerIsAlive();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user