using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestB2 : MonoBehaviour
{
	[SerializeField] private TestBomb m_Bomb;
	[SerializeField] private float m_Speed;
	private Vector3 m_From;
	private Vector3 m_To;
	private Coroutine m_CoBomb;
	Vector3 direction
	{
		get
		{
			return (m_To - m_From).normalized;
		}
	}
	/// 
	/// from to ÔÚ3d¿Õ¼ä
	/// 
	/// 
	/// 
	public void Set(Vector3 from, Vector3 to, float speed, float lifeTime)
	{
		m_From = from;
		m_To = to;
		m_Speed = speed;
		transform.position = from;
		this.gameObject.SetActive(true);
		m_CoBomb = StartCoroutine(coBomb(0.1f));
		Invoke("DestroySelf", lifeTime);
	}
	void DestroySelf()
	{
		if (m_CoBomb != null)
		{
			StopCoroutine(m_CoBomb);
			m_CoBomb = null;
		}
		this.gameObject.SetActive(false);
		Destroy(this.gameObject);
	}
	private void Update()
	{
		Vector3 pos = transform.position;
		pos += direction * m_Speed * Time.deltaTime;
		transform.position = pos;
	}
	IEnumerator coBomb(float interval)
	{
		while (true)
		{
			Vector3 pos = transform.position;
			for(int i = 0; i < 1; ++i)
			{
				TestBomb grenade = Instantiate(m_Bomb);
				Vector3 position = new Vector3(pos.x + Random.Range(-3, 3), pos.y + Random.Range(-5f, 5f), 7f);
				grenade.Set(position, new Vector3(0, 0, -1f), Random.Range(8f, 10f));
				grenade.gameObject.SetActive(true);
			}
			yield return new WaitForSeconds(interval);
		}
	}
}