A game about forced loneliness, made by TACStudios
1using System;
2
3namespace Unity.VisualScripting
4{
5 public abstract class StateTransition : GraphElement<StateGraph>, IStateTransition
6 {
7 public class DebugData : IStateTransitionDebugData
8 {
9 public Exception runtimeException { get; set; }
10
11 public int lastBranchFrame { get; set; }
12
13 public float lastBranchTime { get; set; }
14 }
15
16 protected StateTransition() { }
17
18 protected StateTransition(IState source, IState destination)
19 {
20 Ensure.That(nameof(source)).IsNotNull(source);
21 Ensure.That(nameof(destination)).IsNotNull(destination);
22
23 if (source.graph != destination.graph)
24 {
25 throw new NotSupportedException("Cannot create transitions across state graphs.");
26 }
27
28 this.source = source;
29 this.destination = destination;
30 }
31
32 public IGraphElementDebugData CreateDebugData()
33 {
34 return new DebugData();
35 }
36
37 public override int dependencyOrder => 1;
38
39 [Serialize]
40 public IState source { get; internal set; }
41
42 [Serialize]
43 public IState destination { get; internal set; }
44
45 public override void Instantiate(GraphReference instance)
46 {
47 base.Instantiate(instance);
48
49 if (this is IGraphEventListener listener && instance.GetElementData<State.Data>(source).isActive)
50 {
51 listener.StartListening(instance);
52 }
53 }
54
55 public override void Uninstantiate(GraphReference instance)
56 {
57 if (this is IGraphEventListener listener)
58 {
59 listener.StopListening(instance);
60 }
61
62 base.Uninstantiate(instance);
63 }
64
65 #region Lifecycle
66
67 public void Branch(Flow flow)
68 {
69 if (flow.enableDebug)
70 {
71 var editorData = flow.stack.GetElementDebugData<DebugData>(this);
72
73 editorData.lastBranchFrame = EditorTimeBinding.frame;
74 editorData.lastBranchTime = EditorTimeBinding.time;
75 }
76
77 try
78 {
79 source.OnExit(flow, StateExitReason.Branch);
80 }
81 catch (Exception ex)
82 {
83 source.HandleException(flow.stack, ex);
84 throw;
85 }
86
87 source.OnBranchTo(flow, destination);
88
89 try
90 {
91 destination.OnEnter(flow, StateEnterReason.Branch);
92 }
93 catch (Exception ex)
94 {
95 destination.HandleException(flow.stack, ex);
96 throw;
97 }
98 }
99
100 public abstract void OnEnter(Flow flow);
101
102 public abstract void OnExit(Flow flow);
103
104 #endregion
105
106 #region Analytics
107
108 public override AnalyticsIdentifier GetAnalyticsIdentifier()
109 {
110 return null;
111 }
112
113 #endregion
114 }
115}