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
{
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_e> inputs; // Listado de inputs

View File

@@ -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);

View File

@@ -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, &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->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, &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->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++;
}
}
}
@@ -2766,3 +2772,31 @@ void Game::checkPlayersStatusPlaying()
}
}
}
// 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
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);

View File

@@ -2,7 +2,7 @@
#include "player.h"
// 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
playerSprite = new AnimatedSprite(texture[0], "", animations[0]);
@@ -21,6 +21,7 @@ Player::Player(float x, int y, SDL_Rect *playArea, std::vector<Texture *> textur
powerSprite->setPosY(y - (powerSprite->getHeight() - playerSprite->getHeight()));
// Inicializa variables
this->id = id;
statusPlaying = PLAYER_STATUS_WAITING;
scoreBoardPanel = 0;
name = "";
@@ -588,3 +589,21 @@ 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;
}

View File

@@ -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 *> 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
~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();
};