﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

	public float PlayerSpeed = 4f;
	public float RotationSpeed = 4f;

	private float FrameIndependant;
	private Quaternion PlayerRotation;
	private Rigidbody PlayerRigid;
	private float CamRayLength;

	void Start(){
		PlayerRigid = this.GetComponent<Rigidbody> ();
	}

	void FixedUpdate(){
		float HAxis = Input.GetAxis ("Horizontal");
		float VAxis = Input.GetAxis ("Vertical");

		MovePlayer (HAxis,VAxis);
	}

	void MovePlayer(float InH, float InV){
		if (InV != 0) {//Only allow rotation while driving
			//First Rotate
			FrameIndependant = (InH * RotationSpeed) * Time.deltaTime;
			PlayerRotation = Quaternion.Euler (0, FrameIndependant, 0);
			PlayerRigid.MoveRotation (PlayerRigid.rotation * PlayerRotation);
			//Second Locate
			FrameIndependant = (InV * PlayerSpeed) * Time.deltaTime;

			PlayerRigid.AddForce(this.transform.forward * (1000 *FrameIndependant),ForceMode.Acceleration);
			//PlayerRigid.MovePosition (PlayerRigid.position + (this.transform.forward * FrameIndependant));
		}
	}

	void OnCollisionEnter(Collision TheContact){
		if(TheContact.gameObject.CompareTag("BuildingWall") == true){
		Destroy (TheContact.gameObject);
		}
	}
}
