From ecf34558f48a189c439bef1d782310dc12b94023 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 11 Sep 2024 12:15:18 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adido=20campo=20ID=20al=20jugador=20Las?= =?UTF-8?q?=20balas=20y=20los=20mandos=20utilizan=20ahora=20este=20ID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/common/utils.h | 1 + source/director.cpp | 3 +- source/game.cpp | 76 +++++++++++++++++++++++++++++++------------ source/game.h | 6 ++++ source/player.cpp | 25 ++++++++++++-- source/player.h | 13 +++++++- 6 files changed, 98 insertions(+), 26 deletions(-) diff --git a/source/common/utils.h b/source/common/utils.h index e32a829..09c686c 100644 --- a/source/common/utils.h +++ b/source/common/utils.h @@ -143,6 +143,7 @@ struct op_game_t struct op_controller_t { int index; // Indice en el vector de mandos + int playerId; // Jugador asociado al mando Uint8 deviceType; // Indica si se utilizará teclado o mando o ambos std::string name; // Nombre del dispositivo std::vector inputs; // Listado de inputs diff --git a/source/director.cpp b/source/director.cpp index 7da0c94..93461cf 100644 --- a/source/director.cpp +++ b/source/director.cpp @@ -502,6 +502,7 @@ void Director::initOptions() for (int index = 0; index < numPlayers; ++index) { c.index = index; + c.playerId = index + 1; c.deviceType = INPUT_USE_GAMECONTROLLER; c.name = "NO NAME"; @@ -515,7 +516,7 @@ void Director::initOptions() c.buttons.clear(); c.buttons.push_back(SDL_CONTROLLER_BUTTON_X); c.buttons.push_back(SDL_CONTROLLER_BUTTON_Y); - c.buttons.push_back(SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); + c.buttons.push_back(SDL_CONTROLLER_BUTTON_B); c.buttons.push_back(SDL_CONTROLLER_BUTTON_START); c.buttons.push_back(SDL_CONTROLLER_BUTTON_BACK); diff --git a/source/game.cpp b/source/game.cpp index 58d7b34..8d5a806 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -108,14 +108,18 @@ void Game::init(int playerID) players.clear(); // Crea los dos jugadores - Player *player1 = new Player((param->game.playArea.firstQuarterX * ((0 * 2) + 1)) - 11, param->game.playArea.rect.h - 30, ¶m->game.playArea.rect ,playerTextures[0], playerAnimations); + Player *player1 = new Player(1, (param->game.playArea.firstQuarterX * ((0 * 2) + 1)) - 11, param->game.playArea.rect.h - 30, ¶m->game.playArea.rect, playerTextures[0], playerAnimations); player1->setScoreBoardPanel(SCOREBOARD_LEFT_PANEL); player1->setName(lang->getText(53)); + const int controller1 = getController(player1->getId()); + player1->setController(controller1); players.push_back(player1); - Player *player2 = new Player((param->game.playArea.firstQuarterX * ((1 * 2) + 1)) - 11, param->game.playArea.rect.h - 30, ¶m->game.playArea.rect, playerTextures[1], playerAnimations); + Player *player2 = new Player(2, (param->game.playArea.firstQuarterX * ((1 * 2) + 1)) - 11, param->game.playArea.rect.h - 30, ¶m->game.playArea.rect, playerTextures[1], playerAnimations); player2->setScoreBoardPanel(SCOREBOARD_RIGHT_PANEL); player2->setName(lang->getText(54)); + const int controller2 = getController(player2->getId()); + player2->setController(controller2); players.push_back(player2); // Cambia el estado del jugador seleccionado @@ -1401,9 +1405,13 @@ void Game::checkBulletBalloonCollision() if (checkCollision(balloon->getCollider(), bullet->getCollider())) { // Otorga los puntos correspondientes al globo al jugador que disparó la bala - int index = bullet->getOwner(); - players[index]->incScoreMultiplier(); - players[index]->addScore(Uint32(balloon->getScore() * players[index]->getScoreMultiplier() * difficultyScoreMultiplier)); + Player *player = getPlayer(bullet->getOwner()); + if (!player) + { + return; + } + player->incScoreMultiplier(); + player->addScore(Uint32(balloon->getScore() * player->getScoreMultiplier() * difficultyScoreMultiplier)); updateHiScore(); // Suelta el item si se da el caso @@ -1417,7 +1425,7 @@ void Game::checkBulletBalloonCollision() } else { - createItem(droppeditem, players[index]->getPosX(), 0); + createItem(droppeditem, player->getPosX(), 0); coffeeMachineEnabled = true; } } @@ -2133,14 +2141,14 @@ void Game::checkInput() demo.keys.fireLeft = 0; demo.keys.fireRight = 0; #endif - int i = 0; for (auto player : players) { + const int controllerIndex = player->getController(); const bool autofire = player->isPowerUp() || options->game.autofire; if (player->isPlaying()) { // Input a la izquierda - if (input->checkInput(input_left, ALLOW_REPEAT, options->controller[i].deviceType, options->controller[i].index)) + if (input->checkInput(input_left, ALLOW_REPEAT, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index)) { player->setInput(input_left); #ifdef RECORDING @@ -2150,7 +2158,7 @@ void Game::checkInput() else { // Input a la derecha - if (input->checkInput(input_right, ALLOW_REPEAT, options->controller[i].deviceType, options->controller[i].index)) + if (input->checkInput(input_right, ALLOW_REPEAT, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index)) { player->setInput(input_right); #ifdef RECORDING @@ -2167,12 +2175,12 @@ void Game::checkInput() } } // Comprueba el input de disparar al centro - if (input->checkInput(input_fire_center, autofire, options->controller[i].deviceType, options->controller[i].index)) + if (input->checkInput(input_fire_center, autofire, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index)) { if (player->canFire()) { player->setInput(input_fire_center); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_UP, player->isPowerUp(), i); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_UP, player->isPowerUp(), player->getId()); player->setFireCooldown(10); // Reproduce el sonido de disparo @@ -2184,12 +2192,12 @@ void Game::checkInput() } // Comprueba el input de disparar a la izquierda - else if (input->checkInput(input_fire_left, autofire, options->controller[i].deviceType, options->controller[i].index)) + else if (input->checkInput(input_fire_left, autofire, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index)) { if (player->canFire()) { player->setInput(input_fire_left); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_LEFT, player->isPowerUp(), i); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_LEFT, player->isPowerUp(), player->getId()); player->setFireCooldown(10); // Reproduce el sonido de disparo @@ -2201,12 +2209,12 @@ void Game::checkInput() } // Comprueba el input de disparar a la derecha - else if (input->checkInput(input_fire_right, autofire, options->controller[i].deviceType, options->controller[i].index)) + else if (input->checkInput(input_fire_right, autofire, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index)) { if (player->canFire()) { player->setInput(input_fire_right); - createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_RIGHT, player->isPowerUp(), i); + createBullet(player->getPosX() + (player->getWidth() / 2) - 4, player->getPosY() + (player->getHeight() / 2), BULLET_RIGHT, player->isPowerUp(), player->getId()); player->setFireCooldown(10); // Reproduce el sonido de disparo @@ -2225,12 +2233,11 @@ void Game::checkInput() } } #endif - i++; } else { // Si no está jugando, el botón de start le permite continuar jugando - if (input->checkInput(input_start, ALLOW_REPEAT, options->controller[i].deviceType, options->controller[i].index)) + if (input->checkInput(input_start, ALLOW_REPEAT, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index)) { // Si no ha entrado ya en el estado de game over, entonces se puede continuar if (gameOverCounter == GAME_OVER_COUNTER) @@ -2240,14 +2247,13 @@ void Game::checkInput() } // Si está continuando, los botones de fuego hacen decrementar el contador - const bool fire1 = input->checkInput(input_fire_left, false, options->controller[i].deviceType, options->controller[i].index); - const bool fire2 = input->checkInput(input_fire_center, false, options->controller[i].deviceType, options->controller[i].index); - const bool fire3 = input->checkInput(input_fire_right, false, options->controller[i].deviceType, options->controller[i].index); + const bool fire1 = input->checkInput(input_fire_left, false, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index); + const bool fire2 = input->checkInput(input_fire_center, false, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index); + const bool fire3 = input->checkInput(input_fire_right, false, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index); if (fire1 || fire2 || fire3) { player->decContinueCounter(); } - i++; } } } @@ -2765,4 +2771,32 @@ void Game::checkPlayersStatusPlaying() break; } } +} + +// Obtiene un jugador a partir de su "id" +Player *Game::getPlayer(int id) +{ + for (auto player : players) + { + if (player->getId() == id) + { + return player; + } + } + + return nullptr; +} + +// Obtiene un controlador a partir del "id" del jugador +int Game::getController(int playerId) +{ + for (int i = 0; i < (int)options->controller.size(); ++i) + { + if (options->controller[i].playerId == playerId) + { + return i; + } + } + + return -1; } \ No newline at end of file diff --git a/source/game.h b/source/game.h index bb69a3c..45d9222 100644 --- a/source/game.h +++ b/source/game.h @@ -437,6 +437,12 @@ private: // Comprueba el estado de juego de los jugadores void checkPlayersStatusPlaying(); + // Obtiene un jugador a partir de su "id" + Player *getPlayer(int id); + + // Obtiene un controlador a partir del "id" del jugador + int getController(int playerId); + public: // Constructor Game(int playerID, int currentStage, Screen *screen, Asset *asset, Lang *lang, Input *input, bool demo, param_t *param, options_t *options, section_t *section, JA_Music_t *music); diff --git a/source/player.cpp b/source/player.cpp index 0fcff27..74d5933 100644 --- a/source/player.cpp +++ b/source/player.cpp @@ -2,16 +2,16 @@ #include "player.h" // Constructor -Player::Player(float x, int y, SDL_Rect *playArea, std::vector texture, std::vector *> animations) +Player::Player(int id, float x, int y, SDL_Rect *playArea, std::vector texture, std::vector *> animations) { // Reserva memoria para los objetos playerSprite = new AnimatedSprite(texture[0], "", animations[0]); powerSprite = new AnimatedSprite(texture[1], "", animations[1]); powerSprite->getTexture()->setAlpha(224); - + // Rectangulo con la zona de juego this->playArea = playArea; - + // Establece la posición inicial del jugador defaultPosX = posX = x; defaultPosY = posY = y; @@ -21,6 +21,7 @@ Player::Player(float x, int y, SDL_Rect *playArea, std::vector textur powerSprite->setPosY(y - (powerSprite->getHeight() - playerSprite->getHeight())); // Inicializa variables + this->id = id; statusPlaying = PLAYER_STATUS_WAITING; scoreBoardPanel = 0; name = ""; @@ -587,4 +588,22 @@ void Player::setName(std::string name) std::string Player::getName() { return name; +} + +// Establece el mando que usará para ser controlado +void Player::setController(int index) +{ + controllerIndex = index; +} + +// Obtiene el mando que usa para ser controlado +int Player::getController() +{ + return controllerIndex; +} + +// Obtiene el "id" del jugador +int Player::getId() +{ + return id; } \ No newline at end of file diff --git a/source/player.h b/source/player.h index 7ac9856..fbb44f8 100644 --- a/source/player.h +++ b/source/player.h @@ -35,6 +35,7 @@ private: SDL_Rect *playArea; // Rectangulo con la zona de juego // Variables + int id; // Numero de identificación para el jugador float posX; // Posicion en el eje X int posY; // Posicion en el eje Y float defaultPosX; // Posición inicial para el jugador @@ -63,6 +64,7 @@ private: Uint32 continueTicks; // Variable para poder cambiar el contador de continue en función del tiempo int scoreBoardPanel; // Panel del marcador asociado al jugador std::string name; // Nombre del jugador + int controllerIndex; // Indice del array de mandos que utilizará para moverse // Actualiza el circulo de colisión a la posición del jugador void shiftColliders(); @@ -75,7 +77,7 @@ private: public: // Constructor - Player(float x, int y, SDL_Rect *playArea, std::vector texture, std::vector *> animations); + Player(int id, float x, int y, SDL_Rect *playArea, std::vector texture, std::vector *> animations); // Destructor ~Player(); @@ -229,4 +231,13 @@ public: // Obtiene el nombre del jugador std::string getName(); + + // Establece el mando que usará para ser controlado + void setController(int index); + + // Obtiene el mando que usa para ser controlado + int getController(); + + // Obtiene el "id" del jugador + int getId(); };