using System.Collections.Generic;
using Syntriax.Modules.Factory;
using UnityEngine;
namespace Syntriax.Modules.Movement.Config
{
///
/// The Singleton factory responsible for loading and applying s,
///
///
/// Looks for the path specified in the variable
/// The default path is "Resources/Modules/Syntriax/Movement/Definitions/"
///
public class MovementDefinitionFactory : FactoryBase
{
public const int InitialCapacity = 64;
public const string ResourceDirectoryToDefinitions = "Modules/Syntriax/Movement/Definitions/";
private Dictionary _definitions = null;
public Dictionary Definitions
{
get
{
if (!IsInitialized)
Initialize();
return _definitions;
}
}
private void Start()
{
if (FactorySingleton.Instance == this)
return;
Destroy(this);
}
///
/// Initializes the factory, if already initialized reinitializes
///
///
/// Automatically loads the definition files specificed path
/// The default is "Resources/Modules/Syntriax/Movement/Definitions/"
///
protected override void OnFactoryInitialize()
{
if (_definitions == null)
_definitions = new Dictionary(InitialCapacity);
foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll(ResourceDirectoryToDefinitions))
AddToFactoryWithJSON(definitionTextAsset.text);
}
///
/// Clears all the definitions in the factory
///
///
/// Does not reinitialize, please call to initialize it back
///
protected override void OnFactoryReset() => _definitions?.Clear();
///
/// Manually registers a to the factory with a JSON
///
/// A of a in JSON
public void AddToFactoryWithJSON(string definitionJSONText)
{
MovementDefinition movementDefinition = JsonUtility.FromJson(definitionJSONText);
AddDefinitionToFactory(movementDefinition);
}
///
/// Registers a list of s to the factory
///
public void AddDefinitionsToFactory(List movementDefinitions)
{
foreach (MovementDefinition movementDefinition in movementDefinitions)
AddDefinitionToFactory(movementDefinition);
}
///
/// Registers a single to the factory
///
public void AddDefinitionToFactory(MovementDefinition movementDefinition)
{
if (Definitions.ContainsKey(movementDefinition.Name))
throw new System.ArgumentException($"{movementDefinition.Name} is already in the Movement Definition Factory");
Definitions.Add(movementDefinition.Name, movementDefinition);
}
///
/// Searches for the in the factory and applies it to the
///
/// that the will be applied to
/// The registered 's name that will be applied to the provided
/// When the provided name is not registered in the factory
public void ApplyDefinitionToGameObject(GameObject gameObject, string definitionName)
{
if (!Definitions.ContainsKey(definitionName))
throw new System.ArgumentException($"The definition with name \"{definitionName}\" does not exists in the current {nameof(MovementDefinitionFactory)}");
ApplyDefinitionToGameObject(gameObject, Definitions[definitionName]);
}
///
/// Applies the provided to the
///
/// that the will be applied to
/// The that will be applied to the provided
public void ApplyDefinitionToGameObject(GameObject gameObject, MovementDefinition definition)
{
foreach (MovementConfig movementConfig in definition.MovementConfigs)
{
IMovement movement = (IMovement)FactorySingleton.Instance.AddToGameObject(gameObject, movementConfig.TypeName);
movement.BaseSpeed = movementConfig.BaseSpeed;
}
if (definition.MonoBehaviours != null)
foreach (string monoBehaviours in definition.MonoBehaviours)
FactorySingleton.Instance.AddToGameObject(gameObject, monoBehaviours);
}
}
}