RType
MusicComponent.cpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-type
4 ** File description:
5 ** R-type
6 */
7 
9 #include <iostream>
10 
11 /**
12  * @brief Construct a new Music Component:: Music Component object
13  *
14  * @param core
15  * @param socket
16  */
17 MusicComponent::MusicComponent(ClientCore *core, std::shared_ptr<ClientSocket> socket) :
18  AComponent(core)
19 {
20  _type = ComponentType::MUSIC;
21  if (!_music.openFromFile("../src/client/assets/musics/music.ogg"))
22  throw std::runtime_error("Cannot load music.ogg");
23  _music.setLoop(true);
24  _music.setVolume(_volume);
25  _socket = std::move(socket);
26  _clientCore = core;
27 }
28 
29 /**
30  * @brief Destroy the Music Component:: Music Component object
31  *
32  */
34 {
35  _music.stop();
36 }
37 
38 /**
39  * @brief Display the Music Component
40  *
41  * @param window
42  */
43 void MusicComponent::display(sf::RenderWindow &window)
44 {
45  if (_music.getStatus() != sf::Music::Playing)
46  if (_toPlay) {
47  _music.play();
48  _toPlay = false;
49  }
50 }
51 
52 /**
53  * @brief action, play the music
54  */
56 {
57  _music.play();
58 }
59 
60 /**
61  * @brief handle Event for the Music Component
62  */
63 void MusicComponent::handleEvent(const sf::Event &event, sf::RenderWindow &window)
64 {
65 }
66 
67 /**
68  * @brief set the Sound
69  * @param path
70  */
71 void MusicComponent::setSound(const std::string &path)
72 {
73  if (!_music.openFromFile(path))
74  throw std::runtime_error("Cannot load " + path);
75 }
76 
77 /**
78  * @brief isPlaying,
79  * @return the status of the music
80  */
82 {
83  std::cout << "isPlaying " << _music.getStatus() << " " << sf::Music::Playing << std::endl;
84  return _music.getStatus() == sf::Music::Playing;
85 }
86 
87 /**
88  * @brief getLoop get the music loop
89  * @return the music loop
90  */
91 bool MusicComponent::getLoop() const
92 {
93  return _music.getLoop();
94 }
95 
96 /**
97  * @brief getVolume get the music volume
98  * @return the music volume
99  */
100 float MusicComponent::getVolume() const
101 {
102  return _volume;
103 }
104 
105 /**
106  * @brief setLoop set the music loop
107  * @param loop
108  */
109 void MusicComponent::setLoop(bool loop)
110 {
111  _music.setLoop(loop);
112 }
113 
114 /**
115  * @brief setPaused set the music paused
116  * @param paused
117  */
118 void MusicComponent::setPaused(bool paused)
119 {
120 
121  if (paused) {
122  _music.play();
123  std::cout << "play" << std::endl;
124  } else {
125  do {
126  _music.pause();
127  std::cout << "pause" << std::endl;
128  } while (_music.getStatus() != sf::Music::Paused && _music.getStatus() != sf::Music::Stopped);
129  }
130 }
131 
132 /**
133  * @brief setVolume set the music volume
134  * @param volume
135  */
136 void MusicComponent::setVolume(float volume)
137 {
138  _volume = volume;
139  _music.setVolume(volume);
140 }
141 
142 /**
143  * @brief handleClick
144  */
146 {
147 }
148 
149 /**
150  * @brief stop, stop the music
151  */
153 {
154  std::cout << "stop" << std::endl;
155  _music.stop();
156 }
157 
158 /**
159  * @brief setPersistant set the music persistant
160  * @param persistant
161  */
162 void MusicComponent::setPersistant(bool persistant)
163 {
164  _persistant = persistant;
165 }
166 
167 /**
168  * @brief getPersistant get the music persistant
169  * @return the music persistant
170  */
172 {
173  return _persistant;
174 }