Añadidas físicas al movimiento y salto
This commit is contained in:
@@ -19,8 +19,8 @@ frames=8,9,10,10,9,8,11,12,13,13,14,15
|
|||||||
[animation]
|
[animation]
|
||||||
name=jump
|
name=jump
|
||||||
speed=10
|
speed=10
|
||||||
loop=yes
|
loop=no
|
||||||
frames=16
|
frames=16,17,18,17,16
|
||||||
[/animation]
|
[/animation]
|
||||||
|
|
||||||
[animation]
|
[animation]
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 7.3 KiB After Width: | Height: | Size: 7.6 KiB |
@@ -116,6 +116,12 @@ void Game::renderDebugInfo()
|
|||||||
text = std::to_string((int)player->sprite->getPosX()) + "," + std::to_string((int)player->sprite->getPosY());
|
text = std::to_string((int)player->sprite->getPosX()) + "," + std::to_string((int)player->sprite->getPosY());
|
||||||
debugText->write(0, line, text, -1);
|
debugText->write(0, line, text, -1);
|
||||||
|
|
||||||
//text = std::to_string(player->checkMapCollisions());
|
text = "VY " + std::to_string(player->vy) + " " + std::to_string(player->jumpStrenght);
|
||||||
//debugText->write(0, line+=6, text, -1);
|
debugText->write(0, line+=6, text, -1);
|
||||||
|
|
||||||
|
text = "VX " + std::to_string(player->vx);
|
||||||
|
debugText->write(0, line+=6, text, -1);
|
||||||
|
|
||||||
|
text = "jump_pressed " + std::to_string(player->jumpPressed);
|
||||||
|
debugText->write(0, line+=6, text, -1);
|
||||||
}
|
}
|
||||||
@@ -18,7 +18,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
|
|||||||
sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani"));
|
sprite = new AnimatedSprite(texture, renderer, asset->get("player.ani"));
|
||||||
|
|
||||||
x = 3 * 16;
|
x = 3 * 16;
|
||||||
y = 40;
|
y = 168;
|
||||||
vx = 0;
|
vx = 0;
|
||||||
vy = 0;
|
vy = 0;
|
||||||
const SDL_Rect rect = {(int)x, (int)y, 16, 24};
|
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->setCurrentAnimation("stand");
|
||||||
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
||||||
|
|
||||||
|
// jumpStrenght = -5.6f;
|
||||||
|
jumpStrenght = 2.0f;
|
||||||
gravity = 0.3f;
|
gravity = 0.3f;
|
||||||
can_jump = true;
|
accelX = 0.12f;
|
||||||
|
maxVX = 1.5f;
|
||||||
|
maxVY = 4.0f;
|
||||||
|
|
||||||
|
jumping = false;
|
||||||
|
jumpPressed = false;
|
||||||
standing = true;
|
standing = true;
|
||||||
invulnerable = true;
|
invulnerable = false;
|
||||||
jumpforce = 10;
|
|
||||||
enabled = true;
|
enabled = true;
|
||||||
cooldown = 0;
|
cooldown = 0;
|
||||||
lifes = 10;
|
lives = 10;
|
||||||
coins = 0;
|
coins = 0;
|
||||||
key.insert(key.end(), {0, 0, 0, 0, 0, 0});
|
key.insert(key.end(), {0, 0, 0, 0, 0, 0});
|
||||||
const SDL_Point p = {0, 0};
|
const SDL_Point p = {0, 0};
|
||||||
collider.insert(collider.end(), {p, p, p, p, p, p});
|
collider.insert(collider.end(), {p, p, p, p, p, p});
|
||||||
|
underFeet.insert(underFeet.end(), {p, p});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
@@ -59,6 +66,7 @@ void Player::update()
|
|||||||
checkInput();
|
checkInput();
|
||||||
addGravity();
|
addGravity();
|
||||||
move();
|
move();
|
||||||
|
animate();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuja el objeto
|
// Dibuja el objeto
|
||||||
@@ -70,36 +78,59 @@ void Player::render()
|
|||||||
// Comprueba las entradas y modifica variables
|
// Comprueba las entradas y modifica variables
|
||||||
void Player::checkInput()
|
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))
|
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
|
||||||
{
|
{
|
||||||
vx = -speed;
|
vx = std::max(vx -= accelX, -maxVX);
|
||||||
sprite->setFlip(SDL_FLIP_NONE);
|
sprite->setFlip(SDL_FLIP_NONE);
|
||||||
sprite->setCurrentAnimation("walk");
|
|
||||||
}
|
}
|
||||||
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
|
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
|
||||||
{
|
{
|
||||||
vx = speed;
|
vx = std::min(vx += accelX, maxVX);
|
||||||
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
||||||
sprite->setCurrentAnimation("walk");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vx = 0;
|
if (vx > 0.0f)
|
||||||
sprite->setCurrentAnimation("stand");
|
{
|
||||||
|
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
|
// Aplica la gravedad
|
||||||
void Player::addGravity()
|
void Player::addGravity()
|
||||||
{
|
{
|
||||||
vy = std::min(vy += gravity, 2.0f);
|
if (!isOnFloor())
|
||||||
|
{
|
||||||
|
vy = std::min(vy += gravity, maxVY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualiza los puntos de colisión
|
// Actualiza los puntos de colisión
|
||||||
@@ -113,6 +144,9 @@ void Player::updateColliders()
|
|||||||
collider[3] = {p.x + 15, p.y};
|
collider[3] = {p.x + 15, p.y};
|
||||||
collider[4] = {p.x + 15, p.y + 12};
|
collider[4] = {p.x + 15, p.y + 12};
|
||||||
collider[5] = {p.x + 15, p.y + 23};
|
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
|
// Compruena las colisiones con el mapa
|
||||||
@@ -133,24 +167,39 @@ bool Player::checkMapCollisions()
|
|||||||
// Mueve al jugador en función de la velocidad/desplazamiento
|
// Mueve al jugador en función de la velocidad/desplazamiento
|
||||||
void Player::move()
|
void Player::move()
|
||||||
{
|
{
|
||||||
const float old_x = x;
|
|
||||||
x += vx;
|
x += vx;
|
||||||
if (checkMapCollisions())
|
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;
|
y += vy;
|
||||||
if (checkMapCollisions())
|
if (checkMapCollisions())
|
||||||
{
|
{
|
||||||
// y = old_y;
|
|
||||||
if (vy > 0)
|
if (vy > 0)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
y--;
|
y--;
|
||||||
} while (checkMapCollisions());
|
} while (checkMapCollisions());
|
||||||
|
jumping = false;
|
||||||
|
vy = 0.0f;
|
||||||
|
jumpStrenght = 2.0f;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -159,11 +208,44 @@ void Player::move()
|
|||||||
y++;
|
y++;
|
||||||
} while (checkMapCollisions());
|
} while (checkMapCollisions());
|
||||||
}
|
}
|
||||||
|
|
||||||
vy = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite->setPosX(x);
|
sprite->setPosX(x);
|
||||||
sprite->setPosY(y);
|
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;
|
||||||
}
|
}
|
||||||
@@ -25,17 +25,25 @@ public:
|
|||||||
float y; // Posición del jugador en el eje Y
|
float y; // Posición del jugador en el eje Y
|
||||||
float vx; // Velocidad/desplazamiento del jugador en el eje X
|
float vx; // Velocidad/desplazamiento del jugador en el eje X
|
||||||
float vy; // Velocidad/desplazamiento del jugador en el eje Y
|
float vy; // Velocidad/desplazamiento del jugador en el eje Y
|
||||||
bool can_jump; // Si puede saltar
|
bool jumping; // Indica si se encuentra saltando
|
||||||
|
bool jumpPressed; // Indica si esta pulsada la tecla de salto
|
||||||
bool enabled; // Si está habilitado
|
bool enabled; // Si está habilitado
|
||||||
bool standing; // Si esta de pie (o quieto?)
|
bool standing; // Si esta de pie (o quieto?)
|
||||||
bool invulnerable; // Si es invulnerable
|
bool invulnerable; // Indica si se encuentra en estado invulnerable
|
||||||
int coins; // Cantidad de monedas
|
int coins; // Cantidad de monedas
|
||||||
int cooldown; // Tiempo de inhabilitación
|
int cooldown; // Tiempo de inhabilitación
|
||||||
int jumpforce; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
|
int lives; // Cantidad de vidas
|
||||||
int lifes; // Cantidad de vidas
|
|
||||||
|
// Variables que afectan a la inercia del movimiento
|
||||||
|
float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
|
||||||
float gravity; // Gravedad
|
float gravity; // Gravedad
|
||||||
|
float accelX; // Aceleración al desplazarse horizontalmente
|
||||||
|
float maxVX; // Velocidad mazima de desplazamiento horizontal
|
||||||
|
float maxVY; // Velocidad mazima de desplazamiento vertical
|
||||||
|
|
||||||
std::vector<bool> key; // Indica las llaves que posee el jugador
|
std::vector<bool> key; // Indica las llaves que posee el jugador
|
||||||
std::vector<SDL_Point> collider; // Contiene los puntos de colisión del jugador con el mapa
|
std::vector<SDL_Point> collider; // Contiene los puntos de colisión del jugador con el mapa
|
||||||
|
std::vector<SDL_Point> underFeet; // Contiene los puntos que hay bajo cada pie del jugador
|
||||||
JA_Sound sound_coin; // Sonido al coger monedas
|
JA_Sound sound_coin; // Sonido al coger monedas
|
||||||
JA_Sound sound_death; // Sonido al morir
|
JA_Sound sound_death; // Sonido al morir
|
||||||
JA_Sound sound_jump; // Sonido al saltar
|
JA_Sound sound_jump; // Sonido al saltar
|
||||||
@@ -55,6 +63,12 @@ public:
|
|||||||
// Mueve al jugador en función de la velocidad/desplazamiento
|
// Mueve al jugador en función de la velocidad/desplazamiento
|
||||||
void move();
|
void move();
|
||||||
|
|
||||||
|
// Anima al jugador
|
||||||
|
void animate();
|
||||||
|
|
||||||
|
// Comprueba si el jugador tiene suelo debajo de los pies
|
||||||
|
bool isOnFloor();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map);
|
Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map);
|
||||||
|
|||||||
Reference in New Issue
Block a user