137 lines
5.2 KiB
C#
137 lines
5.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class GameInput : MonoBehaviour
|
|
{
|
|
public static GameInput Instance { get; private set; }
|
|
|
|
public enum Binding
|
|
{
|
|
MoveUp,
|
|
MoveDown,
|
|
MoveLeft,
|
|
MoveRight,
|
|
Interact,
|
|
InteractAlternate,
|
|
Pause,
|
|
Gamepad_Interact,
|
|
Gamepad_InteractAlternate,
|
|
Gamepad_Pause
|
|
}
|
|
|
|
private const string PlayerPrefsBindings = "InputBindings";
|
|
private static PlayerInputActions playerInputActions;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
|
|
playerInputActions = new();
|
|
if (PlayerPrefs.HasKey(PlayerPrefsBindings))
|
|
playerInputActions.LoadBindingOverridesFromJson(PlayerPrefs.GetString(PlayerPrefsBindings));
|
|
playerInputActions.Player.Enable();
|
|
|
|
playerInputActions.Player.Interact.performed += InteractPerformed;
|
|
playerInputActions.Player.InteractAlternate.performed += InteractAlternate_performed;
|
|
playerInputActions.Player.Pause.performed += Pause_performed;
|
|
}
|
|
|
|
public event EventHandler OnInteractAction;
|
|
public event EventHandler OnInteractAlternateAction;
|
|
public event EventHandler OnPauseAction;
|
|
public event EventHandler OnBindingRebind;
|
|
|
|
private void InteractAlternate_performed(InputAction.CallbackContext obj) =>
|
|
OnInteractAlternateAction?.Invoke(this, System.EventArgs.Empty);
|
|
|
|
private void InteractPerformed(InputAction.CallbackContext obj) => OnInteractAction?.Invoke(this, System.EventArgs.Empty);
|
|
private void Pause_performed(InputAction.CallbackContext obj) => OnPauseAction?.Invoke(this, System.EventArgs.Empty);
|
|
|
|
public static Vector2 GetMovementVectorNormalized()
|
|
{
|
|
Vector2 inputVector = playerInputActions.Player.Move.ReadValue<Vector2>();
|
|
return inputVector.normalized;
|
|
}
|
|
|
|
public string GetBindingText(Binding binding)
|
|
{
|
|
return binding switch
|
|
{
|
|
Binding.MoveUp => playerInputActions.Player.Move.bindings[1].ToDisplayString(),
|
|
Binding.MoveDown => playerInputActions.Player.Move.bindings[2].ToDisplayString(),
|
|
Binding.MoveLeft => playerInputActions.Player.Move.bindings[3].ToDisplayString(),
|
|
Binding.MoveRight => playerInputActions.Player.Move.bindings[4].ToDisplayString(),
|
|
Binding.Interact => playerInputActions.Player.Interact.bindings[0].ToDisplayString(),
|
|
Binding.InteractAlternate => playerInputActions.Player.InteractAlternate.bindings[0].ToDisplayString(),
|
|
Binding.Pause => playerInputActions.Player.Pause.bindings[0].ToDisplayString(),
|
|
Binding.Gamepad_Interact => playerInputActions.Player.Interact.bindings[1].ToDisplayString(),
|
|
Binding.Gamepad_InteractAlternate => playerInputActions.Player.InteractAlternate.bindings[1].ToDisplayString(),
|
|
Binding.Gamepad_Pause => playerInputActions.Player.Pause.bindings[1].ToDisplayString(),
|
|
_ => throw new NotImplementedException(),
|
|
};
|
|
}
|
|
|
|
public void RebindBinding(Binding binding, Action onActionRebound)
|
|
{
|
|
playerInputActions.Player.Disable();
|
|
|
|
InputAction inputAction;
|
|
int bindingIndex;
|
|
|
|
switch (binding)
|
|
{
|
|
default:
|
|
case Binding.MoveUp:
|
|
inputAction = playerInputActions.Player.Move;
|
|
bindingIndex = 1;
|
|
break;
|
|
case Binding.MoveDown:
|
|
inputAction = playerInputActions.Player.Move;
|
|
bindingIndex = 2;
|
|
break;
|
|
case Binding.MoveLeft:
|
|
inputAction = playerInputActions.Player.Move;
|
|
bindingIndex = 3;
|
|
break;
|
|
case Binding.MoveRight:
|
|
inputAction = playerInputActions.Player.Move;
|
|
bindingIndex = 4;
|
|
break;
|
|
case Binding.Interact:
|
|
inputAction = playerInputActions.Player.Interact;
|
|
bindingIndex = 0;
|
|
break;
|
|
case Binding.InteractAlternate:
|
|
inputAction = playerInputActions.Player.InteractAlternate;
|
|
bindingIndex = 0;
|
|
break;
|
|
case Binding.Pause:
|
|
inputAction = playerInputActions.Player.Pause;
|
|
bindingIndex = 0;
|
|
break;
|
|
case Binding.Gamepad_Interact:
|
|
inputAction = playerInputActions.Player.Interact;
|
|
bindingIndex = 1;
|
|
break;
|
|
case Binding.Gamepad_InteractAlternate:
|
|
inputAction = playerInputActions.Player.InteractAlternate;
|
|
bindingIndex = 1;
|
|
break;
|
|
case Binding.Gamepad_Pause:
|
|
inputAction = playerInputActions.Player.Interact;
|
|
bindingIndex = 1;
|
|
break;
|
|
}
|
|
|
|
inputAction.PerformInteractiveRebinding(bindingIndex).OnComplete(callback =>
|
|
{
|
|
playerInputActions.Player.Enable();
|
|
onActionRebound();
|
|
PlayerPrefs.SetString(PlayerPrefsBindings, playerInputActions.SaveBindingOverridesAsJson());
|
|
PlayerPrefs.Save();
|
|
OnBindingRebind?.Invoke(this, System.EventArgs.Empty);
|
|
})
|
|
.Start();
|
|
}
|
|
} |