A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using UnityEngine;
3
4namespace Unity.VisualScripting
5{
6 /// <summary>
7 /// Selects a value from a set by matching it with an input flow.
8 /// </summary>
9 [UnitCategory("Control")]
10 [UnitTitle("Select On Flow")]
11 [UnitShortTitle("Select")]
12 [UnitSubtitle("On Flow")]
13 [UnitOrder(8)]
14 [TypeIcon(typeof(ISelectUnit))]
15 public sealed class SelectOnFlow : Unit, ISelectUnit
16 {
17 [SerializeAs(nameof(branchCount))]
18 private int _branchCount = 2;
19
20 [DoNotSerialize]
21 [Inspectable, UnitHeaderInspectable("Branches")]
22 public int branchCount
23 {
24 get => _branchCount;
25 set => _branchCount = Mathf.Clamp(value, 2, 10);
26 }
27
28 [DoNotSerialize]
29 public Dictionary<ControlInput, ValueInput> branches { get; private set; }
30
31 /// <summary>
32 /// Triggered when any selector is entered.
33 /// </summary>
34 [DoNotSerialize]
35 [PortLabelHidden]
36 public ControlOutput exit { get; private set; }
37
38 /// <summary>
39 /// The selected value.
40 /// </summary>
41 [DoNotSerialize]
42 [PortLabelHidden]
43 public ValueOutput selection { get; private set; }
44
45 protected override void Definition()
46 {
47 branches = new Dictionary<ControlInput, ValueInput>();
48
49 selection = ValueOutput<object>(nameof(selection));
50 exit = ControlOutput(nameof(exit));
51
52 for (int i = 0; i < branchCount; i++)
53 {
54 var branchValue = ValueInput<object>("value_" + i);
55 var branchControl = ControlInput("enter_" + i, (flow) => Select(flow, branchValue));
56
57 Requirement(branchValue, branchControl);
58 Assignment(branchControl, selection);
59 Succession(branchControl, exit);
60
61 branches.Add(branchControl, branchValue);
62 }
63 }
64
65 public ControlOutput Select(Flow flow, ValueInput branchValue)
66 {
67 flow.SetValue(selection, flow.GetValue(branchValue));
68
69 return exit;
70 }
71 }
72}