Client LuaCsForBarotrauma
StringComponent.cs
1 using System;
2 
4 {
5  abstract class StringComponent : ItemComponent
6  {
7  //an array to keep track of how long ago a signal was received on both inputs
8  protected float[] timeSinceReceived;
9 
10  protected string[] receivedSignal;
11 
12  //the output is sent if both inputs have received a signal within the timeframe
13  protected float timeFrame;
14 
15 
16  [InGameEditable(DecimalCount = 2),
17  Serialize(0.0f, IsPropertySaveable.Yes, description: "The item must have received signals to both inputs within this timeframe to output the result." +
18  " If set to 0, the inputs must be received at the same time.", alwaysUseInstanceValues: true)]
19  public float TimeFrame
20  {
21  get { return timeFrame; }
22  set
23  {
24  if (value > timeFrame)
25  {
26  timeSinceReceived[0] = timeSinceReceived[1] = Math.Max(value * 2.0f, 0.1f);
27  }
28  timeFrame = Math.Max(0.0f, value);
29  }
30  }
31 
33  : base(item, element)
34  {
35  timeSinceReceived = new float[] { Math.Max(timeFrame * 2.0f, 0.1f), Math.Max(timeFrame * 2.0f, 0.1f) };
36  receivedSignal = new string[2];
37  }
38 
39  sealed public override void Update(float deltaTime, Camera cam)
40  {
41  bool deactivate = true;
42  bool earlyReturn = false;
43  for (int i = 0; i < timeSinceReceived.Length; i++)
44  {
45  deactivate &= timeSinceReceived[i] > timeFrame;
46  earlyReturn |= timeSinceReceived[i] > timeFrame;
47  timeSinceReceived[i] += deltaTime;
48  }
49  // only stop Update() if both signals timed-out. if IsActive == false, then the component stops updating.
50  IsActive = !deactivate;
51  // early return if either of the signal timed-out
52  if (earlyReturn) { return; }
53  string output = Calculate(receivedSignal[0], receivedSignal[1]);
54  item.SendSignal(output, "signal_out");
55  }
56 
57  protected abstract string Calculate(string signal1, string signal2);
58 
59  public override void ReceiveSignal(Signal signal, Connection connection)
60  {
61  switch (connection.Name)
62  {
63  case "signal_in1":
64  receivedSignal[0] = signal.value;
65  timeSinceReceived[0] = 0.0f;
66  IsActive = true;
67  break;
68  case "signal_in2":
69  receivedSignal[1] = signal.value;
70  timeSinceReceived[1] = 0.0f;
71  IsActive = true;
72  break;
73  }
74  }
75  }
76 }
void SendSignal(string signal, string connectionName)
The base class for components holding the different functionalities of the item
abstract string Calculate(string signal1, string signal2)
StringComponent(Item item, ContentXElement element)
override void ReceiveSignal(Signal signal, Connection connection)
sealed override void Update(float deltaTime, Camera cam)