Compare commits

...

2 Commits

Author SHA1 Message Date
Syntriax f25a9b676b Editor Updated & Cleanup 2022-11-21 14:01:01 +03:00
Syntriax 4305d26d17 Factory Submodule Added 2022-11-21 13:51:55 +03:00
9 changed files with 46 additions and 73 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,28 +44,20 @@ 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)
{ {
foreach (Type type in assembly.GetTypes().Where(predicate)) foreach (Type type in assembly.GetTypes().Where(predicate))
Types.Add(GetTypeName(type), type); Types.Add(type.FullName, type);
} }
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]);
} }
public static string GetTypeName(Type type)
{
if (string.IsNullOrEmpty(type.Namespace))
return type.Name;
return $"{type.Namespace}.{type.Name}";
}
} }
} }

View File

@ -3,6 +3,7 @@ using UnityEditor;
using UnityEditorInternal; using UnityEditorInternal;
using Syntriax.Modules.Movement.Config; using Syntriax.Modules.Movement.Config;
using System; using System;
using Syntriax.Modules.Factory;
namespace Syntriax.Modules.Movement.Editor namespace Syntriax.Modules.Movement.Editor
{ {
@ -55,12 +56,12 @@ namespace Syntriax.Modules.Movement.Editor
GUILayout.Space(10); GUILayout.Space(10);
GUILayout.Label("Tip: MovementConfigs->TypeName and MonoBehaviours have the same naming system"); GUILayout.Label("Tip: MovementConfigs->TypeName and MonoBehaviours use the type.FullName value");
GUILayout.Label("If under a namespace: \"Namespace.ClassName\""); GUILayout.Label("If under a namespace: \"Namespace.ClassName\"");
GUILayout.Label("If not under a namespace: \"ClassName\""); GUILayout.Label("If not under a namespace: \"ClassName\"");
GUILayout.Space(5); GUILayout.Space(5);
GUILayout.Label($"Tip: Apply these to your GameObjects by using {nameof(MovementDefinitionFactory)}"); GUILayout.Label($"Tip: Apply these to your GameObjects by using {nameof(MovementDefinitionFactory)} or the {nameof(MovementDefinitionApplier)} component");
GUILayout.Label($"{nameof(MovementDefinitionFactory)}.Instance.ApplyDefinitionToGameObject(\"{movementName.stringValue}\");"); GUILayout.Label($"{nameof(FactorySingleton<MovementDefinitionFactory>)}<{nameof(MovementDefinitionFactory)}>.Instance.ApplyDefinitionToGameObject(\"{movementName.stringValue}\");");
if (quickPresetButtonPressed) LoadPresetQuick(); if (quickPresetButtonPressed) LoadPresetQuick();
if (longerPresetButtonPressed) LoadPresetLonger(); if (longerPresetButtonPressed) LoadPresetLonger();
@ -74,12 +75,12 @@ namespace Syntriax.Modules.Movement.Editor
movementDefinition.MovementConfigs = new MovementConfig[] movementDefinition.MovementConfigs = new MovementConfig[]
{ {
new MovementConfig(MovementFactory.GetTypeName(typeof(Implementations.GroundMovement1D)), 5f) new MovementConfig(typeof(Implementations.GroundMovement1D).FullName, 5f)
}; };
movementDefinition.MonoBehaviours = new string[] movementDefinition.MonoBehaviours = new string[]
{ {
MovementFactory.GetTypeName(typeof(MovementController)) typeof(MovementController).FullName
}; };
UpdateSerializedObject(); UpdateSerializedObject();
@ -91,14 +92,14 @@ namespace Syntriax.Modules.Movement.Editor
movementDefinition.MovementConfigs = new MovementConfig[] movementDefinition.MovementConfigs = new MovementConfig[]
{ {
new MovementConfig(MovementFactory.GetTypeName(typeof(Implementations.AirMovement1D)), 5f), new MovementConfig(typeof(Implementations.AirMovement1D).FullName, 5f),
new MovementConfig(MovementFactory.GetTypeName(typeof(Implementations.GroundMovement1D)), 5f) new MovementConfig(typeof(Implementations.GroundMovement1D).FullName, 5f)
}; };
movementDefinition.MonoBehaviours = new string[] movementDefinition.MonoBehaviours = new string[]
{ {
MovementFactory.GetTypeName(typeof(MovementController)), typeof(MovementController).FullName,
MovementFactory.GetTypeName(typeof(ToggleState.ToggleStateMonoBehaviour)) typeof(ToggleState.ToggleStateMonoBehaviour).FullName
}; };
UpdateSerializedObject(); UpdateSerializedObject();

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": [],