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-12-01 23:30:02 +03:00
|
|
|
public Action<float, float, float> OnMoveCalled { get; set; } = null;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 11:59:08 +03:00
|
|
|
private bool areBothToggleStatesToggled => ToggleStateMember.IsToggledNullChecked() && toggleStateOnGameObject.IsToggledNullChecked();
|
2022-12-02 00:02:39 +03:00
|
|
|
|
2022-12-01 23:30:02 +03:00
|
|
|
public List<IMovement> Movements { get; protected set; } = new List<IMovement>(32);
|
2022-03-08 10:13:27 +03:00
|
|
|
|
2022-12-17 11:59:08 +03:00
|
|
|
public IToggleState ToggleStateMember { get; protected set; } = new ToggleStateMember(true);
|
2022-03-08 10:13:27 +03:00
|
|
|
|
2022-12-01 23:30:02 +03:00
|
|
|
protected IToggleState toggleStateOnGameObject = null;
|
2022-03-10 23:18:11 +03:00
|
|
|
|
2022-03-08 10:13:27 +03:00
|
|
|
protected virtual void Start()
|
|
|
|
{
|
2022-12-17 12:04:14 +03:00
|
|
|
if (GetComponent<DefaultMovement>() == null)
|
|
|
|
gameObject.AddComponent<DefaultMovement>();
|
2022-11-15 16:26:26 +03:00
|
|
|
|
2022-03-08 21:28:52 +03:00
|
|
|
RecacheMovements();
|
2022-12-01 23:30:02 +03:00
|
|
|
toggleStateOnGameObject = GetComponent<IToggleState>();
|
2022-12-02 00:02:39 +03:00
|
|
|
|
|
|
|
toggleStateOnGameObject.OnToggleStateChanged += (_) => InvokeOnMoveAction();
|
2022-12-17 11:59:08 +03:00
|
|
|
ToggleStateMember.OnToggleStateChanged += (_) => InvokeOnMoveAction();
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void FixedUpdate()
|
|
|
|
{
|
2022-12-02 00:02:39 +03:00
|
|
|
if (!areBothToggleStatesToggled)
|
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
|
|
|
|
2022-12-02 00:02:39 +03:00
|
|
|
private Vector3 lastMove = Vector3.zero;
|
2022-11-15 13:08:37 +03:00
|
|
|
public void Move(float x = 0, float y = 0, float z = 0)
|
2022-12-01 23:30:02 +03:00
|
|
|
{
|
|
|
|
ActiveMovement?.Move(x, y, z);
|
2022-12-02 00:02:39 +03:00
|
|
|
(lastMove.x, lastMove.y, lastMove.z) = (x, y, z);
|
|
|
|
|
|
|
|
InvokeOnMoveAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void InvokeOnMoveAction()
|
|
|
|
{
|
|
|
|
if (!areBothToggleStatesToggled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
OnMoveCalled?.Invoke(lastMove.x, lastMove.y, lastMove.z);
|
2022-12-01 23:30:02 +03:00
|
|
|
}
|
2022-03-08 10:13:27 +03:00
|
|
|
}
|
|
|
|
}
|