Añadido campo ID al jugador

Las balas y los mandos utilizan ahora este ID
This commit is contained in:
2024-09-11 12:15:18 +02:00
parent 1e2f121d82
commit ecf34558f4
6 changed files with 98 additions and 26 deletions

View File

@@ -143,6 +143,7 @@ struct op_game_t
struct op_controller_t struct op_controller_t
{ {
int index; // Indice en el vector de mandos 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 Uint8 deviceType; // Indica si se utilizará teclado o mando o ambos
std::string name; // Nombre del dispositivo std::string name; // Nombre del dispositivo
std::vector<inputs_e> inputs; // Listado de inputs std::vector<inputs_e> inputs; // Listado de inputs

View File

@@ -502,6 +502,7 @@ void Director::initOptions()
for (int index = 0; index < numPlayers; ++index) for (int index = 0; index < numPlayers; ++index)
{ {
c.index = index; c.index = index;
c.playerId = index + 1;
c.deviceType = INPUT_USE_GAMECONTROLLER; c.deviceType = INPUT_USE_GAMECONTROLLER;
c.name = "NO NAME"; c.name = "NO NAME";
@@ -515,7 +516,7 @@ void Director::initOptions()
c.buttons.clear(); c.buttons.clear();
c.buttons.push_back(SDL_CONTROLLER_BUTTON_X); c.buttons.push_back(SDL_CONTROLLER_BUTTON_X);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_Y); 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_START);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_BACK); c.buttons.push_back(SDL_CONTROLLER_BUTTON_BACK);

View File

@@ -108,14 +108,18 @@ void Game::init(int playerID)
players.clear(); players.clear();
// Crea los dos jugadores // Crea los dos jugadores
Player *player1 = new Player((param->game.playArea.firstQuarterX * ((0 * 2) + 1)) - 11, param->game.playArea.rect.h - 30, &param->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, &param->game.playArea.rect, playerTextures[0], playerAnimations);
player1->setScoreBoardPanel(SCOREBOARD_LEFT_PANEL); player1->setScoreBoardPanel(SCOREBOARD_LEFT_PANEL);
player1->setName(lang->getText(53)); player1->setName(lang->getText(53));
const int controller1 = getController(player1->getId());
player1->setController(controller1);
players.push_back(player1); players.push_back(player1);
Player *player2 = new Player((param->game.playArea.firstQuarterX * ((1 * 2) + 1)) - 11, param->game.playArea.rect.h - 30, &param->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, &param->game.playArea.rect, playerTextures[1], playerAnimations);
player2->setScoreBoardPanel(SCOREBOARD_RIGHT_PANEL); player2->setScoreBoardPanel(SCOREBOARD_RIGHT_PANEL);
player2->setName(lang->getText(54)); player2->setName(lang->getText(54));
const int controller2 = getController(player2->getId());
player2->setController(controller2);
players.push_back(player2); players.push_back(player2);
// Cambia el estado del jugador seleccionado // Cambia el estado del jugador seleccionado
@@ -1401,9 +1405,13 @@ void Game::checkBulletBalloonCollision()
if (checkCollision(balloon->getCollider(), bullet->getCollider())) if (checkCollision(balloon->getCollider(), bullet->getCollider()))
{ {
// Otorga los puntos correspondientes al globo al jugador que disparó la bala // Otorga los puntos correspondientes al globo al jugador que disparó la bala
int index = bullet->getOwner(); Player *player = getPlayer(bullet->getOwner());
players[index]->incScoreMultiplier(); if (!player)
players[index]->addScore(Uint32(balloon->getScore() * players[index]->getScoreMultiplier() * difficultyScoreMultiplier)); {
return;
}
player->incScoreMultiplier();
player->addScore(Uint32(balloon->getScore() * player->getScoreMultiplier() * difficultyScoreMultiplier));
updateHiScore(); updateHiScore();
// Suelta el item si se da el caso // Suelta el item si se da el caso
@@ -1417,7 +1425,7 @@ void Game::checkBulletBalloonCollision()
} }
else else
{ {
createItem(droppeditem, players[index]->getPosX(), 0); createItem(droppeditem, player->getPosX(), 0);
coffeeMachineEnabled = true; coffeeMachineEnabled = true;
} }
} }
@@ -2133,14 +2141,14 @@ void Game::checkInput()
demo.keys.fireLeft = 0; demo.keys.fireLeft = 0;
demo.keys.fireRight = 0; demo.keys.fireRight = 0;
#endif #endif
int i = 0;
for (auto player : players) for (auto player : players)
{ {
const int controllerIndex = player->getController();
const bool autofire = player->isPowerUp() || options->game.autofire; const bool autofire = player->isPowerUp() || options->game.autofire;
if (player->isPlaying()) if (player->isPlaying())
{ {
// Input a la izquierda // 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); player->setInput(input_left);
#ifdef RECORDING #ifdef RECORDING
@@ -2150,7 +2158,7 @@ void Game::checkInput()
else else
{ {
// Input a la derecha // 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); player->setInput(input_right);
#ifdef RECORDING #ifdef RECORDING
@@ -2167,12 +2175,12 @@ void Game::checkInput()
} }
} }
// Comprueba el input de disparar al centro // 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()) if (player->canFire())
{ {
player->setInput(input_fire_center); 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); player->setFireCooldown(10);
// Reproduce el sonido de disparo // Reproduce el sonido de disparo
@@ -2184,12 +2192,12 @@ void Game::checkInput()
} }
// Comprueba el input de disparar a la izquierda // 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()) if (player->canFire())
{ {
player->setInput(input_fire_left); 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); player->setFireCooldown(10);
// Reproduce el sonido de disparo // Reproduce el sonido de disparo
@@ -2201,12 +2209,12 @@ void Game::checkInput()
} }
// Comprueba el input de disparar a la derecha // 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()) if (player->canFire())
{ {
player->setInput(input_fire_right); 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); player->setFireCooldown(10);
// Reproduce el sonido de disparo // Reproduce el sonido de disparo
@@ -2225,12 +2233,11 @@ void Game::checkInput()
} }
} }
#endif #endif
i++;
} }
else else
{ {
// Si no está jugando, el botón de start le permite continuar jugando // 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 // Si no ha entrado ya en el estado de game over, entonces se puede continuar
if (gameOverCounter == GAME_OVER_COUNTER) if (gameOverCounter == GAME_OVER_COUNTER)
@@ -2240,14 +2247,13 @@ void Game::checkInput()
} }
// Si está continuando, los botones de fuego hacen decrementar el contador // 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 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[i].deviceType, options->controller[i].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[i].deviceType, options->controller[i].index); const bool fire3 = input->checkInput(input_fire_right, false, options->controller[controllerIndex].deviceType, options->controller[controllerIndex].index);
if (fire1 || fire2 || fire3) if (fire1 || fire2 || fire3)
{ {
player->decContinueCounter(); player->decContinueCounter();
} }
i++;
} }
} }
} }
@@ -2765,4 +2771,32 @@ void Game::checkPlayersStatusPlaying()
break; 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;
} }

