forked from jaildesigner-jailgames/jaildoctors_dilemma
style: corregides les capçaleres de game/entities
This commit is contained in:
@@ -13,77 +13,93 @@
|
||||
#include "utils/utils.hpp" // Para Color
|
||||
struct JA_Sound_t; // lines 13-13
|
||||
|
||||
enum class PlayerState {
|
||||
STANDING,
|
||||
JUMPING,
|
||||
FALLING,
|
||||
};
|
||||
|
||||
struct PlayerSpawn {
|
||||
float x;
|
||||
float y;
|
||||
float vx;
|
||||
float vy;
|
||||
int jump_init_pos;
|
||||
PlayerState state;
|
||||
SDL_FlipMode flip;
|
||||
|
||||
// Constructor por defecto
|
||||
PlayerSpawn()
|
||||
: x(0),
|
||||
y(0),
|
||||
vx(0),
|
||||
vy(0),
|
||||
jump_init_pos(0),
|
||||
state(PlayerState::STANDING),
|
||||
flip(SDL_FLIP_NONE) {}
|
||||
|
||||
// Constructor
|
||||
PlayerSpawn(float x, float y, float vx, float vy, int jump_init_pos, PlayerState state, SDL_FlipMode flip)
|
||||
: x(x),
|
||||
y(y),
|
||||
vx(vx),
|
||||
vy(vy),
|
||||
jump_init_pos(jump_init_pos),
|
||||
state(state),
|
||||
flip(flip) {}
|
||||
};
|
||||
|
||||
struct PlayerData {
|
||||
PlayerSpawn spawn;
|
||||
std::string texture_path;
|
||||
std::string animations_path;
|
||||
std::shared_ptr<Room> room;
|
||||
|
||||
// Constructor
|
||||
PlayerData(PlayerSpawn spawn, std::string texture_path, std::string animations_path, std::shared_ptr<Room> room)
|
||||
: spawn(spawn),
|
||||
texture_path(std::move(std::move(texture_path))),
|
||||
animations_path(std::move(std::move(animations_path))),
|
||||
room(std::move(std::move(room))) {}
|
||||
};
|
||||
|
||||
class Player {
|
||||
public:
|
||||
// --- Enums y Structs ---
|
||||
enum class State {
|
||||
STANDING,
|
||||
JUMPING,
|
||||
FALLING,
|
||||
};
|
||||
|
||||
struct SpawnData {
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
float vx = 0;
|
||||
float vy = 0;
|
||||
int jump_init_pos = 0;
|
||||
State state = State::STANDING;
|
||||
SDL_FlipMode flip = SDL_FLIP_NONE;
|
||||
|
||||
// Constructor por defecto
|
||||
SpawnData() = default;
|
||||
|
||||
// Constructor con parámetros
|
||||
SpawnData(float x, float y, float vx, float vy, int jump_init_pos, State state, SDL_FlipMode flip)
|
||||
: x(x),
|
||||
y(y),
|
||||
vx(vx),
|
||||
vy(vy),
|
||||
jump_init_pos(jump_init_pos),
|
||||
state(state),
|
||||
flip(flip) {}
|
||||
};
|
||||
|
||||
struct Data {
|
||||
SpawnData spawn_data{};
|
||||
std::string texture_path{};
|
||||
std::string animations_path{};
|
||||
std::shared_ptr<Room> room = nullptr;
|
||||
|
||||
// Constructor por defecto
|
||||
Data() = default;
|
||||
|
||||
// Constructor con parámetros
|
||||
Data(SpawnData spawn_data, std::string texture_path, std::string animations_path, std::shared_ptr<Room> room)
|
||||
: spawn_data(std::move(spawn_data)),
|
||||
texture_path(std::move(texture_path)),
|
||||
animations_path(std::move(animations_path)),
|
||||
room(std::move(room)) {}
|
||||
};
|
||||
|
||||
// --- Constructor y Destructor ---
|
||||
explicit Player(const Data& player);
|
||||
~Player() = default;
|
||||
|
||||
// --- Funciones ---
|
||||
void render(); // Pinta el enemigo en pantalla
|
||||
void update(float delta_time); // Actualiza las variables del objeto
|
||||
[[nodiscard]] auto getOnBorder() const -> bool { return is_on_border_; } // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
|
||||
[[nodiscard]] auto getBorder() const -> RoomBorder { return border_; } // Indica en cual de los cuatro bordes se encuentra
|
||||
void switchBorders(); // Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
|
||||
auto getRect() -> SDL_FRect { return {x_, y_, WIDTH, HEIGHT}; } // Obtiene el rectangulo que delimita al jugador
|
||||
auto getCollider() -> SDL_FRect& { return collider_box_; } // Obtiene el rectangulo de colision del jugador
|
||||
auto getSpawnParams() -> SpawnData { return {x_, y_, vx_, vy_, jump_init_pos_, state_, sprite_->getFlip()}; } // Obtiene el estado de reaparición del jugador
|
||||
void setColor(); // Establece el color del jugador
|
||||
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); } // Establece la habitación en la que se encuentra el jugador
|
||||
[[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
|
||||
|
||||
private:
|
||||
// Constantes
|
||||
static constexpr int WIDTH = 8; // Ancho del jugador
|
||||
static constexpr int HEIGHT = 16; // ALto del jugador
|
||||
static constexpr int MAX_FALLING_HEIGHT = BLOCK * 4; // Altura maxima permitida de caída en pixels
|
||||
// --- Constantes ---
|
||||
static constexpr int WIDTH = 8; // Ancho del jugador
|
||||
static constexpr int HEIGHT = 16; // ALto del jugador
|
||||
static constexpr int MAX_FALLING_HEIGHT = BLOCK * 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 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_INTERVAL = 0.06F; // Intervalo entre sonidos de salto/caída en segundos (4 frames a 66.67fps)
|
||||
// --- Constantes de sonido ---
|
||||
static constexpr float SOUND_INTERVAL = 0.06F; // Intervalo entre sonidos de salto/caída en segundos (4 frames a 66.67fps)
|
||||
|
||||
// Objetos y punteros
|
||||
// --- --- Objetos y punteros --- ---
|
||||
std::shared_ptr<Room> room_; // Objeto encargado de gestionar cada habitación del juego
|
||||
std::shared_ptr<SurfaceAnimatedSprite> sprite_; // Sprite del jugador
|
||||
|
||||
// Variables
|
||||
// --- Variables ---
|
||||
float x_; // Posición del jugador en el eje X
|
||||
float y_; // Posición del jugador en el eje Y
|
||||
float vx_; // Velocidad/desplazamiento del jugador en el eje X
|
||||
@@ -93,8 +109,8 @@ class Player {
|
||||
std::vector<SDL_FPoint> collider_points_; // Puntos de colisión con el mapa
|
||||
std::vector<SDL_FPoint> under_feet_; // Contiene los puntos que hay bajo cada pie del jugador
|
||||
std::vector<SDL_FPoint> feet_; // Contiene los puntos que hay en el pie del jugador
|
||||
PlayerState state_; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
|
||||
PlayerState previous_state_; // Estado previo en el que se encontraba el jugador
|
||||
State state_; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
|
||||
State previous_state_; // Estado previo en el que se encontraba el jugador
|
||||
bool is_on_border_ = false; // Indica si el jugador esta en uno de los cuatro bordes de la pantalla
|
||||
bool is_alive_ = true; // Indica si el jugador esta vivo o no
|
||||
bool is_paused_ = false; // Indica si el jugador esta en modo pausa
|
||||
@@ -107,130 +123,40 @@ class Player {
|
||||
float jumping_time_ = 0.0F; // Tiempo acumulado de salto en segundos
|
||||
float falling_time_ = 0.0F; // Tiempo acumulado de caída en segundos
|
||||
|
||||
// --- Funciones ---
|
||||
void checkInput(float delta_time); // Comprueba las entradas y modifica variables
|
||||
void checkBorders(); // Comprueba si se halla en alguno de los cuatro bordes
|
||||
void checkState(float delta_time); // Comprueba el estado del jugador
|
||||
void applyGravity(float delta_time); // Aplica gravedad al jugador
|
||||
void move(float delta_time); // Recalcula la posición del jugador y su animación
|
||||
void moveHorizontalLeft(float delta_time); // Maneja el movimiento horizontal hacia la izquierda
|
||||
void moveHorizontalRight(float delta_time); // Maneja el movimiento horizontal hacia la derecha
|
||||
void moveVerticalUp(float delta_time); // Maneja el movimiento vertical hacia arriba
|
||||
void moveVerticalDown(float delta_time); // Maneja el movimiento vertical hacia abajo
|
||||
void animate(float delta_time); // Establece la animación del jugador
|
||||
void checkJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||
void playJumpSound(); // Calcula y reproduce el sonido de salto
|
||||
void playFallSound(); // Calcula y reproduce el sonido de caer
|
||||
auto isOnFloor() -> bool; // Comprueba si el jugador tiene suelo debajo de los pies
|
||||
auto isOnAutoSurface() -> bool; // Comprueba si el jugador esta sobre una superficie automática
|
||||
auto isOnDownSlope() -> bool; // Comprueba si el jugador está sobre una rampa hacia abajo
|
||||
auto checkKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
|
||||
void updateColliderPoints(); // Actualiza los puntos de colisión
|
||||
void updateFeet(); // Actualiza los puntos de los pies
|
||||
void setState(State value); // Cambia el estado del jugador
|
||||
void initSounds(); // Inicializa los sonidos de salto y caida
|
||||
void placeSprite() { sprite_->setPos(x_, y_); } // Coloca el sprite en la posición del jugador
|
||||
void applySpawnValues(const SpawnData& spawn); // Aplica los valores de spawn al jugador
|
||||
void initSprite(const std::string& surface_path, const std::string& animations_path); // Inicializa el sprite del jugador
|
||||
|
||||
#ifdef _DEBUG
|
||||
// --- Variables ---
|
||||
SDL_FRect debug_rect_x_; // Rectangulo de desplazamiento para el modo debug
|
||||
SDL_FRect debug_rect_y_; // Rectangulo de desplazamiento para el modo debug
|
||||
Uint8 debug_color_; // Color del recuadro de debug del jugador
|
||||
SDL_FPoint debug_point_; // Punto para debug
|
||||
|
||||
// --- Funciones ---
|
||||
void renderDebugInfo(); // Pinta la información de debug del jugador
|
||||
#endif
|
||||
|
||||
// Comprueba las entradas y modifica variables
|
||||
void checkInput(float delta_time);
|
||||
|
||||
// Comprueba si se halla en alguno de los cuatro bordes
|
||||
void checkBorders();
|
||||
|
||||
// Comprueba el estado del jugador
|
||||
void checkState(float delta_time);
|
||||
|
||||
// Aplica gravedad al jugador
|
||||
void applyGravity(float delta_time);
|
||||
|
||||
// Recalcula la posición del jugador y su animación
|
||||
void move(float delta_time);
|
||||
|
||||
// Maneja el movimiento horizontal hacia la izquierda
|
||||
void moveHorizontalLeft(float delta_time);
|
||||
|
||||
// Maneja el movimiento horizontal hacia la derecha
|
||||
void moveHorizontalRight(float delta_time);
|
||||
|
||||
// Maneja el movimiento vertical hacia arriba
|
||||
void moveVerticalUp(float delta_time);
|
||||
|
||||
// Maneja el movimiento vertical hacia abajo
|
||||
void moveVerticalDown(float delta_time);
|
||||
|
||||
// Establece la animación del jugador
|
||||
void animate(float delta_time);
|
||||
|
||||
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||
void checkJumpEnd();
|
||||
|
||||
// Calcula y reproduce el sonido de salto
|
||||
void playJumpSound();
|
||||
|
||||
// Calcula y reproduce el sonido de caer
|
||||
void playFallSound();
|
||||
|
||||
// Comprueba si el jugador tiene suelo debajo de los pies
|
||||
auto isOnFloor() -> bool;
|
||||
|
||||
// Comprueba si el jugador esta sobre una superficie automática
|
||||
auto isOnAutoSurface() -> bool;
|
||||
|
||||
// Comprueba si el jugador está sobre una rampa hacia abajo
|
||||
auto isOnDownSlope() -> bool;
|
||||
|
||||
// Comprueba que el jugador no toque ningun tile de los que matan
|
||||
auto checkKillingTiles() -> bool;
|
||||
|
||||
// Actualiza los puntos de colisión
|
||||
void updateColliderPoints();
|
||||
|
||||
// Actualiza los puntos de los pies
|
||||
void updateFeet();
|
||||
|
||||
// Cambia el estado del jugador
|
||||
void setState(PlayerState value);
|
||||
|
||||
// Inicializa los sonidos de salto y caida
|
||||
void initSounds();
|
||||
|
||||
// Coloca el sprite en la posición del jugador
|
||||
void placeSprite() { sprite_->setPos(x_, y_); }
|
||||
|
||||
// Aplica los valores de spawn al jugador
|
||||
void applySpawnValues(const PlayerSpawn& spawn);
|
||||
|
||||
// Inicializa el sprite del jugador
|
||||
void initSprite(const std::string& surface_path, const std::string& animations_path);
|
||||
|
||||
#ifdef _DEBUG
|
||||
// Pinta la información de debug del jugador
|
||||
void renderDebugInfo();
|
||||
#endif
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
explicit Player(const PlayerData& player);
|
||||
|
||||
// Destructor
|
||||
~Player() = default;
|
||||
|
||||
// Pinta el enemigo en pantalla
|
||||
void render();
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void update(float delta_time);
|
||||
|
||||
// Indica si el jugador esta en uno de los cuatro bordes de la pantalla
|
||||
[[nodiscard]] auto getOnBorder() const -> bool { return is_on_border_; }
|
||||
|
||||
// Indica en cual de los cuatro bordes se encuentra
|
||||
[[nodiscard]] auto getBorder() const -> RoomBorder { return border_; }
|
||||
|
||||
// Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
|
||||
void switchBorders();
|
||||
|
||||
// Obtiene el rectangulo que delimita al jugador
|
||||
auto getRect() -> SDL_FRect { return {x_, y_, WIDTH, HEIGHT}; }
|
||||
|
||||
// Obtiene el rectangulo de colision del jugador
|
||||
auto getCollider() -> SDL_FRect& { return collider_box_; }
|
||||
|
||||
// Obtiene el estado de reaparición del jugador
|
||||
auto getSpawnParams() -> PlayerSpawn { return {x_, y_, vx_, vy_, jump_init_pos_, state_, sprite_->getFlip()}; }
|
||||
|
||||
// Establece el color del jugador
|
||||
void setColor();
|
||||
|
||||
// Establece la habitación en la que se encuentra el jugador
|
||||
void setRoom(std::shared_ptr<Room> room) { room_ = std::move(room); }
|
||||
|
||||
// Comprueba si el jugador esta vivo
|
||||
[[nodiscard]] auto isAlive() const -> bool { return is_alive_; }
|
||||
|
||||
// Pone el jugador en modo pausa
|
||||
void setPaused(bool value) { is_paused_ = value; }
|
||||
};
|
||||
Reference in New Issue
Block a user