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

[RequireComponent(typeof(Rigidbody))]
public class BouncyBullet : MonoBehaviour 
{
	public float bulletSpeed = 150;
	public float modifier = 100;
	private Rigidbody rBody;

	private Transform player;

	// Use this for initialization
	void Start () 
	{
		rBody = GetComponent<Rigidbody> ();
		player = GameObject.FindGameObjectWithTag (Tags.Player).transform;
		rBody.AddForce (transform.TransformDirection (player.forward) * (bulletSpeed + (Mathf.Abs (player.GetComponent<PlayerMovement> ().horizontalValue) * modifier)));
		this.tt ().Add (3, () => {
			gameObject.SetActive (false);
		});
	}

	void OnCollisionEnter (Collision other)
	{
		if(other.gameObject.layer == LayerMask.NameToLayer(Layers.Enemy))
		{
			gameObject.SetActive (false);
		}
		if(other.gameObject.layer == LayerMask.NameToLayer(Layers.Bullet) && !other.gameObject.name.Contains("Bouncy"))
		{
			other.gameObject.SetActive (false);
			gameObject.SetActive (false);
		}
	}
}

