File size: 3,016 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;

namespace Unity.VisualScripting
{
    public abstract class StateTransition : GraphElement<StateGraph>, IStateTransition
    {
        public class DebugData : IStateTransitionDebugData
        {
            public Exception runtimeException { get; set; }

            public int lastBranchFrame { get; set; }

            public float lastBranchTime { get; set; }
        }

        protected StateTransition() { }

        protected StateTransition(IState source, IState destination)
        {
            Ensure.That(nameof(source)).IsNotNull(source);
            Ensure.That(nameof(destination)).IsNotNull(destination);

            if (source.graph != destination.graph)
            {
                throw new NotSupportedException("Cannot create transitions across state graphs.");
            }

            this.source = source;
            this.destination = destination;
        }

        public IGraphElementDebugData CreateDebugData()
        {
            return new DebugData();
        }

        public override int dependencyOrder => 1;

        [Serialize]
        public IState source { get; internal set; }

        [Serialize]
        public IState destination { get; internal set; }

        public override void Instantiate(GraphReference instance)
        {
            base.Instantiate(instance);

            if (this is IGraphEventListener listener && instance.GetElementData<State.Data>(source).isActive)
            {
                listener.StartListening(instance);
            }
        }

        public override void Uninstantiate(GraphReference instance)
        {
            if (this is IGraphEventListener listener)
            {
                listener.StopListening(instance);
            }

            base.Uninstantiate(instance);
        }

        #region Lifecycle

        public void Branch(Flow flow)
        {
            if (flow.enableDebug)
            {
                var editorData = flow.stack.GetElementDebugData<DebugData>(this);

                editorData.lastBranchFrame = EditorTimeBinding.frame;
                editorData.lastBranchTime = EditorTimeBinding.time;
            }

            try
            {
                source.OnExit(flow, StateExitReason.Branch);
            }
            catch (Exception ex)
            {
                source.HandleException(flow.stack, ex);
                throw;
            }

            source.OnBranchTo(flow, destination);

            try
            {
                destination.OnEnter(flow, StateEnterReason.Branch);
            }
            catch (Exception ex)
            {
                destination.HandleException(flow.stack, ex);
                throw;
            }
        }

        public abstract void OnEnter(Flow flow);

        public abstract void OnExit(Flow flow);

        #endregion

        #region Analytics

        public override AnalyticsIdentifier GetAnalyticsIdentifier()
        {
            return null;
        }

        #endregion
    }
}