Created
September 23, 2016 01:04
-
-
Save fuzzblob/ce544a3b2afc9e1cf4b84f34844521cf to your computer and use it in GitHub Desktop.
logic to randomize between sound (or other things) with weighted priorities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool WeightedPlaylist() | |
{ | |
bool playlistResult = false; | |
int playListLength = _playlist.Clips.Count; | |
int totalWeight = 0; | |
for (int i = 0; i < _playlist.Clips.Count; i++) { | |
totalWeight += _playlist.Weights[i]; | |
} | |
int choice = 0; | |
while (true) { | |
choice = Random.Range (0, totalWeight); | |
_state._currentIndex = GetWeightedClip (choice, totalWeight); | |
playlistResult = LastPlayedCheck (playListLength); | |
if (playlistResult == true) { | |
break; | |
} | |
} | |
_state._nextSound = _playlist.Clips[_state._currentIndex]; | |
//Debug.Log (_sound + "'s current index swithced to: " + _state._currentIndex); | |
return playlistResult; | |
} | |
int GetWeightedClip (int weight, int totalWeight) | |
{ | |
int returnIndex = 0; | |
int threshhold = 0; | |
for (int i = 0; i < _playlist.Weights.Count; i++) { | |
threshhold += _playlist.Weights [i]; | |
if (weight < threshhold) { | |
returnIndex = i; | |
} | |
} | |
return returnIndex; | |
} | |
bool LastPlayedCheck(int playListLength) | |
{ | |
bool playlistResult = false; | |
if (playListLength > 1) { // if no repeat is even possible | |
if (_state._currentIndex != _state._lastPlayed || !_properties._playbackProps.DontRepeatLast) { // checking for repeats, this is where multiple previous played clips would be checked | |
playlistResult = true; | |
} else { | |
playlistResult = false; | |
} | |
} else | |
{ | |
playlistResult = true; | |
} | |
return playlistResult; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires a List() to hold sounds and a List() to hold weights