using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float movSpeed = 5f;
    public Rigidbody2D playerRigidbody;
    public SpriteRenderer playerSprite;  // Reference to the player's SpriteRenderer

    private Vector2 movement;

    public KeyCode moveUp = KeyCode.W;
    public KeyCode moveDown = KeyCode.S;
    public KeyCode moveLeft = KeyCode.A;
    public KeyCode moveRight = KeyCode.D;

    void Update()
    {
        movement.x = (Input.GetKey(moveRight) ? 1 : (Input.GetKey(moveLeft) ? -1 : 0));
        movement.y = (Input.GetKey(moveUp) ? 1 : (Input.GetKey(moveDown) ? -1 : 0));

        movement = movement.normalized;

        if (movement != Vector2.zero)
        {
            float angle = Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg;
            transform.rotation = Quaternion.Euler(0, 0, angle - 90);
        }
    }

    void FixedUpdate()
    {
        playerRigidbody.linearVelocity = movement * movSpeed;
    }
}
