add sound/music component

This commit is contained in:
Askou
2021-06-07 11:43:06 +02:00
parent e04c8d0bd4
commit 8e93e84006
5 changed files with 190 additions and 76 deletions
+11 -45
View File
@@ -7,25 +7,15 @@
namespace BBM
{
MusicComponent::MusicComponent(WAL::Entity &entity, \
std::map<MusicComponent::musicIndex, std::string> &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<musicIndex>(i)).empty()) {
this->_isLoad[static_cast<musicIndex>(i)] = false;
} else {
this->_isLoad[static_cast<musicIndex>(i)] = true;
this->_musicList[static_cast<musicIndex>(i)] = RAY::Audio::Music(musicPath.at(static_cast<musicIndex>(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<MusicComponent::musicIndex, std::string> &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
+2 -22
View File
@@ -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<musicIndex, std::string> &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<musicIndex, RAY::Audio::Music> _musicList;
std::map<musicIndex, bool> _isLoad;
//! musicIndex
musicIndex _musicIndex;
RAY::Audio::Music _music;
//! @brief Create a new MusicComponent linked to a specific entity
explicit MusicComponent(WAL::Entity &entity);
@@ -0,0 +1,93 @@
//
// Created by Tom Augier on 05/06/2021
//
#include <iostream>
#include "SoundComponent.hpp"
namespace BBM
{
SoundComponent::SoundComponent(WAL::Entity &entity, \
std::map<SoundComponent::soundIndex, std::string> &soundPath)
: WAL::Component(entity),
_soundIndex(IDLE)
{
for (int i = 0; i < DEATH + 1; i++) {
if (soundPath.at(static_cast<soundIndex>(i)).empty()) {
this->_isLoad[static_cast<soundIndex>(i)] = false;
} else {
this->_isLoad[static_cast<soundIndex>(i)] = true;
this->_soundList[static_cast<soundIndex>(i)] = RAY::Audio::Sound(soundPath.at(static_cast<soundIndex>(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
@@ -0,0 +1,74 @@
//
// 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:
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<soundIndex, std::string> &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<soundIndex, RAY::Audio::Sound> _soundList;
std::map<soundIndex, bool> _isLoad;
//! SoundIndex
soundIndex _soundIndex;
//! @brief Create a new SoundComponent linked to a specific entity
explicit SoundComponent(WAL::Entity &entity);
};
} // namespace BBM
+10 -9
View File
@@ -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<WAL::Scene> loadGameScene()
{
auto scene = std::make_shared<WAL::Scene>();
std::map<MusicComponent::musicIndex, std::string> musicPath= {
{MusicComponent::IDLE, ""},
{MusicComponent::JUMP, ""},
{MusicComponent::BOMB, ""},
{MusicComponent::MOVE, "assets/sounds/new_death.ogg"},
{MusicComponent::HURT, ""},
{MusicComponent::THROW, ""},
{MusicComponent::DEATH, ""}
std::map<SoundComponent::soundIndex, std::string> musicPath= {
{SoundComponent::IDLE, ""},
{SoundComponent::JUMP, ""},
{SoundComponent::BOMB, ""},
{SoundComponent::MOVE, "assets/sounds/new_death.ogg"},
{SoundComponent::HURT, ""},
{SoundComponent::THROW, ""},
{SoundComponent::DEATH, ""}
};
scene->addEntity("player")
.addComponent<PositionComponent>()
@@ -82,7 +83,7 @@ namespace BBM
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 1)
.addComponent<CollisionComponent>(2)
.addComponent<MovableComponent>()
.addComponent<MusicComponent>(musicPath);
.addComponent<SoundComponent>(musicPath);
scene->addEntity("cube")
.addComponent<PositionComponent>(-5, 0, -5)
.addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED)