Skip to content

Commit b145731

Browse files
committed
add shuffle mode
1 parent 2689b81 commit b145731

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

Source/Entities/DynamicSong.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ DynamicSongSection::~DynamicSongSection() {
2525
void DynamicSongSection::Clear() {
2626
m_TransitionSoundContainers.clear();
2727
m_LastTransitionSoundContainerIndex = -1;
28-
m_TransitionShuffleIndices.clear();
28+
m_TransitionShuffleUnplayedIndices.clear();
2929

3030
m_SoundContainers.clear();
3131
m_LastSoundContainerIndex = -1;
32-
m_ShuffleIndices.clear();
32+
m_ShuffleUnplayedIndices.clear();
3333

3434
m_SoundContainerSelectionCycleMode = RANDOMNOREPEAT;
3535
m_SectionType = "Default";
@@ -131,10 +131,12 @@ SoundContainer& DynamicSongSection::SelectTransitionSoundContainer() {
131131
if (m_TransitionSoundContainers.size() == 1) {
132132
return m_TransitionSoundContainers[0];
133133
}
134-
135-
if (m_TransitionShuffleIndices.empty()) {
136-
for(unsigned int i = 0; i <= m_TransitionSoundContainers.size() - 1; i++ ) {
137-
m_TransitionShuffleIndices.push_back(i);
134+
135+
if (m_TransitionShuffleUnplayedIndices.empty()) {
136+
for(unsigned int i = 0; i < m_TransitionSoundContainers.size(); i++ ) {
137+
if (i != m_LastTransitionSoundContainerIndex) {
138+
m_TransitionShuffleUnplayedIndices.push_back(i);
139+
}
138140
}
139141
}
140142

@@ -152,8 +154,10 @@ SoundContainer& DynamicSongSection::SelectTransitionSoundContainer() {
152154
return m_TransitionSoundContainers[randomIndex];
153155
}
154156
case SHUFFLE: {
155-
unsigned int selectedIndex = m_TransitionShuffleIndices[RandomNum(0, static_cast<int>(m_TransitionShuffleIndices.size()) - 1)];
156-
m_TransitionShuffleIndices.erase(m_TransitionShuffleIndices.begin() + selectedIndex);
157+
unsigned int randomSelection = RandomNum(0, static_cast<int>(m_TransitionShuffleUnplayedIndices.size() - 1));
158+
unsigned int selectedIndex = m_TransitionShuffleUnplayedIndices[randomSelection];
159+
m_TransitionShuffleUnplayedIndices.erase(m_TransitionShuffleUnplayedIndices.begin() + randomSelection);
160+
m_LastTransitionSoundContainerIndex = selectedIndex;
157161
return m_TransitionSoundContainers[selectedIndex];
158162
}
159163
}
@@ -166,9 +170,11 @@ SoundContainer& DynamicSongSection::SelectSoundContainer() {
166170
return m_SoundContainers[0];
167171
}
168172

169-
if (m_ShuffleIndices.empty()) {
170-
for(unsigned int i = 0; i <= m_SoundContainers.size() - 1; i++ ) {
171-
m_ShuffleIndices.push_back(i);
173+
if (m_ShuffleUnplayedIndices.empty()) {
174+
for(unsigned int i = 0; i < m_SoundContainers.size(); i++ ) {
175+
if (i != m_LastSoundContainerIndex) {
176+
m_ShuffleUnplayedIndices.push_back(i);
177+
}
172178
}
173179
}
174180

@@ -186,8 +192,10 @@ SoundContainer& DynamicSongSection::SelectSoundContainer() {
186192
return m_SoundContainers[randomIndex];
187193
}
188194
case SHUFFLE: {
189-
unsigned int selectedIndex = m_ShuffleIndices[RandomNum(0, static_cast<int>(m_ShuffleIndices.size()) - 1)];
190-
m_ShuffleIndices.erase(m_ShuffleIndices.begin() + selectedIndex);
195+
unsigned int randomSelection = RandomNum(0, static_cast<int>(m_ShuffleUnplayedIndices.size() - 1));
196+
unsigned int selectedIndex = m_ShuffleUnplayedIndices[randomSelection];
197+
m_ShuffleUnplayedIndices.erase(m_ShuffleUnplayedIndices.begin() + randomSelection);
198+
m_LastSoundContainerIndex = selectedIndex;
191199
return m_SoundContainers[selectedIndex];
192200
}
193201
}

Source/Entities/DynamicSong.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ namespace RTE {
8787
void SetSoundContainerSelectionCycleMode(SoundContainerSelectionCycleMode newSoundContainerSelectionCycleMode) {
8888
m_SoundContainerSelectionCycleMode = newSoundContainerSelectionCycleMode;
8989
m_LastTransitionSoundContainerIndex = -1;
90-
m_TransitionShuffleIndices.clear();
91-
for(unsigned int i = 0; i <= m_TransitionSoundContainers.size() - 1; i++ ) {
92-
m_TransitionShuffleIndices.push_back(i);
90+
m_TransitionShuffleUnplayedIndices.clear();
91+
for(unsigned int i = 0; i < m_TransitionSoundContainers.size(); i++ ) {
92+
m_TransitionShuffleUnplayedIndices.push_back(i);
9393
}
9494
m_LastSoundContainerIndex = -1;
95-
m_ShuffleIndices.clear();
96-
for(unsigned int i = 0; i <= m_SoundContainers.size() - 1; i++ ) {
97-
m_ShuffleIndices.push_back(i);
95+
m_ShuffleUnplayedIndices.clear();
96+
for(unsigned int i = 0; i < m_SoundContainers.size(); i++ ) {
97+
m_ShuffleUnplayedIndices.push_back(i);
9898
}
9999
}
100100

@@ -121,10 +121,10 @@ namespace RTE {
121121

122122
std::vector<SoundContainer> m_TransitionSoundContainers; //!< The SoundContainers that will play when first switching to this DynamicSongSection.
123123
unsigned int m_LastTransitionSoundContainerIndex; //!< The last index used to select a TransitionSoundContainer.
124-
std::vector<unsigned int> m_TransitionShuffleIndices; //!< Indices left to play if in Shuffle mode.
124+
std::vector<unsigned int> m_TransitionShuffleUnplayedIndices; //!< Indices left to play if in Shuffle mode.
125125
std::vector<SoundContainer> m_SoundContainers; //!< The SoundContainers making up this DynamicSongSection.
126126
unsigned int m_LastSoundContainerIndex; //!< The last index used to select a SoundContainer.
127-
std::vector<unsigned int> m_ShuffleIndices; //!< Indices left to play if in Shuffle mode.
127+
std::vector<unsigned int> m_ShuffleUnplayedIndices; //!< Indices left to play if in Shuffle mode.
128128

129129
SoundContainerSelectionCycleMode m_SoundContainerSelectionCycleMode; //!< The selection cycle mode to use when selecting the next SoundContainer.
130130
std::string m_SectionType; //!< The name of the type of dynamic music this is.

0 commit comments

Comments
 (0)