diff --git a/source/actor.cpp b/source/actor.cpp index 6db6e0b..e494e04 100644 --- a/source/actor.cpp +++ b/source/actor.cpp @@ -67,4 +67,10 @@ SDL_Rect &Actor::getCollider() actor_name_e Actor::getName() { return name; +} + +// Obtiene el valor de la variable +int Actor::getIncX() +{ + return sprite->getIncX(); } \ No newline at end of file diff --git a/source/actor.h b/source/actor.h index 55681d8..f6dc6ad 100644 --- a/source/actor.h +++ b/source/actor.h @@ -65,6 +65,9 @@ public: // Obtiene el nombre del actor actor_name_e getName(); + + // Obtiene el valor de la variable + int getIncX(); }; #endif diff --git a/source/actor_moving_platform.cpp b/source/actor_moving_platform.cpp index f22522e..14d4008 100644 --- a/source/actor_moving_platform.cpp +++ b/source/actor_moving_platform.cpp @@ -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(); } } diff --git a/source/actor_moving_platform.h b/source/actor_moving_platform.h index fe334eb..5f61c96 100644 --- a/source/actor_moving_platform.h +++ b/source/actor_moving_platform.h @@ -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); diff --git a/source/map.cpp b/source/map.cpp index dddbba0..893de6b 100644 --- a/source/map.cpp +++ b/source/map.cpp @@ -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; } \ No newline at end of file diff --git a/source/map.h b/source/map.h index 2975372..10a8a82 100644 --- a/source/map.h +++ b/source/map.h @@ -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 diff --git a/source/movingsprite.cpp b/source/movingsprite.cpp index aa88c61..8500651 100644 --- a/source/movingsprite.cpp +++ b/source/movingsprite.cpp @@ -356,4 +356,10 @@ void MovingSprite::undoMoveY() void MovingSprite::clearVel() { vx = vy = 0.0f; +} + +// Devuelve el incremento en el eje X en pixels +int MovingSprite::getIncX() +{ + return (int)x - (int)xPrev; } \ No newline at end of file diff --git a/source/movingsprite.h b/source/movingsprite.h index 86f5609..aeedcab 100644 --- a/source/movingsprite.h +++ b/source/movingsprite.h @@ -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 diff --git a/source/player.cpp b/source/player.cpp index 5edf9c3..0099952 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -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; }