From 3d41da0fdfa190e27acfd0ccecd49819edaea8b8 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 11 Sep 2024 13:59:40 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adida=20opci=C3=B3n=20para=20intercambia?= =?UTF-8?q?r=20los=20jugadores=20de=20los=20dos=20primeros=20mandos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/define_buttons.cpp | 12 +++++++- source/define_buttons.h | 3 ++ source/director.cpp | 2 +- source/game.cpp | 12 +++++--- source/title.cpp | 64 +++++++++++++++++++-------------------- 5 files changed, 55 insertions(+), 38 deletions(-) diff --git a/source/define_buttons.cpp b/source/define_buttons.cpp index d9e667e..542f4a0 100644 --- a/source/define_buttons.cpp +++ b/source/define_buttons.cpp @@ -69,7 +69,7 @@ void DefineButtons::render() { if (enabled) { - text->writeCentered(x, y - 10, "JUGADOR " + std::to_string(indexController + 1)); + text->writeCentered(x, y - 10, "JUGADOR " + std::to_string(options->controller[indexController].playerId)); text->writeCentered(x, y, controllerNames[indexController]); text->writeCentered(x, y + 10, buttons[indexButton].label); } @@ -117,7 +117,9 @@ void DefineButtons::checkInput() } if (event.type == SDL_CONTROLLERBUTTONDOWN) + { doControllerButtonDown(&event.cbutton); + } } } } @@ -171,4 +173,12 @@ void DefineButtons::saveBindingsToOptions() { options->controller[indexController].buttons[j] = input->getControllerBinding(indexController, options->controller[indexController].inputs[j]); } +} + +// Intercambia los jugadores asignados a los dos primeros mandos +void DefineButtons::swapControllers() +{ + const int temp = options->controller[0].playerId; + options->controller[0].playerId = options->controller[1].playerId; + options->controller[1].playerId = temp; } \ No newline at end of file diff --git a/source/define_buttons.h b/source/define_buttons.h index 4bd18ae..13d476e 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -65,4 +65,7 @@ public: // Comprueba si está habilitado bool isEnabled(); + + // Intercambia los jugadores asignados a los dos primeros mandos + void swapControllers(); }; \ No newline at end of file diff --git a/source/director.cpp b/source/director.cpp index 93461cf..ce39bfd 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -877,7 +877,7 @@ void Director::runTitle() // Ejecuta la sección donde se juega al juego void Director::runGame() { - const int playerID = section->options == SECTION_OPTIONS_GAME_PLAY_1P ? 0 : 1; + const int playerID = section->options; game = new Game(playerID, 0, screen, asset, lang, input, false, param, options, section, getMusic(musics, "playing.ogg")); game->run(); delete game; diff --git a/source/game.cpp b/source/game.cpp index 8d5a806..d30703d 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -122,11 +122,14 @@ void Game::init(int playerID) player2->setController(controller2); players.push_back(player2); + // Obtiene el "id" del jugador que va a jugar + Player *player = getPlayer(playerID); + // Cambia el estado del jugador seleccionado - players[playerID]->setStatusPlaying(PLAYER_STATUS_PLAYING); + player->setStatusPlaying(PLAYER_STATUS_PLAYING); // Como es el principio del juego, empieza sin inmunidad - players[playerID]->setInvulnerable(false); + player->setInvulnerable(false); // Variables relacionadas con la dificultad switch (difficulty) @@ -224,8 +227,9 @@ void Game::init(int playerID) // Activa o no al otro jugador if (rand() % 2 == 0) { - const int otherPlayer = playerID == 1 ? 1 : 0; - players[otherPlayer]->setStatusPlaying(PLAYER_STATUS_PLAYING); + const int otherPlayer = playerID == 1 ? 2 : 1; + Player *player = getPlayer(otherPlayer); + player->setStatusPlaying(PLAYER_STATUS_PLAYING); } for (auto player : players) diff --git a/source/title.cpp b/source/title.cpp index 2bf9a9a..fb46f98 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -91,26 +91,16 @@ void Title::update() fade->update(); if (fade->hasEnded()) { - switch (postFade) + if (postFade == -1) { - case 1: // 1 PLAYER - section->name = SECTION_PROG_GAME; - section->options = SECTION_OPTIONS_GAME_PLAY_1P; - JA_StopMusic(); - break; - - case 2: // 2 PLAYER - section->name = SECTION_PROG_GAME; - section->options = SECTION_OPTIONS_GAME_PLAY_2P; - JA_StopMusic(); - break; - - case 3: // TIME OUT section->name = SECTION_PROG_GAME_DEMO; - break; - default: - break; + } + else + { + section->name = SECTION_PROG_GAME; + section->options = postFade; + JA_StopMusic(); } } @@ -148,7 +138,7 @@ void Title::update() if (counter == param->title.titleDuration) { fade->activate(); - postFade = 3; + postFade = -1; } } } @@ -220,6 +210,28 @@ void Title::checkEvents() reLoadTextures(); } + else if (eventHandler->type == SDL_KEYDOWN && eventHandler->key.repeat == 0) + { + switch (eventHandler->key.keysym.sym) + { + case SDLK_1: + defineButtons->enable(0); + break; + + case SDLK_2: + defineButtons->enable(1); + break; + + case SDLK_3: + defineButtons->swapControllers(); + screen->showNotification("Swap Controllers"); + break; + + default: + break; + } + } + // Comprueba en el primer mando el botón de salir del programa // else if (eventHandler->type == SDL_CONTROLLERBUTTONDOWN) //{ @@ -257,22 +269,10 @@ void Title::checkInput() if (section->options == SECTION_OPTIONS_TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade->activate(); - postFade = 1; + postFade = options->controller[0].playerId; } } - // Comprueba si se ha pulsado la tecla 1 o 2 para definir los controladores - const Uint8 *keyStates = SDL_GetKeyboardState(nullptr); - - if (keyStates[SDL_SCANCODE_1] != 0) - { - defineButtons->enable(0); - } - - else if (keyStates[SDL_SCANCODE_2] != 0) - { - defineButtons->enable(1); - } ////////////////////////////////////////////////// // MANDO // @@ -306,7 +306,7 @@ void Title::checkInput() if (section->options == SECTION_OPTIONS_TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) { fade->activate(); - postFade = i + 1; + postFade = options->controller[i].playerId; } } }