using UnityEngine.Audio;
using UnityEngine;

[System.Serializable]
public class Sound
{
	public string name;

	public AudioClip clip;

	public AudioMixerGroup mixerBus;

	[Range(0.0f,1.0f)]
	public float volume = 1.0f;
	[Range(0.1f,2.0f)]
	public float pitch = 1.0f;

	//[Range(0.0f,0.5f)]
	//public float randomPitch = 0.1f;

	[HideInInspector]
	public AudioSource source;

	public bool loop;

	public bool fadeOut;
	public float fadeSpeed;

	//[HideInInspector]
	private bool _isPlaying;
	public bool isPlaying
	{
		get
		{
			return _isPlaying;
		}
		set
		{
			if (_isPlaying != value)
			{
				_isPlaying = value;

				if (_isPlaying == true && source.isPlaying == false)
				{
					source.Play();
				}
			}
		}
	}

	public void Tick(float deltaTime)
	{
		if (!isPlaying)
		{
			source.volume -= AudioManager.instance.audioFallOffSpeed * deltaTime;

			if (source.volume <= 0.0f)
			{
				source.Stop();
			}
		}
		else
		{
			source.volume = volume;
            if (!source.isPlaying)
            {
                isPlaying = false;
            }
		}
	}
}
