using FOHEART.GlovePlugin;
using UnityEngine;

public class BulletMoveForwardLogic : MonoBehaviour
{
    [Tooltip("˶ٶ m/s")]
    public float speed = 10f;
    [Tooltip("Զʱ s")]
    public float liveTime = 3f;

    private bool isItemActive = false;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        // Ѽ
        if (isItemActive)
        {
            // ǰ˶
            this.transform.Translate(0f, 0f, Time.deltaTime * speed, Space.Self);
        }
    }

    /// <summary>
    /// Զǰ˶
    /// </summary>
    public void activeItem()
    {
        // 弤ʼǰ˶
        isItemActive = true;
        // Զ
        Invoke(nameof(SelfDestroy), liveTime);
    }

    /// <summary>
    /// 
    /// </summary>
    private void SelfDestroy()
    {
        Destroy(this.gameObject);
    }

    private void OnCollisionEnter(Collision collision)
    {
        destory(collision.gameObject);
    }

    private void OnTriggerEnter(Collider other)
    {
        destory(other.gameObject);
    }

    private void destory(GameObject other)
    {
        CancelInvoke(nameof(SelfDestroy));

        // ȡײǷӵٱ־
        int length = other.GetComponentsInParent(typeof(BulletDestructableFlag)).Length;
        if (length > 0)
        {
            // ־
            Destroy(other.gameObject);
        }

        // ײ壬
        Destroy(this.gameObject);
    }

}
