Character.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Examples.AnimatorControllers.GameKit
  5. {
  6. /// <summary>
  7. /// A centralised group of references to the common parts of a character and a state machine for their actions.
  8. /// </summary>
  9. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/animator-controllers/3d-game-kit">3D Game Kit</see></example>
  10. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.AnimatorControllers.GameKit/Character
  11. ///
  12. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Game Kit - Character")]
  13. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(AnimatorControllers) + "." + nameof(GameKit) + "/" + nameof(Character))]
  14. public sealed class Character : MonoBehaviour
  15. {
  16. /************************************************************************************************************************/
  17. [SerializeField]
  18. private AnimancerComponent _Animancer;
  19. public AnimancerComponent Animancer => _Animancer;
  20. [SerializeField]
  21. private CharacterMovement _Movement;
  22. public CharacterMovement Movement => _Movement;
  23. [SerializeField]
  24. private CharacterParameters _Parameters;
  25. public CharacterParameters Parameters => _Parameters;
  26. /************************************************************************************************************************/
  27. [SerializeField]
  28. private CharacterState.StateMachine _StateMachine;
  29. public CharacterState.StateMachine StateMachine => _StateMachine;
  30. private void Awake()
  31. {
  32. StateMachine.InitializeAfterDeserialize();
  33. }
  34. /************************************************************************************************************************/
  35. /// <summary>
  36. /// Check if this <see cref="Character"/> should enter the Idle, Locomotion, or Airborne state depending on
  37. /// whether it is grounded and the movement input from the <see cref="Brain"/>.
  38. /// </summary>
  39. /// <remarks>
  40. /// We could add some null checks to this method to support characters that don't have all the standard states,
  41. /// such as a character that can't move or a flying character that never lands.
  42. /// </remarks>
  43. public bool CheckMotionState()
  44. {
  45. CharacterState state;
  46. if (Movement.IsGrounded)
  47. {
  48. state = Parameters.MovementDirection == default
  49. ? StateMachine.DefaultState
  50. : StateMachine.Locomotion;
  51. }
  52. else
  53. {
  54. state = StateMachine.Airborne;
  55. }
  56. return
  57. state != StateMachine.CurrentState &&
  58. StateMachine.TryResetState(state);
  59. }
  60. /************************************************************************************************************************/
  61. }
  62. }