InputCollection Field added to IVariableMovementWithState + Bug Fix

This commit is contained in:
Syntriax 2022-03-16 13:56:32 +03:00
parent e35cdcb069
commit 9e3efe49a0
4 changed files with 52 additions and 24 deletions

View File

@ -22,6 +22,8 @@ namespace Syntriax.Modules.Movement
IMovement oldMovement = _activeMovement; IMovement oldMovement = _activeMovement;
_activeMovement = value; _activeMovement = value;
if (oldMovement != null)
OnMovementDeactivated.Invoke(oldMovement); OnMovementDeactivated.Invoke(oldMovement);
OnMovementActivated.Invoke(value); OnMovementActivated.Invoke(value);
} }
@ -29,7 +31,6 @@ namespace Syntriax.Modules.Movement
public List<IMovement> Movements { get; protected set; } = null; public List<IMovement> Movements { get; protected set; } = null;
protected IToggleState toggleState = null; protected IToggleState toggleState = null;
protected virtual void Awake() protected virtual void Awake()
@ -64,7 +65,6 @@ namespace Syntriax.Modules.Movement
foreach (IMovement movement in Movements) foreach (IMovement movement in Movements)
movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener); movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener);
} }
private void OnTakeOverListener(bool arg0) => UpdateActiveMovement(); private void OnTakeOverListener(bool arg0) => UpdateActiveMovement();

View File

@ -1,4 +1,5 @@
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.InputSystem;
namespace Syntriax.Modules.Movement.VariableMovement namespace Syntriax.Modules.Movement.VariableMovement
{ {
@ -6,5 +7,6 @@ namespace Syntriax.Modules.Movement.VariableMovement
{ {
bool Enabled { get; set; } bool Enabled { get; set; }
UnityEvent<bool> OnToggleStateChanged { get; } UnityEvent<bool> OnToggleStateChanged { get; }
IInputActionCollection InputCollection { set; }
} }
} }

View File

@ -9,7 +9,18 @@ namespace Syntriax.Modules.Movement.VariableMovement
public class VariableMovementController : MonoBehaviour, IVariableMovementController public class VariableMovementController : MonoBehaviour, IVariableMovementController
{ {
public UnityEvent<IVariableMovement> OnVariableMovementChanged { get; protected set; } = null; public UnityEvent<IVariableMovement> 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; private IVariableMovement _activeVariableMovement = null;
public IVariableMovement ActiveVariableMovement public IVariableMovement ActiveVariableMovement
{ {
@ -63,9 +74,6 @@ namespace Syntriax.Modules.Movement.VariableMovement
public void LoadVariableMovementCollection(VMCollection collection) 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; this.collection = collection;
isSet = true; isSet = true;

View File

@ -26,20 +26,7 @@ namespace Syntriax.Modules.Movement.VariableMovement
get => _asset; get => _asset;
set set
{ {
if (actionReference != null) UpdateBindings();
{
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;
}
_asset = value; _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; protected InputAction actionReference = null;
public VariableMovementWithState(VMAsset asset, IInputActionCollection inputClass) public VariableMovementWithState(VMAsset asset, IInputActionCollection inputCollection)
{ {
this.inputClass = inputClass; InputCollection = inputCollection;
OnToggleStateChanged = new UnityEvent<bool>(); OnToggleStateChanged = new UnityEvent<bool>();
Asset = asset; Asset = asset;
} }
protected void Cancelled(InputAction.CallbackContext obj) => Enabled = false; protected void Cancelled(InputAction.CallbackContext obj) => Enabled = false;
protected void Performed(InputAction.CallbackContext obj) => Enabled = true; 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;
}
}
} }
} }