2022-03-09 21:46:49 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
namespace Syntriax.Modules.Movement.VariableMovement
|
|
|
|
{
|
|
|
|
public class VariableMovementController : MonoBehaviour, IVariableMovementController
|
|
|
|
{
|
|
|
|
public UnityEvent<IVariableMovement> OnVariableMovementChanged { get; protected 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()
|
|
|
|
{
|
|
|
|
OnVariableMovementChanged = new UnityEvent<IVariableMovement>();
|
|
|
|
defaultVariableMovement = new VariableMovement();
|
|
|
|
variableMovementsWithState = new List<IVariableMovementWithState>(16);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Start()
|
|
|
|
{
|
|
|
|
movementController = GetComponent<IMovementController>();
|
2022-03-13 20:59:45 +03:00
|
|
|
movementController.OnMovementActivated.AddListener(SetMultiplierToActiveMovement);
|
|
|
|
movementController.OnMovementDeactivated.AddListener(ResetMultiplierToDefault);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
variableMovementWithState.OnToggleStateChanged.AddListener(VariableMovementStateListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void UnsubscribeFromMovements()
|
|
|
|
{
|
|
|
|
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
|
|
|
|
variableMovementWithState.OnToggleStateChanged.RemoveListener(VariableMovementStateListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void VariableMovementStateListener(bool arg0) => UpdateActiveVariableMovement();
|
|
|
|
protected virtual void UpdateActiveVariableMovement()
|
|
|
|
{
|
|
|
|
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
|
|
|
|
if (variableMovementWithState.Enabled)
|
|
|
|
{
|
|
|
|
ActiveVariableMovement = variableMovementWithState;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActiveVariableMovement = defaultVariableMovement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|