diff --git a/CMakeLists.txt b/CMakeLists.txt index d8a9e63e..f47d2a11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,14 @@ set(SOURCES sources/System/Animator/AnimatorSystem.cpp sources/System/Animator/AnimatorSystem.hpp sources/Component/Tag/TagComponent.hpp + sources/Component/Music/MusicComponent.cpp + sources/Component/Music/MusicComponent.hpp + sources/Component/Sound/SoundComponent.hpp + 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/assets/map/bumper.png b/assets/map/bumper.png index a86b67f2..63632844 100644 Binary files a/assets/map/bumper.png and b/assets/map/bumper.png differ diff --git a/assets/musics/music_win.ogg b/assets/musics/music_win.ogg index eadd8eff..ee5195e3 100644 Binary files a/assets/musics/music_win.ogg and b/assets/musics/music_win.ogg differ diff --git a/assets/sounds/bomb_drop.ogg b/assets/sounds/bomb_drop.ogg new file mode 100644 index 00000000..40288f0e Binary files /dev/null and b/assets/sounds/bomb_drop.ogg differ diff --git a/assets/sounds/death.ogg b/assets/sounds/death.ogg new file mode 100644 index 00000000..b5822604 Binary files /dev/null and b/assets/sounds/death.ogg differ diff --git a/assets/sounds/fuse.ogg b/assets/sounds/fuse.ogg new file mode 100644 index 00000000..1d3007e8 Binary files /dev/null and b/assets/sounds/fuse.ogg differ diff --git a/assets/sounds/jump.wav b/assets/sounds/jump.wav new file mode 100644 index 00000000..642ae89a Binary files /dev/null and b/assets/sounds/jump.wav differ diff --git a/assets/sounds/move.ogg b/assets/sounds/move.ogg new file mode 100644 index 00000000..fc035509 Binary files /dev/null and b/assets/sounds/move.ogg differ 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/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index df332bb6..a9a1d9bf 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -49,6 +49,7 @@ bool RAY::Window::open(void) } InitWindow(this->_dimensions.x, this->_dimensions.y, this->_title.c_str()); this->_isOpen = true; + InitAudioDevice(); return true; } @@ -60,6 +61,7 @@ bool RAY::Window::shouldClose(void) const void RAY::Window::close(void) { CloseWindow(); + CloseAudioDevice(); } bool RAY::Window::isFocused(void) const diff --git a/sources/Component/Music/MusicComponent.cpp b/sources/Component/Music/MusicComponent.cpp new file mode 100644 index 00000000..40dc33b1 --- /dev/null +++ b/sources/Component/Music/MusicComponent.cpp @@ -0,0 +1,65 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#include +#include "MusicComponent.hpp" + +namespace BBM +{ + float MusicComponent::volume = 0.75; + + 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 + { + return new MusicComponent(entity, this->_musicPath); + } + + void MusicComponent::playMusic(void) + { + if (!this->_music.isPlaying()) { + this->_music.play(); + } + } + + void MusicComponent::stopMusic(void) + { + if (this->_music.isPlaying()) + this->_music.stop(); + } + + void MusicComponent::pauseMusic(void) + { + this->_music.pause(); + } + + void MusicComponent::setVolume(float &volumeUpdate) + { + if (volumeUpdate >= 0) { + this->volume = volumeUpdate; + this->_music.setVolume(this->volume); + } + } + + void MusicComponent::setPitch(float &pitch) + { + this->_music.setPitch(pitch); + } + + bool MusicComponent::isPlaying(void) + { + 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 new file mode 100644 index 00000000..7898fd6e --- /dev/null +++ b/sources/Component/Music/MusicComponent.hpp @@ -0,0 +1,55 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#pragma once + +#include "Component/Component.hpp" +#include +#include "Audio/Music.hpp" + +namespace BBM +{ + //! @brief A basic Music component + class MusicComponent : public WAL::Component + { + public: + //! @brief start music + void playMusic(); + + //! @brief stop music + void stopMusic(); + + //! @brief put music on hold + void pauseMusic(); + + //! @brief set music volume + void setVolume(float &); + + //! @brief set pitch volume + void setPitch(float &); + + //! @brief is music playing + bool isPlaying(void); + //! @brief update music stream + void updateMusicStream(void); + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + //! @brief Create a new MusicComponent at a certain Music + explicit MusicComponent(WAL::Entity &entity, const std::string &musicPath); + //! @brief A Music component is copy constructable + MusicComponent(const MusicComponent &) = default; + //! @brief A default destructor + ~MusicComponent() override = default; + //! @brief A Music component is not assignable + MusicComponent &operator=(const MusicComponent &) = delete; + //! @brief Volume of the muisc + static float volume; + private: + //! @brief music of this entity + RAY::Audio::Music _music; + //! @brief patht to the music assets + const std::string _musicPath; + }; + +} // namespace BBM \ No newline at end of file diff --git a/sources/Component/Sound/SoundComponent.cpp b/sources/Component/Sound/SoundComponent.cpp new file mode 100644 index 00000000..752efe9f --- /dev/null +++ b/sources/Component/Sound/SoundComponent.cpp @@ -0,0 +1,91 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#include +#include +#include "SoundComponent.hpp" + +namespace BBM +{ + float SoundComponent::volume = 0.75; + + SoundComponent::SoundComponent(WAL::Entity &entity, + const std::map &soundPath) + : WAL::Component(entity), + _soundIndex(IDLE), + _soundPath(soundPath) + { + for (int i = 0; i <= DEATH; i++) { + this->_isSoundLoad[static_cast(i)] = false; + } + for (auto &soundPath : soundPath) + { + this->_isSoundLoad[soundPath.first] = true; + this->_soundList[soundPath.first] = std::make_unique(soundPath.second); + } + } + + WAL::Component *SoundComponent::clone(WAL::Entity &entity) const + { + return new SoundComponent(entity, this->_soundPath); + } + + void SoundComponent::playSound() + { + if (!this->_isSoundLoad.at(this->_soundIndex)) + return; + if (!this->_soundList[this->_soundIndex].get()->isPlaying()) + this->_soundList[this->_soundIndex].get()->play(); + } + + void SoundComponent::stopSound() + { + if (!this->_isSoundLoad.at(this->_soundIndex)) + return; + if (this->_soundList[this->_soundIndex].get()->isPlaying()) + this->_soundList[this->_soundIndex].get()->stop(); + } + + void SoundComponent::pauseSound() + { + if (!this->_isSoundLoad.at(this->_soundIndex)) + return; + this->_soundList[this->_soundIndex].get()->pause(); + } + + void SoundComponent::setVolume(float &volumeUpdate) + { + if (!this->_isSoundLoad.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->_isSoundLoad.at(this->_soundIndex)) + return; + this->_soundList[this->_soundIndex].get()->setPitch(pitch); + } + + bool SoundComponent::isPlaying() + { + if (!this->_isSoundLoad.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); + } + +} // namespace WAL diff --git a/sources/Component/Sound/SoundComponent.hpp b/sources/Component/Sound/SoundComponent.hpp new file mode 100644 index 00000000..f5278114 --- /dev/null +++ b/sources/Component/Sound/SoundComponent.hpp @@ -0,0 +1,78 @@ +// +// 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: + + //! @brief All sounds of the player + enum SoundIndex { + IDLE, + JUMP, + BOMB, + MOVE, + HURT, + THROW, + DEATH + }; + + //! @brief to set what sound should be played + void setIndex(SoundIndex index); + + //! @brief to know which sound is selected + SoundIndex getIndex(); + + //! @brief start sound + void playSound(); + + //! @brief stop sound + void stopSound(); + + //! @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(); + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + //! @brief Create a new SoundComponent at a certain Sound + explicit SoundComponent(WAL::Entity &entity, const std::map &); + //! @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; + //! @brief Volume of the sounds + static float volume; + + private: + //! @brief Sounds of this entity + std::map> _soundList; + //! @brief map to know if sound is loaded + std::map _isSoundLoad; + //! @brief All sounds path + const std::map _soundPath; + //! SoundIndex + SoundIndex _soundIndex; + + }; + +} // namespace BBM \ No newline at end of file diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 1bd637cf..b5ca0268 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -77,7 +77,7 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, 0.25, .75) + &MapGenerator::wallCollide, Vector3f(-(width + 1) / 2 , 0.25, 0.25), Vector3f(width + 1, 2, 0.75)) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); @@ -86,7 +86,7 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, 0.25, .75) + &MapGenerator::wallCollide, Vector3f(-(width + 1) / 2 , 0.25, 0.25), Vector3f(width + 1, 2, 0.75)) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); @@ -95,7 +95,7 @@ namespace BBM .addComponent>() .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, 0.25, .75) + &MapGenerator::wallCollide, Vector3f(0.25, 0.25, -(height + 1) / 2 ), Vector3f(0.75, 2, height + 1)) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); @@ -103,7 +103,7 @@ namespace BBM .addComponent(Vector3f(-1, 0, height / 2)) .addComponent( WAL::Callback(), - &MapGenerator::wallCollide, 0.25, .75) + &MapGenerator::wallCollide, Vector3f(0.25, 0.25, -(height + 1) / 2 ), Vector3f(0.75, 2, height + 1)) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); @@ -133,7 +133,6 @@ namespace BBM {HOLE, &createHole}, {FLOOR, &createFloor}, {BUMPER, &createBumper}, - {STAIRS, &createStairs}, {UPPERFLOOR, &createUpperFloor}, }; @@ -237,17 +236,6 @@ namespace BBM }); */ } - void MapGenerator::createStairs(Vector3f coords, std::shared_ptr scene) - { - static const std::string stairsObj = stairsPath + objExtension; - static const std::string stairsPng = stairsPath + imageExtension; - - scene->addEntity("Stairs Block") - .addComponent(coords) - //.addComponent(1) - .addComponent(stairsObj, std::make_pair(MAP_DIFFUSE, stairsPng)); - } - bool MapGenerator::isCloseToBlockType(std::map, BlockType> map, int x, int y, int z, BlockType blockType) { @@ -279,10 +267,10 @@ namespace BBM map[std::make_tuple(i, 1, 0)] = map[std::make_tuple(i, 0, 0)]; map[std::make_tuple(i, 0, 0)] = UPPERFLOOR; } - map[std::make_tuple(0, 0, height - 1)] = STAIRS; - map[std::make_tuple(0, 0, 1)] = STAIRS; - map[std::make_tuple(width, 0, height - 1)] = STAIRS; - map[std::make_tuple(width, 0, 1)] = STAIRS; + map[std::make_tuple(0, -1, height - 1)] = BUMPER; + map[std::make_tuple(0, -1, 1)] = BUMPER; + map[std::make_tuple(width, -1, height - 1)] = BUMPER; + map[std::make_tuple(width, -1, 1)] = BUMPER; map[std::make_tuple(width / 2, -1, height - 1)] = BUMPER; map[std::make_tuple(width / 2, -1, 1)] = BUMPER; } @@ -295,10 +283,10 @@ namespace BBM map[std::make_tuple(i, 0, j)] = UPPERFLOOR; } } - map[std::make_tuple(width / 2 - width / 8, 0, height / 2 + height / 4 + 1)] = STAIRS; - map[std::make_tuple(width / 2 + width / 8, 0, height / 2 - height / 4 - 1)] = STAIRS; - map[std::make_tuple(width / 2 - width / 4 - 1, 0, height / 2 - height / 8)] = STAIRS; - map[std::make_tuple(width / 2 + width / 4 + 1, 0, height / 2 + height / 8)] = STAIRS; + map[std::make_tuple(width / 2 - width / 8, -1, height / 2 + height / 4 + 1)] = BUMPER; + map[std::make_tuple(width / 2 + width / 8, -1, height / 2 - height / 4 - 1)] = BUMPER; + map[std::make_tuple(width / 2 - width / 4 - 1, -1, height / 2 - height / 8)] = BUMPER; + map[std::make_tuple(width / 2 + width / 4 + 1, -1, height / 2 + height / 8)] = BUMPER; } return map; } @@ -317,8 +305,6 @@ namespace BBM { for (int i = 0; i < width + 1; i++) for (int j = 0; j < height; j++) { - if (map[std::make_tuple(i, 0, j)] == BREAKABLE && isCloseToBlockType(map, i, 0, j, STAIRS)) - map[std::make_tuple(i, 0, j)] = NOTHING; if (map[std::make_tuple(i, 0, j)] == BREAKABLE && map[std::make_tuple(i, -1, j)] == BUMPER) map[std::make_tuple(i, 0, j)] = NOTHING; } diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 4a961cbc..f99fd550 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -37,7 +37,6 @@ namespace BBM UPPERFLOOR, FLOOR, BUMPER, - STAIRS, SPAWNER, UNBREAKABLE }; @@ -109,12 +108,6 @@ namespace BBM //! @brief Create upper floor of the map static void createUpperFloor(Vector3f coords, std::shared_ptr scene); - - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create stair of the map - static void createStairs(Vector3f coords, std::shared_ptr scene); - //! @param map Map to load with block declared inside //! @param width Width of the map //! @param height Height of the map diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index ae0215c8..c204005d 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -32,6 +32,10 @@ #include "Component/Animation/AnimationsComponent.hpp" #include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" +#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; @@ -57,7 +61,9 @@ namespace BBM .addSystem() .addSystem() .addSystem() - .addSystem(); + .addSystem() + .addSystem() + .addSystem(); } void enableRaylib(WAL::Wal &wal) @@ -72,6 +78,12 @@ namespace BBM std::shared_ptr loadGameScene() { auto scene = std::make_shared(); + std::map soundPath ={ + {SoundComponent::JUMP, "assets/sounds/jump.wav"}, + {SoundComponent::MOVE, "assets/sounds/move.ogg"}, + {SoundComponent::BOMB, "assets/sounds/bomb_drop.ogg"}, + {SoundComponent::DEATH, "assets/sounds/death.ogg"} + }; scene->addEntity("player") .addComponent() .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) @@ -83,6 +95,7 @@ namespace BBM .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() + .addComponent(soundPath) .addComponent() .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..ea909d9a --- /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..d524d300 --- /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 MusicManager 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 new file mode 100644 index 00000000..dac45304 --- /dev/null +++ b/sources/System/Sound/PlayerSoundManagerSystem.cpp @@ -0,0 +1,34 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#include "PlayerSoundManagerSystem.hpp" +#include + +namespace BBM { + + PlayerSoundManagerSystem::PlayerSoundManagerSystem(WAL::Wal &wal) + : System(wal) + {} + + void PlayerSoundManagerSystem::onFixedUpdate(WAL::ViewEntity &entity) + { + const auto &controllable = entity.get(); + auto &sound = entity.get(); + auto &health = entity.get(); + + sound.setVolume(sound.volume); + std::map soundIndex = { + {health.getHealthPoint() <= 0, SoundComponent::DEATH}, + {controllable.bomb, SoundComponent::BOMB}, + {controllable.jump, SoundComponent::JUMP}, + {controllable.move.x != 0 || controllable.move.y != 0, SoundComponent::MOVE} + }; + for (auto &a : soundIndex) { + if (a.first) { + sound.setIndex(a.second); + sound.playSound(); + } + } + } +} \ No newline at end of file diff --git a/sources/System/Sound/PlayerSoundManagerSystem.hpp b/sources/System/Sound/PlayerSoundManagerSystem.hpp new file mode 100644 index 00000000..6e49763b --- /dev/null +++ b/sources/System/Sound/PlayerSoundManagerSystem.hpp @@ -0,0 +1,31 @@ +// +// Created by Tom Augier on 05/06/2021 +// + +#pragma once + +#include "System/System.hpp" +#include "Window.hpp" +#include "Component/Sound/SoundComponent.hpp" +#include "Component/Health/HealthComponent.hpp" +#include +#include "Wal.hpp" + +namespace BBM +{ + class PlayerSoundManagerSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::ViewEntity &entity) override; + + //! @brief ctor + PlayerSoundManagerSystem(WAL::Wal &wal); + //! @brief Default copy ctor + PlayerSoundManagerSystem(const PlayerSoundManagerSystem &) = default; + //! @brief Default dtor + ~PlayerSoundManagerSystem() override = default; + //! @brief A SoundManager screen system can't be assigned. + PlayerSoundManagerSystem &operator=(const PlayerSoundManagerSystem &) = delete; + }; +}