KitchenChaos/Assets/Scripts/GameInput.cs

132 lines
4.3 KiB
C#

using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class GameInput : MonoBehaviour {
public enum Binding {
MoveUp,
MoveDown,
MoveLeft,
MoveRight,
Interact,
InteractAlternate,
Pause,
GamepadInteract,
GamepadInteractAlternate,
GamepadPause
}
private const string PlayerPrefsBindings = "InputBindings";
private static PlayerInputActions playerInputActions;
public static GameInput Instance { get; private set; }
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, EventArgs.Empty);
}
private void InteractPerformed(InputAction.CallbackContext obj) {
OnInteractAction?.Invoke(this, EventArgs.Empty);
}
private void Pause_performed(InputAction.CallbackContext obj) {
OnPauseAction?.Invoke(this, EventArgs.Empty);
}
public static Vector2 GetMovementVectorNormalized() {
Vector2 inputVector = playerInputActions.Player.Move.ReadValue<Vector2>();
return inputVector.normalized;
}
public static 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.GamepadInteract => playerInputActions.Player.Interact.bindings[1].ToDisplayString(),
Binding.GamepadInteractAlternate => playerInputActions.Player.InteractAlternate.bindings[1].ToDisplayString(),
Binding.GamepadPause => 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.GamepadInteract:
inputAction = playerInputActions.Player.Interact;
bindingIndex = 1;
break;
case Binding.GamepadInteractAlternate:
inputAction = playerInputActions.Player.InteractAlternate;
bindingIndex = 1;
break;
case Binding.GamepadPause:
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, EventArgs.Empty);
}).Start();
}
}