diff --git a/CMakeLists.txt b/CMakeLists.txt index e919b9f0..3bc882f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,6 +79,8 @@ set(SOURCES sources/Component/Sound/SoundComponent.cpp sources/System/Sound/PlayerSoundManagerSystem.cpp sources/System/Sound/PlayerSoundManagerSystem.hpp + sources/System/Music/MusicSystem.hpp + sources/System/Music/MusicSystem.cpp ) add_executable(bomberman sources/main.cpp diff --git a/lib/Ray/sources/Audio/Music.cpp b/lib/Ray/sources/Audio/Music.cpp index db3dae78..f58921bc 100644 --- a/lib/Ray/sources/Audio/Music.cpp +++ b/lib/Ray/sources/Audio/Music.cpp @@ -55,3 +55,9 @@ RAY::Audio::Music &RAY::Audio::Music::setPitch(float pitch) SetMusicPitch(*_music, pitch); return *this; } + +RAY::Audio::Music &RAY::Audio::Music::updateMusicStream(void) +{ + UpdateMusicStream(*_music); + return *this; +} diff --git a/lib/Ray/sources/Audio/Music.hpp b/lib/Ray/sources/Audio/Music.hpp index c28e8089..66ab0162 100644 --- a/lib/Ray/sources/Audio/Music.hpp +++ b/lib/Ray/sources/Audio/Music.hpp @@ -51,6 +51,8 @@ namespace RAY::Audio // Set pitch for a Music (1.0 is base level) Music &setPitch(float pitch) override; + Music &updateMusicStream(void); + private: std::shared_ptr<::Music> _music; diff --git a/sources/Component/Music/MusicComponent.cpp b/sources/Component/Music/MusicComponent.cpp index 339e040d..fe3867cb 100644 --- a/sources/Component/Music/MusicComponent.cpp +++ b/sources/Component/Music/MusicComponent.cpp @@ -7,49 +7,59 @@ namespace BBM { - MusicComponent::MusicComponent(WAL::Entity &entity, const std::string &musicPath) - : WAL::Component(entity), - _musicPath(musicPath), - _music(RAY::Audio::Music(musicPath)) - { - } + MusicComponent::MusicComponent(WAL::Entity &entity, const std::string &musicPath) + : WAL::Component(entity), + _musicPath(musicPath), + _music(RAY::Audio::Music(musicPath)) + { + } - WAL::Component *MusicComponent::clone(WAL::Entity &entity) const + WAL::Component *MusicComponent::clone(WAL::Entity &entity) const { return new MusicComponent(entity, this->_musicPath); } - void MusicComponent::loadMusic(void) - { - if (!this->_music.isPlaying()) - this->_music.play(); - } + void MusicComponent::playMusic(void) + { + if (!this->_music.isPlaying()) { + this->_music.play(); + this->_music.updateMusicStream(); + } + } - void MusicComponent::unloadMusic(void) - { - if (!this->_music.isPlaying()) - this->_music.stop(); - } + void MusicComponent::stopMusic(void) + { + if (!this->_music.isPlaying()) { + this->_music.stop(); + this->_music.updateMusicStream(); + } + } - void MusicComponent::pauseMusic(void) - { - this->_music.pause(); - } + void MusicComponent::pauseMusic(void) + { + this->_music.pause(); + } - void MusicComponent::setVolume(float &volume) - { - if (volume >= 0) - this->_music.setVolume(volume); - } + void MusicComponent::setVolume(float &volumeUpdate) + { + if (volumeUpdate >= 0) { + this->_music.setVolume(volume); + } + } void MusicComponent::setPitch(float &pitch) - { - this->_music.setPitch(pitch); - } + { + this->_music.setPitch(pitch); + } bool MusicComponent::isPlaying(void) - { - return (this->_music.isPlaying()); - } + { + return (this->_music.isPlaying()); + } + + void MusicComponent::updateMusicStream(void) + { + this->_music.updateMusicStream(); + } } // namespace WAL diff --git a/sources/Component/Music/MusicComponent.hpp b/sources/Component/Music/MusicComponent.hpp index 835a8e5c..5761437b 100644 --- a/sources/Component/Music/MusicComponent.hpp +++ b/sources/Component/Music/MusicComponent.hpp @@ -14,11 +14,11 @@ namespace BBM class MusicComponent : public WAL::Component { public: - //! @brief load music - void loadMusic(); + //! @brief start music + void playMusic(); - //! @brief unload music - void unloadMusic(); + //! @brief stop music + void stopMusic(); //! @brief put music on hold void pauseMusic(); @@ -31,7 +31,8 @@ namespace BBM //! @brief is music playing bool isPlaying(void); - + + void updateMusicStream(void); //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; //! @brief Create a new MusicComponent at a certain Music @@ -42,7 +43,8 @@ namespace BBM ~MusicComponent() override = default; //! @brief A Music component is not assignable MusicComponent &operator=(const MusicComponent &) = delete; - + //! @brief Volume of the muisc + float volume = 1; private: //! @brief music of this entity RAY::Audio::Music _music; diff --git a/sources/Component/Sound/SoundComponent.cpp b/sources/Component/Sound/SoundComponent.cpp index a6a31323..dd492c03 100644 --- a/sources/Component/Sound/SoundComponent.cpp +++ b/sources/Component/Sound/SoundComponent.cpp @@ -8,80 +8,81 @@ namespace BBM { - SoundComponent::SoundComponent(WAL::Entity &entity, \ + SoundComponent::SoundComponent(WAL::Entity &entity, \ const std::map &soundPath) - : WAL::Component(entity), - _soundIndex(IDLE), - _soundPath(soundPath) - { - for (int i = 0; i <= DEATH; i++) { - if (soundPath.at(static_cast(i)).empty()) { - this->_isLoad[static_cast(i)] = false; - } else { - this->_isLoad[static_cast(i)] = true; - this->_soundList[static_cast(i)] = std::make_unique(soundPath.at(static_cast(i))); - } - } - } + : WAL::Component(entity), + _soundIndex(IDLE), + _soundPath(soundPath) + { + for (int i = 0; i <= DEATH; i++) { + if (soundPath.at(static_cast(i)).empty()) { + this->_isLoad[static_cast(i)] = false; + } else { + this->_isLoad[static_cast(i)] = true; + this->_soundList[static_cast(i)] = std::make_unique(soundPath.at(static_cast(i))); + } + } + } - WAL::Component *SoundComponent::clone(WAL::Entity &entity) const + WAL::Component *SoundComponent::clone(WAL::Entity &entity) const { return new SoundComponent(entity, this->_soundPath); } void SoundComponent::playSound() - { - if (!this->_isLoad.at(this->_soundIndex)) - return; - if (!this->_soundList[this->_soundIndex].get()->isPlaying()) - this->_soundList[this->_soundIndex].get()->play(); - } + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + if (!this->_soundList[this->_soundIndex].get()->isPlaying()) + this->_soundList[this->_soundIndex].get()->play(); + } - void SoundComponent::stopSound() - { - if (!this->_isLoad.at(this->_soundIndex)) - return; - if (this->_soundList[this->_soundIndex].get()->isPlaying()) - this->_soundList[this->_soundIndex].get()->stop(); - } + void SoundComponent::stopSound() + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + if (this->_soundList[this->_soundIndex].get()->isPlaying()) + this->_soundList[this->_soundIndex].get()->stop(); + } - void SoundComponent::pauseSound() - { - if (!this->_isLoad.at(this->_soundIndex)) - return; - this->_soundList[this->_soundIndex].get()->pause(); - } + void SoundComponent::pauseSound() + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + this->_soundList[this->_soundIndex].get()->pause(); + } - void SoundComponent::setVolume(float &volume) - { - if (!this->_isLoad.at(this->_soundIndex)) - return; - if (volume >= 0) - this->_soundList[this->_soundIndex].get()->setVolume(volume); - } + void SoundComponent::setVolume(float &volumeUpdate) + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + if (volumeUpdate >= 0) + this->volume = volumeUpdate; + this->_soundList[this->_soundIndex].get()->setVolume(this->volume); + } void SoundComponent::setPitch(float &pitch) - { - if (!this->_isLoad.at(this->_soundIndex)) - return; - this->_soundList[this->_soundIndex].get()->setPitch(pitch); - } + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + this->_soundList[this->_soundIndex].get()->setPitch(pitch); + } bool SoundComponent::isPlaying() - { - if (!this->_isLoad.at(this->_soundIndex)) - return (false); - return (this->_soundList[this->_soundIndex].get()->isPlaying()); - } - - void SoundComponent::setIndex(soundIndex index) - { - this->_soundIndex = index; - } + { + if (!this->_isLoad.at(this->_soundIndex)) + return (false); + return (this->_soundList[this->_soundIndex].get()->isPlaying()); + } + + void SoundComponent::setIndex(soundIndex index) + { + this->_soundIndex = index; + } - SoundComponent::soundIndex SoundComponent::getIndex() - { - return (this->_soundIndex); - } + SoundComponent::soundIndex SoundComponent::getIndex() + { + return (this->_soundIndex); + } } // namespace WAL diff --git a/sources/Component/Sound/SoundComponent.hpp b/sources/Component/Sound/SoundComponent.hpp index 1df2a537..a953f6da 100644 --- a/sources/Component/Sound/SoundComponent.hpp +++ b/sources/Component/Sound/SoundComponent.hpp @@ -15,6 +15,7 @@ namespace BBM { public: + //! @brief All sounds of the player enum soundIndex { IDLE, JUMP, @@ -25,15 +26,17 @@ namespace BBM DEATH, }; + //! @brief to set what sound should be played void setIndex(soundIndex index); + //! @brief to know which sound is selected soundIndex getIndex(); - //! @brief load Sound + //! @brief start sound void playSound(); - //! @brief unload Sound - void stopSound(); + //! @brief stop sound + void stopSound(); //! @brief put Sound on hold void pauseSound(); @@ -57,14 +60,16 @@ namespace BBM ~SoundComponent() override = default; //! @brief A Sound component is not assignable SoundComponent &operator=(const SoundComponent &) = delete; + //! @brief Volume of the sounds + float volume = 1; + private: - //! @brief Sound of this entity + //! @brief Sounds of this entity std::map> _soundList; - + //! @brief map to know if sound is loaded std::map _isLoad; - + //! @brief All sounds path const std::map _soundPath; - //! SoundIndex soundIndex _soundIndex; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index d977d757..244c3ae5 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -30,6 +30,7 @@ #include "Component/Music/MusicComponent.hpp" #include "Component/Sound/SoundComponent.hpp" #include "System/Sound/PlayerSoundManagerSystem.hpp" +#include "System/Music/MusicSystem.hpp" namespace RAY3D = RAY::Drawables::Drawables3D; @@ -52,7 +53,8 @@ namespace BBM .addSystem() .addSystem() .addSystem() - .addSystem(); + .addSystem() + .addSystem(); } void enableRaylib(WAL::Wal &wal) @@ -69,12 +71,12 @@ namespace BBM auto scene = std::make_shared(); std::map soundPath= { {SoundComponent::IDLE, ""}, - {SoundComponent::JUMP, "assets/sounds/death.ogg"}, - {SoundComponent::BOMB, ""}, + {SoundComponent::JUMP, "assets/sounds/jump.wav"}, + {SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"}, {SoundComponent::MOVE, "assets/sounds/jump.wav"}, {SoundComponent::HURT, ""}, {SoundComponent::THROW, ""}, - {SoundComponent::DEATH, ""} + {SoundComponent::DEATH, "assets/sounds/death.ogg"} }; scene->addEntity("player") .addComponent() @@ -85,7 +87,7 @@ namespace BBM .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(1) .addComponent() - //.addComponent("assets/musics/music_win.ogg"); + .addComponent("assets/musics/music_win.ogg") .addComponent(soundPath) .addComponent(1, [](WAL::Entity &entity) { auto &animation = entity.getComponent(); diff --git a/sources/System/Music/MusicSystem.cpp b/sources/System/Music/MusicSystem.cpp new file mode 100644 index 00000000..94cb7c7e --- /dev/null +++ b/sources/System/Music/MusicSystem.cpp @@ -0,0 +1,24 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#include "MusicSystem.hpp" +#include + +namespace BBM { + + MusicSystem::MusicSystem(WAL::Wal &wal) + : System(wal) + {} + + void MusicSystem::onFixedUpdate(WAL::ViewEntity &entity) + { + auto &music = entity.get(); + + music.setVolume(music.volume); + if (!music.isPlaying()) + music.playMusic(); + music.updateMusicStream(); + + } +} \ No newline at end of file diff --git a/sources/System/Music/MusicSystem.hpp b/sources/System/Music/MusicSystem.hpp new file mode 100644 index 00000000..ce65321c --- /dev/null +++ b/sources/System/Music/MusicSystem.hpp @@ -0,0 +1,31 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#pragma once + +#include "System/System.hpp" +#include "Window.hpp" +#include "Component/Music/MusicComponent.hpp" +#include "Component/Health/HealthComponent.hpp" +#include +#include "Wal.hpp" + +namespace BBM +{ + class MusicSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::ViewEntity &entity) override; + + //! @brief ctor + MusicSystem(WAL::Wal &wal); + //! @brief Default copy ctor + MusicSystem(const MusicSystem &) = default; + //! @brief Default dtor + ~MusicSystem() override = default; + //! @brief A SoundManager screen system can't be assigned. + MusicSystem &operator=(const MusicSystem &) = delete; + }; +} diff --git a/sources/System/Sound/PlayerSoundManagerSystem.cpp b/sources/System/Sound/PlayerSoundManagerSystem.cpp index 6b50f31f..487a4081 100644 --- a/sources/System/Sound/PlayerSoundManagerSystem.cpp +++ b/sources/System/Sound/PlayerSoundManagerSystem.cpp @@ -16,7 +16,8 @@ namespace BBM { const auto &controllable = entity.get(); auto &sound = entity.get(); auto &health = entity.get(); - + + sound.setVolume(sound.volume); std::map soundIndex = { {controllable.bomb, SoundComponent::BOMB}, {controllable.jump, SoundComponent::JUMP}, diff --git a/sources/System/Sound/PlayerSoundManagerSystem.hpp b/sources/System/Sound/PlayerSoundManagerSystem.hpp index 2f1967ff..42869888 100644 --- a/sources/System/Sound/PlayerSoundManagerSystem.hpp +++ b/sources/System/Sound/PlayerSoundManagerSystem.hpp @@ -16,7 +16,7 @@ namespace BBM class SoundManagerSystem : public WAL::System { public: - //! @inherit + //! @inherit void onFixedUpdate(WAL::ViewEntity &entity) override; //! @brief ctor