implementada la logica de so (time based) en Player per a imitar la anterior frame based (amb els seus fallos)

This commit is contained in:
2025-11-07 23:04:02 +01:00
parent 7d0e0e0d18
commit 667ab73fc6
3 changed files with 167 additions and 61 deletions

View File

@@ -3,10 +3,10 @@
#include <SDL3/SDL.h>
#include <array> // Para array
#include <limits> // Para numeric_limits
#include <memory> // Para shared_ptr, __shared_ptr_access
#include <string> // Para string
#include <utility>
#include <vector> // Para vector
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
#include "game/gameplay/room.hpp"
@@ -29,6 +29,12 @@ class Player {
STAY
};
// --- Constantes de física (públicas para permitir cálculos en structs) ---
static constexpr float HORIZONTAL_VELOCITY = 40.0F; // Velocidad horizontal en pixels/segundo (0.6 * 66.67fps)
static constexpr float MAX_VY = 80.0F; // Velocidad vertical máxima en pixels/segundo (1.2 * 66.67fps)
static constexpr float JUMP_VELOCITY = -80.0F; // Velocidad inicial del salto en pixels/segundo
static constexpr float GRAVITY_FORCE = 155.6F; // Fuerza de gravedad en pixels/segundo² (0.035 * 66.67²)
struct SpawnData {
float x = 0;
float y = 0;
@@ -67,6 +73,37 @@ class Player {
room(std::move(room)) {}
};
struct JumpSoundController {
// Duración del salto calculada automáticamente con física: t = 2 * v0 / g
static constexpr float JUMP_DURATION = (2.0F * MAX_VY) / GRAVITY_FORCE;
static constexpr size_t FIRST_SOUND = 1; // Primer sonido a reproducir (índice 1)
static constexpr size_t LAST_SOUND = 17; // Último sonido a reproducir (índice 17)
static constexpr float SECONDS_PER_SOUND = JUMP_DURATION / (LAST_SOUND - FIRST_SOUND + 1);
size_t current_index_ = 0; // Índice del sonido actual
float elapsed_time_ = 0.0F; // Tiempo transcurrido durante el salto
bool active_ = false; // Indica si el controlador está activo
void start(); // Inicia el controlador
void reset(); // Resetea el controlador
bool shouldPlay(float delta_time, size_t& out_index); // Comprueba si debe reproducir un sonido
};
struct FallSoundController {
static constexpr float PIXELS_PER_SOUND = 5.0F; // Intervalo de píxeles por sonido (configurable)
static constexpr size_t FIRST_SOUND = 1; // Primer sonido a reproducir (índice 1)
static constexpr size_t LAST_SOUND = 13; // Último sonido a reproducir (índice 13)
size_t current_index_ = 0; // Índice del sonido actual
float distance_traveled_ = 0.0F; // Distancia acumulada durante la caída
float last_y_ = 0.0F; // Última posición Y registrada
bool active_ = false; // Indica si el controlador está activo
void start(float start_y); // Inicia el controlador
void reset(); // Resetea el controlador
bool shouldPlay(float delta_time, float current_y, size_t& out_index); // Comprueba si debe reproducir un sonido
};
// --- Constructor y Destructor ---
explicit Player(const Data& player);
~Player() = default;
@@ -91,16 +128,7 @@ class Player {
static constexpr int HEIGHT = 16; // ALto del jugador
static constexpr int MAX_FALLING_HEIGHT = TILE_SIZE * 4; // Altura maxima permitida de caída en pixels
// --- Constantes de física (per-second values) ---
static constexpr float HORIZONTAL_VELOCITY = 40.0F; // Velocidad horizontal en pixels/segundo (0.6 * 66.67fps)
static constexpr float MAX_VY = 80.0F; // Velocidad vertical máxima en pixels/segundo (1.2 * 66.67fps)
static constexpr float JUMP_VELOCITY = -80.0F; // Velocidad inicial del salto en pixels/segundo
static constexpr float GRAVITY_FORCE = 155.6F; // Fuerza de gravedad en pixels/segundo² (0.035 * 66.67²)
// --- Constantes de sonido ---
static constexpr float SOUND_DISTANCE_INTERVAL = 3.0F; // Distancia en píxeles entre cada sonido de salto/caída
// --- --- Objetos y punteros --- ---
// --- Objetos y punteros ---
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
std::unique_ptr<SurfaceAnimatedSprite> sprite_; // Sprite del jugador
@@ -133,15 +161,18 @@ class Player {
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 ---
Uint8 color_ = 0; // Color del jugador
std::vector<JA_Sound_t*> jumping_sound_; // Vecor con todos los sonidos del salto
std::vector<JA_Sound_t*> falling_sound_; // Vecor con todos los sonidos de la caída
Uint8 color_ = 0; // Color del jugador
std::array<JA_Sound_t*, 24> jumping_sound_; // Array con todos los sonidos del salto
std::array<JA_Sound_t*, 14> falling_sound_; // Array con todos los sonidos de la caída
JumpSoundController jump_sound_ctrl_; // Controlador de sonidos de salto
FallSoundController fall_sound_ctrl_; // Controlador de sonidos de caída
int fall_start_position_ = 0; // Posición Y al iniciar la caída
void handleHorizontalMovement(float delta_time);
void handleVerticalMovement(float delta_time);
void handleConveyorBelts();
void handleShouldFall();
void updateState();
void updateState(float delta_time);
void moveAndCollide(float delta_time);
// --- Funciones de inicialización ---
@@ -182,8 +213,8 @@ class Player {
void handleBorders(); // Comprueba si se halla en alguno de los cuatro bordes
void handleJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
auto handleKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
void playJumpSound(); // Calcula y reproduce el sonido de salto
void playFallSound(); // Calcula y reproduce el sonido de caer
void playJumpSound(float delta_time); // Calcula y reproduce el sonido de salto
void playFallSound(float delta_time); // Calcula y reproduce el sonido de caer
void handleDeathByFalling(); // Gestiona la muerte al caer desde muy alto
void updateVelocity(); // Calcula la velocidad en x
};