117 lines
4.3 KiB
C#
117 lines
4.3 KiB
C#
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;
|
|
private IInputActionCollection _inputActionCollection = null;
|
|
public IInputActionCollection InputActionCollection
|
|
{
|
|
get => _inputActionCollection;
|
|
set
|
|
{
|
|
_inputActionCollection = value;
|
|
|
|
foreach (IVariableMovementWithState variableMovement in variableMovementsWithState)
|
|
variableMovement.InputCollection = value;
|
|
}
|
|
}
|
|
private IVariableMovement _activeVariableMovement = null;
|
|
public IVariableMovement ActiveVariableMovement
|
|
{
|
|
get => _activeVariableMovement;
|
|
protected set
|
|
{
|
|
_activeVariableMovement = value;
|
|
OnVariableMovementChanged.Invoke(value);
|
|
|
|
if (movementController != null)
|
|
SetMultiplierToActiveMovement(movementController.ActiveMovement);
|
|
}
|
|
}
|
|
|
|
protected bool isSet = false;
|
|
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>();
|
|
movementController.OnMovementActivated.AddListener(SetMultiplierToActiveMovement);
|
|
movementController.OnMovementDeactivated.AddListener(ResetMultiplierToDefault);
|
|
}
|
|
|
|
private void ResetMultiplierToDefault(IMovement currentMovement)
|
|
{
|
|
if (!isSet)
|
|
return;
|
|
|
|
currentMovement.MovementMultiplier = defaultVariableMovement.Asset.Multiplier;
|
|
}
|
|
|
|
protected void SetMultiplierToActiveMovement(IMovement currentMovement)
|
|
{
|
|
if (!isSet)
|
|
return;
|
|
|
|
currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier;
|
|
|
|
}
|
|
|
|
public void LoadVariableMovementCollection(VMCollection collection)
|
|
{
|
|
this.collection = collection;
|
|
isSet = true;
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|