223 lines
6.6 KiB
C#
223 lines
6.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace OnlyScove.Scripts
|
|
{
|
|
public class InputReader : MonoBehaviour
|
|
{
|
|
[SerializeField] private InputActionAsset inputActions;
|
|
public InputActionAsset InputActions => inputActions;
|
|
|
|
private const string REBINDS_KEY = "InputRebinds";
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (inputActions != null)
|
|
{
|
|
LoadBindings();
|
|
inputActions.Enable();
|
|
}
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (inputActions != null)
|
|
{
|
|
inputActions.Disable();
|
|
}
|
|
}
|
|
|
|
public void SaveBindings()
|
|
{
|
|
if (inputActions == null) return;
|
|
string rebinds = inputActions.SaveBindingOverridesAsJson();
|
|
PlayerPrefs.SetString(REBINDS_KEY, rebinds);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
public void LoadBindings()
|
|
{
|
|
if (inputActions == null) return;
|
|
string rebinds = PlayerPrefs.GetString(REBINDS_KEY, string.Empty);
|
|
if (!string.IsNullOrEmpty(rebinds))
|
|
{
|
|
inputActions.LoadBindingOverridesFromJson(rebinds);
|
|
}
|
|
}
|
|
|
|
public void ResetBindings()
|
|
{
|
|
if (inputActions == null) return;
|
|
inputActions.RemoveAllBindingOverrides();
|
|
PlayerPrefs.DeleteKey(REBINDS_KEY);
|
|
PlayerPrefs.Save();
|
|
}
|
|
|
|
// Continuous Inputs
|
|
public virtual Vector2 MoveInput { get; protected set; }
|
|
public virtual Vector2 LookInput { get; protected set; }
|
|
public virtual Vector2 ScrollInput { get; protected set; }
|
|
public virtual bool IsSprintHeld { get; protected set; }
|
|
public virtual bool IsAttackHeld { get; protected set; }
|
|
|
|
|
|
public bool IsAimHeld { get; protected set; }
|
|
public bool IsBlockHeld { get; protected set; }
|
|
|
|
public void ApplyNetworkInput(Vector2 move, bool isSprint)
|
|
{
|
|
MoveInput = move;
|
|
IsSprintHeld = isSprint;
|
|
}
|
|
|
|
// One-shot Events
|
|
public event Action OnJumpEvent;
|
|
public event Action OnDodgeEvent;
|
|
public event Action OnSprintEvent;
|
|
public event Action OnAttackEvent;
|
|
public event Action OnCrouchEvent;
|
|
public event Action OnInteractEvent;
|
|
public event Action OnNextInteractEvent;
|
|
public event Action OnPreviousInteractEvent;
|
|
public event Action OnToggleViewEvent;
|
|
|
|
public event Action OnReloadEvent;
|
|
public event Action OnStrongAttackEvent;
|
|
public event Action OnSwitchSideEvent;
|
|
|
|
// UI Events
|
|
public event Action OnToggleSettingsEvent; // Cho Ctrl+O
|
|
public event Action OnCancelEvent; // Cho phím ESC hoặc phím đóng UI
|
|
|
|
public void OnAttack(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
OnAttackEvent?.Invoke();
|
|
IsAttackHeld = true;
|
|
}
|
|
if (context.canceled)
|
|
{
|
|
IsAttackHeld = false;
|
|
}
|
|
}
|
|
|
|
public void OnMove(InputAction.CallbackContext context)
|
|
{
|
|
MoveInput = context.ReadValue<Vector2>();
|
|
}
|
|
|
|
public void OnLook(InputAction.CallbackContext context)
|
|
{
|
|
LookInput = context.ReadValue<Vector2>();
|
|
}
|
|
|
|
public void OnScroll(InputAction.CallbackContext context)
|
|
{
|
|
ScrollInput = context.ReadValue<Vector2>();
|
|
}
|
|
|
|
public void OnSprint(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
IsSprintHeld = true;
|
|
OnSprintEvent?.Invoke();
|
|
}
|
|
if (context.canceled) IsSprintHeld = false;
|
|
}
|
|
|
|
public void OnToggleView(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnToggleViewEvent?.Invoke();
|
|
}
|
|
|
|
public void OnJump(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
wasJumpPressed = true;
|
|
OnJumpEvent?.Invoke();
|
|
}
|
|
}
|
|
|
|
private bool wasJumpPressed;
|
|
public bool ConsumeJumpInput()
|
|
{
|
|
bool val = wasJumpPressed;
|
|
wasJumpPressed = false;
|
|
return val;
|
|
}
|
|
|
|
public void OnDodgeOrThrust(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnDodgeEvent?.Invoke();
|
|
}
|
|
|
|
public void OnCrouch(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnCrouchEvent?.Invoke();
|
|
}
|
|
|
|
public void OnInteract(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnInteractEvent?.Invoke();
|
|
}
|
|
|
|
public void OnNext(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnNextInteractEvent?.Invoke();
|
|
}
|
|
|
|
public void OnPrevious(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnPreviousInteractEvent?.Invoke();
|
|
}
|
|
|
|
// UI Callbacks
|
|
public void OnToggleSettings(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
Debug.Log("[InputReader] Toggle Settings Action Performed!");
|
|
OnToggleSettingsEvent?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void OnCancel(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed)
|
|
{
|
|
Debug.Log("[InputReader] Cancel Action Performed (ESC)!");
|
|
OnCancelEvent?.Invoke();
|
|
}
|
|
}
|
|
public void OnAim(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) IsAimHeld = true;
|
|
if (context.canceled) IsAimHeld = false;
|
|
}
|
|
public void OnBlock(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) IsBlockHeld = true;
|
|
if (context.canceled) IsBlockHeld = false;
|
|
}
|
|
|
|
public void OnStrongAttack(InputAction.CallbackContext context)
|
|
{
|
|
if(context.performed) OnStrongAttackEvent?.Invoke();
|
|
}
|
|
|
|
public void OnReload(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnReloadEvent?.Invoke();
|
|
}
|
|
|
|
public void OnSwitchSide(InputAction.CallbackContext context)
|
|
{
|
|
if (context.performed) OnSwitchSideEvent?.Invoke();
|
|
}
|
|
}
|
|
}
|