Movement/VariableMovement/VariableMovementController.cs

115 lines
4.1 KiB
C#
Raw Normal View History

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
{
public Action<IVariableMovement> OnVariableMovementChanged { get; set; } = null;
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);
}
}
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>();
movementController.OnMovementActivated += SetMultiplierToActiveMovement;
movementController.OnMovementDeactivated += 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)
{
if (!isSet)
return;
currentMovement.MovementMultiplier = ActiveVariableMovement.Asset.Multiplier;
}
2022-03-09 21:46:49 +03:00
public void LoadVariableMovementCollection(VMCollection collection)
{
this.collection = collection;
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 += VariableMovementStateListener;
2022-03-09 21:46:49 +03:00
}
protected virtual void UnsubscribeFromMovements()
{
foreach (IVariableMovementWithState variableMovementWithState in variableMovementsWithState)
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;
}
}
}