108 lines
3.8 KiB
C#
108 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Syntriax.Modules.Movement.Config
|
|
{
|
|
public class MovementDefinitionFactory : MonoBehaviour
|
|
{
|
|
private const string Name = "Movement Definition Factory";
|
|
private const int InitialCapacity = 64;
|
|
private const string ResourceDirectoryToDefinitions = "Modules/Syntriax/Movement/Definitions/";
|
|
|
|
private static MovementDefinitionFactory _instance = null;
|
|
public static MovementDefinitionFactory Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance == null)
|
|
_instance = new GameObject(Name).AddComponent<MovementDefinitionFactory>();
|
|
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
private Dictionary<string, MovementDefinition> _definitions = null;
|
|
public Dictionary<string, MovementDefinition> Definitions
|
|
{
|
|
get
|
|
{
|
|
if (_definitions == null)
|
|
Initialize();
|
|
|
|
return _definitions;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (_instance == this)
|
|
return;
|
|
|
|
Destroy(this);
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
if (_definitions == null)
|
|
_definitions = new Dictionary<string, MovementDefinition>(InitialCapacity);
|
|
|
|
Reset();
|
|
|
|
foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll<TextAsset>(ResourceDirectoryToDefinitions))
|
|
AddToFactoryWithJSON(definitionTextAsset.text);
|
|
}
|
|
|
|
public void AddToFactoryWithJSON(string definitionJSONText)
|
|
{
|
|
MovementDefinition movementDefinition = JsonUtility.FromJson<MovementDefinition>(definitionJSONText);
|
|
AddDefinitionToFactory(movementDefinition);
|
|
}
|
|
|
|
public void Reset() => _definitions?.Clear();
|
|
|
|
public void AddDefinitionsToFactory(List<MovementDefinition> movementDefinitions)
|
|
{
|
|
foreach (MovementDefinition movementDefinition in movementDefinitions)
|
|
AddDefinitionToFactory(movementDefinition);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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 {Name}");
|
|
|
|
ApplyDefinitionToGameObject(gameObject, Definitions[definitionName]);
|
|
}
|
|
|
|
public void ApplyDefinitionToGameObject(GameObject gameObject, MovementDefinition definition)
|
|
{
|
|
foreach (MovementConfig movementConfig in definition.MovementConfigs)
|
|
{
|
|
IMovement movement = (IMovement)MovementFactory.Instance.AddToGameObject(gameObject, movementConfig.TypeName);
|
|
movement.BaseSpeed = movementConfig.BaseSpeed;
|
|
}
|
|
|
|
if (definition.MonoBehaviours != null)
|
|
foreach (string monoBehaviours in definition.MonoBehaviours)
|
|
MovementFactory.Instance.AddToGameObject(gameObject, monoBehaviours);
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public void SaveMovementDefinition(MovementDefinition definition)
|
|
{
|
|
string jsonText = JsonUtility.ToJson(definition, true);
|
|
string path = $"Assets/Resources/{ResourceDirectoryToDefinitions}{definition.Name}.json";
|
|
System.IO.File.WriteAllText(path, jsonText);
|
|
}
|
|
#endif
|
|
}
|
|
}
|