using System.Collections.Generic;
using UnityEngine;
using Lightbug.CharacterControllerPro.Core;
using Lightbug.Utilities;
namespace Lightbug.CharacterControllerPro.Implementation
{
///
/// This class represents a state, that is, a basic element used by the character state controller (finite state machine).
///
public abstract class CharacterState : MonoBehaviour, IUpdatable
{
[SerializeField]
bool overrideAnimatorController = true;
[Condition("overrideAnimatorController", ConditionAttribute.ConditionType.IsTrue)]
[SerializeField]
RuntimeAnimatorController runtimeAnimatorController = null;
///
/// Gets the hash value (Animator) associated with this state, based on its name.
///
public int StateNameHash { get; private set; }
///
/// Gets the state runtime animator controller.
///
public RuntimeAnimatorController RuntimeAnimatorController => runtimeAnimatorController;
public bool OverrideAnimatorController => overrideAnimatorController;
///
/// Gets the CharacterActor component of the gameObject.
///
public CharacterActor CharacterActor { get; private set; }
///
/// Gets the CharacterBrain component of the gameObject.
///
// public CharacterBrain CharacterBrain{ get; private set; }
CharacterBrain CharacterBrain = null;
///
/// Gets the current brain actions CharacterBrain component of the gameObject.
///
public CharacterActions CharacterActions
{
get
{
return CharacterBrain == null ? new CharacterActions() : CharacterBrain.CharacterActions;
}
}
///
/// Gets the CharacterStateController component of the gameObject.
///
public CharacterStateController CharacterStateController { get; private set; }
protected virtual void Awake()
{
CharacterActor = this.GetComponentInBranch();
CharacterStateController = this.GetComponentInBranch();
CharacterBrain = this.GetComponentInBranch();
}
protected virtual void Start()
{
StateNameHash = Animator.StringToHash(this.GetType().Name);
}
///
/// This method runs once when the state has entered the state machine.
///
public virtual void EnterBehaviour(float dt, CharacterState fromState)
{
}
///
/// This methods runs before the main Update method.
///
public virtual void PreUpdateBehaviour(float dt)
{
}
///
/// This method runs frame by frame, and should be implemented by the derived state class.
///
public abstract void UpdateBehaviour(float dt);
///
/// This methods runs after the main Update method.
///
public virtual void PostUpdateBehaviour(float dt)
{
}
///
/// This methods runs just before the character physics simulation.
///
public virtual void PreCharacterSimulation(float dt)
{
}
///
/// This methods runs after the character physics simulation.
///
public virtual void PostCharacterSimulation(float dt)
{
}
///
/// This method runs once when the state has exited the state machine.
///
public virtual void ExitBehaviour(float dt, CharacterState toState)
{
}
///
/// Checks if the required conditions to exit this state are true. If so it returns the desired state (null otherwise). After this the state machine will
/// proceed to evaluate the "enter transition" condition on the target state.
///
public virtual void CheckExitTransition()
{
}
///
/// Checks if the required conditions to enter this state are true. If so the state machine will automatically change the current state to the desired one.
///
public virtual bool CheckEnterTransition(CharacterState fromState)
{
return true;
}
///
/// This methods runs after getting all the ik positions, rotations and their respective weights. Use it to modify the ik data of the humanoid rig.
///
public virtual void UpdateIK(int layerIndex)
{
}
public virtual string GetInfo()
{
return "";
}
///
/// Checks if the Animator component associated with the character is "valid" or not.
///
/// True if the Animator is valid, false otherwise.
public bool IsAnimatorValid() => CharacterActor.IsAnimatorValid();
}
}