using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {
	//Public
	[Header("Obiekty przypięte")]
	public Transform	forceDirection	= null;
	public Transform	sideDirection	= null;

	[Header("Dane")]
	public float		moveForce		= 1f;

	//Private
	private bool		instantGo		= false;
	private float		instantTimer	= 0f;
	private	float		lastInstant		= 0f;
	private	float		boostTimer		= 0f;
	private float		pulseTimer		= 0f;

	void Start() {
	
	}
	
	
	void FixedUpdate() {
		Vector3		tempDirection	= forceDirection.position - transform.position;
		tempDirection				= tempDirection.normalized;
		Vector3		tempDirection2	= sideDirection.position - transform.position;
		tempDirection2				= tempDirection2.normalized;
		
		if (instantGo == false) {
			rigidbody.AddForce(
				tempDirection
				*
				moveForce
				*
				Input.GetAxis("Vertical")
			);
			rigidbody.AddForce(
				tempDirection2
				*
				moveForce
				*
				Input.GetAxis("Horizontal")
			);
		} else {
			instantTimer	+= Time.deltaTime;
			if (instantTimer > 0.1f) {
				rigidbody.AddForce(
					tempDirection
					*
					moveForce
					*
					5f
				);
				instantTimer	= 0f;
			}
		}
		
		boostTimer	+= Time.deltaTime;
		if (boostTimer > 1f) {
			if (Input.GetAxisRaw("Boost") == 1f) {
				rigidbody.AddForce(
					tempDirection
					*
					moveForce
					*
					10f
				);
			}
			boostTimer	= 0f;
		}
		
		lastInstant += Time.deltaTime;
		if (Input.GetAxisRaw("InstantGo") == 1f && lastInstant > 0.5f) {
			instantGo	= !instantGo;
			lastInstant	= 0f;
		}
		
		if (pulseTimer > 0f) {
			transform.localScale	= new Vector3(
				3f + Mathf.Sin(pulseTimer * 3f) * 0.1f,
				3f + Mathf.Sin(pulseTimer * 3.5f) * 0.1f,
				3f + Mathf.Cos(pulseTimer * 3f) * 0.1f
			);
			pulseTimer	-= Time.deltaTime;
		}
	}
	
	void LateUpdate() {
		if (Vector3.Distance(transform.position, Vector3.zero) > 200f) {
			Vector3		tempDirection	= -transform.position;
			tempDirection				= tempDirection.normalized;
			rigidbody.AddForce(
				tempDirection
				*
				moveForce
				*
				10f
			);
		}
	}
	
	public void pulse() {
		pulseTimer	=	2f * Mathf.PI;
	}
}
