A game about forced loneliness, made by TACStudios
1namespace Unity.VisualScripting
2{
3 [UnitOrder(202)]
4 public abstract class Round<TInput, TOutput> : Unit
5 {
6 public enum Rounding
7 {
8 Floor = 0,
9 Ceiling = 1,
10 AwayFromZero = 2,
11 }
12
13 /// <summary>
14 /// The rounding mode.
15 /// </summary>
16 [Inspectable, UnitHeaderInspectable, Serialize]
17 public Rounding rounding { get; set; } = Rounding.AwayFromZero;
18
19 /// <summary>
20 /// The value to round.
21 /// </summary>
22 [DoNotSerialize]
23 [PortLabelHidden]
24 public ValueInput input { get; private set; }
25
26 /// <summary>
27 /// The rounded value.
28 /// </summary>
29 [DoNotSerialize]
30 [PortLabelHidden]
31 public ValueOutput output { get; private set; }
32
33 protected override void Definition()
34 {
35 input = ValueInput<TInput>(nameof(input));
36 output = ValueOutput(nameof(output), Operation).Predictable();
37
38 Requirement(input, output);
39 }
40
41 protected abstract TOutput Floor(TInput input);
42 protected abstract TOutput AwayFromZero(TInput input);
43 protected abstract TOutput Ceiling(TInput input);
44
45 public TOutput Operation(Flow flow)
46 {
47 switch (rounding)
48 {
49 case Rounding.Floor:
50 return Floor(flow.GetValue<TInput>(input));
51 case Rounding.AwayFromZero:
52 return AwayFromZero(flow.GetValue<TInput>(input));
53 case Rounding.Ceiling:
54 return Ceiling(flow.GetValue<TInput>(input));
55 default:
56 throw new UnexpectedEnumValueException<Rounding>(rounding);
57 }
58 }
59 }
60}