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

public class character : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    private int angle = 0;
    public Rigidbody2D _actualRigid;
    public Camera mainCamera;

    public float moveSpeed = 1.5f;

    // Update is called once per frame
    void FixedUpdate()
    {

        float _z = 1000 - (-1.0f * (transform.position.y)) * 0.01f;
        if (_z < 0.0f)
        {
            _z = 0.0f;
        }
        transform.position = new Vector3(transform.position.x, transform.position.y, _z);


        int Vector_X = 0;
        int Vector_Y = 0;

        if (Input.GetKey(KeyCode.W))
            Vector_Y = 1;
        else if (Input.GetKey(KeyCode.S))
            Vector_Y = -1;
        else
            Vector_Y = 0;


        if (Input.GetKey(KeyCode.D))
            Vector_X = 1;
        else if (Input.GetKey(KeyCode.A))
            Vector_X = -1;
        else
            Vector_X = 0;


        if (Vector_X != 0 || Vector_Y != 0)
        {
            if (Vector_X == 1 && Vector_Y == 1)
            {
                angle = 45;
            }
            if (Vector_X == 0 && Vector_Y == 1)
            {
                angle = 90;
            }
            if (Vector_X == -1 && Vector_Y == 1)
            {
                angle = 135;
            }
            if (Vector_X == -1 && Vector_Y == 0)
            {
                angle = 180;
            }
            if (Vector_X == -1 && Vector_Y == -1)
            {
                angle = 225;
            }
            if (Vector_X == 0 && Vector_Y == -1)
            {
                angle = 270;
            }
            if (Vector_X == 1 && Vector_Y == -1)
            {
                angle = 315;
            }
            if (Vector_X == 1 && Vector_Y == 0)
            {
                angle = 360;
            }

            float lookAngle = angle;

            angle += 180;
            float _angle = (float)(Mathf.PI * angle / 180.0f);

            float newSpeed = -1f * moveSpeed;

            _actualRigid.velocity = new Vector2((newSpeed * Mathf.Cos(_angle)), ((newSpeed * Mathf.Sin(_angle)) / 1.5f));



        }
        else
        {
            
            _actualRigid.velocity = new Vector2(0f, 0f);
        }


    }

    public bool followCamera = false;

    void LateUpdate()
    {
        transform.position = new Vector3((float)System.Math.Round(transform.position.x, 2), (float)System.Math.Round(transform.position.y, 2), transform.position.z);
        if (followCamera == true)
        {
            mainCamera.transform.position = new Vector3(transform.position.x, transform.position.y, -20f);
        }
    }
}
