using UnityEngine;
using System.Collections;

public class ObjetoColor : MonoBehaviour {

    public WaveColor color;
    public Color colorC;
    public bool positivo = true;
    private SpriteRenderer sr;
    private BoxCollider2D coll;
    public float alfaInactivo = .3f;

    void Awake()
    {
        sr = GetComponent<SpriteRenderer>();
        coll = GetComponent<BoxCollider2D>();

        colorC = ControlColor.GetColor(color);
        if (!positivo)
            colorC = new Color(1f - colorC.r, 1f - colorC.g, 1f - colorC.b);
    }

    public void ActualizarColor()
    {
        activo = (Cromometro.current.currentColor == color) == positivo;
    }

    public bool activo
    {
        set
        {
            sr.color = new Color(colorC.r, colorC.g, colorC.b, value ? 1 : alfaInactivo);
            var previo = coll.enabled;
            coll.enabled = value;
            if (value && !previo)
                StartCoroutine(EsperarYRevisar());
        }
    }

    private IEnumerator EsperarYRevisar()
    {
        yield return null;
        Nivel.current.protagonista.GetComponent<UnityStandardAssets._2D.PlatformerCharacter2D>().CheckSuddenCollision(coll);
    }
}
