using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EndGame : MonoBehaviour
{
    public Fader fade;
    public AudioSource source;
    private float timeToQuit = 1f;

    public void CallEndGame()
    {
        fade.Fade(true, timeToQuit, false);
        StartCoroutine(FadeAudio());
        Invoke("Quit", timeToQuit + 0.1f);
    }

    private IEnumerator FadeAudio()
    {
        float time = 0f;

        while (time < timeToQuit)
        {
            float t = time / timeToQuit;
            source.volume = Mathf.Lerp(1f, 0f, t);
            time += Time.deltaTime;
            yield return null;
        }

        source.Stop();
    }

    private void Quit()
    {
        Application.Quit();
    }
}