Controller Improvements

This commit is contained in:
Syntriax 2022-12-01 23:30:02 +03:00
parent ca589f1244
commit fc22863f57
3 changed files with 25 additions and 9 deletions

View File

@ -1,10 +1,16 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
{ {
public interface IMovementController public interface IMovementController
{ {
/// <summary>
/// Member Toggle State
/// </summary>
IToggleState ToggleState { get; }
/// <summary> /// <summary>
/// Currently active <see cref="IMovement"/> /// Currently active <see cref="IMovement"/>
/// </summary> /// </summary>
@ -27,6 +33,12 @@ namespace Syntriax.Modules.Movement
/// <value>Actived <see cref="IMovement"/></value> /// <value>Actived <see cref="IMovement"/></value>
Action<IMovement> OnMovementActivated { get; set; } Action<IMovement> OnMovementActivated { get; set; }
/// <summary>
/// Called when <see cref="Move"/> is called
/// </summary>
/// <value>The x, y, z values of <see cref="Move"/></value>
Action<float, float, float> OnMoveCalled { get; set; }
/// <summary> /// <summary>
/// Updates the <see cref="Movements"/> list /// Updates the <see cref="Movements"/> list
/// </summary> /// </summary>

View File

@ -8,12 +8,12 @@ namespace Syntriax.Modules.Movement
/// Calls <see cref="IMovementController.Move"/> with a <see cref="Vector2"/>'s values accordingly, leaving the z parameter 0 /// Calls <see cref="IMovementController.Move"/> with a <see cref="Vector2"/>'s values accordingly, leaving the z parameter 0
/// </summary> /// </summary>
public static void Move(this IMovementController movementController, Vector2 moveVector) public static void Move(this IMovementController movementController, Vector2 moveVector)
=> movementController.ActiveMovement?.Move(moveVector.x, moveVector.y, 0f); => movementController.Move(moveVector.x, moveVector.y, 0f);
/// <summary> /// <summary>
/// Calls <see cref="IMovementController.Move"/> with a <see cref="Vector3"/>'s values accordingly /// Calls <see cref="IMovementController.Move"/> with a <see cref="Vector3"/>'s values accordingly
/// </summary> /// </summary>
public static void Move(this IMovementController movementController, Vector3 moveVector) public static void Move(this IMovementController movementController, Vector3 moveVector)
=> movementController.ActiveMovement?.Move(moveVector.x, moveVector.y, moveVector.z); => movementController.Move(moveVector.x, moveVector.y, moveVector.z);
} }
} }

View File

@ -7,6 +7,8 @@ namespace Syntriax.Modules.Movement
{ {
public class MovementController : MonoBehaviour, IMovementController public class MovementController : MonoBehaviour, IMovementController
{ {
public Action<float, float, float> OnMoveCalled { get; set; } = null;
public Action<IMovement> OnMovementDeactivated { get; set; } = null; public Action<IMovement> OnMovementDeactivated { get; set; } = null;
public Action<IMovement> OnMovementActivated { get; set; } = null; public Action<IMovement> OnMovementActivated { get; set; } = null;
@ -29,12 +31,11 @@ namespace Syntriax.Modules.Movement
} }
} }
public List<IMovement> Movements { get; protected set; } = null; public List<IMovement> Movements { get; protected set; } = new List<IMovement>(32);
protected IToggleState toggleState = null; public IToggleState ToggleState { get; protected set; } = new ToggleStateMember(true);
protected virtual void Awake() protected IToggleState toggleStateOnGameObject = null;
=> Movements = new List<IMovement>(32);
protected virtual void Start() protected virtual void Start()
{ {
@ -42,12 +43,12 @@ namespace Syntriax.Modules.Movement
gameObject.AddComponent<Implementations.DefaultMovement>(); gameObject.AddComponent<Implementations.DefaultMovement>();
RecacheMovements(); RecacheMovements();
toggleState = GetComponent<IToggleState>(); toggleStateOnGameObject = GetComponent<IToggleState>();
} }
protected virtual void FixedUpdate() protected virtual void FixedUpdate()
{ {
if (!toggleState.IsToggledNullChecked()) if (!ToggleState.IsToggledNullChecked() || !toggleStateOnGameObject.IsToggledNullChecked())
return; return;
ActiveMovement?.ApplyMovement(); ActiveMovement?.ApplyMovement();
@ -79,6 +80,9 @@ namespace Syntriax.Modules.Movement
} }
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);
}
} }
} }