slimecing

a fighting game featuring slimes and swords
Log | Files | Refs | README

TriggerInput.cs (1381B)


      1 using System;
      2 using UnityEngine;
      3 using UnityEngine.InputSystem;
      4 
      5 namespace Slimecing.Triggers
      6 {
      7     public abstract class TriggerInput : Trigger
      8     {
      9         [SerializeField] private InputActionReference inputActionReference;
     10         public InputActionReference currentActionReference { get => inputActionReference; set => inputActionReference = value; }
     11         public InputAction action { get; set; }
     12         public override void EnableTrigger(GameObject target)
     13         {
     14             PlayerInput currentPlayerInput = target.GetComponent<PlayerInput>();
     15             if (currentPlayerInput == null) return;
     16 
     17             foreach (var a in currentPlayerInput.actions)
     18             {
     19                 if (!currentActionReference.action.name.Equals(a.name)) continue;
     20                 a.started += ctx => TriggerStarted();
     21                 a.performed += ctx => TriggerPerformed();
     22                 a.canceled += ctx => TriggerCanceled();
     23                 a.Enable();
     24             }
     25         }
     26 
     27         public void OnDisable()
     28         {
     29             action?.Disable();
     30         }
     31         public override T ReadCurrentValue<T>()
     32         {
     33             return (T) Convert.ChangeType(action, typeof(T));
     34         } 
     35         
     36         protected abstract void TriggerStarted();
     37         protected abstract void TriggerPerformed();
     38         protected abstract void TriggerCanceled();
     39         
     40     }
     41 }