forked from jaildesigner-jailgames/jaildoctors_dilemma
tractant d'entendre el codi de Player i posar ordre
This commit is contained in:
@@ -46,7 +46,7 @@ constexpr int BORDER_HEIGHT = 24; // Alto del borde por defecto
|
|||||||
// AUDIO
|
// AUDIO
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
constexpr int AUDIO_VOLUME = 100; // Volumen por defecto
|
constexpr int AUDIO_VOLUME = 100; // Volumen por defecto
|
||||||
constexpr bool AUDIO_ENABLED = true; // Audio por defecto
|
constexpr bool AUDIO_ENABLED = false; // Audio por defecto
|
||||||
|
|
||||||
// MUSIC
|
// MUSIC
|
||||||
constexpr int MUSIC_VOLUME = 80; // Volumen por defecto de la musica
|
constexpr int MUSIC_VOLUME = 80; // Volumen por defecto de la musica
|
||||||
|
|||||||
@@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
#include <algorithm> // Para max, min
|
#include <algorithm> // Para max, min
|
||||||
#include <cmath> // Para ceil, abs
|
#include <cmath> // Para ceil, abs
|
||||||
#include <ranges> // Para std::ranges::any_of
|
#include <iostream>
|
||||||
|
#include <ranges> // Para std::ranges::any_of
|
||||||
|
|
||||||
#include "core/input/input.hpp" // Para Input, InputAction
|
#include "core/input/input.hpp" // Para Input, InputAction
|
||||||
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
|
#include "core/rendering/surface_animated_sprite.hpp" // Para SAnimatedSprite
|
||||||
#include "core/resources/resource.hpp" // Para Resource
|
#include "core/resources/resource.hpp" // Para Resource
|
||||||
#include "core/system/debug.hpp" // Para Debug
|
|
||||||
#include "external/jail_audio.h" // Para JA_PlaySound
|
#include "external/jail_audio.h" // Para JA_PlaySound
|
||||||
#include "game/gameplay/room.hpp" // Para Room, TileType
|
#include "game/gameplay/room.hpp" // Para Room, TileType
|
||||||
#include "game/options.hpp" // Para Cheat, Options, options
|
#include "game/options.hpp" // Para Cheat, Options, options
|
||||||
@@ -18,52 +18,37 @@
|
|||||||
Player::Player(const Data& player)
|
Player::Player(const Data& player)
|
||||||
: room_(player.room) {
|
: room_(player.room) {
|
||||||
// Inicializa algunas variables
|
// Inicializa algunas variables
|
||||||
initSprite(/*player.texture_path, */player.animations_path);
|
initSprite(player.animations_path);
|
||||||
setColor();
|
setColor();
|
||||||
applySpawnValues(player.spawn_data);
|
applySpawnValues(player.spawn_data);
|
||||||
placeSprite();
|
placeSprite();
|
||||||
initSounds();
|
initSounds();
|
||||||
|
|
||||||
previous_state_ = state_;
|
previous_state_ = state_;
|
||||||
last_position_ = getRect();
|
|
||||||
collider_box_ = getRect();
|
|
||||||
collider_points_.resize(collider_points_.size() + 8, {0, 0});
|
|
||||||
under_feet_.resize(under_feet_.size() + 2, {0, 0});
|
|
||||||
feet_.resize(feet_.size() + 2, {0, 0});
|
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_rect_x_ = {.x = 0, .y = 0, .w = 0, .h = 0};
|
|
||||||
debug_rect_y_ = {.x = 0, .y = 0, .w = 0, .h = 0};
|
|
||||||
debug_color_ = static_cast<Uint8>(PaletteColor::GREEN);
|
|
||||||
debug_point_ = {.x = 0, .y = 0};
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pinta el jugador en pantalla
|
// Pinta el jugador en pantalla
|
||||||
void Player::render() {
|
void Player::render() {
|
||||||
sprite_->render(1, color_);
|
sprite_->render(1, color_);
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
renderDebugInfo();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza las variables del objeto
|
// Actualiza las variables del objeto
|
||||||
void Player::update(float delta_time) {
|
void Player::update(float delta_time) {
|
||||||
if (!is_paused_) {
|
if (!is_paused_) {
|
||||||
checkInput(delta_time); // Comprueba las entradas y modifica variables
|
checkInput(); // Comprueba las entradas y establece las velocidades y el flip
|
||||||
move(delta_time); // Recalcula la posición del jugador
|
move(delta_time); // Recalcula la posición del jugador
|
||||||
animate(delta_time); // Establece la animación del jugador
|
animate(delta_time); // Establece la animación del jugador
|
||||||
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
updateState(delta_time); // Comprueba el estado del jugador
|
||||||
checkJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||||
checkKillingTiles(); // Comprueba que el jugador no toque ningun tile de los que matan}
|
checkJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||||
|
checkKillingTiles(); // Comprueba que el jugador no toque ningun tile de los que matan}
|
||||||
|
|
||||||
|
setColor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba las entradas y modifica variables
|
// Comprueba las entradas y establece las velocidades y el flip
|
||||||
void Player::checkInput(float delta_time) {
|
void Player::checkInput() {
|
||||||
(void)delta_time; // No usado en este método, pero mantenido para consistencia
|
|
||||||
|
|
||||||
// Solo comprueba las entradas de dirección cuando está sobre una superficie
|
// Solo comprueba las entradas de dirección cuando está sobre una superficie
|
||||||
if (state_ != State::STANDING) {
|
if (state_ != State::STANDING) {
|
||||||
return;
|
return;
|
||||||
@@ -74,42 +59,36 @@ void Player::checkInput(float delta_time) {
|
|||||||
if (Input::get()->checkInput(InputAction::LEFT)) {
|
if (Input::get()->checkInput(InputAction::LEFT)) {
|
||||||
vx_ = -HORIZONTAL_VELOCITY;
|
vx_ = -HORIZONTAL_VELOCITY;
|
||||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
||||||
|
left_or_right_is_pressed_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (Input::get()->checkInput(InputAction::RIGHT)) {
|
else if (Input::get()->checkInput(InputAction::RIGHT)) {
|
||||||
vx_ = HORIZONTAL_VELOCITY;
|
vx_ = HORIZONTAL_VELOCITY;
|
||||||
sprite_->setFlip(SDL_FLIP_NONE);
|
sprite_->setFlip(SDL_FLIP_NONE);
|
||||||
|
left_or_right_is_pressed_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
// No se pulsa ninguna dirección
|
// No se pulsa ninguna dirección
|
||||||
vx_ = 0.0F;
|
vx_ = 0.0F;
|
||||||
if (isOnAutoSurface()) {
|
left_or_right_is_pressed_ = false;
|
||||||
// Si deja de moverse sobre una superficie se engancha
|
|
||||||
auto_movement_ = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else { // El movimiento lo proporciona la superficie
|
} else {
|
||||||
|
// El movimiento lo proporciona la superficie
|
||||||
vx_ = HORIZONTAL_VELOCITY * room_->getAutoSurfaceDirection();
|
vx_ = HORIZONTAL_VELOCITY * room_->getAutoSurfaceDirection();
|
||||||
|
sprite_->setFlip(vx_ > 0.0F ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL);
|
||||||
if (vx_ > 0.0F) {
|
|
||||||
sprite_->setFlip(SDL_FLIP_NONE);
|
|
||||||
} else {
|
|
||||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input::get()->checkInput(InputAction::JUMP)) {
|
if (Input::get()->checkInput(InputAction::JUMP)) {
|
||||||
// Solo puede saltar si ademas de estar (state == s_standing)
|
/*
|
||||||
// Esta sobre el suelo, rampa o suelo que se mueve
|
Solo puede saltar si ademas de estar (State::STANDING)
|
||||||
// Esto es para evitar el salto desde el vacio al cambiar de pantalla verticalmente
|
Esta sobre el suelo, rampa o suelo que se mueve
|
||||||
// Ya que se coloca el estado s_standing al cambiar de pantalla
|
Esto es para evitar el salto desde el vacio al cambiar de pantalla verticalmente
|
||||||
|
Ya que se coloca el estado State::STANDING al cambiar de pantalla
|
||||||
|
*/
|
||||||
|
|
||||||
if (isOnFloor() || isOnAutoSurface()) {
|
if (isOnFloor() || isOnAutoSurface()) {
|
||||||
setState(State::JUMPING);
|
setState(State::JUMPING);
|
||||||
vy_ = JUMP_VELOCITY;
|
|
||||||
jump_init_pos_ = y_;
|
|
||||||
jumping_time_ = 0.0F;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,37 +121,42 @@ void Player::checkBorders() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba el estado del jugador
|
// Comprueba el estado del jugador
|
||||||
void Player::checkState(float delta_time) {
|
void Player::updateState(float delta_time) {
|
||||||
// Actualiza las variables en función del estado
|
// Actualiza las variables en función del estado
|
||||||
if (state_ == State::FALLING) {
|
switch (state_) {
|
||||||
vx_ = 0.0F;
|
case State::STANDING: {
|
||||||
vy_ = MAX_VY;
|
// Calcula la distancia de caída en pixels (velocidad * tiempo)
|
||||||
falling_time_ += delta_time;
|
const int FALLING_DISTANCE = static_cast<int>(y_) - last_grounded_position_;
|
||||||
playFallSound();
|
if (previous_state_ == State::FALLING && FALLING_DISTANCE > MAX_FALLING_HEIGHT) {
|
||||||
}
|
// Si cae de muy alto, el jugador muere
|
||||||
|
is_alive_ = false;
|
||||||
else if (state_ == State::STANDING) {
|
}
|
||||||
// Calcula la distancia de caída en pixels (velocidad * tiempo)
|
vy_ = 0.0F;
|
||||||
const float FALLING_DISTANCE = MAX_VY * falling_time_;
|
jumping_time_ = 0.0F;
|
||||||
if (previous_state_ == State::FALLING && FALLING_DISTANCE > MAX_FALLING_HEIGHT) { // Si cae de muy alto, el jugador muere
|
last_grounded_position_ = static_cast<int>(y_);
|
||||||
is_alive_ = false;
|
auto_movement_ = !isOnFloor() && isOnAutoSurface();
|
||||||
|
if (!isOnFloor() && !isOnAutoSurface() && !isOnDownSlope()) {
|
||||||
|
setState(State::FALLING);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
vy_ = 0.0F;
|
|
||||||
jumping_time_ = 0.0F;
|
case State::FALLING: {
|
||||||
falling_time_ = 0.0F;
|
|
||||||
if (!isOnFloor() && !isOnAutoSurface() && !isOnDownSlope()) {
|
|
||||||
setState(State::FALLING);
|
|
||||||
vx_ = 0.0F;
|
vx_ = 0.0F;
|
||||||
vy_ = MAX_VY;
|
vy_ = MAX_VY;
|
||||||
falling_time_ += delta_time;
|
|
||||||
playFallSound();
|
playFallSound();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
else if (state_ == State::JUMPING) {
|
case State::JUMPING: {
|
||||||
falling_time_ = 0.0F;
|
last_grounded_position_ = static_cast<int>(y_);
|
||||||
jumping_time_ += delta_time;
|
jumping_time_ += delta_time;
|
||||||
playJumpSound();
|
playJumpSound();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,7 +187,6 @@ void Player::switchBorders() {
|
|||||||
|
|
||||||
is_on_border_ = false;
|
is_on_border_ = false;
|
||||||
placeSprite();
|
placeSprite();
|
||||||
collider_box_ = getRect();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aplica gravedad al jugador
|
// Aplica gravedad al jugador
|
||||||
@@ -220,15 +203,11 @@ void Player::applyGravity(float delta_time) {
|
|||||||
void Player::moveHorizontalLeft(float delta_time) {
|
void Player::moveHorizontalLeft(float delta_time) {
|
||||||
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
||||||
const float DISPLACEMENT = vx_ * delta_time;
|
const float DISPLACEMENT = vx_ * delta_time;
|
||||||
SDL_FRect proj;
|
SDL_FRect proj = {
|
||||||
proj.x = static_cast<int>(x_ + DISPLACEMENT);
|
.x = x_ + DISPLACEMENT,
|
||||||
proj.y = static_cast<int>(y_);
|
.y = y_,
|
||||||
proj.h = HEIGHT;
|
.w = std::ceil(std::fabs(DISPLACEMENT)), // Para evitar que tenga un ancho de 0 pixels
|
||||||
proj.w = static_cast<int>(std::ceil(std::fabs(DISPLACEMENT))); // Para evitar que tenga un ancho de 0 pixels
|
.h = HEIGHT};
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_rect_x_ = proj;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Comprueba la colisión con las superficies
|
// Comprueba la colisión con las superficies
|
||||||
const int POS = room_->checkRightSurfaces(&proj);
|
const int POS = room_->checkRightSurfaces(&proj);
|
||||||
@@ -240,20 +219,25 @@ void Player::moveHorizontalLeft(float delta_time) {
|
|||||||
} else {
|
} else {
|
||||||
// Si hay colisión lo mueve hasta donde no colisiona
|
// Si hay colisión lo mueve hasta donde no colisiona
|
||||||
x_ = POS + 1;
|
x_ = POS + 1;
|
||||||
|
std::cout << "LEFT\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
|
// Si ha tocado alguna rampa mientras camina (sin saltar)
|
||||||
if (state_ != State::JUMPING) {
|
if (state_ == State::STANDING) {
|
||||||
const LineVertical LEFT_SIDE = {.x = static_cast<int>(x_), .y1 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, .y2 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
|
// Si está bajando la rampa, recoloca al jugador
|
||||||
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
|
if (isOnDownSlope() && state_ == State::STANDING) {
|
||||||
if (LY > -1) {
|
y_ += 1;
|
||||||
y_ = LY - HEIGHT;
|
std::cout << "RAMP DOWN LEFT\n";
|
||||||
|
}
|
||||||
|
// Asciende
|
||||||
|
else {
|
||||||
|
const LineVertical LEFT_SIDE = {.x = static_cast<int>(x_), .y1 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, .y2 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
|
||||||
|
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
|
||||||
|
if (LY > -1) {
|
||||||
|
y_ = LY - HEIGHT;
|
||||||
|
std::cout << "RAMP UP LEFT\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Si está bajando la rampa, recoloca al jugador
|
|
||||||
if (isOnDownSlope() && state_ != State::JUMPING) {
|
|
||||||
y_ += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,15 +245,11 @@ void Player::moveHorizontalLeft(float delta_time) {
|
|||||||
void Player::moveHorizontalRight(float delta_time) {
|
void Player::moveHorizontalRight(float delta_time) {
|
||||||
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
||||||
const float DISPLACEMENT = vx_ * delta_time;
|
const float DISPLACEMENT = vx_ * delta_time;
|
||||||
SDL_FRect proj;
|
SDL_FRect proj = {
|
||||||
proj.x = x_ + WIDTH;
|
.x = x_ + WIDTH,
|
||||||
proj.y = y_;
|
.y = y_,
|
||||||
proj.h = HEIGHT;
|
.w = std::ceil(DISPLACEMENT), // Para evitar que tenga un ancho de 0 pixels
|
||||||
proj.w = std::ceil(DISPLACEMENT); // Para evitar que tenga un ancho de 0 pixels
|
.h = HEIGHT};
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_rect_x_ = proj;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Comprueba la colisión
|
// Comprueba la colisión
|
||||||
const int POS = room_->checkLeftSurfaces(&proj);
|
const int POS = room_->checkLeftSurfaces(&proj);
|
||||||
@@ -281,20 +261,25 @@ void Player::moveHorizontalRight(float delta_time) {
|
|||||||
} else {
|
} else {
|
||||||
// Si hay colisión lo mueve hasta donde no colisiona
|
// Si hay colisión lo mueve hasta donde no colisiona
|
||||||
x_ = POS - WIDTH;
|
x_ = POS - WIDTH;
|
||||||
|
std::cout << "RIGHT\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
|
// Si ha tocado alguna rampa mientras camina (sin saltar)
|
||||||
if (state_ != State::JUMPING) {
|
if (state_ == State::STANDING) {
|
||||||
const LineVertical RIGHT_SIDE = {.x = static_cast<int>(x_) + static_cast<int>(WIDTH) - 1, .y1 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, .y2 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
|
// Si está bajando la rampa, recoloca al jugador
|
||||||
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
|
if (isOnDownSlope() && state_ == State::STANDING) {
|
||||||
if (RY > -1) {
|
y_ += 1;
|
||||||
y_ = RY - HEIGHT;
|
std::cout << "RAMP DOWN RIGHT\n";
|
||||||
|
}
|
||||||
|
// Asciende
|
||||||
|
else {
|
||||||
|
const LineVertical RIGHT_SIDE = {.x = static_cast<int>(x_) + static_cast<int>(WIDTH) - 1, .y1 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 2, .y2 = static_cast<int>(y_) + static_cast<int>(HEIGHT) - 1}; // Comprueba solo los dos pixels de abajo
|
||||||
|
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
|
||||||
|
if (RY > -1) {
|
||||||
|
y_ = RY - HEIGHT;
|
||||||
|
std::cout << "RAMP UP RIGHT\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Si está bajando la rampa, recoloca al jugador
|
|
||||||
if (isOnDownSlope() && state_ != State::JUMPING) {
|
|
||||||
y_ += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -302,15 +287,12 @@ void Player::moveHorizontalRight(float delta_time) {
|
|||||||
void Player::moveVerticalUp(float delta_time) {
|
void Player::moveVerticalUp(float delta_time) {
|
||||||
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
||||||
const float DISPLACEMENT = vy_ * delta_time;
|
const float DISPLACEMENT = vy_ * delta_time;
|
||||||
SDL_FRect proj;
|
SDL_FRect proj = {
|
||||||
proj.x = static_cast<int>(x_);
|
.x = x_,
|
||||||
proj.y = static_cast<int>(y_ + DISPLACEMENT);
|
.y = y_ + DISPLACEMENT,
|
||||||
proj.h = static_cast<int>(std::ceil(std::fabs(DISPLACEMENT))); // Para evitar que tenga una altura de 0 pixels
|
.w = WIDTH,
|
||||||
proj.w = WIDTH;
|
.h = std::ceil(std::fabs(DISPLACEMENT)) // Para evitar que tenga una altura de 0 pixels
|
||||||
|
};
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_rect_y_ = proj;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Comprueba la colisión
|
// Comprueba la colisión
|
||||||
const int POS = room_->checkBottomSurfaces(&proj);
|
const int POS = room_->checkBottomSurfaces(&proj);
|
||||||
@@ -322,6 +304,7 @@ void Player::moveVerticalUp(float delta_time) {
|
|||||||
} else {
|
} else {
|
||||||
// Si hay colisión lo mueve hasta donde no colisiona y entra en caída
|
// Si hay colisión lo mueve hasta donde no colisiona y entra en caída
|
||||||
y_ = POS + 1;
|
y_ = POS + 1;
|
||||||
|
std::cout << "TOP\n";
|
||||||
setState(State::FALLING);
|
setState(State::FALLING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -330,48 +313,39 @@ void Player::moveVerticalUp(float delta_time) {
|
|||||||
void Player::moveVerticalDown(float delta_time) {
|
void Player::moveVerticalDown(float delta_time) {
|
||||||
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
||||||
const float DISPLACEMENT = vy_ * delta_time;
|
const float DISPLACEMENT = vy_ * delta_time;
|
||||||
SDL_FRect proj;
|
SDL_FRect proj = {
|
||||||
proj.x = x_;
|
.x = x_,
|
||||||
proj.y = y_ + HEIGHT;
|
.y = y_ + HEIGHT,
|
||||||
proj.h = std::ceil(DISPLACEMENT); // Para evitar que tenga una altura de 0 pixels
|
.w = WIDTH,
|
||||||
proj.w = WIDTH;
|
.h = std::ceil(DISPLACEMENT) // Para evitar que tenga una altura de 0 pixels
|
||||||
|
};
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_rect_y_ = proj;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Comprueba la colisión con las superficies normales y las automáticas
|
// Comprueba la colisión con las superficies normales y las automáticas
|
||||||
const float POS = std::max(room_->checkTopSurfaces(&proj), room_->checkAutoSurfaces(&proj));
|
const float POS = std::max(room_->checkTopSurfaces(&proj), room_->checkAutoSurfaces(&proj));
|
||||||
if (POS > -1) {
|
if (POS > -1) {
|
||||||
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
|
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
|
||||||
y_ = POS - HEIGHT;
|
y_ = POS - HEIGHT;
|
||||||
|
std::cout << "BOTTOM\n";
|
||||||
setState(State::STANDING);
|
setState(State::STANDING);
|
||||||
|
|
||||||
// Deja de estar enganchado a la superficie automatica
|
|
||||||
auto_movement_ = false;
|
|
||||||
} else {
|
} else {
|
||||||
// Si no hay colisión con los muros, comprueba la colisión con las rampas
|
// Si no hay colisión con los muros, comprueba la colisión con las rampas
|
||||||
if (state_ != State::JUMPING) { // Las rampas no se miran si se está saltando
|
if (state_ != State::JUMPING) { // Las rampas no se miran si se está saltando
|
||||||
auto rect = toSDLRect(proj);
|
auto frect = getRect();
|
||||||
|
auto rect = toSDLRect(frect);
|
||||||
const LineVertical LEFT_SIDE = {.x = rect.x, .y1 = rect.y, .y2 = rect.y + rect.h - 1};
|
const LineVertical LEFT_SIDE = {.x = rect.x, .y1 = rect.y, .y2 = rect.y + rect.h - 1};
|
||||||
const LineVertical RIGHT_SIDE = {.x = rect.x + rect.w - 1, .y1 = rect.y, .y2 = rect.y + rect.h - 1};
|
const LineVertical RIGHT_SIDE = {.x = rect.x + rect.w - 1, .y1 = rect.y, .y2 = rect.y + rect.h - 1};
|
||||||
|
std::cout << RIGHT_SIDE.x << " " << RIGHT_SIDE.y1 << " " << RIGHT_SIDE.y2 << "\n";
|
||||||
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
|
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
|
||||||
if (POINT > -1) {
|
if (POINT > -1) {
|
||||||
// No está saltando y hay colisión con una rampa
|
// No está saltando y hay colisión con una rampa
|
||||||
// Calcula la nueva posición
|
// Calcula la nueva posición
|
||||||
y_ = POINT - HEIGHT;
|
y_ = POINT - HEIGHT;
|
||||||
|
std::cout << "BOTTOM SLOPE\n";
|
||||||
setState(State::STANDING);
|
setState(State::STANDING);
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_color_ = static_cast<Uint8>(PaletteColor::YELLOW);
|
|
||||||
debug_point_ = {.x = x_ + (WIDTH / 2), .y = POINT};
|
|
||||||
#endif
|
|
||||||
} else {
|
} else {
|
||||||
// No está saltando y no hay colisón con una rampa
|
// No está saltando y no hay colisón con una rampa
|
||||||
// Calcula la nueva posición
|
// Calcula la nueva posición
|
||||||
y_ += DISPLACEMENT;
|
y_ += DISPLACEMENT;
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_color_ = static_cast<Uint8>(PaletteColor::RED);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Esta saltando y no hay colisión con los muros
|
// Esta saltando y no hay colisión con los muros
|
||||||
@@ -383,13 +357,7 @@ void Player::moveVerticalDown(float delta_time) {
|
|||||||
|
|
||||||
// Recalcula la posición del jugador y su animación
|
// Recalcula la posición del jugador y su animación
|
||||||
void Player::move(float delta_time) {
|
void Player::move(float delta_time) {
|
||||||
last_position_ = {.x = x_, .y = y_}; // Guarda la posicion actual antes de modificarla
|
applyGravity(delta_time); // Aplica gravedad al jugador
|
||||||
applyGravity(delta_time); // Aplica gravedad al jugador
|
|
||||||
checkState(delta_time); // Comprueba el estado del jugador
|
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
debug_color_ = static_cast<Uint8>(PaletteColor::GREEN);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Movimiento horizontal
|
// Movimiento horizontal
|
||||||
if (vx_ < 0.0F) {
|
if (vx_ < 0.0F) {
|
||||||
@@ -398,17 +366,6 @@ void Player::move(float delta_time) {
|
|||||||
moveHorizontalRight(delta_time);
|
moveHorizontalRight(delta_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ha salido del suelo, el jugador cae
|
|
||||||
if (state_ == State::STANDING && !isOnFloor()) {
|
|
||||||
setState(State::FALLING);
|
|
||||||
auto_movement_ = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Si ha salido de una superficie automatica, detiene el movimiento automatico
|
|
||||||
if (state_ == State::STANDING && isOnFloor() && !isOnAutoSurface()) {
|
|
||||||
auto_movement_ = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Movimiento vertical
|
// Movimiento vertical
|
||||||
if (vy_ < 0.0F) {
|
if (vy_ < 0.0F) {
|
||||||
moveVerticalUp(delta_time);
|
moveVerticalUp(delta_time);
|
||||||
@@ -416,13 +373,7 @@ void Player::move(float delta_time) {
|
|||||||
moveVerticalDown(delta_time);
|
moveVerticalDown(delta_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
placeSprite(); // Coloca el sprite en la nueva posición
|
placeSprite(); // Coloca el sprite en la nueva posición
|
||||||
collider_box_ = getRect(); // Actualiza el rectangulo de colisión
|
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
Debug::get()->add("RECT_X: " + std::to_string(debug_rect_x_.x) + "," + std::to_string(debug_rect_x_.y) + "," + std::to_string(debug_rect_x_.w) + "," + std::to_string(debug_rect_x_.h));
|
|
||||||
Debug::get()->add("RECT_Y: " + std::to_string(debug_rect_y_.x) + "," + std::to_string(debug_rect_y_.y) + "," + std::to_string(debug_rect_y_.w) + "," + std::to_string(debug_rect_y_.h));
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Establece la animación del jugador
|
// Establece la animación del jugador
|
||||||
@@ -434,15 +385,9 @@ void Player::animate(float delta_time) {
|
|||||||
|
|
||||||
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||||
void Player::checkJumpEnd() {
|
void Player::checkJumpEnd() {
|
||||||
if (state_ == State::JUMPING) {
|
if (state_ == State::JUMPING && vy_ > 0.0F && static_cast<int>(y_) >= jump_init_pos_) {
|
||||||
if (vy_ > 0) {
|
// Si alcanza la altura de salto inicial, pasa al estado de caída
|
||||||
if (y_ >= jump_init_pos_) {
|
setState(State::FALLING);
|
||||||
// Si alcanza la altura de salto inicial, pasa al estado de caída
|
|
||||||
setState(State::FALLING);
|
|
||||||
vy_ = MAX_VY;
|
|
||||||
jumping_time_ = 0.0F;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -454,14 +399,14 @@ void Player::playJumpSound() {
|
|||||||
// Solo reproduce el sonido cuando cambia de índice
|
// Solo reproduce el sonido cuando cambia de índice
|
||||||
if (SOUND_INDEX != PREVIOUS_INDEX && SOUND_INDEX < static_cast<int>(jumping_sound_.size())) {
|
if (SOUND_INDEX != PREVIOUS_INDEX && SOUND_INDEX < static_cast<int>(jumping_sound_.size())) {
|
||||||
JA_PlaySound(jumping_sound_[SOUND_INDEX]);
|
JA_PlaySound(jumping_sound_[SOUND_INDEX]);
|
||||||
#ifdef _DEBUG
|
|
||||||
Debug::get()->add("JUMP: " + std::to_string(SOUND_INDEX));
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcula y reproduce el sonido de caer
|
// Calcula y reproduce el sonido de caer
|
||||||
void Player::playFallSound() {
|
void Player::playFallSound() {
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
const int SOUND_INDEX = static_cast<int>(falling_time_ / SOUND_INTERVAL);
|
const int SOUND_INDEX = static_cast<int>(falling_time_ / SOUND_INTERVAL);
|
||||||
const int PREVIOUS_INDEX = static_cast<int>((falling_time_ - SOUND_INTERVAL) / SOUND_INTERVAL);
|
const int PREVIOUS_INDEX = static_cast<int>((falling_time_ - SOUND_INTERVAL) / SOUND_INTERVAL);
|
||||||
|
|
||||||
@@ -473,6 +418,7 @@ void Player::playFallSound() {
|
|||||||
Debug::get()->add("FALL: " + std::to_string(CLAMPED_INDEX));
|
Debug::get()->add("FALL: " + std::to_string(CLAMPED_INDEX));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba si el jugador tiene suelo debajo de los pies
|
// Comprueba si el jugador tiene suelo debajo de los pies
|
||||||
@@ -490,23 +436,9 @@ auto Player::isOnFloor() -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba las rampas
|
// Comprueba las rampas
|
||||||
on_slope_l = room_->checkLeftSlopes(under_feet_.data());
|
on_slope_l = room_->checkLeftSlopes(&under_feet_[0]);
|
||||||
on_slope_r = room_->checkRightSlopes(&under_feet_[1]);
|
on_slope_r = room_->checkRightSlopes(&under_feet_[1]);
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
if (on_floor) {
|
|
||||||
Debug::get()->add("ON_FLOOR");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (on_slope_l) {
|
|
||||||
Debug::get()->add("ON_SLOPE_L: " + std::to_string(under_feet_[0].x) + "," + std::to_string(under_feet_[0].y));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (on_slope_r) {
|
|
||||||
Debug::get()->add("ON_SLOPE_R: " + std::to_string(under_feet_[1].x) + "," + std::to_string(under_feet_[1].y));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return on_floor || on_slope_l || on_slope_r;
|
return on_floor || on_slope_l || on_slope_r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -521,12 +453,6 @@ auto Player::isOnAutoSurface() -> bool {
|
|||||||
on_auto_surface |= room_->checkAutoSurfaces(&f);
|
on_auto_surface |= room_->checkAutoSurfaces(&f);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
if (on_auto_surface) {
|
|
||||||
Debug::get()->add("ON_AUTO_SURFACE");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return on_auto_surface;
|
return on_auto_surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -538,27 +464,20 @@ auto Player::isOnDownSlope() -> bool {
|
|||||||
|
|
||||||
// Cuando el jugador baja una escalera, se queda volando
|
// Cuando el jugador baja una escalera, se queda volando
|
||||||
// Hay que mirar otro pixel más por debajo
|
// Hay que mirar otro pixel más por debajo
|
||||||
under_feet_[0].y += 1;
|
SDL_FPoint foot0 = under_feet_[0];
|
||||||
under_feet_[1].y += 1;
|
SDL_FPoint foot1 = under_feet_[1];
|
||||||
|
foot0.y += 1.0f;
|
||||||
|
foot1.y += 1.0f;
|
||||||
|
|
||||||
// Comprueba las rampas
|
// Comprueba las rampas
|
||||||
on_slope |= room_->checkLeftSlopes(under_feet_.data());
|
on_slope |= room_->checkLeftSlopes(&foot0);
|
||||||
on_slope |= room_->checkRightSlopes(&under_feet_[1]);
|
on_slope |= room_->checkRightSlopes(&foot1);
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
if (on_slope) {
|
|
||||||
Debug::get()->add("ON_DOWN_SLOPE");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return on_slope;
|
return on_slope;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba que el jugador no toque ningun tile de los que matan
|
// Comprueba que el jugador no toque ningun tile de los que matan
|
||||||
auto Player::checkKillingTiles() -> bool {
|
auto Player::checkKillingTiles() -> bool {
|
||||||
// Actualiza los puntos de colisión
|
|
||||||
updateColliderPoints();
|
|
||||||
|
|
||||||
// Comprueba si hay contacto con algún tile que mata
|
// Comprueba si hay contacto con algún tile que mata
|
||||||
if (std::ranges::any_of(collider_points_, [this](const auto& c) {
|
if (std::ranges::any_of(collider_points_, [this](const auto& c) {
|
||||||
return room_->getTile(c) == Room::Tile::KILL;
|
return room_->getTile(c) == Room::Tile::KILL;
|
||||||
@@ -572,6 +491,7 @@ auto Player::checkKillingTiles() -> bool {
|
|||||||
|
|
||||||
// Establece el color del jugador
|
// Establece el color del jugador
|
||||||
void Player::setColor() {
|
void Player::setColor() {
|
||||||
|
/*
|
||||||
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
|
if (Options::cheats.invincible == Options::Cheat::State::ENABLED) {
|
||||||
color_ = static_cast<Uint8>(PaletteColor::CYAN);
|
color_ = static_cast<Uint8>(PaletteColor::CYAN);
|
||||||
} else if (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED) {
|
} else if (Options::cheats.infinite_lives == Options::Cheat::State::ENABLED) {
|
||||||
@@ -579,6 +499,24 @@ void Player::setColor() {
|
|||||||
} else {
|
} else {
|
||||||
color_ = static_cast<Uint8>(PaletteColor::WHITE);
|
color_ = static_cast<Uint8>(PaletteColor::WHITE);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
switch (state_) {
|
||||||
|
case State::STANDING:
|
||||||
|
color_ = static_cast<Uint8>(PaletteColor::YELLOW);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::JUMPING:
|
||||||
|
color_ = static_cast<Uint8>(PaletteColor::GREEN);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::FALLING:
|
||||||
|
color_ = static_cast<Uint8>(PaletteColor::RED);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza los puntos de colisión
|
// Actualiza los puntos de colisión
|
||||||
@@ -610,8 +548,39 @@ void Player::setState(State value) {
|
|||||||
previous_state_ = state_;
|
previous_state_ = state_;
|
||||||
state_ = value;
|
state_ = value;
|
||||||
|
|
||||||
|
switch (state_) {
|
||||||
|
case State::STANDING:
|
||||||
|
vy_ = 0.0F;
|
||||||
|
last_grounded_position_ = static_cast<int>(y_);
|
||||||
|
std::cout << "SET STANDING\n";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::JUMPING:
|
||||||
|
last_grounded_position_ = static_cast<int>(y_);
|
||||||
|
vy_ = JUMP_VELOCITY;
|
||||||
|
jump_init_pos_ = y_;
|
||||||
|
jumping_time_ = 0.0F;
|
||||||
|
std::cout << "SET JUMPING\n";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case State::FALLING:
|
||||||
|
vx_ = 0.0F;
|
||||||
|
vy_ = MAX_VY;
|
||||||
|
playFallSound();
|
||||||
|
if (previous_state_ == State::STANDING) {
|
||||||
|
last_grounded_position_ = static_cast<int>(y_);
|
||||||
|
}
|
||||||
|
auto_movement_ = false;
|
||||||
|
jumping_time_ = 0.0F;
|
||||||
|
std::cout << "SET FALLING\n";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Llama a checkState con delta_time 0 porque esto es un cambio de estado inmediato
|
// Llama a checkState con delta_time 0 porque esto es un cambio de estado inmediato
|
||||||
checkState(0.0F);
|
updateState(0.0F);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inicializa los sonidos de salto y caida
|
// Inicializa los sonidos de salto y caida
|
||||||
@@ -641,40 +610,18 @@ void Player::applySpawnValues(const SpawnData& spawn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Inicializa el sprite del jugador
|
// Inicializa el sprite del jugador
|
||||||
void Player::initSprite(/*const std::string& surface_path, */const std::string& animations_path) {
|
void Player::initSprite(const std::string& animations_path) {
|
||||||
//auto surface = Resource::get()->getSurface(surface_path);
|
|
||||||
auto animations = Resource::get()->getAnimations(animations_path);
|
auto animations = Resource::get()->getAnimations(animations_path);
|
||||||
|
|
||||||
sprite_ = std::make_shared<SurfaceAnimatedSprite>(/*surface, */animations);
|
sprite_ = std::make_unique<SurfaceAnimatedSprite>(animations);
|
||||||
sprite_->setWidth(WIDTH);
|
sprite_->setWidth(WIDTH);
|
||||||
sprite_->setHeight(HEIGHT);
|
sprite_->setHeight(HEIGHT);
|
||||||
sprite_->setCurrentAnimation("walk");
|
sprite_->setCurrentAnimation("walk");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _DEBUG
|
// Coloca el sprite en la posición del jugador
|
||||||
// Pinta la información de debug del jugador
|
void Player::placeSprite() {
|
||||||
void Player::renderDebugInfo() {
|
sprite_->setPos(x_, y_); // Recoloca el sprite
|
||||||
if (Debug::get()->getEnabled()) {
|
collider_box_ = getRect(); // Actualiza el rectangulo de colisión
|
||||||
auto surface = Screen::get()->getRendererSurface();
|
updateColliderPoints(); // Actualiza los puntos de colisión
|
||||||
|
|
||||||
// Pinta los underfeet
|
|
||||||
surface->putPixel(under_feet_[0].x, under_feet_[0].y, static_cast<Uint8>(PaletteColor::BRIGHT_MAGENTA));
|
|
||||||
surface->putPixel(under_feet_[1].x, under_feet_[1].y, static_cast<Uint8>(PaletteColor::BRIGHT_MAGENTA));
|
|
||||||
|
|
||||||
// Pinta rectangulo del jugador
|
|
||||||
SDL_FRect rect = getRect();
|
|
||||||
surface->drawRectBorder(&rect, static_cast<Uint8>(PaletteColor::BRIGHT_CYAN));
|
|
||||||
|
|
||||||
// Pinta el rectangulo de movimiento
|
|
||||||
if (vx_ != 0.0F) {
|
|
||||||
surface->fillRect(&debug_rect_x_, static_cast<Uint8>(PaletteColor::BRIGHT_RED));
|
|
||||||
}
|
|
||||||
if (vy_ != 0.0F) {
|
|
||||||
surface->fillRect(&debug_rect_y_, static_cast<Uint8>(PaletteColor::BRIGHT_RED));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pinta el punto de debug
|
|
||||||
surface->putPixel(debug_point_.x, debug_point_.y, rand() % 16);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif // _DEBUG
|
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include <array> // Para array
|
||||||
#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>
|
||||||
@@ -47,7 +48,6 @@ class Player {
|
|||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
SpawnData spawn_data;
|
SpawnData spawn_data;
|
||||||
//std::string texture_path{};
|
|
||||||
std::string animations_path;
|
std::string animations_path;
|
||||||
std::shared_ptr<Room> room = nullptr;
|
std::shared_ptr<Room> room = nullptr;
|
||||||
|
|
||||||
@@ -57,7 +57,6 @@ class Player {
|
|||||||
// Constructor con parámetros
|
// Constructor con parámetros
|
||||||
Data(SpawnData spawn_data, const std::string& texture_path, std::string animations_path, std::shared_ptr<Room> room)
|
Data(SpawnData spawn_data, const std::string& texture_path, std::string animations_path, std::shared_ptr<Room> room)
|
||||||
: spawn_data(spawn_data),
|
: spawn_data(spawn_data),
|
||||||
// texture_path(std::move(texture_path)),
|
|
||||||
animations_path(std::move(animations_path)),
|
animations_path(std::move(animations_path)),
|
||||||
room(std::move(room)) {}
|
room(std::move(room)) {}
|
||||||
};
|
};
|
||||||
@@ -97,66 +96,55 @@ class Player {
|
|||||||
|
|
||||||
// --- --- 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_; // Objeto encargado de gestionar cada habitación del juego
|
||||||
std::shared_ptr<SurfaceAnimatedSprite> sprite_; // Sprite del jugador
|
std::unique_ptr<SurfaceAnimatedSprite> sprite_; // Sprite del jugador
|
||||||
|
|
||||||
// --- Variables ---
|
// --- Variables ---
|
||||||
float x_; // Posición del jugador en el eje X
|
float x_ = 0.0F; // Posición del jugador en el eje X
|
||||||
float y_; // Posición del jugador en el eje Y
|
float y_ = 0.0F; // Posición del jugador en el eje Y
|
||||||
float vx_; // Velocidad/desplazamiento del jugador en el eje X
|
float vx_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje X
|
||||||
float vy_; // Velocidad/desplazamiento del jugador en el eje Y
|
float vy_ = 0.0F; // Velocidad/desplazamiento del jugador en el eje Y
|
||||||
Uint8 color_; // Color del jugador
|
Uint8 color_ = 0; // Color del jugador
|
||||||
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::vector<SDL_FPoint> collider_points_; // Puntos de colisión con el mapa
|
std::array<SDL_FPoint, 8> 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::array<SDL_FPoint, 2> 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
|
std::array<SDL_FPoint, 2> feet_{}; // Contiene los puntos que hay en el pie del jugador
|
||||||
State state_; // Estado en el que se encuentra el jugador. Util apara saber si está saltando o cayendo
|
State state_ = State::STANDING; // 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
|
State previous_state_ = State::STANDING; // 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_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_alive_ = true; // Indica si el jugador esta vivo o no
|
||||||
bool is_paused_ = false; // Indica si el jugador esta en modo pausa
|
bool is_paused_ = false; // Indica si el jugador esta en modo pausa
|
||||||
bool auto_movement_ = false; // Indica si esta siendo arrastrado por una superficie automatica
|
bool auto_movement_ = false; // Indica si esta siendo arrastrado por una superficie automatica
|
||||||
Room::Border border_ = Room::Border::TOP; // Indica en cual de los cuatro bordes se encuentra
|
Room::Border border_ = Room::Border::TOP; // Indica en cual de los cuatro bordes se encuentra
|
||||||
SDL_FRect last_position_; // Contiene la ultima posición del jugador, por si hay que deshacer algun movimiento
|
int jump_init_pos_ = 0; // Valor del eje Y en el que se inicia el salto
|
||||||
int jump_init_pos_; // Valor del eje Y en el que se inicia el salto
|
std::vector<JA_Sound_t*> jumping_sound_; // Vecor con todos los sonidos del salto
|
||||||
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
|
||||||
std::vector<JA_Sound_t*> falling_sound_; // Vecor con todos los sonidos de la caída
|
float jumping_time_ = 0.0F; // Tiempo acumulado de salto en segundos
|
||||||
float jumping_time_ = 0.0F; // Tiempo acumulado de salto en segundos
|
int last_grounded_position_ = 0; // Ultima posición en Y en la que se estaba en contacto con el suelo
|
||||||
float falling_time_ = 0.0F; // Tiempo acumulado de caída en segundos
|
bool left_or_right_is_pressed_ = false; // Indica si se está pulsando una de las dos direcciones. Sirve para las conveyor belts
|
||||||
|
|
||||||
// --- Funciones ---
|
// --- Funciones ---
|
||||||
void checkInput(float delta_time); // Comprueba las entradas y modifica variables
|
void checkInput(); // Comprueba las entradas y modifica variables
|
||||||
void checkBorders(); // Comprueba si se halla en alguno de los cuatro bordes
|
void checkBorders(); // Comprueba si se halla en alguno de los cuatro bordes
|
||||||
void checkState(float delta_time); // Comprueba el estado del jugador
|
void updateState(float delta_time); // Comprueba el estado del jugador
|
||||||
void applyGravity(float delta_time); // Aplica gravedad al 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 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 moveHorizontalLeft(float delta_time); // Maneja el movimiento horizontal hacia la izquierda
|
||||||
void moveHorizontalRight(float delta_time); // Maneja el movimiento horizontal hacia la derecha
|
void moveHorizontalRight(float delta_time); // Maneja el movimiento horizontal hacia la derecha
|
||||||
void moveVerticalUp(float delta_time); // Maneja el movimiento vertical hacia arriba
|
void moveVerticalUp(float delta_time); // Maneja el movimiento vertical hacia arriba
|
||||||
void moveVerticalDown(float delta_time); // Maneja el movimiento vertical hacia abajo
|
void moveVerticalDown(float delta_time); // Maneja el movimiento vertical hacia abajo
|
||||||
void animate(float delta_time); // Establece la animación del jugador
|
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 checkJumpEnd(); // Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||||
void playJumpSound(); // Calcula y reproduce el sonido de salto
|
void playJumpSound(); // Calcula y reproduce el sonido de salto
|
||||||
void playFallSound(); // Calcula y reproduce el sonido de caer
|
void playFallSound(); // Calcula y reproduce el sonido de caer
|
||||||
auto isOnFloor() -> bool; // Comprueba si el jugador tiene suelo debajo de los pies
|
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 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 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
|
auto checkKillingTiles() -> bool; // Comprueba que el jugador no toque ningun tile de los que matan
|
||||||
void updateColliderPoints(); // Actualiza los puntos de colisión
|
void updateColliderPoints(); // Actualiza los puntos de colisión
|
||||||
void updateFeet(); // Actualiza los puntos de los pies
|
void updateFeet(); // Actualiza los puntos de los pies
|
||||||
void setState(State value); // Cambia el estado del jugador
|
void setState(State value); // Cambia el estado del jugador
|
||||||
void initSounds(); // Inicializa los sonidos de salto y caida
|
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 placeSprite(); // Coloca el sprite en la posición del jugador
|
||||||
void applySpawnValues(const SpawnData& spawn); // Aplica los valores de spawn al 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
|
void initSprite(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
|
|
||||||
};
|
};
|
||||||
@@ -37,7 +37,7 @@ enum class Options {
|
|||||||
inline Scene current = Scene::LOGO; // Escena actual
|
inline Scene current = Scene::LOGO; // Escena actual
|
||||||
inline Options options = Options::LOGO_TO_LOADING_SCREEN; // Opciones de la escena actual
|
inline Options options = Options::LOGO_TO_LOADING_SCREEN; // Opciones de la escena actual
|
||||||
#else
|
#else
|
||||||
inline Scene current = Scene::GAME_OVER; // Escena actual
|
inline Scene current = Scene::GAME; // Escena actual
|
||||||
inline Options options = Options::LOGO_TO_LOADING_SCREEN; // Opciones de la escena actual
|
inline Options options = Options::LOGO_TO_LOADING_SCREEN; // Opciones de la escena actual
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -155,17 +155,17 @@ void playMusic(const std::string& music_path);
|
|||||||
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
|
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
|
||||||
|
|
||||||
inline auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect {
|
inline auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect {
|
||||||
SDL_Rect rect;
|
SDL_Rect rect = {
|
||||||
rect.x = static_cast<int>(frect.x);
|
.x = static_cast<int>(frect.x),
|
||||||
rect.y = static_cast<int>(frect.y);
|
.y = static_cast<int>(frect.y),
|
||||||
rect.w = static_cast<int>(frect.w);
|
.w = static_cast<int>(frect.w),
|
||||||
rect.h = static_cast<int>(frect.h);
|
.h = static_cast<int>(frect.h)};
|
||||||
return rect;
|
return rect;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point {
|
inline auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point {
|
||||||
SDL_Point point;
|
SDL_Point point = {
|
||||||
point.x = static_cast<int>(fpoint.x);
|
.x = static_cast<int>(fpoint.x),
|
||||||
point.y = static_cast<int>(fpoint.y);
|
.y = static_cast<int>(fpoint.y)};
|
||||||
return point;
|
return point;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user