diff --git a/sources/Component/Music/MusicComponent.cpp b/sources/Component/Music/MusicComponent.cpp index 48daf6c3..e1aaca5e 100644 --- a/sources/Component/Music/MusicComponent.cpp +++ b/sources/Component/Music/MusicComponent.cpp @@ -7,25 +7,15 @@ namespace BBM { - MusicComponent::MusicComponent(WAL::Entity &entity, \ -std::map &musicPath) + MusicComponent::MusicComponent(WAL::Entity &entity, std::string &musicPath) : WAL::Component(entity), - _musicIndex(IDLE) + _music(RAY::Audio::Music(musicPath)) { - for (int i = 0; i < DEATH + 1; i++) { - if (musicPath.at(static_cast(i)).empty()) { - this->_isLoad[static_cast(i)] = false; - } else { - this->_isLoad[static_cast(i)] = true; - this->_musicList[static_cast(i)] = RAY::Audio::Music(musicPath.at(static_cast(i))); - } - } } MusicComponent::MusicComponent(WAL::Entity &entity) : Component(entity), - _musicList(), - _musicIndex() + _music() {} WAL::Component *MusicComponent::clone(WAL::Entity &entity) const @@ -35,59 +25,35 @@ std::map &musicPath) void MusicComponent::loadMusic(void) { - if (!this->_isLoad.at(this->_musicIndex)) - return; - if (!this->_musicList[this->_musicIndex].isPlaying()) { - std::cout << this->_musicIndex << std::endl; - this->_musicList[this->_musicIndex].play(); - } + if (!this->_music.isPlaying()) + this->_music.play(); } void MusicComponent::unloadMusic(void) { - if (!this->_isLoad.at(this->_musicIndex)) - return; - if (!this->_musicList[this->_musicIndex].isPlaying()) - this->_musicList[this->_musicIndex].stop(); + if (!this->_music.isPlaying()) + this->_music.stop(); } void MusicComponent::pauseMusic(void) { - if (!this->_isLoad.at(this->_musicIndex)) - return; - this->_musicList[this->_musicIndex].pause(); + this->_music.pause(); } void MusicComponent::setVolume(float &volume) { - if (!this->_isLoad.at(this->_musicIndex)) - return; if (volume >= 0) - this->_musicList[this->_musicIndex].setVolume(volume); + this->_music.setVolume(volume); } void MusicComponent::setPitch(float &pitch) { - if (!this->_isLoad.at(this->_musicIndex)) - return; - this->_musicList[this->_musicIndex].setPitch(pitch); + this->_music.setPitch(pitch); } bool MusicComponent::isPlaying(void) { - if (!this->_isLoad.at(this->_musicIndex)) - return (false); - return (this->_musicList[this->_musicIndex].isPlaying()); - } - - void MusicComponent::setIndex(musicIndex index) - { - this->_musicIndex = index; - } - - MusicComponent::musicIndex MusicComponent::getIndex(void) - { - return (this->_musicIndex); + return (this->_music.isPlaying()); } } // namespace WAL diff --git a/sources/Component/Music/MusicComponent.hpp b/sources/Component/Music/MusicComponent.hpp index 7cb48fed..132179f2 100644 --- a/sources/Component/Music/MusicComponent.hpp +++ b/sources/Component/Music/MusicComponent.hpp @@ -14,21 +14,6 @@ namespace BBM class MusicComponent : public WAL::Component { public: - - enum musicIndex { - IDLE, - JUMP, - BOMB, - MOVE, - HURT, - THROW, - DEATH, - }; - - void setIndex(musicIndex index); - - musicIndex getIndex(); - //! @brief load music void loadMusic(); @@ -47,11 +32,10 @@ namespace BBM //! @brief is music playing bool isPlaying(void); - //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; //! @brief Create a new MusicComponent at a certain Music - MusicComponent(WAL::Entity &entity, std::map &musicPath); + MusicComponent(WAL::Entity &entity, std::string &musicPath); //! @brief A Music component is copy constructable MusicComponent(const MusicComponent &) = default; //! @brief A default destructor @@ -60,11 +44,7 @@ namespace BBM MusicComponent &operator=(const MusicComponent &) = delete; private: //! @brief music of this entity - std::map _musicList; - - std::map _isLoad; - //! musicIndex - musicIndex _musicIndex; + RAY::Audio::Music _music; //! @brief Create a new MusicComponent linked to a specific entity explicit MusicComponent(WAL::Entity &entity); diff --git a/sources/Component/Sound/SoundComponent.cpp b/sources/Component/Sound/SoundComponent.cpp new file mode 100644 index 00000000..eb7b8ad8 --- /dev/null +++ b/sources/Component/Sound/SoundComponent.cpp @@ -0,0 +1,93 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#include +#include "SoundComponent.hpp" + +namespace BBM +{ + SoundComponent::SoundComponent(WAL::Entity &entity, \ +std::map &soundPath) + : WAL::Component(entity), + _soundIndex(IDLE) + { + for (int i = 0; i < DEATH + 1; 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)] = RAY::Audio::Sound(soundPath.at(static_cast(i))); + } + } + } + + SoundComponent::SoundComponent(WAL::Entity &entity) + : Component(entity), + _soundList(), + _soundIndex() + {} + + WAL::Component *SoundComponent::clone(WAL::Entity &entity) const + { + return new SoundComponent(entity); + } + + void SoundComponent::loadSound(void) + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + if (!this->_soundList[this->_soundIndex].isPlaying()) { + std::cout << this->_soundIndex << std::endl; + this->_soundList[this->_soundIndex].play(); + } + } + + void SoundComponent::unloadSound(void) + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + if (!this->_soundList[this->_soundIndex].isPlaying()) + this->_soundList[this->_soundIndex].stop(); + } + + void SoundComponent::pauseSound(void) + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + this->_soundList[this->_soundIndex].pause(); + } + + void SoundComponent::setVolume(float &volume) + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + if (volume >= 0) + this->_soundList[this->_soundIndex].setVolume(volume); + } + + void SoundComponent::setPitch(float &pitch) + { + if (!this->_isLoad.at(this->_soundIndex)) + return; + this->_soundList[this->_soundIndex].setPitch(pitch); + } + + bool SoundComponent::isPlaying(void) + { + if (!this->_isLoad.at(this->_soundIndex)) + return (false); + return (this->_soundList[this->_soundIndex].isPlaying()); + } + + void SoundComponent::setIndex(soundIndex index) + { + this->_soundIndex = index; + } + + SoundComponent::soundIndex SoundComponent::getIndex(void) + { + return (this->_soundIndex); + } + +} // namespace WAL diff --git a/sources/Component/Sound/SoundComponent.hpp b/sources/Component/Sound/SoundComponent.hpp new file mode 100644 index 00000000..1feaa261 --- /dev/null +++ b/sources/Component/Sound/SoundComponent.hpp @@ -0,0 +1,74 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#pragma once + +#include "Component/Component.hpp" +#include +#include "Audio/Sound.hpp" + +namespace BBM +{ + //! @brief A basic Sound component + class SoundComponent : public WAL::Component + { + public: + + enum soundIndex { + IDLE, + JUMP, + BOMB, + MOVE, + HURT, + THROW, + DEATH, + }; + + void setIndex(soundIndex index); + + soundIndex getIndex(); + + //! @brief load Sound + void loadSound(); + + //! @brief unload Sound + void unloadSound(); + + //! @brief put Sound on hold + void pauseSound(); + + //! @brief set Sound volume + void setVolume(float &); + + //! @brief set pitch volume + void setPitch(float &); + + //! @brief is Sound playing + bool isPlaying(void); + + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + //! @brief Create a new SoundComponent at a certain Sound + SoundComponent(WAL::Entity &entity, std::map &SoundPath); + //! @brief A Sound component is copy constructable + SoundComponent(const SoundComponent &) = default; + //! @brief A default destructor + ~SoundComponent() override = default; + //! @brief A Sound component is not assignable + SoundComponent &operator=(const SoundComponent &) = delete; + private: + //! @brief Sound of this entity + std::map _soundList; + + std::map _isLoad; + //! SoundIndex + soundIndex _soundIndex; + //! @brief Create a new SoundComponent linked to a specific entity + explicit SoundComponent(WAL::Entity &entity); + + + }; + +} // namespace BBM \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 74b807e1..3670a151 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -30,6 +30,7 @@ #include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" #include "Component/Music/MusicComponent.hpp" +#include "Component/Sound/SoundComponent.hpp" namespace RAY2D = RAY::Drawables::Drawables2D; namespace RAY3D = RAY::Drawables::Drawables3D; @@ -65,14 +66,14 @@ namespace BBM std::shared_ptr loadGameScene() { auto scene = std::make_shared(); - std::map musicPath= { - {MusicComponent::IDLE, ""}, - {MusicComponent::JUMP, ""}, - {MusicComponent::BOMB, ""}, - {MusicComponent::MOVE, "assets/sounds/new_death.ogg"}, - {MusicComponent::HURT, ""}, - {MusicComponent::THROW, ""}, - {MusicComponent::DEATH, ""} + std::map musicPath= { + {SoundComponent::IDLE, ""}, + {SoundComponent::JUMP, ""}, + {SoundComponent::BOMB, ""}, + {SoundComponent::MOVE, "assets/sounds/new_death.ogg"}, + {SoundComponent::HURT, ""}, + {SoundComponent::THROW, ""}, + {SoundComponent::DEATH, ""} }; scene->addEntity("player") .addComponent() @@ -82,7 +83,7 @@ namespace BBM .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) .addComponent(2) .addComponent() - .addComponent(musicPath); + .addComponent(musicPath); scene->addEntity("cube") .addComponent(-5, 0, -5) .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED)