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

@@ -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)
{
jumpStrenght = std::max(jumpStrenght -= 0.4f, 0.0f);
vy -= jumpStrenght;
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");
}