using UnityEngine;
using System.Collections;

public class Heros : MonoBehaviour {

    Vector2 acceleration = Vector2.zero;
    float previousJump = 0;
    private bool jump = false;

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) Jump();
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "AllowedJump") jump = true;
        Bloc blocScript = collision.gameObject.GetComponent<Bloc>();
        if (blocScript != null)
        {
            blocScript.DoAction();
        }
    }

    public void Jump()
    {
        Ray ray = new Ray(transform.position, Vector3.down - transform.position);
        RaycastHit hitinfo;

        Debug.Log("Jump");
        if (Physics.Raycast(ray, out hitinfo, 0.7f))
        {
            if (hitinfo.collider.tag == "AllowedJump" || jump)
            {
                //previousJump = Time.time;
                Game.instance.PlaySound(7);
                transform.rigidbody.AddForce(25.0f * Vector3.up * Time.deltaTime, ForceMode.Impulse);
            }
            else Debug.Log("ReFail");
        }
        else Debug.Log("Fail");
    }

    void OnCollisionExit(Collision col)
    {
        jump = false;
    }

    public void PushLeft()
    {
        
            transform.rigidbody.AddForce(1f * Vector3.left, ForceMode.Impulse);
        
    }

    public void PushRight()
    {
        
            transform.rigidbody.AddForce(1f * Vector3.right, ForceMode.Impulse);
    }


    void FixedUpdate()
    {
        #if UNITY_EDITOR
		if (Input.GetKey(KeyCode.LeftArrow))
		{
			acceleration.x -= 0.5f;
		}
		if (Input.GetKey(KeyCode.RightArrow))
		{
			acceleration.x += 0.5f;
		}
		acceleration *= 0.8f;
        #else
        acceleration = Input.acceleration * 1.5f;
        #endif

        if (rigidbody.velocity.magnitude > 30.0f)
        {
            rigidbody.velocity = 30.0f * rigidbody.velocity.normalized;
        }
		
        transform.rigidbody.AddForce(30.0f * acceleration * Time.deltaTime, ForceMode.Force);
	}
   
    
}
