From 6cd00aacfa22fed6a54a9b812f6b069ad16feec0 Mon Sep 17 00:00:00 2001 From: untodesu Date: Sun, 29 Jun 2025 22:24:42 +0500 Subject: Move game sources into src subdirectory --- src/game/client/sound_emitter.cc | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/game/client/sound_emitter.cc (limited to 'src/game/client/sound_emitter.cc') diff --git a/src/game/client/sound_emitter.cc b/src/game/client/sound_emitter.cc new file mode 100644 index 0000000..9b84df2 --- /dev/null +++ b/src/game/client/sound_emitter.cc @@ -0,0 +1,58 @@ +#include "client/pch.hh" + +#include "client/sound_emitter.hh" + +#include "core/config.hh" +#include "core/constexpr.hh" + +#include "shared/coord.hh" +#include "shared/dimension.hh" +#include "shared/transform.hh" +#include "shared/velocity.hh" + +#include "client/camera.hh" +#include "client/globals.hh" +#include "client/sound.hh" + +SoundEmitterComponent::SoundEmitterComponent(void) +{ + alGenSources(1, &source); + sound = nullptr; +} + +SoundEmitterComponent::~SoundEmitterComponent(void) +{ + alSourceStop(source); + alDeleteSources(1, &source); +} + +void SoundEmitterComponent::update(void) +{ + if(globals::dimension) { + const auto view = globals::dimension->entities.view(); + + const auto& pivot = camera::position_chunk; + const auto gain = vx::clamp(sound::volume_effects.get_value() * 0.01f, 0.0f, 1.0f); + + for(const auto [entity, emitter] : view.each()) { + alSourcef(emitter.source, AL_GAIN, gain); + + if(const auto transform = globals::dimension->entities.try_get(entity)) { + auto position = coord::to_relative(pivot, transform->chunk, transform->local); + alSource3f(emitter.source, AL_POSITION, position.x, position.y, position.z); + } + + if(const auto velocity = globals::dimension->entities.try_get(entity)) { + alSource3f(emitter.source, AL_VELOCITY, velocity->value.x, velocity->value.y, velocity->value.z); + } + + ALint source_state; + alGetSourcei(emitter.source, AL_SOURCE_STATE, &source_state); + + if(source_state == AL_STOPPED) { + alSourceRewind(emitter.source); + emitter.sound = nullptr; + } + } + } +} -- cgit