Client LuaCsForBarotrauma
ArithmeticComponent.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Globalization;
4 using System.Xml.Linq;
5 
7 {
9  {
10  //an array to keep track of how long ago a signal was received on both inputs
11  protected float[] timeSinceReceived;
12 
13  protected float[] receivedSignal;
14 
15  //the output is sent if both inputs have received a signal within the timeframe
16  protected float timeFrame;
17 
18  protected readonly Character[] signalSender = new Character[2];
19 
20  [Serialize(999999.0f, IsPropertySaveable.Yes, description: "The output of the item is restricted below this value.", alwaysUseInstanceValues: true),
21  InGameEditable(MinValueFloat = -999999.0f, MaxValueFloat = 999999.0f)]
22  public float ClampMax
23  {
24  get;
25  set;
26  }
27 
28  [Serialize(-999999.0f, IsPropertySaveable.Yes, description: "The output of the item is restricted above this value.", alwaysUseInstanceValues: true),
29  InGameEditable(MinValueFloat = -999999.0f, MaxValueFloat = 999999.0f)]
30  public float ClampMin
31  {
32  get;
33  set;
34  }
35 
36  [InGameEditable(DecimalCount = 2),
37  Serialize(0.0f, IsPropertySaveable.Yes, description: "The item must have received signals to both inputs within this timeframe to output the result." +
38  " If set to 0, the inputs must be received at the same time.", alwaysUseInstanceValues: true, translationTextTag: "sp.")]
39  public float TimeFrame
40  {
41  get { return timeFrame; }
42  set
43  {
44  if (value > timeFrame)
45  {
46  timeSinceReceived[0] = timeSinceReceived[1] = Math.Max(value * 2.0f, 0.1f);
47  }
48  timeFrame = Math.Max(0.0f, value);
49  }
50  }
51 
53  : base(item, element)
54  {
55  timeSinceReceived = new float[] { Math.Max(timeFrame * 2.0f, 0.1f), Math.Max(timeFrame * 2.0f, 0.1f) };
56  receivedSignal = new float[2];
57  }
58 
59  sealed public override void Update(float deltaTime, Camera cam)
60  {
61  bool deactivate = true;
62  bool earlyReturn = false;
63  for (int i = 0; i < timeSinceReceived.Length; i++)
64  {
65  deactivate &= timeSinceReceived[i] > timeFrame;
66  earlyReturn |= timeSinceReceived[i] > timeFrame;
67  timeSinceReceived[i] += deltaTime;
68  }
69  // only stop Update() if both signals timed-out. if IsActive == false, then the component stops updating.
70  IsActive = !deactivate;
71  // early return if either of the signal timed-out
72  if (earlyReturn) { return; }
73  float output = Calculate(receivedSignal[0], receivedSignal[1]);
74  if (MathUtils.IsValid(output))
75  {
76  item.SendSignal(new Signal(MathHelper.Clamp(output, ClampMin, ClampMax).ToString("G", CultureInfo.InvariantCulture), sender: signalSender[0] ?? signalSender[1]), "signal_out");
77  }
78  }
79 
80  protected abstract float Calculate(float signal1, float signal2);
81 
82  public override void ReceiveSignal(Signal signal, Connection connection)
83  {
84  switch (connection.Name)
85  {
86  case "signal_in1":
87  float.TryParse(signal.value, NumberStyles.Float, CultureInfo.InvariantCulture, out receivedSignal[0]);
88  signalSender[0] = signal.sender;
89  timeSinceReceived[0] = 0.0f;
90  IsActive = true;
91  break;
92  case "signal_in2":
93  float.TryParse(signal.value, NumberStyles.Float, CultureInfo.InvariantCulture, out receivedSignal[1]);
94  signalSender[1] = signal.sender;
95  timeSinceReceived[1] = 0.0f;
96  IsActive = true;
97  break;
98  }
99  }
100  }
101 }
void SendSignal(string signal, string connectionName)
abstract float Calculate(float signal1, float signal2)
ArithmeticComponent(Item item, ContentXElement element)
override void ReceiveSignal(Signal signal, Connection connection)
sealed override void Update(float deltaTime, Camera cam)
The base class for components holding the different functionalities of the item