File size: 6,446 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Unity.VisualScripting
{
    public abstract class State : GraphElement<StateGraph>, IState
    {
        public class Data : IGraphElementData
        {
            public bool isActive;

            public bool hasEntered;
        }

        public class DebugData : IStateDebugData
        {
            public int lastEnterFrame { get; set; }

            public float lastExitTime { get; set; }

            public Exception runtimeException { get; set; }
        }

        public IGraphElementData CreateData()
        {
            return new Data();
        }

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

        [Serialize]
        public bool isStart { get; set; }

        [DoNotSerialize]
        public virtual bool canBeSource => true;

        [DoNotSerialize]
        public virtual bool canBeDestination => true;

        public override void BeforeRemove()
        {
            base.BeforeRemove();

            Disconnect();
        }

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

            var data = instance.GetElementData<Data>(this);

            if (this is IGraphEventListener listener && data.isActive)
            {
                listener.StartListening(instance);
            }
            else if (isStart && !data.hasEntered && graph.IsListening(instance))
            {
                using (var flow = Flow.New(instance))
                {
                    OnEnter(flow, StateEnterReason.Start);
                }
            }
        }

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

            base.Uninstantiate(instance);
        }

        #region Poutine

        protected void CopyFrom(State source)
        {
            base.CopyFrom(source);

            isStart = source.isStart;
            width = source.width;
        }

        #endregion

        #region Transitions

        public IEnumerable<IStateTransition> outgoingTransitions => graph?.transitions.WithSource(this) ?? Enumerable.Empty<IStateTransition>();

        public IEnumerable<IStateTransition> incomingTransitions => graph?.transitions.WithDestination(this) ?? Enumerable.Empty<IStateTransition>();

        protected List<IStateTransition> outgoingTransitionsNoAlloc => graph?.transitions.WithSourceNoAlloc(this) ?? Empty<IStateTransition>.list;

        public IEnumerable<IStateTransition> transitions => LinqUtility.Concat<IStateTransition>(outgoingTransitions, incomingTransitions);

        public void Disconnect()
        {
            foreach (var transition in transitions.ToArray())
            {
                graph.transitions.Remove(transition);
            }
        }

        #endregion

        #region Lifecycle

        public virtual void OnEnter(Flow flow, StateEnterReason reason)
        {
            var data = flow.stack.GetElementData<Data>(this);

            if (data.isActive) // Prevent re-entry from Any State
            {
                return;
            }

            data.isActive = true;

            data.hasEntered = true;

            foreach (var transition in outgoingTransitionsNoAlloc)
            {
                // Start listening for the transition's events
                // before entering the state in case OnEnterState
                // actually instantly triggers a transition via event
                // http://support.ludiq.io/topics/261-event-timing-issue/
                (transition as IGraphEventListener)?.StartListening(flow.stack);
            }

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

                editorData.lastEnterFrame = EditorTimeBinding.frame;
            }

            OnEnterImplementation(flow);

            foreach (var transition in outgoingTransitionsNoAlloc)
            {
                try
                {
                    transition.OnEnter(flow);
                }
                catch (Exception ex)
                {
                    transition.HandleException(flow.stack, ex);
                    throw;
                }
            }
        }

        public virtual void OnExit(Flow flow, StateExitReason reason)
        {
            var data = flow.stack.GetElementData<Data>(this);

            if (!data.isActive)
            {
                return;
            }

            OnExitImplementation(flow);

            data.isActive = false;

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

                editorData.lastExitTime = EditorTimeBinding.time;
            }

            foreach (var transition in outgoingTransitionsNoAlloc)
            {
                try
                {
                    transition.OnExit(flow);
                }
                catch (Exception ex)
                {
                    transition.HandleException(flow.stack, ex);
                    throw;
                }
            }
        }

        protected virtual void OnEnterImplementation(Flow flow) { }

        protected virtual void UpdateImplementation(Flow flow) { }

        protected virtual void FixedUpdateImplementation(Flow flow) { }

        protected virtual void LateUpdateImplementation(Flow flow) { }

        protected virtual void OnExitImplementation(Flow flow) { }

        public virtual void OnBranchTo(Flow flow, IState destination) { }

        #endregion

        #region Widget

        public const float DefaultWidth = 170;

        [Serialize]
        public Vector2 position { get; set; }

        [Serialize]
        public float width { get; set; } = DefaultWidth;

        #endregion

        #region Analytics

        public override AnalyticsIdentifier GetAnalyticsIdentifier()
        {
            var aid = new AnalyticsIdentifier
            {
                Identifier = GetType().FullName,
                Namespace = GetType().Namespace,
            };
            aid.Hashcode = aid.Identifier.GetHashCode();
            return aid;
        }

        #endregion
    }
}