A game about forced loneliness, made by TACStudios
1using System.Collections.Generic;
2using System.Linq;
3
4namespace Unity.VisualScripting
5{
6 [UnitOrder(303)]
7 [TypeIcon(typeof(Add<>))]
8 public abstract class Sum<T> : MultiInputUnit<T>
9 {
10 /// <summary>
11 /// The sum.
12 /// </summary>
13 [DoNotSerialize]
14 [PortLabelHidden]
15 public ValueOutput sum { get; private set; }
16
17 protected override void Definition()
18 {
19 if (this is IDefaultValue<T> defaultValueUnit)
20 {
21 var mi = new List<ValueInput>();
22 multiInputs = mi.AsReadOnly();
23
24 for (var i = 0; i < inputCount; i++)
25 {
26 if (i == 0)
27 {
28 mi.Add(ValueInput<T>(i.ToString()));
29 continue;
30 }
31
32 mi.Add(ValueInput(i.ToString(), defaultValueUnit.defaultValue));
33 }
34 }
35 else
36 {
37 base.Definition();
38 }
39
40 sum = ValueOutput(nameof(sum), Operation).Predictable();
41
42 foreach (var multiInput in multiInputs)
43 {
44 Requirement(multiInput, sum);
45 }
46 }
47
48 public abstract T Operation(T a, T b);
49
50 public abstract T Operation(IEnumerable<T> values);
51
52 public T Operation(Flow flow)
53 {
54 if (inputCount == 2)
55 {
56 return Operation(flow.GetValue<T>(multiInputs[0]), flow.GetValue<T>(multiInputs[1]));
57 }
58 else
59 {
60 return Operation(multiInputs.Select(flow.GetValue<T>));
61 }
62 }
63 }
64}