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

8
Runtime/Bases.meta Normal file
View File

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

View File

@@ -0,0 +1,77 @@
using System;
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public abstract class MovementBase : MonoBehaviour, IMovement
{
protected IToggleState toggleState = null;
protected IMovementController movementController = null;
public float BaseSpeed { get; set; } = 1f;
public float MovementMultiplier { get; set; } = 1f;
public Action<bool> OnTakeOverStateChanged { get; set; } = null;
private bool _canTakeOver = false;
public bool CanTakeOver
{
get => _canTakeOver;
protected set
{
if (value == _canTakeOver)
return;
_canTakeOver = value;
OnTakeOverStateChanged?.Invoke(value);
}
}
public IToggleState ToggleState { get; protected set; } = null;
/// <inheritdoc/>
public abstract void ApplyMovement();
public abstract void Move(float x = 0, float y = 0, float z = 0);
/// <summary>
/// Called when this <see cref="IMovement"/> is activated.
/// </summary>
protected abstract void OnActivated();
/// <summary>
/// Called when this <see cref="IMovement"/> is deactivated.
/// </summary>
protected abstract void OnDeactivated();
protected virtual void Start()
{
toggleState = GetComponent<IToggleState>();
movementController = GetComponent<IMovementController>();
movementController.OnMovementActivated += OnActivated;
movementController.OnMovementDeactivated += OnDeactivated;
}
/// <summary>
/// Called when the <see cref="IMovementController"/> activates one of it's <see cref="IMovement"/>s.
/// </summary>
private void OnActivated(IMovement movement)
{
if ((object)movement != this)
return;
OnActivated();
}
/// <summary>
/// Called when the <see cref="IMovementController"/> activates one of it's <see cref="IMovement"/>s.
/// </summary>
private void OnDeactivated(IMovement movement)
{
if ((object)movement != this)
return;
OnDeactivated();
}
}
}

View File

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

View File

@@ -0,0 +1,10 @@
namespace Syntriax.Modules.Movement
{
public abstract class MovementBase1D : MovementBase
{
protected abstract float moveValue { get; set; }
public override void Move(float x = 0, float y = 0, float z = 0)
=> moveValue = x * BaseSpeed * MovementMultiplier;
}
}

View File

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

View File

@@ -0,0 +1,12 @@
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public abstract class MovementBase2D : MovementBase
{
protected abstract Vector2 moveValue { get; set; }
public override void Move(float x = 0, float y = 0, float z = 0)
=> moveValue = new Vector2(x, y) * BaseSpeed * MovementMultiplier;
}
}

View File

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

View File

@@ -0,0 +1,12 @@
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public abstract class MovementBase3D : MovementBase
{
protected abstract Vector3 moveValue { get; set; }
public override void Move(float x = 0, float y = 0, float z = 0)
=> moveValue = new Vector3(x, y, z) * BaseSpeed * MovementMultiplier;
}
}

View File

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

8
Runtime/Config.meta Normal file
View File

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

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:

35
Runtime/IMovement.cs Normal file
View File

@@ -0,0 +1,35 @@
using System;
namespace Syntriax.Modules.Movement
{
public interface IMovement
{
float BaseSpeed { get; set; }
/// <summary>
/// The value will be multiplied with the <see cref="BaseSpeed"/> value
/// </summary>
float MovementMultiplier { get; set; }
/// <summary>
/// If the <see cref="IMovement"/> can be taken over by the <see cref="IMovementController"/>s as the <see cref="IMovementController.ActiveMovement"/>
/// </summary>
bool CanTakeOver { get; }
/// <summary>
/// Called everytime the <see cref="CanTakeOver"/> field is changed
/// </summary>
/// <value>The new value of <see cref="CanTakeOver"/></value>
Action<bool> OnTakeOverStateChanged { get; set; }
/// <summary>
/// Updates the movement data on the <see cref="IMovement"/>
/// </summary>
void Move(float x = 0f, float y = 0f, float z = 0f);
/// <summary>
/// Applies the movement that was passed on with the <see cref="Move"/> method
/// </summary>
void ApplyMovement();
}
}

11
Runtime/IMovement.cs.meta Normal file
View File

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

View File

@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Movement
{
public interface IMovementController
{
/// <summary>
/// Member Toggle State
/// </summary>
IToggleState ToggleState { get; }
/// <summary>
/// Currently active <see cref="IMovement"/>
/// </summary>
IMovement ActiveMovement { get; }
/// <summary>
/// List of all <see cref="IMovement"/>s controlled by this controller, can be updated by calling <see cref="RecacheMovements"/> method
/// </summary>
List<IMovement> Movements { get; }
/// <summary>
/// Called when a <see cref="IMovement"/> is deactivated when another <see cref="IMovement"/> takes over it's place
/// </summary>
/// <value>Deactived <see cref="IMovement"/></value>
Action<IMovement> OnMovementDeactivated { get; set; }
/// <summary>
/// Called when a new <see cref="IMovement"/> takes over by the controller
/// </summary>
/// <value>Actived <see cref="IMovement"/></value>
Action<IMovement> OnMovementActivated { get; set; }
/// <summary>
/// Called when <see cref="Move"/> is called
/// </summary>
/// <value>The x, y, z values of <see cref="Move"/></value>
Action<float, float, float> OnMoveCalled { get; set; }
/// <summary>
/// Updates the <see cref="Movements"/> list
/// </summary>
void RecacheMovements();
/// <summary>
/// Apply the movement data that <see cref="IMovement"/>s will use
/// </summary>
void Move(float x = 0, float y = 0, float z = 0);
}
}

