diff options
| author | untodesu <kirill@untode.su> | 2025-03-15 16:22:09 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-03-15 16:22:09 +0500 |
| commit | 3bf42c6ff3805a0d42bbc661794a95ff31bedc26 (patch) | |
| tree | 05049955847504808d6bed2bb7b155f8b03807bb /game/client/sound_emitter.cc | |
| parent | 02294547dcde0d4ad76e229106702261e9f10a51 (diff) | |
| download | voxelius-3bf42c6ff3805a0d42bbc661794a95ff31bedc26.tar.bz2 voxelius-3bf42c6ff3805a0d42bbc661794a95ff31bedc26.zip | |
Add whatever I was working on for the last month
Diffstat (limited to 'game/client/sound_emitter.cc')
| -rw-r--r-- | game/client/sound_emitter.cc | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/game/client/sound_emitter.cc b/game/client/sound_emitter.cc new file mode 100644 index 0000000..441ca17 --- /dev/null +++ b/game/client/sound_emitter.cc @@ -0,0 +1,57 @@ +#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<SoundEmitterComponent>(); + + const auto &pivot = camera::position_chunk; + const auto gain = cxpr::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<TransformComponentIntr>(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<VelocityComponent>(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; + } + } + } +} |
