The camera is now also an audio listener

This commit is contained in:
Kae 2023-06-26 19:25:37 +10:00
parent 4b9b02783f
commit 68d20787cf
3 changed files with 28 additions and 3 deletions

View File

@ -408,6 +408,7 @@ void ClientApplication::changeState(MainAppState newState) {
m_universeClient.reset(); m_universeClient.reset();
m_mainMixer->setUniverseClient({}); m_mainMixer->setUniverseClient({});
m_mainMixer->setWorldPainter({});
m_titleScreen.reset(); m_titleScreen.reset();
} }
@ -535,6 +536,7 @@ void ClientApplication::changeState(MainAppState newState) {
m_worldPainter = make_shared<WorldPainter>(); m_worldPainter = make_shared<WorldPainter>();
m_mainInterface = make_shared<MainInterface>(m_universeClient, m_worldPainter, m_cinematicOverlay); m_mainInterface = make_shared<MainInterface>(m_universeClient, m_worldPainter, m_cinematicOverlay);
m_mainMixer->setWorldPainter(m_worldPainter);
if (auto renderer = Application::renderer()) { if (auto renderer = Application::renderer()) {
m_worldPainter->renderInit(renderer); m_worldPainter->renderInit(renderer);

View File

@ -6,6 +6,7 @@
#include "StarPlayer.hpp" #include "StarPlayer.hpp"
#include "StarAssets.hpp" #include "StarAssets.hpp"
#include "StarWorldClient.hpp" #include "StarWorldClient.hpp"
#include "StarWorldPainter.hpp"
namespace Star { namespace Star {
@ -17,6 +18,10 @@ void MainMixer::setUniverseClient(UniverseClientPtr universeClient) {
m_universeClient = move(universeClient); m_universeClient = move(universeClient);
} }
void MainMixer::setWorldPainter(WorldPainterPtr worldPainter) {
m_worldPainter = move(worldPainter);
}
void MainMixer::update(bool muteSfx, bool muteMusic) { void MainMixer::update(bool muteSfx, bool muteMusic) {
auto assets = Root::singleton().assets(); 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); m_mixer->setGroupVolume(group, m_groupVolumes[group], 1.0f);
} }
} else if (!m_mutedGroups.contains(group)) { } 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]) { if (!m_groupVolumes.contains(group) || volumeSetting != m_groupVolumes[group]) {
m_mixer->setGroupVolume(group, volumeSetting); m_mixer->setGroupVolume(group, volumeSetting);
m_groupVolumes[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")); Vec2F stereoAdjustmentRange = jsonToVec2F(assets->json("/sfx.config:stereoAdjustmentRange"));
float attenuationGamma = assets->json("/sfx.config:attenuationGamma").toFloat(); float attenuationGamma = assets->json("/sfx.config:attenuationGamma").toFloat();
auto playerPos = m_universeClient->mainPlayer()->position(); auto playerPos = m_universeClient->mainPlayer()->position();
auto cameraPos = m_worldPainter->camera().centerWorldPosition();
auto worldGeometry = currentWorld->geometry(); auto worldGeometry = currentWorld->geometry();
m_mixer->update([&](unsigned channel, Vec2F pos, float rangeMultiplier) { m_mixer->update([&](unsigned channel, Vec2F pos, float rangeMultiplier) {
Vec2F diff = worldGeometry.diff(pos, playerPos); Vec2F playerDiff = worldGeometry.diff(pos, playerPos);
float diffMagnitude = diff.magnitude(); 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) if (diffMagnitude == 0.0f)
return 0.0f; return 0.0f;

View File

@ -7,6 +7,7 @@
namespace Star { namespace Star {
STAR_CLASS(UniverseClient); STAR_CLASS(UniverseClient);
STAR_CLASS(WorldPainter);
STAR_CLASS(MainMixer); STAR_CLASS(MainMixer);
class MainMixer { class MainMixer {
@ -14,6 +15,7 @@ public:
MainMixer(unsigned sampleRate, unsigned channels); MainMixer(unsigned sampleRate, unsigned channels);
void setUniverseClient(UniverseClientPtr universeClient); void setUniverseClient(UniverseClientPtr universeClient);
void setWorldPainter(WorldPainterPtr worldPainter);
void update(bool muteSfx = false, bool muteMusic = false); void update(bool muteSfx = false, bool muteMusic = false);
@ -24,6 +26,7 @@ public:
private: private:
UniverseClientPtr m_universeClient; UniverseClientPtr m_universeClient;
WorldPainterPtr m_worldPainter;
MixerPtr m_mixer; MixerPtr m_mixer;
Set<MixerGroup> m_mutedGroups; Set<MixerGroup> m_mutedGroups;
Map<MixerGroup, float> m_groupVolumes; Map<MixerGroup, float> m_groupVolumes;