View File

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

View File

@@ -0,0 +1,19 @@
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public static class IMovementControllerExtensions
{
/// <summary>
/// Calls <see cref="IMovementController.Move"/> with a <see cref="Vector2"/>'s values accordingly, leaving the z parameter 0
/// </summary>
public static void Move(this IMovementController movementController, Vector2 moveVector)
=> movementController.Move(moveVector.x, moveVector.y, 0f);
/// <summary>
/// Calls <see cref="IMovementController.Move"/> with a <see cref="Vector3"/>'s values accordingly
/// </summary>
public static void Move(this IMovementController movementController, Vector3 moveVector)
=> movementController.Move(moveVector.x, moveVector.y, moveVector.z);
}
}

View File

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

View File

@@ -0,0 +1,19 @@
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public static class IMovementExtensions
{
/// <summary>
/// Applies a <see cref="Vector2"/> parameter into the <see cref="IMovement"/>'s <see cref="IMovement.Move(x, y, z)"/> method by applying the vector's X and Y values accordingly
/// </summary>
public static void Move(this IMovement movement, Vector2 moveVector)
=> movement.Move(moveVector.x, moveVector.y);
/// <summary>
/// Applies a <see cref="Vector3"/> parameter into the <see cref="IMovement"/>'s <see cref="IMovement.Move(x, y, z)"/> method by applying the vector's X, Y and Z values accordingly
/// </summary>
public static void Move(this IMovement movement, Vector3 moveVector)
=> movement.Move(moveVector.x, moveVector.y, moveVector.z);
}
}

View File

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

View File

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

View File

