2022-03-17 22:12:09 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
using UnityEngine;
|
|
|
|
using Syntriax.Modules.Movement.VariableMovement;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Movement.Config
|
|
|
|
{
|
|
|
|
public class VariableMovementFactory : MonoBehaviour
|
|
|
|
{
|
|
|
|
private const string Name = "Variable Movement Factory";
|
|
|
|
private const int InitialCapacity = 64;
|
|
|
|
private const string ResourceDirectoryToCollections = "Modules/Syntriax/Movement/Variable Movement/Collections/";
|
|
|
|
|
|
|
|
private static VariableMovementFactory _instance = null;
|
|
|
|
public static VariableMovementFactory Instance
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_instance == null)
|
|
|
|
_instance = new GameObject(Name).AddComponent<VariableMovementFactory>();
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Dictionary<string, VMCollection> _collections = null;
|
|
|
|
public Dictionary<string, VMCollection> Collections
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (_collections == null)
|
|
|
|
Initialize();
|
|
|
|
|
|
|
|
return _collections;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
if (_instance == this)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Destroy(this);
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:04:44 +03:00
|
|
|
public void Initialize()
|
2022-03-17 22:12:09 +03:00
|
|
|
{
|
2022-03-18 16:04:44 +03:00
|
|
|
if (_collections == null)
|
|
|
|
_collections = new Dictionary<string, VMCollection>(InitialCapacity);
|
|
|
|
|
|
|
|
Reset();
|
2022-03-17 22:12:09 +03:00
|
|
|
|
|
|
|
foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll<TextAsset>(ResourceDirectoryToCollections))
|
|
|
|
{
|
|
|
|
VMCollection collection = JsonUtility.FromJson<VMCollection>(definitionTextAsset.text);
|
|
|
|
AddCollectionToFactory(collection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 16:04:44 +03:00
|
|
|
public void Reset() => _collections?.Clear();
|
|
|
|
|
2022-03-17 22:12:09 +03:00
|
|
|
public void AddCollectionsToFactory(List<VMCollection> collections)
|
|
|
|
{
|
|
|
|
foreach (VMCollection collection in collections)
|
|
|
|
AddCollectionToFactory(collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddCollectionToFactory(VMCollection collection)
|
|
|
|
{
|
|
|
|
if (Collections.ContainsKey(collection.Name))
|
|
|
|
throw new System.ArgumentException($"{ collection.Name } is already in the { Name }");
|
|
|
|
|
|
|
|
Collections.Add(collection.Name, collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// public Component AddToGameObject(GameObject gameObject, string name)
|
|
|
|
// {
|
|
|
|
// if (!Collections.ContainsKey(name))
|
|
|
|
// throw new ArgumentException($"The key \"{ name }\" does not exists in the current Movement Factory");
|
|
|
|
|
|
|
|
// return gameObject.AddComponent(Collections[name]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
public string GetTypeName(Type type)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(type.Namespace))
|
|
|
|
return type.Name;
|
|
|
|
|
|
|
|
return $"{ type.Namespace }.{ type.Name }";
|
|
|
|
}
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
public void SaveCollection(VMCollection collection)
|
|
|
|
{
|
|
|
|
string jsonText = JsonUtility.ToJson(collection, true);
|
|
|
|
string path = $"Assets/Resources/{ ResourceDirectoryToCollections }{collection.Name}.json";
|
|
|
|
System.IO.File.WriteAllText(path, jsonText);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|