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.Events;
namespace Syntriax.Modules.Movement
{
public class MovementController : MonoBehaviour, IMovementController
{
public IMovement ActiveMovement { get; protected set; } = null;
public UnityEvent<IMovement> OnMovementChanged { get; protected set; } = new UnityEvent<IMovement>();
public UnityEvent<IMovement> OnMovementChanged { get; protected set; } = null;
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 virtual void Awake()
{
OnMovementChanged = new UnityEvent<IMovement>();
Movements = new List<IMovement>(32);
}
protected virtual void Start()
{
toggleState = GetComponent<IToggleState>();
@ -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<IMovement>(movements);
Movements.Clear();
GetComponents<IMovement>(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];
}
}
}

View File

@ -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<IMovement> Movements { get; }
UnityEvent<IMovement> OnMovementChanged { get; }
void RecacheMovements();

View File

@ -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<Rigidbody2D>();
groundCheck = GetComponentInChildren<IGroundTrigger>();
toggleState = GetComponent<IToggleState>();
}
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;

View File

@ -24,6 +24,7 @@ namespace Syntriax.Modules.Movement.VariableMovement
}
}
protected bool isSet = false;
protected IMovementController movementController = null;
protected IVariableMovement defaultVariableMovement = null;
protected List<IVariableMovementWithState> 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();