View File

@@ -437,6 +437,12 @@ private:
// Comprueba el estado de juego de los jugadores // Comprueba el estado de juego de los jugadores
void checkPlayersStatusPlaying(); 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: public:
// Constructor // 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); 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);

View File

@@ -2,16 +2,16 @@
#include "player.h" #include "player.h"
// Constructor // Constructor
Player::Player(float x, int y, SDL_Rect *playArea, std::vector<Texture *> texture, std::vector<std::vector<std::string> *> animations) Player::Player(int id, float x, int y, SDL_Rect *playArea, std::vector<Texture *> texture, std::vector<std::vector<std::string> *> animations)
{ {
// Reserva memoria para los objetos // Reserva memoria para los objetos
playerSprite = new AnimatedSprite(texture[0], "", animations[0]); playerSprite = new AnimatedSprite(texture[0], "", animations[0]);
powerSprite = new AnimatedSprite(texture[1], "", animations[1]); powerSprite = new AnimatedSprite(texture[1], "", animations[1]);
powerSprite->getTexture()->setAlpha(224); powerSprite->getTexture()->setAlpha(224);
// Rectangulo con la zona de juego // Rectangulo con la zona de juego
this->playArea = playArea; this->playArea = playArea;
// Establece la posición inicial del jugador // Establece la posición inicial del jugador
defaultPosX = posX = x; defaultPosX = posX = x;
defaultPosY = posY = y; defaultPosY = posY = y;
@@ -21,6 +21,7 @@ Player::Player(float x, int y, SDL_Rect *playArea, std::vector<Texture *> textur
powerSprite->setPosY(y - (powerSprite->getHeight() - playerSprite->getHeight())); powerSprite->setPosY(y - (powerSprite->getHeight() - playerSprite->getHeight()));
// Inicializa variables // Inicializa variables
this->id = id;
statusPlaying = PLAYER_STATUS_WAITING; statusPlaying = PLAYER_STATUS_WAITING;
scoreBoardPanel = 0; scoreBoardPanel = 0;
name = ""; name = "";
@@ -587,4 +588,22 @@ void Player::setName(std::string name)
std::string Player::getName() std::string Player::getName()
{ {
return name; 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;
} }

View File

@@ -35,6 +35,7 @@ private:
SDL_Rect *playArea; // Rectangulo con la zona de juego SDL_Rect *playArea; // Rectangulo con la zona de juego
// Variables // Variables
int id; // Numero de identificación para el jugador
float posX; // Posicion en el eje X float posX; // Posicion en el eje X
int posY; // Posicion en el eje Y int posY; // Posicion en el eje Y
float defaultPosX; // Posición inicial para el jugador 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 Uint32 continueTicks; // Variable para poder cambiar el contador de continue en función del tiempo
int scoreBoardPanel; // Panel del marcador asociado al jugador int scoreBoardPanel; // Panel del marcador asociado al jugador
std::string name; // Nombre del 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 // Actualiza el circulo de colisión a la posición del jugador
void shiftColliders(); void shiftColliders();
@@ -75,7 +77,7 @@ private:
public: public:
// Constructor // Constructor
Player(float x, int y, SDL_Rect *playArea, std::vector<Texture *> texture, std::vector<std::vector<std::string> *> animations); Player(int id, float x, int y, SDL_Rect *playArea, std::vector<Texture *> texture, std::vector<std::vector<std::string> *> animations);
// Destructor // Destructor
~Player(); ~Player();
@@ -229,4 +231,13 @@ public:
// Obtiene el nombre del jugador // Obtiene el nombre del jugador
std::string getName(); 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();
}; };