2022-03-09 21:46:49 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Movement.VariableMovement
|
|
|
|
{
|
|
|
|
public class VariableMovementController : MonoBehaviour, IVariableMovementController
|
|
|
|
{
|
2022-11-13 18:07:54 +03:00
|
|
|
public Action<IVariableMovement> OnVariableMovementChanged { get; set; } = null;
|
2022-03-16 13:56:32 +03:00
|
|
|
private IInputActionCollection _inputActionCollection = null;
|
|
|
|
public IInputActionCollection InputActionCollection
|
|
|
|
{
|
|
|
|
get => _inputActionCollection;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_inputActionCollection = value;
|
|
|
|
|
|
|
|
foreach (IVariableMovementWithState variableMovement in variableMovementsWithState)
|
|
|
|
variableMovement.InputCollection = value;
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 21:46:49 +03:00
|
|
|
private IVariableMovement _activeVariableMovement = null;
|
|
|
|
public IVariableMovement ActiveVariableMovement
|
|
|
|
{
|
|
|
|
get => _activeVariableMovement;
|
|
|
|
protected set
|
|
|
|
{
|
|
|
|
_activeVariableMovement = value;
|
|
|
|
OnVariableMovementChanged.Invoke(value);
|
|
|
|
|
|
|
|
if (movementController != null)
|
|
|
|
SetMultiplierToActiveMovement(movementController.ActiveMovement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 23:18:11 +03:00
|
|
|
protected bool isSet = false;
|
2022-03-09 21:46:49 +03:00
|
|
|
protected IMovementController movementController = null;
|
|
|
|
protected IVariableMovement defaultVariableMovement = null;
|
|
|
|
protected List<IVariableMovementWithState> variableMovementsWithState = null;
|
|
|
|
protected VMCollection collection;
|
|
|
|
|
|
|
|
protected virtual void Awake()
|
|
|
|
{
|
|
|
|
defaultVariableMovement = new VariableMovement();
|
|
|
|
variableMovementsWithState = new List<IVariableMovementWithState>(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Start()
|
|
|
|
{
|
|
|
|
movementController = GetComponent<IMovementController>();
|
2022-11-13 18:07:54 +03:00
|
|
|
movementController.OnMovementActivated += SetMultiplierToActiveMovement;
|
|
|
|
movementController.OnMovementDeactivated += ResetMultiplierToDefault;
|
2022-03-13 20:59:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void ResetMultiplierToDefault(IMovement currentMovement)
|
|
|
|
{
|
|
|
|
if (!isSet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
currentMovement.MovementMultiplier = defaultVariableMovement.Asset.Multiplier;
|
2022-03-09 21:46:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void SetMultiplierToActiveMovement(IMovement currentMovement)
|
2022-03-10 23:18:11 +03:00
|
|
|
{
|
|
|
|
if (!isSet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier;
|
|
|
|
|
|
|
|
}
|
2022-03-09 21:46:49 +03:00
|
|
|
|
|
|
|
public void LoadVariableMovementCollection(VMCollection collection)
|
|
|
|
{
|
|
|
|
this.collection = collection;
|
2022-03-10 23:18:11 +03:00
|
|
|
isSet = true;
|
2022-03-09 21:46:49 +03:00
|
|
|
|
|
|
|
UnsubscribeFromMovements();
|
|
|
|
|
|
|
|
variableMovementsWithState.Clear();
|
|
|
|
foreach (VMAsset asset in collection.VMAssets)
|
|
|
|
variableMovementsWithState.Add(new VariableMovementWithState(asset, InputActionCollection));
|
|
|
|
defaultVariableMovement.Asset = collection.DefaultAsset;
|
|
|
|
|
|
|
|
SubscribeToMovements();
|
|
|
|
UpdateActiveVariableMovement();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void SubscribeToMovements()
|
|
|
|
{
|
|
|
|
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
|
2022-11-13 18:07:54 +03:00
|
|
|
variableMovementWithState.OnToggleStateChanged += VariableMovementStateListener;
|
2022-03-09 21:46:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void UnsubscribeFromMovements()
|
|
|
|
{
|
|
|
|
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
|
2022-11-13 18:07:54 +03:00
|
|
|
variableMovementWithState.OnToggleStateChanged -= VariableMovementStateListener;
|
2022-03-09 21:46:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void VariableMovementStateListener(bool arg0) => UpdateActiveVariableMovement();
|
|
|
|
protected virtual void UpdateActiveVariableMovement()
|
|
|
|
{
|
|
|
|
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
|
|
|
|
if (variableMovementWithState.Enabled)
|
|
|
|
{
|
|
|
|
ActiveVariableMovement = variableMovementWithState;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActiveVariableMovement = defaultVariableMovement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|