diff --git a/Assets/Scripts/Common/InputController.cs b/Assets/Scripts/Common/InputController.cs new file mode 100644 index 0000000..ce8db23 --- /dev/null +++ b/Assets/Scripts/Common/InputController.cs @@ -0,0 +1,77 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Events; + +public class InputController : MonoBehaviour +{ + public List Bindings; + + private bool initialized = false; + private bool polling = false; + + public void Init(bool startPolling) + { + if(Bindings == null) + { + Debug.LogError("Биндинги не назначены", this); + return; + } + + polling = startPolling; + initialized = true; + } + + public void StartPolling() => polling = true; + public void StopPolling() => polling = false; + + private void Update() + { + if (!(initialized && polling)) return; + + foreach (var binding in Bindings) + if (binding.IsTriggered()) binding.Perform(); + } + + [System.Serializable] + public class SignalBinding + { + private static readonly Dictionary> SignalChecks = new() + { + [ButtonState.Down] = s => In.GetButtonDown(s), + [ButtonState.Up] = s => In.GetButtonUp(s), + [ButtonState.Pressed] = s => In.GetButton(s), + }; + + private static readonly Dictionary> KeyboardChecks = new() + { + [ButtonState.Down] = k => Input.GetKeyDown(k), + [ButtonState.Up] = k => Input.GetKeyUp(k), + [ButtonState.Pressed] = k => Input.GetKey(k), + }; + + public InputMode Mode; + public InSignal Signal; + public KeyCode KeyboardKey; + public ButtonState RequiredButtonState; + public UnityEvent Callback; + + public bool IsTriggered() + { + bool signalActive = SignalChecks[RequiredButtonState](Signal); + bool keyboardActive = KeyboardChecks[RequiredButtonState](KeyboardKey); + + return Mode switch + { + InputMode.OnlySignal => signalActive, + InputMode.OnlyKeyboard => keyboardActive, + InputMode.Both => signalActive || keyboardActive, + _ => false + }; + } + + public void Perform() => Callback?.Invoke(); + } + + public enum InputMode { OnlySignal, OnlyKeyboard, Both } + public enum ButtonState { Down, Up, Pressed } +} \ No newline at end of file diff --git a/Assets/Scripts/Common/InputController.cs.meta b/Assets/Scripts/Common/InputController.cs.meta new file mode 100644 index 0000000..e1eb162 --- /dev/null +++ b/Assets/Scripts/Common/InputController.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: e7daf9cad9d628e4690c6795a052b6d9 \ No newline at end of file