@@ -0,0 +1,49 @@
using Syntriax.Modules.Trigger;
using UnityEngine;
namespace Syntriax.Modules.Movement.Implementations
{
[RequireComponent(typeof(Rigidbody2D))]
public class AirMovement1D : MovementBase1D
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected Rigidbody2D rigid = null;
protected override void Start()
{
base.Start();
rigid = GetComponent<Rigidbody2D>();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
if (groundTrigger != null)
{
groundTrigger.OnTriggered += OnGroundTrigger;
CanTakeOver = false;
}
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = !isGrounded;
public override void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x += moveValue * Time.fixedDeltaTime;
if (moveValue != 0f)
{
if (Mathf.Abs(velocity.x) > Mathf.Abs(moveValue))
velocity.x = moveValue;
else if (Mathf.Abs(velocity.x - moveValue) > Mathf.Abs(moveValue))
velocity.x += moveValue * Time.fixedDeltaTime;
}
rigid.velocity = velocity;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}

View File

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

View File

@@ -0,0 +1,13 @@
using UnityEngine;
namespace Syntriax.Modules.Movement.Implementations
{
public class DefaultMovement : MovementBase
{
public override void ApplyMovement() { }
public override void Move(float x = 0, float y = 0, float z = 0) { }
protected override void OnActivated()
=> Debug.LogWarning("Default Movement is activated, this movement has no implementation, please add a Movement that you can use as a fallback with it's \"CanTakeOver\" value set to true if this was not intentional");
protected override void OnDeactivated() { }
}
}

View File

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

View File

@@ -0,0 +1,42 @@
using Syntriax.Modules.Trigger;
using UnityEngine;
namespace Syntriax.Modules.Movement.Implementations
{
[RequireComponent(typeof(Rigidbody2D))]
public class GroundMovement1D : MovementBase1D
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected Rigidbody2D rigid = null;
private void Awake() => CanTakeOver = true;
protected override void Start()
{
base.Start();
rigid = GetComponent<Rigidbody2D>();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
if (groundTrigger != null)
{
groundTrigger.OnTriggered += OnGroundTrigger;
CanTakeOver = false;
}
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = isGrounded;
public override void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x = moveValue;
rigid.velocity = velocity;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}

View File

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

View File

@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public class MovementController : MonoBehaviour, IMovementController
{
public Action<float, float, float> OnMoveCalled { get; set; } = null;
public Action<IMovement> OnMovementDeactivated { get; set; } = null;
public Action<IMovement> OnMovementActivated { get; set; } = null;
private IMovement _activeMovement = null;
public IMovement ActiveMovement
{
get => _activeMovement;
protected set
{
if (_activeMovement == value)
return;
IMovement oldMovement = _activeMovement;
_activeMovement = value;
if (oldMovement != null)
OnMovementDeactivated?.Invoke(oldMovement);
OnMovementActivated?.Invoke(value);
}
}
private bool areBothToggleStatesToggled => ToggleState.IsToggledNullChecked() && toggleStateOnGameObject.IsToggledNullChecked();
public List<IMovement> Movements { get; protected set; } = new List<IMovement>(32);
public IToggleState ToggleState { get; protected set; } = new ToggleStateMember(true);
protected IToggleState toggleStateOnGameObject = null;
protected virtual void Start()
{
if (GetComponent<Implementations.DefaultMovement>() == null)
gameObject.AddComponent<Implementations.DefaultMovement>();
RecacheMovements();
toggleStateOnGameObject = GetComponent<IToggleState>();
toggleStateOnGameObject.OnToggleStateChanged += (_) => InvokeOnMoveAction();
ToggleState.OnToggleStateChanged += (_) => InvokeOnMoveAction();
}
protected virtual void FixedUpdate()
{
if (!areBothToggleStatesToggled)
return;
ActiveMovement?.ApplyMovement();
}
public virtual void RecacheMovements()
{
foreach (IMovement movement in Movements)
movement.OnTakeOverStateChanged -= OnTakeOver;
Movements.Clear();
GetComponents<IMovement>(Movements);
UpdateActiveMovement();
foreach (IMovement movement in Movements)
movement.OnTakeOverStateChanged += OnTakeOver;
}
private void OnTakeOver(bool arg0) => UpdateActiveMovement();
protected virtual void UpdateActiveMovement()
{
foreach (IMovement movement in Movements)
if (movement.CanTakeOver)
{
ActiveMovement = movement;
return;
}
}
private Vector3 lastMove = Vector3.zero;
public void Move(float x = 0, float y = 0, float z = 0)
{
ActiveMovement?.Move(x, y, z);
(lastMove.x, lastMove.y, lastMove.z) = (x, y, z);
InvokeOnMoveAction();
}
private void InvokeOnMoveAction()
{
if (!areBothToggleStatesToggled)
return;
OnMoveCalled?.Invoke(lastMove.x, lastMove.y, lastMove.z);
}
}
}

View File

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

View File

@@ -0,0 +1,74 @@
using System;
using Syntriax.Modules.Factory;
using Syntriax.Modules.Movement.Config;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public class MovementDefinitionApplier : MonoBehaviour
{
[Tooltip("The definition's name as defined in it's \"Name\" field")]
[SerializeField] private string startupDefinitionName = "";
private string appliedDefinitionName = "";
public Action<GameObject> OnMovementDefinitionApplied = null;
public Action<GameObject> OnMovementDefinitionRemoved = null;
private void Awake()
{
if (string.IsNullOrWhiteSpace(startupDefinitionName))
return;
LoadDefinition(startupDefinitionName);
}
public void LoadDefinition(string definitionName)
{
RemoveDefinition();
FactorySingleton<MovementDefinitionFactory>.Instance.ApplyDefinitionToGameObject(gameObject, definitionName);
appliedDefinitionName = definitionName;
OnMovementDefinitionApplied?.Invoke(gameObject);
}
public void LoadDefinition(MovementDefinition definition)
{
RemoveDefinition();
FactorySingleton<MovementDefinitionFactory>.Instance.ApplyDefinitionToGameObject(gameObject, definition);
appliedDefinitionName = definition.Name;
OnMovementDefinitionApplied?.Invoke(gameObject);
}
public void RemoveDefinition()
{
if (string.IsNullOrWhiteSpace(appliedDefinitionName))
return;
MovementDefinition appliedDefinition = FactorySingleton<MovementDefinitionFactory>.Instance.Definitions[appliedDefinitionName];
if (appliedDefinition.MovementConfigs == null || appliedDefinition.MonoBehaviours == null)
return;
foreach (MovementConfig movementConfig in appliedDefinition.MovementConfigs)
{
Type type = FactorySingleton<MovementFactory>.Instance.Types[movementConfig.TypeName];
if (TryGetComponent(type, out Component component))
Destroy(component);
}
foreach (string monoBehaviour in appliedDefinition.MonoBehaviours)
{
Type type = FactorySingleton<MovementFactory>.Instance.Types[monoBehaviour];
if (TryGetComponent(type, out Component component))
Destroy(component);
}
appliedDefinition = default;
OnMovementDefinitionRemoved?.Invoke(gameObject);
}
[ContextMenu("Load Longer")] private void LoadLonger() => LoadDefinition("Example Movement Definition (Longer)");
[ContextMenu("Load Quick")] private void LoadQuick() => LoadDefinition("Example Movement Definition (Quick)");
[ContextMenu("Remove Definition")] private void UnloadDefinition() => RemoveDefinition();
}
}

View File

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

View File

@@ -0,0 +1,18 @@
{
"name": "Syntriax.Modules.Movement",
"rootNamespace": "",
"references": [
"GUID:efa9a9bc94c60c74684aafb7428fbf61",
"GUID:1f5f15fe7e49bdb48a76c5ce9b1c9f2f",
"GUID:d4c952ed5f59c5a449cda1b0080ed841"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c967acc4be781ca44b42a1887eb1ac7a
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: