From e35cdcb0692b5b442c2c52cd77c4266bd270a13b Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 13 Mar 2022 20:59:45 +0300 Subject: [PATCH] Added OnMovement Activated&Deactivated to the IMovementController --- 2D/OneWay2DAirMovement.cs | 3 +++ 2D/OneWay2DGroundMovement.cs | 3 +++ 2D/TwoDimensionalMovementBase.cs | 21 +++++++++++++++++++ IMovementController.cs | 4 +++- ...mentController.cs => MovementController.cs | 16 +++++++++++--- ...ller.cs.meta => MovementController.cs.meta | 2 +- .../VariableMovementController.cs | 11 +++++++++- 7 files changed, 54 insertions(+), 6 deletions(-) rename 2D/MovementController.cs => MovementController.cs (77%) rename 2D/MovementController.cs.meta => MovementController.cs.meta (83%) diff --git a/2D/OneWay2DAirMovement.cs b/2D/OneWay2DAirMovement.cs index 0e2126d..165531d 100644 --- a/2D/OneWay2DAirMovement.cs +++ b/2D/OneWay2DAirMovement.cs @@ -33,5 +33,8 @@ namespace Syntriax.Modules.Movement rigid.velocity = velocity; } + + protected override void OnDeactivated() { } + protected override void OnActivated() { } } } diff --git a/2D/OneWay2DGroundMovement.cs b/2D/OneWay2DGroundMovement.cs index 064a92e..28ba3ef 100644 --- a/2D/OneWay2DGroundMovement.cs +++ b/2D/OneWay2DGroundMovement.cs @@ -24,5 +24,8 @@ namespace Syntriax.Modules.Movement velocity.x = moveValue; rigid.velocity = velocity; } + + protected override void OnDeactivated() { } + protected override void OnActivated() { } } } diff --git a/2D/TwoDimensionalMovementBase.cs b/2D/TwoDimensionalMovementBase.cs index 963d0c2..03f05b2 100644 --- a/2D/TwoDimensionalMovementBase.cs +++ b/2D/TwoDimensionalMovementBase.cs @@ -9,6 +9,7 @@ namespace Syntriax.Modules.Movement { protected Rigidbody2D rigid = null; protected IToggleState toggleState = null; + protected IMovementController movementController = null; private bool _canTakeOver = false; public float BaseSpeed { get; set; } = 1f; @@ -40,6 +41,26 @@ namespace Syntriax.Modules.Movement { rigid = GetComponent(); toggleState = GetComponent(); + movementController = GetComponent(); + + movementController.OnMovementActivated.AddListener(OnActivated); + movementController.OnMovementDeactivated.AddListener(OnDeactivated); + } + + protected abstract void OnDeactivated(); + private void OnDeactivated(IMovement movement) + { + if ((object)movement != this) + return; + OnDeactivated(); + } + + protected abstract void OnActivated(); + private void OnActivated(IMovement movement) + { + if ((object)movement != this) + return; + OnActivated(); } public abstract void Move(float x = 0, float y = 0, float z = 0); diff --git a/IMovementController.cs b/IMovementController.cs index e69e942..28cbdf2 100644 --- a/IMovementController.cs +++ b/IMovementController.cs @@ -7,7 +7,9 @@ namespace Syntriax.Modules.Movement { IMovement ActiveMovement { get; } List Movements { get; } - UnityEvent OnMovementChanged { get; } + + UnityEvent OnMovementDeactivated { get; } + UnityEvent OnMovementActivated { get; } void RecacheMovements(); } diff --git a/2D/MovementController.cs b/MovementController.cs similarity index 77% rename from 2D/MovementController.cs rename to MovementController.cs index 94b3274..6df7094 100644 --- a/2D/MovementController.cs +++ b/MovementController.cs @@ -7,25 +7,35 @@ namespace Syntriax.Modules.Movement { public class MovementController : MonoBehaviour, IMovementController { - public UnityEvent OnMovementChanged { get; protected set; } = null; + public UnityEvent OnMovementDeactivated { get; protected set; } = null; + public UnityEvent OnMovementActivated { get; protected set; } = null; + private IMovement _activeMovement = null; public IMovement ActiveMovement { get => _activeMovement; protected set { + if (_activeMovement == value) + return; + + IMovement oldMovement = _activeMovement; + _activeMovement = value; - OnMovementChanged.Invoke(value); + OnMovementDeactivated.Invoke(oldMovement); + OnMovementActivated.Invoke(value); } } public List Movements { get; protected set; } = null; + protected IToggleState toggleState = null; protected virtual void Awake() { - OnMovementChanged = new UnityEvent(); + OnMovementDeactivated = new UnityEvent(); + OnMovementActivated = new UnityEvent(); Movements = new List(32); } diff --git a/2D/MovementController.cs.meta b/MovementController.cs.meta similarity index 83% rename from 2D/MovementController.cs.meta rename to MovementController.cs.meta index 867aca0..28e8669 100644 --- a/2D/MovementController.cs.meta +++ b/MovementController.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: f9a57082945be29498d2785f8be6484d +guid: e5a74cc1b761aae448c854eb652fcc13 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/VariableMovement/VariableMovementController.cs b/VariableMovement/VariableMovementController.cs index 0825720..551e9f1 100644 --- a/VariableMovement/VariableMovementController.cs +++ b/VariableMovement/VariableMovementController.cs @@ -40,7 +40,16 @@ namespace Syntriax.Modules.Movement.VariableMovement protected virtual void Start() { movementController = GetComponent(); - movementController.OnMovementChanged.AddListener(SetMultiplierToActiveMovement); + movementController.OnMovementActivated.AddListener(SetMultiplierToActiveMovement); + movementController.OnMovementDeactivated.AddListener(ResetMultiplierToDefault); + } + + private void ResetMultiplierToDefault(IMovement currentMovement) + { + if (!isSet) + return; + + currentMovement.MovementMultiplier = defaultVariableMovement.Asset.Multiplier; } protected void SetMultiplierToActiveMovement(IMovement currentMovement)