Añadidas físicas al movimiento y salto
This commit is contained in:
@@ -18,7 +18,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
|
||||
sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani"));
|
||||
|
||||
x = 3 * 16;
|
||||
y = 40;
|
||||
y = 168;
|
||||
vx = 0;
|
||||
vy = 0;
|
||||
const SDL_Rect rect = {(int)x, (int)y, 16, 24};
|
||||
@@ -26,18 +26,25 @@ 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;
|
||||
can_jump = true;
|
||||
accelX = 0.12f;
|
||||
maxVX = 1.5f;
|
||||
maxVY = 4.0f;
|
||||
|
||||
jumping = false;
|
||||
jumpPressed = false;
|
||||
standing = true;
|
||||
invulnerable = true;
|
||||
jumpforce = 10;
|
||||
invulnerable = false;
|
||||
enabled = true;
|
||||
cooldown = 0;
|
||||
lifes = 10;
|
||||
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});
|
||||
underFeet.insert(underFeet.end(), {p, p});
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -59,6 +66,7 @@ void Player::update()
|
||||
checkInput();
|
||||
addGravity();
|
||||
move();
|
||||
animate();
|
||||
}
|
||||
|
||||
// Dibuja el objeto
|
||||
@@ -70,36 +78,59 @@ void Player::render()
|
||||
// Comprueba las entradas y modifica variables
|
||||
void Player::checkInput()
|
||||
{
|
||||
const float speed = 1.0f;
|
||||
// Solo comprueba las entradas de dirección cuando está de pie
|
||||
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
|
||||
{
|
||||
vx = -speed;
|
||||
vx = std::max(vx -= accelX, -maxVX);
|
||||
sprite->setFlip(SDL_FLIP_NONE);
|
||||
sprite->setCurrentAnimation("walk");
|
||||
}
|
||||
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
|
||||
{
|
||||
vx = speed;
|
||||
vx = std::min(vx += accelX, maxVX);
|
||||
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
sprite->setCurrentAnimation("walk");
|
||||
}
|
||||
else
|
||||
{
|
||||
vx = 0;
|
||||
sprite->setCurrentAnimation("stand");
|
||||
if (vx > 0.0f)
|
||||
{
|
||||
vx = std::max(vx -= accelX, 0.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
vx = std::min(vx += accelX, 0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
if (input->checkInput(INPUT_UP, REPEAT_FALSE))
|
||||
if (input->checkInput(INPUT_UP, REPEAT_TRUE))
|
||||
{
|
||||
vy = -5.0f;
|
||||
if (!jumping)
|
||||
{
|
||||
if (!jumpPressed)
|
||||
{
|
||||
jumpStrenght = 2.0f;
|
||||
vy -= jumpStrenght;
|
||||
jumping = true;
|
||||
jumpPressed = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jumpStrenght = std::max(jumpStrenght -= 0.4f, 0.0f);
|
||||
vy -= jumpStrenght;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jumpPressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Aplica la gravedad
|
||||
void Player::addGravity()
|
||||
{
|
||||
vy = std::min(vy += gravity, 2.0f);
|
||||
if (!isOnFloor())
|
||||
{
|
||||
vy = std::min(vy += gravity, maxVY);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los puntos de colisión
|
||||
@@ -113,6 +144,9 @@ void Player::updateColliders()
|
||||
collider[3] = {p.x + 15, p.y};
|
||||
collider[4] = {p.x + 15, p.y + 12};
|
||||
collider[5] = {p.x + 15, p.y + 23};
|
||||
|
||||
underFeet[0] = {p.x, p.y + 24};
|
||||
underFeet[1] = {p.x + 15, p.y + 24};
|
||||
}
|
||||
|
||||
// Compruena las colisiones con el mapa
|
||||
@@ -133,24 +167,39 @@ bool Player::checkMapCollisions()
|
||||
// Mueve al jugador en función de la velocidad/desplazamiento
|
||||
void Player::move()
|
||||
{
|
||||
const float old_x = x;
|
||||
x += vx;
|
||||
if (checkMapCollisions())
|
||||
{
|
||||
x = old_x;
|
||||
if (vx > 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
x--;
|
||||
} while (checkMapCollisions());
|
||||
}
|
||||
else
|
||||
{
|
||||
do
|
||||
{
|
||||
x++;
|
||||
} while (checkMapCollisions());
|
||||
}
|
||||
|
||||
vx = 0.0f;
|
||||
}
|
||||
|
||||
// const float old_y = y;
|
||||
y += vy;
|
||||
if (checkMapCollisions())
|
||||
{
|
||||
// y = old_y;
|
||||
if (vy > 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
y--;
|
||||
} while (checkMapCollisions());
|
||||
jumping = false;
|
||||
vy = 0.0f;
|
||||
jumpStrenght = 2.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -159,11 +208,44 @@ void Player::move()
|
||||
y++;
|
||||
} while (checkMapCollisions());
|
||||
}
|
||||
|
||||
vy = 0;
|
||||
}
|
||||
|
||||
sprite->setPosX(x);
|
||||
sprite->setPosY(y);
|
||||
sprite->update();
|
||||
}
|
||||
|
||||
// Anima al jugador
|
||||
void Player::animate()
|
||||
{
|
||||
if (jumping)
|
||||
{
|
||||
sprite->setCurrentAnimation("jump");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (abs(vx) < 1.00f)
|
||||
{
|
||||
sprite->setCurrentAnimation("stand");
|
||||
}
|
||||
else
|
||||
{
|
||||
sprite->setCurrentAnimation("walk");
|
||||
}
|
||||
}
|
||||
|
||||
sprite->animate();
|
||||
}
|
||||
|
||||
// Comprueba si el jugador tiene suelo debajo de los pies
|
||||
bool Player::isOnFloor()
|
||||
{
|
||||
bool onFloor = false;
|
||||
|
||||
updateColliders();
|
||||
|
||||
for (auto f : underFeet)
|
||||
{
|
||||
onFloor |= (map->getTile(f) == wall);
|
||||
}
|
||||
return onFloor;
|
||||
}
|
||||
Reference in New Issue
Block a user