Skip to content

Commit

Permalink
time limit the repeat duration of MenuSoundDirector
Browse files Browse the repository at this point in the history
  • Loading branch information
fallahn committed Nov 11, 2024
1 parent 20ea5f5 commit 977d1ef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
14 changes: 13 additions & 1 deletion samples/golf/src/golf/MenuSoundDirector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ source distribution.

#include <crogine/util/Random.hpp>

namespace
{
const cro::Time MinAudioTime = cro::seconds(0.5f);
}

MenuSoundDirector::MenuSoundDirector(cro::AudioResource& ar, const std::size_t& currentMenu)
: m_currentMenu(currentMenu)
{
Expand Down Expand Up @@ -172,7 +177,14 @@ cro::Entity MenuSoundDirector::playSound(std::int32_t id, float vol)
ent.getComponent<cro::AudioEmitter>().setMixerChannel(MixerChannel::Menu);
ent.getComponent<cro::AudioEmitter>().setSource(*m_audioSources[id]);
ent.getComponent<cro::AudioEmitter>().setVolume(vol);
ent.getComponent<cro::AudioEmitter>().play();

//only play if the min time since the last occurance of
//the sound has been met. Ent is automatically recycled otherwise
if (m_audioTimers[id].elapsed() > MinAudioTime)
{
ent.getComponent<cro::AudioEmitter>().play();
m_audioTimers[id].restart();
}

return ent;
}
3 changes: 3 additions & 0 deletions samples/golf/src/golf/MenuSoundDirector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ source distribution.

#include "SoundEffectsDirector.hpp"

#include <crogine/core/Clock.hpp>

namespace cro
{
class AudioResource;
Expand Down Expand Up @@ -62,6 +64,7 @@ class MenuSoundDirector final : public SoundEffectsDirector
};
};
std::array<const cro::AudioSource*, AudioID::Count> m_audioSources = {};
std::array<cro::Clock, AudioID::Count> m_audioTimers = {};

cro::Entity playSound(std::int32_t, float = 1.f);
};
14 changes: 8 additions & 6 deletions samples/golf/src/golf/MenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1794,18 +1794,20 @@ void MenuState::createScene()

//load random / seasonal props
std::string propFilePath;
std::int32_t timeOfDay = TimeOfDay::Day;

const bool spooky = cro::SysTime::now().months() == 10
&& cro::SysTime::now().days() > 22;
if (spooky)
{
propFilePath = "spooky.bgd";
timeOfDay = TimeOfDay::Night;
m_sharedData.menuSky = Skies[TimeOfDay::Night];
}
else
{
auto td = m_tod.getTimeOfDay();
switch (td)
timeOfDay = m_tod.getTimeOfDay();
switch (timeOfDay)
{
default:
case TimeOfDay::Night:
Expand All @@ -1821,7 +1823,7 @@ void MenuState::createScene()
propFilePath = "03.bgd";
break;
}
m_sharedData.menuSky = Skies[td];
m_sharedData.menuSky = Skies[timeOfDay];
}


Expand Down Expand Up @@ -1965,7 +1967,7 @@ void MenuState::createScene()
std::string phoneBoxPath = "assets/golf/models/phone_box.cmt";
std::string cartPath = "assets/golf/models/menu/cart.cmt";

if (m_sharedData.menuSky.stars > 0.5f)
if (timeOfDay == TimeOfDay::Night)
{
texID = MaterialID::CelTexturedMasked;
pavilionPath = "assets/golf/models/menu_pavilion_night.cmt";
Expand Down Expand Up @@ -2183,7 +2185,7 @@ void MenuState::createScene()


cro::ModelDefinition lightsDef(m_resources);
if (m_sharedData.menuSky.stars > 0.5f)
if (timeOfDay == TimeOfDay::Night)
{
lightsDef.loadFromFile("assets/golf/models/menu/headlights.cmt");
}
Expand Down Expand Up @@ -2374,7 +2376,7 @@ void MenuState::createScene()
camEnt.getComponent<cro::Transform>().rotate(cro::Transform::X_AXIS, -8.f * cro::Util::Const::degToRad);

//add the ambience to the cam cos why not
camEnt.addComponent<cro::AudioEmitter>() = m_menuSounds.getEmitter(m_sharedData.menuSky.stars < 0.5f ? "01" : "02");
camEnt.addComponent<cro::AudioEmitter>() = m_menuSounds.getEmitter(timeOfDay == TimeOfDay::Night ? "02" : "01");
camEnt.getComponent<cro::AudioEmitter>().play();

//set up cam / models for ball preview
Expand Down
13 changes: 11 additions & 2 deletions samples/golf/src/golf/TimeOfDay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ source distribution.

#ifdef _WIN32
#include <winnls.h>
#else
#include <locale>
#endif

namespace
Expand Down Expand Up @@ -255,9 +257,16 @@ std::string TimeOfDay::getCountryCode()
retVal = temp.toAnsiString();
}
#else
//TODO figure out linux.
//mac... well probably toughS
retVal = "US";

//POSIX systems ought to return XPG format locales wo we'll take
//a wild swing at using that - eg en_US.UTF-8
auto lc = std::locale("").name();
if (lc.size() > 4
&& lc[2] == '_')
{
retVal = lc.sub_str(3, 2);
}
#endif
}
return retVal;
Expand Down

0 comments on commit 977d1ef

Please sign in to comment.