summaryrefslogtreecommitdiffstats
path: root/src/game/client/entity/sound_emitter.cc
blob: b0dbc6d218e9ccd11048cd3d9ecb277b4658e91d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "client/pch.hh"

#include "client/entity/sound_emitter.hh"

#include "core/config/number.hh"

#include "core/math/constexpr.hh"

#include "shared/entity/transform.hh"
#include "shared/entity/velocity.hh"

#include "shared/world/dimension.hh"

#include "shared/coord.hh"

#include "client/entity/camera.hh"

#include "client/io/sound.hh"

#include "client/globals.hh"

SoundEmitter::SoundEmitter(void)
{
    alGenSources(1, &source);
    sound = nullptr;
}

SoundEmitter::~SoundEmitter(void)
{
    alSourceStop(source);
    alDeleteSources(1, &source);
}

void SoundEmitter::update(void)
{
    if(globals::dimension) {
        const auto view = globals::dimension->entities.view<SoundEmitter>();

        const auto& pivot = camera::position_chunk;
        const auto gain = glm::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<client::TransformIntr>(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<Velocity>(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;
            }
        }
    }
}