Nessuna descrizione
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.

FlowStateTransition.cs 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using UnityEngine;
  3. namespace Unity.VisualScripting
  4. {
  5. [SerializationVersion("A")]
  6. public sealed class FlowStateTransition : NesterStateTransition<FlowGraph, ScriptGraphAsset>, IGraphEventListener
  7. {
  8. public FlowStateTransition() : base() { }
  9. public FlowStateTransition(IState source, IState destination) : base(source, destination)
  10. {
  11. if (!source.canBeSource)
  12. {
  13. throw new InvalidOperationException("Source state cannot emit transitions.");
  14. }
  15. if (!destination.canBeDestination)
  16. {
  17. throw new InvalidOperationException("Destination state cannot receive transitions.");
  18. }
  19. }
  20. public static FlowStateTransition WithDefaultTrigger(IState source, IState destination)
  21. {
  22. var flowStateTransition = new FlowStateTransition(source, destination);
  23. flowStateTransition.nest.source = GraphSource.Embed;
  24. flowStateTransition.nest.embed = GraphWithDefaultTrigger();
  25. return flowStateTransition;
  26. }
  27. public static FlowGraph GraphWithDefaultTrigger()
  28. {
  29. return new FlowGraph()
  30. {
  31. units =
  32. {
  33. new TriggerStateTransition() { position = new Vector2(100, -50) }
  34. }
  35. };
  36. }
  37. #region Lifecycle
  38. public override void OnEnter(Flow flow)
  39. {
  40. if (flow.stack.TryEnterParentElement(this))
  41. {
  42. flow.stack.TriggerEventHandler(hook => hook == StateEventHooks.OnEnterState, new EmptyEventArgs(), parent => parent is SubgraphUnit, false);
  43. flow.stack.ExitParentElement();
  44. }
  45. }
  46. public override void OnExit(Flow flow)
  47. {
  48. if (flow.stack.TryEnterParentElement(this))
  49. {
  50. flow.stack.TriggerEventHandler(hook => hook == StateEventHooks.OnExitState, new EmptyEventArgs(), parent => parent is SubgraphUnit, false);
  51. nest.graph.StopListening(flow.stack);
  52. flow.stack.ExitParentElement();
  53. }
  54. }
  55. public void StartListening(GraphStack stack)
  56. {
  57. if (stack.TryEnterParentElement(this))
  58. {
  59. nest.graph.StartListening(stack);
  60. stack.ExitParentElement();
  61. }
  62. }
  63. public void StopListening(GraphStack stack)
  64. {
  65. if (stack.TryEnterParentElement(this))
  66. {
  67. nest.graph.StopListening(stack);
  68. stack.ExitParentElement();
  69. }
  70. }
  71. public bool IsListening(GraphPointer pointer)
  72. {
  73. return pointer.GetElementData<State.Data>(source).isActive;
  74. }
  75. #endregion
  76. public override FlowGraph DefaultGraph()
  77. {
  78. return GraphWithDefaultTrigger();
  79. }
  80. }
  81. }