HKI Core
Playlist.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections.Generic;
3 
4 namespace HKI.Core.Audio
5 {
17  [System.Serializable]
18  [CreateAssetMenu(fileName = "Playlist", menuName = "HKI/Audio/Playlist", order = 0)]
19  public class Playlist : ScriptableObject
20  {
21  // Public enums
22  [System.Serializable]
23  public enum PlaylistPlayTypes
24  {
25  RandomShuffelWithProtection,
26  RandomShuffel,
27  RandomTotal,
28  Order
29  }
30 
31  // Public variables
32  [SerializeField] PlaylistPlayTypes PlaylistPlayType = PlaylistPlayTypes.RandomShuffelWithProtection;
33 
34  [SerializeField] List<SoundClip> Clips = new List<SoundClip>();
35 
36  List<int> shuffleList;
37 
38  int index = -1;
39 
40  // Getter
41  public int GetNumberOfClips { get { return Clips.Count; } }
42 
43  // OnEnable function
44  void OnEnable()
45  {
46  index = -1;
47 
48  if(PlaylistPlayType == PlaylistPlayTypes.RandomShuffelWithProtection || PlaylistPlayType == PlaylistPlayTypes.RandomShuffel)
49  CreateShuffleList();
50  }
51 
52  // Getter
53  public PlaylistPlayTypes GetPlaylistPlayType { get { return PlaylistPlayType; } }
54 
55  // Interface functions
57  {
58  if(Clips.Count == 0)
59  return null;
60 
61  index = (index + 1) % Clips.Count;
62 
63  switch(PlaylistPlayType)
64  {
65  case PlaylistPlayTypes.RandomShuffelWithProtection:
66  case PlaylistPlayTypes.RandomShuffel:
67  if(index == 0)
68  Shuffle();
69  return Clips[shuffleList[index]];
70 
71  case PlaylistPlayTypes.RandomTotal:
72  return Clips[Random.Range(0, Clips.Count)];
73 
74  case PlaylistPlayTypes.Order:
75  return Clips[index];
76 
77  default:
78  return null;
79  }
80  }
81 
82  public SoundClip GetClipByName(string name)
83  {
84  if(string.IsNullOrEmpty(name))
85  return null;
86 
87  for(int i = 0; i < Clips.Count; i++)
88  {
89  if(Clips[i].name == name)
90  return Clips[i];
91  }
92 
93  return null;
94  }
95 
96  public SoundClip GetClipByClipId(int clipId)
97  {
98  if(string.IsNullOrEmpty(name))
99  return null;
100 
101  for(int i = 0; i < Clips.Count; i++)
102  {
103  if (Clips[i].ClipId == clipId)
104  return Clips[i];
105  }
106 
107  return null;
108  }
109 
110  public void ChangeMusicPlaylist(bool playPlaylist = true)
111  {
113  }
114 
115  public void ChangeAmbientPlaylist(bool playPlaylist = true)
116  {
118  }
119 
120  public void ChangeFxPlaylist(bool playPlaylist = false)
121  {
122  AudioSystem.Instance.FxAudioPlayer.ChangePlaylist(this, playPlaylist);
123  }
124 
125  public void ChangeUiFxPlaylist(bool playPlaylist = false)
126  {
128  }
129 
130  public void ChangeVoicesPlaylist(bool playPlaylist = false)
131  {
133  }
134 
135  // Helper functions
136  void Shuffle()
137  {
138  if(PlaylistPlayType == PlaylistPlayTypes.RandomShuffelWithProtection)
139  {
140  int count = Clips.Count;
141  int last = count - 1;
142 
143  int lastIndex = shuffleList[last];
144 
145  for(int i = 0; i < count; ++i)
146  {
147  int rand = Random.Range(i, count);
148  int tmp = shuffleList[i];
149  shuffleList[i] = shuffleList[rand];
150  shuffleList[rand] = tmp;
151  }
152 
153  if(count > 1 && shuffleList[0] == lastIndex)
154  {
155  int rand = Random.Range(1, count);
156  shuffleList[0] = shuffleList[rand];
157  shuffleList[rand] = lastIndex;
158  }
159  }
160  else
161  {
162  int count = Clips.Count;
163  int last = count - 1;
164  for(int i = 0; i < count; ++i)
165  {
166  int rand = Random.Range(i, count);
167  int tmp = shuffleList[i];
168  shuffleList[i] = shuffleList[rand];
169  shuffleList[rand] = tmp;
170  }
171  }
172  }
173 
175  {
176  shuffleList = new List<int>();
177  for (int i = 0; i < Clips.Count; i++)
178  shuffleList.Add(i);
179  }
180  }
181 }
SimpleAudioPlayer VoicesAudioPlayer
Definition: AudioSystem.cs:42
This ScriptableObject contains the data of a playlist for the AudioPlayer.
Definition: Playlist.cs:19
SimpleAudioPlayer UiFxAudioPlayer
Definition: AudioSystem.cs:41
void ChangePlaylist(Playlist playlist)
void ChangeVoicesPlaylist(bool playPlaylist=false)
Definition: Playlist.cs:130
SoundClip GetClipByClipId(int clipId)
Definition: Playlist.cs:96
SimpleAudioPlayer MusicAudioPlayer
Definition: AudioSystem.cs:38
List< int > shuffleList
Definition: Playlist.cs:36
static AudioSystem Instance
Definition: AudioSystem.cs:15
The AudioSystem is responsble of the creation and access of the general purpose AudioPlayers.
Definition: AudioSystem.cs:12
SimpleAudioPlayer FxAudioPlayer
Definition: AudioSystem.cs:40
SoundClip GetNextClip()
Definition: Playlist.cs:56
void ChangeAmbientPlaylist(bool playPlaylist=true)
Definition: Playlist.cs:115
SimpleAudioPlayer AmbientAudioPlayer
Definition: AudioSystem.cs:39
SoundClip GetClipByName(string name)
Definition: Playlist.cs:82
void ChangeMusicPlaylist(bool playPlaylist=true)
Definition: Playlist.cs:110
This is a container ScriptableObject that holds all data required to play a AudioClip.
Definition: SoundClip.cs:11
void ChangeFxPlaylist(bool playPlaylist=false)
Definition: Playlist.cs:120
void ChangeUiFxPlaylist(bool playPlaylist=false)
Definition: Playlist.cs:125