R-Type  2
Doom but in better
Loading...
Searching...
No Matches
SoundLib.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** SoundLib.cpp
6*/
7
14#include "SoundLib.hpp"
15
21SoundLib::SoundLib(const std::uint32_t entityId)
22 : EntityNode(entityId)
23{
24
25};
26
32SoundLib::SoundLib(const std::unordered_map<std::type_index, std::vector<std::any>> &ecsEntities)
33 : EntityNode(0), _ecsEntities(ecsEntities)
34{
35};
36
43SoundLib::SoundLib(const std::uint32_t entityId, const std::unordered_map<std::type_index, std::vector<std::any>> &ecsEntities)
44 : EntityNode(entityId), _ecsEntities(ecsEntities)
45{
46};
47
53void SoundLib::updateSoundLib(const std::unordered_map<std::type_index, std::vector<std::any>> &ecsEntities)
54{
55 _ecsEntities = ecsEntities;
56}
57
64void SoundLib::setPlay(const bool play)
65{
66 _playSound = play;
67}
68
75const bool SoundLib::isEnabled() const
76{
77 return _playSound;
78};
79
89{
90 if (!_playSound) {
91 std::cout << "Play Sound is disabled" << std::endl;
92 PRETTY_WARNING << "The sound playing has been disabled, skipping sound" << std::endl;
93 return;
94 }
95 std::cout << "The shoot sound is enabled" << std::endl;
96 std::vector<std::any> musics = _ecsEntities[typeid(GUI::ECS::Components::MusicComponent)];
97 std::cout << "The music vector length is: " << std::to_string(musics.size()) << std::endl;
98 for (std::any music : musics) {
99 std::cout << "Getting the music node" << std::endl;
100 std::optional<std::shared_ptr<GUI::ECS::Components::MusicComponent>> node = Utilities::unCast<std::shared_ptr<GUI::ECS::Components::MusicComponent>, CustomExceptions::MusicNotInitialised>(music, true, "<There was no music found in the vector item>");
101 std::cout << "Fetched the music node" << std::endl;
102 if (!node.has_value()) {
103 std::cout << "The sound value node is empty" << std::endl;
104 continue;
105 }
106 std::cout << "Sound application: " << node.value()->getApplication() << ", name: " << node.value()->getMusicName() << std::endl;
107 if (
108 node.value()->getApplication() == "shooting" ||
109 node.value()->getMusicName() == "shooting" ||
110 node.value()->getApplication() == "Shooting" ||
111 node.value()->getMusicName() == "Shooting"
112 ) {
113 node.value()->setLoopMusic(false);
114 PRETTY_DEBUG << "Playing sound: Shoot" << std::endl;
115 node.value()->play();
116 return;
117 }
118 }
119}
120
130{
131 if (!_playSound) {
132 PRETTY_WARNING << "The sound playing has been disabled, skipping sound" << std::endl;
133 return;
134 }
135 std::vector<std::any> musics = _ecsEntities[typeid(GUI::ECS::Components::MusicComponent)];
136 for (std::any music : musics) {
137 std::optional<std::shared_ptr<GUI::ECS::Components::MusicComponent>> node = Utilities::unCast<std::shared_ptr<GUI::ECS::Components::MusicComponent>, CustomExceptions::MusicNotInitialised>(music, true, "<There was no music found in the vector item>");
138 if (!node.has_value()) {
139 continue;
140 }
141 if (
142 node.value()->getApplication() == "damage" ||
143 node.value()->getMusicName() == "damage" ||
144 node.value()->getApplication() == "Damage" ||
145 node.value()->getMusicName() == "Damage"
146 ) {
147 node.value()->setLoopMusic(false);
148 PRETTY_DEBUG << "Playing sound: Damage" << std::endl;
149 node.value()->play();
150 return;
151 }
152 }
153};
154
164{
165 if (!_playSound) {
166 PRETTY_WARNING << "The sound playing has been disabled, skipping sound" << std::endl;
167 return;
168 }
169 std::vector<std::any> musics = _ecsEntities[typeid(GUI::ECS::Components::MusicComponent)];
170 for (std::any music : musics) {
171 std::optional<std::shared_ptr<GUI::ECS::Components::MusicComponent>> node = Utilities::unCast<std::shared_ptr<GUI::ECS::Components::MusicComponent>, CustomExceptions::MusicNotInitialised>(music, true, "<There was no music found in the vector item>");
172 if (!node.has_value()) {
173 continue;
174 }
175 if (
176 node.value()->getApplication() == "dead" ||
177 node.value()->getMusicName() == "dead" ||
178 node.value()->getApplication() == "Dead" ||
179 node.value()->getMusicName() == "Dead"
180 ) {
181 node.value()->setLoopMusic(false);
182 PRETTY_DEBUG << "Playing sound: Dead" << std::endl;
183 node.value()->play();
184 return;
185 }
186 }
187};
188
198{
199 if (!_playSound) {
200 PRETTY_WARNING << "The sound playing has been disabled, skipping sound" << std::endl;
201 return;
202 }
203 std::vector<std::any> musics = _ecsEntities[typeid(GUI::ECS::Components::MusicComponent)];
204 for (std::any music : musics) {
205 std::optional<std::shared_ptr<GUI::ECS::Components::MusicComponent>> node = Utilities::unCast<std::shared_ptr<GUI::ECS::Components::MusicComponent>, CustomExceptions::MusicNotInitialised>(music, true, "<There was no music found in the vector item>");
206 if (!node.has_value()) {
207 continue;
208 }
209 if (
210 node.value()->getApplication() == "button" ||
211 node.value()->getMusicName() == "button" ||
212 node.value()->getApplication() == "Button" ||
213 node.value()->getMusicName() == "Button"
214 ) {
215 node.value()->setLoopMusic(false);
216 PRETTY_DEBUG << "Playing sound: Button" << std::endl;
217 node.value()->play();
218 return;
219 }
220 }
221};
222
232{
233 if (!_playSound) {
234 PRETTY_WARNING << "The sound playing has been disabled, skipping sound" << std::endl;
235 return;
236 }
237 std::vector<std::any> musics = _ecsEntities[typeid(GUI::ECS::Components::MusicComponent)];
238 for (std::any music : musics) {
239 std::optional<std::shared_ptr<GUI::ECS::Components::MusicComponent>> node = Utilities::unCast<std::shared_ptr<GUI::ECS::Components::MusicComponent>, CustomExceptions::MusicNotInitialised>(music, true, "<There was no music found in the vector item>");
240 if (!node.has_value()) {
241 continue;
242 }
243 if (
244 node.value()->getApplication() == "gameOver" ||
245 node.value()->getMusicName() == "gameOver" ||
246 node.value()->getApplication() == "Game Over" ||
247 node.value()->getMusicName() == "Game Over"
248 ) {
249 node.value()->setLoopMusic(false);
250 PRETTY_DEBUG << "Playing sound: Game Over" << std::endl;
251 node.value()->play();
252 return;
253 }
254 }
255};
256
266{
267 if (!_playSound) {
268 PRETTY_WARNING << "The sound playing has been disabled, skipping sound" << std::endl;
269 return;
270 }
271 std::vector<std::any> musics = _ecsEntities[typeid(GUI::ECS::Components::MusicComponent)];
272 for (std::any music : musics) {
273 std::optional<std::shared_ptr<GUI::ECS::Components::MusicComponent>> node = Utilities::unCast<std::shared_ptr<GUI::ECS::Components::MusicComponent>, CustomExceptions::MusicNotInitialised>(music, true, "<There was no music found in the vector item>");
274 if (!node.has_value()) {
275 continue;
276 }
277 if (
278 node.value()->getApplication() == "success" ||
279 node.value()->getMusicName() == "success" ||
280 node.value()->getApplication() == "Success" ||
281 node.value()->getMusicName() == "Success"
282 ) {
283 node.value()->setLoopMusic(false);
284 PRETTY_DEBUG << "Playing sound: win" << std::endl;
285 node.value()->play();
286 return;
287 }
288 }
289};
#define PRETTY_DEBUG
Debug log with details and colour.
#define PRETTY_WARNING
Warning log with details and colour.
Declaration of the SoundLib class for managing sounds in the ECS architecture.
This is the class in charge of informing the user that they tried to play music from a class that has...
Manages music playback and properties for an entity in the ECS system.
void updateSoundLib(const std::unordered_map< std::type_index, std::vector< std::any > > &ecsEntities)
Updates the sound library with the provided ECS entities.
Definition SoundLib.cpp:53
void buttonSound()
Plays the button interaction sound effect.
Definition SoundLib.cpp:197
void winSound()
Plays the game win sound effect.
Definition SoundLib.cpp:265
void damageSound()
Plays the damage sound effect.
Definition SoundLib.cpp:129
void setPlay(const bool play)
Enables or disables sound playback.
Definition SoundLib.cpp:64
SoundLib(const std::uint32_t entityId=0)
Default constructor.
Definition SoundLib.cpp:21
void shootSound()
Plays the shooting sound effect.
Definition SoundLib.cpp:88
void gameOverSound()
Plays the game over sound effect.
Definition SoundLib.cpp:231
const bool isEnabled() const
Checks if sound playback is enabled.
Definition SoundLib.cpp:75
void deadSound()
Plays the dead sound effect.
Definition SoundLib.cpp:163
std::optional< T > unCast(const std::any &classNode, const bool raiseOnError=true, const std::string customErrorMessage="")
Casts the content of a std::any back to its original type.
Definition UnCast.hpp:65