new: ja es pot començar el joc els dos jugador a l'hora

new: feedback visual per a saber qui ha pulsat start en la pantalla de titol
Player: afegit estat RESPAWNING per no tindre que estar fent cabrioles amb la invulnerabilitat al crear als jugadors
This commit is contained in:
2025-07-14 11:41:12 +02:00
parent e14336bad9
commit 31910b8a74
9 changed files with 309 additions and 112 deletions

View File

@@ -16,6 +16,7 @@
#include "notifier.h" // Para Notifier
#include "options.h" // Para OptionsController, Options, options
#include "param.h" // Para Param, param, ParamGame, ParamTitle
#include "player.h" // Para Player, PlayerState
#include "resource.h" // Para Resource
#include "screen.h" // Para Screen
#include "section.h" // Para Options, Name, name, AttractMode, options
@@ -43,6 +44,7 @@ Title::Title()
fade_->setColor(param.fade.color);
fade_->setType(FadeType::RANDOM_SQUARE);
fade_->setPostDuration(param.fade.post_duration);
initPlayers();
// Asigna valores a otras variables
Section::options = Section::Options::TITLE_1;
@@ -75,6 +77,7 @@ void Title::update()
updateFade();
updateState();
updateStartPrompt();
updatePlayers();
Screen::get()->update();
}
}
@@ -89,6 +92,7 @@ void Title::render()
game_logo_->render();
renderStartPrompt();
renderCopyright();
renderPlayers();
define_buttons_->render();
fade_->render();
@@ -144,38 +148,42 @@ void Title::checkEvents()
// Comprueba las entradas
void Title::checkInput()
{
// Comprueba las entradas solo si no se estan definiendo los botones
if (define_buttons_->isEnabled())
return;
Input::get()->update();
if (!ServiceMenu::get()->isEnabled())
{
// Comprueba las entradas solo si no se estan definiendo los botones
if (!define_buttons_->isEnabled())
// Comprueba todos los métodos de control
for (const auto &CONTROLLER : Options::controllers)
{
// Comprueba todos los métodos de control
for (const auto &CONTROLLER : Options::controllers)
// Boton START
if (Input::get()->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index))
{
// START
if (Input::get()->checkInput(InputAction::START, INPUT_DO_NOT_ALLOW_REPEAT, CONTROLLER.type, CONTROLLER.index))
if ((state_ != TitleState::LOGO_ANIMATING || ALLOW_TITLE_ANIMATION_SKIP))
{
if ((state_ == TitleState::LOGO_FINISHED || ALLOW_TITLE_ANIMATION_SKIP) && !fade_->isEnabled())
if (CONTROLLER.player_id == 1)
{
Audio::get()->playSound("game_start.wav");
Audio::get()->fadeOutMusic(1500);
switch (CONTROLLER.player_id)
if (!player1_start_pressed_)
{
case 1:
selection_ = Section::Options::GAME_PLAY_1P;
break;
case 2:
selection_ = Section::Options::GAME_PLAY_2P;
break;
default:
selection_ = Section::Options::TITLE_TIME_OUT;
break;
player1_start_pressed_ = true;
getPlayer(1)->setPlayingState(PlayerState::TITLE_ANIMATION);
setState(TitleState::START_HAS_BEEN_PRESSED);
counter_ = 0;
}
}
if (CONTROLLER.player_id == 2)
{
if (!player2_start_pressed_)
{
player2_start_pressed_ = true;
getPlayer(2)->setPlayingState(PlayerState::TITLE_ANIMATION);
setState(TitleState::START_HAS_BEEN_PRESSED);
counter_ = 0;
}
state_ = TitleState::START_HAS_BEEN_PRESSED;
counter_ = 0;
return;
}
}
}
@@ -183,10 +191,7 @@ void Title::checkInput()
}
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
if (!define_buttons_->isEnabled())
{
GlobalInputs::check();
}
GlobalInputs::check();
}
// Bucle para el titulo del juego
@@ -208,9 +213,7 @@ void Title::resetCounter() { counter_ = 0; }
void Title::swapControllers()
{
if (Input::get()->getNumControllers() == 0)
{
return;
}
Options::swapControllers();
showControllers();
@@ -259,17 +262,31 @@ void Title::updateFade()
fade_->update();
if (fade_->hasEnded())
{
if (selection_ == Section::Options::TITLE_TIME_OUT)
const int COMBO = (player1_start_pressed_ ? 1 : 0) | (player2_start_pressed_ ? 2 : 0);
switch (COMBO)
{
// El menu ha hecho time out
case 0: // Ningún jugador ha pulsado Start
Section::name = next_section_;
}
else
{
// Se ha pulsado para jugar
break;
case 1: // Solo el jugador 1 ha pulsado Start
Section::name = Section::Name::GAME;
Section::options = selection_;
Section::options = Section::Options::GAME_PLAY_1P;
Audio::get()->stopMusic();
break;
case 2: // Solo el jugador 2 ha pulsado Start
Section::name = Section::Name::GAME;
Section::options = Section::Options::GAME_PLAY_2P;
Audio::get()->stopMusic();
break;
case 3: // Ambos jugadores han pulsado Start
Section::name = Section::Name::GAME;
Section::options = Section::Options::GAME_PLAY_BOTH;
Audio::get()->stopMusic();
break;
}
}
}
@@ -285,8 +302,7 @@ void Title::updateState()
game_logo_->update();
if (game_logo_->hasFinished())
{
state_ = TitleState::LOGO_FINISHED;
Audio::get()->playMusic("title.ogg");
setState(TitleState::LOGO_FINISHED);
}
break;
}
@@ -395,4 +411,94 @@ void Title::renderCopyright()
1,
TITLE_SHADOW_TEXT_COLOR);
}
}
// Cambia el estado
void Title::setState(TitleState state)
{
if (state_ == state)
return;
state_ = state;
switch (state_)
{
case TitleState::LOGO_ANIMATING:
break;
case TitleState::LOGO_FINISHED:
Audio::get()->playMusic("title.ogg");
break;
case TitleState::START_HAS_BEEN_PRESSED:
Audio::get()->fadeOutMusic(1500);
break;
}
}
// Inicializa los jugadores
void Title::initPlayers()
{
std::vector<std::vector<std::shared_ptr<Texture>>> player_textures; // Vector con todas las texturas de los jugadores;
std::vector<std::vector<std::string>> player_animations; // Vector con las animaciones del jugador
// Texturas - Player1
{
std::vector<std::shared_ptr<Texture>> player_texture;
player_texture.emplace_back(Resource::get()->getTexture("player1.gif"));
player_texture.emplace_back(Resource::get()->getTexture("player1_power.png"));
player_textures.push_back(player_texture);
}
// Texturas - Player2
{
std::vector<std::shared_ptr<Texture>> player_texture;
player_texture.emplace_back(Resource::get()->getTexture("player2.gif"));
player_texture.emplace_back(Resource::get()->getTexture("player2_power.png"));
player_textures.push_back(player_texture);
}
// Animaciones -- Jugador
{
player_animations.emplace_back(Resource::get()->getAnimation("player.ani"));
player_animations.emplace_back(Resource::get()->getAnimation("player_power.ani"));
}
// Crea los dos jugadores
constexpr int PLAYER_WIDTH = 32;
const int Y = param.title.press_start_position;
constexpr bool DEMO = false;
players_.emplace_back(std::make_unique<Player>(1, param.game.game_area.first_quarter_x - (PLAYER_WIDTH / 2), Y, DEMO, param.game.play_area.rect, player_textures.at(0), player_animations));
players_.back()->setPlayingState(PlayerState::TITLE_HIDDEN);
players_.emplace_back(std::make_unique<Player>(2, param.game.game_area.third_quarter_x - (PLAYER_WIDTH / 2), Y, DEMO, param.game.play_area.rect, player_textures.at(1), player_animations));
players_.back()->setPlayingState(PlayerState::TITLE_HIDDEN);
}
// Actualza los jugadores
void Title::updatePlayers()
{
for (auto &player : players_)
{
player->update();
}
}
// Renderiza los jugadores
void Title::renderPlayers()
{
for (auto const &player : players_)
{
player->render();
}
}
// Obtiene un jugador a partir de su "id"
std::shared_ptr<Player> Title::getPlayer(int id)
{
auto it = std::find_if(players_.begin(), players_.end(), [id](const auto &player)
{ return player->getId() == id; });
if (it != players_.end())
{
return *it;
}
return nullptr;
}