From 9e3efe49a0b088a195afa37921e4714208d09e12 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 16 Mar 2022 13:56:32 +0300 Subject: [PATCH] InputCollection Field added to IVariableMovementWithState + Bug Fix --- MovementController.cs | 6 +-- .../IVariableMovementWithState.cs | 2 + .../VariableMovementController.cs | 16 ++++-- VariableMovement/VariableMovementWithState.cs | 52 +++++++++++++------ 4 files changed, 52 insertions(+), 24 deletions(-) diff --git a/MovementController.cs b/MovementController.cs index 6df7094..dba5743 100644 --- a/MovementController.cs +++ b/MovementController.cs @@ -22,14 +22,15 @@ namespace Syntriax.Modules.Movement IMovement oldMovement = _activeMovement; _activeMovement = value; - OnMovementDeactivated.Invoke(oldMovement); + + if (oldMovement != null) + OnMovementDeactivated.Invoke(oldMovement); OnMovementActivated.Invoke(value); } } public List Movements { get; protected set; } = null; - protected IToggleState toggleState = null; protected virtual void Awake() @@ -64,7 +65,6 @@ namespace Syntriax.Modules.Movement foreach (IMovement movement in Movements) movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener); - } private void OnTakeOverListener(bool arg0) => UpdateActiveMovement(); diff --git a/VariableMovement/IVariableMovementWithState.cs b/VariableMovement/IVariableMovementWithState.cs index 0015c45..736501e 100644 --- a/VariableMovement/IVariableMovementWithState.cs +++ b/VariableMovement/IVariableMovementWithState.cs @@ -1,4 +1,5 @@ using UnityEngine.Events; +using UnityEngine.InputSystem; namespace Syntriax.Modules.Movement.VariableMovement { @@ -6,5 +7,6 @@ namespace Syntriax.Modules.Movement.VariableMovement { bool Enabled { get; set; } UnityEvent OnToggleStateChanged { get; } + IInputActionCollection InputCollection { set; } } } diff --git a/VariableMovement/VariableMovementController.cs b/VariableMovement/VariableMovementController.cs index 551e9f1..5e80489 100644 --- a/VariableMovement/VariableMovementController.cs +++ b/VariableMovement/VariableMovementController.cs @@ -9,7 +9,18 @@ namespace Syntriax.Modules.Movement.VariableMovement public class VariableMovementController : MonoBehaviour, IVariableMovementController { public UnityEvent OnVariableMovementChanged { get; protected set; } = null; - public IInputActionCollection InputActionCollection { get; set; } = null; + private IInputActionCollection _inputActionCollection = null; + public IInputActionCollection InputActionCollection + { + get => _inputActionCollection; + set + { + _inputActionCollection = value; + + foreach (IVariableMovementWithState variableMovement in variableMovementsWithState) + variableMovement.InputCollection = value; + } + } private IVariableMovement _activeVariableMovement = null; public IVariableMovement ActiveVariableMovement { @@ -63,9 +74,6 @@ namespace Syntriax.Modules.Movement.VariableMovement public void LoadVariableMovementCollection(VMCollection collection) { - if (InputActionCollection == null) - throw new System.NullReferenceException("InputActionCollection must be set on the VariableMovementController to load a collection!"); - this.collection = collection; isSet = true; diff --git a/VariableMovement/VariableMovementWithState.cs b/VariableMovement/VariableMovementWithState.cs index 19fbad7..352d8ee 100644 --- a/VariableMovement/VariableMovementWithState.cs +++ b/VariableMovement/VariableMovementWithState.cs @@ -26,20 +26,7 @@ namespace Syntriax.Modules.Movement.VariableMovement get => _asset; set { - if (actionReference != null) - { - actionReference.performed -= Performed; - actionReference.canceled -= Cancelled; - } - - foreach (InputAction action in inputClass) - if (action.name == value.Name) - { - actionReference = action; - actionReference.performed += Performed; - actionReference.canceled += Cancelled; - break; - } + UpdateBindings(); _asset = value; @@ -47,17 +34,48 @@ namespace Syntriax.Modules.Movement.VariableMovement } } - protected readonly IInputActionCollection inputClass; + private IInputActionCollection _inputCollection = null; + public IInputActionCollection InputCollection + { + set + { + if (_inputCollection == value) + return; + + _inputCollection = value; + UpdateBindings(); + } + } + protected InputAction actionReference = null; - public VariableMovementWithState(VMAsset asset, IInputActionCollection inputClass) + public VariableMovementWithState(VMAsset asset, IInputActionCollection inputCollection) { - this.inputClass = inputClass; + InputCollection = inputCollection; OnToggleStateChanged = new UnityEvent(); Asset = asset; } protected void Cancelled(InputAction.CallbackContext obj) => Enabled = false; protected void Performed(InputAction.CallbackContext obj) => Enabled = true; + + protected void UpdateBindings() + { + if (actionReference != null) + { + actionReference.performed -= Performed; + actionReference.canceled -= Cancelled; + } + + if (_inputCollection != null) + foreach (InputAction action in _inputCollection) + if (action.name == Asset.Name) + { + actionReference = action; + actionReference.performed += Performed; + actionReference.canceled += Cancelled; + break; + } + } } }