using UnityEngine;
using System.Collections;

public class Spear : MonoBehaviour
{
	
	
		public AnimationCurve curve;
		float curveTime = 0;
		bool init = false;
		Vector3 targetPos;
		Vector3 prevPos;
		float deltaX;
		float deltaY;
		
		float spearSpeed = 0.4f;
		float shootDuration;
		
		public void Init (Vector3 _targetPos)
		{
				init = true;
				this.targetPos = _targetPos;
				targetPos.x += 0.5f;
				targetPos.y += 0.5f;
				deltaX = targetPos.x - this.transform.position.x;
				deltaY = targetPos.y - this.transform.position.y;
				//Vector3.Distance (targetPos, transform.position) * spearSpeed;
				spearSpeed /= Mathf.Abs (Vector3.Distance (targetPos, transform.position));
				this.transform.LookAt (targetPos);
		}
		
		void OnTriggerEnter2D (Collider2D other)
		{
				if (other.tag == "MainCharacter") {
						Destroy (this.gameObject);
						MainCharacter.inst.GetHit ();
				}
		}
		// Update is called once per frame
		void Update ()
		{
				if (!init) {
						return;
				}
				
				Vector3 myPos = this.transform.position;
				curveTime += spearSpeed; 
				float yPos = deltaY * spearSpeed; 
				float xPos = deltaX * spearSpeed;
				
				myPos.x += xPos;
				myPos.y += yPos;
				
				this.transform.position = myPos;
				
				Vector3 angle = this.transform.eulerAngles; 
				angle.x = (Vector3.Angle (prevPos, myPos));
				this.transform.eulerAngles = angle;
				prevPos = myPos;
		}
}
