using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class Ennemie : MonoBehaviour, IEventGameOver
{
    private float m_currentAlpha = 1f;
    private bool m_isDead;
    protected SpriteRenderer m_spriteRenderer;

    public bool IsDead { get { return m_isDead;} }

    protected virtual void Start()
    {
        ExecuteEvents.AddHandler<IEventGameOver>(this);

        m_spriteRenderer = GetComponent<SpriteRenderer>();
    }

    protected virtual void OnDestroy()
    {
        ExecuteEvents.RemoveHandler<IEventGameOver>(this);
    }

    protected virtual void Update()
    {
        if (m_isDead)
        {
            m_currentAlpha -= Time.deltaTime;

            Color color = m_spriteRenderer.color;
            color.a = m_currentAlpha;
            m_spriteRenderer.color = color;

            if (m_currentAlpha <= 0f)
            {
                Destroy(gameObject);
            }
        }        
    }

    public virtual void OnGameOver()
    {        
        m_isDead = true;
    }
}
