Improved OnMoveCalled on MovementController

This commit is contained in:
Syntriax 2022-12-02 00:02:39 +03:00
parent fc22863f57
commit 70edb0e5a1
1 changed files with 18 additions and 2 deletions

View File

@ -31,6 +31,8 @@ namespace Syntriax.Modules.Movement
} }
} }
private bool areBothToggleStatesToggled => ToggleState.IsToggledNullChecked() && toggleStateOnGameObject.IsToggledNullChecked();
public List<IMovement> Movements { get; protected set; } = new List<IMovement>(32); public List<IMovement> Movements { get; protected set; } = new List<IMovement>(32);
public IToggleState ToggleState { get; protected set; } = new ToggleStateMember(true); public IToggleState ToggleState { get; protected set; } = new ToggleStateMember(true);
@ -44,11 +46,14 @@ namespace Syntriax.Modules.Movement
RecacheMovements(); RecacheMovements();
toggleStateOnGameObject = GetComponent<IToggleState>(); toggleStateOnGameObject = GetComponent<IToggleState>();
toggleStateOnGameObject.OnToggleStateChanged += (_) => InvokeOnMoveAction();
ToggleState.OnToggleStateChanged += (_) => InvokeOnMoveAction();
} }
protected virtual void FixedUpdate() protected virtual void FixedUpdate()
{ {
if (!ToggleState.IsToggledNullChecked() || !toggleStateOnGameObject.IsToggledNullChecked()) if (!areBothToggleStatesToggled)
return; return;
ActiveMovement?.ApplyMovement(); ActiveMovement?.ApplyMovement();
@ -79,10 +84,21 @@ namespace Syntriax.Modules.Movement
} }
} }
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);
OnMoveCalled?.Invoke(x, y, z); (lastMove.x, lastMove.y, lastMove.z) = (x, y, z);
InvokeOnMoveAction();
}
private void InvokeOnMoveAction()
{
if (!areBothToggleStatesToggled)
return;
OnMoveCalled?.Invoke(lastMove.x, lastMove.y, lastMove.z);
} }
} }
} }