projetrunandgun/Assets/Scripts/Players/PlayerInputHandler.cs
2023-10-30 21:16:40 -04:00

64 lines
1.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.PlayerLoop;
[DefaultExecutionOrder(-100)]
public class PlayerInputHandler : MonoBehaviour
{
public event EventHandler OnJumpTrigger;
public Vector2 MoveDir { get; private set; }
private GameInputs _input;
private bool _isReadyToClear;
private void Awake()
{
_input = new GameInputs();
}
private void OnEnable()
{
_input.Player.Enable();
}
private void OnDisable()
{
_input.Player.Disable();
}
private void Start()
{
_input.Player.Jump.performed += (InputAction.CallbackContext obj) => OnJumpTrigger?.Invoke(this, EventArgs.Empty);
}
private void Update()
{
ClearInput();
ProcessInput();
}
private void FixedUpdate()
{
_isReadyToClear = true;
}
private void ProcessInput()
{
MoveDir = _input.Player.Movement.ReadValue<Vector2>();
}
private void ClearInput()
{
if (!_isReadyToClear)
return;
MoveDir = Vector2.zero;
_isReadyToClear = false;
}
}