Colisiones básicas terminadas

This commit is contained in:
2022-08-17 17:06:49 +02:00
parent 02c2c1bce2
commit b1c91d2d0d
3 changed files with 44 additions and 12 deletions

View File

@@ -17,7 +17,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani"));
x = 16;
x = 3 * 16;
y = 40;
vx = 0;
vy = 0;
@@ -120,6 +120,8 @@ bool Player::checkMapCollisions()
{
bool collision = false;
updateColliders();
for (auto c : collider)
{
collision |= (map->getTile(c) == wall);
@@ -133,18 +135,32 @@ void Player::move()
{
const float old_x = x;
x += vx;
updateColliders();
if (checkMapCollisions())
{
x = old_x;
}
const float old_y = y;
// const float old_y = y;
y += vy;
updateColliders();
if (checkMapCollisions())
{
y = old_y;
// y = old_y;
if (vy > 0)
{
do
{
y--;
} while (checkMapCollisions());
}
else
{
do
{
y++;
} while (checkMapCollisions());
}
vy = 0;
}
sprite->setPosX(x);