/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Items.AnimatorAudioStates
{
using Opsive.UltimateCharacterController.Character;
using Opsive.UltimateCharacterController.StateSystem;
using UnityEngine;
///
/// The AnimatorAudioState will return a Item Substate Index parameter based on the object's state.
///
[UnityEngine.Scripting.Preserve]
public abstract class AnimatorAudioStateSelector
{
protected Item m_Item;
protected GameObject m_Character;
protected UltimateCharacterLocomotion m_CharacterLocomotion;
protected AnimatorAudioStateSet.AnimatorAudioState[] m_States;
///
/// Initializes the selector.
///
/// The GameObject that the state belongs to.
/// The character that the state bleongs to.
/// The item that the state belongs to.
/// The states which are being selected.
public virtual void Initialize(GameObject gameObject, UltimateCharacterLocomotion characterLocomotion, Item item, AnimatorAudioStateSet.AnimatorAudioState[] states)
{
m_Item = item;
m_Character = characterLocomotion.gameObject;
m_CharacterLocomotion = characterLocomotion;
m_States = states;
}
///
/// Starts or stops the state selection. Will activate or deactivate the state with the name specified within the AnimatorAudioState.
///
/// Is the object starting?
public virtual void StartStopStateSelection(bool start)
{
// Activate or deactivate the state.
var stateIndex = GetStateIndex();
if (stateIndex == -1 || stateIndex >= m_States.Length) {
return;
}
var stateName = m_States[stateIndex].StateName;
if (string.IsNullOrEmpty(stateName)) {
return;
}
StateManager.SetState(m_Character, stateName, start);
}
///
/// Returns the current state index. -1 indicates this index is not set by the class.
///
/// The current state index.
public virtual int GetStateIndex()
{
return -1;
}
///
/// Moves to the next state.
///
/// Was the state changed successfully?
public virtual bool NextState() { return true; }
///
/// Is the state at the specified index valid?
///
/// The index to check the state of.
/// True if the state at the specified index is valid.
protected bool IsStateValid(int index) { return (m_States[index].AllowDuringMovement || !m_CharacterLocomotion.Moving) &&
(!m_States[index].RequireGrounded || m_CharacterLocomotion.Grounded); }
///
/// Returns an additional value that should be added to the Item Substate Index.
///
/// An additional value that should be added to the Item Substate Index.
public virtual int GetAdditionalItemSubstateIndex() { return 0; }
///
/// The selector has been destroyed.
///
public virtual void OnDestroy() { }
}
}