Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace Unity.VisualScripting
  6. {
  7. public abstract class State : GraphElement<StateGraph>, IState
  8. {
  9. public class Data : IGraphElementData
  10. {
  11. public bool isActive;
  12. public bool hasEntered;
  13. }
  14. public class DebugData : IStateDebugData
  15. {
  16. public int lastEnterFrame { get; set; }
  17. public float lastExitTime { get; set; }
  18. public Exception runtimeException { get; set; }
  19. }
  20. public IGraphElementData CreateData()
  21. {
  22. return new Data();
  23. }
  24. public IGraphElementDebugData CreateDebugData()
  25. {
  26. return new DebugData();
  27. }
  28. [Serialize]
  29. public bool isStart { get; set; }
  30. [DoNotSerialize]
  31. public virtual bool canBeSource => true;
  32. [DoNotSerialize]
  33. public virtual bool canBeDestination => true;
  34. public override void BeforeRemove()
  35. {
  36. base.BeforeRemove();
  37. Disconnect();
  38. }
  39. public override void Instantiate(GraphReference instance)
  40. {
  41. base.Instantiate(instance);
  42. var data = instance.GetElementData<Data>(this);
  43. if (this is IGraphEventListener listener && data.isActive)
  44. {
  45. listener.StartListening(instance);
  46. }
  47. else if (isStart && !data.hasEntered && graph.IsListening(instance))
  48. {
  49. using (var flow = Flow.New(instance))
  50. {
  51. OnEnter(flow, StateEnterReason.Start);
  52. }
  53. }
  54. }
  55. public override void Uninstantiate(GraphReference instance)
  56. {
  57. if (this is IGraphEventListener listener)
  58. {
  59. listener.StopListening(instance);
  60. }
  61. base.Uninstantiate(instance);
  62. }
  63. #region Poutine
  64. protected void CopyFrom(State source)
  65. {
  66. base.CopyFrom(source);
  67. isStart = source.isStart;
  68. width = source.width;
  69. }
  70. #endregion
  71. #region Transitions
  72. public IEnumerable<IStateTransition> outgoingTransitions => graph?.transitions.WithSource(this) ?? Enumerable.Empty<IStateTransition>();
  73. public IEnumerable<IStateTransition> incomingTransitions => graph?.transitions.WithDestination(this) ?? Enumerable.Empty<IStateTransition>();
  74. protected List<IStateTransition> outgoingTransitionsNoAlloc => graph?.transitions.WithSourceNoAlloc(this) ?? Empty<IStateTransition>.list;
  75. public IEnumerable<IStateTransition> transitions => LinqUtility.Concat<IStateTransition>(outgoingTransitions, incomingTransitions);
  76. public void Disconnect()
  77. {
  78. foreach (var transition in transitions.ToArray())
  79. {
  80. graph.transitions.Remove(transition);
  81. }
  82. }
  83. #endregion
  84. #region Lifecycle
  85. public virtual void OnEnter(Flow flow, StateEnterReason reason)
  86. {
  87. var data = flow.stack.GetElementData<Data>(this);
  88. if (data.isActive) // Prevent re-entry from Any State
  89. {
  90. return;
  91. }
  92. data.isActive = true;
  93. data.hasEntered = true;
  94. foreach (var transition in outgoingTransitionsNoAlloc)
  95. {
  96. // Start listening for the transition's events
  97. // before entering the state in case OnEnterState
  98. // actually instantly triggers a transition via event
  99. // http://support.ludiq.io/topics/261-event-timing-issue/
  100. (transition as IGraphEventListener)?.StartListening(flow.stack);
  101. }
  102. if (flow.enableDebug)
  103. {
  104. var editorData = flow.stack.GetElementDebugData<DebugData>(this);
  105. editorData.lastEnterFrame = EditorTimeBinding.frame;
  106. }
  107. OnEnterImplementation(flow);
  108. foreach (var transition in outgoingTransitionsNoAlloc)
  109. {
  110. try
  111. {
  112. transition.OnEnter(flow);
  113. }
  114. catch (Exception ex)
  115. {
  116. transition.HandleException(flow.stack, ex);
  117. throw;
  118. }
  119. }
  120. }
  121. public virtual void OnExit(Flow flow, StateExitReason reason)
  122. {
  123. var data = flow.stack.GetElementData<Data>(this);
  124. if (!data.isActive)
  125. {
  126. return;
  127. }
  128. OnExitImplementation(flow);
  129. data.isActive = false;
  130. if (flow.enableDebug)
  131. {
  132. var editorData = flow.stack.GetElementDebugData<DebugData>(this);
  133. editorData.lastExitTime = EditorTimeBinding.time;
  134. }
  135. foreach (var transition in outgoingTransitionsNoAlloc)
  136. {
  137. try
  138. {
  139. transition.OnExit(flow);
  140. }
  141. catch (Exception ex)
  142. {
  143. transition.HandleException(flow.stack, ex);
  144. throw;
  145. }
  146. }
  147. }
  148. protected virtual void OnEnterImplementation(Flow flow) { }
  149. protected virtual void UpdateImplementation(Flow flow) { }
  150. protected virtual void FixedUpdateImplementation(Flow flow) { }
  151. protected virtual void LateUpdateImplementation(Flow flow) { }
  152. protected virtual void OnExitImplementation(Flow flow) { }
  153. public virtual void OnBranchTo(Flow flow, IState destination) { }
  154. #endregion
  155. #region Widget
  156. public const float DefaultWidth = 170;
  157. [Serialize]
  158. public Vector2 position { get; set; }
  159. [Serialize]
  160. public float width { get; set; } = DefaultWidth;
  161. #endregion
  162. #region Analytics
  163. public override AnalyticsIdentifier GetAnalyticsIdentifier()
  164. {
  165. var aid = new AnalyticsIdentifier
  166. {
  167. Identifier = GetType().FullName,
  168. Namespace = GetType().Namespace,
  169. };
  170. aid.Hashcode = aid.Identifier.GetHashCode();
  171. return aid;
  172. }
  173. #endregion
  174. }
  175. }