SoundManagerCS.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * FUNCTION:
  3. * This script plays and controls sounds used. Only 2D sounds are used
  4. * which are handled by a single SoundManager prefab.
  5. *
  6. * USED BY: This script is a part of the SoundManager prefab.
  7. */
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using System.Collections;
  11. public class SoundManagerCS : MonoBehaviour
  12. {
  13. private class StateManager
  14. {
  15. private class State
  16. {
  17. private AudioSource _source;
  18. private float _volume;
  19. private bool _isPlaying;
  20. public State(AudioSource source)
  21. {
  22. _source = source;
  23. }
  24. public void Mute()
  25. {
  26. _volume = _source.volume;
  27. _source.volume = 0;
  28. _isPlaying = _source.isPlaying;
  29. if (_isPlaying)
  30. {
  31. _source.Pause();
  32. }
  33. }
  34. public void UnMute()
  35. {
  36. _source.volume = 1;
  37. if (_isPlaying)
  38. {
  39. _source.UnPause();
  40. }
  41. }
  42. }
  43. private List<State> _sources = new List<State>();
  44. public StateManager(params AudioSource[][] sources)
  45. {
  46. foreach (var source in sources)
  47. {
  48. foreach (var audioSource in source)
  49. {
  50. _sources.Add(new State(audioSource));
  51. }
  52. }
  53. }
  54. public void Mute()
  55. {
  56. foreach (var state in _sources)
  57. {
  58. state.Mute();
  59. }
  60. }
  61. public void UnMute()
  62. {
  63. foreach (var state in _sources)
  64. {
  65. state.UnMute();
  66. }
  67. }
  68. }
  69. public static SoundManagerCS Instance;
  70. public AudioSource[] asCharacterSounds;
  71. public AudioSource[] asPowerupSounds;
  72. public AudioSource[] asMenuSounds;
  73. public AudioSource[] asMusic;
  74. public AudioSource[] asEnemySounds;
  75. public AudioSource GameOverSound;
  76. public UIToggle MusicDisable;
  77. public UIToggle MusicEnable;
  78. public UIToggle FxDisable;
  79. public UIToggle FxEnable;
  80. public bool Sound
  81. {
  82. get { return MusicEnable.value; }
  83. }
  84. public bool Fx
  85. {
  86. get { return FxEnable.value; }
  87. }
  88. //Sound Enums
  89. public enum CharacterSounds
  90. {
  91. Footsteps = 0,
  92. JumpLand = 1,
  93. Swipe = 2,
  94. OhYeah = 3,
  95. Slide = 4,
  96. }
  97. public enum PowerupSounds
  98. {
  99. CurrencyCollection = 0,
  100. PowerupCollection = 1
  101. }
  102. public enum MenuSounds
  103. {
  104. ButtonTap = 0,
  105. CC_Ping = 1
  106. }
  107. public enum EnemySounds
  108. {
  109. TiresSqueal = 0,
  110. Siren = 1,
  111. Blood = 2,
  112. Crush = 3
  113. }
  114. //Audio state
  115. public bool bSoundEnabled = true; //gameplay sounds
  116. private bool bMusicEnabled = true;//background music
  117. //script references
  118. private InGameScriptCS hInGameScriptCS;
  119. private ControllerScriptCS hControllerScriptCS;
  120. //variables
  121. private bool bPlayFootsteps = false;
  122. private bool bFootstepsPlaying = false;
  123. /*
  124. * FUNCTION: Turn the sounds On or Off
  125. */
  126. public void toggleSoundEnabled (bool state)
  127. {
  128. bSoundEnabled = state;
  129. PlayerPrefs.SetInt("FX", bSoundEnabled ? 1 : 0);
  130. PlayerPrefs.Save();
  131. }
  132. /*
  133. * FUNCION: Turn the background music ON or OFF
  134. */
  135. public void toggleMusicEnabled (bool state)
  136. {
  137. bMusicEnabled = state;
  138. if (state)
  139. {
  140. asMusic[0].Play();
  141. }
  142. else
  143. {
  144. asMusic[0].Pause();
  145. }
  146. PlayerPrefs.SetInt("Music", bMusicEnabled ? 1 : 0);
  147. PlayerPrefs.Save();
  148. }
  149. public bool isSoundEnabled ()
  150. {
  151. return bSoundEnabled;
  152. }
  153. public bool isMusicEnabled ()
  154. {
  155. return bMusicEnabled;
  156. }
  157. void Start ()
  158. {
  159. _manager = new StateManager(asCharacterSounds, asPowerupSounds, asMenuSounds, asMusic, asEnemySounds);
  160. Instance = this;
  161. hControllerScriptCS = (ControllerScriptCS)GameObject.Find ("Player").GetComponent (typeof(ControllerScriptCS));
  162. hInGameScriptCS = (InGameScriptCS)GameObject.Find ("Player").GetComponent (typeof(InGameScriptCS));
  163. hControllerScriptCS = (ControllerScriptCS)GameObject.Find ("Player").GetComponent (typeof(ControllerScriptCS));
  164. stopAllSounds ();
  165. bSoundEnabled = PlayerPrefs.GetInt("FX", 1) == 1;
  166. bMusicEnabled = PlayerPrefs.GetInt("Music", 1) == 1;
  167. StartCoroutine(EnableMusic());
  168. }
  169. public void GameOverMusic()
  170. {
  171. if (bSoundEnabled)
  172. GameOverSound.Play();
  173. }
  174. private IEnumerator EnableMusic()
  175. {
  176. yield return new WaitForSeconds(2f);
  177. if (bMusicEnabled)
  178. {
  179. asMusic[0].Play();
  180. }
  181. else
  182. {
  183. asMusic[0].Stop();
  184. }
  185. //MusicEnable.value = bMusicEnabled;
  186. //FxEnable.value = bSoundEnabled;
  187. //MusicDisable.onChange.Add(new EventDelegate(SetMusic));
  188. // MusicEnable.onChange.Add(new EventDelegate(SetMusic));
  189. //FxDisable.onChange.Add(new EventDelegate(SetFx));
  190. //FxEnable.onChange.Add(new EventDelegate(SetFx));
  191. }
  192. private void SetMusic()
  193. {
  194. toggleMusicEnabled(MusicEnable.value);
  195. }
  196. private void SetFx()
  197. {
  198. toggleSoundEnabled(FxEnable.value);
  199. }
  200. private StateManager _manager;
  201. void FixedUpdate ()
  202. {
  203. StartCoroutine (toggleFootStepsSound ());
  204. if (hInGameScriptCS.isGamePaused () == true)
  205. stopSound (CharacterSounds.Footsteps);
  206. if (bPlayFootsteps == true) {
  207. //adjust footsteps pitch according to movement speed
  208. asCharacterSounds [(int)CharacterSounds.Footsteps].pitch = hControllerScriptCS.getCurrentForwardSpeed () / 3.0f;
  209. if (bFootstepsPlaying == false) {
  210. if (bSoundEnabled)
  211. asCharacterSounds [(int)CharacterSounds.Footsteps].Play ();
  212. bFootstepsPlaying = true;
  213. }
  214. } else {
  215. if (bFootstepsPlaying == true) {
  216. if (bSoundEnabled)
  217. asCharacterSounds [(int)CharacterSounds.Footsteps].Stop ();
  218. bFootstepsPlaying = false;
  219. }
  220. }
  221. }
  222. /*
  223. * FUNCTION: Play a sound
  224. */
  225. public void playSound (CharacterSounds soundType)
  226. {
  227. if (bSoundEnabled)
  228. asCharacterSounds [(int)soundType].Play ();
  229. }
  230. public void playSound (PowerupSounds soundType)
  231. {
  232. if (bSoundEnabled)
  233. asPowerupSounds [(int)soundType].Play ();
  234. }
  235. public void playSound (MenuSounds soundType)
  236. {
  237. if (bSoundEnabled)
  238. asMenuSounds [(int)soundType].Play ();
  239. }
  240. public void playSound (EnemySounds soundType)
  241. {
  242. //santa has no enemy
  243. return;
  244. if (bSoundEnabled && asEnemySounds [(int)soundType].isPlaying == false)
  245. asEnemySounds [(int)soundType].Play ();
  246. }
  247. /*
  248. * FUNCITON: Stop a sound
  249. */
  250. public void stopSound (CharacterSounds soundType)
  251. {
  252. asCharacterSounds [(int)soundType].Stop ();
  253. }
  254. public void stopSound (PowerupSounds soundType)
  255. {
  256. asPowerupSounds [(int)soundType].Stop ();
  257. }
  258. public void stopSound (MenuSounds soundType)
  259. {
  260. asMenuSounds [(int)soundType].Stop ();
  261. }
  262. public void stopSound (EnemySounds soundType)
  263. {
  264. asEnemySounds [(int)soundType].Stop ();
  265. }
  266. /*
  267. * FUNCTION: Turn off footsetps sound if player is in the air and vice versa
  268. */
  269. private IEnumerator toggleFootStepsSound ()
  270. {
  271. yield return new WaitForEndOfFrame();
  272. if (!hControllerScriptCS.isInAir ())
  273. bPlayFootsteps = true;
  274. else
  275. bPlayFootsteps = false;
  276. }
  277. /*
  278. * FUNCTION: Stops all sounds except background music
  279. */
  280. public void stopAllSounds ()
  281. {
  282. for (int i=0; i<CharacterSounds.GetValues(typeof(CharacterSounds)).Length; i++)
  283. asCharacterSounds [i].Stop ();
  284. for (int i=0; i<PowerupSounds.GetValues(typeof(PowerupSounds)).Length; i++)
  285. asPowerupSounds [i].Stop ();
  286. for (int i=0; i<MenuSounds.GetValues(typeof(MenuSounds)).Length; i++)
  287. asMenuSounds [i].Stop ();
  288. for (int i=0; i<EnemySounds.GetValues(typeof(EnemySounds)).Length; i++)
  289. asEnemySounds [i].Stop ();
  290. bFootstepsPlaying = false;
  291. }
  292. /*
  293. * FUNCTION: Check if a sound is currently playing.
  294. */
  295. public bool isPlaying (CharacterSounds sound)
  296. {
  297. if (asCharacterSounds [(int)sound].isPlaying)
  298. return true;
  299. else
  300. return false;
  301. }
  302. public bool isPlaying (PowerupSounds sound)
  303. {
  304. if (asPowerupSounds [(int)sound].isPlaying)
  305. return true;
  306. else
  307. return false;
  308. }
  309. public bool isPlaying (MenuSounds sound)
  310. {
  311. if (asMenuSounds [(int)sound].isPlaying)
  312. return true;
  313. else
  314. return false;
  315. }
  316. public bool isPlaying (EnemySounds sound)
  317. {
  318. if (asEnemySounds [(int)sound].isPlaying)
  319. return true;
  320. else
  321. return false;
  322. }
  323. /*
  324. * FUNCITON: Enable or disable the current script.
  325. * */
  326. public void toggleScriptState (bool state)
  327. {
  328. this.enabled = state;
  329. }
  330. public void EndVideoAd()
  331. {
  332. _manager.UnMute();
  333. }
  334. public void PlayVideoAd()
  335. {
  336. _manager.Mute();
  337. }
  338. }