using UnityEngine;
using System.Collections;

public class PlayerControler : MonoBehaviour {
	
	void Start () {	
	}

	bool pushed = false;
	public float speed;
	public Vector2 jumpVector;
	public GameObject bullet;
	bool canJump = true;
	bool facingLeft = true;
	float currentSpeed;
	float pushtime;

	void Update() {
		if (!pushed) {
			Vector2 velo = rigidbody2D.velocity;
			velo.x = (Input.GetAxis ("Horizontal_P1") * speed);
			rigidbody2D.velocity = velo;
		} else {
			Vector2 velo = rigidbody2D.velocity;
			if (velo.x < 0.4f && Time.time > pushtime)
				pushed = false;
		}

		if (Input.GetKey (KeyCode.UpArrow)){
			if (canJump == true){
				rigidbody2D.velocity = jumpVector;
				canJump = false;
			}
		}

		if (Input.GetKeyDown ("RightArrow") && facingLeft) {
			transform.Rotate (Vector3.up * 180);
			facingLeft = false;
		}
		
		if (Input.GetKeyDown ("LeftArrow") && !facingLeft) {
			transform.Rotate (Vector3.up * 180);
			facingLeft = true;
		}

		transform.eulerAngles = new Vector3 (transform.eulerAngles.x, transform.eulerAngles.y,0);

}

	void OnCollisionEnter2D(Collision2D col){
		if (col.collider.tag == "ground")
			canJump = true;
		else if(col.collider.tag == "Player")
			canJump = true;
}

	public void Pushed(){
		pushed = true;
		pushtime = Time.time + 0.5f;
	}

}