123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /*
- * FUNCTION:
- * This script plays and controls sounds used. Only 2D sounds are used
- * which are handled by a single SoundManager prefab.
- *
- * USED BY: This script is a part of the SoundManager prefab.
- */
- using System.Collections.Generic;
- using UnityEngine;
- using System.Collections;
- public class SoundManagerCS : MonoBehaviour
- {
- private class StateManager
- {
- private class State
- {
- private AudioSource _source;
- private float _volume;
- private bool _isPlaying;
- public State(AudioSource source)
- {
- _source = source;
- }
- public void Mute()
- {
- _volume = _source.volume;
- _source.volume = 0;
- _isPlaying = _source.isPlaying;
- if (_isPlaying)
- {
- _source.Pause();
- }
- }
- public void UnMute()
- {
- _source.volume = 1;
- if (_isPlaying)
- {
- _source.UnPause();
- }
- }
- }
- private List<State> _sources = new List<State>();
- public StateManager(params AudioSource[][] sources)
- {
- foreach (var source in sources)
- {
- foreach (var audioSource in source)
- {
- _sources.Add(new State(audioSource));
- }
- }
- }
- public void Mute()
- {
- foreach (var state in _sources)
- {
- state.Mute();
- }
- }
- public void UnMute()
- {
- foreach (var state in _sources)
- {
- state.UnMute();
- }
- }
- }
- public static SoundManagerCS Instance;
- public AudioSource[] asCharacterSounds;
- public AudioSource[] asPowerupSounds;
- public AudioSource[] asMenuSounds;
- public AudioSource[] asMusic;
- public AudioSource[] asEnemySounds;
- public AudioSource GameOverSound;
- public UIToggle MusicDisable;
- public UIToggle MusicEnable;
- public UIToggle FxDisable;
- public UIToggle FxEnable;
- public bool Sound
- {
- get { return MusicEnable.value; }
- }
- public bool Fx
- {
- get { return FxEnable.value; }
- }
- //Sound Enums
- public enum CharacterSounds
- {
- Footsteps = 0,
- JumpLand = 1,
- Swipe = 2,
- OhYeah = 3,
- Slide = 4,
- }
-
- public enum PowerupSounds
- {
- CurrencyCollection = 0,
- PowerupCollection = 1
- }
-
- public enum MenuSounds
- {
- ButtonTap = 0,
- CC_Ping = 1
- }
-
- public enum EnemySounds
- {
- TiresSqueal = 0,
- Siren = 1,
- Blood = 2,
- Crush = 3
- }
-
- //Audio state
- public bool bSoundEnabled = true; //gameplay sounds
- private bool bMusicEnabled = true;//background music
-
- //script references
- private InGameScriptCS hInGameScriptCS;
- private ControllerScriptCS hControllerScriptCS;
-
- //variables
- private bool bPlayFootsteps = false;
- private bool bFootstepsPlaying = false;
-
- /*
- * FUNCTION: Turn the sounds On or Off
- */
- public void toggleSoundEnabled (bool state)
- {
- bSoundEnabled = state;
- PlayerPrefs.SetInt("FX", bSoundEnabled ? 1 : 0);
- PlayerPrefs.Save();
- }
-
- /*
- * FUNCION: Turn the background music ON or OFF
- */
- public void toggleMusicEnabled (bool state)
- {
- bMusicEnabled = state;
- if (state)
- {
- asMusic[0].Play();
- }
- else
- {
- asMusic[0].Pause();
- }
- PlayerPrefs.SetInt("Music", bMusicEnabled ? 1 : 0);
- PlayerPrefs.Save();
- }
-
- public bool isSoundEnabled ()
- {
- return bSoundEnabled;
- }
- public bool isMusicEnabled ()
- {
- return bMusicEnabled;
- }
-
- void Start ()
- {
- _manager = new StateManager(asCharacterSounds, asPowerupSounds, asMenuSounds, asMusic, asEnemySounds);
- Instance = this;
- hControllerScriptCS = (ControllerScriptCS)GameObject.Find ("Player").GetComponent (typeof(ControllerScriptCS));
- hInGameScriptCS = (InGameScriptCS)GameObject.Find ("Player").GetComponent (typeof(InGameScriptCS));
- hControllerScriptCS = (ControllerScriptCS)GameObject.Find ("Player").GetComponent (typeof(ControllerScriptCS));
-
- stopAllSounds ();
-
- bSoundEnabled = PlayerPrefs.GetInt("FX", 1) == 1;
- bMusicEnabled = PlayerPrefs.GetInt("Music", 1) == 1;
- StartCoroutine(EnableMusic());
- }
- public void GameOverMusic()
- {
- if (bSoundEnabled)
- GameOverSound.Play();
-
- }
- private IEnumerator EnableMusic()
- {
- yield return new WaitForSeconds(2f);
- if (bMusicEnabled)
- {
- asMusic[0].Play();
- }
- else
- {
- asMusic[0].Stop();
- }
- //MusicEnable.value = bMusicEnabled;
- //FxEnable.value = bSoundEnabled;
- //MusicDisable.onChange.Add(new EventDelegate(SetMusic));
- // MusicEnable.onChange.Add(new EventDelegate(SetMusic));
- //FxDisable.onChange.Add(new EventDelegate(SetFx));
- //FxEnable.onChange.Add(new EventDelegate(SetFx));
- }
- private void SetMusic()
- {
- toggleMusicEnabled(MusicEnable.value);
- }
- private void SetFx()
- {
- toggleSoundEnabled(FxEnable.value);
- }
- private StateManager _manager;
- void FixedUpdate ()
- {
- StartCoroutine (toggleFootStepsSound ());
-
- if (hInGameScriptCS.isGamePaused () == true)
- stopSound (CharacterSounds.Footsteps);
-
- if (bPlayFootsteps == true) {
- //adjust footsteps pitch according to movement speed
- asCharacterSounds [(int)CharacterSounds.Footsteps].pitch = hControllerScriptCS.getCurrentForwardSpeed () / 3.0f;
- if (bFootstepsPlaying == false) {
- if (bSoundEnabled)
- asCharacterSounds [(int)CharacterSounds.Footsteps].Play ();
- bFootstepsPlaying = true;
- }
- } else {
- if (bFootstepsPlaying == true) {
- if (bSoundEnabled)
- asCharacterSounds [(int)CharacterSounds.Footsteps].Stop ();
- bFootstepsPlaying = false;
- }
- }
- }
-
- /*
- * FUNCTION: Play a sound
- */
- public void playSound (CharacterSounds soundType)
- {
- if (bSoundEnabled)
- asCharacterSounds [(int)soundType].Play ();
- }
- public void playSound (PowerupSounds soundType)
- {
- if (bSoundEnabled)
- asPowerupSounds [(int)soundType].Play ();
- }
- public void playSound (MenuSounds soundType)
- {
- if (bSoundEnabled)
- asMenuSounds [(int)soundType].Play ();
- }
- public void playSound (EnemySounds soundType)
- {
- //santa has no enemy
- return;
- if (bSoundEnabled && asEnemySounds [(int)soundType].isPlaying == false)
- asEnemySounds [(int)soundType].Play ();
- }
-
- /*
- * FUNCITON: Stop a sound
- */
- public void stopSound (CharacterSounds soundType)
- {
- asCharacterSounds [(int)soundType].Stop ();
- }
- public void stopSound (PowerupSounds soundType)
- {
- asPowerupSounds [(int)soundType].Stop ();
- }
- public void stopSound (MenuSounds soundType)
- {
- asMenuSounds [(int)soundType].Stop ();
- }
- public void stopSound (EnemySounds soundType)
- {
- asEnemySounds [(int)soundType].Stop ();
- }
-
- /*
- * FUNCTION: Turn off footsetps sound if player is in the air and vice versa
- */
- private IEnumerator toggleFootStepsSound ()
- {
- yield return new WaitForEndOfFrame();
-
- if (!hControllerScriptCS.isInAir ())
- bPlayFootsteps = true;
- else
- bPlayFootsteps = false;
- }
-
- /*
- * FUNCTION: Stops all sounds except background music
- */
- public void stopAllSounds ()
- {
- for (int i=0; i<CharacterSounds.GetValues(typeof(CharacterSounds)).Length; i++)
- asCharacterSounds [i].Stop ();
- for (int i=0; i<PowerupSounds.GetValues(typeof(PowerupSounds)).Length; i++)
- asPowerupSounds [i].Stop ();
- for (int i=0; i<MenuSounds.GetValues(typeof(MenuSounds)).Length; i++)
- asMenuSounds [i].Stop ();
- for (int i=0; i<EnemySounds.GetValues(typeof(EnemySounds)).Length; i++)
- asEnemySounds [i].Stop ();
-
- bFootstepsPlaying = false;
- }
-
- /*
- * FUNCTION: Check if a sound is currently playing.
- */
- public bool isPlaying (CharacterSounds sound)
- {
- if (asCharacterSounds [(int)sound].isPlaying)
- return true;
- else
- return false;
- }
- public bool isPlaying (PowerupSounds sound)
- {
- if (asPowerupSounds [(int)sound].isPlaying)
- return true;
- else
- return false;
- }
- public bool isPlaying (MenuSounds sound)
- {
- if (asMenuSounds [(int)sound].isPlaying)
- return true;
- else
- return false;
- }
- public bool isPlaying (EnemySounds sound)
- {
- if (asEnemySounds [(int)sound].isPlaying)
- return true;
- else
- return false;
- }
-
- /*
- * FUNCITON: Enable or disable the current script.
- * */
- public void toggleScriptState (bool state)
- {
- this.enabled = state;
- }
- public void EndVideoAd()
- {
- _manager.UnMute();
- }
- public void PlayVideoAd()
- {
- _manager.Mute();
- }
- }
|