From 27f1884692eaabf615e404b49f3c35fe1c19bef5 Mon Sep 17 00:00:00 2001 From: sega Date: Wed, 10 Jun 2026 12:30:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20InputController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -В него можно удобно добавлять биндинги --- Assets/Scripts/Common/InputController.cs | 77 +++++++++++++++++++ Assets/Scripts/Common/InputController.cs.meta | 2 + 2 files changed, 79 insertions(+) create mode 100644 Assets/Scripts/Common/InputController.cs create mode 100644 Assets/Scripts/Common/InputController.cs.meta 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