treballant en la classe Audio

This commit is contained in:
2025-03-28 23:27:33 +01:00
parent 31a0ad6fd5
commit d2417f48d9
29 changed files with 321 additions and 253 deletions

View File

@@ -18,8 +18,7 @@
#include "global_inputs.h" // Para check, update
#include "input.h" // Para InputAction, Input, INPUT_DO_NOT_A...
#include "item.h" // Para Item, ItemType
#include "jail_audio.h" // Para JA_PlaySound, JA_GetMusicState
#include "lang.h" // Para getText
#include "audio.h" // Para Audio::icStateplaynclude "lang.h" // Para getText
#include "manage_hiscore_table.h" // Para ManageHiScoreTable, HiScoreEntry
#include "notifier.h" // Para Notifier
#include "param.h" // Para Param, param, ParamGame, ParamFade
@@ -34,11 +33,13 @@
#include "tabe.h" // Para Tabe, TabeState
#include "text.h" // Para Text
#include "texture.h" // Para Texture
#include "lang.h"
// Constructor
Game::Game(int player_id, int current_stage, bool demo)
: renderer_(Screen::get()->getRenderer()),
screen_(Screen::get()),
audio_(Audio::get()),
asset_(Asset::get()),
input_(Input::get()),
background_(std::make_unique<Background>()),
@@ -99,19 +100,18 @@ Game::Game(int player_id, int current_stage, bool demo)
Game::~Game()
{
// MODO DEMO
// [Modo DEMO] Vuelve a activar los sonidos
if (demo_.enabled)
{
// Habilita los sonidos
JA_EnableSound(true);
audio_->enableSound();
}
// MODO JUEGO
else
{
// Guarda las puntuaciones en un fichero
// [Modo JUEGO] Guarda puntuaciones y transita a modo título
auto manager = std::make_unique<ManageHiScoreTable>(options.game.hi_score_table);
manager->saveToFile(asset_->get("score.bin"));
section::attract_mode = section::AttractMode::TITLE_TO_DEMO;
audio_->stopMusic();
}
#ifdef RECORDING
@@ -203,7 +203,7 @@ void Game::updateHiScore()
if (hi_score_achieved_ == false)
{
hi_score_achieved_ = true;
JA_PlaySound(Resource::get()->getSound("hi_score_achieved.wav"));
audio_->playSound("hi_score_achieved.wav");
}
}
}
@@ -261,7 +261,7 @@ void Game::updateStage()
// Cambio de fase
Stage::power = Stage::get(Stage::number).power_to_complete - Stage::power;
++Stage::number;
JA_PlaySound(Resource::get()->getSound("stage_change.wav"));
audio_->playSound("stage_change.wav");
balloon_manager_->resetBalloonSpeed();
screen_->flash(FLASH_COLOR, 3);
screen_->shake();
@@ -313,7 +313,7 @@ void Game::updateGameStateGameOver()
if (game_over_counter_ == GAME_OVER_COUNTER_)
{
createMessage({paths_.at(2), paths_.at(3)}, Resource::get()->getTexture("game_text_game_over"));
JA_FadeOutMusic(1000);
audio_->fadeOutMusic(1000);
balloon_manager_->setSounds(true);
}
@@ -329,8 +329,8 @@ void Game::updateGameStateGameOver()
{
if (options.audio.enabled)
{
const float vol = static_cast<float>(64 * (100 - fade_out_->getValue())) / 100.0f;
JA_SetSoundVolume(to_JA_volume(static_cast<int>(vol)));
const float VOL = static_cast<float>(64 * (100 - fade_out_->getValue())) / 100.0f;
audio_->setSoundVolume(static_cast<int>(VOL));
}
}
@@ -349,8 +349,8 @@ void Game::updateGameStateGameOver()
section::options = section::Options::HI_SCORE_AFTER_PLAYING;
if (options.audio.enabled)
{
JA_StopChannel(-1);
JA_SetSoundVolume(to_JA_volume(options.audio.sound.volume));
audio_->stopAllSounds();
audio_->setSoundVolume(options.audio.sound.volume);
}
}
}
@@ -483,7 +483,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
player->addScore(1000);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_.at(0)->getWidth()) / 2;
createItemText(x, game_text_textures_.at(0));
JA_PlaySound(Resource::get()->getSound("item_pickup.wav"));
audio_->playSound("item_pickup.wav");
break;
}
case ItemType::GAVINA:
@@ -491,7 +491,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
player->addScore(2500);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_.at(1)->getWidth()) / 2;
createItemText(x, game_text_textures_.at(1));
JA_PlaySound(Resource::get()->getSound("item_pickup.wav"));
audio_->playSound("item_pickup.wav");
break;
}
case ItemType::PACMAR:
@@ -499,7 +499,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
player->addScore(5000);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_.at(2)->getWidth()) / 2;
createItemText(x, game_text_textures_.at(2));
JA_PlaySound(Resource::get()->getSound("item_pickup.wav"));
audio_->playSound("item_pickup.wav");
break;
}
case ItemType::DEBIAN:
@@ -507,7 +507,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
player->addScore(100000);
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_.at(6)->getWidth()) / 2;
createItemText(x, game_text_textures_.at(6));
JA_PlaySound(Resource::get()->getSound("debian_pickup.wav"));
audio_->playSound("debian_pickup.wav");
break;
}
case ItemType::CLOCK:
@@ -515,7 +515,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
enableTimeStopItem();
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_.at(5)->getWidth()) / 2;
createItemText(x, game_text_textures_.at(5));
JA_PlaySound(Resource::get()->getSound("item_pickup.wav"));
audio_->playSound("item_pickup.wav");
break;
}
case ItemType::COFFEE:
@@ -532,7 +532,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_.at(4)->getWidth()) / 2;
createItemText(x, game_text_textures_.at(4));
}
JA_PlaySound(Resource::get()->getSound("voice_coffee.wav"));
audio_->playSound("voice_coffee.wav");
break;
}
case ItemType::COFFEE_MACHINE:
@@ -541,7 +541,7 @@ void Game::checkPlayerItemCollision(std::shared_ptr<Player> &player)
coffee_machine_enabled_ = false;
const auto x = item->getPosX() + (item->getWidth() - game_text_textures_.at(3)->getWidth()) / 2;
createItemText(x, game_text_textures_.at(3));
JA_PlaySound(Resource::get()->getSound("voice_power_up.wav"));
audio_->playSound("voice_power_up.wav");
break;
}
default:
@@ -570,20 +570,15 @@ void Game::checkBulletCollision()
if (tabe_->tryToGetBonus())
{
createItem(ItemType::DEBIAN, pos.x, pos.y);
JA_PlaySound(Resource::get()->getSound("debian_drop.wav"));
audio_->playSound("debian_drop.wav");
}
else
{
if (rand() % 3 == 0)
{
createItem(ItemType::COFFEE, pos.x, pos.y);
// JA_PlaySound(Resource::get()->getSound("item_drop.wav"));
}
else
{
// JA_PlaySound(Resource::get()->getSound("tabe_hit.wav"));
}
JA_PlaySound(Resource::get()->getSound("tabe_hit.wav"));
audio_->playSound("tabe_hit.wav");
}
break;
}
@@ -605,7 +600,7 @@ void Game::checkBulletCollision()
if (dropped_item != ItemType::COFFEE_MACHINE)
{
createItem(dropped_item, balloon->getPosX(), balloon->getPosY());
JA_PlaySound(Resource::get()->getSound("item_drop.wav"));
audio_->playSound("item_drop.wav");
}
else
{
@@ -627,7 +622,7 @@ void Game::checkBulletCollision()
updateHiScore();
// Sonido de explosión
JA_PlaySound(Resource::get()->getSound("balloon.wav"));
audio_->playSound("balloon.wav");
// Deshabilita la bala
bullet->disable();
@@ -683,7 +678,7 @@ void Game::updateItems()
item->update();
if (item->isOnFloor())
{
JA_PlaySound(Resource::get()->getSound("title.wav"));
audio_->playSound("title.wav");
screen_->shake();
}
}
@@ -910,16 +905,16 @@ void Game::killPlayer(std::shared_ptr<Player> &player)
// Lo pierde
player->removeExtraHit();
throwCoffee(player->getPosX() + (player->getWidth() / 2), player->getPosY() + (player->getHeight() / 2));
JA_PlaySound(Resource::get()->getSound("coffee_out.wav"));
audio_->playSound("coffee_out.wav");
screen_->shake();
}
else
{
// Si no tiene cafes, muere
balloon_manager_->stopAllBalloons();
JA_PlaySound(Resource::get()->getSound("player_collision.wav"));
audio_->playSound("player_collision.wav");
screen_->shake();
JA_PlaySound(Resource::get()->getSound("voice_no.wav"));
audio_->playSound("voice_no.wav");
player->setPlayingState(PlayerState::DYING);
if (allPlayersAreNotPlaying())
{
@@ -939,7 +934,7 @@ void Game::updateTimeStopped()
{
if (time_stopped_counter_ % 30 == 0)
{
JA_PlaySound(Resource::get()->getSound("clock.wav"));
audio_->playSound("clock.wav");
}
}
else
@@ -947,12 +942,12 @@ void Game::updateTimeStopped()
if (time_stopped_counter_ % 30 == 0)
{
balloon_manager_->normalColorsToAllBalloons();
JA_PlaySound(Resource::get()->getSound("clock.wav"));
audio_->playSound("clock.wav");
}
else if (time_stopped_counter_ % 30 == 15)
{
balloon_manager_->reverseColorsToAllBalloons();
JA_PlaySound(Resource::get()->getSound("clock.wav"));
audio_->playSound("clock.wav");
}
}
}
@@ -1106,9 +1101,6 @@ void Game::run()
checkEvents(); // Tiene que ir antes del render
render();
}
// Vuelve a dejar el sonido como estaba
(demo_.enabled) ? JA_EnableSound(options.audio.sound.enabled) : JA_StopMusic();
}
// Inicializa las variables que contienen puntos de ruta para mover objetos
@@ -1438,7 +1430,7 @@ void Game::handleFireInput(const std::shared_ptr<Player> &player, BulletType bul
player->setInput(bulletType == BulletType::UP ? InputAction::FIRE_CENTER : bulletType == BulletType::LEFT ? InputAction::FIRE_LEFT
: InputAction::FIRE_RIGHT);
createBullet(player->getPosX() + (player->getWidth() / 2) - 6, player->getPosY() + (player->getHeight() / 2), bulletType, player->isPowerUp(), player->getId());
JA_PlaySound(Resource::get()->getSound("bullet.wav"));
audio_->playSound("bullet.wav");
// Establece un tiempo de espera para el próximo disparo.
const int cooldown = player->isPowerUp() ? 5 : options.game.autofire ? 10
@@ -1602,18 +1594,18 @@ void Game::initDemo(int player_id)
// Aleatoriza la asignación del fichero con los datos del modo demostracion
{
const auto demo1 = rand() % 2;
const auto demo2 = (demo1 == 0) ? 1 : 0;
demo_.data.emplace_back(Resource::get()->getDemoData(demo1));
demo_.data.emplace_back(Resource::get()->getDemoData(demo2));
const auto DEMO1 = rand() % 2;
const auto DEMO2 = (DEMO1 == 0) ? 1 : 0;
demo_.data.emplace_back(Resource::get()->getDemoData(DEMO1));
demo_.data.emplace_back(Resource::get()->getDemoData(DEMO2));
}
// Selecciona una pantalla al azar
{
constexpr auto demos = 3;
const auto demo = rand() % demos;
const int stages[demos] = {0, 3, 5};
Stage::number = stages[demo];
constexpr auto NUM_DEMOS = 3;
const auto DEMO = rand() % NUM_DEMOS;
const int STAGES[NUM_DEMOS] = {0, 3, 5};
Stage::number = STAGES[DEMO];
}
// Actualiza el numero de globos explotados según la fase del modo demostración
@@ -1625,8 +1617,8 @@ void Game::initDemo(int player_id)
// Activa o no al otro jugador
if (rand() % 3 != 0)
{
const auto other_player_id = player_id == 1 ? 2 : 1;
auto other_player = getPlayer(other_player_id);
const auto OTHER_PLAYER_ID = player_id == 1 ? 2 : 1;
auto other_player = getPlayer(OTHER_PLAYER_ID);
other_player->setPlayingState(PlayerState::PLAYING);
}
@@ -1640,7 +1632,7 @@ void Game::initDemo(int player_id)
}
// Deshabilita los sonidos
JA_EnableSound(false);
audio_->disableSound();
// Configura los marcadores
scoreboard_->setMode(SCOREBOARD_LEFT_PANEL, ScoreboardMode::DEMO);
@@ -1739,32 +1731,10 @@ void Game::initPlayers(int player_id)
player->setInvulnerable(false);
}
// Pausa la música
void Game::pauseMusic()
{
if (JA_GetMusicState() == JA_MUSIC_PLAYING && !demo_.enabled)
{
JA_PauseMusic();
}
}
// Reanuda la música
void Game::resumeMusic()
{
if (JA_GetMusicState() == JA_MUSIC_PAUSED && !demo_.enabled)
{
JA_ResumeMusic();
}
}
// Hace sonar la música
void Game::playMusic()
{
// Si la música no está sonando
if (JA_GetMusicState() == JA_MUSIC_INVALID || JA_GetMusicState() == JA_MUSIC_STOPPED)
{
JA_PlayMusic(Resource::get()->getMusic("playing.ogg"));
}
audio_->playMusic("playing.ogg");
}
// Detiene la música
@@ -1772,7 +1742,7 @@ void Game::stopMusic()
{
if (!demo_.enabled)
{
JA_StopMusic();
audio_->stopMusic();
}
}
@@ -1856,7 +1826,7 @@ void Game::updateGameStateEnteringPlayer()
{
setState(GameState::SHOWING_GET_READY_MESSAGE);
createMessage({paths_.at(0), paths_.at(1)}, Resource::get()->getTexture("game_text_get_ready"));
JA_PlaySound(Resource::get()->getSound("voice_get_ready.wav"));
audio_->playSound("voice_get_ready.wav");
}
}
}
@@ -1909,7 +1879,7 @@ void Game::updateGameStatePlaying()
checkAndUpdateBalloonSpeed();
checkState();
cleanVectors();
playMusic();
//playMusic();
}
// Vacía los vectores de elementos deshabilitados