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)
|
2022-11-14 14:22:18 +03:00
|
|
|
OnMovementDeactivated?.Invoke(oldMovement);
|
|
|
|
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()
|
2022-11-14 14:22:18 +03:00
|
|
|
=> Movements = new List<IMovement>(32);
|
2022-03-10 23:18:11 +03:00
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
protected virtual void Start()
|
|
|
|
{
|
2022-11-15 16:26:26 +03:00
|
|
|
if (GetComponent<Implementations.DefaultMovement>() == null)
|
|
|
|
gameObject.AddComponent<Implementations.DefaultMovement>();
|
|
|
|
|
2022-03-08 21:28:52 +03:00
|
|
|
RecacheMovements();
|
2022-11-14 14:22:18 +03:00
|
|
|
toggleState = GetComponent<IToggleState>();
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void FixedUpdate()
|
|
|
|
{
|
2022-11-14 14:22:18 +03:00
|
|
|
if (!toggleState.IsToggledNullChecked())
|
2022-03-08 10:13:27 +03:00
|
|
|
return;
|
|
|
|
|
2022-11-14 14:22:18 +03:00
|
|
|
ActiveMovement?.ApplyMovement();
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
|
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-14 14:22:18 +03:00
|
|
|
movement.OnTakeOverStateChanged -= OnTakeOver;
|
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-14 14:22:18 +03:00
|
|
|
movement.OnTakeOverStateChanged += OnTakeOver;
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
|
2022-11-14 14:22:18 +03:00
|
|
|
private void OnTakeOver(bool arg0) => UpdateActiveMovement();
|
2022-03-08 10:13:27 +03:00
|
|
|
|
|
|
|
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-11-15 13:08:37 +03:00
|
|
|
|
|
|
|
public void Move(float x = 0, float y = 0, float z = 0)
|
|
|
|
=> ActiveMovement?.Move(x, y, z);
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
}
|