using System.Collections;
using UnityEngine;
using matnesis.TeaTime;

public class FloweyBullets : MonoBehaviour 
{
	public float speedH = 4;
	public float speedV = 10;

	public Transform player;
	private Vector3 offset;
	private Vector3 currDirection;

	void Start () 
	{
		this.tt ("DestroyBullets").Add (5, () => 
			{
				gameObject.SetActive(false);
			});

		player = GameObject.FindGameObjectWithTag (Tags.Player).transform.FindChild("HitZone");
		offset = player.position - transform.position;
		currDirection = player.position;
		this.tt("SlowBulletFollow").Loop(0.5f, (ttHandler t) => 
			{
				Vector3 bulletDown = transform.position;
				bulletDown.y = player.position.y;
				transform.position = Vector3.Lerp(transform.position, bulletDown, t.t);
			}).Immutable();
		
	}
	
	// Update is called once per frame
	void Update () 
	{

		/*if(Vector3.Distance(transform.position, player.position) < 15) 
		{
			this.tt("SlowBulletFollow").Loop(1f, (ttHandler t) => 
				{
					currDirection = Vector3.Lerp(currDirection, player.position, t.deltaTime);
				}).Immutable();
			
			Vector3 newPos = transform.position;
			newPos.y = currDirection.y;
			transform.position = newPos;
		}*/

		transform.Translate (Vector3.forward * speedH * Time.deltaTime);
	}

	void OnCollisionEnter (Collision other)
	{
		if(other.gameObject.layer == LayerMask.NameToLayer(Layers.Player))
		{
			gameObject.SetActive (false);
			player.GetComponentInParent<PlayerActor> ().TakePlayerLife ();
		}
	}
}
