CharacterState.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using Animancer.FSM;
  4. using UnityEngine;
  5. namespace Animancer.Samples.StateMachines
  6. {
  7. /// <summary>A state for a <see cref="Character"/>.</summary>
  8. ///
  9. /// <remarks>
  10. /// <strong>Sample:</strong>
  11. /// <see href="https://kybernetik.com.au/animancer/docs/samples/fsm/characters">
  12. /// Characters</see>
  13. /// </remarks>
  14. ///
  15. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.StateMachines/CharacterState
  16. ///
  17. [AddComponentMenu(Strings.SamplesMenuPrefix + "Characters - Character State")]
  18. [AnimancerHelpUrl(typeof(CharacterState))]
  19. public abstract class CharacterState : StateBehaviour
  20. {
  21. /************************************************************************************************************************/
  22. [SerializeField]
  23. private Character _Character;
  24. public Character Character => _Character;
  25. /************************************************************************************************************************/
  26. #if UNITY_EDITOR
  27. protected override void OnValidate()
  28. {
  29. base.OnValidate();
  30. gameObject.GetComponentInParentOrChildren(ref _Character);
  31. }
  32. #endif
  33. /************************************************************************************************************************/
  34. // Explained in the Interruptions sample.
  35. /************************************************************************************************************************/
  36. public virtual CharacterStatePriority Priority => CharacterStatePriority.Low;
  37. public virtual bool CanInterruptSelf => false;
  38. public override bool CanExitState
  39. {
  40. get
  41. {
  42. // There are several different ways of accessing the state change details:
  43. // CharacterState nextState = StateChange<CharacterState>.NextState;
  44. // CharacterState nextState = this.GetNextState();
  45. CharacterState nextState = _Character.StateMachine.NextState;
  46. if (nextState == this)
  47. return CanInterruptSelf;
  48. else if (Priority == CharacterStatePriority.Low)
  49. return true;
  50. else
  51. return nextState.Priority > Priority;
  52. }
  53. }
  54. /************************************************************************************************************************/
  55. }
  56. }