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