2022-03-17 22:12:09 +03:00
using System.Collections.Generic ;
2022-11-21 13:51:55 +03:00
using Syntriax.Modules.Factory ;
2022-03-17 22:12:09 +03:00
using UnityEngine ;
namespace Syntriax.Modules.Movement.Config
{
2022-11-15 13:08:37 +03:00
/// <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>
2022-11-21 13:51:55 +03:00
public class MovementDefinitionFactory : FactoryBase
2022-03-17 22:12:09 +03:00
{
2022-11-16 15:53:06 +03:00
internal const int InitialCapacity = 64 ;
internal const string ResourceDirectoryToDefinitions = "Modules/Syntriax/Movement/Definitions/" ;
2022-03-17 22:12:09 +03:00
private Dictionary < string , MovementDefinition > _definitions = null ;
public Dictionary < string , MovementDefinition > Definitions
{
get
{
2022-11-21 21:57:25 +03:00
if ( ! IsInitialized )
2022-03-17 22:12:09 +03:00
Initialize ( ) ;
return _definitions ;
}
}
2022-11-21 21:51:27 +03:00
2022-03-17 22:12:09 +03:00
private void Start ( )
{
2022-11-21 13:51:55 +03:00
if ( FactorySingleton < MovementDefinitionFactory > . Instance = = this )
2022-03-17 22:12:09 +03:00
return ;
Destroy ( this ) ;
}
2022-11-15 13:08:37 +03:00
/// <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>
2022-11-21 21:51:27 +03:00
protected override void OnFactoryInitialize ( )
2022-03-17 22:12:09 +03:00
{
2022-03-18 16:04:44 +03:00
if ( _definitions = = null )
_definitions = new Dictionary < string , MovementDefinition > ( InitialCapacity ) ;
2022-03-17 22:12:09 +03:00
foreach ( TextAsset definitionTextAsset in UnityEngine . Resources . LoadAll < TextAsset > ( ResourceDirectoryToDefinitions ) )
2022-04-04 18:28:10 +03:00
AddToFactoryWithJSON ( definitionTextAsset . text ) ;
}
2022-11-21 21:51:27 +03:00
/// <summary>
/// Clears all the definitions in the factory
/// </summary>
/// <remarks>
/// Does not reinitialize, please call <see cref="Initialize"/> to initialize it back
/// </remarks>
protected override void OnFactoryReset ( ) = > _definitions ? . Clear ( ) ;
2022-11-15 13:08:37 +03:00
/// <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>
2022-04-04 18:28:10 +03:00
public void AddToFactoryWithJSON ( string definitionJSONText )
{
MovementDefinition movementDefinition = JsonUtility . FromJson < MovementDefinition > ( definitionJSONText ) ;
AddDefinitionToFactory ( movementDefinition ) ;
2022-03-17 22:12:09 +03:00
}
2022-11-15 13:08:37 +03:00
/// <summary>
/// Registers a list of <see cref="MovementDefinition"/>s to the factory
/// </summary>
2022-03-18 16:04:44 +03:00
public void AddDefinitionsToFactory ( List < MovementDefinition > movementDefinitions )
2022-03-17 22:12:09 +03:00
{
foreach ( MovementDefinition movementDefinition in movementDefinitions )
2022-03-18 16:04:44 +03:00
AddDefinitionToFactory ( movementDefinition ) ;
2022-03-17 22:12:09 +03:00
}
2022-11-15 13:08:37 +03:00
/// <summary>
/// Registers a single <see cref="MovementDefinition"/> to the factory
/// </summary>
2022-03-18 16:04:44 +03:00
public void AddDefinitionToFactory ( MovementDefinition movementDefinition )
2022-03-17 22:12:09 +03:00
{
if ( Definitions . ContainsKey ( movementDefinition . Name ) )
2022-04-03 19:56:25 +03:00
throw new System . ArgumentException ( $"{movementDefinition.Name} is already in the Movement Definition Factory" ) ;
2022-03-17 22:12:09 +03:00
Definitions . Add ( movementDefinition . Name , movementDefinition ) ;
}
2022-11-15 13:08:37 +03:00
/// <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>
2022-03-17 22:12:09 +03:00
public void ApplyDefinitionToGameObject ( GameObject gameObject , string definitionName )
{
if ( ! Definitions . ContainsKey ( definitionName ) )
2022-11-21 13:51:55 +03:00
throw new System . ArgumentException ( $"The definition with name \" { definitionName } \ " does not exists in the current {nameof(MovementDefinitionFactory)}" ) ;
2022-03-17 22:12:09 +03:00
ApplyDefinitionToGameObject ( gameObject , Definitions [ definitionName ] ) ;
}
2022-11-15 13:08:37 +03:00
/// <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>
2022-03-17 22:12:09 +03:00
public void ApplyDefinitionToGameObject ( GameObject gameObject , MovementDefinition definition )
{
foreach ( MovementConfig movementConfig in definition . MovementConfigs )
{
2022-11-21 13:51:55 +03:00
IMovement movement = ( IMovement ) FactorySingleton < MovementFactory > . Instance . AddToGameObject ( gameObject , movementConfig . TypeName ) ;
2022-03-17 22:12:09 +03:00
movement . BaseSpeed = movementConfig . BaseSpeed ;
}
2022-03-27 20:57:17 +03:00
if ( definition . MonoBehaviours ! = null )
foreach ( string monoBehaviours in definition . MonoBehaviours )
2022-11-21 13:51:55 +03:00
FactorySingleton < MovementFactory > . Instance . AddToGameObject ( gameObject , monoBehaviours ) ;
2022-03-17 22:12:09 +03:00
}
}
}