using UnityEngine;
using System.Collections;

public class MoveForward : MonoBehaviour {

	public float Speed;
	public float InheritFactor = 0.6f;

	private Transform player;
	private GameController gc;
	
	// Use this for initialization
	void Start () {
		GameObject go = GameObject.Find ("GameController");
		gc = go.GetComponent<GameController>();
		
		GameObject pgo = GameObject.Find("Player");
		if(pgo!=null)
			player = pgo.transform;
	}
	
	// Update is called once per frame
	void FixedUpdate () {
		if(player == null)
		{
			GameObject pgo = GameObject.Find("Player");
			if(pgo!=null)
				player = pgo.transform;
		}


		//Vector2 relative = rigidbody2D.velocity - new Vector2(0, player.rigidbody2D.velocity.y);

		//Vector2 inherit = (player == null) ? new Vector2 (0,0) : player.rigidbody2D.velocity * InheritFactor; 
		Vector2 target = (rigidbody2D.velocity.normalized * Speed);
		Vector2 delta = target - rigidbody2D.velocity;
		Vector2 force = rigidbody2D.mass * delta / Time.fixedDeltaTime;
		rigidbody2D.AddForce(force);
	}

	void SCRAP_FixedUpdate () {
		if(player == null)
		{
			GameObject pgo = GameObject.Find("Player");
			if(pgo!=null)
				player = pgo.transform;
		}

		Vector2 dir = PosFromAngle(transform.eulerAngles.z).normalized;
		Vector2 target = dir * Speed;// + new Vector2(0, player.rigidbody2D.velocity.y * InheritFactor);

		Vector2 delta = target - rigidbody2D.velocity;
		Vector2 force = rigidbody2D.mass * delta / Time.fixedDeltaTime;
		rigidbody2D.AddForce(force);
	}

	private Vector2 PosFromAngle(float rot)
	{
		float rads = rot * Mathf.PI/180;
		Vector2 pos = new Vector3(Mathf.Cos(rads), Mathf.Sin(rads));
		return pos;
	}
}
