Ya se engancha verticalmente en las plataformas moviles

This commit is contained in:
2022-08-27 07:55:06 +02:00
parent 6b7c49aecd
commit 43c10d335b
4 changed files with 63 additions and 47 deletions

View File

@@ -24,8 +24,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
vx = 0;
vy = 0;
lastPosition = {(int)x, (int)y};
const SDL_Rect rect = {(int)x, (int)y, w, h};
sprite->setRect(rect);
sprite->setRect({(int)x, (int)y, w, h});
sprite->setCurrentAnimation("stand");
sprite->setFlip(SDL_FLIP_NONE);
@@ -37,15 +36,11 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
state = standing;
jumpPressed = false;
invulnerable = false;
enabled = true;
cooldown = 0;
lives = 10;
coins = 0;
key.insert(key.end(), {0, 0, 0, 0, 0, 0});
const SDL_Point p = {0, 0};
collider.insert(collider.end(), {p, p, p, p, p, p, p, p, p, p, p, p});
underFeet.insert(underFeet.end(), {p, p, p});
hookedOnMovingPlatform = -1;
}
// Destructor
@@ -123,6 +118,9 @@ void Player::checkInput()
}
jumpPressed = true;
// Si salta sale de la plataforma movil
hookedOnMovingPlatform = -1;
}
else
{
@@ -255,19 +253,36 @@ void Player::move()
vy = 0.0f;
y -= ((int)y + h) % tileSize;
}
// Tiene uno de los pies sobre una plataforma móvil
else if (isOnMovingPlatform())
{
state = standing;
vy = 0.0f;
y = SDKJSGHK - h-1;
}
// Tiene ambos pies sobre el vacío
else
{
state = falling;
}
}
// Si cayendo toca una plataforma movil
if (vy >= 0.0f)
{
if (hookedOnMovingPlatform == -1)
{
if (isOnMovingPlatform())
{
// Detener la caída y alinearlo con la plataforma
state = standing;
vy = 0.0f;
y = -h + map->getActorCollider(hookedOnMovingPlatform);
}
}
}
// Si está enganchado a una plataforma movil
if (hookedOnMovingPlatform != -1)
{
// Dejarlo alineado con la plataforma
state = standing;
vy = 0.0f;
y = -h + map->getActorCollider(hookedOnMovingPlatform);
}
}
// Actualiza la posición del sprite
@@ -280,7 +295,10 @@ void Player::animate()
{
if (state != standing)
{
sprite->setCurrentAnimation("jump");
// if (abs(vy) > 1.0f)
{
sprite->setCurrentAnimation("jump");
}
}
else
{
@@ -315,17 +333,13 @@ bool Player::isOnFloor()
bool Player::isOnMovingPlatform()
{
bool onMovingPlatform = false;
SDKJSGHK = 0;
updateFeet();
for (auto f : underFeet)
{
onMovingPlatform |= (map->getActorName(map->actorCollision(f)) == a_moving_platform);
if (onMovingPlatform)
{
SDKJSGHK = map->getActorCollider(map->actorCollision(f));
}
hookedOnMovingPlatform = std::max(hookedOnMovingPlatform,(map->actorCollision(f)));
}
return onMovingPlatform;
}