/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Items.AnimatorAudioStates { using UnityEngine; /// /// The Sequence state will move from one state to the in a sequence order. /// public class Sequence : AnimatorAudioStateSelector { [Tooltip("Resets the index back to the start after the specified delay. Set to -1 to never reset.")] [SerializeField] protected float m_ResetDelay = -1; public float ResetDelay { get { return m_ResetDelay; } set { m_ResetDelay = value; } } private int m_CurrentIndex = -1; private float m_LastUsedTime = -1; /// /// Starts or stops the state selection. /// /// Is the object starting? public override void StartStopStateSelection(bool start) { base.StartStopStateSelection(start); // The Sequence task can reset which index is returned if the next state is selected too slowly. if (start && m_ResetDelay != -1 && m_LastUsedTime != -1 && m_LastUsedTime + m_ResetDelay < Time.time) { m_CurrentIndex = -1; } } /// /// Returns the current state index. -1 indicates this index is not set by the class. /// /// The current state index. public override int GetStateIndex() { return m_CurrentIndex; } /// /// Moves to the next state. /// /// Was the state changed successfully? public override bool NextState() { m_LastUsedTime = Time.time; var count = 0; var size = m_States.Length; if (size == 0) { return false; } do { m_CurrentIndex = (m_CurrentIndex + 1) % size; count++; } while ((!IsStateValid(m_CurrentIndex) || !m_States[m_CurrentIndex].Enabled) && count <= size); return count <= size; } } }