using UnityEngine;
using System.Collections;

public class RinnamonController : MonoBehaviour {

	//declare variables
	public GameObject Player;
	public float maxSpeed;
	public float maxJumpSpeed;
	public float jumpHeight;
    bool facingRight = true;
    Vector3 gravity = new Vector3(0, -9.81f, 0);
	Vector3 curVel;
	public bool Grounded;
	public Transform groundedEndCenter;
    public LayerMask whatIsGround;
    float groundRadius = 0.2f;


    public void Update(){
		//Vector3 playerPos = new Vector3(transform.position.x, transform.position.y, 0f);
		//Vector3 groundLeftPos = new Vector3(groundedEndLeft.position.x, groundedEndLeft.position.y, 0f);
		//Vector3 groundRightPos = new Vector3(groundedEndRight.position.x, groundedEndRight.position.y, 0f);
		//Vector3 groundCenterPos = new Vector3(groundedEndCenter.position.x, groundedEndCenter.position.y, 0f);

		if (Grounded = Physics2D.OverlapCircle(groundedEndCenter.position, groundRadius, whatIsGround) == true) {
			Grounded = true;
		}else {
			Grounded = false;
		}

		Debug.Log (Grounded);

		//Move character left/right instantly
		if (Input.GetAxisRaw ("Horizontal") != 0) {
			curVel = new Vector3(Input.GetAxisRaw ("Horizontal") * maxSpeed, Player.GetComponent<Rigidbody2D>().velocity.y, 0);
			Player.GetComponent<Rigidbody2D> ().velocity = curVel;
		}
		//otherwise set velocity to 0
		else {
			curVel = new Vector3(0, Player.GetComponent<Rigidbody2D>().velocity.y, 0);
			Player.GetComponent<Rigidbody2D>().velocity = curVel;
		}
		
		//Jump if able
		if (Input.GetButton("Jump") && Grounded) {
			Grounded = false;
			curVel = new Vector3(Player.GetComponent<Rigidbody2D>().velocity.x, jumpHeight, 0);
			Player.GetComponent<Rigidbody2D>().velocity = curVel;
		}
		
		//if moving vertically, apply gravity
		if (Player.GetComponent<Rigidbody2D> ().velocity.y != 0) {
			curVel += (gravity * Time.deltaTime * 2);
			Player.GetComponent<Rigidbody2D>().velocity = curVel;
		}
	}

	public void FixedUpdate()
	{
		//Move character left/right instantly
		if (Input.GetAxisRaw ("Horizontal") != 0) {
			curVel = new Vector3(Input.GetAxisRaw ("Horizontal") * maxSpeed, Player.GetComponent<Rigidbody2D>().velocity.y, 0);
			Player.GetComponent<Rigidbody2D> ().velocity = curVel;
		}
		//otherwise set velocity to 0
		else {
			curVel = new Vector3(0, Player.GetComponent<Rigidbody2D>().velocity.y, 0);
			Player.GetComponent<Rigidbody2D>().velocity = curVel;
		}

		//Jump if able
		if (Input.GetKeyDown(KeyCode.W) && Grounded) {
			Grounded = false;
			curVel = new Vector3(Player.GetComponent<Rigidbody2D>().velocity.x, jumpHeight, 0);
			Player.GetComponent<Rigidbody2D>().velocity = curVel;
		}

		//if moving vertically, apply gravity
		if (Player.GetComponent<Rigidbody2D> ().velocity.y != 0) {
			curVel += (gravity * Time.deltaTime * 2);
			Player.GetComponent<Rigidbody2D>().velocity = curVel;
		}

        if ((Input.GetAxisRaw("Horizontal") > 0 && !facingRight) || (Input.GetAxisRaw("Horizontal") < 0 && facingRight))
        {
            Flip();
        }

    }//end FixedUpdate

    void Flip()
    {
        facingRight = !facingRight;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }

}//end script