diff --git a/Config.meta b/Config.meta new file mode 100644 index 0000000..6498917 --- /dev/null +++ b/Config.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1701b64210e053f47a800c9e3958e1ff +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Config/MovementConfig.cs b/Config/MovementConfig.cs new file mode 100644 index 0000000..efce433 --- /dev/null +++ b/Config/MovementConfig.cs @@ -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; + } + } +} diff --git a/Config/MovementConfig.cs.meta b/Config/MovementConfig.cs.meta new file mode 100644 index 0000000..b8e5ef1 --- /dev/null +++ b/Config/MovementConfig.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 15e372dde7d46d24e88ccb953b763fe4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Config/MovementDefinition.cs b/Config/MovementDefinition.cs new file mode 100644 index 0000000..1c7edec --- /dev/null +++ b/Config/MovementDefinition.cs @@ -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; + } + } +} diff --git a/Config/MovementDefinition.cs.meta b/Config/MovementDefinition.cs.meta new file mode 100644 index 0000000..14a8c45 --- /dev/null +++ b/Config/MovementDefinition.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: be60b5c0520c0c144b5debdd609db0ce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Config/MovementDefinitionFactory.cs b/Config/MovementDefinitionFactory.cs new file mode 100644 index 0000000..29ca797 --- /dev/null +++ b/Config/MovementDefinitionFactory.cs @@ -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(); + + return _instance; + } + } + + private Dictionary _definitions = null; + public Dictionary Definitions + { + get + { + if (_definitions == null) + Initialize(); + + return _definitions; + } + } + + private void Start() + { + if (_instance == this) + return; + + Destroy(this); + } + + private void Initialize() + { + _definitions = new Dictionary(InitialCapacity); + + foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll(ResourceDirectoryToDefinitions)) + { + MovementDefinition movementDefinition = JsonUtility.FromJson(definitionTextAsset.text); + AddDefinerToFactory(movementDefinition); + } + } + + public void AddDefinersToFactory(List 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(); + 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 + } +} diff --git a/Config/MovementDefinitionFactory.cs.meta b/Config/MovementDefinitionFactory.cs.meta new file mode 100644 index 0000000..bdb4c38 --- /dev/null +++ b/Config/MovementDefinitionFactory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 932063af5c72ad340818b9e75debbc48 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Config/MovementFactory.cs b/Config/MovementFactory.cs new file mode 100644 index 0000000..b205861 --- /dev/null +++ b/Config/MovementFactory.cs @@ -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(); + + return _instance; + } + } + + private Dictionary _types = null; + public Dictionary Types + { + get + { + if (_types == null) + Initialize(); + + return _types; + } + } + + private Func predicate = type => !type.IsAbstract && type.IsSubclassOf(typeof(MonoBehaviour)); + + private void Start() + { + if (_instance == this) + return; + + Destroy(this); + } + + private void Initialize() + { + _types = new Dictionary(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 }"; + } + } +} diff --git a/Config/MovementFactory.cs.meta b/Config/MovementFactory.cs.meta new file mode 100644 index 0000000..e359e22 --- /dev/null +++ b/Config/MovementFactory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1613a55f404a46542870625c4407ffa7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Config/VariableMovementFactory.cs b/Config/VariableMovementFactory.cs new file mode 100644 index 0000000..9191061 --- /dev/null +++ b/Config/VariableMovementFactory.cs @@ -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(); + + return _instance; + } + } + + private Dictionary _collections = null; + public Dictionary Collections + { + get + { + if (_collections == null) + Initialize(); + + return _collections; + } + } + + private void Start() + { + if (_instance == this) + return; + + Destroy(this); + } + + private void Initialize() + { + _collections = new Dictionary(InitialCapacity); + + foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll(ResourceDirectoryToCollections)) + { + VMCollection collection = JsonUtility.FromJson(definitionTextAsset.text); + AddCollectionToFactory(collection); + } + } + + public void AddCollectionsToFactory(List 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 + } +} diff --git a/Config/VariableMovementFactory.cs.meta b/Config/VariableMovementFactory.cs.meta new file mode 100644 index 0000000..4e279d8 --- /dev/null +++ b/Config/VariableMovementFactory.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 514a385e4a5f87b47ae0f508bc253fb9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/VariableMovement/VMCollection.cs b/VariableMovement/VMCollection.cs index 0423db2..435ff46 100644 --- a/VariableMovement/VMCollection.cs +++ b/VariableMovement/VMCollection.cs @@ -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; }