using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; namespace MoreMountains.Tools { /// /// A class used to store MMInputExecution bindings, associating a target keycode to UnityEvents /// [System.Serializable] public class MMInputExecutionBinding { /// the key the user needs to press to trigger events public KeyCode TargetKey = KeyCode.Space; /// the event to trigger when the key is pressed down public UnityEvent OnKeyDown; /// the event to trigger every frame if the key is being pressed public UnityEvent OnKey; /// the event to trigger when the key is released public UnityEvent OnKeyUp; /// /// Checks for input and invokes events if needed /// public virtual void ProcessInput() { if (OnKey != null) { if (Input.GetKey(TargetKey)) { OnKey.Invoke(); } } if (OnKeyDown != null) { if (Input.GetKeyDown(TargetKey)) { OnKeyDown.Invoke(); } } if (OnKeyUp != null) { if (Input.GetKeyUp(TargetKey)) { OnKeyUp.Invoke(); } } } } /// /// A simple class used to bind target keys to specific events to trigger when the key is pressed or released /// public class MMInputExecution : MonoBehaviour { [Header("Bindings")] /// a list of bindings public List Bindings; /// /// On update we process our input /// protected virtual void Update() { HandleInput(); } /// /// Parses all bindings and asks them to trigger events if needed /// protected virtual void HandleInput() { foreach(MMInputExecutionBinding binding in Bindings) { binding.ProcessInput(); } } } }