refactor: Replaced "Member" IStateEnable

This commit is contained in:
Syntriax 2023-03-20 22:51:19 +03:00
parent 25be6929f2
commit 0fc73f330b
2 changed files with 15 additions and 12 deletions

View File

@ -7,9 +7,9 @@ namespace Syntriax.Modules.Movement
public interface IMovementController public interface IMovementController
{ {
/// <summary> /// <summary>
/// Member <see cref="IStateEnable"/> to switch on or off /// <see cref="IStateEnable"/> to control the state of the <see cref="IMovementController"/> is on or off
/// </summary> /// </summary>
IStateEnable StateEnableMember { get; } IStateEnable StateEnable { get; }
/// <summary> /// <summary>
/// Currently active <see cref="IMovement"/> /// Currently active <see cref="IMovement"/>

View File

@ -31,13 +31,17 @@ namespace Syntriax.Modules.Movement
} }
} }
private bool areBothStateEnablesToggled => StateEnableMember.IsEnabledNullChecked() && stateEnableOnGameObject.IsEnabledNullChecked();
public List<IMovement> Movements { get; protected set; } = new List<IMovement>(32); public List<IMovement> Movements { get; protected set; } = new List<IMovement>(32);
public IStateEnable StateEnableMember { get; protected set; } = new StateEnableMember(true); private IStateEnable _stateEnable = null;
public IStateEnable StateEnable
protected IStateEnable stateEnableOnGameObject = null; {
get
{
_stateEnable = _stateEnable ?? GetComponent<State.IStateEnable>() ?? gameObject.AddComponent<State.StateEnableMonoBehaviour>();
return _stateEnable;
}
}
protected virtual void Start() protected virtual void Start()
{ {
@ -45,16 +49,14 @@ namespace Syntriax.Modules.Movement
gameObject.AddComponent<DefaultMovement>(); gameObject.AddComponent<DefaultMovement>();
RecacheMovements(); RecacheMovements();
stateEnableOnGameObject = GetComponent<IStateEnable>();
stateEnableOnGameObject.OnEnabledChanged += (_) => InvokeOnMoveAction(); StateEnable.OnEnabledChanged += (_) => InvokeOnMoveAction();
StateEnableMember.OnEnabledChanged += (_) => InvokeOnMoveAction();
OnMovementActivated += (newMovement) => newMovement.Move(lastMove); OnMovementActivated += (newMovement) => newMovement.Move(lastMove);
} }
protected virtual void FixedUpdate() protected virtual void FixedUpdate()
{ {
if (!areBothStateEnablesToggled) if (!StateEnable.IsEnabledNullChecked())
return; return;
ActiveMovement?.ApplyMovement(); ActiveMovement?.ApplyMovement();
@ -86,6 +88,7 @@ namespace Syntriax.Modules.Movement
} }
private Vector3 lastMove = Vector3.zero; private Vector3 lastMove = Vector3.zero;
public void Move(float x = 0, float y = 0, float z = 0) public void Move(float x = 0, float y = 0, float z = 0)
{ {
ActiveMovement?.Move(x, y, z); ActiveMovement?.Move(x, y, z);
@ -96,7 +99,7 @@ namespace Syntriax.Modules.Movement
private void InvokeOnMoveAction() private void InvokeOnMoveAction()
{ {
if (!areBothStateEnablesToggled) if (!StateEnable.IsEnabledNullChecked())
return; return;
OnMoveCalled?.Invoke(lastMove.x, lastMove.y, lastMove.z); OnMoveCalled?.Invoke(lastMove.x, lastMove.y, lastMove.z);