2022-03-08 10:13:27 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Syntriax.Modules.Movement.State;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Movement
|
|
|
|
{
|
|
|
|
public class MovementController : MonoBehaviour, IMovementController
|
|
|
|
{
|
2022-03-10 23:18:11 +03:00
|
|
|
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;
|
2022-03-08 10:13:27 +03:00
|
|
|
|
|
|
|
protected IToggleState toggleState = null;
|
|
|
|
|
2022-03-10 23:18:11 +03:00
|
|
|
protected virtual void Awake()
|
|
|
|
{
|
|
|
|
OnMovementChanged = new UnityEvent<IMovement>();
|
|
|
|
Movements = new List<IMovement>(32);
|
|
|
|
}
|
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
protected virtual void Start()
|
|
|
|
{
|
|
|
|
toggleState = GetComponent<IToggleState>();
|
2022-03-08 21:28:52 +03:00
|
|
|
RecacheMovements();
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (!toggleState.Toggled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ActiveMovement.ApplyMovement();
|
|
|
|
}
|
|
|
|
|
2022-03-08 21:28:52 +03:00
|
|
|
public virtual void RecacheMovements()
|
2022-03-08 10:13:27 +03:00
|
|
|
{
|
2022-03-10 23:18:11 +03:00
|
|
|
foreach (IMovement movement in Movements)
|
2022-03-08 10:13:27 +03:00
|
|
|
movement.OnTakeOverStateChanged.RemoveListener(OnTakeOverListener);
|
|
|
|
|
2022-03-10 23:18:11 +03:00
|
|
|
Movements.Clear();
|
|
|
|
GetComponents<IMovement>(Movements);
|
2022-03-08 10:13:27 +03:00
|
|
|
UpdateActiveMovement();
|
|
|
|
|
2022-03-10 23:18:11 +03:00
|
|
|
foreach (IMovement movement in Movements)
|
2022-03-08 10:13:27 +03:00
|
|
|
movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTakeOverListener(bool arg0) => UpdateActiveMovement();
|
|
|
|
|
|
|
|
protected virtual void UpdateActiveMovement()
|
|
|
|
{
|
2022-03-10 23:18:11 +03:00
|
|
|
foreach (IMovement movement in Movements)
|
2022-03-08 10:13:27 +03:00
|
|
|
if (movement.CanTakeOver)
|
|
|
|
{
|
|
|
|
ActiveMovement = movement;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-10 23:18:11 +03:00
|
|
|
ActiveMovement = Movements[Movements.Count - 1];
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|