Added Movements List to the IMovementController + Code Improvement

This commit is contained in:
Syntriax 2022-03-10 23:18:11 +03:00
parent e531bdf77e
commit a991c05fad
4 changed files with 65 additions and 34 deletions

View File

@ -3,17 +3,32 @@ using Syntriax.Modules.Movement.State;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
{ {
public class MovementController : MonoBehaviour, IMovementController public class MovementController : MonoBehaviour, IMovementController
{ {
public IMovement ActiveMovement { get; protected set; } = null; public UnityEvent<IMovement> OnMovementChanged { get; protected set; } = null;
public UnityEvent<IMovement> OnMovementChanged { get; protected set; } = new UnityEvent<IMovement>(); private IMovement _activeMovement = null;
public IMovement ActiveMovement
{
get => _activeMovement;
protected set
{
_activeMovement = value;
OnMovementChanged.Invoke(value);
}
}
public List<IMovement> Movements { get; protected set; } = null;
protected List<IMovement> movements = new List<IMovement>(32);
protected IToggleState toggleState = null; protected IToggleState toggleState = null;
protected virtual void Awake()
{
OnMovementChanged = new UnityEvent<IMovement>();
Movements = new List<IMovement>(32);
}
protected virtual void Start() protected virtual void Start()
{ {
toggleState = GetComponent<IToggleState>(); toggleState = GetComponent<IToggleState>();
@ -30,14 +45,14 @@ namespace Syntriax.Modules.Movement
public virtual void RecacheMovements() public virtual void RecacheMovements()
{ {
foreach (IMovement movement in movements) foreach (IMovement movement in Movements)
movement.OnTakeOverStateChanged.RemoveListener(OnTakeOverListener); movement.OnTakeOverStateChanged.RemoveListener(OnTakeOverListener);
movements.Clear(); Movements.Clear();
GetComponents<IMovement>(movements); GetComponents<IMovement>(Movements);
UpdateActiveMovement(); UpdateActiveMovement();
foreach (IMovement movement in movements) foreach (IMovement movement in Movements)
movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener); movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener);
} }
@ -46,16 +61,14 @@ namespace Syntriax.Modules.Movement
protected virtual void UpdateActiveMovement() protected virtual void UpdateActiveMovement()
{ {
foreach (IMovement movement in movements) foreach (IMovement movement in Movements)
if (movement.CanTakeOver) if (movement.CanTakeOver)
{ {
ActiveMovement = movement; ActiveMovement = movement;
OnMovementChanged.Invoke(ActiveMovement);
return; return;
} }
ActiveMovement = movements[movements.Count - 1]; ActiveMovement = Movements[Movements.Count - 1];
OnMovementChanged.Invoke(ActiveMovement);
} }
} }
} }

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine.Events; using UnityEngine.Events;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
@ -5,6 +6,7 @@ namespace Syntriax.Modules.Movement
public interface IMovementController public interface IMovementController
{ {
IMovement ActiveMovement { get; } IMovement ActiveMovement { get; }
List<IMovement> Movements { get; }
UnityEvent<IMovement> OnMovementChanged { get; } UnityEvent<IMovement> OnMovementChanged { get; }
void RecacheMovements(); void RecacheMovements();

View File

@ -8,56 +8,64 @@ namespace Syntriax.Modules.Movement.SpecialAction
public class PlatformerJump : MonoBehaviour, ISpecialActionActivate, ISpecialActionDeactivate public class PlatformerJump : MonoBehaviour, ISpecialActionActivate, ISpecialActionDeactivate
{ {
public float JumpSpeed { get; set; } = 0f; 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; private float fallMultiplier = 0f;
public float FallMultiplier
{
get => fallMultiplier;
set => fallMultiplier = value * Time.fixedDeltaTime;
}
private float suspensionMultiplier = 0f; private float suspensionMultiplier = 0f;
public float SuspensionMultiplier
{
get => suspensionMultiplier;
set => suspensionMultiplier = value * Time.fixedDeltaTime;
}
private IGroundTrigger groundCheck = null; protected bool airSuspension = false;
private Rigidbody2D rigid = null; protected IGroundTrigger groundCheck = null;
private IToggleState toggleState = null; protected Rigidbody2D rigid = null;
protected IToggleState toggleState = null;
protected virtual void Awake()
private void Awake()
{ {
JumpSpeed = 10f; JumpSpeed = 10f;
fallMultiplier = 1.5f * Time.fixedDeltaTime; FallMultiplier = 1.5f;
suspensionMultiplier = 1f * Time.fixedDeltaTime; SuspensionMultiplier = 1f;
EnabledToggleState = new MemberToggleState(); EnabledToggleState = new MemberToggleState();
} }
private void Start() protected virtual void Start()
{ {
rigid = GetComponent<Rigidbody2D>(); rigid = GetComponent<Rigidbody2D>();
groundCheck = GetComponentInChildren<IGroundTrigger>(); groundCheck = GetComponentInChildren<IGroundTrigger>();
toggleState = GetComponent<IToggleState>(); toggleState = GetComponent<IToggleState>();
} }
private void FixedUpdate() protected virtual void FixedUpdate()
{ {
if (!EnabledToggleState.Toggled) if (!EnabledToggleState.Toggled)
return; return;
if (rigid.velocity.y < 0f) if (rigid.velocity.y < FallThreshold)
ApplySuspension(suspensionMultiplier); ApplySuspension(SuspensionMultiplier);
else if (!airSuspension) else if (!airSuspension)
ApplySuspension(fallMultiplier); ApplySuspension(FallMultiplier);
} }
private void ApplySuspension(float multiplier) protected virtual void ApplySuspension(float multiplier)
{ => rigid.velocity += Physics2D.gravity * multiplier;
rigid.velocity += Physics2D.gravity * multiplier;
}
private void Jump() protected virtual void Jump()
{ {
Vector2 velocity = rigid.velocity; Vector2 velocity = rigid.velocity;
velocity.y = JumpSpeed; velocity.y = JumpSpeed;
rigid.velocity = velocity; rigid.velocity = velocity;
} }
public void Activate() public virtual void Activate()
{ {
if (!toggleState.Toggled) if (!toggleState.Toggled)
return; return;
@ -68,7 +76,7 @@ namespace Syntriax.Modules.Movement.SpecialAction
airSuspension = true; airSuspension = true;
} }
public void Deactivate() public virtual void Deactivate()
{ {
if (!toggleState.Toggled) if (!toggleState.Toggled)
return; return;

View File

@ -24,6 +24,7 @@ namespace Syntriax.Modules.Movement.VariableMovement
} }
} }
protected bool isSet = false;
protected IMovementController movementController = null; protected IMovementController movementController = null;
protected IVariableMovement defaultVariableMovement = null; protected IVariableMovement defaultVariableMovement = null;
protected List<IVariableMovementWithState> variableMovementsWithState = null; protected List<IVariableMovementWithState> variableMovementsWithState = null;
@ -43,7 +44,13 @@ namespace Syntriax.Modules.Movement.VariableMovement
} }
protected void SetMultiplierToActiveMovement(IMovement currentMovement) protected void SetMultiplierToActiveMovement(IMovement currentMovement)
=> currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier; {
if (!isSet)
return;
currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier;
}
public void LoadVariableMovementCollection(VMCollection collection) 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!"); throw new System.NullReferenceException("InputActionCollection must be set on the VariableMovementController to load a collection!");
this.collection = collection; this.collection = collection;
isSet = true;
UnsubscribeFromMovements(); UnsubscribeFromMovements();