using UnityEngine;
using System.Collections;

public class Player_Movement : MonoBehaviour {
	Rigidbody2D r_body;
	//Animator anim;
	public float jump_force;
	public float move_force;
	public float max_speed;
	public Animator anim;
	bool ground;

	// Use this for initialization
	void Start () {
		r_body = GetComponent<Rigidbody2D>();
		//anim = GetComponent<Animator>();

	}
	
	// Update is called once per frame
	void Update () {
		float h = Input.GetAxis("Horizontal");
		anim.SetFloat("Speed", Mathf.Abs (r_body.velocity.x));
		r_body.AddForce(new Vector2(move_force * h, 0));
		ground = whatIsGround.Grounded;

		if(r_body.velocity.x > max_speed)
		{
			r_body.velocity = new Vector2(max_speed, r_body.velocity.y);
		}
		else if(r_body.velocity.x < max_speed * -1)
		{
			r_body.velocity = new Vector2(max_speed * -1, r_body.velocity.y);
		}
		if(Input.GetButtonDown("Jump") && ground == true)
		{
			r_body.AddForce(new Vector2(0, jump_force));
			anim.SetBool("Jump", true);
			ground = false;
		}
		else
		{
			anim.SetBool ("Jump", false);
		}


	}
	void OnCollisionEnter2D(Collider2D col){
		if (col.gameObject.tag == "surface") {
			ground = true;
		} 
		else {
			ground = false;
		}
	}


}
