Factory Submodule Added

This commit is contained in:
Syntriax 2022-11-21 13:51:55 +03:00
parent 03499d4692
commit 4305d26d17
8 changed files with 35 additions and 55 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "Trigger"] [submodule "Trigger"]
path = Trigger path = Trigger
url = git@git.syntriax.com:Syntriax/Trigger.git url = git@git.syntriax.com:Syntriax/Trigger.git
[submodule "Factory"]
path = Factory
url = git@git.syntriax.com:Syntriax/Factory.git

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using Syntriax.Modules.Factory;
using UnityEngine; using UnityEngine;
namespace Syntriax.Modules.Movement.Config namespace Syntriax.Modules.Movement.Config
@ -10,29 +11,11 @@ namespace Syntriax.Modules.Movement.Config
/// <para> Looks for the path specified in the variable <see cref="ResourceDirectoryToDefinitions"/></para> /// <para> Looks for the path specified in the variable <see cref="ResourceDirectoryToDefinitions"/></para>
/// <para> The default path is "Resources/Modules/Syntriax/Movement/Definitions/"</para> /// <para> The default path is "Resources/Modules/Syntriax/Movement/Definitions/"</para>
/// </remarks> /// </remarks>
public class MovementDefinitionFactory : MonoBehaviour public class MovementDefinitionFactory : FactoryBase
{ {
internal const string Name = "Movement Definition Factory";
internal const int InitialCapacity = 64; internal const int InitialCapacity = 64;
internal const string ResourceDirectoryToDefinitions = "Modules/Syntriax/Movement/Definitions/"; 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<MovementDefinitionFactory>();
_instance.transform.SetParent(factoriesGO.transform);
DontDestroyOnLoad(factoriesGO);
}
return _instance;
}
}
private Dictionary<string, MovementDefinition> _definitions = null; private Dictionary<string, MovementDefinition> _definitions = null;
public Dictionary<string, MovementDefinition> Definitions public Dictionary<string, MovementDefinition> Definitions
{ {
@ -47,7 +30,7 @@ namespace Syntriax.Modules.Movement.Config
private void Start() private void Start()
{ {
if (_instance == this) if (FactorySingleton<MovementDefinitionFactory>.Instance == this)
return; return;
Destroy(this); Destroy(this);
@ -60,7 +43,7 @@ namespace Syntriax.Modules.Movement.Config
/// <para> Automatically loads the definition files specificed path <see cref="ResourceDirectoryToDefinitions"/></para> /// <para> Automatically loads the definition files specificed path <see cref="ResourceDirectoryToDefinitions"/></para>
/// <para> The default is "Resources/Modules/Syntriax/Movement/Definitions/"</para> /// <para> The default is "Resources/Modules/Syntriax/Movement/Definitions/"</para>
/// </remarks> /// </remarks>
public void Initialize() public override void Initialize()
{ {
if (_definitions == null) if (_definitions == null)
_definitions = new Dictionary<string, MovementDefinition>(InitialCapacity); _definitions = new Dictionary<string, MovementDefinition>(InitialCapacity);
@ -87,7 +70,7 @@ namespace Syntriax.Modules.Movement.Config
/// <remarks> /// <remarks>
/// Does not reinitialize, please call <see cref="Initialize"/> to initialize it back /// Does not reinitialize, please call <see cref="Initialize"/> to initialize it back
/// </remarks> /// </remarks>
public void Reset() => _definitions?.Clear(); public override void Reset() => _definitions?.Clear();
/// <summary> /// <summary>
/// Registers a list of <see cref="MovementDefinition"/>s to the factory /// Registers a list of <see cref="MovementDefinition"/>s to the factory
@ -118,7 +101,7 @@ namespace Syntriax.Modules.Movement.Config
public void ApplyDefinitionToGameObject(GameObject gameObject, string definitionName) public void ApplyDefinitionToGameObject(GameObject gameObject, string definitionName)
{ {
if (!Definitions.ContainsKey(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]); ApplyDefinitionToGameObject(gameObject, Definitions[definitionName]);
} }
@ -132,13 +115,13 @@ namespace Syntriax.Modules.Movement.Config
{ {
foreach (MovementConfig movementConfig in definition.MovementConfigs) foreach (MovementConfig movementConfig in definition.MovementConfigs)
{ {
IMovement movement = (IMovement)MovementFactory.Instance.AddToGameObject(gameObject, movementConfig.TypeName); IMovement movement = (IMovement)FactorySingleton<MovementFactory>.Instance.AddToGameObject(gameObject, movementConfig.TypeName);
movement.BaseSpeed = movementConfig.BaseSpeed; movement.BaseSpeed = movementConfig.BaseSpeed;
} }
if (definition.MonoBehaviours != null) if (definition.MonoBehaviours != null)
foreach (string monoBehaviours in definition.MonoBehaviours) foreach (string monoBehaviours in definition.MonoBehaviours)
MovementFactory.Instance.AddToGameObject(gameObject, monoBehaviours); FactorySingleton<MovementFactory>.Instance.AddToGameObject(gameObject, monoBehaviours);
} }
} }
} }

View File

@ -2,32 +2,15 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Syntriax.Modules.Factory;
using UnityEngine; using UnityEngine;
namespace Syntriax.Modules.Movement.Config 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 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<MovementFactory>();
_instance.transform.SetParent(factoriesGO.transform);
DontDestroyOnLoad(factoriesGO);
}
return _instance;
}
}
private Dictionary<string, Type> _types = null; private Dictionary<string, Type> _types = null;
public Dictionary<string, Type> Types public Dictionary<string, Type> Types
{ {
@ -44,13 +27,13 @@ namespace Syntriax.Modules.Movement.Config
private void Start() private void Start()
{ {
if (_instance == this) if (FactorySingleton<MovementFactory>.Instance == this)
return; return;
Destroy(this); Destroy(this);
} }
public void Initialize() public override void Initialize()
{ {
if (_types == null) if (_types == null)
_types = new Dictionary<string, Type>(InitialCapacity); _types = new Dictionary<string, Type>(InitialCapacity);
@ -61,7 +44,7 @@ namespace Syntriax.Modules.Movement.Config
AddAssemblyToFactory(assembly); AddAssemblyToFactory(assembly);
} }
public void Reset() => _types?.Clear(); public override void Reset() => _types?.Clear();
public void AddAssemblyToFactory(Assembly assembly) public void AddAssemblyToFactory(Assembly assembly)
{ {
@ -72,7 +55,7 @@ namespace Syntriax.Modules.Movement.Config
public Component AddToGameObject(GameObject gameObject, string name) public Component AddToGameObject(GameObject gameObject, string name)
{ {
if (!Types.ContainsKey(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]); return gameObject.AddComponent(Types[name]);
} }

1
Factory Submodule

@ -0,0 +1 @@
Subproject commit 49b1bcd578aa5d3b6f84750e730d179a950f26c8

8
Factory.meta Normal file
View File

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

View File

@ -1,4 +1,5 @@
using System; using System;
using Syntriax.Modules.Factory;
using Syntriax.Modules.Movement.Config; using Syntriax.Modules.Movement.Config;
using UnityEngine; using UnityEngine;
@ -25,7 +26,7 @@ namespace Syntriax.Modules.Movement
{ {
RemoveDefinition(); RemoveDefinition();
MovementDefinitionFactory.Instance.ApplyDefinitionToGameObject(gameObject, definitionName); FactorySingleton<MovementDefinitionFactory>.Instance.ApplyDefinitionToGameObject(gameObject, definitionName);
appliedDefinitionName = definitionName; appliedDefinitionName = definitionName;
OnMovementDefinitionApplied?.Invoke(gameObject); OnMovementDefinitionApplied?.Invoke(gameObject);
} }
@ -34,7 +35,7 @@ namespace Syntriax.Modules.Movement
{ {
RemoveDefinition(); RemoveDefinition();
MovementDefinitionFactory.Instance.ApplyDefinitionToGameObject(gameObject, definition); FactorySingleton<MovementDefinitionFactory>.Instance.ApplyDefinitionToGameObject(gameObject, definition);
appliedDefinitionName = definition.Name; appliedDefinitionName = definition.Name;
OnMovementDefinitionApplied?.Invoke(gameObject); OnMovementDefinitionApplied?.Invoke(gameObject);
} }
@ -44,20 +45,20 @@ namespace Syntriax.Modules.Movement
if (string.IsNullOrWhiteSpace(appliedDefinitionName)) if (string.IsNullOrWhiteSpace(appliedDefinitionName))
return; return;
MovementDefinition appliedDefinition = MovementDefinitionFactory.Instance.Definitions[appliedDefinitionName]; MovementDefinition appliedDefinition = FactorySingleton<MovementDefinitionFactory>.Instance.Definitions[appliedDefinitionName];
if (appliedDefinition.MovementConfigs == null || appliedDefinition.MonoBehaviours == null) if (appliedDefinition.MovementConfigs == null || appliedDefinition.MonoBehaviours == null)
return; return;
foreach (MovementConfig movementConfig in appliedDefinition.MovementConfigs) foreach (MovementConfig movementConfig in appliedDefinition.MovementConfigs)
{ {
Type type = MovementFactory.Instance.Types[movementConfig.TypeName]; Type type = FactorySingleton<MovementFactory>.Instance.Types[movementConfig.TypeName];
if (TryGetComponent(type, out Component component)) if (TryGetComponent(type, out Component component))
Destroy(component); Destroy(component);
} }
foreach (string monoBehaviour in appliedDefinition.MonoBehaviours) foreach (string monoBehaviour in appliedDefinition.MonoBehaviours)
{ {
Type type = MovementFactory.Instance.Types[monoBehaviour]; Type type = FactorySingleton<MovementFactory>.Instance.Types[monoBehaviour];
if (TryGetComponent(type, out Component component)) if (TryGetComponent(type, out Component component))
Destroy(component); Destroy(component);
} }

View File

@ -3,6 +3,7 @@
## Dependencies ## Dependencies
1. [ToggleState Module](https://git.syntriax.com/Syntriax/ToggleState) 1. [ToggleState Module](https://git.syntriax.com/Syntriax/ToggleState)
2. [Trigger Module](https://git.syntriax.com/Syntriax/Trigger) 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. Make sure to separately clone these modules along with this one too.

View File

@ -2,9 +2,9 @@
"name": "Syntriax.Modules.Movement", "name": "Syntriax.Modules.Movement",
"rootNamespace": "", "rootNamespace": "",
"references": [ "references": [
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:efa9a9bc94c60c74684aafb7428fbf61", "GUID:efa9a9bc94c60c74684aafb7428fbf61",
"GUID:1f5f15fe7e49bdb48a76c5ce9b1c9f2f" "GUID:1f5f15fe7e49bdb48a76c5ce9b1c9f2f",
"GUID:d4c952ed5f59c5a449cda1b0080ed841"
], ],
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],