canvi de pc enmig de la enfangà

This commit is contained in:
2025-02-25 13:18:56 +01:00
parent 817140825a
commit c9da5135b2
29 changed files with 878 additions and 1042 deletions

View File

@@ -25,86 +25,84 @@ Player::Player(player_t player)
debug_(Debug::get())
{
// Crea objetos
sprite_ = new AnimatedSprite(renderer_, resource_->getAnimation(player.animation));
sprite_ = std::make_shared<AnimatedSprite>(resource_->getAnimation(player.animation));
// Inicializa variables
reLoadPalette();
onBorder = false;
border = BORDER_TOP;
autoMovement = false;
alive = true;
maxFallHeight = BLOCK * 4;
paused = false;
is_on_border_ = false;
border_ = BORDER_TOP;
auto_movement_ = false;
is_alive_ = true;
is_paused_ = false;
jumpIni = player.spawn.jumpIni;
state = player.spawn.state;
prevState = state;
jump_init_pos_ = player.spawn.jump_init_pos;
state_ = player.spawn.state;
previous_state_ = state_;
x = player.spawn.x;
y = player.spawn.y;
vx = player.spawn.vx;
vy = player.spawn.vy;
w = 8;
h = 16;
maxVY = 1.2f;
x_ = player.spawn.x;
y_ = player.spawn.y;
vx_ = player.spawn.vx;
vy_ = player.spawn.vy;
w_ = 8;
h_ = 16;
sprite_->setPosX(player.spawn.x);
sprite_->setPosY(player.spawn.y);
sprite_->setWidth(8);
sprite_->setHeight(16);
sprite_->setFlipH(player.spawn.flipH);
sprite_->setFlip(player.spawn.flip);
sprite_->setCurrentAnimation("walk");
sprite_->animate();
sprite_->update();
lastPosition = getRect();
colliderBox = getRect();
last_position_ = getRect();
collider_box_ = getRect();
const SDL_Point p = {0, 0};
colliderPoints.insert(colliderPoints.end(), {p, p, p, p, p, p, p, p});
underFeet.insert(underFeet.end(), {p, p});
feet.insert(feet.end(), {p, p});
jumpSound.push_back(JA_LoadSound(asset_->get("jump1.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump2.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump3.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump4.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump5.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump6.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump7.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump8.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump9.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump10.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump11.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump12.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump13.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump14.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump15.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump16.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump17.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump18.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump19.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump20.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump21.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump22.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump23.wav").c_str()));
jumpSound.push_back(JA_LoadSound(asset_->get("jump24.wav").c_str()));
collider_points_.insert(collider_points_.end(), {p, p, p, p, p, p, p, p});
under_feet_.insert(under_feet_.end(), {p, p});
feet_.insert(feet_.end(), {p, p});
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump1.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump2.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump3.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump4.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump5.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump6.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump7.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump8.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump9.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump10.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump11.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump12.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump13.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump14.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump15.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump16.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump17.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump18.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump19.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump20.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump21.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump22.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump23.wav").c_str()));
jumping_sound_.push_back(JA_LoadSound(asset_->get("jump24.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump11.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump12.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump13.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump14.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump15.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump16.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump17.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump18.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump19.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump20.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump21.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump22.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump23.wav").c_str()));
fallSound.push_back(JA_LoadSound(asset_->get("jump24.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump11.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump12.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump13.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump14.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump15.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump16.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump17.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump18.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump19.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump20.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump21.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump22.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump23.wav").c_str()));
falling_sound_.push_back(JA_LoadSound(asset_->get("jump24.wav").c_str()));
jumpCounter = 0;
fallCounter = 0;
jumping_counter_ = 0;
falling_counter_ = 0;
#ifdef DEBUG
rx = {0, 0, 0, 0};
@@ -114,21 +112,10 @@ Player::Player(player_t player)
#endif
}
// Destructor
Player::~Player()
{
delete sprite_;
for (auto s : jumpSound)
{
JA_DeleteSound(s);
}
}
// Pinta el jugador en pantalla
void Player::render()
{
sprite_->getTexture()->setColor(color.r, color.g, color.b);
sprite_->getTexture()->setColor(color_.r, color_.g, color_.b);
sprite_->render();
#ifdef DEBUG
@@ -167,7 +154,7 @@ void Player::render()
// Actualiza las variables del objeto
void Player::update()
{
if (paused)
if (is_paused_)
{ // Si está en modo pausa no se actualiza nada
return;
}
@@ -184,45 +171,45 @@ void Player::update()
void Player::checkInput()
{
// Solo comprueba las entradas de dirección cuando está sobre una superficie
if (state != s_standing)
if (state_ != s_standing)
{
return;
}
if (!autoMovement)
if (!auto_movement_)
{ // Comprueba las entradas de desplazamiento lateral solo en el caso de no estar enganchado a una superficie automatica
if (input_->checkInput(input_left))
{
vx = -0.6f;
sprite_->setFlipH(true);
vx_ = -0.6f;
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
}
else if (input_->checkInput(input_right))
{
vx = 0.6f;
sprite_->setFlipH(false);
vx_ = 0.6f;
sprite_->setFlip(SDL_FLIP_NONE);
}
else
{ // No se pulsa ninguna dirección
vx = 0.0f;
vx_ = 0.0f;
if (isOnAutoSurface())
{ // Si deja de moverse sobre una superficie se engancha
autoMovement = true;
auto_movement_ = true;
}
}
}
else
{ // El movimiento lo proporciona la superficie
vx = 0.6f * room_->getAutoSurfaceDirection();
vx_ = 0.6f * room_->getAutoSurfaceDirection();
if (vx > 0.0f)
if (vx_ > 0.0f)
{
sprite_->setFlipH(false);
sprite_->setFlip(SDL_FLIP_NONE);
}
else
{
sprite_->setFlipH(true);
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
}
}
@@ -236,9 +223,9 @@ void Player::checkInput()
if (isOnFloor() || isOnAutoSurface())
{
setState(s_jumping);
vy = -maxVY;
jumpIni = y;
jumpCounter = 0;
vy_ = -MAX_VY_;
jump_init_pos_ = y_;
jumping_counter_ = 0;
}
}
}
@@ -246,45 +233,45 @@ void Player::checkInput()
// Indica si el jugador esta en uno de los cuatro bordes de la pantalla
bool Player::getOnBorder()
{
return onBorder;
return is_on_border_;
}
// Indica en cual de los cuatro bordes se encuentra
int Player::getBorder()
{
return border;
return border_;
}
// Comprueba si está situado en alguno de los cuatro bordes de la habitación
void Player::checkBorders()
{
if (x < PLAY_AREA_LEFT)
if (x_ < PLAY_AREA_LEFT)
{
border = BORDER_LEFT;
onBorder = true;
border_ = BORDER_LEFT;
is_on_border_ = true;
}
else if (x > PLAY_AREA_RIGHT - w)
else if (x_ + w_ > PLAY_AREA_RIGHT)
{
border = BORDER_RIGHT;
onBorder = true;
border_ = BORDER_RIGHT;
is_on_border_ = true;
}
else if (y < PLAY_AREA_TOP)
else if (y_ < PLAY_AREA_TOP)
{
border = BORDER_TOP;
onBorder = true;
border_ = BORDER_TOP;
is_on_border_ = true;
}
else if (y > PLAY_AREA_BOTTOM - h)
else if (y_ + h_ > PLAY_AREA_BOTTOM)
{
border = BORDER_BOTTOM;
onBorder = true;
border_ = BORDER_BOTTOM;
is_on_border_ = true;
}
else
{
onBorder = false;
is_on_border_ = false;
}
}
@@ -292,37 +279,37 @@ void Player::checkBorders()
void Player::checkState()
{
// Actualiza las variables en función del estado
if (state == s_falling)
if (state_ == s_falling)
{
vx = 0.0f;
vy = maxVY;
fallCounter++;
vx_ = 0.0f;
vy_ = MAX_VY_;
falling_counter_++;
playFallSound();
}
else if (state == s_standing)
else if (state_ == s_standing)
{
if (prevState == s_falling && fallCounter > maxFallHeight)
if (previous_state_ == s_falling && falling_counter_ > MAX_FALLING_HEIGHT_)
{ // Si cae de muy alto, el jugador muere
alive = false;
is_alive_ = false;
}
vy = 0.0f;
jumpCounter = 0;
fallCounter = 0;
vy_ = 0.0f;
jumping_counter_ = 0;
falling_counter_ = 0;
if (!isOnFloor() && !isOnAutoSurface() && !isOnDownSlope())
{
setState(s_falling);
vx = 0.0f;
vy = maxVY;
fallCounter++;
vx_ = 0.0f;
vy_ = MAX_VY_;
falling_counter_++;
playFallSound();
}
}
else if (state == s_jumping)
else if (state_ == s_jumping)
{
fallCounter = 0;
jumpCounter++;
falling_counter_ = 0;
jumping_counter_++;
playJumpSound();
}
}
@@ -330,41 +317,41 @@ void Player::checkState()
// Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
void Player::switchBorders()
{
if (border == BORDER_TOP)
if (border_ == BORDER_TOP)
{
y = PLAY_AREA_BOTTOM - h - 0 - BLOCK;
y_ = PLAY_AREA_BOTTOM - h_ - 0 - BLOCK;
setState(s_standing);
}
else if (border == BORDER_BOTTOM)
else if (border_ == BORDER_BOTTOM)
{
y = PLAY_AREA_TOP + 0;
y_ = PLAY_AREA_TOP + 0;
setState(s_standing);
}
else if (border == BORDER_RIGHT)
else if (border_ == BORDER_RIGHT)
{
x = PLAY_AREA_LEFT + 0;
x_ = PLAY_AREA_LEFT + 0;
}
if (border == BORDER_LEFT)
if (border_ == BORDER_LEFT)
{
x = PLAY_AREA_RIGHT - w - 0;
x_ = PLAY_AREA_RIGHT - w_ - 0;
}
onBorder = false;
is_on_border_ = false;
}
// Aplica gravedad al jugador
void Player::applyGravity()
{
const float gf = 0.035f;
constexpr float GF = 0.035f;
// La gravedad solo se aplica cuando el jugador esta saltando
// Nunca mientras cae o esta de pie
if (state == s_jumping)
if (state_ == s_jumping)
{
vy += gf;
if (vy > maxVY)
vy_ += GF;
if (vy_ > MAX_VY_)
{
vy = maxVY;
vy_ = MAX_VY_;
}
}
}
@@ -372,36 +359,36 @@ void Player::applyGravity()
// Obtiene el rectangulo que delimita al jugador
SDL_Rect Player::getRect()
{
return {(int)x, (int)y, w, h};
return {static_cast<int>(x_), static_cast<int>(y_), w_, h_};
}
// Obtiene el rectangulo de colision del jugador
SDL_Rect &Player::getCollider()
{
colliderBox = getRect();
return colliderBox;
collider_box_ = getRect();
return collider_box_;
}
// Recalcula la posición del jugador y su animación
void Player::move()
{
lastPosition = {(int)x, (int)y}; // Guarda la posicion actual antes de modificarla
applyGravity(); // Aplica gravedad al jugador
checkState(); // Comprueba el estado del jugador
last_position_ = {static_cast<int>(x_), static_cast<int>(y_)}; // Guarda la posicion actual antes de modificarla
applyGravity(); // Aplica gravedad al jugador
checkState(); // Comprueba el estado del jugador
#ifdef DEBUG
debugColor = {0, 255, 0};
#endif
// Se mueve hacia la izquierda
if (vx < 0.0f)
if (vx_ < 0.0f)
{
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_Rect proj;
proj.x = (int)(x + vx);
proj.y = (int)y;
proj.h = h;
proj.w = ceil(abs(vx)); // Para evitar que tenga un ancho de 0 pixels
proj.x = static_cast<int>(x_ + vx_);
proj.y = static_cast<int>(y_);
proj.h = h_;
proj.w = ceil(abs(vx_)); // Para evitar que tenga un ancho de 0 pixels
#ifdef DEBUG
rx = proj;
@@ -412,41 +399,43 @@ void Player::move()
// Calcula la nueva posición
if (pos == -1)
{ // Si no hay colisión
x += vx;
{
// Si no hay colisión
x_ += vx_;
}
else
{ // Si hay colisión lo mueve hasta donde no colisiona
x = pos + 1;
{
// Si hay colisión lo mueve hasta donde no colisiona
x_ = pos + 1;
}
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state != s_jumping)
if (state_ != s_jumping)
{
v_line_t leftSide = {(int)x, (int)y + h - 2, (int)y + h - 1}; // Comprueba solo los dos pixels de abajo
v_line_t leftSide = {static_cast<int>(x_), static_cast<int>(y_) + h_ - 2, static_cast<int>(y_) + h_ - 1}; // Comprueba solo los dos pixels de abajo
const int ly = room_->checkLeftSlopes(&leftSide);
if (ly > -1)
{
y = ly - h;
y_ = ly - h_;
}
}
// Si está bajando la rampa, recoloca al jugador
if (isOnDownSlope() && state != s_jumping)
if (isOnDownSlope() && state_ != s_jumping)
{
y += 1;
y_ += 1;
}
}
// Se mueve hacia la derecha
else if (vx > 0.0f)
else if (vx_ > 0.0f)
{
// Crea el rectangulo de proyección en el eje X para ver si colisiona
SDL_Rect proj;
proj.x = (int)x + w;
proj.y = (int)y;
proj.h = h;
proj.w = ceil(vx); // Para evitar que tenga un ancho de 0 pixels
proj.x = static_cast<int>(x_) + w_;
proj.y = static_cast<int>(y_);
proj.h = h_;
proj.w = ceil(vx_); // Para evitar que tenga un ancho de 0 pixels
#ifdef DEBUG
rx = proj;
@@ -457,57 +446,59 @@ void Player::move()
// Calcula la nueva posición
if (pos == -1)
{ // Si no hay colisión
x += vx;
{
// Si no hay colisión
x_ += vx_;
}
else
{ // Si hay colisión lo mueve hasta donde no colisiona
x = pos - w;
{
// Si hay colisión lo mueve hasta donde no colisiona
x_ = pos - w_;
}
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
if (state != s_jumping)
if (state_ != s_jumping)
{
v_line_t rightSide = {(int)x + w - 1, (int)y + h - 2, (int)y + h - 1}; // Comprueba solo los dos pixels de abajo
v_line_t rightSide = {static_cast<int>(x_) + w_ - 1, static_cast<int>(y_) + h_ - 2, static_cast<int>(y_) + h_ - 1}; // Comprueba solo los dos pixels de abajo
const int ry = room_->checkRightSlopes(&rightSide);
if (ry > -1)
{
y = ry - h;
y_ = ry - h_;
}
}
// Si está bajando la rampa, recoloca al jugador
if (isOnDownSlope() && state != s_jumping)
if (isOnDownSlope() && state_ != s_jumping)
{
y += 1;
y_ += 1;
}
}
// Si ha salido del suelo, el jugador cae
if (state == s_standing && !isOnFloor())
if (state_ == s_standing && !isOnFloor())
{
setState(s_falling);
// Deja de estar enganchado a la superficie automatica
autoMovement = false;
auto_movement_ = false;
}
// Si ha salido de una superficie automatica, detiene el movimiento automatico
if (state == s_standing && isOnFloor() && !isOnAutoSurface())
if (state_ == s_standing && isOnFloor() && !isOnAutoSurface())
{
// Deja de estar enganchado a la superficie automatica
autoMovement = false;
auto_movement_ = false;
}
// Se mueve hacia arriba
if (vy < 0.0f)
if (vy_ < 0.0f)
{
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)(y + vy);
proj.h = ceil(abs(vy)); // Para evitar que tenga una altura de 0 pixels
proj.w = w;
proj.x = static_cast<int>(x_);
proj.y = static_cast<int>(y_ + vy_);
proj.h = ceil(abs(vy_)); // Para evitar que tenga una altura de 0 pixels
proj.w = w_;
#ifdef DEBUG
ry = proj;
@@ -518,25 +509,27 @@ void Player::move()
// Calcula la nueva posición
if (pos == -1)
{ // Si no hay colisión
y += vy;
{
// Si no hay colisión
y_ += vy_;
}
else
{ // Si hay colisión lo mueve hasta donde no colisiona y entra en caída
y = pos + 1;
{
// Si hay colisión lo mueve hasta donde no colisiona y entra en caída
y_ = pos + 1;
setState(s_falling);
}
}
// Se mueve hacia abajo
else if (vy > 0.0f)
else if (vy_ > 0.0f)
{
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)y + h;
proj.h = ceil(vy); // Para evitar que tenga una altura de 0 pixels
proj.w = w;
proj.x = static_cast<int>(x_);
proj.y = static_cast<int>(y_) + h_;
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
proj.w = w_;
#ifdef DEBUG
ry = proj;
@@ -545,24 +538,27 @@ void Player::move()
// Comprueba la colisión con las superficies normales y las automáticas
const int pos = std::max(room_->checkTopSurfaces(&proj), room_->checkAutoSurfaces(&proj));
if (pos > -1)
{ // Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
y = pos - h;
{
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
y_ = pos - h_;
setState(s_standing);
// Deja de estar enganchado a la superficie automatica
autoMovement = false;
auto_movement_ = false;
}
else
{ // Si no hay colisión con los muros, comprueba la colisión con las rampas
if (state != s_jumping)
{
// Si no hay colisión con los muros, comprueba la colisión con las rampas
if (state_ != s_jumping)
{ // Las rampas no se miran si se está saltando
v_line_t leftSide = {proj.x, proj.y, proj.y + proj.h - 1};
v_line_t rightSide = {proj.x + proj.w - 1, proj.y, proj.y + proj.h - 1};
const int p = std::max(room_->checkRightSlopes(&rightSide), room_->checkLeftSlopes(&leftSide));
if (p > -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
y = p - h;
y_ = p - h_;
setState(s_standing);
#ifdef DEBUG
debugColor = {255, 255, 0};
@@ -570,25 +566,27 @@ void Player::move()
#endif
}
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
y += vy;
y_ += vy_;
#ifdef DEBUG
debugColor = {255, 0, 0};
#endif
}
}
else
{ // Esta saltando y no hay colisión con los muros
{
// Esta saltando y no hay colisión con los muros
// Calcula la nueva posición
y += vy;
y_ += vy_;
}
}
}
// Actualiza la posición del sprite
sprite_->setPosX(x);
sprite_->setPosY(y);
sprite_->setPosX(x_);
sprite_->setPosY(y_);
#ifdef DEBUG
debug_->add("RECT_X: " + std::to_string(rx.x) + "," + std::to_string(rx.y) + "," + std::to_string(rx.w) + "," + std::to_string(rx.h));
@@ -599,24 +597,25 @@ void Player::move()
// Establece la animación del jugador
void Player::animate()
{
if (vx != 0)
if (vx_ != 0)
{
sprite_->animate();
sprite_->update();
}
}
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
void Player::checkJumpEnd()
{
if (state == s_jumping)
if (state_ == s_jumping)
{
if (vy > 0)
if (vy_ > 0)
{
if (y >= jumpIni)
{ // Si alcanza la altura de salto inicial, pasa al estado de caída
if (y_ >= jump_init_pos_)
{
// Si alcanza la altura de salto inicial, pasa al estado de caída
setState(s_falling);
vy = maxVY;
jumpCounter = 0;
vy_ = MAX_VY_;
jumping_counter_ = 0;
}
}
}
@@ -625,9 +624,9 @@ void Player::checkJumpEnd()
// Calcula y reproduce el sonido de salto
void Player::playJumpSound()
{
if (jumpCounter % 4 == 0)
if (jumping_counter_ % 4 == 0)
{
JA_PlaySound(jumpSound[jumpCounter / 4]);
JA_PlaySound(jumping_sound_[jumping_counter_ / 4]);
}
#ifdef DEBUG
@@ -638,9 +637,9 @@ void Player::playJumpSound()
// Calcula y reproduce el sonido de caer
void Player::playFallSound()
{
if (fallCounter % 4 == 0)
if (falling_counter_ % 4 == 0)
{
JA_PlaySound(fallSound[std::min((fallCounter / 4), (int)fallSound.size() - 1)]);
JA_PlaySound(falling_sound_[std::min((falling_counter_ / 4), (int)falling_sound_.size() - 1)]);
}
#ifdef DEBUG
@@ -658,15 +657,15 @@ bool Player::isOnFloor()
updateFeet();
// Comprueba las superficies
for (auto f : underFeet)
for (auto f : under_feet_)
{
onFloor |= room_->checkTopSurfaces(&f);
onFloor |= room_->checkAutoSurfaces(&f);
}
// Comprueba las rampas
onSlopeL = room_->checkLeftSlopes(&underFeet[0]);
onSlopeR = room_->checkRightSlopes(&underFeet[1]);
onSlopeL = room_->checkLeftSlopes(&under_feet_[0]);
onSlopeR = room_->checkRightSlopes(&under_feet_[1]);
#ifdef DEBUG
if (onFloor)
@@ -696,7 +695,7 @@ bool Player::isOnAutoSurface()
updateFeet();
// Comprueba las superficies
for (auto f : underFeet)
for (auto f : under_feet_)
{
onAutoSurface |= room_->checkAutoSurfaces(&f);
}
@@ -720,12 +719,12 @@ bool Player::isOnDownSlope()
// Cuando el jugador baja una escalera, se queda volando
// Hay que mirar otro pixel más por debajo
underFeet[0].y += 1;
underFeet[1].y += 1;
under_feet_[0].y += 1;
under_feet_[1].y += 1;
// Comprueba las rampas
onSlope |= room_->checkLeftSlopes(&underFeet[0]);
onSlope |= room_->checkRightSlopes(&underFeet[1]);
onSlope |= room_->checkLeftSlopes(&under_feet_[0]);
onSlope |= room_->checkRightSlopes(&under_feet_[1]);
#ifdef DEBUG
if (onSlope)
@@ -746,7 +745,7 @@ bool Player::checkKillingTiles()
// Comprueba si hay contacto
bool check = false;
for (auto c : colliderPoints)
for (auto c : collider_points_)
{
check |= (room_->getTile(c) == t_kill);
}
@@ -754,7 +753,7 @@ bool Player::checkKillingTiles()
// Mata al jugador si hay colisión
if (check)
{
alive = false;
is_alive_ = false;
}
return check;
@@ -765,13 +764,13 @@ playerSpawn_t Player::getSpawnParams()
{
playerSpawn_t params;
params.x = x;
params.y = y;
params.vx = vx;
params.vy = vy;
params.jumpIni = jumpIni;
params.state = state;
params.flipH = sprite_->getFlipH();
params.x = x_;
params.y = y_;
params.vx = vx_;
params.vy = vy_;
params.jump_init_pos = jump_init_pos_;
params.state = state_;
params.flip = sprite_->getFlip();
return params;
}
@@ -785,60 +784,54 @@ void Player::reLoadTexture()
// Recarga la paleta
void Player::reLoadPalette()
{
color = stringToColor(options.video.palette, "white");
color_ = stringToColor(options.video.palette, "white");
if (options.cheats.infinite_lives == Cheat::CheatState::ENABLED)
{
color = stringToColor(options.video.palette, "green");
}
{
color = stringToColor(options.video.palette, "yellow");
color_ = stringToColor(options.video.palette, "yellow");
}
if (options.cheats.invincible == Cheat::CheatState::ENABLED)
{
color = stringToColor(options.video.palette, "green");
}
{
color = stringToColor(options.video.palette, "cyan");
color_ = stringToColor(options.video.palette, "cyan");
}
}
// Establece el valor de la variable
void Player::setRoom(Room *room)
void Player::setRoom(std::shared_ptr<Room> room)
{
this->room_ = room;
room_ = room;
}
// Actualiza los puntos de colisión
void Player::updateColliderPoints()
{
const SDL_Rect rect = getRect();
colliderPoints[0] = {rect.x, rect.y};
colliderPoints[1] = {rect.x + 7, rect.y};
colliderPoints[2] = {rect.x + 7, rect.y + 7};
colliderPoints[3] = {rect.x, rect.y + 7};
colliderPoints[4] = {rect.x, rect.y + 8};
colliderPoints[5] = {rect.x + 7, rect.y + 8};
colliderPoints[6] = {rect.x + 7, rect.y + 15};
colliderPoints[7] = {rect.x, rect.y + 15};
collider_points_[0] = {rect.x, rect.y};
collider_points_[1] = {rect.x + 7, rect.y};
collider_points_[2] = {rect.x + 7, rect.y + 7};
collider_points_[3] = {rect.x, rect.y + 7};
collider_points_[4] = {rect.x, rect.y + 8};
collider_points_[5] = {rect.x + 7, rect.y + 8};
collider_points_[6] = {rect.x + 7, rect.y + 15};
collider_points_[7] = {rect.x, rect.y + 15};
}
// Actualiza los puntos de los pies
void Player::updateFeet()
{
const SDL_Point p = {(int)x, (int)y};
const SDL_Point p = {static_cast<int>(x_), static_cast<int>(y_)};
underFeet[0] = {p.x, p.y + h};
underFeet[1] = {p.x + 7, p.y + h};
under_feet_[0] = {p.x, p.y + h_};
under_feet_[1] = {p.x + 7, p.y + h_};
feet[0] = {p.x, p.y + h - 1};
feet[1] = {p.x + 7, p.y + h - 1};
feet_[0] = {p.x, p.y + h_ - 1};
feet_[1] = {p.x + 7, p.y + h_ - 1};
}
// Cambia el estado del jugador
void Player::setState(state_e value)
{
prevState = state;
state = value;
previous_state_ = state_;
state_ = value;
checkState();
}
@@ -846,17 +839,17 @@ void Player::setState(state_e value)
// Comprueba si el jugador esta vivo
bool Player::isAlive()
{
return alive;
return is_alive_;
}
// Pone el jugador en modo pausa
void Player::pause()
{
paused = true;
is_paused_ = true;
}
// Quita el modo pausa del jugador
void Player::resume()
{
paused = false;
is_paused_ = false;
}