Aladidos estados al jugador para mejorar el control sobre el salto y las caídas

This commit is contained in:
2022-08-19 13:21:23 +02:00
parent 1882f57c57
commit 5144a14bcd
3 changed files with 45 additions and 27 deletions

View File

@@ -138,4 +138,7 @@ void Game::renderDebugInfo()
text = "getTile " + std::to_string(player->map->getTile(player->collider[0]));
debugText->write(0, line += 6, text, -1);
text = "state " + std::to_string(player->state);
debugText->write(0, line += 6, text, -1);
}

View File

@@ -28,16 +28,14 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
sprite->setCurrentAnimation("stand");
sprite->setFlip(SDL_FLIP_HORIZONTAL);
// jumpStrenght = -5.6f;
jumpStrenght = 2.0f;
gravity = 0.3f;
accelX = 0.12f;
maxVX = 1.5f;
maxVY = 4.0f;
jumping = false;
state = standing;
jumpPressed = false;
standing = true;
invulnerable = false;
enabled = true;
cooldown = 0;
@@ -104,26 +102,34 @@ void Player::checkInput()
if (input->checkInput(INPUT_UP, REPEAT_TRUE))
{
if (!jumping)
if (state == standing)
{
if (!jumpPressed)
{
jumpStrenght = 2.0f;
vy -= jumpStrenght;
jumping = true;
state = jumping;
jumpPressed = true;
}
}
else
else if (state == jumping)
{
if (jumpPressed)
{
jumpStrenght = std::max(jumpStrenght -= 0.4f, 0.0f);
vy -= jumpStrenght;
}
}
jumpPressed = true;
}
else
{
jumpPressed = false;
if (state == jumping)
{
state = falling;
}
}
}
@@ -133,7 +139,10 @@ void Player::addGravity()
if (!isOnFloor())
{
vy = std::min(vy += gravity, maxVY);
jumping = true;
if (state == standing)
{
state = falling;
}
}
}
@@ -215,18 +224,18 @@ void Player::move()
if (vy > 0.0f)
{
y -= ((int)y + h) % tile;
state = standing;
}
else
{
y += tile - ((int)y % tile);
state = falling;
}
jumping = false;
vy = 0.0f;
}
else if ((!wasOnFloor) && (isOnFloor()) && (vy > 0.0f))
{
jumping = false;
state = standing;
vy = 0.0f;
y -= ((int)y + h) % tile;
}
@@ -238,7 +247,7 @@ void Player::move()
// Anima al jugador
void Player::animate()
{
if (jumping)
if (state != standing)
{
sprite->setCurrentAnimation("jump");
}

View File

@@ -10,6 +10,13 @@
#ifndef PLAYER_H
#define PLAYER_H
enum t_player_state
{
standing,
jumping,
falling
};
// The player
class Player
{
@@ -25,16 +32,15 @@ public:
float y; // Posición del jugador en el eje Y
float vx; // Velocidad/desplazamiento del jugador en el eje X
float vy; // Velocidad/desplazamiento del jugador en el eje Y
bool jumping; // Indica si se encuentra saltando
bool jumpPressed; // Indica si esta pulsada la tecla de salto
bool enabled; // Si está habilitado
bool standing; // Si esta de pie (o quieto?)
bool invulnerable; // Indica si se encuentra en estado invulnerable
int coins; // Cantidad de monedas
int cooldown; // Tiempo de inhabilitación
int lives; // Cantidad de vidas
int w; // Ancho del jugador
int h; // ALto del jugador
t_player_state state; // Estado actual del jugador
// Variables que afectan a la inercia del movimiento
float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar