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

public class Damage : MonoBehaviour
{
    public AudioClip _audio;
    bool spriteEnable=true;
    // Start is called before the first frame update
    IEnumerator Start()
    {

        //StartCoroutine(FlashTimer());
        InvokeRepeating("FlashFrog", 0.5f, 0.5f);
        yield return new WaitForSeconds(3f);
        CancelInvoke();
        spriteEnable = true;
        transform.GetChild(0).GetComponent<SpriteRenderer>().enabled = spriteEnable;
        gameObject.GetComponent<BoxCollider2D>().enabled = true;
    }
    /*
    IEnumerator Timer()
    {
        Debug.Log("timer :)");
        yield return null;
    }*/


    void FlashFrog()
    {
        spriteEnable = !spriteEnable;
        transform.GetChild(0).GetComponent<SpriteRenderer>().enabled = spriteEnable;
    }
    /*IEnumerator FlashTimer()
    {
        float elapsedTime = 0f;
        while(!gameObject.GetComponent<BoxCollider2D>().enabled)
        {
            elapsedTime = Time.deltaTime;
            if (elapsedTime > 0.5f)
            {
                
                break;
            }
        }
        
        yield return null;
    }*/
    

    // Update is called once per frame
    void Update()
    {
        /*
        float elapsedTime = 0f;
        while (!gameObject.GetComponent<BoxCollider2D>().enabled)
        {
            if (elapsedTime > 0.5f)
            {
                transform.GetChild(0).GetComponent<SpriteRenderer>().enabled = !transform.GetChild(0).GetComponent<SpriteRenderer>().enabled;
                elapsedTime = 0f;
            }
        }
        elapsedTime += Time.deltaTime;
        */
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        
        if(collision.gameObject.layer == 7)
        {
            AudioSource.PlayClipAtPoint(_audio, gameObject.transform.position);
            Destroy(gameObject);
        }
    }

}
