Compare commits
2 Commits
03499d4692
...
f25a9b676b
Author | SHA1 | Date |
---|---|---|
Syntriax | f25a9b676b | |
Syntriax | 4305d26d17 |
|
@ -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
|
||||
|
|
|
@ -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
|
|||
/// <para> Looks for the path specified in the variable <see cref="ResourceDirectoryToDefinitions"/></para>
|
||||
/// <para> The default path is "Resources/Modules/Syntriax/Movement/Definitions/"</para>
|
||||
/// </remarks>
|
||||
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<MovementDefinitionFactory>();
|
||||
_instance.transform.SetParent(factoriesGO.transform);
|
||||
DontDestroyOnLoad(factoriesGO);
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, MovementDefinition> _definitions = null;
|
||||
public Dictionary<string, MovementDefinition> Definitions
|
||||
{
|
||||
|
@ -47,7 +30,7 @@ namespace Syntriax.Modules.Movement.Config
|
|||
|
||||
private void Start()
|
||||
{
|
||||
if (_instance == this)
|
||||
if (FactorySingleton<MovementDefinitionFactory>.Instance == this)
|
||||
return;
|
||||
|
||||
Destroy(this);
|
||||
|
@ -60,7 +43,7 @@ namespace Syntriax.Modules.Movement.Config
|
|||
/// <para> Automatically loads the definition files specificed path <see cref="ResourceDirectoryToDefinitions"/></para>
|
||||
/// <para> The default is "Resources/Modules/Syntriax/Movement/Definitions/"</para>
|
||||
/// </remarks>
|
||||
public void Initialize()
|
||||
public override void Initialize()
|
||||
{
|
||||
if (_definitions == null)
|
||||
_definitions = new Dictionary<string, MovementDefinition>(InitialCapacity);
|
||||
|
@ -87,7 +70,7 @@ namespace Syntriax.Modules.Movement.Config
|
|||
/// <remarks>
|
||||
/// Does not reinitialize, please call <see cref="Initialize"/> to initialize it back
|
||||
/// </remarks>
|
||||
public void Reset() => _definitions?.Clear();
|
||||
public override void Reset() => _definitions?.Clear();
|
||||
|
||||
/// <summary>
|
||||
/// 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)
|
||||
{
|
||||
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<MovementFactory>.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<MovementFactory>.Instance.AddToGameObject(gameObject, monoBehaviours);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<MovementFactory>();
|
||||
_instance.transform.SetParent(factoriesGO.transform);
|
||||
DontDestroyOnLoad(factoriesGO);
|
||||
}
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, Type> _types = null;
|
||||
public Dictionary<string, Type> Types
|
||||
{
|
||||
|
@ -44,13 +27,13 @@ namespace Syntriax.Modules.Movement.Config
|
|||
|
||||
private void Start()
|
||||
{
|
||||
if (_instance == this)
|
||||
if (FactorySingleton<MovementFactory>.Instance == this)
|
||||
return;
|
||||
|
||||
Destroy(this);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
public override void Initialize()
|
||||
{
|
||||
if (_types == null)
|
||||
_types = new Dictionary<string, Type>(InitialCapacity);
|
||||
|
@ -61,28 +44,20 @@ namespace Syntriax.Modules.Movement.Config
|
|||
AddAssemblyToFactory(assembly);
|
||||
}
|
||||
|
||||
public void Reset() => _types?.Clear();
|
||||
public override void Reset() => _types?.Clear();
|
||||
|
||||
public void AddAssemblyToFactory(Assembly assembly)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
|
||||
public static string GetTypeName(Type type)
|
||||
{
|
||||
if (string.IsNullOrEmpty(type.Namespace))
|
||||
return type.Name;
|
||||
|
||||
return $"{type.Namespace}.{type.Name}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ using UnityEditor;
|
|||
using UnityEditorInternal;
|
||||
using Syntriax.Modules.Movement.Config;
|
||||
using System;
|
||||
using Syntriax.Modules.Factory;
|
||||
|
||||
namespace Syntriax.Modules.Movement.Editor
|
||||
{
|
||||
|
@ -55,12 +56,12 @@ namespace Syntriax.Modules.Movement.Editor
|
|||
|
||||
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 not under a namespace: \"ClassName\"");
|
||||
GUILayout.Space(5);
|
||||
GUILayout.Label($"Tip: Apply these to your GameObjects by using {nameof(MovementDefinitionFactory)}");
|
||||
GUILayout.Label($"{nameof(MovementDefinitionFactory)}.Instance.ApplyDefinitionToGameObject(\"{movementName.stringValue}\");");
|
||||
GUILayout.Label($"Tip: Apply these to your GameObjects by using {nameof(MovementDefinitionFactory)} or the {nameof(MovementDefinitionApplier)} component");
|
||||
GUILayout.Label($"{nameof(FactorySingleton<MovementDefinitionFactory>)}<{nameof(MovementDefinitionFactory)}>.Instance.ApplyDefinitionToGameObject(\"{movementName.stringValue}\");");
|
||||
|
||||
if (quickPresetButtonPressed) LoadPresetQuick();
|
||||
if (longerPresetButtonPressed) LoadPresetLonger();
|
||||
|
@ -74,12 +75,12 @@ namespace Syntriax.Modules.Movement.Editor
|
|||
|
||||
movementDefinition.MovementConfigs = new MovementConfig[]
|
||||
{
|
||||
new MovementConfig(MovementFactory.GetTypeName(typeof(Implementations.GroundMovement1D)), 5f)
|
||||
new MovementConfig(typeof(Implementations.GroundMovement1D).FullName, 5f)
|
||||
};
|
||||
|
||||
movementDefinition.MonoBehaviours = new string[]
|
||||
{
|
||||
MovementFactory.GetTypeName(typeof(MovementController))
|
||||
typeof(MovementController).FullName
|
||||
};
|
||||
|
||||
UpdateSerializedObject();
|
||||
|
@ -91,14 +92,14 @@ namespace Syntriax.Modules.Movement.Editor
|
|||
|
||||
movementDefinition.MovementConfigs = new MovementConfig[]
|
||||
{
|
||||
new MovementConfig(MovementFactory.GetTypeName(typeof(Implementations.AirMovement1D)), 5f),
|
||||
new MovementConfig(MovementFactory.GetTypeName(typeof(Implementations.GroundMovement1D)), 5f)
|
||||
new MovementConfig(typeof(Implementations.AirMovement1D).FullName, 5f),
|
||||
new MovementConfig(typeof(Implementations.GroundMovement1D).FullName, 5f)
|
||||
};
|
||||
|
||||
movementDefinition.MonoBehaviours = new string[]
|
||||
{
|
||||
MovementFactory.GetTypeName(typeof(MovementController)),
|
||||
MovementFactory.GetTypeName(typeof(ToggleState.ToggleStateMonoBehaviour))
|
||||
typeof(MovementController).FullName,
|
||||
typeof(ToggleState.ToggleStateMonoBehaviour).FullName
|
||||
};
|
||||
|
||||
UpdateSerializedObject();
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 49b1bcd578aa5d3b6f84750e730d179a950f26c8
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 218ba81f028403c4185f629220600c77
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -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<MovementDefinitionFactory>.Instance.ApplyDefinitionToGameObject(gameObject, definitionName);
|
||||
appliedDefinitionName = definitionName;
|
||||
OnMovementDefinitionApplied?.Invoke(gameObject);
|
||||
}
|
||||
|
@ -34,7 +35,7 @@ namespace Syntriax.Modules.Movement
|
|||
{
|
||||
RemoveDefinition();
|
||||
|
||||
MovementDefinitionFactory.Instance.ApplyDefinitionToGameObject(gameObject, definition);
|
||||
FactorySingleton<MovementDefinitionFactory>.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<MovementDefinitionFactory>.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<MovementFactory>.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<MovementFactory>.Instance.Types[monoBehaviour];
|
||||
if (TryGetComponent(type, out Component component))
|
||||
Destroy(component);
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
"name": "Syntriax.Modules.Movement",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:75469ad4d38634e559750d17036d5f7c",
|
||||
"GUID:efa9a9bc94c60c74684aafb7428fbf61",
|
||||
"GUID:1f5f15fe7e49bdb48a76c5ce9b1c9f2f"
|
||||
"GUID:1f5f15fe7e49bdb48a76c5ce9b1c9f2f",
|
||||
"GUID:d4c952ed5f59c5a449cda1b0080ed841"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
|
Loading…
Reference in New Issue