RiftMayhem/Assets/Scripts/Input/GameInputManager.cs
Pedro Gomes 8db9d99a02 Input updates
- point and click
- WASD + mouse (WIP)
- Gamepad (WIP)
2025-01-16 19:33:19 +00:00

92 lines
2.8 KiB
C#

// GameInputManager.cs
using UnityEngine;
using System.Collections.Generic;
public class GameInputManager : MonoBehaviour
{
private static GameInputManager _instance;
public static GameInputManager Instance => _instance;
public bool isUsingGamepad { get; private set; }
[SerializeField] private float gamepadDetectionThreshold = 0.1f;
private Dictionary<string, GameInputBinding> inputBindings = new Dictionary<string, GameInputBinding>();
private void Awake()
{
if (_instance != null && _instance != this)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
LoadInputBindings();
}
private void Update()
{
DetectInputMethod();
}
private void DetectInputMethod()
{
// Check gamepad input
bool gamepadInput =
Mathf.Abs(Input.GetAxis(GameConstants.Input.HorizontalAxis)) > gamepadDetectionThreshold ||
Mathf.Abs(Input.GetAxis(GameConstants.Input.VerticalAxis)) > gamepadDetectionThreshold ||
Mathf.Abs(Input.GetAxis(GameConstants.Input.AimHorizontalAxis)) > gamepadDetectionThreshold ||
Mathf.Abs(Input.GetAxis(GameConstants.Input.AimVerticalAxis)) > gamepadDetectionThreshold ||
Input.GetAxis(GameConstants.Input.RightTrigger) > gamepadDetectionThreshold ||
Input.GetAxis(GameConstants.Input.LeftTrigger) > gamepadDetectionThreshold;
if (gamepadInput)
{
isUsingGamepad = true;
}
else if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.anyKeyDown)
{
isUsingGamepad = false;
}
}
private void LoadInputBindings()
{
var bindings = Resources.LoadAll<GameInputBinding>("InputBindings");
foreach (var binding in bindings)
{
inputBindings[binding.bindingName] = binding;
Debug.Log(binding.bindingName);
}
}
public float GetValue(string bindingName)
{
if (inputBindings.TryGetValue(bindingName, out GameInputBinding binding))
return binding.GetValue();
return 0f;
}
public bool GetButtonDown(string bindingName)
{
if (inputBindings.TryGetValue(bindingName, out GameInputBinding binding))
return binding.GetButtonDown();
return false;
}
public bool GetButton(string bindingName)
{
if (inputBindings.TryGetValue(bindingName, out GameInputBinding binding))
return binding.GetButton();
return false;
}
public bool GetButtonUp(string bindingName)
{
if (inputBindings.TryGetValue(bindingName, out GameInputBinding binding))
return binding.GetButtonUp();
return false;
}
}