KitchenChaos/Assets/Scripts/GameInput.cs

138 lines
5.3 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;
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 string GetBindingText(Binding binding) {
switch (binding)
{
default:
case Binding.MoveUp:
return playerInputActions.Player.Move.bindings[1].ToDisplayString();
case Binding.MoveDown:
return playerInputActions.Player.Move.bindings[2].ToDisplayString();
case Binding.MoveLeft:
return playerInputActions.Player.Move.bindings[3].ToDisplayString();
case Binding.MoveRight:
return playerInputActions.Player.Move.bindings[4].ToDisplayString();
case Binding.Interact:
return playerInputActions.Player.Interact.bindings[0].ToDisplayString();
case Binding.InteractAlternate:
return playerInputActions.Player.InteractAlternate.bindings[0].ToDisplayString();
case Binding.Pause:
return playerInputActions.Player.Pause.bindings[0].ToDisplayString();
case Binding.Gamepad_Interact:
return playerInputActions.Player.Interact.bindings[1].ToDisplayString();
case Binding.Gamepad_InteractAlternate:
return playerInputActions.Player.InteractAlternate.bindings[1].ToDisplayString();
case Binding.Gamepad_Pause:
return playerInputActions.Player.Pause.bindings[1].ToDisplayString();
}
}
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();
})
.Start();
}
}