Ya se engancha verticalmente en las plataformas moviles

This commit is contained in:
2022-08-27 07:55:06 +02:00
parent 6b7c49aecd
commit 43c10d335b
4 changed files with 63 additions and 47 deletions

View File

@@ -23,7 +23,9 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input)
section.name = SECTION_PROG_GAME;
section.subsection = SUBSECTION_GAME_PLAY;
musicEnabled = true;
debug = true;
musicEnabled = !debug;
}
// Destructor
@@ -39,7 +41,10 @@ Game::~Game()
// Bucle para el juego
section_t Game::run()
{
JA_PlayMusic(music);
if (musicEnabled)
{
JA_PlayMusic(music);
}
while (section.name == SECTION_PROG_GAME)
{
@@ -105,6 +110,15 @@ void Game::checkInput()
if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE))
{
debug = !debug;
musicEnabled = !debug;
if (musicEnabled)
{
JA_PlayMusic(music);
}
else
{
JA_StopMusic();
}
}
if (input->checkInput(INPUT_BUTTON_3, REPEAT_FALSE))
@@ -177,16 +191,9 @@ void Game::renderDebugInfo()
text = map->getRoomFileName(b_top) + " " + map->getRoomFileName(b_right) + " " + map->getRoomFileName(b_bottom) + " " + map->getRoomFileName(b_left);
debugText->write(0, line += 6, text, -1);
text = "isOnMovingPlatform = " + std::to_string(player->isOnMovingPlatform());
//SDL_Point p = {76, 180};
//SDL_Rect r = player->sprite->getRect();
//SDL_SetRenderDrawColor(renderer, 255, 0, 0, 128);
//SDL_RenderDrawPoint(renderer, p.x, p.y);
//text = "checkCollision = " + std::to_string(checkCollision(p, r));
text = "hookedOn = " + std::to_string(player->hookedOnMovingPlatform);
debugText->write(0, line += 6, text, -1);
text = std::to_string(map->getActorCollider(0)) + " " + std::to_string(map->getActorCollider(1));
debugText->write(0, line += 6, text, -1);
// Pinta mascaras
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128);
SDL_Rect rect = player->sprite->getRect();

View File

@@ -28,6 +28,7 @@ private:
int ticks; // Contador de ticks para ajustar la velocidad del programa
int ticksSpeed; // Velocidad a la que se repiten los bucles del programa
bool debug; // Indica si esta activo el modo de depuración
bool musicEnabled; // Indica si la musica puede sonar o no
// Actualiza el juego, las variables, comprueba la entrada, etc.
void update();

View File

@@ -24,8 +24,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
vx = 0;
vy = 0;
lastPosition = {(int)x, (int)y};
const SDL_Rect rect = {(int)x, (int)y, w, h};
sprite->setRect(rect);
sprite->setRect({(int)x, (int)y, w, h});
sprite->setCurrentAnimation("stand");
sprite->setFlip(SDL_FLIP_NONE);
@@ -37,15 +36,11 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
state = standing;
jumpPressed = false;
invulnerable = false;
enabled = true;
cooldown = 0;
lives = 10;
coins = 0;
key.insert(key.end(), {0, 0, 0, 0, 0, 0});
const SDL_Point p = {0, 0};
collider.insert(collider.end(), {p, p, p, p, p, p, p, p, p, p, p, p});
underFeet.insert(underFeet.end(), {p, p, p});
hookedOnMovingPlatform = -1;
}
// Destructor
@@ -123,6 +118,9 @@ void Player::checkInput()
}
jumpPressed = true;
// Si salta sale de la plataforma movil
hookedOnMovingPlatform = -1;
}
else
{
@@ -255,19 +253,36 @@ void Player::move()
vy = 0.0f;
y -= ((int)y + h) % tileSize;
}
// Tiene uno de los pies sobre una plataforma móvil
else if (isOnMovingPlatform())
{
state = standing;
vy = 0.0f;
y = SDKJSGHK - h-1;
}
// Tiene ambos pies sobre el vacío
else
{
state = falling;
}
}
// Si cayendo toca una plataforma movil
if (vy >= 0.0f)
{
if (hookedOnMovingPlatform == -1)
{
if (isOnMovingPlatform())
{
// Detener la caída y alinearlo con la plataforma
state = standing;
vy = 0.0f;
y = -h + map->getActorCollider(hookedOnMovingPlatform);
}
}
}
// Si está enganchado a una plataforma movil
if (hookedOnMovingPlatform != -1)
{
// Dejarlo alineado con la plataforma
state = standing;
vy = 0.0f;
y = -h + map->getActorCollider(hookedOnMovingPlatform);
}
}
// Actualiza la posición del sprite
@@ -280,7 +295,10 @@ void Player::animate()
{
if (state != standing)
{
sprite->setCurrentAnimation("jump");
// if (abs(vy) > 1.0f)
{
sprite->setCurrentAnimation("jump");
}
}
else
{
@@ -315,17 +333,13 @@ bool Player::isOnFloor()
bool Player::isOnMovingPlatform()
{
bool onMovingPlatform = false;
SDKJSGHK = 0;
updateFeet();
for (auto f : underFeet)
{
onMovingPlatform |= (map->getActorName(map->actorCollision(f)) == a_moving_platform);
if (onMovingPlatform)
{
SDKJSGHK = map->getActorCollider(map->actorCollision(f));
}
hookedOnMovingPlatform = std::max(hookedOnMovingPlatform,(map->actorCollision(f)));
}
return onMovingPlatform;
}

View File

@@ -30,23 +30,19 @@ public:
LTexture *texture; // Textura con los graficos del jugador
Map *map; // Objeto con el mapa
float x; // Posición del jugador en el eje X
float y; // Posición del jugador en el eje Y
float vx; // Velocidad/desplazamiento del jugador en el eje X
float vy; // Velocidad/desplazamiento del jugador en el eje Y
bool jumpPressed; // Indica si esta pulsada la tecla de salto
bool enabled; // Si está habilitado
bool invulnerable; // Indica si se encuentra en estado invulnerable
int coins; // Cantidad de monedas
int cooldown; // Tiempo de inhabilitación
int lives; // Cantidad de vidas
int w; // Ancho del jugador
int h; // ALto del jugador
e_state state; // Estado actual del jugador
e_border border; // Indica en qué borde de la pantalla está el jugador
SDL_Point lastPosition; // Posición anterior
float x; // Posición del jugador en el eje X
float y; // Posición del jugador en el eje Y
float vx; // Velocidad/desplazamiento del jugador en el eje X
float vy; // Velocidad/desplazamiento del jugador en el eje Y
int w; // Ancho del jugador
int h; // ALto del jugador
e_state state; // Estado actual del jugador
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
// Variables que afectan a la inercia del movimiento
bool jumpPressed; // Indica si esta pulsada la tecla de salto
float jumpStrenght; // Cantidad de pixels a desplazarse y velocidad que pilla al saltar
float gravity; // Gravedad
float accelX; // Aceleración al desplazarse horizontalmente
@@ -60,8 +56,6 @@ public:
JA_Sound sound_death; // Sonido al morir
JA_Sound sound_jump; // Sonido al saltar
int SDKJSGHK;
// Comprueba las entradas y modifica variables
void checkInput();