// Animancer // Copyright 2020 Kybernetik // #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value. using System; using UnityEngine; namespace Animancer.Examples.StateMachines.Platformer { /// /// A centralised group of references to the common parts of a creature and a state machine for their actions. /// [AddComponentMenu(Strings.MenuPrefix + "Examples/Platformer - Creature")] [HelpURL(Strings.APIDocumentationURL + ".Examples.StateMachines.Platformer/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 SpriteRenderer _Renderer; public SpriteRenderer Renderer { get { return _Renderer; } } [SerializeField] private CreatureBrain _Brain; public CreatureBrain Brain { get { return _Brain; } } [SerializeField] private Rigidbody2D _Rigidbody; public Rigidbody2D Rigidbody { get { return _Rigidbody; } } [SerializeField] private GroundDetector _GroundDetector; public GroundDetector GroundDetector { get { return _GroundDetector; } } [SerializeField] private Health _Health; public Health Health { get { return _Health; } } [SerializeField] private CreatureState _Introduction; public CreatureState Introduction { get { return _Introduction; } } [SerializeField] private CreatureState _Idle; public CreatureState Idle { get { return _Idle; } } // Stats. // Mana. // Pathfinding. // Etc. // Anything common to most creatures. /************************************************************************************************************************/ /// /// The Finite State Machine that manages the actions of this creature. /// public FSM.StateMachine StateMachine { get; private set; } /// /// Forces the to return to the state. /// public Action ForceEnterIdleState { 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. ForceEnterIdleState = () => StateMachine.ForceSetState(_Idle); StateMachine = new FSM.StateMachine(_Introduction); } /************************************************************************************************************************/ private void FixedUpdate() { var speed = StateMachine.CurrentState.MovementSpeed * _Brain.MovementDirection; _Rigidbody.velocity = new Vector2(speed, _Rigidbody.velocity.y); // The sprites face right by default, so flip the X axis when moving left. if (speed != 0) _Renderer.flipX = _Brain.MovementDirection < 0; } /************************************************************************************************************************/ #if UNITY_EDITOR /************************************************************************************************************************/ /// [Editor-Only] /// Displays the current state at the bottom of the Inspector. /// /// /// 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 /************************************************************************************************************************/ } }