From 68d20787cf8540ebc22a45e6e82afbf8ad4dea15 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Mon, 26 Jun 2023 19:25:37 +1000 Subject: [PATCH] The camera is now also an audio listener --- source/client/StarClientApplication.cpp | 2 ++ source/frontend/StarMainMixer.cpp | 26 ++++++++++++++++++++++--- source/frontend/StarMainMixer.hpp | 3 +++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/source/client/StarClientApplication.cpp b/source/client/StarClientApplication.cpp index b53a9c1..542e41b 100644 --- a/source/client/StarClientApplication.cpp +++ b/source/client/StarClientApplication.cpp @@ -408,6 +408,7 @@ void ClientApplication::changeState(MainAppState newState) { m_universeClient.reset(); m_mainMixer->setUniverseClient({}); + m_mainMixer->setWorldPainter({}); m_titleScreen.reset(); } @@ -535,6 +536,7 @@ void ClientApplication::changeState(MainAppState newState) { m_worldPainter = make_shared(); m_mainInterface = make_shared(m_universeClient, m_worldPainter, m_cinematicOverlay); + m_mainMixer->setWorldPainter(m_worldPainter); if (auto renderer = Application::renderer()) { m_worldPainter->renderInit(renderer); diff --git a/source/frontend/StarMainMixer.cpp b/source/frontend/StarMainMixer.cpp index 46c26cd..c49a0fc 100644 --- a/source/frontend/StarMainMixer.cpp +++ b/source/frontend/StarMainMixer.cpp @@ -6,6 +6,7 @@ #include "StarPlayer.hpp" #include "StarAssets.hpp" #include "StarWorldClient.hpp" +#include "StarWorldPainter.hpp" namespace Star { @@ -17,6 +18,10 @@ void MainMixer::setUniverseClient(UniverseClientPtr universeClient) { m_universeClient = move(universeClient); } +void MainMixer::setWorldPainter(WorldPainterPtr worldPainter) { + m_worldPainter = move(worldPainter); +} + void MainMixer::update(bool muteSfx, bool muteMusic) { auto assets = Root::singleton().assets(); @@ -30,7 +35,7 @@ void MainMixer::update(bool muteSfx, bool muteMusic) { m_mixer->setGroupVolume(group, m_groupVolumes[group], 1.0f); } } else if (!m_mutedGroups.contains(group)) { - float volumeSetting = Root::singleton().configuration()->get(settingName).toFloat() / 100; + float volumeSetting = Root::singleton().configuration()->get(settingName).toFloat() / 100.0f; if (!m_groupVolumes.contains(group) || volumeSetting != m_groupVolumes[group]) { m_mixer->setGroupVolume(group, volumeSetting); m_groupVolumes[group] = volumeSetting; @@ -72,11 +77,26 @@ void MainMixer::update(bool muteSfx, bool muteMusic) { Vec2F stereoAdjustmentRange = jsonToVec2F(assets->json("/sfx.config:stereoAdjustmentRange")); float attenuationGamma = assets->json("/sfx.config:attenuationGamma").toFloat(); auto playerPos = m_universeClient->mainPlayer()->position(); + auto cameraPos = m_worldPainter->camera().centerWorldPosition(); auto worldGeometry = currentWorld->geometry(); m_mixer->update([&](unsigned channel, Vec2F pos, float rangeMultiplier) { - Vec2F diff = worldGeometry.diff(pos, playerPos); - float diffMagnitude = diff.magnitude(); + Vec2F playerDiff = worldGeometry.diff(pos, playerPos); + Vec2F cameraDiff = worldGeometry.diff(pos, cameraPos); + float playerMagSq = playerDiff.magnitudeSquared(); + float cameraMagSq = cameraDiff.magnitudeSquared(); + + Vec2F diff; + float diffMagnitude; + if (playerMagSq < cameraMagSq) { + diff = playerDiff; + diffMagnitude = sqrt(playerMagSq); + } + else { + diff = cameraDiff; + diffMagnitude = sqrt(cameraMagSq); + } + if (diffMagnitude == 0.0f) return 0.0f; diff --git a/source/frontend/StarMainMixer.hpp b/source/frontend/StarMainMixer.hpp index e0f1223..ecf9443 100644 --- a/source/frontend/StarMainMixer.hpp +++ b/source/frontend/StarMainMixer.hpp @@ -7,6 +7,7 @@ namespace Star { STAR_CLASS(UniverseClient); +STAR_CLASS(WorldPainter); STAR_CLASS(MainMixer); class MainMixer { @@ -14,6 +15,7 @@ public: MainMixer(unsigned sampleRate, unsigned channels); void setUniverseClient(UniverseClientPtr universeClient); + void setWorldPainter(WorldPainterPtr worldPainter); void update(bool muteSfx = false, bool muteMusic = false); @@ -24,6 +26,7 @@ public: private: UniverseClientPtr m_universeClient; + WorldPainterPtr m_worldPainter; MixerPtr m_mixer; Set m_mutedGroups; Map m_groupVolumes;