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.name = SECTION_PROG_GAME;
section.subsection = SUBSECTION_GAME_PLAY; section.subsection = SUBSECTION_GAME_PLAY;
musicEnabled = true;
debug = true; debug = true;
musicEnabled = !debug;
} }
// Destructor // Destructor
@@ -39,7 +41,10 @@ Game::~Game()
// Bucle para el juego // Bucle para el juego
section_t Game::run() section_t Game::run()
{ {
JA_PlayMusic(music); if (musicEnabled)
{
JA_PlayMusic(music);
}
while (section.name == SECTION_PROG_GAME) while (section.name == SECTION_PROG_GAME)
{ {
@@ -105,6 +110,15 @@ void Game::checkInput()
if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE)) if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE))
{ {
debug = !debug; debug = !debug;
musicEnabled = !debug;
if (musicEnabled)
{
JA_PlayMusic(music);
}
else
{
JA_StopMusic();
}
} }
if (input->checkInput(INPUT_BUTTON_3, REPEAT_FALSE)) 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); text = map->getRoomFileName(b_top) + " " + map->getRoomFileName(b_right) + " " + map->getRoomFileName(b_bottom) + " " + map->getRoomFileName(b_left);
debugText->write(0, line += 6, text, -1); debugText->write(0, line += 6, text, -1);
text = "isOnMovingPlatform = " + std::to_string(player->isOnMovingPlatform()); text = "hookedOn = " + std::to_string(player->hookedOnMovingPlatform);
//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));
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); debugText->write(0, line += 6, text, -1);
// Pinta mascaras // Pinta mascaras
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128); SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128);
SDL_Rect rect = player->sprite->getRect(); 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 ticks; // Contador de ticks para ajustar la velocidad del programa
int ticksSpeed; // Velocidad a la que se repiten los bucles 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 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. // Actualiza el juego, las variables, comprueba la entrada, etc.
void update(); void update();

View File

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

View File

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