Factory Submodule Added
This commit is contained in:
@@ -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,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]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user