public TrackInfo PickATrack(TrackInfo excludedInfo) { float r = Random.value; float p = 0; float totalWeight = 0; foreach (TrackInfo info in tracks) { if (info != excludedInfo) totalWeight += info.weight; } if (totalWeight == 0) return PickATrack(null); foreach (TrackInfo info in tracks) { if (info == excludedInfo) continue; float probability = info.weight / totalWeight; p += probability; if (r < p) { return info; } } Log.Assert(false, "Found no track to play!?"); return null; }
public void NextStep(TrackInfo trackInfo) { float duration = trackInfo.track.Play(); TrackInfo nextTrack = PickATrack(trackInfo); duration += nextTrack.startTime; Log.Debug("AudioPlayerAdvanced", "Starting track, {0} seconds until next.", duration); StartCoroutine(MakeNextDecisionIn(duration, nextTrack)); }
public IEnumerator MakeNextDecisionIn(float delay, TrackInfo nextTrack) { yield return new WaitForSeconds(delay); NextStep(nextTrack); }