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; 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 // Obtiene el nombre del actor
actor_name_e getName(); actor_name_e getName();
// Obtiene el valor de la variable
int getIncX();
}; };
#endif #endif

View File

@@ -21,14 +21,12 @@ void ActorMovingPlatform::checkPath()
if (sprite->getPosX() > p2.x || sprite->getPosX() < p1.x) if (sprite->getPosX() > p2.x || sprite->getPosX() < p1.x)
{ {
sprite->setVelX(sprite->getVelX() * (-1)); sprite->setVelX(sprite->getVelX() * (-1));
//sprite->flip();
} }
// Comprueba los límites verticales // Comprueba los límites verticales
if (sprite->getPosY() > p2.y || sprite->getPosY() < p1.y) if (sprite->getPosY() > p2.y || sprite->getPosY() < p1.y)
{ {
sprite->setVelY(sprite->getVelY() * (-1)); 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 // Comprueba si ha llegado al limite del recorrido para darse media vuelta
void checkPath(); void checkPath();
// Actualiza la variable
void updateShift();
public: public:
// Constructor // Constructor
ActorMovingPlatform(actor_t actor, SDL_Point p1, SDL_Point p2); 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 // 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) 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); int getActorName(int index);
// Devuelve el rectangulo de colisión // 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 #endif

View File

@@ -357,3 +357,9 @@ void MovingSprite::clearVel()
{ {
vx = vy = 0.0f; 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 // Pone a cero las velocidades de desplacamiento
void clearVel(); void clearVel();
// Devuelve el incremento en el eje X en pixels
int getIncX();
}; };
#endif #endif

View File

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