Configs and Factories added

This commit is contained in:
Syntriax 2022-03-17 22:12:09 +03:00
parent 9e3efe49a0
commit 785f6be5b9
12 changed files with 383 additions and 1 deletions

8
Config.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 1701b64210e053f47a800c9e3958e1ff
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

17
Config/MovementConfig.cs Normal file
View File

@ -0,0 +1,17 @@
using System;
namespace Syntriax.Modules.Movement.Config
{
[Serializable]
public struct MovementConfig
{
public string TypeName;
public float BaseSpeed;
public MovementConfig(string typeName, float baseSpeed)
{
TypeName = typeName;
BaseSpeed = baseSpeed;
}
}
}

View File

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

View File

@ -0,0 +1,21 @@
using System;
namespace Syntriax.Modules.Movement.Config
{
[Serializable]
public struct MovementDefinition
{
public string Name;
public string DefaultVariableMovement;
public MovementConfig[] MovementConfigs;
public string[] MonoBehaviours;
public MovementDefinition(string name, string defaultVariableMovement, MovementConfig[] movementConfigs, string[] monoBehaviours)
{
Name = name;
DefaultVariableMovement = defaultVariableMovement;
MovementConfigs = movementConfigs;
MonoBehaviours = monoBehaviours;
}
}
}

View File

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

View File

@ -0,0 +1,103 @@
using System.Collections.Generic;
using Syntriax.Modules.Movement.VariableMovement;
using UnityEngine;
namespace Syntriax.Modules.Movement.Config
{
public class MovementDefinitionFactory : MonoBehaviour
{
private const string Name = "Movement Definer 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);
}
private void Initialize()
{
_definitions = new Dictionary<string, MovementDefinition>(InitialCapacity);
foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll<TextAsset>(ResourceDirectoryToDefinitions))
{
MovementDefinition movementDefinition = JsonUtility.FromJson<MovementDefinition>(definitionTextAsset.text);
AddDefinerToFactory(movementDefinition);
}
}
public void AddDefinersToFactory(List<MovementDefinition> movementDefinitions)
{
foreach (MovementDefinition movementDefinition in movementDefinitions)
AddDefinerToFactory(movementDefinition);
}
public void AddDefinerToFactory(MovementDefinition movementDefinition)
{
if (Definitions.ContainsKey(movementDefinition.Name))
throw new System.ArgumentException($"{ movementDefinition.Name } is already in the Movement Definer 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;
}
foreach (string monoBehaviours in definition.MonoBehaviours)
MovementFactory.Instance.AddToGameObject(gameObject, monoBehaviours);
IVariableMovementController variableMovementController = gameObject.GetComponent<IVariableMovementController>();
VMCollection collection = VariableMovementFactory.Instance.Collections[definition.DefaultVariableMovement];
variableMovementController?.LoadVariableMovementCollection(collection);
}
#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
}
}

View File

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

78
Config/MovementFactory.cs Normal file
View File

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace Syntriax.Modules.Movement.Config
{
public class MovementFactory : MonoBehaviour
{
private const string Name = "Movement Factory";
private const int InitialCapacity = 64;
private static MovementFactory _instance = null;
public static MovementFactory Instance
{
get
{
if (_instance == null)
_instance = new GameObject(Name).AddComponent<MovementFactory>();
return _instance;
}
}
private Dictionary<string, Type> _types = null;
public Dictionary<string, Type> Types
{
get
{
if (_types == null)
Initialize();
return _types;
}
}
private Func<Type, bool> predicate = type => !type.IsAbstract && type.IsSubclassOf(typeof(MonoBehaviour));
private void Start()
{
if (_instance == this)
return;
Destroy(this);
}
private void Initialize()
{
_types = new Dictionary<string, Type>(InitialCapacity);
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
AddAssemblyToFactory(assembly);
}
public void AddAssemblyToFactory(Assembly assembly)
{
foreach (Type type in assembly.GetTypes().Where(predicate))
Types.Add(GetTypeName(type), type);
}
public Component AddToGameObject(GameObject gameObject, string name)
{
if (!Types.ContainsKey(name))
throw new ArgumentException($"The key \"{ name }\" does not exists in the current { Name }");
return gameObject.AddComponent(Types[name]);
}
public string GetTypeName(Type type)
{
if (string.IsNullOrEmpty(type.Namespace))
return type.Name;
return $"{ type.Namespace }.{ type.Name }";
}
}
}

View File

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

View File

@ -0,0 +1,98 @@
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);
}
private void Initialize()
{
_collections = new Dictionary<string, VMCollection>(InitialCapacity);
foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll<TextAsset>(ResourceDirectoryToCollections))
{
VMCollection collection = JsonUtility.FromJson<VMCollection>(definitionTextAsset.text);
AddCollectionToFactory(collection);
}
}
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
}
}

View File

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

View File

@ -5,11 +5,13 @@ namespace Syntriax.Modules.Movement.VariableMovement
[Serializable]
public struct VMCollection
{
public string Name;
public VMAsset DefaultAsset;
public VMAsset[] VMAssets;
public VMCollection(VMAsset defaultAsset, VMAsset[] vMAssets)
public VMCollection(string name, VMAsset defaultAsset, VMAsset[] vMAssets)
{
Name = name;
DefaultAsset = defaultAsset;
VMAssets = vMAssets;
}