Unity Package Structure

This commit is contained in:
2022-12-15 19:24:01 +03:00
parent f84502ceb4
commit 36ec79c262
43 changed files with 44 additions and 3 deletions

View File

@@ -0,0 +1,17 @@
using System;
namespace Syntriax.Modules.Movement.Config
{
[Serializable]
public struct MovementConfig
{
public string TypeName;
public float BaseSpeed;
public MovementConfig(string typeName, float baseSpeed)
{
TypeName = typeName;
BaseSpeed = baseSpeed;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 15e372dde7d46d24e88ccb953b763fe4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
using System;
namespace Syntriax.Modules.Movement.Config
{
[Serializable]
public struct MovementDefinition
{
public string Name;
public MovementConfig[] MovementConfigs;
public string[] MonoBehaviours;
public MovementDefinition(string name, MovementConfig[] movementConfigs, string[] monoBehaviours)
{
Name = name;
MovementConfigs = movementConfigs;
MonoBehaviours = monoBehaviours;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: be60b5c0520c0c144b5debdd609db0ce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,126 @@
using System.Collections.Generic;
using Syntriax.Modules.Factory;
using UnityEngine;
namespace Syntriax.Modules.Movement.Config
{
/// <summary>
/// The Singleton factory responsible for loading and applying <see cref="MovementDefinition"/>s,
/// </summary>
/// <remarks>
/// <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 : FactoryBase
{
public const int InitialCapacity = 64;
public const string ResourceDirectoryToDefinitions = "Modules/Syntriax/Movement/Definitions/";
private Dictionary<string, MovementDefinition> _definitions = null;
public Dictionary<string, MovementDefinition> Definitions
{
get
{
if (!IsInitialized)
Initialize();
return _definitions;
}
}
private void Start()
{
if (FactorySingleton<MovementDefinitionFactory>.Instance == this)
return;
Destroy(this);
}
/// <summary>
/// Initializes the factory, if already initialized reinitializes
/// </summary>
/// <remarks>
/// <para> Automatically loads the definition files specificed path <see cref="ResourceDirectoryToDefinitions"/></para>
/// <para> The default is "Resources/Modules/Syntriax/Movement/Definitions/"</para>
/// </remarks>
protected override void OnFactoryInitialize()
{
if (_definitions == null)
_definitions = new Dictionary<string, MovementDefinition>(InitialCapacity);
foreach (TextAsset definitionTextAsset in UnityEngine.Resources.LoadAll<TextAsset>(ResourceDirectoryToDefinitions))
AddToFactoryWithJSON(definitionTextAsset.text);
}
/// <summary>
/// Clears all the definitions in the factory
/// </summary>
/// <remarks>
/// Does not reinitialize, please call <see cref="Initialize"/> to initialize it back
/// </remarks>
protected override void OnFactoryReset() => _definitions?.Clear();
/// <summary>
/// Manually registers a <see cref="MovementDefinition"/> to the factory with a JSON <see cref="string"/>
/// </summary>
/// <param name="definitionJSONText">A <see cref="string"/> of a <see cref="MovementDefinition"/> in JSON</param>
public void AddToFactoryWithJSON(string definitionJSONText)
{
MovementDefinition movementDefinition = JsonUtility.FromJson<MovementDefinition>(definitionJSONText);
AddDefinitionToFactory(movementDefinition);
}
/// <summary>
/// Registers a list of <see cref="MovementDefinition"/>s to the factory
/// </summary>
public void AddDefinitionsToFactory(List<MovementDefinition> movementDefinitions)
{
foreach (MovementDefinition movementDefinition in movementDefinitions)
AddDefinitionToFactory(movementDefinition);
}
/// <summary>
/// Registers a single <see cref="MovementDefinition"/> to the factory
/// </summary>
public void AddDefinitionToFactory(MovementDefinition movementDefinition)
{
if (Definitions.ContainsKey(movementDefinition.Name))
throw new System.ArgumentException($"{movementDefinition.Name} is already in the Movement Definition Factory");
Definitions.Add(movementDefinition.Name, movementDefinition);
}
/// <summary>
/// Searches for the <see cref="MovementDefinition"/> in the factory and applies it to the <see cref="GameObject"/>
/// </summary>
/// <param name="gameObject"><see cref="GameObject"/> that the <see cref="MovementDefinition"/> will be applied to</param>
/// <param name="definitionName">The registered <see cref="MovementDefinition"/>'s name that will be applied to the provided <see cref="GameObject"/></param>
/// <exception cref="System.ArgumentException">When the provided <see cref="MovementDefinition"/> name is not registered in the factory</exception>
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 {nameof(MovementDefinitionFactory)}");
ApplyDefinitionToGameObject(gameObject, Definitions[definitionName]);
}
/// <summary>
/// Applies the provided <see cref="MovementDefinition"/> to the <see cref="GameObject"/>
/// </summary>
/// <param name="gameObject"><see cref="GameObject"/> that the <see cref="MovementDefinition"/> will be applied to</param>
/// <param name="definition">The <see cref="MovementDefinition"/> that will be applied to the provided <see cref="GameObject"/></param>
public void ApplyDefinitionToGameObject(GameObject gameObject, MovementDefinition definition)
{
foreach (MovementConfig movementConfig in definition.MovementConfigs)
{
IMovement movement = (IMovement)FactorySingleton<MovementFactory>.Instance.AddToGameObject(gameObject, movementConfig.TypeName);
movement.BaseSpeed = movementConfig.BaseSpeed;
}
if (definition.MonoBehaviours != null)
foreach (string monoBehaviours in definition.MonoBehaviours)
FactorySingleton<MovementFactory>.Instance.AddToGameObject(gameObject, monoBehaviours);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 932063af5c72ad340818b9e75debbc48
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,61 @@
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 : FactoryBase
{
private const int InitialCapacity = 64;
private Dictionary<string, Type> _types = null;
public Dictionary<string, Type> Types
{
get
{
if (!IsInitialized)
Initialize();
return _types;
}
}
private Func<Type, bool> predicate = type => !type.IsAbstract && type.IsSubclassOf(typeof(MonoBehaviour));
private void Start()
{
if (FactorySingleton<MovementFactory>.Instance == this)
return;
Destroy(this);
}
protected override void OnFactoryInitialize()
{
if (_types == null)
_types = new Dictionary<string, Type>(InitialCapacity);
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
AddAssemblyToFactory(assembly);
}
protected override void OnFactoryReset() => _types?.Clear();
public void AddAssemblyToFactory(Assembly assembly)
{
foreach (Type type in assembly.GetTypes().Where(predicate))
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 {nameof(MovementFactory)}");
return gameObject.AddComponent(Types[name]);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1613a55f404a46542870625c4407ffa7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: