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

public class BlackScreen : MonoBehaviour {

    SpriteRenderer blackForeground;

    float fadeInTime = 0.6f;
    float fadeOutTime = 0.6f;

	// Use this for initialization
	void Start () {
        blackForeground = GetComponent<SpriteRenderer>();
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public IEnumerator FadeInAsync() {
        float alpha = blackForeground.color.a;
        for (float t = 0.0f; t < 1.0f; t += Time.unscaledDeltaTime / fadeInTime) {
            Color newColor = new Color(0,0,0, Mathf.Lerp(alpha, 1, t));
            blackForeground.color = newColor;
            yield return null;
        }
    }

    public IEnumerator FadeOutAsync() {
        float alpha = blackForeground.color.a;
        for (float t = 0.0f; t <= 1.0f; t += Time.unscaledDeltaTime / fadeOutTime) {
            Color newColor = new Color(0,0,0, Mathf.Lerp(alpha, 0, t));
            blackForeground.color = newColor;
            yield return null;
        }

        SetCompletelyTransparent();
    }

    public void SetCompletelyTransparent() {
        Color newColor = new Color(0, 0, 0, 0);
        blackForeground.color = newColor;
    }
}
