using System;
using UnityEngine;
public class GameInput : MonoBehaviour
{
public event EventHandler OnInteractAction; //Event that is triggered when the player performs the interact action. This allows other scripts to subscribe to this event and respond when the player interacts with something.
private PlayerInputActions playerInputActions; //Reference to the PlayerInputActions class generated by the Input System. This class contains the input actions defined in the Input Actions asset.
private void Awake()
{
playerInputActions = new PlayerInputActions(); //Create an instance of the PlayerInputActions class.
playerInputActions.Player.Enable(); //Enable the Player action map, which contains the input actions for player movement. This allows the script to read input from the defined actions.
playerInputActions.Player.Interact.performed += Interact_performed; //Subscribe to the performed event of the Interact action. This event is triggered when the Interact action is performed (e.g., when the player presses the interact button).
}
private void Interact_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
OnInteractAction?.Invoke(this, EventArgs.Empty); //Invoke the OnInteractAction event when the Interact action is performed. This will notify any subscribers that the player has interacted with something.
}
public Vector2 GetMovementVectorNormalized() //This method reads the movement input from the Player action map and returns it as a normalized Vector2. Normalizing the vector ensures that the player moves at a consistent speed in all directions, even when moving diagonally.
{
Vector2 inputVector = playerInputActions.Player.Move.ReadValue<Vector2>();
inputVector.Normalize();
return inputVector;
}
}