forked from jaildesigner-jailgames/jaildoctors_dilemma
corregits mes includes
llevats els errors en texture
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
// IWYU pragma: no_include <bits/std_abs.h>
|
||||
#include "player.h"
|
||||
#include <algorithm> // Para max, min
|
||||
#include <cmath> // Para ceil, abs
|
||||
#include "debug.h" // Para Debug
|
||||
#include "defines.h" // Para RoomBorder::BOTTOM, RoomBorder::LEFT, RoomBorder::RIGHT
|
||||
#include "input.h" // Para Input, InputAction
|
||||
#include "jail_audio.h" // Para JA_PlaySound
|
||||
#include "options.h" // Para Cheat, Options, options
|
||||
#include "resource.h" // Para Resource
|
||||
#include "room.h" // Para Room, TileType
|
||||
#include "s_animated_sprite.h" // Para SAnimatedSprite
|
||||
|
||||
#include <algorithm> // Para max, min
|
||||
#include <cmath> // Para ceil, abs
|
||||
|
||||
#include "debug.h" // Para Debug
|
||||
#include "defines.h" // Para RoomBorder::BOTTOM, RoomBorder::LEFT, RoomBorder::RIGHT
|
||||
#include "external/jail_audio.h" // Para JA_PlaySound
|
||||
#include "input.h" // Para Input, InputAction
|
||||
#include "options.h" // Para Cheat, Options, options
|
||||
#include "resource.h" // Para Resource
|
||||
#include "room.h" // Para Room, TileType
|
||||
#include "sprite/surface_animated_sprite.h" // Para SAnimatedSprite
|
||||
|
||||
// Constructor
|
||||
Player::Player(const PlayerData &player)
|
||||
: room_(player.room)
|
||||
{
|
||||
Player::Player(const PlayerData& player)
|
||||
: room_(player.room) {
|
||||
// Inicializa algunas variables
|
||||
initSprite(player.texture_path, player.animations_path);
|
||||
setColor();
|
||||
@@ -38,8 +39,7 @@ Player::Player(const PlayerData &player)
|
||||
}
|
||||
|
||||
// Pinta el jugador en pantalla
|
||||
void Player::render()
|
||||
{
|
||||
void Player::render() {
|
||||
sprite_->render(1, color_);
|
||||
|
||||
#ifdef DEBUG
|
||||
@@ -48,77 +48,61 @@ void Player::render()
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
void Player::update()
|
||||
{
|
||||
if (!is_paused_)
|
||||
{
|
||||
checkInput(); // Comprueba las entradas y modifica variables
|
||||
move(); // Recalcula la posición del jugador
|
||||
animate(); // Establece la animación del jugador
|
||||
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||
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}
|
||||
void Player::update() {
|
||||
if (!is_paused_) {
|
||||
checkInput(); // Comprueba las entradas y modifica variables
|
||||
move(); // Recalcula la posición del jugador
|
||||
animate(); // Establece la animación del jugador
|
||||
checkBorders(); // Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||
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}
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba las entradas y modifica variables
|
||||
void Player::checkInput()
|
||||
{
|
||||
void Player::checkInput() {
|
||||
// Solo comprueba las entradas de dirección cuando está sobre una superficie
|
||||
if (state_ != PlayerState::STANDING)
|
||||
{
|
||||
if (state_ != PlayerState::STANDING) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!auto_movement_)
|
||||
{
|
||||
if (!auto_movement_) {
|
||||
// Comprueba las entradas de desplazamiento lateral solo en el caso de no estar enganchado a una superficie automatica
|
||||
if (Input::get()->checkInput(InputAction::LEFT))
|
||||
{
|
||||
if (Input::get()->checkInput(InputAction::LEFT)) {
|
||||
vx_ = -0.6f;
|
||||
sprite_->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
}
|
||||
|
||||
else if (Input::get()->checkInput(InputAction::RIGHT))
|
||||
{
|
||||
else if (Input::get()->checkInput(InputAction::RIGHT)) {
|
||||
vx_ = 0.6f;
|
||||
sprite_->setFlip(SDL_FLIP_NONE);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
// No se pulsa ninguna dirección
|
||||
vx_ = 0.0f;
|
||||
if (isOnAutoSurface())
|
||||
{
|
||||
if (isOnAutoSurface()) {
|
||||
// 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_ = 0.6f * room_->getAutoSurfaceDirection();
|
||||
|
||||
if (vx_ > 0.0f)
|
||||
{
|
||||
if (vx_ > 0.0f) {
|
||||
sprite_->setFlip(SDL_FLIP_NONE);
|
||||
}
|
||||
else
|
||||
{
|
||||
} 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
|
||||
// Esto es para evitar el salto desde el vacio al cambiar de pantalla verticalmente
|
||||
// Ya que se coloca el estado s_standing al cambiar de pantalla
|
||||
|
||||
if (isOnFloor() || isOnAutoSurface())
|
||||
{
|
||||
if (isOnFloor() || isOnAutoSurface()) {
|
||||
setState(PlayerState::JUMPING);
|
||||
vy_ = -MAX_VY_;
|
||||
jump_init_pos_ = y_;
|
||||
@@ -128,61 +112,50 @@ void Player::checkInput()
|
||||
}
|
||||
|
||||
// Comprueba si está situado en alguno de los cuatro bordes de la habitación
|
||||
void Player::checkBorders()
|
||||
{
|
||||
if (x_ < PLAY_AREA_LEFT)
|
||||
{
|
||||
void Player::checkBorders() {
|
||||
if (x_ < PLAY_AREA_LEFT) {
|
||||
border_ = RoomBorder::LEFT;
|
||||
is_on_border_ = true;
|
||||
}
|
||||
|
||||
else if (x_ + WIDTH_ > PLAY_AREA_RIGHT)
|
||||
{
|
||||
else if (x_ + WIDTH_ > PLAY_AREA_RIGHT) {
|
||||
border_ = RoomBorder::RIGHT;
|
||||
is_on_border_ = true;
|
||||
}
|
||||
|
||||
else if (y_ < PLAY_AREA_TOP)
|
||||
{
|
||||
else if (y_ < PLAY_AREA_TOP) {
|
||||
border_ = RoomBorder::TOP;
|
||||
is_on_border_ = true;
|
||||
}
|
||||
|
||||
else if (y_ + HEIGHT_ > PLAY_AREA_BOTTOM)
|
||||
{
|
||||
else if (y_ + HEIGHT_ > PLAY_AREA_BOTTOM) {
|
||||
border_ = RoomBorder::BOTTOM;
|
||||
is_on_border_ = true;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
else {
|
||||
is_on_border_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba el estado del jugador
|
||||
void Player::checkState()
|
||||
{
|
||||
void Player::checkState() {
|
||||
// Actualiza las variables en función del estado
|
||||
if (state_ == PlayerState::FALLING)
|
||||
{
|
||||
if (state_ == PlayerState::FALLING) {
|
||||
vx_ = 0.0f;
|
||||
vy_ = MAX_VY_;
|
||||
falling_counter_++;
|
||||
playFallSound();
|
||||
}
|
||||
|
||||
else if (state_ == PlayerState::STANDING)
|
||||
{
|
||||
if (previous_state_ == PlayerState::FALLING && falling_counter_ > MAX_FALLING_HEIGHT_)
|
||||
{ // Si cae de muy alto, el jugador muere
|
||||
else if (state_ == PlayerState::STANDING) {
|
||||
if (previous_state_ == PlayerState::FALLING && falling_counter_ > MAX_FALLING_HEIGHT_) { // Si cae de muy alto, el jugador muere
|
||||
is_alive_ = false;
|
||||
}
|
||||
vy_ = 0.0f;
|
||||
jumping_counter_ = 0;
|
||||
falling_counter_ = 0;
|
||||
if (!isOnFloor() && !isOnAutoSurface() && !isOnDownSlope())
|
||||
{
|
||||
if (!isOnFloor() && !isOnAutoSurface() && !isOnDownSlope()) {
|
||||
setState(PlayerState::FALLING);
|
||||
vx_ = 0.0f;
|
||||
vy_ = MAX_VY_;
|
||||
@@ -191,8 +164,7 @@ void Player::checkState()
|
||||
}
|
||||
}
|
||||
|
||||
else if (state_ == PlayerState::JUMPING)
|
||||
{
|
||||
else if (state_ == PlayerState::JUMPING) {
|
||||
falling_counter_ = 0;
|
||||
jumping_counter_++;
|
||||
playJumpSound();
|
||||
@@ -200,30 +172,28 @@ void Player::checkState()
|
||||
}
|
||||
|
||||
// Cambia al jugador de un borde al opuesto. Util para el cambio de pantalla
|
||||
void Player::switchBorders()
|
||||
{
|
||||
switch (border_)
|
||||
{
|
||||
case RoomBorder::TOP:
|
||||
y_ = PLAY_AREA_BOTTOM - HEIGHT_ - BLOCK;
|
||||
setState(PlayerState::STANDING);
|
||||
break;
|
||||
void Player::switchBorders() {
|
||||
switch (border_) {
|
||||
case RoomBorder::TOP:
|
||||
y_ = PLAY_AREA_BOTTOM - HEIGHT_ - BLOCK;
|
||||
setState(PlayerState::STANDING);
|
||||
break;
|
||||
|
||||
case RoomBorder::BOTTOM:
|
||||
y_ = PLAY_AREA_TOP;
|
||||
setState(PlayerState::STANDING);
|
||||
break;
|
||||
case RoomBorder::BOTTOM:
|
||||
y_ = PLAY_AREA_TOP;
|
||||
setState(PlayerState::STANDING);
|
||||
break;
|
||||
|
||||
case RoomBorder::RIGHT:
|
||||
x_ = PLAY_AREA_LEFT;
|
||||
break;
|
||||
case RoomBorder::RIGHT:
|
||||
x_ = PLAY_AREA_LEFT;
|
||||
break;
|
||||
|
||||
case RoomBorder::LEFT:
|
||||
x_ = PLAY_AREA_RIGHT - WIDTH_;
|
||||
break;
|
||||
case RoomBorder::LEFT:
|
||||
x_ = PLAY_AREA_RIGHT - WIDTH_;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
is_on_border_ = false;
|
||||
@@ -232,42 +202,37 @@ void Player::switchBorders()
|
||||
}
|
||||
|
||||
// Aplica gravedad al jugador
|
||||
void Player::applyGravity()
|
||||
{
|
||||
void Player::applyGravity() {
|
||||
constexpr float GRAVITY_FORCE = 0.035f;
|
||||
|
||||
// La gravedad solo se aplica cuando el jugador esta saltando
|
||||
// Nunca mientras cae o esta de pie
|
||||
if (state_ == PlayerState::JUMPING)
|
||||
{
|
||||
if (state_ == PlayerState::JUMPING) {
|
||||
vy_ += GRAVITY_FORCE;
|
||||
if (vy_ > MAX_VY_)
|
||||
{
|
||||
if (vy_ > MAX_VY_) {
|
||||
vy_ = MAX_VY_;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Recalcula la posición del jugador y su animación
|
||||
void Player::move()
|
||||
{
|
||||
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
|
||||
void Player::move() {
|
||||
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
|
||||
debug_color_ = static_cast<Uint8>(PaletteColor::GREEN);
|
||||
#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 = static_cast<int>(x_ + vx_);
|
||||
proj.y = static_cast<int>(y_);
|
||||
proj.h = HEIGHT_;
|
||||
proj.w = static_cast<int>(std::ceil(std::fabs(vx_))); // Para evitar que tenga un ancho de 0 pixels
|
||||
proj.w = static_cast<int>(std::ceil(std::fabs(vx_))); // Para evitar que tenga un ancho de 0 pixels
|
||||
|
||||
#ifdef DEBUG
|
||||
debug_rect_x_ = proj;
|
||||
@@ -277,44 +242,37 @@ void Player::move()
|
||||
const int POS = room_->checkRightSurfaces(&proj);
|
||||
|
||||
// Calcula la nueva posición
|
||||
if (POS == -1)
|
||||
{
|
||||
if (POS == -1) {
|
||||
// Si no hay colisión
|
||||
x_ += vx_;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// 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_ != PlayerState::JUMPING)
|
||||
{
|
||||
const LineVertical LEFT_SIDE = {static_cast<int>(x_), static_cast<int>(y_) + HEIGHT_ - 2, static_cast<int>(y_) + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo
|
||||
if (state_ != PlayerState::JUMPING) {
|
||||
const LineVertical LEFT_SIDE = {static_cast<int>(x_), static_cast<int>(y_) + HEIGHT_ - 2, static_cast<int>(y_) + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo
|
||||
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
|
||||
if (LY > -1)
|
||||
{
|
||||
if (LY > -1) {
|
||||
y_ = LY - HEIGHT_;
|
||||
}
|
||||
}
|
||||
|
||||
// Si está bajando la rampa, recoloca al jugador
|
||||
if (isOnDownSlope() && state_ != PlayerState::JUMPING)
|
||||
{
|
||||
if (isOnDownSlope() && state_ != PlayerState::JUMPING) {
|
||||
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 = static_cast<int>(x_) + WIDTH_;
|
||||
proj.y = static_cast<int>(y_);
|
||||
proj.h = HEIGHT_;
|
||||
proj.w = ceil(vx_); // Para evitar que tenga un ancho de 0 pixels
|
||||
proj.w = ceil(vx_); // Para evitar que tenga un ancho de 0 pixels
|
||||
|
||||
#ifdef DEBUG
|
||||
debug_rect_x_ = proj;
|
||||
@@ -324,38 +282,31 @@ void Player::move()
|
||||
const int POS = room_->checkLeftSurfaces(&proj);
|
||||
|
||||
// Calcula la nueva posición
|
||||
if (POS == -1)
|
||||
{
|
||||
if (POS == -1) {
|
||||
// Si no hay colisión
|
||||
x_ += vx_;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Si hay colisión lo mueve hasta donde no colisiona
|
||||
x_ = POS - WIDTH_;
|
||||
}
|
||||
|
||||
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
|
||||
if (state_ != PlayerState::JUMPING)
|
||||
{
|
||||
const LineVertical RIGHT_SIDE = {static_cast<int>(x_) + WIDTH_ - 1, static_cast<int>(y_) + HEIGHT_ - 2, static_cast<int>(y_) + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo
|
||||
if (state_ != PlayerState::JUMPING) {
|
||||
const LineVertical RIGHT_SIDE = {static_cast<int>(x_) + WIDTH_ - 1, static_cast<int>(y_) + HEIGHT_ - 2, static_cast<int>(y_) + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo
|
||||
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
|
||||
if (RY > -1)
|
||||
{
|
||||
if (RY > -1) {
|
||||
y_ = RY - HEIGHT_;
|
||||
}
|
||||
}
|
||||
|
||||
// Si está bajando la rampa, recoloca al jugador
|
||||
if (isOnDownSlope() && state_ != PlayerState::JUMPING)
|
||||
{
|
||||
if (isOnDownSlope() && state_ != PlayerState::JUMPING) {
|
||||
y_ += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Si ha salido del suelo, el jugador cae
|
||||
if (state_ == PlayerState::STANDING && !isOnFloor())
|
||||
{
|
||||
if (state_ == PlayerState::STANDING && !isOnFloor()) {
|
||||
setState(PlayerState::FALLING);
|
||||
|
||||
// Deja de estar enganchado a la superficie automatica
|
||||
@@ -363,20 +314,18 @@ void Player::move()
|
||||
}
|
||||
|
||||
// Si ha salido de una superficie automatica, detiene el movimiento automatico
|
||||
if (state_ == PlayerState::STANDING && isOnFloor() && !isOnAutoSurface())
|
||||
{
|
||||
if (state_ == PlayerState::STANDING && isOnFloor() && !isOnAutoSurface()) {
|
||||
// Deja de estar enganchado a la superficie automatica
|
||||
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 = static_cast<int>(x_);
|
||||
proj.y = static_cast<int>(y_ + vy_);
|
||||
proj.h = static_cast<int>(std::ceil(std::fabs(vy_))); // Para evitar que tenga una altura de 0 pixels
|
||||
proj.h = static_cast<int>(std::ceil(std::fabs(vy_))); // Para evitar que tenga una altura de 0 pixels
|
||||
proj.w = WIDTH_;
|
||||
|
||||
#ifdef DEBUG
|
||||
@@ -387,13 +336,10 @@ void Player::move()
|
||||
const int POS = room_->checkBottomSurfaces(&proj);
|
||||
|
||||
// Calcula la nueva posición
|
||||
if (POS == -1)
|
||||
{
|
||||
if (POS == -1) {
|
||||
// Si no hay colisión
|
||||
y_ += vy_;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Si hay colisión lo mueve hasta donde no colisiona y entra en caída
|
||||
y_ = POS + 1;
|
||||
setState(PlayerState::FALLING);
|
||||
@@ -401,13 +347,12 @@ void Player::move()
|
||||
}
|
||||
|
||||
// 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 = static_cast<int>(x_);
|
||||
proj.y = static_cast<int>(y_) + HEIGHT_;
|
||||
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
|
||||
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
|
||||
proj.w = WIDTH_;
|
||||
|
||||
#ifdef DEBUG
|
||||
@@ -416,25 +361,20 @@ 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)
|
||||
{
|
||||
if (POS > -1) {
|
||||
// Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre la superficie
|
||||
y_ = POS - HEIGHT_;
|
||||
setState(PlayerState::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
|
||||
if (state_ != PlayerState::JUMPING)
|
||||
{ // Las rampas no se miran si se está saltando
|
||||
if (state_ != PlayerState::JUMPING) { // Las rampas no se miran si se está saltando
|
||||
const LineVertical LEFT_SIDE = {proj.x, proj.y, proj.y + proj.h - 1};
|
||||
const LineVertical RIGHT_SIDE = {proj.x + proj.w - 1, proj.y, proj.y + proj.h - 1};
|
||||
const int 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
|
||||
// Calcula la nueva posición
|
||||
y_ = POINT - HEIGHT_;
|
||||
@@ -443,9 +383,7 @@ void Player::move()
|
||||
debug_color_ = static_cast<Uint8>(PaletteColor::YELLOW);
|
||||
debug_point_ = {(int)x_ + (WIDTH_ / 2), POINT};
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// No está saltando y no hay colisón con una rampa
|
||||
// Calcula la nueva posición
|
||||
y_ += vy_;
|
||||
@@ -453,9 +391,7 @@ void Player::move()
|
||||
debug_color_ = static_cast<Uint8>(PaletteColor::RED);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Esta saltando y no hay colisión con los muros
|
||||
// Calcula la nueva posición
|
||||
y_ += vy_;
|
||||
@@ -463,8 +399,8 @@ void Player::move()
|
||||
}
|
||||
}
|
||||
|
||||
placeSprite(); // Coloca el sprite en la nueva posición
|
||||
collider_box_ = getRect(); // Actualiza el rectangulo de colisió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));
|
||||
@@ -473,23 +409,17 @@ void Player::move()
|
||||
}
|
||||
|
||||
// Establece la animación del jugador
|
||||
void Player::animate()
|
||||
{
|
||||
if (vx_ != 0)
|
||||
{
|
||||
void Player::animate() {
|
||||
if (vx_ != 0) {
|
||||
sprite_->update();
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si ha finalizado el salto al alcanzar la altura de inicio
|
||||
void Player::checkJumpEnd()
|
||||
{
|
||||
if (state_ == PlayerState::JUMPING)
|
||||
{
|
||||
if (vy_ > 0)
|
||||
{
|
||||
if (y_ >= jump_init_pos_)
|
||||
{
|
||||
void Player::checkJumpEnd() {
|
||||
if (state_ == PlayerState::JUMPING) {
|
||||
if (vy_ > 0) {
|
||||
if (y_ >= jump_init_pos_) {
|
||||
// Si alcanza la altura de salto inicial, pasa al estado de caída
|
||||
setState(PlayerState::FALLING);
|
||||
vy_ = MAX_VY_;
|
||||
@@ -500,10 +430,8 @@ void Player::checkJumpEnd()
|
||||
}
|
||||
|
||||
// Calcula y reproduce el sonido de salto
|
||||
void Player::playJumpSound()
|
||||
{
|
||||
if (jumping_counter_ % 4 == 0)
|
||||
{
|
||||
void Player::playJumpSound() {
|
||||
if (jumping_counter_ % 4 == 0) {
|
||||
JA_PlaySound(jumping_sound_[jumping_counter_ / 4]);
|
||||
}
|
||||
|
||||
@@ -513,10 +441,8 @@ void Player::playJumpSound()
|
||||
}
|
||||
|
||||
// Calcula y reproduce el sonido de caer
|
||||
void Player::playFallSound()
|
||||
{
|
||||
if (falling_counter_ % 4 == 0)
|
||||
{
|
||||
void Player::playFallSound() {
|
||||
if (falling_counter_ % 4 == 0) {
|
||||
JA_PlaySound(falling_sound_[std::min((falling_counter_ / 4), (int)falling_sound_.size() - 1)]);
|
||||
}
|
||||
|
||||
@@ -526,8 +452,7 @@ void Player::playFallSound()
|
||||
}
|
||||
|
||||
// Comprueba si el jugador tiene suelo debajo de los pies
|
||||
bool Player::isOnFloor()
|
||||
{
|
||||
bool Player::isOnFloor() {
|
||||
bool on_floor = false;
|
||||
bool on_slope_l = false;
|
||||
bool on_slope_r = false;
|
||||
@@ -535,8 +460,7 @@ bool Player::isOnFloor()
|
||||
updateFeet();
|
||||
|
||||
// Comprueba las superficies
|
||||
for (auto f : under_feet_)
|
||||
{
|
||||
for (auto f : under_feet_) {
|
||||
on_floor |= room_->checkTopSurfaces(&f);
|
||||
on_floor |= room_->checkAutoSurfaces(&f);
|
||||
}
|
||||
@@ -546,18 +470,15 @@ bool Player::isOnFloor()
|
||||
on_slope_r = room_->checkRightSlopes(&under_feet_[1]);
|
||||
|
||||
#ifdef DEBUG
|
||||
if (on_floor)
|
||||
{
|
||||
if (on_floor) {
|
||||
Debug::get()->add("ON_FLOOR");
|
||||
}
|
||||
|
||||
if (on_slope_l)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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
|
||||
@@ -566,21 +487,18 @@ bool Player::isOnFloor()
|
||||
}
|
||||
|
||||
// Comprueba si el jugador esta sobre una superficie automática
|
||||
bool Player::isOnAutoSurface()
|
||||
{
|
||||
bool Player::isOnAutoSurface() {
|
||||
bool on_auto_surface = false;
|
||||
|
||||
updateFeet();
|
||||
|
||||
// Comprueba las superficies
|
||||
for (auto f : under_feet_)
|
||||
{
|
||||
for (auto f : under_feet_) {
|
||||
on_auto_surface |= room_->checkAutoSurfaces(&f);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
if (on_auto_surface)
|
||||
{
|
||||
if (on_auto_surface) {
|
||||
Debug::get()->add("ON_AUTO_SURFACE");
|
||||
}
|
||||
#endif
|
||||
@@ -589,8 +507,7 @@ bool Player::isOnAutoSurface()
|
||||
}
|
||||
|
||||
// Comprueba si el jugador está sobre una rampa hacia abajo
|
||||
bool Player::isOnDownSlope()
|
||||
{
|
||||
bool Player::isOnDownSlope() {
|
||||
bool on_slope = false;
|
||||
|
||||
updateFeet();
|
||||
@@ -605,8 +522,7 @@ bool Player::isOnDownSlope()
|
||||
on_slope |= room_->checkRightSlopes(&under_feet_[1]);
|
||||
|
||||
#ifdef DEBUG
|
||||
if (on_slope)
|
||||
{
|
||||
if (on_slope) {
|
||||
Debug::get()->add("ON_DOWN_SLOPE");
|
||||
}
|
||||
#endif
|
||||
@@ -615,44 +531,34 @@ bool Player::isOnDownSlope()
|
||||
}
|
||||
|
||||
// Comprueba que el jugador no toque ningun tile de los que matan
|
||||
bool Player::checkKillingTiles()
|
||||
{
|
||||
bool Player::checkKillingTiles() {
|
||||
// Actualiza los puntos de colisión
|
||||
updateColliderPoints();
|
||||
|
||||
// Comprueba si hay contacto y retorna en cuanto se encuentra colisión
|
||||
for (const auto &c : collider_points_)
|
||||
{
|
||||
if (room_->getTile(c) == TileType::KILL)
|
||||
{
|
||||
is_alive_ = false; // Mata al jugador inmediatamente
|
||||
return true; // Retorna en cuanto se detecta una colisión
|
||||
for (const auto& c : collider_points_) {
|
||||
if (room_->getTile(c) == TileType::KILL) {
|
||||
is_alive_ = false; // Mata al jugador inmediatamente
|
||||
return true; // Retorna en cuanto se detecta una colisión
|
||||
}
|
||||
}
|
||||
|
||||
return false; // No se encontró ninguna colisión
|
||||
return false; // No se encontró ninguna colisión
|
||||
}
|
||||
|
||||
// Establece el color del jugador
|
||||
void Player::setColor()
|
||||
{
|
||||
if (options.cheats.invincible == Cheat::CheatState::ENABLED)
|
||||
{
|
||||
void Player::setColor() {
|
||||
if (options.cheats.invincible == Cheat::CheatState::ENABLED) {
|
||||
color_ = static_cast<Uint8>(PaletteColor::CYAN);
|
||||
}
|
||||
else if (options.cheats.infinite_lives == Cheat::CheatState::ENABLED)
|
||||
{
|
||||
} else if (options.cheats.infinite_lives == Cheat::CheatState::ENABLED) {
|
||||
color_ = static_cast<Uint8>(PaletteColor::YELLOW);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
color_ = static_cast<Uint8>(PaletteColor::WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los puntos de colisión
|
||||
void Player::updateColliderPoints()
|
||||
{
|
||||
void Player::updateColliderPoints() {
|
||||
const SDL_Rect rect = getRect();
|
||||
collider_points_[0] = {rect.x, rect.y};
|
||||
collider_points_[1] = {rect.x + 7, rect.y};
|
||||
@@ -665,8 +571,7 @@ void Player::updateColliderPoints()
|
||||
}
|
||||
|
||||
// Actualiza los puntos de los pies
|
||||
void Player::updateFeet()
|
||||
{
|
||||
void Player::updateFeet() {
|
||||
const SDL_Point p = {static_cast<int>(x_), static_cast<int>(y_)};
|
||||
|
||||
under_feet_[0] = {p.x, p.y + HEIGHT_};
|
||||
@@ -677,8 +582,7 @@ void Player::updateFeet()
|
||||
}
|
||||
|
||||
// Cambia el estado del jugador
|
||||
void Player::setState(PlayerState value)
|
||||
{
|
||||
void Player::setState(PlayerState value) {
|
||||
previous_state_ = state_;
|
||||
state_ = value;
|
||||
|
||||
@@ -686,26 +590,22 @@ void Player::setState(PlayerState value)
|
||||
}
|
||||
|
||||
// Inicializa los sonidos de salto y caida
|
||||
void Player::initSounds()
|
||||
{
|
||||
void Player::initSounds() {
|
||||
jumping_sound_.clear();
|
||||
falling_sound_.clear();
|
||||
|
||||
for (int i = 1; i <= 24; ++i)
|
||||
{
|
||||
for (int i = 1; i <= 24; ++i) {
|
||||
std::string soundFile = "jump" + std::to_string(i) + ".wav";
|
||||
jumping_sound_.push_back(Resource::get()->getSound(soundFile));
|
||||
|
||||
if (i >= 11)
|
||||
{
|
||||
if (i >= 11) {
|
||||
falling_sound_.push_back(Resource::get()->getSound(soundFile));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica los valores de spawn al jugador
|
||||
void Player::applySpawnValues(const PlayerSpawn &spawn)
|
||||
{
|
||||
void Player::applySpawnValues(const PlayerSpawn& spawn) {
|
||||
x_ = spawn.x;
|
||||
y_ = spawn.y;
|
||||
vx_ = spawn.vx;
|
||||
@@ -716,8 +616,7 @@ void Player::applySpawnValues(const PlayerSpawn &spawn)
|
||||
}
|
||||
|
||||
// Inicializa el sprite del jugador
|
||||
void Player::initSprite(const std::string &surface_path, const std::string &animations_path)
|
||||
{
|
||||
void Player::initSprite(const std::string& surface_path, const std::string& animations_path) {
|
||||
auto surface = Resource::get()->getSurface(surface_path);
|
||||
auto animations = Resource::get()->getAnimations(animations_path);
|
||||
|
||||
@@ -729,27 +628,23 @@ void Player::initSprite(const std::string &surface_path, const std::string &anim
|
||||
|
||||
#ifdef DEBUG
|
||||
// Pinta la información de debug del jugador
|
||||
void Player::renderDebugInfo()
|
||||
{
|
||||
if (Debug::get()->getEnabled())
|
||||
{
|
||||
auto surface = Screen::get()->getRendererSurface();
|
||||
void Player::renderDebugInfo() {
|
||||
if (Debug::get()->getEnabled()) {
|
||||
auto surface = Screen::get()->getRendererSurface();
|
||||
|
||||
// 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));
|
||||
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_Rect rect = getRect();
|
||||
surface->drawRectBorder(&rect, static_cast<Uint8>(PaletteColor::BRIGHT_CYAN));
|
||||
|
||||
// Pinta el rectangulo de movimiento
|
||||
if (vx_ != 0.0f)
|
||||
{
|
||||
if (vx_ != 0.0f) {
|
||||
surface->fillRect(&debug_rect_x_, static_cast<Uint8>(PaletteColor::BRIGHT_RED));
|
||||
}
|
||||
if (vy_ != 0.0f)
|
||||
{
|
||||
if (vy_ != 0.0f) {
|
||||
surface->fillRect(&debug_rect_y_, static_cast<Uint8>(PaletteColor::BRIGHT_RED));
|
||||
}
|
||||
|
||||
@@ -757,4 +652,4 @@ void Player::renderDebugInfo()
|
||||
surface->putPixel(debug_point_.x, debug_point_.y, rand() % 16);
|
||||
}
|
||||
}
|
||||
#endif // DEBUG
|
||||
#endif // DEBUG
|
||||
Reference in New Issue
Block a user