using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

public class SelfDestroyTimer : MonoBehaviour
{
    private float delayTime = 5;
    private float minRotateTime = 0.5f;
    private float rotateTime = 0.5f;
    private bool turnWay = false;

    // Start is called before the first frame update
    void Start()
    {
        Destroy(gameObject, Random.Range(8, 8));
    }

    // Update is called once per frame
    void Update()
    {
        delayTime -= Time.deltaTime;
        rotateTime -= Time.deltaTime;
        if (delayTime <= 0) {
            if (turnWay)
            {
                gameObject.transform.Rotate(0, 0, -0.2f, Space.Self);

            }
            if (!turnWay)
            {
                gameObject.transform.Rotate(0, 0, 0.2f, Space.Self);

            }
            if (rotateTime <= 0)
            {
                turnWay = !turnWay;
                rotateTime = minRotateTime;
            }
        }
    }


    private void OnDestroy()
    {
        Collider2D collider = Physics2D.OverlapCircle(transform.position, 3f);
        if (collider != null)
        {
            if(collider.tag == "Player")
            {
                Destroy(collider.gameObject);
            }
            
            Debug.Log(collider.name);

        }
    }
}
