/// ---------------------------------------------
/// Ultimate Character Controller
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateCharacterController.Items.AnimatorAudioStates
{
using Opsive.UltimateCharacterController.Character;
using UnityEngine;
///
/// The RandomRecoil state will move from one state to another in a random order.
///
public class RandomRecoil : RecoilAnimatorAudioStateSelector
{
private int m_CurrentIndex;
///
/// 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 override void Initialize(GameObject gameObject, UltimateCharacterLocomotion characterLocomotion, Item item, AnimatorAudioStateSet.AnimatorAudioState[] states)
{
base.Initialize(gameObject, characterLocomotion, item, states);
// Call next state so the index will be initialized to a random value.
NextState();
}
///
/// 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()
{
var count = 0;
var size = m_States.Length;
if (size == 0) {
return false;
}
do {
m_CurrentIndex = UnityEngine.Random.Range(0, size);
++count;
} while ((!IsStateValid(m_CurrentIndex) || !m_States[m_CurrentIndex].Enabled) && count <= size);
return count <= size;
}
}
}