using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class LoadLevel : MonoBehaviour {

    public GameObject fader1;
    public GameObject fader2;
    public bool spacePushed = false;
    private SpriteRenderer fader1_mat;
    private SpriteRenderer fader2_mat;
    public float newColor =0;

    void Start()
    {
        newColor = 0;
        fader1.GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, newColor);
        fader1.GetComponent<SpriteRenderer>();
        fader2.GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, newColor);
        fader2.GetComponent<SpriteRenderer>();
    }
	void Update () {

        if(Input.GetKeyDown(KeyCode.Space))
        {
            spacePushed = true;
            StartCoroutine(FadeOut(6.5f));
        }


        if(spacePushed)
        {
            newColor += 0.5f * Time.deltaTime;
            fader1.GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, newColor/1.5f);

            fader2.GetComponent<SpriteRenderer>().color = new Color(0, 0, 0, (newColor/1.5f) - 1.0f);

            GetComponent<AudioSource>().volume -= Time.deltaTime / 5;
        }
	}

    IEnumerator FadeOut(float time)
    {
        yield return new WaitForSeconds(time);

        SceneManager.LoadScene("farm");
    }
}
