El juego ya empieza con el jugador que ha pulsado el botón

This commit is contained in:
2024-07-05 16:59:14 +02:00
parent a734c01dc5
commit dc09c189e9
6 changed files with 58 additions and 23 deletions

View File

@@ -16,7 +16,7 @@ Director::Director(int argc, char *argv[])
{ {
// Inicializa variables // Inicializa variables
section = new section_t(); section = new section_t();
section->name = SECTION_PROG_LOGO; section->name = SECTION_PROG_TITLE;
// Comprueba los parametros del programa // Comprueba los parametros del programa
checkProgramArguments(argc, argv); checkProgramArguments(argc, argv);
@@ -758,8 +758,8 @@ void Director::runTitle()
// Ejecuta la seccion de juego donde se juega // Ejecuta la seccion de juego donde se juega
void Director::runGame() void Director::runGame()
{ {
const int numPlayers = section->subsection == SUBSECTION_GAME_PLAY_1P ? 1 : 2; const int playerID = section->subsection == SUBSECTION_GAME_PLAY_1P ? 0 : 1;
game = new Game(numPlayers, 0, renderer, screen, asset, lang, input, false, param, options, section, getMusic(musics, "playing.ogg")); game = new Game(playerID, 0, renderer, screen, asset, lang, input, false, param, options, section, getMusic(musics, "playing.ogg"));
game->run(); game->run();
delete game; delete game;
} }

View File

@@ -162,7 +162,10 @@ void Game::init(int playerID)
// Elimina qualquier jugador que hubiese antes de crear los nuevos // Elimina qualquier jugador que hubiese antes de crear los nuevos
for (auto player : players) for (auto player : players)
{ {
delete player; if (player)
{
delete player;
}
}; };
players.clear(); players.clear();
@@ -173,7 +176,7 @@ void Game::init(int playerID)
Player *player2 = new Player((PLAY_AREA_CENTER_FIRST_QUARTER_X * ((1 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24, renderer, playerTextures[1], playerAnimations); Player *player2 = new Player((PLAY_AREA_CENTER_FIRST_QUARTER_X * ((1 * 2) + 1)) - 11, PLAY_AREA_BOTTOM - 24, renderer, playerTextures[1], playerAnimations);
players.push_back(player2); players.push_back(player2);
numPlayers = 2; players[playerID]->enable(true);
// Variables relacionadas con la dificultad // Variables relacionadas con la dificultad
switch (difficulty) switch (difficulty)
@@ -1606,7 +1609,7 @@ void Game::updateDeath()
bool allPlayersAreDead = true; bool allPlayersAreDead = true;
for (auto player : players) for (auto player : players)
{ {
allPlayersAreDead &= (!player->isAlive()); allPlayersAreDead &= (!player->isAlive() || !player->isEnabled());
} }
if (allPlayersAreDead) if (allPlayersAreDead)

View File

@@ -174,7 +174,6 @@ private:
JA_Music_t *music; // Musica de fondo JA_Music_t *music; // Musica de fondo
// Variables // Variables
int numPlayers; // Numero de jugadores
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa
Uint8 ticksSpeed; // Velocidad a la que se repiten los bucles del programa Uint8 ticksSpeed; // Velocidad a la que se repiten los bucles del programa
Uint32 hiScore; // Puntuación máxima Uint32 hiScore; // Puntuación máxima

View File

@@ -19,6 +19,8 @@ Player::Player(float x, int y, SDL_Renderer *renderer, std::vector<Texture *> te
defaultPosX = posX = x; defaultPosX = posX = x;
defaultPosY = posY = y; defaultPosY = posY = y;
// Inicializa variables
enabled = false;
init(); init();
} }
@@ -135,7 +137,7 @@ void Player::move()
// Si el jugador abandona el area de juego por los laterales // Si el jugador abandona el area de juego por los laterales
if ((posX < PLAY_AREA_LEFT - 5) || (posX + width > PLAY_AREA_RIGHT + 5)) if ((posX < PLAY_AREA_LEFT - 5) || (posX + width > PLAY_AREA_RIGHT + 5))
{ {
// Restaura su posición // Restaura su posición
posX -= velX; posX -= velX;
} }
@@ -159,7 +161,7 @@ void Player::move()
// Si el cadaver abandona el area de juego por los laterales // Si el cadaver abandona el area de juego por los laterales
if ((deathSprite->getPosX() < PLAY_AREA_LEFT) || (deathSprite->getPosX() + width > PLAY_AREA_RIGHT)) if ((deathSprite->getPosX() < PLAY_AREA_LEFT) || (deathSprite->getPosX() + width > PLAY_AREA_RIGHT))
{ {
// Restaura su posición // Restaura su posición
const float vx = deathSprite->getVelX(); const float vx = deathSprite->getVelX();
deathSprite->setPosX(deathSprite->getPosX() - vx); deathSprite->setPosX(deathSprite->getPosX() - vx);
@@ -173,6 +175,11 @@ void Player::move()
// Pinta el jugador en pantalla // Pinta el jugador en pantalla
void Player::render() void Player::render()
{ {
if (!enabled)
{
return;
}
if (isAlive()) if (isAlive())
{ {
if (invulnerable) if (invulnerable)
@@ -248,7 +255,7 @@ void Player::setAnimation()
legsSprite->setCurrentAnimation(aWalking); legsSprite->setCurrentAnimation(aWalking);
legsSprite->setFlip(flipWalk); legsSprite->setFlip(flipWalk);
if (statusFiring == PLAYER_STATUS_FIRING_NO) if (statusFiring == PLAYER_STATUS_FIRING_NO)
{ {
// No esta disparando // No esta disparando
bodySprite->setCurrentAnimation(aWalking + aBodyCoffees + aPowerUp); bodySprite->setCurrentAnimation(aWalking + aBodyCoffees + aPowerUp);
bodySprite->setFlip(flipWalk); bodySprite->setFlip(flipWalk);
@@ -256,7 +263,7 @@ void Player::setAnimation()
headSprite->setFlip(flipWalk); headSprite->setFlip(flipWalk);
} }
else else
{ {
// Está disparando // Está disparando
bodySprite->setCurrentAnimation(aFiring + aBodyCoffees + aPowerUp); bodySprite->setCurrentAnimation(aFiring + aBodyCoffees + aPowerUp);
bodySprite->setFlip(flipFire); bodySprite->setFlip(flipFire);
@@ -330,6 +337,11 @@ void Player::updateCooldown()
// Actualiza al jugador a su posicion, animación y controla los contadores // Actualiza al jugador a su posicion, animación y controla los contadores
void Player::update() void Player::update()
{ {
if (!enabled)
{
return;
}
move(); move();
setAnimation(); setAnimation();
shiftColliders(); shiftColliders();
@@ -602,4 +614,16 @@ void Player::setPlayerTextures(std::vector<Texture *> texture)
legsSprite->setTexture(texture[2]); legsSprite->setTexture(texture[2]);
deathSprite->setTexture(texture[3]); deathSprite->setTexture(texture[3]);
fireSprite->setTexture(texture[4]); fireSprite->setTexture(texture[4]);
}
// Activa o descativa el jugador
void Player::enable(bool value)
{
enabled = value;
}
// Obtiene el valor de la variable
bool Player::isEnabled()
{
return enabled;
} }

View File

@@ -67,6 +67,7 @@ private:
bool input; // Indica si puede recibir ordenes de entrada bool input; // Indica si puede recibir ordenes de entrada
circle_t collider; // Circulo de colisión del jugador circle_t collider; // Circulo de colisión del jugador
bool alive; // Indica si el jugador está vivo bool alive; // Indica si el jugador está vivo
bool enabled; // Indica si el jugador está activo
// Actualiza el circulo de colisión a la posición del jugador // Actualiza el circulo de colisión a la posición del jugador
void shiftColliders(); void shiftColliders();
@@ -209,6 +210,12 @@ public:
// Obtiene el puntero a la textura con los gráficos de la animación de morir // Obtiene el puntero a la textura con los gráficos de la animación de morir
Texture *getDeadTexture(); Texture *getDeadTexture();
// Activa o descativa el jugador
void enable(bool value);
// Obtiene el valor de la variable
bool isEnabled();
}; };
#endif #endif

View File

@@ -94,13 +94,13 @@ void Title::update()
{ {
switch (postFade) switch (postFade)
{ {
case 0: // 1 PLAYER case 1: // 1 PLAYER
section->name = SECTION_PROG_GAME; section->name = SECTION_PROG_GAME;
section->subsection = SUBSECTION_GAME_PLAY_1P; section->subsection = SUBSECTION_GAME_PLAY_1P;
JA_StopMusic(); JA_StopMusic();
break; break;
case 1: // 2 PLAYER case 2: // 2 PLAYER
section->name = SECTION_PROG_GAME; section->name = SECTION_PROG_GAME;
section->subsection = SUBSECTION_GAME_PLAY_2P; section->subsection = SUBSECTION_GAME_PLAY_2P;
JA_StopMusic(); JA_StopMusic();
@@ -216,33 +216,35 @@ void Title::checkEvents()
// Comprueba las entradas // Comprueba las entradas
void Title::checkInput() void Title::checkInput()
{ {
// Comprueba todos los controladores // Comprueba todos los controladores para salir
for (int i = 0; i < numControllers; ++i) for (int i = 0; i < numControllers; ++i)
{ {
if (input->checkInput(input_exit, REPEAT_FALSE, options->game.input[i].deviceType, options->game.input[i].id)) if (input->checkInput(input_exit, REPEAT_FALSE, options->game.input[i].deviceType, options->game.input[i].id))
{ {
section->name = SECTION_PROG_QUIT; section->name = SECTION_PROG_QUIT;
} }
else if (input->checkInput(input_accept, REPEAT_FALSE, options->game.input[i].deviceType, options->game.input[i].id))
{
fade->activate();
postFade = i;
}
} }
// Comprueba el teclado // Comprueba el teclado para salir
if (input->checkInput(input_exit, REPEAT_FALSE)) if (input->checkInput(input_exit, REPEAT_FALSE))
{ {
section->name = SECTION_PROG_QUIT; section->name = SECTION_PROG_QUIT;
} }
else if (input->checkInput(input_accept, REPEAT_FALSE)) // Comprueba si se ha pulsado algún botón para empezar a jugar
const int index = input->checkAnyButtonPressed();
if (index)
{ {
fade->activate(); fade->activate();
postFade = 0; postFade = index;
} }
//else if (input->checkInput(input_start, REPEAT_FALSE))
//{
// fade->activate();
// postFade = 0;
//}
// Comprueba el input para el resto de objetos // Comprueba el input para el resto de objetos
screen->checkInput(); screen->checkInput();
} }