mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(audio): add day/night-based BGM switching
This commit is contained in:
BIN
assets/sound/bgm/bgm002.ogg
Normal file
BIN
assets/sound/bgm/bgm002.ogg
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -26,6 +26,7 @@ public:
|
||||
|
||||
void init();
|
||||
void play_bgm();
|
||||
void change_bgm(const std::string& sound);
|
||||
void play_3d(const std::string& sound, const glm::vec3& pos,
|
||||
bool check = false);
|
||||
void play_2d(const std::string& sound, bool check = false);
|
||||
|
||||
@@ -6,6 +6,7 @@ public:
|
||||
AudioFade(AudioSource* source, float fade_in = 0.0f, float fade_out = 0.0f);
|
||||
~AudioFade();
|
||||
void update();
|
||||
void reset();
|
||||
|
||||
private:
|
||||
AudioSource* m_source = nullptr;
|
||||
|
||||
@@ -71,6 +71,20 @@ void AudioEngine::init() {
|
||||
|
||||
void AudioEngine::play_bgm() { m_bgm->play(); }
|
||||
|
||||
void AudioEngine::change_bgm(const std::string& sound) {
|
||||
Logger::info("change bgm {}", sound);
|
||||
m_bgm->stop();
|
||||
m_bgm->reset();
|
||||
m_bgm->set_buffer_2d(m_sounds.get_buffer(sound));
|
||||
m_bgm->set_target_volume(m_music_volume);
|
||||
m_bgm->set_volume(m_music_volume);
|
||||
auto it = m_fade_map.find("bgm");
|
||||
if (it != m_fade_map.end()) {
|
||||
it->second.reset();
|
||||
}
|
||||
m_bgm->play();
|
||||
};
|
||||
|
||||
void AudioEngine::play_3d(const std::string& sound, const glm::vec3& pos,
|
||||
bool check) {
|
||||
if (!m_pool) {
|
||||
|
||||
@@ -16,6 +16,14 @@ AudioFade::AudioFade(AudioSource* source, float fade_in, float fade_out)
|
||||
m_source->set_volume(0.0f);
|
||||
}
|
||||
AudioFade::~AudioFade() {}
|
||||
|
||||
void AudioFade::reset() {
|
||||
m_active = true;
|
||||
m_fade_in = true;
|
||||
m_start_gain = 0.0f;
|
||||
m_source->set_volume(0.0f);
|
||||
}
|
||||
|
||||
void AudioFade::update() {
|
||||
if (!m_active || !m_source) {
|
||||
return;
|
||||
|
||||
@@ -47,6 +47,8 @@ void AudioSource::set_buffer_3d(const AudioBuffer& buffer,
|
||||
alSourcef(m_source, AL_REFERENCE_DISTANCE, 4.0f);
|
||||
alSourcef(m_source, AL_ROLLOFF_FACTOR, 1.0f);
|
||||
alSourcef(m_source, AL_MAX_DISTANCE, 48.0f);
|
||||
|
||||
/*
|
||||
ALfloat l[3];
|
||||
alGetListenerfv(AL_POSITION, l);
|
||||
|
||||
@@ -55,6 +57,7 @@ void AudioSource::set_buffer_3d(const AudioBuffer& buffer,
|
||||
alGetSourcefv(m_source, AL_POSITION, p);
|
||||
|
||||
Logger::info("Source Pos = ({}, {}, {})", p[0], p[1], p[2]);
|
||||
*/
|
||||
}
|
||||
|
||||
void AudioSource::set_loop(bool on) {
|
||||
|
||||
@@ -12,6 +12,7 @@ void SoundManager::clear() { m_buffers.clear(); }
|
||||
void SoundManager::init() {
|
||||
try {
|
||||
load("bgm/bgm001.mp3");
|
||||
load("bgm/bgm002.ogg");
|
||||
load("ambient/birds.ogg");
|
||||
} catch (const std::exception& e) {
|
||||
}
|
||||
|
||||
@@ -407,6 +407,14 @@ void ClientWorld::init(std::string_view player_name,
|
||||
}
|
||||
});
|
||||
|
||||
m_timers.try_emplace("bgm change", 350.0f, [this]() {
|
||||
if (m_day_tick >= 17000 || m_day_tick < 5000) {
|
||||
m_audio.change_bgm("bgm/bgm002.ogg");
|
||||
} else {
|
||||
m_audio.change_bgm("bgm/bgm001.mp3");
|
||||
}
|
||||
});
|
||||
|
||||
LoginReq req;
|
||||
req.set_name(m_player.get_name());
|
||||
while (!client->is_connected()) {
|
||||
|
||||
Reference in New Issue
Block a user