Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SoundManager : MonoBehaviour {
- bool isTrue = false;
- bool YES = false;
- bool changeSoundCheckIsTrue = false;
- bool onField = false;
- bool doChangeToField = true;
- public Dictionary<int, AudioSource> audioDict = new Dictionary<int, AudioSource>();
- public Dictionary<int, AudioSource> audioDictPlayed = new Dictionary<int, AudioSource>();
- AudioSource footAudio;
- AudioSource bassAudio;
- AudioSource hugSource;
- AudioSource hugPurr;
- AudioSource currentThemeSource;
- AudioSource titleSource, nightmareSource;
- AudioSource heartBeatSource;
- AudioClip mainTitleClip, fieldTitleClip;
- AudioClip hugClip1, hugClip2, hugClip3;
- AudioClip grass1, grass2, grass3, standardFootClip, sludge1, sludge2, sludge3, sludge4;
- GameObject houseWhisper;
- GameObject bookWhisper;
- GameObject leonObject;
- Transform playerTransform;
- float bassPercentage;
- HelperFunctions helpFunctions;
- float houseVolume = 0.5f;
- float bookVolume = 0.5f;
- bool playHugSound = false;
- bool playTitleMusic = true;
- RaycastHit footStepRay;
- PhysicMaterial grassMat;
- public void Initialize()
- {
- playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
- grassMat = (PhysicMaterial)Resources.Load("Physicmat/GrassMat");
- helpFunctions = GameObject.Find("Main").GetComponent<HelperFunctions>();
- titleSource = playerTransform.gameObject.AddComponent<AudioSource>();
- nightmareSource = playerTransform.gameObject.AddComponent<AudioSource>();
- heartBeatSource = playerTransform.gameObject.AddComponent<AudioSource>();
- nightmareSource.volume = 0f;
- titleSource.volume = 0.5f;
- titleSource.loop = true;
- nightmareSource.loop = true;
- footAudio = gameObject.AddComponent<AudioSource>();
- bassAudio = gameObject.AddComponent<AudioSource>();
- houseWhisper = GameObject.Find("Large_House");
- bookWhisper = GameObject.Find("Bookwhispers");
- leonObject = GameObject.FindGameObjectWithTag("Leon");
- hugSource = leonObject.AddComponent<AudioSource>();
- hugPurr = leonObject.AddComponent<AudioSource>();
- LoadSounds();
- nightmareSource.clip = mainTitleClip;
- }
- public void UpdateSound (ref PlayerData _playerData) {
- //if (audioDict.Count > (5 + Convert.ToInt32(isTrue)) * Convert.ToInt32(YES))
- //{
- // Debug.Log("check");
- //}
- HouseSound(_playerData.GetHugIntensity());
- BookSound(_playerData.GetHugIntensity());
- BassSound(_playerData.GetFearLevel(), _playerData.GetFearLevelMax());
- ChangeThemeMusic(_playerData.GetTransitionTimer());
- HeartBeat(_playerData.GetFearLevel());
- //HugSoundTest(_playerData.GetTransitionTimer());
- }
- /// <summary>
- /// Main function of the sound manager.
- /// </summary>
- /// <param name="_instanceID">Send in the instanceID of the audio source</param>
- /// <param name="_audio">Send in the audio source</param>
- /// <param name="_playOnce">if it should only be played once set this to true</param>
- /// <param name="_pitchAmount">If you would like to pitch the sound. Default is 1.</param>
- /// <param name="_volume">Set the volume of the audio source. Defualt is 1.</param>
- public void PlaySound(int _instanceID, AudioSource _audio, bool _playOnce, float _pitchAmount, float _volume)
- {
- audioDict.Add(_instanceID, _audio);
- if (!_audio.isPlaying && !audioDictPlayed.ContainsKey(_instanceID))
- {
- _audio.volume = _volume;
- _audio.pitch = _pitchAmount;
- _audio.Play();
- }
- if (_playOnce && !audioDictPlayed.ContainsKey(_instanceID))
- audioDictPlayed.Add(_instanceID, _audio);
- DeleteSound(_instanceID, _audio);
- }
- public void FootSteps(float _volume)
- {
- float randomPitch = Random.Range(0.8f, 1.2f);
- if (Physics.Raycast(playerTransform.position, Vector3.down, out footStepRay, 1f))
- {
- Debug.DrawLine(playerTransform.position, footStepRay.point, Color.red);
- if (footStepRay.collider.material == grassMat)
- {
- Debug.Log("Im hitting the grass!");
- int randFootClip = Random.Range(0, 3);
- if (randFootClip == 0)
- footAudio.clip = grass1;
- else if (randFootClip == 1)
- footAudio.clip = grass2;
- else if (randFootClip == 2)
- footAudio.clip = grass3;
- }
- else if(footStepRay.collider.material == grassMat)
- {
- //Check for other material
- //add other footstep sound
- }
- }
- footAudio.volume = _volume;
- footAudio.pitch = randomPitch;
- footAudio.Play();
- }
- void DeleteSound(int _instanceID, AudioSource _audio)
- {
- audioDict.Remove(_instanceID);
- }
- //Make beautiful this looks shit!
- void HouseSound(float _hugIntensity)
- {
- if (_hugIntensity > 0)
- {
- if (houseVolume > 0)
- houseVolume -= Time.deltaTime * 0.2f;
- houseWhisper.GetComponent<AudioSource>().volume = houseVolume;
- }
- else
- {
- if (houseVolume < 0.5f)
- {
- houseVolume += Time.deltaTime * 0.2f;
- houseWhisper.GetComponent<AudioSource>().volume = houseVolume;
- }
- }
- }
- void HeartBeat(float _fearLevel)
- {
- if (_fearLevel > 20)
- {
- if (!heartBeatSource.isPlaying)
- {
- heartBeatSource.Play();
- }
- heartBeatSource.volume = _fearLevel * 0.1f;
- }
- else
- {
- heartBeatSource.volume -= Time.deltaTime;
- Mathf.Clamp(heartBeatSource.volume, 0, 0.3f);
- }
- }
- void BookSound(float _transitionTimer)
- {
- if (_transitionTimer > 0)
- {
- if (bookVolume > 0)
- bookVolume -= Time.deltaTime * 0.2f;
- bookWhisper.GetComponent<AudioSource>().volume = bookVolume;
- }
- else
- {
- if (bookVolume < 0.5f)
- {
- bookVolume += Time.deltaTime * 0.2f;
- bookWhisper.GetComponent<AudioSource>().volume = bookVolume;
- }
- }
- }
- void BassSound(float _fearLevel, float _maxFearLevel)
- {
- if (!bassAudio.isPlaying)
- bassAudio.Play();
- bassPercentage = (_fearLevel / _maxFearLevel) / 2;
- bassAudio.volume = Mathf.Lerp(0.05f, 1f, bassPercentage);
- }
- public void HugSound()
- {
- if (!hugSource.isPlaying)
- {
- int randSound = Random.Range(0, 3);
- if (randSound == 0)
- hugSource.clip = hugClip1;
- else if (randSound == 1)
- hugSource.clip = hugClip1;
- else if (randSound == 2)
- hugSource.clip = hugClip1;
- hugSource.Play();
- }
- playHugSound = true;
- }
- void MainMusic(AudioSource _aSource)
- {
- if (currentThemeSource != _aSource || onField)
- {
- if (currentThemeSource == null)
- currentThemeSource = _aSource;
- currentThemeSource.volume = helpFunctions.FadeOutSound(currentThemeSource.volume, 0);
- if (currentThemeSource.volume == 0)
- {
- changeSoundCheckIsTrue = true;
- currentThemeSource = _aSource;
- if (onField)
- {
- nightmareSource.clip = fieldTitleClip;
- onField = false;
- }
- }
- }
- if (changeSoundCheckIsTrue)
- {
- float maxVolume = 0.5f;
- currentThemeSource.volume = helpFunctions.FadeInSound(currentThemeSource.volume, maxVolume);
- if (currentThemeSource.volume == maxVolume)
- {
- changeSoundCheckIsTrue = false;
- }
- }
- }
- void ChangeThemeMusic(float _transitionTimer)
- {
- if (!titleSource.isPlaying)
- titleSource.Play();
- if (!nightmareSource.isPlaying)
- nightmareSource.Play();
- if (_transitionTimer > 0 || playTitleMusic)
- MainMusic(titleSource);
- else
- MainMusic(nightmareSource);
- }
- //void HugSoundTest(float _transTimer)
- //{
- // if (!hugSource.isPlaying)
- // {
- // hugSource.clip = hugClip1;
- // hugSource.Play();
- // hugSource.volume = _transTimer * 0.1f;
- // }
- // Mathf.Clamp(hugSource.volume,0,0.3f);
- //}
- public void SetOnField(bool _onField) { onField = _onField; }
- public void SetTitleMusic(bool _playTitleMusic) { playTitleMusic = _playTitleMusic; }
- void LoadSounds()
- {
- if (!(hugClip1 = (AudioClip)Resources.Load("Sounds/Sounds/leon_hug_cloth1")))
- Debug.Log("Laddar inte kramljud1!");
- if (!(hugClip2 = (AudioClip)Resources.Load("Sounds/Sounds/leon_hug_cloth2")))
- Debug.Log("Laddar inte kramljud2!");
- if (!(hugClip3 = (AudioClip)Resources.Load("Sounds/Sounds/leon_hug_cloth3")))
- Debug.Log("Laddar inte kramljud3!");
- if (!(hugPurr.clip = (AudioClip)Resources.Load("Sounds/Sounds/leon_purr")))
- Debug.Log("Laddar inte purrljud!");
- if (!(bassAudio.clip = (AudioClip)Resources.Load("Sounds/Basljud")))
- Debug.Log("Laddar inte basljud!");
- if (!(standardFootClip = (AudioClip)Resources.Load("Sounds/fot4")))
- Debug.Log("Laddar inte fotljud!");
- if (!(grass1 = (AudioClip)Resources.Load("Sounds/Sounds/grass1")))
- Debug.Log("Laddar inte fotljud!");
- if (!(grass2 = (AudioClip)Resources.Load("Sounds/Sounds/grass2")))
- Debug.Log("Laddar inte fotljud!");
- if (!(grass3 = (AudioClip)Resources.Load("Sounds/Sounds/grass3")))
- Debug.Log("Laddar inte fotljud!");
- if (!(grass1 = (AudioClip)Resources.Load("Sounds/Sounds/grass1")))
- Debug.Log("Laddar inte fotljud!");
- if (!(sludge1 = (AudioClip)Resources.Load("Sounds/Sounds/sludge1")))
- Debug.Log("Laddar inte fotljud!");
- if (!(sludge2 = (AudioClip)Resources.Load("Sounds/Sounds/sludge2")))
- Debug.Log("Laddar inte fotljud!");
- if (!(sludge3 = (AudioClip)Resources.Load("Sounds/Sounds/sludge3")))
- Debug.Log("Laddar inte fotljud!");
- if (!(sludge4 = (AudioClip)Resources.Load("Sounds/Sounds/sludge4")))
- Debug.Log("Laddar inte fotljud!");
- if (!(mainTitleClip = (AudioClip)Resources.Load("Sounds/somniumHUB")))
- Debug.Log("Laddar inte nightmareljud");
- if (!(fieldTitleClip = (AudioClip)Resources.Load("Sounds/SomniumFIELD")))
- Debug.Log("Laddar somniumFIELD");
- if (!(titleSource.clip = (AudioClip)Resources.Load("Sounds/TitelSomnFade")))
- Debug.Log("Laddar inte nightmareljud");
- if (!(heartBeatSource.clip = (AudioClip)Resources.Load("Sounds/heartbeat")))
- Debug.Log("Laddar inte heartbeat");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment