A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace Unity.VisualScripting
5{
6 [UnitCategory("Graphs/Graph Nodes")]
7 public abstract class SetGraph<TGraph, TMacro, TMachine> : Unit
8 where TGraph : class, IGraph, new()
9 where TMacro : Macro<TGraph>
10 where TMachine : Machine<TGraph, TMacro>
11 {
12 /// <summary>
13 /// The entry point for the node.
14 /// </summary>
15 [DoNotSerialize]
16 [PortLabelHidden]
17 public ControlInput enter { get; protected set; }
18
19 /// <summary>
20 /// The GameObject or the ScriptMachine where the graph will be set.
21 /// </summary>
22 [DoNotSerialize]
23 [PortLabelHidden]
24 [NullMeansSelf]
25 public ValueInput target { get; protected set; }
26
27 /// <summary>
28 /// The script graph.
29 /// </summary>
30 [DoNotSerialize]
31 [PortLabel("Graph")]
32 [PortLabelHidden]
33 public ValueInput graphInput { get; protected set; }
34
35 /// <summary>
36 /// The graph that has been set to the ScriptMachine.
37 /// </summary>
38 [DoNotSerialize]
39 [PortLabel("Graph")]
40 [PortLabelHidden]
41 public ValueOutput graphOutput { get; protected set; }
42
43 /// <summary>
44 /// The action to execute once the graph has been set.
45 /// </summary>
46 [DoNotSerialize]
47 [PortLabelHidden]
48 public ControlOutput exit { get; protected set; }
49
50 protected abstract bool isGameObject { get; }
51 Type targetType => isGameObject ? typeof(GameObject) : typeof(TMachine);
52
53 protected override void Definition()
54 {
55 enter = ControlInput(nameof(enter), SetMacro);
56 target = ValueInput(targetType, nameof(target)).NullMeansSelf();
57 target.SetDefaultValue(targetType.PseudoDefault());
58
59 graphInput = ValueInput<TMacro>(nameof(graphInput), null);
60 graphOutput = ValueOutput<TMacro>(nameof(graphOutput));
61 exit = ControlOutput(nameof(exit));
62
63 Requirement(graphInput, enter);
64 Assignment(enter, graphOutput);
65 Succession(enter, exit);
66 }
67
68 ControlOutput SetMacro(Flow flow)
69 {
70 var macro = flow.GetValue<TMacro>(graphInput);
71 var targetValue = flow.GetValue(target, targetType);
72
73 if (targetValue is GameObject go)
74 {
75 go.GetComponent<TMachine>().nest.SwitchToMacro(macro);
76 }
77 else
78 {
79 ((TMachine)targetValue).nest.SwitchToMacro(macro);
80 }
81
82 flow.SetValue(graphOutput, macro);
83
84 return exit;
85 }
86 }
87}