forked from jaildesigner-jailgames/jaildoctors_dilemma
Completadas las colisiones y estados
This commit is contained in:
@@ -24,6 +24,7 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren
|
||||
|
||||
jump_ini = ini.jump_ini;
|
||||
state = ini.state;
|
||||
prevState = state;
|
||||
|
||||
x = ini.x;
|
||||
y = ini.y;
|
||||
@@ -62,6 +63,12 @@ void Player::render()
|
||||
{
|
||||
sprite->getTexture()->setColor(color.r, color.g, color.b);
|
||||
sprite->render();
|
||||
if (debug->getEnabled())
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||
SDL_RenderDrawPoint(renderer, underFeet[0].x, underFeet[0].y);
|
||||
SDL_RenderDrawPoint(renderer, underFeet[1].x, underFeet[1].y);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las variables del objeto
|
||||
@@ -78,34 +85,33 @@ void Player::update()
|
||||
void Player::checkInput()
|
||||
{
|
||||
// Solo comprueba las entradas de dirección cuando está dsobre una superficie
|
||||
if (!isOnFloor())
|
||||
if (state != s_standing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((input->checkInput(INPUT_LEFT, REPEAT_TRUE)) && (state == s_standing))
|
||||
if (input->checkInput(INPUT_LEFT, REPEAT_TRUE))
|
||||
{
|
||||
vx = -0.6f;
|
||||
sprite->setFlip(SDL_FLIP_HORIZONTAL);
|
||||
}
|
||||
else if ((input->checkInput(INPUT_RIGHT, REPEAT_TRUE)) && (state == s_standing))
|
||||
|
||||
else if (input->checkInput(INPUT_RIGHT, REPEAT_TRUE))
|
||||
{
|
||||
vx = 0.6f;
|
||||
sprite->setFlip(SDL_FLIP_NONE);
|
||||
}
|
||||
else if (state == s_standing)
|
||||
|
||||
else
|
||||
{
|
||||
vx = 0.0f;
|
||||
}
|
||||
|
||||
if (input->checkInput(INPUT_UP, REPEAT_TRUE))
|
||||
{
|
||||
if (state == s_standing)
|
||||
{
|
||||
state = s_jumping;
|
||||
vy = -maxVY;
|
||||
jump_ini = y;
|
||||
}
|
||||
setState(s_jumping);
|
||||
vy = -maxVY;
|
||||
jump_ini = y;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +159,7 @@ void Player::checkBorders()
|
||||
// Comprueba el estado del jugador
|
||||
void Player::checkState()
|
||||
{
|
||||
// Actualiza las variables en función del estado
|
||||
if (state == s_falling)
|
||||
{
|
||||
vx = 0.0f;
|
||||
@@ -194,8 +201,9 @@ void Player::applyGravity()
|
||||
{
|
||||
const float gf = 0.035f;
|
||||
|
||||
// La gravedad solo se aplica cuando no está sobre una superficie
|
||||
if (!isOnFloor())
|
||||
// La gravedad solo se aplica cuando el jugador esta saltando
|
||||
// Nunca mientras cae o esta de pie
|
||||
if (state == s_jumping)
|
||||
{
|
||||
vy += gf;
|
||||
if (vy > maxVY)
|
||||
@@ -221,23 +229,18 @@ SDL_Rect &Player::getCollider()
|
||||
// Recalcula la posición del jugador y su animación
|
||||
void Player::move()
|
||||
{
|
||||
const int tileSize = room->getTileSize();
|
||||
lastPosition = {(int)x, (int)y}; // Guarda la posicion actual antes de modificarla
|
||||
applyGravity(); // Aplica gravedad al jugador
|
||||
checkState(); // Comprueba el estado del jugador
|
||||
|
||||
//--------------------------------------------------------------
|
||||
//--- NUEVA DETECCION DE COLISIONES ---
|
||||
//--------------------------------------------------------------
|
||||
|
||||
// Se mueve hacia la izquierda
|
||||
if (vx < 0.0f)
|
||||
{
|
||||
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
||||
SDL_Rect proj;
|
||||
proj.x = lastPosition.x;
|
||||
proj.y = lastPosition.y;
|
||||
proj.h = h;
|
||||
proj.x = (int)x;
|
||||
proj.y = (int)y;
|
||||
proj.h = h - 1;
|
||||
proj.w = (int)vx;
|
||||
|
||||
// Comprueba la colisión
|
||||
@@ -259,9 +262,9 @@ void Player::move()
|
||||
{
|
||||
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
||||
SDL_Rect proj;
|
||||
proj.x = lastPosition.x + w;
|
||||
proj.y = lastPosition.y;
|
||||
proj.h = h;
|
||||
proj.x = (int)x + w;
|
||||
proj.y = (int)y;
|
||||
proj.h = h - 1;
|
||||
proj.w = (int)(vx);
|
||||
|
||||
// Comprueba la colisión
|
||||
@@ -278,15 +281,21 @@ void Player::move()
|
||||
}
|
||||
}
|
||||
|
||||
// Si ha salido del suelo, el jugador cae
|
||||
if (state == s_standing && !isOnFloor())
|
||||
{
|
||||
setState(s_falling);
|
||||
}
|
||||
|
||||
// Se mueve hacia arriba
|
||||
if (vy < 0.0f)
|
||||
{
|
||||
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
||||
SDL_Rect proj;
|
||||
proj.x = lastPosition.x;
|
||||
proj.y = lastPosition.y;
|
||||
proj.x = (int)x;
|
||||
proj.y = (int)y;
|
||||
proj.h = (int)vy;
|
||||
proj.w = w;
|
||||
proj.w = w - 1;
|
||||
|
||||
// Comprueba la colisión
|
||||
const int pos = room->checkBottomSurfaces(&proj);
|
||||
@@ -297,9 +306,9 @@ void Player::move()
|
||||
y += vy;
|
||||
}
|
||||
else
|
||||
{ // Si hay colisión lo coloca donde colisiona
|
||||
{ // Si hay colisión lo coloca donde colisiona y entra en caída
|
||||
y = pos;
|
||||
state = s_falling;
|
||||
setState(s_falling);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,10 +317,10 @@ void Player::move()
|
||||
{
|
||||
// Crea el rectangulo de proyección en el eje X para ver si colisiona
|
||||
SDL_Rect proj;
|
||||
proj.x = lastPosition.x;
|
||||
proj.y = lastPosition.y + h;
|
||||
proj.x = (int)x;
|
||||
proj.y = (int)y + h;
|
||||
proj.h = (int)vy;
|
||||
proj.w = w;
|
||||
proj.w = w - 1;
|
||||
|
||||
// Comprueba la colisión
|
||||
const int pos = room->checkTopSurfaces(&proj);
|
||||
@@ -322,9 +331,9 @@ void Player::move()
|
||||
y += vy;
|
||||
}
|
||||
else
|
||||
{ // Si hay colisión lo coloca donde colisiona
|
||||
{ // Si hay colisión lo coloca donde colisiona y pasa a estar sobre el suelo
|
||||
y = pos - h;
|
||||
state = s_standing;
|
||||
setState(s_standing);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -394,13 +403,13 @@ void Player::move()
|
||||
if (vy > 0.0f)
|
||||
{ // Bajando
|
||||
y -= ((int)y + h) % tileSize;
|
||||
state = s_standing;
|
||||
setState(s_standing);
|
||||
vy = 0.0f;
|
||||
}
|
||||
else
|
||||
{ // Subiendo
|
||||
y += tileSize - ((int)y % tileSize);
|
||||
state = s_falling;
|
||||
setState(s_falling);
|
||||
vy = maxVY;
|
||||
}
|
||||
}
|
||||
@@ -420,7 +429,7 @@ void Player::move()
|
||||
// Comprueba si tiene uno de los pies sobre una superficie
|
||||
if (isOnFloor())
|
||||
{ // Y deja al jugador de pie
|
||||
state = s_standing;
|
||||
setState(s_standing);
|
||||
vy = 0.0f;
|
||||
|
||||
// Si ademas ha habido un cambio de tile recoloca al jugador
|
||||
@@ -433,7 +442,7 @@ void Player::move()
|
||||
// Si tiene ambos pies sobre el vacío y no está saltando
|
||||
else if (state != s_jumping)
|
||||
{
|
||||
state = s_falling;
|
||||
setState(s_falling);
|
||||
vy = maxVY;
|
||||
}
|
||||
}
|
||||
@@ -446,7 +455,7 @@ void Player::move()
|
||||
{
|
||||
if (state != s_jumping)
|
||||
{
|
||||
state = s_falling;
|
||||
setState(s_falling);
|
||||
vy = maxVY;
|
||||
}
|
||||
|
||||
@@ -456,14 +465,14 @@ void Player::move()
|
||||
{
|
||||
if (isOnFloor())
|
||||
{
|
||||
state = s_standing;
|
||||
setState(s_standing);
|
||||
vy = 0.0f;
|
||||
}
|
||||
}
|
||||
// EXPERIMENTAL
|
||||
else if (checkSlopes())
|
||||
{
|
||||
state = s_standing;
|
||||
setState(s_standing);
|
||||
vy = 0.0f;
|
||||
}
|
||||
}
|
||||
@@ -489,8 +498,8 @@ void Player::checkJumpEnd()
|
||||
if (state == s_jumping)
|
||||
if (vy > 0)
|
||||
if (y >= jump_ini)
|
||||
{
|
||||
state = s_falling;
|
||||
{ // Si alcanza la altura de salto inicial, pasa al estado de caída
|
||||
setState(s_falling);
|
||||
vy = maxVY;
|
||||
}
|
||||
}
|
||||
@@ -504,11 +513,13 @@ bool Player::isOnFloor()
|
||||
|
||||
for (auto f : underFeet)
|
||||
{
|
||||
const tile_e tile = (room->getTile(f));
|
||||
onFloor |= (tile == t_wall || tile == t_passable);
|
||||
onFloor |= room->checkTopSurfaces(&f);
|
||||
}
|
||||
|
||||
debug->add("ONFLOOR = " + std::to_string(onFloor));
|
||||
if (onFloor)
|
||||
{
|
||||
debug->add("ONFLOOR");
|
||||
}
|
||||
|
||||
return onFloor;
|
||||
}
|
||||
@@ -661,4 +672,11 @@ bool Player::getInvincible()
|
||||
void Player::setInvincible(bool value)
|
||||
{
|
||||
invincible = value;
|
||||
}
|
||||
|
||||
// Cambia el estado del jugador
|
||||
void Player::setState(state_e value)
|
||||
{
|
||||
prevState = state;
|
||||
state = value;
|
||||
}
|
||||
Reference in New Issue
Block a user