add basic music manager

This commit is contained in:
Askou
2021-06-07 11:22:45 +02:00
parent 9dbb1d6d6d
commit e04c8d0bd4
9 changed files with 161 additions and 33 deletions
+4
View File
@@ -67,6 +67,10 @@ set(SOURCES
sources/Component/Collision/CollisionComponent.hpp sources/Component/Collision/CollisionComponent.hpp
sources/System/Collision/CollisionSystem.hpp sources/System/Collision/CollisionSystem.hpp
sources/System/Collision/CollisionSystem.cpp sources/System/Collision/CollisionSystem.cpp
sources/Component/Music/MusicComponent.cpp
sources/Component/Music/MusicComponent.hpp
sources/System/Music/PlayerMusicManagerSystem.cpp
sources/System/Music/PlayerMusicManagerSystem.hpp
) )
add_executable(bomberman add_executable(bomberman
sources/main.cpp sources/main.cpp
Binary file not shown.
Binary file not shown.
Binary file not shown.
+55 -18
View File
@@ -1,56 +1,93 @@
/* //
** EPITECH PROJECT, 2021 // Created by Tom Augier on 05/06/2021
** Bomberman //
** File description:
** MusicComponent
*/
#include <iostream>
#include "MusicComponent.hpp" #include "MusicComponent.hpp"
namespace BBM namespace BBM
{ {
MusicComponent::MusicComponent(WAL::Entity &entity, std::string &path) MusicComponent::MusicComponent(WAL::Entity &entity, \
std::map<MusicComponent::musicIndex, std::string> &musicPath)
: WAL::Component(entity), : WAL::Component(entity),
_musicPath(path) _musicIndex(IDLE)
{} {
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()
{}
WAL::Component *MusicComponent::clone(WAL::Entity &entity) const WAL::Component *MusicComponent::clone(WAL::Entity &entity) const
{ {
return new MusicComponent(entity); return new MusicComponent(entity);
} }
void MusicComponent::loadMusic(void) void MusicComponent::loadMusic(void)
{ {
this->_music = RAY::Audio::Music(this->_musicPath); if (!this->_isLoad.at(this->_musicIndex))
this->_music.play(); return;
if (!this->_musicList[this->_musicIndex].isPlaying()) {
std::cout << this->_musicIndex << std::endl;
this->_musicList[this->_musicIndex].play();
}
} }
void MusicComponent::unloadMusic(void) void MusicComponent::unloadMusic(void)
{ {
this->_music.stop(); if (!this->_isLoad.at(this->_musicIndex))
return;
if (!this->_musicList[this->_musicIndex].isPlaying())
this->_musicList[this->_musicIndex].stop();
} }
void MusicComponent::pauseMusic(void) void MusicComponent::pauseMusic(void)
{ {
this->_music.pause(); if (!this->_isLoad.at(this->_musicIndex))
return;
this->_musicList[this->_musicIndex].pause();
} }
void MusicComponent::setVolume(float &volume) void MusicComponent::setVolume(float &volume)
{ {
this->_music.setVolume(volume); if (!this->_isLoad.at(this->_musicIndex))
return;
if (volume >= 0)
this->_musicList[this->_musicIndex].setVolume(volume);
} }
void MusicComponent::setPitch(float &pitch) void MusicComponent::setPitch(float &pitch)
{ {
this->_music.setPitch(pitch); if (!this->_isLoad.at(this->_musicIndex))
return;
this->_musicList[this->_musicIndex].setPitch(pitch);
} }
bool MusicComponent::isPlaying(void) bool MusicComponent::isPlaying(void)
{ {
return (this->_music.isPlaying()); 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);
}
} // namespace WAL } // namespace WAL
+32 -13
View File
@@ -1,28 +1,34 @@
// //
// Created by Zoe Roux on 5/17/21. // Created by Tom Augier on 05/06/2021
// //
#pragma once #pragma once
#include "Models/Vector3.hpp"
#include "Component/Component.hpp" #include "Component/Component.hpp"
#include "Music.hpp" #include <map>
#include "Audio/Music.hpp"
namespace BBM namespace BBM
{ {
//! @brief A basic Music component //! @brief A basic Music component
class MusicComponent : public WAL::Component class MusicComponent : public WAL::Component
{ {
private:
//! @brief music of this entity
RAY::Audio::Music _music;
//! @brief path to the music
std::string _musicPath;
//! @brief Create a new MusicComponent linked to a specific entity
explicit MusicComponent(WAL::Entity &entity);
public: public:
enum musicIndex {
IDLE,
JUMP,
BOMB,
MOVE,
HURT,
THROW,
DEATH,
};
void setIndex(musicIndex index);
musicIndex getIndex();
//! @brief load music //! @brief load music
void loadMusic(); void loadMusic();
@@ -41,15 +47,28 @@ namespace BBM
//! @brief is music playing //! @brief is music playing
bool isPlaying(void); bool isPlaying(void);
//! @inherit //! @inherit
WAL::Component *clone(WAL::Entity &entity) const override; WAL::Component *clone(WAL::Entity &entity) const override;
//! @brief Create a new MusicComponent at a certain Music //! @brief Create a new MusicComponent at a certain Music
MusicComponent(WAL::Entity &entity, std::string &musicPath); MusicComponent(WAL::Entity &entity, std::map<musicIndex, std::string> &musicPath);
//! @brief A Music component is copy constructable //! @brief A Music component is copy constructable
MusicComponent(const MusicComponent &) = default; MusicComponent(const MusicComponent &) = default;
//! @brief A default destructor //! @brief A default destructor
~MusicComponent() override = default; ~MusicComponent() override = default;
//! @brief A Music component is not assignable //! @brief A Music component is not assignable
MusicComponent &operator=(const MusicComponent &) = delete; 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;
//! @brief Create a new MusicComponent linked to a specific entity
explicit MusicComponent(WAL::Entity &entity);
}; };
} // namespace WAL
} // namespace BBM
+12 -1
View File
@@ -29,6 +29,7 @@
#include "Component/Animation/AnimationsComponent.hpp" #include "Component/Animation/AnimationsComponent.hpp"
#include "System/Animation/AnimationsSystem.hpp" #include "System/Animation/AnimationsSystem.hpp"
#include "Map/Map.hpp" #include "Map/Map.hpp"
#include "Component/Music/MusicComponent.hpp"
namespace RAY2D = RAY::Drawables::Drawables2D; namespace RAY2D = RAY::Drawables::Drawables2D;
namespace RAY3D = RAY::Drawables::Drawables3D; namespace RAY3D = RAY::Drawables::Drawables3D;
@@ -64,6 +65,15 @@ namespace BBM
std::shared_ptr<WAL::Scene> loadGameScene() std::shared_ptr<WAL::Scene> loadGameScene()
{ {
auto scene = std::make_shared<WAL::Scene>(); 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, ""}
};
scene->addEntity("player") scene->addEntity("player")
.addComponent<PositionComponent>() .addComponent<PositionComponent>()
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent<Drawable3DComponent, RAY3D::Model>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
@@ -71,7 +81,8 @@ namespace BBM
.addComponent<KeyboardComponent>() .addComponent<KeyboardComponent>()
.addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 1) .addComponent<AnimationsComponent>(RAY::ModelAnimations("assets/player/player.iqm"), 1)
.addComponent<CollisionComponent>(2) .addComponent<CollisionComponent>(2)
.addComponent<MovableComponent>(); .addComponent<MovableComponent>()
.addComponent<MusicComponent>(musicPath);
scene->addEntity("cube") scene->addEntity("cube")
.addComponent<PositionComponent>(-5, 0, -5) .addComponent<PositionComponent>(-5, 0, -5)
.addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) .addComponent<Drawable3DComponent, RAY3D::Cube>(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED)
@@ -0,0 +1,26 @@
//
// Created by Tom Augier on 05/06/2021
//
#include "PlayerMusicManagerSystem.hpp"
namespace BBM {
void MusicManagerSystem::onFixedUpdate(WAL::Entity &entity)
{
if (!entity.hasComponent<ControllableComponent>())
return;
const auto &controllable = entity.getComponent<ControllableComponent>();
auto &music = entity.getComponent<MusicComponent>();
auto &health = entity.getComponent<HealthComponent>();
music.setIndex(MusicComponent::BOMB);
controllable.bomb ? music.loadMusic() : music.unloadMusic();
music.setIndex(MusicComponent::JUMP);
controllable.jump ? music.loadMusic() : music.unloadMusic();
music.setIndex(MusicComponent::MOVE);
(controllable.move.x != 0 || controllable.move.y != 0) ? music.loadMusic() : music.unloadMusic();
music.setIndex(MusicComponent::DEATH);
health.getHealthPoint() == 0 ? music.loadMusic() : music.unloadMusic();
}
}
@@ -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 MusicManagerSystem : public WAL::System
{
public:
//! @inherit
void onFixedUpdate(WAL::Entity &entity) override;
//! @brief ctor
MusicManagerSystem(WAL::Wal &wal, RAY::Window &window);
//! @brief Default copy ctor
MusicManagerSystem(const MusicManagerSystem &) = default;
//! @brief Default dtor
~MusicManagerSystem() override = default;
//! @brief A MusicManager screen system can't be assigned.
MusicManagerSystem &operator=(const MusicManagerSystem &) = delete;
};
}