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

public class PlayerController : MonoBehaviour
{

    private CapsuleCollider2D capsuleCollider;


    private Vector3 moveDelta;

    private RaycastHit2D hit;

    private void Start()
    {
        capsuleCollider = GetComponent<CapsuleCollider2D>();
    }

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

        float playerX = Input.GetAxisRaw("Horizontal");
        float playerY = Input.GetAxisRaw("Vertical");

        //Reset MoveDelta
        moveDelta = new Vector3(playerX,playerY,0);

        //Testing
        //Debug.Log(playerX);
        //Debug.Log(playerY);
        
        // Swap Sprite Direction

        if(moveDelta.x > 0)
        {
            transform.localScale = Vector3.one;
        }
        else if(moveDelta.x < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        
        // Making sure the player can move in this direction, by casting a box there first, if the box returns null, we're free to move
        hit = Physics2D.BoxCast(transform.position, capsuleCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));

        if(hit.collider == null)
        {
            //if there's nothing there to block the player it will move
            transform.Translate(0, moveDelta.y * Time.deltaTime, 0);
        }

        hit = Physics2D.BoxCast(transform.position, capsuleCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));

        if (hit.collider == null)
        {
            //if there's nothing there to block the player it will move
            transform.Translate(moveDelta.x * Time.deltaTime, 0, 0);


            
        }
       
    }
}
