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

public class MyCoroutine : MonoBehaviour
{
    protected Action Act_Repeat_for1sec;
    float t_for1sec = 0f;
    Coroutine m_Co;

    protected virtual void Update()
    {
        if (Act_Repeat_for1sec != null)
        {
            t_for1sec -= Time.deltaTime;
            if (t_for1sec <= 0f)
            {
                t_for1sec = 1f;
                Act_Repeat_for1sec();
            }
        }
    }

    protected void Set_Coroutine(Action _act, float _time)
    {
        if (gameObject.activeInHierarchy)
            StartCoroutine(Run_Coroutine(_act, _time));
    }

    IEnumerator Run_Coroutine(Action _act, float _time)
    {
        yield return new WaitForSeconds(_time);
        _act?.Invoke();
    }

    protected void Cancel_CurCoroutine() { if (m_Co != null) StopCoroutine(m_Co); }
    protected void Set_Coroutine_withCancel(Action _act, float _time)
    {
        if (gameObject.activeInHierarchy)
        {
            Cancel_CurCoroutine();
            m_Co = StartCoroutine(Run_Coroutine(_act, _time));
        }
    }
}