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

public class PlayerControls : MonoBehaviour {

    public bool keyboard = false;

	public int playerNumber;
	public float movementSpeed;
	public float turnSpeed;
	public Vector2 movementVector;
	public Vector2 turnVector;

    private Animator anim;

	private CharacterController characterController;

	// Use this for initialization
	void Start () {
		characterController = GetComponent<CharacterController>();
	    rb2d = GetComponent<Rigidbody2D>();
	    anim = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {

		//movementVector.x = Input.GetAxis ("LeftJoystickX_P" + playerNumber) * movementSpeed;
		//movementVector.y = Input.GetAxis ("LeftJoystickY_P" + playerNumber) * movementSpeed;

		//turnVector.x = Input.GetAxis ("RightJoystickY_P" + playerNumber) * turnSpeed;
		//turnVector.y = Input.GetAxis ("RightJoystickX_P" + playerNumber) * turnSpeed;

		//if(characterController.isGrounded) {
		//	movementVector.y = 0;

		//	if(Input.GetButtonDown("A_P"+ playerNumber)) {
		//	}
		//}
        
		//characterController.Move(movementVector * Time.deltaTime);
		////transform.Rotate(turnVector, Space.Self);
		//transform.Rotate(Vector3.up, Time.deltaTime, Space.World);
		//transform.Rotate(Vector3.right, Time.deltaTime, Space.World);
		////transform.Rotate(Time.deltaTime, Time.deltaTime, 0);
	}

    private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
    
    //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    void FixedUpdate()
    {
        //Store the current horizontal input in the float moveHorizontal.
        float moveHorizontal = Input.GetAxis("Horizontal_P" + playerNumber);

        //Store the current vertical input in the float moveVertical.
        float moveVertical = Input.GetAxis("Vertical_P" + playerNumber);

        //Use the two store floats to create a new Vector2 variable movement.
        Vector2 movement = new Vector2(moveHorizontal, moveVertical).normalized;

        int direction = 0;

        if (movement.magnitude > 0)
        {
            // Moving up/down
            if (Mathf.Abs(moveVertical) > Mathf.Abs(moveHorizontal))
            {
                direction = moveVertical < 0 ? 1 : 3;
            }
            // Moving right/left
            else
            {
                direction = moveHorizontal < 0 ? 4 : 2;
            }

        }

        anim.SetInteger("Direction", direction);

        //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
        rb2d.position += movement * movementSpeed * Time.deltaTime;
    }
}
