using UnityEngine;

using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour {
	
	public Rigidbody2D rigidBody;
	public float moveSpeed, jumpSpeed, gravity; 
	public Transform groundCheck;
	protected int facedir; //1=right
 

	// Use this for initialization
	void Start () { 
	}

	void Update(){
		if (Input.GetKeyDown(KeyCode.UpArrow)){
			Jump(jumpSpeed);
		}  
	}

	// Update is called once per frame
	void FixedUpdate () {   
		rigidBody.velocity = (new Vector2(rigidBody.velocity.x,rigidBody.velocity.y - gravity));
		
		if (Input.GetKey(KeyCode.RightArrow)){
			SetHorizontalVelocity(moveSpeed);
			//rigidBody.AddForce(new Vector2(movespeed,rigidBody.velocity.y));
		} 
		
		if (Input.GetKey(KeyCode.LeftArrow)){
			SetHorizontalVelocity(-moveSpeed);
			//rigidBody.AddForce(new Vector2(-movespeed,rigidBody.velocity.y));
		} 

		facedir = rigidBody.velocity.x>0?1:-1;
	}  
 

	public bool CheckGrounded(){
		return Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
	}

	public virtual void Jump(float strength){
		if (CheckGrounded()){
			SetVerticalVelocity(strength); 
		}
	} 

	public void SetHorizontalVelocity(float amount){
		rigidBody.velocity = (new Vector2(amount,rigidBody.velocity.y));
	}

	public void SetVerticalVelocity(float amount){
		rigidBody.velocity = (new Vector2(rigidBody.velocity.x,amount));
	}
 	
	public int GetFaceDir(){
		return facedir;
	}

	public virtual void Kill(){
	}
}
