Las plataformas horizontales ya no empujan ni se queda pegado al suelo

This commit is contained in:
2022-08-29 10:48:31 +02:00
parent f9482594ea
commit e6dad36f3b
3 changed files with 57 additions and 25 deletions

View File

@@ -34,7 +34,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
maxVX = 1.5f;
maxVY = 4.0f;
state = standing;
state = s_standing;
jumpPressed = false;
key.insert(key.end(), {0, 0, 0, 0, 0, 0});
const SDL_Point p = {0, 0};
@@ -99,18 +99,19 @@ void Player::checkInput()
if (input->checkInput(INPUT_UP, REPEAT_TRUE))
{
if (state == standing)
if (state == s_standing)
{
if (!jumpPressed)
{
jumpStrenght = 2.0f;
vy -= jumpStrenght;
state = jumping;
state = s_jumping;
isOn = f_none;
jumpPressed = true;
JA_PlaySound(sound_jump);
}
}
else if (state == jumping)
else if (state == s_jumping)
{
if (jumpPressed)
{
@@ -127,9 +128,9 @@ void Player::checkInput()
else
{
jumpPressed = false;
if (state == jumping)
if (state == s_jumping)
{
state = falling;
state = s_falling;
}
}
}
@@ -138,7 +139,7 @@ void Player::checkInput()
void Player::addGravity()
{
// *** Falta ver pq la gravedad empuja al muñeco hacia abajo en los tiles atravesables
if (state != standing)
if (state != s_standing)
{
vy = std::min(vy += gravity, maxVY);
}
@@ -227,12 +228,12 @@ void Player::move()
if (vy > 0.0f)
{
y -= ((int)y + h) % tileSize;
state = standing;
state = s_standing;
}
else
{
y += tileSize - ((int)y % tileSize);
state = falling;
state = s_falling;
}
vy = 0.0f;
}
@@ -253,14 +254,14 @@ void Player::move()
// Tiene uno de los pies sobre una superficie
if (isOnFloor())
{
state = standing;
state = s_standing;
vy = 0.0f;
y -= ((int)y + h) % tileSize;
}
// Tiene ambos pies sobre el vacío
else
{
state = falling;
state = s_falling;
}
}
@@ -269,14 +270,14 @@ void Player::move()
// Si está cayendo
if (going_down2)
{
state = falling;
state = s_falling;
// Si está alineado con el tile mira el suelo (para que no lo mire si está
// dentro de un tile atravesable y lo deje a medias)
if (tile_aligned2)
{
if (isOnFloor())
{
state = standing;
state = s_standing;
}
}
@@ -287,9 +288,9 @@ void Player::move()
if (isOnMovingPlatform())
{
// Detener la caída y alinearlo con la plataforma
state = standing;
state = s_standing;
vy = 0.0f;
y = -h + map->getActorCollider(hookedOnMovingPlatform).y;
y = map->getActorCollider(hookedOnMovingPlatform).y - h;
}
}
}
@@ -298,7 +299,7 @@ void Player::move()
if (hookedOnMovingPlatform != -1)
{
// Dejarlo alineado con la plataforma
state = standing;
state = s_standing;
vy = 0.0f;
y = map->getActorCollider(hookedOnMovingPlatform).y - h;
x += map->getActorIncX(hookedOnMovingPlatform);
@@ -314,12 +315,9 @@ void Player::move()
// Anima al jugador
void Player::animate()
{
if (state != standing)
if (state != s_standing)
{
// if (abs(vy) > 1.0f)
{
sprite->setCurrentAnimation("jump");
}
sprite->setCurrentAnimation("jump");
}
else
{
@@ -339,6 +337,11 @@ void Player::animate()
// Comprueba si el jugador tiene suelo debajo de los pies
bool Player::isOnFloor()
{
if (isOn == f_platform)
{
return false;
}
bool onFloor = false;
updateFeet();
@@ -347,6 +350,16 @@ bool Player::isOnFloor()
{
onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable));
}
if (onFloor)
{
isOn = f_wall;
}
else
{
isOn = f_none;
}
return onFloor;
}
@@ -356,6 +369,12 @@ bool Player::isOnMovingPlatform()
bool onMovingPlatform = false;
hookedOnMovingPlatform = -1;
// Si esta sobre el suelo, no puede estar tambien sobre una plataforma movil
if (isOn == f_wall)
{
return false;
}
updateFeet();
for (auto f : underFeet)
@@ -367,6 +386,11 @@ bool Player::isOnMovingPlatform()
if (!onMovingPlatform)
{
hookedOnMovingPlatform = -1;
isOn = f_none;
}
else
{
isOn = f_platform;
}
return onMovingPlatform;

View File

@@ -14,9 +14,16 @@
enum e_state
{
standing,
jumping,
falling
s_standing,
s_jumping,
s_falling
};
enum e_floor
{
f_none,
f_wall,
f_platform
};
// The player
@@ -40,6 +47,7 @@ public:
e_border border; // Indica en qué borde de la pantalla está el jugador
SDL_Point lastPosition; // Posición anterior
int hookedOnMovingPlatform; // Índice de la plataforma movil a la que está enganchado
e_floor isOn; // Indica sobre que tipo de suelo se encuentra
// Variables que afectan a la inercia del movimiento
bool jumpPressed; // Indica si esta pulsada la tecla de salto