Terminados los tiles atravesables

This commit is contained in:
2022-08-20 09:15:51 +02:00
parent 8765049b69
commit 7e93b3150f
6 changed files with 102 additions and 58 deletions

View File

@@ -23,6 +23,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
y = 168;
vx = 0;
vy = 0;
lastPosition = {(int)x, (int)y};
const SDL_Rect rect = {(int)x, (int)y, 16, 24};
sprite->setPos(rect);
sprite->setCurrentAnimation("stand");
@@ -64,7 +65,7 @@ Player::~Player()
void Player::update()
{
checkInput();
addGravity();
// addGravity();
move();
animate();
}
@@ -136,13 +137,10 @@ void Player::checkInput()
// Aplica la gravedad
void Player::addGravity()
{
if (!isOnFloor())
// *** Falta ver pq la gravedad empuja al muñeco hacia abajo en los tiles atravesables
if (state != standing)
{
vy = std::min(vy += gravity, maxVY);
if (state == standing)
{
state = falling;
}
}
}
@@ -198,48 +196,74 @@ bool Player::checkMapCollisions()
// Mueve al jugador en función de la velocidad/desplazamiento
void Player::move()
{
const int tile = map->getTileSize();
const int tileSize = map->getTileSize();
lastPosition = {(int)x, (int)y};
addGravity();
// Mueve en el eje X y comprueba colisiones con muros
x += vx;
if (checkMapCollisions())
{
// Recoloca
if (vx > 0)
{
x -= ((int)x + w) % tile;
x -= ((int)x + w) % tileSize;
}
else
{
x += tile - ((int)x % tile);
x += tileSize - ((int)x % tileSize);
}
vx = 0.0f;
}
const bool wasOnFloor = isOnFloor();
// Mueve en el eje Y y comprueba colisiones con muros
y += vy;
if (checkMapCollisions())
{
// Recoloca
if (vy > 0.0f)
{
y -= ((int)y + h) % tile;
y -= ((int)y + h) % tileSize;
state = standing;
}
else
{
y += tile - ((int)y % tile);
y += tileSize - ((int)y % tileSize);
state = falling;
}
vy = 0.0f;
}
else if ((!wasOnFloor) && (isOnFloor()) && (vy > 0.0f))
// Si no hay colisiones con los muros en el eje Y, comprueba no haya atravesado
// el suelo de un tile atravesble
else
{
state = standing;
vy = 0.0f;
y -= ((int)y + h) % tile;
const int a = (lastPosition.y + h) / tileSize;
const int b = ((int)y + h) / tileSize;
const bool tile_change = a != b;
const bool going_down = vy >= 0.0f;
const bool tile_aligned = ((int)y + h) % tileSize == 0;
if (((going_down) && (tile_aligned)) || ((going_down) && (tile_change)))
{
// Tiene uno de los pies sobre una superficie
if (isOnFloor())
{
state = standing;
vy = 0.0f;
y -= ((int)y + h) % tileSize;
}
// Tiene ambos pies sobre el vacío
else
{
state = falling;
}
}
}
// Actualiza la posición del sprite
sprite->setPosX(x);
sprite->setPosY(y);
}
@@ -273,11 +297,6 @@ bool Player::isOnFloor()
updateFeet();
if (underFeet[0].y % map->getTileSize() != 0)
{
return false;
}
for (auto f : underFeet)
{
onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable));