collision tile
This commit is contained in:
43
CLAUDE.md
43
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
|
## 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.
|
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()`.
|
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** — 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.
|
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** — al cambiar de habitación, en lugar de warpear al jugador del borde opuesto, mostrar una animación/transición de cambio de pantalla.
|
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.
|
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
|
## Estado del renombrado
|
||||||
@@ -49,6 +49,43 @@ Se han renombrado las referencias de `JailDoctor's Dilemma` → `Projecte 2026`
|
|||||||
### Otros
|
### Otros
|
||||||
- Añadidos `desktop.ini` y `Thumbs.db` al `.gitignore`.
|
- 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<int> 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)
|
## Overview (legacy)
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ set(APP_SOURCES
|
|||||||
# Game - Gameplay
|
# Game - Gameplay
|
||||||
source/game/gameplay/cheevos.cpp
|
source/game/gameplay/cheevos.cpp
|
||||||
source/game/gameplay/collision_map.cpp
|
source/game/gameplay/collision_map.cpp
|
||||||
|
source/game/gameplay/tile_collider.cpp
|
||||||
source/game/gameplay/enemy_manager.cpp
|
source/game/gameplay/enemy_manager.cpp
|
||||||
source/game/gameplay/item_manager.cpp
|
source/game/gameplay/item_manager.cpp
|
||||||
source/game/gameplay/item_tracker.cpp
|
source/game/gameplay/item_tracker.cpp
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 396 B After Width: | Height: | Size: 407 B |
File diff suppressed because it is too large
Load Diff
@@ -2,26 +2,24 @@
|
|||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
#include <array> // Para array
|
#include <memory> // Para shared_ptr
|
||||||
#include <limits> // Para numeric_limits
|
|
||||||
#include <memory> // Para shared_ptr, __shared_ptr_access
|
|
||||||
#include <string> // Para string
|
#include <string> // Para string
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#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/gameplay/room.hpp"
|
||||||
#include "game/options.hpp" // Para Cheat, Options, options
|
#include "game/gameplay/tile_collider.hpp" // Para TileCollider::Tile
|
||||||
#include "utils/defines.hpp" // Para BORDER_TOP, BLOCK
|
#include "game/options.hpp" // Para Cheat, Options
|
||||||
#include "utils/utils.hpp" // Para Color
|
#include "utils/defines.hpp" // Para PlayArea, Tile, Flip
|
||||||
struct JA_Sound_t; // lines 13-13
|
struct JA_Sound_t;
|
||||||
|
|
||||||
class Player {
|
class Player {
|
||||||
public:
|
public:
|
||||||
// --- Enums y Structs ---
|
// --- Enums y Structs ---
|
||||||
enum class State {
|
enum class State {
|
||||||
ON_GROUND, // En suelo plano o conveyor belt
|
ON_GROUND,
|
||||||
ON_SLOPE, // En rampa/pendiente
|
ON_SLOPE,
|
||||||
ON_AIR, // En el aire (saltando, cayendo o caminando al vacío)
|
ON_AIR,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class Direction {
|
enum class Direction {
|
||||||
@@ -32,13 +30,13 @@ class Player {
|
|||||||
NONE
|
NONE
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Constantes de física (públicas para permitir cálculos en structs) ---
|
// --- Constantes de física ---
|
||||||
static constexpr float HORIZONTAL_VELOCITY = 60.0F; // Velocidad horizontal objetivo en pixels/segundo
|
static constexpr float HORIZONTAL_VELOCITY = 60.0F;
|
||||||
static constexpr float HORIZONTAL_ACCEL = 500.0F; // Aceleración/deceleración horizontal en pixels/segundo² (inercia ligera)
|
static constexpr float HORIZONTAL_ACCEL = 500.0F;
|
||||||
static constexpr float MAX_VY = 160.0F; // Velocidad vertical máxima en pixels/segundo
|
static constexpr float MAX_VY = 160.0F;
|
||||||
static constexpr float JUMP_VELOCITY = -178.5F; // Velocidad inicial del salto en pixels/segundo
|
static constexpr float JUMP_VELOCITY = -178.5F;
|
||||||
static constexpr float GRAVITY_FORCE = 360.0F; // Fuerza de gravedad en pixels/segundo²
|
static constexpr float GRAVITY_FORCE = 360.0F;
|
||||||
static constexpr float LOW_JUMP_GRAVITY_MULT = 3.0F; // Multiplicador de gravedad al soltar el botón de salto (salto variable)
|
static constexpr float LOW_JUMP_GRAVITY_MULT = 3.0F;
|
||||||
|
|
||||||
struct SpawnData {
|
struct SpawnData {
|
||||||
float x = 0;
|
float x = 0;
|
||||||
@@ -60,130 +58,98 @@ class Player {
|
|||||||
explicit Player(const Data& player);
|
explicit Player(const Data& player);
|
||||||
~Player() = default;
|
~Player() = default;
|
||||||
|
|
||||||
// --- Funciones ---
|
// --- Interfaz pública ---
|
||||||
void render(); // Pinta el enemigo en pantalla
|
void render();
|
||||||
void update(float delta_time); // Actualiza las variables del objeto
|
void update(float delta_time);
|
||||||
[[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 isOnBorder() const -> bool { return border_ != Room::Border::NONE; }
|
||||||
[[nodiscard]] auto getBorder() const -> Room::Border { return border_; } // Indica en cual de los cuatro bordes se encuentra
|
[[nodiscard]] auto getBorder() const -> Room::Border { return border_; }
|
||||||
void switchBorders(); // Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
|
void switchBorders();
|
||||||
auto getRect() -> SDL_FRect { return {.x = x_, .y = y_, .w = WIDTH, .h = HEIGHT}; } // Obtiene el rectangulo que delimita al jugador
|
auto getRect() -> SDL_FRect { return {.x = x_, .y = y_, .w = WIDTH, .h = HEIGHT}; }
|
||||||
auto getCollider() -> SDL_FRect& { return collider_box_; } // Obtiene el rectangulo de colision del jugador
|
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()}; } // Obtiene el estado de reaparición 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()}; }
|
||||||
void setColor(Uint8 color = 0); // Establece el color del jugador (0 = automático según cheats)
|
void setColor(Uint8 color = 0);
|
||||||
void setSkin(const std::string& skin_name); // Cambia la skin del jugador en caliente ("default" o nombre de enemigo)
|
void setSkin(const std::string& skin_name);
|
||||||
static auto skinToAnimationPath(const std::string& skin_name) -> std::string; // Resuelve nombre de skin a fichero de animación
|
static auto skinToAnimationPath(const std::string& skin_name) -> std::string;
|
||||||
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); } // Establece la habitación en la que se encuentra el jugador
|
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); }
|
||||||
//[[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_; }
|
||||||
[[nodiscard]] auto isAlive() const -> bool { return is_alive_; } // Comprueba si el jugador esta vivo
|
void setPaused(bool value) { is_paused_ = value; }
|
||||||
void setPaused(bool value) { is_paused_ = value; } // Pone el jugador en modo pausa
|
void setIgnoreInput(bool value) { ignore_input_ = value; }
|
||||||
void setIgnoreInput(bool value) { ignore_input_ = value; } // Ignora inputs del jugador (física sigue activa)
|
|
||||||
[[nodiscard]] auto getIgnoreInput() const -> bool { return ignore_input_; }
|
[[nodiscard]] auto getIgnoreInput() const -> bool { return ignore_input_; }
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
// --- Funciones de debug ---
|
void setDebugPosition(float x, float y);
|
||||||
void setDebugPosition(float x, float y); // Establece la posición del jugador directamente (debug)
|
void finalizeDebugTeleport();
|
||||||
void finalizeDebugTeleport(); // Fija estado ON_GROUND, velocidades a 0, actualiza last_grounded_position_ (debug)
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// --- Constantes ---
|
// --- Constantes ---
|
||||||
static constexpr int WIDTH = 12; // Ancho del jugador
|
static constexpr int WIDTH = 12;
|
||||||
static constexpr int HEIGHT = 24; // Alto del jugador
|
static constexpr int HEIGHT = 24;
|
||||||
static constexpr int MAX_FALLING_HEIGHT = Tile::SIZE * 4; // Altura maxima permitida de caída en pixels
|
|
||||||
|
|
||||||
// --- Objetos y punteros ---
|
// --- Objetos y punteros ---
|
||||||
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
|
std::shared_ptr<Room> room_;
|
||||||
std::unique_ptr<AnimatedSprite> sprite_; // Sprite del jugador
|
std::unique_ptr<AnimatedSprite> sprite_;
|
||||||
|
|
||||||
// --- Variables de posición y física ---
|
// --- Posición y física ---
|
||||||
float x_ = 0.0F; // Posición del jugador en el eje X
|
float x_ = 0.0F;
|
||||||
float y_ = 0.0F; // Posición del jugador en el eje Y
|
float y_ = 0.0F;
|
||||||
float y_prev_ = 0.0F; // Posición Y del frame anterior (para detectar hitos de distancia en sonidos)
|
float vx_ = 0.0F;
|
||||||
float vx_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje X
|
float vy_ = 0.0F;
|
||||||
float vy_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje Y
|
|
||||||
|
|
||||||
Direction wanna_go_ = Direction::NONE;
|
Direction wanna_go_ = Direction::NONE;
|
||||||
bool wanna_jump_ = false;
|
bool wanna_jump_ = false;
|
||||||
bool wanna_down_ = 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 ---
|
// --- Estado ---
|
||||||
State state_ = State::ON_GROUND; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
|
State state_ = State::ON_GROUND;
|
||||||
State previous_state_ = State::ON_GROUND; // Estado previo en el que se encontraba el jugador
|
State previous_state_ = State::ON_GROUND;
|
||||||
|
|
||||||
// --- Variables de colisión ---
|
// --- Colisión ---
|
||||||
SDL_FRect collider_box_{}; // Caja de colisión con los enemigos u objetos
|
SDL_FRect collider_box_{};
|
||||||
std::array<SDL_FPoint, 12> collider_points_{}; // Puntos de colisión con el mapa (3 columnas × 4 filas, gap ≤ 8px)
|
int slope_tile_x_{0};
|
||||||
SDL_FPoint under_left_foot_ = {.x = 0.0F, .y = 0.0F}; // El punto bajo la esquina inferior izquierda del jugador
|
int slope_tile_y_{0};
|
||||||
SDL_FPoint under_right_foot_ = {.x = 0.0F, .y = 0.0F}; // El punto bajo la esquina inferior derecha del jugador
|
TileCollider::Tile slope_type_{TileCollider::Tile::EMPTY};
|
||||||
const LineDiagonal* current_slope_{nullptr}; // Rampa actual sobe la que está el jugador
|
|
||||||
|
|
||||||
// --- Variables de juego ---
|
// --- Variables de juego ---
|
||||||
bool is_alive_ = true; // Indica si el jugador esta vivo o no
|
bool is_alive_ = true;
|
||||||
bool is_paused_ = false; // Indica si el jugador esta en modo pausa
|
bool is_paused_ = false;
|
||||||
bool ignore_input_ = false; // Ignora inputs pero mantiene la física activa
|
bool ignore_input_ = false;
|
||||||
bool auto_movement_ = false; // Indica si esta siendo arrastrado por una superficie automatica
|
Room::Border border_ = Room::Border::TOP;
|
||||||
Room::Border border_ = Room::Border::TOP; // Indica en cual de los cuatro bordes se encuentra
|
int last_grounded_position_ = 0;
|
||||||
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)
|
|
||||||
|
|
||||||
// --- Variables de renderizado y sonido ---
|
// --- Renderizado y sonido ---
|
||||||
Uint8 color_ = 0; // Color del jugador
|
Uint8 color_ = 0;
|
||||||
JA_Sound_t* jump_sound_ = nullptr; // Sonido al iniciar el salto
|
JA_Sound_t* jump_sound_ = nullptr;
|
||||||
JA_Sound_t* land_sound_ = nullptr; // Sonido al aterrizar en el suelo
|
JA_Sound_t* land_sound_ = nullptr;
|
||||||
|
|
||||||
void handleConveyorBelts();
|
// --- Pipeline de update ---
|
||||||
void handleShouldFall();
|
void handleInput();
|
||||||
void updateState(float delta_time);
|
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 ---
|
// --- Gestión de estado ---
|
||||||
void updateOnGround(float delta_time); // Actualización lógica estado ON_GROUND
|
void transitionToState(State state);
|
||||||
void updateOnSlope(float delta_time); // Actualización lógica estado ON_SLOPE
|
void startJump();
|
||||||
void updateOnAir(float delta_time); // Actualización lógica estado ON_AIR
|
|
||||||
|
|
||||||
// --- Métodos de movimiento por estado ---
|
// --- Geometría y renderizado ---
|
||||||
void moveOnGround(float delta_time); // Movimiento físico estado ON_GROUND
|
void syncSpriteAndCollider();
|
||||||
void moveOnSlope(float delta_time); // Movimiento físico estado ON_SLOPE
|
void placeSprite();
|
||||||
void moveOnAir(float delta_time); // Movimiento físico estado ON_AIR
|
void animate(float delta_time);
|
||||||
|
auto handleBorders() -> Room::Border;
|
||||||
|
|
||||||
// --- Funciones de inicialización ---
|
// --- Inicialización ---
|
||||||
void initSprite(const std::string& animations_path); // Inicializa el sprite del jugador
|
void initSprite(const std::string& animations_path);
|
||||||
void initSounds(); // Inicializa los sonidos de salto y caida
|
void initSounds();
|
||||||
void applySpawnValues(const SpawnData& spawn); // Aplica los valores de spawn al jugador
|
void applySpawnValues(const SpawnData& spawn);
|
||||||
|
|
||||||
// --- Funciones de procesamiento de entrada ---
|
// --- Utilidad ---
|
||||||
void handleInput(); // Comprueba las entradas y modifica variables
|
void markAsDead();
|
||||||
|
|
||||||
// --- 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
|
|
||||||
};
|
};
|
||||||
@@ -10,7 +10,8 @@
|
|||||||
// Constructor
|
// Constructor
|
||||||
CollisionMap::CollisionMap(std::vector<int> collision_tile_map, int conveyor_belt_direction)
|
CollisionMap::CollisionMap(std::vector<int> collision_tile_map, int conveyor_belt_direction)
|
||||||
: collision_tile_map_(std::move(collision_tile_map)),
|
: 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
|
// Inicializa todas las superficies de colisión
|
||||||
initializeSurfaces();
|
initializeSurfaces();
|
||||||
}
|
}
|
||||||
@@ -41,13 +42,20 @@ auto CollisionMap::getTile(int index) const -> Tile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (collision_tile_map_[index]) {
|
switch (collision_tile_map_[index]) {
|
||||||
case 1: return Tile::WALL;
|
case 1:
|
||||||
case 2: return Tile::PASSABLE;
|
return Tile::WALL;
|
||||||
case 3: return Tile::SLOPE_L;
|
case 2:
|
||||||
case 4: return Tile::SLOPE_R;
|
return Tile::PASSABLE;
|
||||||
case 5: return Tile::KILL;
|
case 3:
|
||||||
case 6: return Tile::ANIMATED;
|
return Tile::SLOPE_L;
|
||||||
default: return Tile::EMPTY;
|
case 4:
|
||||||
|
return Tile::SLOPE_R;
|
||||||
|
case 5:
|
||||||
|
return Tile::KILL;
|
||||||
|
case 6:
|
||||||
|
return Tile::ANIMATED;
|
||||||
|
default:
|
||||||
|
return Tile::EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <vector> // Para vector
|
#include <vector> // Para vector
|
||||||
|
|
||||||
|
#include "game/gameplay/tile_collider.hpp" // Para TileCollider
|
||||||
#include "utils/defines.hpp" // Para Tile::SIZE, Map::WIDTH, Map::HEIGHT
|
#include "utils/defines.hpp" // Para Tile::SIZE, Map::WIDTH, Map::HEIGHT
|
||||||
#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical
|
#include "utils/utils.hpp" // Para LineHorizontal, LineDiagonal, LineVertical
|
||||||
|
|
||||||
@@ -71,6 +72,7 @@ class CollisionMap {
|
|||||||
|
|
||||||
// --- Getters ---
|
// --- Getters ---
|
||||||
[[nodiscard]] auto getConveyorBeltDirection() const -> int { return conveyor_belt_direction_; }
|
[[nodiscard]] auto getConveyorBeltDirection() const -> int { return conveyor_belt_direction_; }
|
||||||
|
[[nodiscard]] auto getTileCollider() const -> const TileCollider& { return tile_collider_; }
|
||||||
|
|
||||||
// Getters para debug visualization
|
// Getters para debug visualization
|
||||||
[[nodiscard]] auto getBottomFloors() const -> const std::vector<LineHorizontal>& { return bottom_floors_; }
|
[[nodiscard]] auto getBottomFloors() const -> const std::vector<LineHorizontal>& { return bottom_floors_; }
|
||||||
@@ -90,6 +92,7 @@ class CollisionMap {
|
|||||||
// --- Datos de la habitación ---
|
// --- Datos de la habitación ---
|
||||||
std::vector<int> collision_tile_map_; // Mapa de colisiones por tile
|
std::vector<int> collision_tile_map_; // Mapa de colisiones por tile
|
||||||
int conveyor_belt_direction_; // Dirección de conveyor belts
|
int conveyor_belt_direction_; // Dirección de conveyor belts
|
||||||
|
TileCollider tile_collider_; // Sistema de colisión por tiles
|
||||||
|
|
||||||
// --- Geometría de colisión ---
|
// --- Geometría de colisión ---
|
||||||
std::vector<LineHorizontal> bottom_floors_; // Superficies inferiores (suelos)
|
std::vector<LineHorizontal> bottom_floors_; // Superficies inferiores (suelos)
|
||||||
|
|||||||
@@ -161,6 +161,10 @@ void Room::setPaused(bool value) {
|
|||||||
item_manager_->setPaused(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
|
// 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)
|
auto Room::getRoom(Border border) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
||||||
switch (border) {
|
switch (border) {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ class Surface; // lines 13-13
|
|||||||
class EnemyManager;
|
class EnemyManager;
|
||||||
class ItemManager;
|
class ItemManager;
|
||||||
class CollisionMap;
|
class CollisionMap;
|
||||||
|
class TileCollider;
|
||||||
class TilemapRenderer;
|
class TilemapRenderer;
|
||||||
|
|
||||||
class Room {
|
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
|
[[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
|
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 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)
|
// 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
|
static auto loadYAML(const std::string& file_path, bool verbose = false) -> Data; // Carga habitación desde archivo YAML unificado
|
||||||
|
|||||||
182
source/game/gameplay/tile_collider.cpp
Normal file
182
source/game/gameplay/tile_collider.cpp
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
#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 {};
|
||||||
|
}
|
||||||
55
source/game/gameplay/tile_collider.hpp
Normal file
55
source/game/gameplay/tile_collider.hpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#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<int>& 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<int>& tile_map_;
|
||||||
|
|
||||||
|
[[nodiscard]] static auto toTile(int px) -> int { return px / TS; }
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user