Merge pull request #127 from AnonymusRaccoon/music_component

Music/Sound component
This commit is contained in:
Zoe Roux
2021-06-09 14:13:19 +02:00
committed by GitHub
19 changed files with 441 additions and 1 deletions
+8
View File
@@ -81,6 +81,14 @@ set(SOURCES
sources/Component/Animator/AnimatorComponent.hpp
sources/System/Animator/AnimatorSystem.cpp
sources/System/Animator/AnimatorSystem.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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+6
View File
@@ -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;
}
+2
View File
@@ -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;
+2
View File
@@ -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
@@ -0,0 +1,65 @@
//
// Created by Tom Augier on 05/06/2021
//
#include <iostream>
#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
@@ -0,0 +1,55 @@
//
// Created by Tom Augier on 05/06/2021
//
#pragma once
#include "Component/Component.hpp"
#include <map>
#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
@@ -0,0 +1,91 @@
//
// Created by Tom Augier on 05/06/2021
//
#include <iostream>
#include <memory>
#include "SoundComponent.hpp"
namespace BBM
{
float SoundComponent::volume = 0.75;
SoundComponent::SoundComponent(WAL::Entity &entity,
const std::map<SoundComponent::SoundIndex, std::string> &soundPath)
: WAL::Component(entity),
_soundIndex(IDLE),
_soundPath(soundPath)
{
for (int i = 0; i <= DEATH; i++) {
this->_isSoundLoad[static_cast<SoundIndex>(i)] = false;
}
for (auto &soundPath : soundPath)
{
this->_isSoundLoad[soundPath.first] = true;
this->_soundList[soundPath.first] = std::make_unique<RAY::Audio::Sound>(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
@@ -0,0 +1,78 @@
//
// Created by Tom Augier on 05/06/2021
//
#pragma once
#include "Component/Component.hpp"
#include <map>
#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<SoundIndex, std::string> &);
//! @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<SoundIndex, std::shared_ptr<RAY::Audio::Sound>> _soundList;
//! @brief map to know if sound is loaded
std::map<SoundIndex, bool> _isSoundLoad;
//! @brief All sounds path
const std::map<SoundIndex, std::string> _soundPath;
//! SoundIndex
SoundIndex _soundIndex;
};
} // namespace BBM
+14 -1
View File
@@ -31,6 +31,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;
@@ -56,7 +60,9 @@ namespace BBM
.addSystem<EventSystem>()
.addSystem<HealthSystem>()
.addSystem<CollisionSystem>()
.addSystem<MovableSystem>();
.addSystem<MovableSystem>()
.addSystem<PlayerSoundManagerSystem>()
.addSystem<MusicSystem>();
}
void enableRaylib(WAL::Wal &wal)
@@ -71,6 +77,12 @@ namespace BBM
std::shared_ptr<WAL::Scene> loadGameScene()
{
auto scene = std::make_shared<WAL::Scene>();
std::map<SoundComponent::SoundIndex, std::string> 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<PositionComponent>()
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
@@ -81,6 +93,7 @@ namespace BBM
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 3)
.addComponent<CollisionComponent>(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75})
.addComponent<MovableComponent>()
.addComponent<SoundComponent>(soundPath)
.addComponent<BombHolderComponent>()
.addComponent<HealthComponent>(1, [](WAL::Entity &entity) {
auto &animation = entity.getComponent<AnimationsComponent>();
+24
View File
@@ -0,0 +1,24 @@
//
// Created by Tom Augier on 05/06/2021
//
#include "MusicSystem.hpp"
#include <map>
namespace BBM {
MusicSystem::MusicSystem(WAL::Wal &wal)
: System(wal)
{}
void MusicSystem::onFixedUpdate(WAL::ViewEntity<MusicComponent> &entity)
{
auto &music = entity.get<MusicComponent>();
music.setVolume(music.volume);
if (!music.isPlaying()) {
music.playMusic();
}
music.updateMusicStream();
}
}
+31
View File
@@ -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 <Component/Controllable/ControllableComponent.hpp>
#include "Wal.hpp"
namespace BBM
{
class MusicSystem : public WAL::System<MusicComponent>
{
public:
//! @inherit
void onFixedUpdate(WAL::ViewEntity<MusicComponent> &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;
};
}
@@ -0,0 +1,34 @@
//
// Created by Tom Augier on 05/06/2021
//
#include "PlayerSoundManagerSystem.hpp"
#include <map>
namespace BBM {
PlayerSoundManagerSystem::PlayerSoundManagerSystem(WAL::Wal &wal)
: System(wal)
{}
void PlayerSoundManagerSystem::onFixedUpdate(WAL::ViewEntity<SoundComponent, ControllableComponent, HealthComponent> &entity)
{
const auto &controllable = entity.get<ControllableComponent>();
auto &sound = entity.get<SoundComponent>();
auto &health = entity.get<HealthComponent>();
sound.setVolume(sound.volume);
std::map<bool, SoundComponent::SoundIndex> 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();
}
}
}
}
@@ -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 <Component/Controllable/ControllableComponent.hpp>
#include "Wal.hpp"
namespace BBM
{
class PlayerSoundManagerSystem : public WAL::System<SoundComponent, ControllableComponent, HealthComponent>
{
public:
//! @inherit
void onFixedUpdate(WAL::ViewEntity<SoundComponent, ControllableComponent, HealthComponent> &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;
};
}