// Animancer // Copyright 2020 Kybernetik // #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value. using Animancer.FSM; using System; using UnityEngine; namespace Animancer.Examples.StateMachines.InterruptManagement { /// /// A centralised group of references to the common parts of a creature and a state machine for their actions. /// [AddComponentMenu(Strings.MenuPrefix + "Examples/Interrupt Management - Creature")] [HelpURL(Strings.APIDocumentationURL + ".Examples.StateMachines.InterruptManagement/Creature")] [DefaultExecutionOrder(-5000)]// Initialise the State Machine early. public sealed class Creature : MonoBehaviour { /************************************************************************************************************************/ [SerializeField] private AnimancerComponent _Animancer; public AnimancerComponent Animancer { get { return _Animancer; } } [SerializeField] private CreatureState _Idle; public CreatureState Idle { get { return _Idle; } } // Rigidbody. // Ground Detector. // Stats. // Health and Mana. // Pathfinding. // Etc. // Anything common to most creatures. /************************************************************************************************************************/ /// /// The Finite State Machine that manages the actions of this creature. /// public StateMachine StateMachine { get; private set; } /// /// Forces the to return to the state. /// public Action ForceIdleState { get; private set; } /************************************************************************************************************************/ private void Awake() { // Note that this class has a [DefaultExecutionOrder] attribute to ensure that this method runs before any // other components that might want to access it. ForceIdleState = () => StateMachine.ForceSetState(_Idle); StateMachine = new StateMachine(_Idle); } /************************************************************************************************************************/ /// /// Calls . Normally you would just access the /// directly. This method only exists to be called by UI buttons. /// public void TrySetState(CreatureState state) { StateMachine.TrySetState(state); } /************************************************************************************************************************/ #if UNITY_EDITOR /************************************************************************************************************************/ /// [Editor-Only] /// Inspector Gadgets Pro calls this method after drawing the regular Inspector GUI, allowing this script to /// display its current state in Play Mode. /// /// /// Inspector Gadgets Pro allows you to easily customise the Inspector without writing a full custom Inspector /// class by simply adding a method with this name. Without Inspector Gadgets, this method will do nothing. /// It can be purchased from https://kybernetik.com.au/inspector-gadgets/pro /// private void AfterInspectorGUI() { if (UnityEditor.EditorApplication.isPlaying) { var enabled = GUI.enabled; GUI.enabled = false; UnityEditor.EditorGUILayout.ObjectField("Current State", StateMachine.CurrentState, typeof(CreatureState), true); GUI.enabled = enabled; } } /************************************************************************************************************************/ #endif /************************************************************************************************************************/ } }