Skip to content

Commit f80bb01

Browse files
committed
Fixed forwards sound selection cycle mode so it's not crashy and starts from the 1st entry at the start
Bit of cleanup in SelectNextSounds
1 parent 05e1cef commit f80bb01

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

Entities/SoundSet.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ namespace RTE {
4444

4545
int SoundSet::ReadProperty(std::string propName, Reader &reader) {
4646
if (propName == "SoundSelectionCycleMode") {
47-
m_SoundSelectionCycleMode = ReadSoundSelectionCycleMode(reader);
47+
SetSoundSelectionCycleMode(ReadSoundSelectionCycleMode(reader));
4848
} else if (propName == "AddSound") {
4949
AddSoundData(ReadAndGetSoundData(reader));
5050
} else if (propName == "AddSoundSet") {
@@ -233,6 +233,10 @@ namespace RTE {
233233
}
234234
int selectedVectorSize = m_CurrentSelection.first == false ? m_SoundData.size() : m_SubSoundSets.size();
235235
int unselectedVectorSize = m_CurrentSelection.first == true ? m_SoundData.size() : m_SubSoundSets.size();
236+
if (selectedVectorSize == 0 && unselectedVectorSize > 0) {
237+
m_CurrentSelection.first = !m_CurrentSelection.first;
238+
std::swap(selectedVectorSize, unselectedVectorSize);
239+
}
236240

237241
/// <summary>
238242
/// Internal lambda function to pick a random sound that's not the previously played sound. Done to avoid scoping issues inside the switch below.
@@ -256,9 +260,8 @@ namespace RTE {
256260
auto selectSoundForwards = [&selectedVectorSize, &unselectedVectorSize, this]() {
257261
m_CurrentSelection.second++;
258262
if (m_CurrentSelection.second > selectedVectorSize - 1) {
259-
m_CurrentSelection = {!m_CurrentSelection.first, 0};
260-
std::swap(selectedVectorSize, unselectedVectorSize);
261-
if (selectedVectorSize == 0) {
263+
m_CurrentSelection.second = 0;
264+
if (unselectedVectorSize > 0) {
262265
m_CurrentSelection.first = !m_CurrentSelection.first;
263266
std::swap(selectedVectorSize, unselectedVectorSize);
264267
}
@@ -269,7 +272,7 @@ namespace RTE {
269272
case 0:
270273
return false;
271274
case 1:
272-
if (selectedVectorSize == 0) { m_CurrentSelection.first = !m_CurrentSelection.first; }
275+
break;
273276
case 2:
274277
selectSoundForwards();
275278
break;

Entities/SoundSet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ namespace RTE {
150150
/// Sets the SoundSelectionCycleMode for this SoundSet, which is used to determine what SoundSet to select next time SelectNextSounds is called.
151151
/// </summary>
152152
/// <param name="newSoundSelectionCycleMOde">The new SoundSelectionCycleMode for this SoundSet.</param>
153-
void SetSoundSelectionCycleMode(SoundSelectionCycleMode newSoundSelectionCycleMode) { m_SoundSelectionCycleMode = newSoundSelectionCycleMode; }
153+
void SetSoundSelectionCycleMode(SoundSelectionCycleMode newSoundSelectionCycleMode) { m_SoundSelectionCycleMode = newSoundSelectionCycleMode; if (m_SoundSelectionCycleMode == SoundSelectionCycleMode::FORWARDS) { m_CurrentSelection.second = -1; } }
154154

155155
/// <summary>
156156
/// Fills the passed in vector with the flattened SoundData in the SoundSet, optionally only getting currently selected SoundData.
@@ -195,7 +195,7 @@ namespace RTE {
195195
static const std::unordered_map<std::string, SoundSet::SoundSelectionCycleMode> c_SoundSelectionCycleModeMap; //!< A map of strings to SoundSelectionCycleModes to support string parsing for the SoundCycleMode enum. Populated in the implementing cpp file.
196196

197197
SoundSelectionCycleMode m_SoundSelectionCycleMode; //!< The SoundSelectionCycleMode for this SoundSet.
198-
std::pair<bool, std::size_t> m_CurrentSelection; //!< Whether the currently selection is in the SoundData (false) or SoundSet (true) vector, and its index in the appropriate vector.
198+
std::pair<bool, int> m_CurrentSelection; //!< Whether the currently selection is in the SoundData (false) or SoundSet (true) vector, and its index in the appropriate vector.
199199

200200
std::vector<SoundData> m_SoundData; //!< The SoundData available for selection in this SoundSet.
201201
std::vector<SoundSet> m_SubSoundSets; //!< The sub SoundSets available for selection in this SoundSet.

0 commit comments

Comments
 (0)