﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour {

	public float speed = 4;
	private Rigidbody2D rb2d;

	public float thrust = 100;
	public float maxJumpTime = 0.1f;
	public bool jumping;
	private float jumpTimer;

	void Start () {

		rb2d = GetComponent<Rigidbody2D> ();
		
	}
	

	void FixedUpdate () {

		if (Input.GetKey (KeyCode.UpArrow) && jumpTimer <= maxJumpTime) {
			jumping = true;
			rb2d.AddForce (Vector2.up * thrust);
		}

		if (jumping) {
			jumpTimer += Time.deltaTime;
		}

		if (rb2d.velocity.y < -0.1f || jumpTimer >= maxJumpTime) {
			rb2d.gravityScale = 2.5f;
		}

		if (jumpTimer > 2f) {
			ResetJump ();
		}


		float moveHorizontal = Input.GetAxis ("Horizontal");
		rb2d.velocity = new Vector2 (speed * moveHorizontal, rb2d.velocity.y);

	}

	void OnCollisionEnter2D (Collision2D col)
	{
		if (col.collider.gameObject.tag == "Floor") {
			ResetJump ();
		}

		if (col.collider.gameObject.tag == "Wall") {
			rb2d.velocity = new Vector2 (rb2d.velocity.x, 0);
		}
	}

	void ResetJump()
	{
		rb2d.gravityScale = 1;
		jumping = false;
		jumpTimer = 0;
	}



}
