using UnityEngine;
using System;

namespace MenuMap
{
    public class MapCharacterController : MonoBehaviour
    {
        public static Action<bool> BlockMovement;
        [SerializeField] private float speed = 10;
        private bool _movementBlocked;
        private Vector2 _moveInput;
        private Rigidbody2D _rb;
        

        void Start()
        {
            _rb = GetComponent<Rigidbody2D>();
            BlockMovement += SetBlockMovement;
        }

        private void OnDestroy()
        {
            BlockMovement -= SetBlockMovement;
        }

        void Update()
        {
            #region MoveInput

            float horizontal = Input.GetAxisRaw("Horizontal");
            float vertical = Input.GetAxisRaw("Vertical");
            _moveInput = new Vector2(horizontal, vertical).normalized;

            #endregion
        }

        private void FixedUpdate()
        {
            Move();
        }

        private void Move()
        {
            _rb.velocity = _movementBlocked ? Vector2.zero : _moveInput * speed;
        }

        private void SetBlockMovement(bool blocked)
        {
            _movementBlocked = blocked;
        }
    }
}
