Client LuaCsForBarotrauma
FunctionComponent.cs
1 using System;
2 using System.Globalization;
3 using System.Xml.Linq;
4 
6 {
8  {
9  public enum FunctionType
10  {
11  Round,
12  Ceil,
13  Floor,
14  Factorial,
15  AbsoluteValue,
16  SquareRoot
17  }
18 
19  [Serialize(FunctionType.Round, IsPropertySaveable.No, description: "Which kind of function to run the input through.", alwaysUseInstanceValues: true)]
21  {
22  get; set;
23  }
24 
26  : base(item, element)
27  {
28  IsActive = true;
29  }
30 
31  public override void ReceiveSignal(Signal signal, Connection connection)
32  {
33  if (connection.Name != "signal_in") { return; }
34  if (!float.TryParse(signal.value, NumberStyles.Float, CultureInfo.InvariantCulture, out float value)) { return; }
35  switch (Function)
36  {
37  case FunctionType.Round:
38  value = MathF.Round(value);
39  if (value == -0)
40  {
41  value = 0;
42  }
43  break;
44  case FunctionType.Ceil:
45  value = MathF.Ceiling(value);
46  if (value == -0)
47  {
48  value = 0;
49  }
50  break;
51  case FunctionType.Floor:
52  value = MathF.Floor(value);
53  break;
54  case FunctionType.Factorial:
55  int intVal = (int)Math.Min(value, 20);
56  ulong factorial = 1;
57  for (int i = intVal; i > 0; i--)
58  {
59  factorial *= (ulong)i;
60  }
61  value = factorial;
62  break;
63  case FunctionType.AbsoluteValue:
64  value = MathF.Abs(value);
65  break;
66  case FunctionType.SquareRoot:
67  if (value < 0)
68  {
69  return;
70  }
71  value = MathF.Sqrt(value);
72  break;
73  default:
74  throw new NotImplementedException($"Function {Function} has not been implemented.");
75  }
76 
77  signal.value = value.ToString("G", CultureInfo.InvariantCulture);
78  item.SendSignal(signal, "signal_out");
79  }
80  }
81 }
void SendSignal(string signal, string connectionName)
FunctionComponent(Item item, ContentXElement element)
override void ReceiveSignal(Signal signal, Connection connection)
The base class for components holding the different functionalities of the item