diff --git a/2D/MovementController.cs b/2D/MovementController.cs index 379e037..94b3274 100644 --- a/2D/MovementController.cs +++ b/2D/MovementController.cs @@ -3,17 +3,32 @@ using Syntriax.Modules.Movement.State; using UnityEngine; using UnityEngine.Events; - namespace Syntriax.Modules.Movement { public class MovementController : MonoBehaviour, IMovementController { - public IMovement ActiveMovement { get; protected set; } = null; - public UnityEvent OnMovementChanged { get; protected set; } = new UnityEvent(); + public UnityEvent OnMovementChanged { get; protected set; } = null; + private IMovement _activeMovement = null; + public IMovement ActiveMovement + { + get => _activeMovement; + protected set + { + _activeMovement = value; + OnMovementChanged.Invoke(value); + } + } + + public List Movements { get; protected set; } = null; - protected List movements = new List(32); protected IToggleState toggleState = null; + protected virtual void Awake() + { + OnMovementChanged = new UnityEvent(); + Movements = new List(32); + } + protected virtual void Start() { toggleState = GetComponent(); @@ -30,14 +45,14 @@ namespace Syntriax.Modules.Movement public virtual void RecacheMovements() { - foreach (IMovement movement in movements) + foreach (IMovement movement in Movements) movement.OnTakeOverStateChanged.RemoveListener(OnTakeOverListener); - movements.Clear(); - GetComponents(movements); + Movements.Clear(); + GetComponents(Movements); UpdateActiveMovement(); - foreach (IMovement movement in movements) + foreach (IMovement movement in Movements) movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener); } @@ -46,16 +61,14 @@ namespace Syntriax.Modules.Movement protected virtual void UpdateActiveMovement() { - foreach (IMovement movement in movements) + foreach (IMovement movement in Movements) if (movement.CanTakeOver) { ActiveMovement = movement; - OnMovementChanged.Invoke(ActiveMovement); return; } - ActiveMovement = movements[movements.Count - 1]; - OnMovementChanged.Invoke(ActiveMovement); + ActiveMovement = Movements[Movements.Count - 1]; } } } diff --git a/IMovementController.cs b/IMovementController.cs index b7f7f85..e69e942 100644 --- a/IMovementController.cs +++ b/IMovementController.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using UnityEngine.Events; namespace Syntriax.Modules.Movement @@ -5,6 +6,7 @@ namespace Syntriax.Modules.Movement public interface IMovementController { IMovement ActiveMovement { get; } + List Movements { get; } UnityEvent OnMovementChanged { get; } void RecacheMovements(); diff --git a/SpecialAction/Actions/PlatformerJump.cs b/SpecialAction/Actions/PlatformerJump.cs index a1f4346..e7a34a7 100644 --- a/SpecialAction/Actions/PlatformerJump.cs +++ b/SpecialAction/Actions/PlatformerJump.cs @@ -8,56 +8,64 @@ namespace Syntriax.Modules.Movement.SpecialAction public class PlatformerJump : MonoBehaviour, ISpecialActionActivate, ISpecialActionDeactivate { public float JumpSpeed { get; set; } = 0f; - public IToggleState EnabledToggleState { get; private set; } = null; + public IToggleState EnabledToggleState { get; protected set; } = null; - private bool airSuspension = false; + public float FallThreshold { get; set; } = 0f; private float fallMultiplier = 0f; + public float FallMultiplier + { + get => fallMultiplier; + set => fallMultiplier = value * Time.fixedDeltaTime; + } private float suspensionMultiplier = 0f; + public float SuspensionMultiplier + { + get => suspensionMultiplier; + set => suspensionMultiplier = value * Time.fixedDeltaTime; + } - private IGroundTrigger groundCheck = null; - private Rigidbody2D rigid = null; - private IToggleState toggleState = null; + protected bool airSuspension = false; + protected IGroundTrigger groundCheck = null; + protected Rigidbody2D rigid = null; + protected IToggleState toggleState = null; - - private void Awake() + protected virtual void Awake() { JumpSpeed = 10f; - fallMultiplier = 1.5f * Time.fixedDeltaTime; - suspensionMultiplier = 1f * Time.fixedDeltaTime; + FallMultiplier = 1.5f; + SuspensionMultiplier = 1f; EnabledToggleState = new MemberToggleState(); } - private void Start() + protected virtual void Start() { rigid = GetComponent(); groundCheck = GetComponentInChildren(); toggleState = GetComponent(); } - private void FixedUpdate() + protected virtual void FixedUpdate() { if (!EnabledToggleState.Toggled) return; - if (rigid.velocity.y < 0f) - ApplySuspension(suspensionMultiplier); + if (rigid.velocity.y < FallThreshold) + ApplySuspension(SuspensionMultiplier); else if (!airSuspension) - ApplySuspension(fallMultiplier); + ApplySuspension(FallMultiplier); } - private void ApplySuspension(float multiplier) - { - rigid.velocity += Physics2D.gravity * multiplier; - } + protected virtual void ApplySuspension(float multiplier) + => rigid.velocity += Physics2D.gravity * multiplier; - private void Jump() + protected virtual void Jump() { Vector2 velocity = rigid.velocity; velocity.y = JumpSpeed; rigid.velocity = velocity; } - public void Activate() + public virtual void Activate() { if (!toggleState.Toggled) return; @@ -68,7 +76,7 @@ namespace Syntriax.Modules.Movement.SpecialAction airSuspension = true; } - public void Deactivate() + public virtual void Deactivate() { if (!toggleState.Toggled) return; diff --git a/VariableMovement/VariableMovementController.cs b/VariableMovement/VariableMovementController.cs index 91d0a46..0825720 100644 --- a/VariableMovement/VariableMovementController.cs +++ b/VariableMovement/VariableMovementController.cs @@ -24,6 +24,7 @@ namespace Syntriax.Modules.Movement.VariableMovement } } + protected bool isSet = false; protected IMovementController movementController = null; protected IVariableMovement defaultVariableMovement = null; protected List variableMovementsWithState = null; @@ -43,7 +44,13 @@ namespace Syntriax.Modules.Movement.VariableMovement } protected void SetMultiplierToActiveMovement(IMovement currentMovement) - => currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier; + { + if (!isSet) + return; + + currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier; + + } public void LoadVariableMovementCollection(VMCollection collection) { @@ -51,6 +58,7 @@ namespace Syntriax.Modules.Movement.VariableMovement throw new System.NullReferenceException("InputActionCollection must be set on the VariableMovementController to load a collection!"); this.collection = collection; + isSet = true; UnsubscribeFromMovements();