Plataformas moviles completadas

This commit is contained in:
2022-08-27 09:36:43 +02:00
parent 43c10d335b
commit 2e102160e6
9 changed files with 61 additions and 12 deletions

View File

@@ -68,3 +68,9 @@ actor_name_e Actor::getName()
{
return name;
}
// Obtiene el valor de la variable
int Actor::getIncX()
{
return sprite->getIncX();
}

View File

@@ -65,6 +65,9 @@ public:
// Obtiene el nombre del actor
actor_name_e getName();
// Obtiene el valor de la variable
int getIncX();
};
#endif

View File

@@ -21,14 +21,12 @@ void ActorMovingPlatform::checkPath()
if (sprite->getPosX() > p2.x || sprite->getPosX() < p1.x)
{
sprite->setVelX(sprite->getVelX() * (-1));
//sprite->flip();
}
// Comprueba los límites verticales
if (sprite->getPosY() > p2.y || sprite->getPosY() < p1.y)
{
sprite->setVelY(sprite->getVelY() * (-1));
//sprite->flip();
}
}

View File

@@ -17,6 +17,9 @@ private:
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
void checkPath();
// Actualiza la variable
void updateShift();
public:
// Constructor
ActorMovingPlatform(actor_t actor, SDL_Point p1, SDL_Point p2);

View File

@@ -533,13 +533,25 @@ int Map::getActorName(int index)
}
// Devuelve el rectangulo de colisión
int Map::getActorCollider(int index)
SDL_Rect Map::getActorCollider(int index)
{
int y = 0;
SDL_Rect rect = {0, 0, 0, 0};
if (index != -1)
{
y = actors[index]->getCollider().y;
rect = actors[index]->getCollider();
}
return y;
return rect;
}
// Devuelve el desplazamiento relativo del actor
int Map::getActorIncX(int index)
{
int shift = 0;
if (index != -1)
{
shift = actors[index]->getIncX();
}
return shift;
}

View File

@@ -102,7 +102,10 @@ public:
int getActorName(int index);
// Devuelve el rectangulo de colisión
int getActorCollider(int index);
SDL_Rect getActorCollider(int index);
// Devuelve el desplazamiento relativo del actor
int getActorIncX(int index);
};
#endif

View File

@@ -357,3 +357,9 @@ void MovingSprite::clearVel()
{
vx = vy = 0.0f;
}
// Devuelve el incremento en el eje X en pixels
int MovingSprite::getIncX()
{
return (int)x - (int)xPrev;
}

View File

@@ -165,6 +165,9 @@ public:
// Pone a cero las velocidades de desplacamiento
void clearVel();
// Devuelve el incremento en el eje X en pixels
int getIncX();
};
#endif

View File

@@ -198,8 +198,10 @@ void Player::move()
lastPosition = {(int)x, (int)y};
addGravity();
// Mueve en el eje X y comprueba colisiones con muros
// Mueve en el eje X
x += vx;
// Comprueba colisiones con muros
if (checkMapCollisions())
{
// Recoloca
@@ -215,6 +217,7 @@ void Player::move()
vx = 0.0f;
}
// Mueve en el eje Y y comprueba colisiones con muros
y += vy;
if (checkMapCollisions())
@@ -260,17 +263,26 @@ void Player::move()
}
}
// Si cayendo toca una plataforma movil
// Si está cayendo
if (vy >= 0.0f)
{
state = falling;
if (isOnFloor())
{
state = standing;
}
// Si no esta enganchado a una plataforma
if (hookedOnMovingPlatform == -1)
{
// Si esta sobre una plataforma
if (isOnMovingPlatform())
{
// Detener la caída y alinearlo con la plataforma
state = standing;
vy = 0.0f;
y = -h + map->getActorCollider(hookedOnMovingPlatform);
y = -h + map->getActorCollider(hookedOnMovingPlatform).y;
}
}
}
@@ -281,7 +293,9 @@ void Player::move()
// Dejarlo alineado con la plataforma
state = standing;
vy = 0.0f;
y = -h + map->getActorCollider(hookedOnMovingPlatform);
y = -h + map->getActorCollider(hookedOnMovingPlatform).y;
x += map->getActorIncX(hookedOnMovingPlatform);
isOnMovingPlatform();
}
}
@@ -333,13 +347,14 @@ bool Player::isOnFloor()
bool Player::isOnMovingPlatform()
{
bool onMovingPlatform = false;
hookedOnMovingPlatform = -1;
updateFeet();
for (auto f : underFeet)
{
onMovingPlatform |= (map->getActorName(map->actorCollision(f)) == a_moving_platform);
hookedOnMovingPlatform = std::max(hookedOnMovingPlatform,(map->actorCollision(f)));
hookedOnMovingPlatform = std::max(hookedOnMovingPlatform, (map->actorCollision(f)));
}
return onMovingPlatform;
}