14 : _registry(registry), _masterVolume(100.0f), _musicVolume(100.0f), _sfxVolume(100.0f), _nextSoundId(0) {}
33 sound->setVolume(baseVolume *
_masterVolume * categoryVolume / 100.0f);
80 auto buffer = std::make_unique<sf::SoundBuffer>();
81 if (!buffer->loadFromFile(path)) {
86 sf::SoundBuffer* result = buffer.get();
95 for (
auto&& [audioSource] : view) {
96 if (audioSource.dirty) {
98 if (audioSource.isPlaying) {
101 audioSource.dirty =
false;
103 if (audioSource.loop) {
105 if (it !=
_activeSounds.end() && it->second->getStatus() == sf::Sound::Status::Stopped) {
111 }
catch (
const std::exception& e) {
112 log::error(
"Error updating AudioSources: {}", e.what());
120 for (
auto&& [soundEvent] : view) {
121 if (!soundEvent.played) {
122 log::info(
"Playing sound: {}", soundEvent.soundPath);
124 soundEvent.played =
true;
127 }
catch (
const std::exception& e) {
128 log::error(
"Error updating SoundEvents: {}", e.what());
136 auto sound = std::make_unique<sf::Sound>(*buffer);
138 sound->setPitch(audioSource.
pitch);
152 auto sound = std::make_unique<sf::Sound>(*buffer);
154 sound->setPitch(soundEvent.
pitch);
164 const uint32_t
id = it->first;
166 if (it->second->getStatus() == sf::Sound::Status::Stopped && !isLooping) {
Logger declaration with support for multiple log levels.
void setSfxVolume(float volume)
Set SFX volume (affects SoundEvent components)
void updateAudioSources(float dt)
Update all AudioSource components.
AudioSystem(ecs::Registry ®istry)
ecs::Registry & _registry
void update(float dt) override
Update audio system (called every frame)
void stopAllSounds()
Stop all currently playing sounds.
std::unordered_map< uint32_t, bool > _soundIsMusic
Track if sound is music (true) or SFX (false)
std::unordered_map< uint32_t, float > _soundBaseVolumes
Track base volume (0.0-1.0) for each sound.
void cleanupFinishedSounds()
Remove finished non-looping sounds from tracking.
float getMasterVolume() const
Get current master volume.
std::unordered_map< uint32_t, std::unique_ptr< sf::Sound > > _activeSounds
void setMusicVolume(float volume)
Set music volume (affects AudioSource components)
void updateSoundEvents(float dt)
Update all SoundEvent components.
void playSoundEffect(ecs::components::audio::SoundEvent &soundEvent)
Play a sound effect (one-time)
std::unordered_map< std::string, std::unique_ptr< sf::SoundBuffer > > _soundBuffers
std::unordered_map< uint32_t, bool > _loopingSounds
Track which sounds should loop.
void playAudioSource(ecs::components::audio::AudioSource &audioSource)
Play an AudioSource.
void setMasterVolume(float volume)
Set master volume for all audio.
sf::SoundBuffer * loadSoundBuffer(const std::string &path)
Load or get cached sound buffer.
auto zipView(this Self &self)
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
Component for entities that emit continuous sound (music, loops, ambient)
std::string audioPath
Path to the audio file.
float pitch
Pitch multiplier.
uint32_t sourceId
Internal audio source identifier.
float volume
Volume level (0.0 - 1.0)
bool loop
Whether the audio should loop.
Component for triggering one-time sound effects (SFX)
float volume
Volume level (0.0 - 1.0)
std::string soundPath
Path to the sound file.
float pitch
Pitch multiplier.