R-Type  2
Doom but in better
Loading...
Searching...
No Matches
MusicComponent.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** rtype (Workspace)
4** File description:
5** MusicComponent.cpp
6*/
7
15
20
22 : EntityNode(entityId)
23{
24};
25
31
33 : EntityNode(entityId)
34{
35 update(music);
36};
37
38GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name)
39 : EntityNode(0)
40{
41 setMusicName(name);
42 setMusic(filePath);
43 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
44};
45
46GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name, bool loop)
47 : EntityNode(0)
48{
49 setLoopMusic(loop);
50 setMusicName(name);
51 setMusic(filePath);
52 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
53};
54
55GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name, float volume)
56 : EntityNode(0)
57{
58 setVolume(volume);
59 setMusicName(name);
60 setMusic(filePath);
61 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
62};
63
64GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name, float volume, bool loop)
65 : EntityNode(0)
66{
67 setVolume(volume);
68 setLoopMusic(loop);
69 setMusicName(name);
70 setMusic(filePath);
71 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
72};
73
74GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name, const std::string &application)
75 : EntityNode(0)
76{
77 setMusicName(name);
78 setMusic(filePath);
79 setApplication(application);
80 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
81};
82
83GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name, const std::string &application, bool loop)
84 : EntityNode(0)
85{
86 setLoopMusic(loop);
87 setMusicName(name);
88 setMusic(filePath);
89 setApplication(application);
90 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
91};
92
93GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name, const std::string &application, float volume)
94 : EntityNode(0)
95{
96 setVolume(volume);
97 setMusicName(name);
98 setMusic(filePath);
99 setApplication(application);
100 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
101};
102
103GUI::ECS::Components::MusicComponent::MusicComponent(const std::string &filePath, const std::string &name, const std::string &application, float volume, bool loop)
104 : EntityNode(0)
105{
106 setVolume(volume);
107 setLoopMusic(loop);
108 setMusicName(name);
109 setMusic(filePath);
110 setApplication(application);
111 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
112};
113
114GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name)
115 : EntityNode(entityId)
116{
117 setMusicName(name);
118 setMusic(filePath);
119 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
120};
121
122GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name, bool loop)
123 : EntityNode(entityId)
124{
125 setLoopMusic(loop);
126 setMusicName(name);
127 setMusic(filePath);
128 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
129};
130
131GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name, float volume)
132 : EntityNode(entityId)
133{
134 setVolume(volume);
135 setMusicName(name);
136 setMusic(filePath);
137 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
138};
139
140GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name, float volume, bool loop)
141 : EntityNode(entityId)
142{
143 setVolume(volume);
144 setLoopMusic(loop);
145 setMusicName(name);
146 setMusic(filePath);
147 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
148};
149
150GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name, const std::string &application)
151 : EntityNode(entityId)
152{
153 setMusicName(name);
154 setMusic(filePath);
155 setApplication(application);
156 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
157};
158
159GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name, const std::string &application, bool loop)
160 : EntityNode(entityId)
161{
162 setLoopMusic(loop);
163 setMusicName(name);
164 setMusic(filePath);
165 setApplication(application);
166 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
167};
168
169GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name, const std::string &application, float volume)
170 : EntityNode(entityId)
171{
172 setVolume(volume);
173 setMusicName(name);
174 setMusic(filePath);
175 setApplication(application);
176 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
177};
178
179
180
181GUI::ECS::Components::MusicComponent::MusicComponent(const std::uint32_t entityId, const std::string &filePath, const std::string &name, const std::string &application, float volume, bool loop)
182 : EntityNode(entityId)
183{
184 setVolume(volume);
185 setLoopMusic(loop);
186 setMusicName(name);
187 setMusic(filePath);
188 setApplication(application);
189 PRETTY_DEBUG << "Music " << name << " is loaded and ready to be played." << std::endl;
190};
191
193
195{
196 if (_volume < 0) {
197 _volume = 0;
198 }
199 if (_volume > 100) {
200 _volume = 100;
201 }
202 _volume = volume;
203 if (_isInitialised == true) {
204 _music.setVolume(_volume);
205 }
206}
207
209{
210 _isLooping = loop;
211 if (_isInitialised == true) {
212 _music.setLooping(_isLooping);
213 }
214}
215
217{
218 _name = name;
219}
220
221void GUI::ECS::Components::MusicComponent::setMusic(const std::string &filePath)
222{
223 _isInitialised = false;
224 if (filePath.empty()) {
225 throw CustomExceptions::FileNotFound("'<no path provided>'");
226 }
227 _filePath = filePath;
228 if (!_music.openFromFile(_filePath)) {
229 throw CustomExceptions::FileNotFound(_filePath);
230 }
231 _music.setVolume(_volume);
232 _music.setLooping(_isLooping);
233 _isInitialised = true;
234}
235
236void GUI::ECS::Components::MusicComponent::setMusic(const std::string &filePath, const std::string &name)
237{
238 setMusicName(name);
239 setMusic(filePath);
240}
241
242void GUI::ECS::Components::MusicComponent::setMusic(const std::string &filePath, const std::string &name, float volume)
243{
244 setVolume(volume);
245 setMusicName(name);
246 setMusic(filePath);
247}
248
250{
251 _application = application;
252}
253
255{
256 if (_isInitialised == false) {
258 }
259 _isPlaying = true;
260 _isPaused = false;
261 _isStopped = false;
262 _music.play();
263}
264
266{
267 if (_isInitialised == false) {
269 }
270 _isPlaying = false;
271 _isPaused = false;
272 _isStopped = true;
273 _music.stop();
274}
275
277{
278 if (_isInitialised == false) {
280 }
281 _isPlaying = false;
282 _isPaused = true;
283 _isStopped = false;
284 _music.pause();
285}
286
288{
289 return _isPaused;
290}
291
293{
294 return _isPlaying;
295}
296
298{
299 return _isStopped;
300}
301
303{
304 return _isLooping;
305}
306
308{
309 return _isInitialised;
310}
311
313{
314 setVolume(copy.getVolume());
315 setLoopMusic(copy.isLooping());
316 setMusicName(copy.getMusicName());
317 setMusic(copy.getFilePath());
318 setApplication(copy.getApplication());
319 if (copy.isPaused()) {
320 pause();
321 } else if (copy.isPlaying()) {
322 play();
323 } else if (copy.isStopped()) {
324 stop();
325 };
326}
327
329{
330 return _volume;
331}
332
334{
335 return _name;
336}
337
339{
340 return _filePath;
341}
342
344{
345 return _application;
346}
347
348const std::string GUI::ECS::Components::MusicComponent::getInfo(const unsigned int indent) const
349{
350
351 std::string indentation = "";
352 for (unsigned int i = 0; i < indent; ++i) {
353 indentation += "\t";
354 }
355 std::string result = indentation + "Music:\n";
356 result += indentation + "- Entity Id: " + Recoded::myToString(getEntityNodeId()) + "\n";
357 result += indentation + "- Name: " + _name + "\n";
358 result += indentation + "- Application: " + _application + "\n";
359 result += indentation + "- File Path: " + _filePath + "\n";
360 result += indentation + "- Volume: " + Recoded::myToString(_volume) + "\n";
361 result += indentation + "- Looping: " + Recoded::myToString(_isLooping) + "\n";
362 result += indentation + "- Playing: " + Recoded::myToString(_isPlaying) + "\n";
363 result += indentation + "- Paused: " + Recoded::myToString(_isPaused) + "\n";
364 result += indentation + "- Stopped: " + Recoded::myToString(_isStopped) + "\n";
365 result += indentation + "- Initialised: " + Recoded::myToString(_isInitialised) + "\n";
366 return result;
367}
368
370{
371 if (this != &copy) {
372 update(copy);
373 }
374 return *this;
375};
376
378{
379 os << item.getInfo();
380 return os;
381}
#define PRETTY_DEBUG
Debug log with details and colour.
This is the file that contains the class in charge of managing music.
This is the class in charge of informing the user that the provided file path could not be found.
Definition NotFound.hpp:27
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 setMusicName(const std::string &name)
Set the name of the music.
MusicComponent & operator=(const GUI::ECS::Components::MusicComponent &copy)
The '=' operator that will allow the user to seamlessly update their music component from another mus...
const bool isLooping() const
Check is the music is looping.
void setApplication(const std::string &application)
Set the Application context of the music.
void play()
Start playing the music.
const std::string getInfo(const unsigned int indent=0) const
This is a function meant for debugging purposes It will dump the current state of the variables upon ...
const float getVolume() const
Get the Volume of the music.
const std::string getFilePath() const
Get the File Path of the music stored in the component.
const bool isPlaying() const
Check if the music is playing.
void setMusic(const std::string &filePath)
Set the music file to load using a file path.
void setLoopMusic(bool loop)
Set if to play the music in a loop or only when asked to.
const bool isInitialised() const
Check if there is an instance of the music to be played in the component.
const bool isStopped() const
Check if the music is stopped.
const std::string getApplication() const
Get the Application context of the music stored in the component.
const std::string getMusicName() const
Get the name of the music stored in the component.
const bool isPaused() const
Check if the music is pause.
void setVolume(float volume)
Set the music Volume (0-100)
void update(const MusicComponent &copy)
Copy the values from an existing music component into the current instance.
std::ostream & operator<<(std::ostream &os, const AnimationComponent &item)
Outputs the animation's info to a stream.
const std::string myToString(const Rect< RectType > &rectangle)
Converts a Rect<T> object to its string representation.
Definition Rect.hpp:223