Added OnMovement Activated&Deactivated to the IMovementController

This commit is contained in:
Syntriax 2022-03-13 20:59:45 +03:00
parent a991c05fad
commit e35cdcb069
7 changed files with 54 additions and 6 deletions

View File

@ -33,5 +33,8 @@ namespace Syntriax.Modules.Movement
rigid.velocity = velocity;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}

View File

@ -24,5 +24,8 @@ namespace Syntriax.Modules.Movement
velocity.x = moveValue;
rigid.velocity = velocity;
}
protected override void OnDeactivated() { }
protected override void OnActivated() { }
}
}

View File

@ -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<Rigidbody2D>();
toggleState = GetComponent<ToggleState>();
movementController = GetComponent<IMovementController>();
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);

View File

@ -7,7 +7,9 @@ namespace Syntriax.Modules.Movement
{
IMovement ActiveMovement { get; }
List<IMovement> Movements { get; }
UnityEvent<IMovement> OnMovementChanged { get; }
UnityEvent<IMovement> OnMovementDeactivated { get; }
UnityEvent<IMovement> OnMovementActivated { get; }
void RecacheMovements();
}

View File

@ -7,25 +7,35 @@ namespace Syntriax.Modules.Movement
{
public class MovementController : MonoBehaviour, IMovementController
{
public UnityEvent<IMovement> OnMovementChanged { get; protected set; } = null;
public UnityEvent<IMovement> OnMovementDeactivated { get; protected set; } = null;
public UnityEvent<IMovement> 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<IMovement> Movements { get; protected set; } = null;
protected IToggleState toggleState = null;
protected virtual void Awake()
{
OnMovementChanged = new UnityEvent<IMovement>();
OnMovementDeactivated = new UnityEvent<IMovement>();
OnMovementActivated = new UnityEvent<IMovement>();
Movements = new List<IMovement>(32);
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f9a57082945be29498d2785f8be6484d
guid: e5a74cc1b761aae448c854eb652fcc13
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -40,7 +40,16 @@ namespace Syntriax.Modules.Movement.VariableMovement
protected virtual void Start()
{
movementController = GetComponent<IMovementController>();
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)