Guest User

Untitled

a guest
May 27th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.51 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class SoundManager : MonoBehaviour {
  7.  
  8.     bool isTrue = false;
  9.     bool YES = false;
  10.     bool changeSoundCheckIsTrue = false;
  11.     bool onField = false;
  12.     bool doChangeToField = true;
  13.     public Dictionary<int, AudioSource> audioDict = new Dictionary<int, AudioSource>();
  14.     public Dictionary<int, AudioSource> audioDictPlayed = new Dictionary<int, AudioSource>();
  15.     AudioSource footAudio;
  16.     AudioSource bassAudio;
  17.     AudioSource hugSource;
  18.     AudioSource hugPurr;
  19.     AudioSource currentThemeSource;
  20.     AudioSource titleSource, nightmareSource;
  21.     AudioSource heartBeatSource;
  22.  
  23.     AudioClip mainTitleClip, fieldTitleClip;
  24.     AudioClip hugClip1, hugClip2, hugClip3;
  25.     AudioClip grass1, grass2, grass3, standardFootClip, sludge1, sludge2, sludge3, sludge4;
  26.     GameObject houseWhisper;
  27.     GameObject bookWhisper;
  28.     GameObject leonObject;
  29.     Transform playerTransform;
  30.     float bassPercentage;
  31.  
  32.     HelperFunctions helpFunctions;
  33.    
  34.     float houseVolume = 0.5f;
  35.     float bookVolume = 0.5f;
  36.  
  37.     bool playHugSound = false;
  38.     bool playTitleMusic = true;
  39.  
  40.     RaycastHit footStepRay;
  41.  
  42.     PhysicMaterial grassMat;
  43.  
  44.     public void Initialize()
  45.     {
  46.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
  47.         grassMat = (PhysicMaterial)Resources.Load("Physicmat/GrassMat");
  48.  
  49.         helpFunctions = GameObject.Find("Main").GetComponent<HelperFunctions>();
  50.         titleSource = playerTransform.gameObject.AddComponent<AudioSource>();
  51.         nightmareSource = playerTransform.gameObject.AddComponent<AudioSource>();
  52.         heartBeatSource = playerTransform.gameObject.AddComponent<AudioSource>();
  53.         nightmareSource.volume = 0f;
  54.         titleSource.volume = 0.5f;
  55.         titleSource.loop = true;
  56.         nightmareSource.loop = true;
  57.         footAudio = gameObject.AddComponent<AudioSource>();
  58.         bassAudio = gameObject.AddComponent<AudioSource>();
  59.         houseWhisper = GameObject.Find("Large_House");
  60.         bookWhisper = GameObject.Find("Bookwhispers");
  61.         leonObject = GameObject.FindGameObjectWithTag("Leon");
  62.         hugSource = leonObject.AddComponent<AudioSource>();
  63.         hugPurr = leonObject.AddComponent<AudioSource>();
  64.  
  65.         LoadSounds();
  66.         nightmareSource.clip = mainTitleClip;
  67.  
  68.     }
  69.  
  70.     public void UpdateSound (ref PlayerData _playerData) {
  71.  
  72.         //if (audioDict.Count > (5 + Convert.ToInt32(isTrue)) * Convert.ToInt32(YES))
  73.         //{
  74.         //    Debug.Log("check");
  75.         //}
  76.  
  77.         HouseSound(_playerData.GetHugIntensity());
  78.         BookSound(_playerData.GetHugIntensity());
  79.         BassSound(_playerData.GetFearLevel(), _playerData.GetFearLevelMax());
  80.         ChangeThemeMusic(_playerData.GetTransitionTimer());
  81.         HeartBeat(_playerData.GetFearLevel());
  82.         //HugSoundTest(_playerData.GetTransitionTimer());
  83.     }
  84.  
  85.  
  86.     /// <summary>
  87.     /// Main function of the sound manager.
  88.     /// </summary>
  89.     /// <param name="_instanceID">Send in the instanceID of the audio source</param>
  90.     /// <param name="_audio">Send in the audio source</param>
  91.     /// <param name="_playOnce">if it should only be played once set this to true</param>
  92.     /// <param name="_pitchAmount">If you would like to pitch the sound. Default is 1.</param>
  93.     /// <param name="_volume">Set the volume of the audio source. Defualt is 1.</param>
  94.     public void PlaySound(int _instanceID, AudioSource _audio, bool _playOnce, float _pitchAmount, float _volume)      
  95.     {
  96.  
  97.         audioDict.Add(_instanceID, _audio);
  98.  
  99.         if (!_audio.isPlaying && !audioDictPlayed.ContainsKey(_instanceID))
  100.         {
  101.             _audio.volume = _volume;
  102.             _audio.pitch = _pitchAmount;
  103.             _audio.Play();
  104.            
  105.         }
  106.  
  107.         if (_playOnce && !audioDictPlayed.ContainsKey(_instanceID))
  108.             audioDictPlayed.Add(_instanceID, _audio);
  109.  
  110.         DeleteSound(_instanceID, _audio);
  111.     }
  112.  
  113.     public void FootSteps(float _volume)
  114.     {
  115.         float randomPitch = Random.Range(0.8f, 1.2f);
  116.  
  117.         if (Physics.Raycast(playerTransform.position, Vector3.down, out footStepRay, 1f))
  118.         {
  119.             Debug.DrawLine(playerTransform.position, footStepRay.point, Color.red);
  120.             if (footStepRay.collider.material == grassMat)
  121.             {
  122.                 Debug.Log("Im hitting the grass!");
  123.                 int randFootClip = Random.Range(0, 3);
  124.                 if (randFootClip == 0)
  125.                     footAudio.clip = grass1;
  126.                 else if (randFootClip == 1)
  127.                     footAudio.clip = grass2;
  128.                 else if (randFootClip == 2)
  129.                     footAudio.clip = grass3;
  130.             }
  131.             else if(footStepRay.collider.material == grassMat)
  132.             {
  133.                 //Check for other material
  134.                 //add other footstep sound
  135.             }
  136.         }
  137.         footAudio.volume = _volume;
  138.         footAudio.pitch = randomPitch;
  139.         footAudio.Play();
  140.     }
  141.  
  142.     void DeleteSound(int _instanceID, AudioSource _audio)
  143.     {
  144.         audioDict.Remove(_instanceID);
  145.     }
  146.  
  147.     //Make beautiful this looks shit!
  148.     void HouseSound(float _hugIntensity)
  149.     {
  150.         if (_hugIntensity > 0)
  151.         {
  152.             if (houseVolume > 0)
  153.                 houseVolume -= Time.deltaTime * 0.2f;
  154.  
  155.             houseWhisper.GetComponent<AudioSource>().volume = houseVolume;
  156.         }
  157.         else
  158.         {
  159.             if (houseVolume < 0.5f)
  160.             {
  161.                 houseVolume += Time.deltaTime * 0.2f;
  162.                 houseWhisper.GetComponent<AudioSource>().volume = houseVolume;
  163.             }
  164.         }
  165.     }
  166.  
  167.     void HeartBeat(float _fearLevel)
  168.     {
  169.         if (_fearLevel > 20)
  170.         {
  171.             if (!heartBeatSource.isPlaying)
  172.             {
  173.                 heartBeatSource.Play();
  174.             }
  175.             heartBeatSource.volume = _fearLevel * 0.1f;
  176.         }
  177.         else
  178.         {
  179.             heartBeatSource.volume -= Time.deltaTime;
  180.             Mathf.Clamp(heartBeatSource.volume, 0, 0.3f);
  181.         }
  182.     }
  183.  
  184.     void BookSound(float _transitionTimer)
  185.     {
  186.         if (_transitionTimer > 0)
  187.         {
  188.             if (bookVolume > 0)
  189.                 bookVolume -= Time.deltaTime * 0.2f;
  190.  
  191.             bookWhisper.GetComponent<AudioSource>().volume = bookVolume;
  192.         }
  193.         else
  194.         {
  195.             if (bookVolume < 0.5f)
  196.             {
  197.                 bookVolume += Time.deltaTime * 0.2f;
  198.                 bookWhisper.GetComponent<AudioSource>().volume = bookVolume;
  199.             }
  200.         }
  201.     }
  202.  
  203.     void BassSound(float _fearLevel, float _maxFearLevel)
  204.     {
  205.  
  206.         if (!bassAudio.isPlaying)
  207.             bassAudio.Play();
  208.  
  209.         bassPercentage = (_fearLevel / _maxFearLevel) / 2;
  210.         bassAudio.volume = Mathf.Lerp(0.05f, 1f, bassPercentage);
  211.     }
  212.  
  213.     public void HugSound()
  214.     {
  215.         if (!hugSource.isPlaying)
  216.         {
  217.             int randSound = Random.Range(0, 3);
  218.  
  219.             if (randSound == 0)
  220.                 hugSource.clip = hugClip1;
  221.  
  222.             else if (randSound == 1)
  223.                 hugSource.clip = hugClip1;
  224.  
  225.             else if (randSound == 2)
  226.                 hugSource.clip = hugClip1;
  227.  
  228.             hugSource.Play();
  229.         }
  230.  
  231.         playHugSound = true;
  232.     }
  233.  
  234.     void MainMusic(AudioSource _aSource)
  235.     {
  236.         if (currentThemeSource != _aSource || onField)
  237.         {
  238.             if (currentThemeSource == null)
  239.                 currentThemeSource = _aSource;
  240.  
  241.             currentThemeSource.volume = helpFunctions.FadeOutSound(currentThemeSource.volume, 0);
  242.  
  243.             if (currentThemeSource.volume == 0)
  244.             {
  245.                 changeSoundCheckIsTrue = true;
  246.                 currentThemeSource = _aSource;
  247.                 if (onField)
  248.                 {
  249.                     nightmareSource.clip = fieldTitleClip;
  250.                     onField = false;
  251.                 }
  252.                    
  253.             }
  254.  
  255.         }
  256.         if (changeSoundCheckIsTrue)
  257.         {
  258.             float maxVolume = 0.5f;
  259.             currentThemeSource.volume = helpFunctions.FadeInSound(currentThemeSource.volume, maxVolume);
  260.             if (currentThemeSource.volume == maxVolume)
  261.             {
  262.                 changeSoundCheckIsTrue = false;
  263.             }
  264.         }
  265.     }
  266.  
  267.     void ChangeThemeMusic(float _transitionTimer)
  268.     {
  269.  
  270.         if (!titleSource.isPlaying)
  271.             titleSource.Play();
  272.  
  273.         if (!nightmareSource.isPlaying)
  274.             nightmareSource.Play();
  275.  
  276.         if (_transitionTimer > 0 || playTitleMusic)
  277.             MainMusic(titleSource);
  278.         else
  279.             MainMusic(nightmareSource);
  280.     }
  281.  
  282.     //void HugSoundTest(float _transTimer)
  283.     //{
  284.     //    if (!hugSource.isPlaying)
  285.     //    {
  286.     //        hugSource.clip = hugClip1;
  287.     //        hugSource.Play();
  288.     //        hugSource.volume = _transTimer * 0.1f;
  289.     //    }
  290.  
  291.     //    Mathf.Clamp(hugSource.volume,0,0.3f);
  292.     //}
  293.  
  294.     public void SetOnField(bool _onField) { onField = _onField; }
  295.     public void SetTitleMusic(bool _playTitleMusic) { playTitleMusic = _playTitleMusic; }
  296.  
  297.     void LoadSounds()
  298.     {
  299.         if (!(hugClip1 = (AudioClip)Resources.Load("Sounds/Sounds/leon_hug_cloth1")))
  300.             Debug.Log("Laddar inte kramljud1!");
  301.  
  302.         if (!(hugClip2 = (AudioClip)Resources.Load("Sounds/Sounds/leon_hug_cloth2")))
  303.             Debug.Log("Laddar inte kramljud2!");
  304.  
  305.         if (!(hugClip3 = (AudioClip)Resources.Load("Sounds/Sounds/leon_hug_cloth3")))
  306.             Debug.Log("Laddar inte kramljud3!");
  307.  
  308.         if (!(hugPurr.clip = (AudioClip)Resources.Load("Sounds/Sounds/leon_purr")))
  309.             Debug.Log("Laddar inte purrljud!");
  310.  
  311.         if (!(bassAudio.clip = (AudioClip)Resources.Load("Sounds/Basljud")))
  312.             Debug.Log("Laddar inte basljud!");
  313.  
  314.         if (!(standardFootClip = (AudioClip)Resources.Load("Sounds/fot4")))
  315.             Debug.Log("Laddar inte fotljud!");
  316.  
  317.         if (!(grass1 = (AudioClip)Resources.Load("Sounds/Sounds/grass1")))
  318.             Debug.Log("Laddar inte fotljud!");
  319.  
  320.         if (!(grass2 = (AudioClip)Resources.Load("Sounds/Sounds/grass2")))
  321.             Debug.Log("Laddar inte fotljud!");
  322.  
  323.         if (!(grass3 = (AudioClip)Resources.Load("Sounds/Sounds/grass3")))
  324.             Debug.Log("Laddar inte fotljud!");
  325.  
  326.         if (!(grass1 = (AudioClip)Resources.Load("Sounds/Sounds/grass1")))
  327.             Debug.Log("Laddar inte fotljud!");
  328.  
  329.         if (!(sludge1 = (AudioClip)Resources.Load("Sounds/Sounds/sludge1")))
  330.             Debug.Log("Laddar inte fotljud!");
  331.  
  332.         if (!(sludge2 = (AudioClip)Resources.Load("Sounds/Sounds/sludge2")))
  333.             Debug.Log("Laddar inte fotljud!");
  334.  
  335.         if (!(sludge3 = (AudioClip)Resources.Load("Sounds/Sounds/sludge3")))
  336.             Debug.Log("Laddar inte fotljud!");
  337.  
  338.         if (!(sludge4 = (AudioClip)Resources.Load("Sounds/Sounds/sludge4")))
  339.             Debug.Log("Laddar inte fotljud!");
  340.  
  341.         if (!(mainTitleClip = (AudioClip)Resources.Load("Sounds/somniumHUB")))
  342.             Debug.Log("Laddar inte nightmareljud");
  343.  
  344.         if (!(fieldTitleClip = (AudioClip)Resources.Load("Sounds/SomniumFIELD")))
  345.             Debug.Log("Laddar somniumFIELD");
  346.  
  347.         if (!(titleSource.clip = (AudioClip)Resources.Load("Sounds/TitelSomnFade")))
  348.             Debug.Log("Laddar inte nightmareljud");
  349.  
  350.         if (!(heartBeatSource.clip = (AudioClip)Resources.Load("Sounds/heartbeat")))
  351.             Debug.Log("Laddar inte heartbeat");
  352.  
  353.  
  354.     }
  355.  
  356. }
Advertisement
Add Comment
Please, Sign In to add comment