From 4305d26d179f67897a329f98de7fcd0eef9ee2eb Mon Sep 17 00:00:00 2001 From: Syntriax Date: Mon, 21 Nov 2022 13:51:55 +0300 Subject: [PATCH] Factory Submodule Added --- .gitmodules | 3 +++ Config/MovementDefinitionFactory.cs | 33 +++++++---------------------- Config/MovementFactory.cs | 29 ++++++------------------- Factory | 1 + Factory.meta | 8 +++++++ MovementDefinitionApplier.cs | 11 +++++----- README.md | 1 + Syntriax.Modules.Movement.asmdef | 4 ++-- 8 files changed, 35 insertions(+), 55 deletions(-) create mode 160000 Factory create mode 100644 Factory.meta diff --git a/.gitmodules b/.gitmodules index a8a59d7..a3a1df6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "Trigger"] path = Trigger url = git@git.syntriax.com:Syntriax/Trigger.git +[submodule "Factory"] + path = Factory + url = git@git.syntriax.com:Syntriax/Factory.git diff --git a/Config/MovementDefinitionFactory.cs b/Config/MovementDefinitionFactory.cs index 614b82e..a67afc5 100644 --- a/Config/MovementDefinitionFactory.cs +++ b/Config/MovementDefinitionFactory.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Syntriax.Modules.Factory; using UnityEngine; namespace Syntriax.Modules.Movement.Config @@ -10,29 +11,11 @@ namespace Syntriax.Modules.Movement.Config /// Looks for the path specified in the variable /// The default path is "Resources/Modules/Syntriax/Movement/Definitions/" /// - public class MovementDefinitionFactory : MonoBehaviour + public class MovementDefinitionFactory : FactoryBase { - internal const string Name = "Movement Definition Factory"; internal const int InitialCapacity = 64; internal const string ResourceDirectoryToDefinitions = "Modules/Syntriax/Movement/Definitions/"; - private static MovementDefinitionFactory _instance = null; - public static MovementDefinitionFactory Instance - { - get - { - if (_instance == null) - { - GameObject factoriesGO = GameObject.Find("Factories") ?? new GameObject("Factories"); - _instance = new GameObject(Name).AddComponent(); - _instance.transform.SetParent(factoriesGO.transform); - DontDestroyOnLoad(factoriesGO); - } - - return _instance; - } - } - private Dictionary _definitions = null; public Dictionary Definitions { @@ -47,7 +30,7 @@ namespace Syntriax.Modules.Movement.Config private void Start() { - if (_instance == this) + if (FactorySingleton.Instance == this) return; Destroy(this); @@ -60,7 +43,7 @@ namespace Syntriax.Modules.Movement.Config /// Automatically loads the definition files specificed path /// The default is "Resources/Modules/Syntriax/Movement/Definitions/" /// - public void Initialize() + public override void Initialize() { if (_definitions == null) _definitions = new Dictionary(InitialCapacity); @@ -87,7 +70,7 @@ namespace Syntriax.Modules.Movement.Config /// /// Does not reinitialize, please call to initialize it back /// - public void Reset() => _definitions?.Clear(); + public override void Reset() => _definitions?.Clear(); /// /// Registers a list of s to the factory @@ -118,7 +101,7 @@ namespace Syntriax.Modules.Movement.Config 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}"); + throw new System.ArgumentException($"The definition with name \"{definitionName}\" does not exists in the current {nameof(MovementDefinitionFactory)}"); ApplyDefinitionToGameObject(gameObject, Definitions[definitionName]); } @@ -132,13 +115,13 @@ namespace Syntriax.Modules.Movement.Config { foreach (MovementConfig movementConfig in definition.MovementConfigs) { - IMovement movement = (IMovement)MovementFactory.Instance.AddToGameObject(gameObject, movementConfig.TypeName); + IMovement movement = (IMovement)FactorySingleton.Instance.AddToGameObject(gameObject, movementConfig.TypeName); movement.BaseSpeed = movementConfig.BaseSpeed; } if (definition.MonoBehaviours != null) foreach (string monoBehaviours in definition.MonoBehaviours) - MovementFactory.Instance.AddToGameObject(gameObject, monoBehaviours); + FactorySingleton.Instance.AddToGameObject(gameObject, monoBehaviours); } } } diff --git a/Config/MovementFactory.cs b/Config/MovementFactory.cs index 58ab60d..be1ce46 100644 --- a/Config/MovementFactory.cs +++ b/Config/MovementFactory.cs @@ -2,32 +2,15 @@ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; +using Syntriax.Modules.Factory; using UnityEngine; namespace Syntriax.Modules.Movement.Config { - public class MovementFactory : MonoBehaviour + public class MovementFactory : FactoryBase { - private const string Name = "Movement Factory"; private const int InitialCapacity = 64; - private static MovementFactory _instance = null; - public static MovementFactory Instance - { - get - { - if (_instance == null) - { - GameObject factoriesGO = GameObject.Find("Factories") ?? new GameObject("Factories"); - _instance = new GameObject(Name).AddComponent(); - _instance.transform.SetParent(factoriesGO.transform); - DontDestroyOnLoad(factoriesGO); - } - - return _instance; - } - } - private Dictionary _types = null; public Dictionary Types { @@ -44,13 +27,13 @@ namespace Syntriax.Modules.Movement.Config private void Start() { - if (_instance == this) + if (FactorySingleton.Instance == this) return; Destroy(this); } - public void Initialize() + public override void Initialize() { if (_types == null) _types = new Dictionary(InitialCapacity); @@ -61,7 +44,7 @@ namespace Syntriax.Modules.Movement.Config AddAssemblyToFactory(assembly); } - public void Reset() => _types?.Clear(); + public override void Reset() => _types?.Clear(); public void AddAssemblyToFactory(Assembly assembly) { @@ -72,7 +55,7 @@ namespace Syntriax.Modules.Movement.Config public Component AddToGameObject(GameObject gameObject, string name) { if (!Types.ContainsKey(name)) - throw new ArgumentException($"The key \"{name}\" does not exists in the current {Name}"); + throw new ArgumentException($"The key \"{name}\" does not exists in the current {nameof(MovementFactory)}"); return gameObject.AddComponent(Types[name]); } diff --git a/Factory b/Factory new file mode 160000 index 0000000..49b1bcd --- /dev/null +++ b/Factory @@ -0,0 +1 @@ +Subproject commit 49b1bcd578aa5d3b6f84750e730d179a950f26c8 diff --git a/Factory.meta b/Factory.meta new file mode 100644 index 0000000..9c4a804 --- /dev/null +++ b/Factory.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 218ba81f028403c4185f629220600c77 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MovementDefinitionApplier.cs b/MovementDefinitionApplier.cs index b52ea6b..2d4e3cc 100644 --- a/MovementDefinitionApplier.cs +++ b/MovementDefinitionApplier.cs @@ -1,4 +1,5 @@ using System; +using Syntriax.Modules.Factory; using Syntriax.Modules.Movement.Config; using UnityEngine; @@ -25,7 +26,7 @@ namespace Syntriax.Modules.Movement { RemoveDefinition(); - MovementDefinitionFactory.Instance.ApplyDefinitionToGameObject(gameObject, definitionName); + FactorySingleton.Instance.ApplyDefinitionToGameObject(gameObject, definitionName); appliedDefinitionName = definitionName; OnMovementDefinitionApplied?.Invoke(gameObject); } @@ -34,7 +35,7 @@ namespace Syntriax.Modules.Movement { RemoveDefinition(); - MovementDefinitionFactory.Instance.ApplyDefinitionToGameObject(gameObject, definition); + FactorySingleton.Instance.ApplyDefinitionToGameObject(gameObject, definition); appliedDefinitionName = definition.Name; OnMovementDefinitionApplied?.Invoke(gameObject); } @@ -44,20 +45,20 @@ namespace Syntriax.Modules.Movement if (string.IsNullOrWhiteSpace(appliedDefinitionName)) return; - MovementDefinition appliedDefinition = MovementDefinitionFactory.Instance.Definitions[appliedDefinitionName]; + MovementDefinition appliedDefinition = FactorySingleton.Instance.Definitions[appliedDefinitionName]; if (appliedDefinition.MovementConfigs == null || appliedDefinition.MonoBehaviours == null) return; foreach (MovementConfig movementConfig in appliedDefinition.MovementConfigs) { - Type type = MovementFactory.Instance.Types[movementConfig.TypeName]; + Type type = FactorySingleton.Instance.Types[movementConfig.TypeName]; if (TryGetComponent(type, out Component component)) Destroy(component); } foreach (string monoBehaviour in appliedDefinition.MonoBehaviours) { - Type type = MovementFactory.Instance.Types[monoBehaviour]; + Type type = FactorySingleton.Instance.Types[monoBehaviour]; if (TryGetComponent(type, out Component component)) Destroy(component); } diff --git a/README.md b/README.md index c5bb252..84c6bee 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ ## Dependencies 1. [ToggleState Module](https://git.syntriax.com/Syntriax/ToggleState) 2. [Trigger Module](https://git.syntriax.com/Syntriax/Trigger) +3. [Factory Module](https://git.syntriax.com/Syntriax/Factory) Make sure to separately clone these modules along with this one too. diff --git a/Syntriax.Modules.Movement.asmdef b/Syntriax.Modules.Movement.asmdef index 5a97952..ca9261d 100644 --- a/Syntriax.Modules.Movement.asmdef +++ b/Syntriax.Modules.Movement.asmdef @@ -2,9 +2,9 @@ "name": "Syntriax.Modules.Movement", "rootNamespace": "", "references": [ - "GUID:75469ad4d38634e559750d17036d5f7c", "GUID:efa9a9bc94c60c74684aafb7428fbf61", - "GUID:1f5f15fe7e49bdb48a76c5ce9b1c9f2f" + "GUID:1f5f15fe7e49bdb48a76c5ce9b1c9f2f", + "GUID:d4c952ed5f59c5a449cda1b0080ed841" ], "includePlatforms": [], "excludePlatforms": [],