2022-11-13 18:07:54 +03:00
|
|
|
using System;
|
2022-03-08 10:13:27 +03:00
|
|
|
using System.Collections.Generic;
|
2022-11-14 13:04:09 +03:00
|
|
|
using Syntriax.Modules.ToggleState;
|
2022-03-08 10:13:27 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Movement
|
|
|
|
{
|
|
|
|
public class MovementController : MonoBehaviour, IMovementController
|
|
|
|
{
|
2022-11-13 18:07:54 +03:00
|
|
|
public Action<IMovement> OnMovementDeactivated { get; set; } = null;
|
|
|
|
public Action<IMovement> OnMovementActivated { get; set; } = null;
|
2022-03-13 20:59:45 +03:00
|
|
|
|
2022-03-10 23:18:11 +03:00
|
|
|
private IMovement _activeMovement = null;
|
|
|
|
public IMovement ActiveMovement
|
|
|
|
{
|
|
|
|
get => _activeMovement;
|
|
|
|
protected set
|
|
|
|
{
|
2022-03-13 20:59:45 +03:00
|
|
|
if (_activeMovement == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IMovement oldMovement = _activeMovement;
|
|
|
|
|
2022-03-10 23:18:11 +03:00
|
|
|
_activeMovement = value;
|
2022-03-16 13:56:32 +03:00
|
|
|
|
|
|
|
if (oldMovement != null)
|
|
|
|
OnMovementDeactivated.Invoke(oldMovement);
|
2022-03-13 20:59:45 +03:00
|
|
|
OnMovementActivated.Invoke(value);
|
2022-03-10 23:18:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
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-11-13 18:07:54 +03:00
|
|
|
movement.OnTakeOverStateChanged -= OnTakeOverListener;
|
2022-03-08 10:13:27 +03:00
|
|
|
|
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-11-13 18:07:54 +03:00
|
|
|
movement.OnTakeOverStateChanged += OnTakeOverListener;
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|