Added IMovementController.Move method & Added Some Documentation

This commit is contained in:
Syntriax 2022-11-15 13:08:37 +03:00
parent e0b7277c19
commit 3b3531d52c
8 changed files with 127 additions and 3 deletions

View File

@ -8,22 +8,22 @@ namespace Syntriax.Modules.Movement
{
protected IToggleState toggleState = null;
protected IMovementController movementController = null;
private bool _canTakeOver = false;
public float BaseSpeed { get; set; } = 1f;
public float MovementMultiplier { get; set; } = 1f;
public Action<bool> OnTakeOverStateChanged { get; set; } = null;
private bool _canTakeOver = false;
public bool CanTakeOver
{
get => _canTakeOver;
protected set
{
bool isNewValue = _canTakeOver != value;
bool oldValue = _canTakeOver;
_canTakeOver = value;
if (isNewValue)
if (oldValue != value)
OnTakeOverStateChanged?.Invoke(value);
}
}
@ -45,12 +45,14 @@ namespace Syntriax.Modules.Movement
movementController.OnMovementActivated += OnActivated;
movementController.OnMovementDeactivated += OnDeactivated;
}
private void OnActivated(IMovement movement)
{
if ((object)movement != this)
return;
OnActivated();
}
private void OnDeactivated(IMovement movement)
{
if ((object)movement != this)

View File

@ -3,6 +3,13 @@ using UnityEngine;
namespace Syntriax.Modules.Movement.Config
{
/// <summary>
/// The Singleton factory responsible for loading and applying <see cref="MovementDefinition"/>s,
/// </summary>
/// <remarks>
/// <para> Looks for the path specified in the variable <see cref="ResourceDirectoryToDefinitions"/></para>
/// <para> The default path is "Resources/Modules/Syntriax/Movement/Definitions/"</para>
/// </remarks>
public class MovementDefinitionFactory : MonoBehaviour
{
private const string Name = "Movement Definition Factory";
@ -41,6 +48,13 @@ namespace Syntriax.Modules.Movement.Config
Destroy(this);
}
/// <summary>
/// Initializes the factory, if already initialized reinitializes
/// </summary>
/// <remarks>
/// <para> Automatically loads the definition files specificed path <see cref="ResourceDirectoryToDefinitions"/></para>
/// <para> The default is "Resources/Modules/Syntriax/Movement/Definitions/"</para>
/// </remarks>
public void Initialize()
{
if (_definitions == null)
@ -52,20 +66,36 @@ namespace Syntriax.Modules.Movement.Config
AddToFactoryWithJSON(definitionTextAsset.text);
}
/// <summary>
/// Manually registers a <see cref="MovementDefinition"/> to the factory with a JSON <see cref="string"/>
/// </summary>
/// <param name="definitionJSONText">A <see cref="string"/> of a <see cref="MovementDefinition"/> in JSON</param>
public void AddToFactoryWithJSON(string definitionJSONText)
{
MovementDefinition movementDefinition = JsonUtility.FromJson<MovementDefinition>(definitionJSONText);
AddDefinitionToFactory(movementDefinition);
}
/// <summary>
/// Clears all the definitions in the factory
/// </summary>
/// <remarks>
/// Does not reinitialize, please call <see cref="Initialize"/> to initialize it back
/// </remarks>
public void Reset() => _definitions?.Clear();
/// <summary>
/// Registers a list of <see cref="MovementDefinition"/>s to the factory
/// </summary>
public void AddDefinitionsToFactory(List<MovementDefinition> movementDefinitions)
{
foreach (MovementDefinition movementDefinition in movementDefinitions)
AddDefinitionToFactory(movementDefinition);
}
/// <summary>
/// Registers a single <see cref="MovementDefinition"/> to the factory
/// </summary>
public void AddDefinitionToFactory(MovementDefinition movementDefinition)
{
if (Definitions.ContainsKey(movementDefinition.Name))
@ -74,6 +104,12 @@ namespace Syntriax.Modules.Movement.Config
Definitions.Add(movementDefinition.Name, movementDefinition);
}
/// <summary>
/// Searches for the <see cref="MovementDefinition"/> in the factory and applies it to the <see cref="GameObject"/>
/// </summary>
/// <param name="gameObject"><see cref="GameObject"/> that the <see cref="MovementDefinition"/> will be applied to</param>
/// <param name="definitionName">The registered <see cref="MovementDefinition"/>'s name that will be applied to the provided <see cref="GameObject"/></param>
/// <exception cref="System.ArgumentException">When the provided <see cref="MovementDefinition"/> name is not registered in the factory</exception>
public void ApplyDefinitionToGameObject(GameObject gameObject, string definitionName)
{
if (!Definitions.ContainsKey(definitionName))
@ -82,6 +118,11 @@ namespace Syntriax.Modules.Movement.Config
ApplyDefinitionToGameObject(gameObject, Definitions[definitionName]);
}
/// <summary>
/// Applies the provided <see cref="MovementDefinition"/> to the <see cref="GameObject"/>
/// </summary>
/// <param name="gameObject"><see cref="GameObject"/> that the <see cref="MovementDefinition"/> will be applied to</param>
/// <param name="definition">The <see cref="MovementDefinition"/> that will be applied to the provided <see cref="GameObject"/></param>
public void ApplyDefinitionToGameObject(GameObject gameObject, MovementDefinition definition)
{
foreach (MovementConfig movementConfig in definition.MovementConfigs)

View File

@ -5,13 +5,31 @@ namespace Syntriax.Modules.Movement
public interface IMovement
{
float BaseSpeed { get; set; }
/// <summary>
/// The value will be multiplied with the <see cref="BaseSpeed"/> value
/// </summary>
float MovementMultiplier { get; set; }
/// <summary>
/// If the <see cref="IMovement"/> can be taken over by the <see cref="IMovementController"/>s as the <see cref="IMovementController.ActiveMovement"/>
/// </summary>
bool CanTakeOver { get; }
/// <summary>
/// Called everytime the <see cref="CanTakeOver"/> field is changed
/// </summary>
/// <value>The new value of <see cref="CanTakeOver"/></value>
Action<bool> OnTakeOverStateChanged { get; set; }
/// <summary>
/// Updates the movement data on the <see cref="IMovement"/>
/// </summary>
void Move(float x = 0f, float y = 0f, float z = 0f);
/// <summary>
/// Applies the movement that was passed on with the <see cref="Move"/> method
/// </summary>
void ApplyMovement();
}
}

View File

@ -5,12 +5,36 @@ namespace Syntriax.Modules.Movement
{
public interface IMovementController
{
/// <summary>
/// Currently active <see cref="IMovement"/>
/// </summary>
IMovement ActiveMovement { get; }
/// <summary>
/// List of all <see cref="IMovement"/>s controlled by this controller, can be updated by calling <see cref="RecacheMovements"/> method
/// </summary>
List<IMovement> Movements { get; }
/// <summary>
/// Called when a <see cref="IMovement"/> is deactivated when another <see cref="IMovement"/> takes over it's place
/// </summary>
/// <value>Deactived <see cref="IMovement"/></value>
Action<IMovement> OnMovementDeactivated { get; set; }
/// <summary>
/// Called when a new <see cref="IMovement"/> takes over by the controller
/// </summary>
/// <value>Actived <see cref="IMovement"/></value>
Action<IMovement> OnMovementActivated { get; set; }
/// <summary>
/// Updates the <see cref="Movements"/> list
/// </summary>
void RecacheMovements();
/// <summary>
/// Apply the movement data that <see cref="IMovement"/>s will use
/// </summary>
void Move(float x = 0, float y = 0, float z = 0);
}
}

View File

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

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4fef99ac1b1716848ad4b8462a03ebce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,9 +4,15 @@ namespace Syntriax.Modules.Movement
{
public static class IMovementExtensions
{
/// <summary>
/// Applies a <see cref="Vector2"/> parameter into the <see cref="IMovement"/>'s <see cref="IMovement.Move(x, y, z)"/> method by applying the vector's X and Y values accordingly
/// </summary>
public static void Move(this IMovement movement, Vector2 moveVector)
=> movement.Move(moveVector.x, moveVector.y);
/// <summary>
/// Applies a <see cref="Vector3"/> parameter into the <see cref="IMovement"/>'s <see cref="IMovement.Move(x, y, z)"/> method by applying the vector's X, Y and Z values accordingly
/// </summary>
public static void Move(this IMovement movement, Vector3 moveVector)
=> movement.Move(moveVector.x, moveVector.y, moveVector.z);
}

View File

@ -79,5 +79,8 @@ namespace Syntriax.Modules.Movement
try { ActiveMovement = Movements[Movements.Count - 1]; }
catch (System.Exception) { Debug.LogError("Movement Controller component needs at least one Monobehaviour attached that implements IMovement interface", this); }
}
public void Move(float x = 0, float y = 0, float z = 0)
=> ActiveMovement?.Move(x, y, z);
}
}