From e6dad36f3bff0023e2434a0d88604641d1fdcaa3 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 29 Aug 2022 10:48:31 +0200 Subject: [PATCH] Las plataformas horizontales ya no empujan ni se queda pegado al suelo --- source/player.cpp | 66 ++++++++++++++++++++++++++++++++--------------- source/player.h | 14 +++++++--- todo.md | 2 +- 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/source/player.cpp b/source/player.cpp index 1d4f741..07a17e6 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -34,7 +34,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map) maxVX = 1.5f; maxVY = 4.0f; - state = standing; + state = s_standing; jumpPressed = false; key.insert(key.end(), {0, 0, 0, 0, 0, 0}); const SDL_Point p = {0, 0}; @@ -99,18 +99,19 @@ void Player::checkInput() if (input->checkInput(INPUT_UP, REPEAT_TRUE)) { - if (state == standing) + if (state == s_standing) { if (!jumpPressed) { jumpStrenght = 2.0f; vy -= jumpStrenght; - state = jumping; + state = s_jumping; + isOn = f_none; jumpPressed = true; JA_PlaySound(sound_jump); } } - else if (state == jumping) + else if (state == s_jumping) { if (jumpPressed) { @@ -127,9 +128,9 @@ void Player::checkInput() else { jumpPressed = false; - if (state == jumping) + if (state == s_jumping) { - state = falling; + state = s_falling; } } } @@ -138,7 +139,7 @@ void Player::checkInput() void Player::addGravity() { // *** Falta ver pq la gravedad empuja al muñeco hacia abajo en los tiles atravesables - if (state != standing) + if (state != s_standing) { vy = std::min(vy += gravity, maxVY); } @@ -227,12 +228,12 @@ void Player::move() if (vy > 0.0f) { y -= ((int)y + h) % tileSize; - state = standing; + state = s_standing; } else { y += tileSize - ((int)y % tileSize); - state = falling; + state = s_falling; } vy = 0.0f; } @@ -253,14 +254,14 @@ void Player::move() // Tiene uno de los pies sobre una superficie if (isOnFloor()) { - state = standing; + state = s_standing; vy = 0.0f; y -= ((int)y + h) % tileSize; } // Tiene ambos pies sobre el vacío else { - state = falling; + state = s_falling; } } @@ -269,14 +270,14 @@ void Player::move() // Si está cayendo if (going_down2) { - state = falling; + state = s_falling; // Si está alineado con el tile mira el suelo (para que no lo mire si está // dentro de un tile atravesable y lo deje a medias) if (tile_aligned2) { if (isOnFloor()) { - state = standing; + state = s_standing; } } @@ -287,9 +288,9 @@ void Player::move() if (isOnMovingPlatform()) { // Detener la caída y alinearlo con la plataforma - state = standing; + state = s_standing; vy = 0.0f; - y = -h + map->getActorCollider(hookedOnMovingPlatform).y; + y = map->getActorCollider(hookedOnMovingPlatform).y - h; } } } @@ -298,7 +299,7 @@ void Player::move() if (hookedOnMovingPlatform != -1) { // Dejarlo alineado con la plataforma - state = standing; + state = s_standing; vy = 0.0f; y = map->getActorCollider(hookedOnMovingPlatform).y - h; x += map->getActorIncX(hookedOnMovingPlatform); @@ -314,12 +315,9 @@ void Player::move() // Anima al jugador void Player::animate() { - if (state != standing) + if (state != s_standing) { - // if (abs(vy) > 1.0f) - { - sprite->setCurrentAnimation("jump"); - } + sprite->setCurrentAnimation("jump"); } else { @@ -339,6 +337,11 @@ void Player::animate() // Comprueba si el jugador tiene suelo debajo de los pies bool Player::isOnFloor() { + if (isOn == f_platform) + { + return false; + } + bool onFloor = false; updateFeet(); @@ -347,6 +350,16 @@ bool Player::isOnFloor() { onFloor |= ((map->getTile(f) == wall) || (map->getTile(f) == passable)); } + + if (onFloor) + { + isOn = f_wall; + } + else + { + isOn = f_none; + } + return onFloor; } @@ -356,6 +369,12 @@ bool Player::isOnMovingPlatform() bool onMovingPlatform = false; hookedOnMovingPlatform = -1; + // Si esta sobre el suelo, no puede estar tambien sobre una plataforma movil + if (isOn == f_wall) + { + return false; + } + updateFeet(); for (auto f : underFeet) @@ -367,6 +386,11 @@ bool Player::isOnMovingPlatform() if (!onMovingPlatform) { hookedOnMovingPlatform = -1; + isOn = f_none; + } + else + { + isOn = f_platform; } return onMovingPlatform; diff --git a/source/player.h b/source/player.h index d49ffde..d8458c1 100644 --- a/source/player.h +++ b/source/player.h @@ -14,9 +14,16 @@ enum e_state { - standing, - jumping, - falling + s_standing, + s_jumping, + s_falling +}; + +enum e_floor +{ + f_none, + f_wall, + f_platform }; // The player @@ -40,6 +47,7 @@ public: e_border border; // Indica en qué borde de la pantalla está el jugador SDL_Point lastPosition; // Posición anterior int hookedOnMovingPlatform; // Índice de la plataforma movil a la que está enganchado + e_floor isOn; // Indica sobre que tipo de suelo se encuentra // Variables que afectan a la inercia del movimiento bool jumpPressed; // Indica si esta pulsada la tecla de salto diff --git a/todo.md b/todo.md index 4a2eac4..9de187d 100644 --- a/todo.md +++ b/todo.md @@ -1,6 +1,6 @@ ## TAREAS (A) Plataformas moviles verticales: se ha de enganchar a la vy para no verse afectado por la gravedad {cm:2022-08-29} -(A) Plataformas moviles horizontales: que no te empujen +(A) Plataformas moviles horizontales: que no te empujen {cm:2022-08-29} (B) Plantearse hasta cuanto puede estar fuera el personaje de una plataforma. Ahora mismo con un pixel se aguanta dentro (A) Colisiones más alla del mapa. Si al otro lado del mapa hay una pared, ahora mismo hace cosas raras