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

public class DarkControl : MonoBehaviour
{
    public float Temp1;
    public float TempDarken;
    public int DarkenTimes;
    public float Temp2;

    private Transform dark;
    private bool inGame;
    // Use this for initialization
    void Start ()
    {
        dark = transform;
        inGame = false;
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
        dark.position = new Vector2(0, dark.position.y);
    }
    public bool IsInGame()
    {
        return inGame;
    }
    public void StartGame()
    {
        StartCoroutine(GameStart());
    }
    public void EndGame()
    {
        StartCoroutine(GameOver());
    }
    IEnumerator GameStart()
    {
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
        yield return new WaitForSeconds(Temp1);
        for (float i = 0; i < TempDarken * DarkenTimes; i += TempDarken)
        {
            dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1-(i / (float)(TempDarken * DarkenTimes)));
            yield return new WaitForSeconds(TempDarken);
        }
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
        inGame = true;
    }
    IEnumerator GameOver()
    {
        inGame = false;
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0);
        yield return new WaitForSeconds(Temp2);
        for (float i = 0; i < TempDarken * DarkenTimes; i += TempDarken)
        {
            dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, i / (float)(TempDarken * DarkenTimes));
            yield return new WaitForSeconds(TempDarken);
        }
        dark.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
        SceneManager.LoadSceneAsync("score");
        //Application.LoadLevel("score");
    }
    // Update is called once per frame
    void Update () {
	
	}
}
