Client LuaCsForBarotrauma
EqualsComponent.cs
1 using System;
2 using System.Xml.Linq;
3 
5 {
7  {
8  protected string output, falseOutput;
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 string[] receivedSignal;
14 
15  private readonly Character[] signalSender = new Character[2];
16 
17  //the output is sent if both inputs have received a signal within the timeframe
18  protected float timeFrame;
19 
20  private int maxOutputLength;
21  [Editable, Serialize(200, IsPropertySaveable.No, description: "The maximum length of the output strings. Warning: Large values can lead to large memory usage or networking issues.")]
22  public int MaxOutputLength
23  {
24  get { return maxOutputLength; }
25  set
26  {
27  maxOutputLength = Math.Max(value, 0);
28  }
29  }
30 
31  [InGameEditable, Serialize("1", IsPropertySaveable.Yes, description: "The signal sent when the condition is met.", alwaysUseInstanceValues: true)]
32  public string Output
33  {
34  get { return output; }
35  set
36  {
37  if (value == null) { return; }
38  output = value;
39  if (output.Length > MaxOutputLength && (item.Submarine == null || !item.Submarine.Loading))
40  {
41  output = output.Substring(0, MaxOutputLength);
42  }
43  }
44  }
45 
46  [InGameEditable, Serialize("0", IsPropertySaveable.Yes, description: "The signal sent when the condition is met (if empty, no signal is sent).", alwaysUseInstanceValues: true)]
47  public string FalseOutput
48  {
49  get { return falseOutput; }
50  set
51  {
52  if (value == null) { return; }
53  falseOutput = value;
54  if (falseOutput.Length > MaxOutputLength && (item.Submarine == null || !item.Submarine.Loading))
55  {
56  falseOutput = falseOutput.Substring(0, MaxOutputLength);
57  }
58  }
59  }
60 
61  [InGameEditable(DecimalCount = 2), Serialize(0.0f, IsPropertySaveable.Yes, description: "The maximum amount of time between the received signals. If set to 0, the signals must be received at the same time.", alwaysUseInstanceValues: true)]
62  public float TimeFrame
63  {
64  get { return timeFrame; }
65  set
66  {
67  if (value > timeFrame)
68  {
69  timeSinceReceived[0] = timeSinceReceived[1] = Math.Max(value * 2.0f, 0.1f);
70  }
71  timeFrame = Math.Max(0.0f, value);
72  }
73  }
74 
76  : base(item, element)
77  {
78  timeSinceReceived = new float[] { Math.Max(timeFrame * 2.0f, 0.1f), Math.Max(timeFrame * 2.0f, 0.1f) };
79  receivedSignal = new string[2];
80  IsActive = true;
81  }
82 
83  public override void Update(float deltaTime, Camera cam)
84  {
85  bool sendOutput = false;
86  for (int i = 0; i < timeSinceReceived.Length; i++)
87  {
88  if (timeSinceReceived[i] <= timeFrame) sendOutput = true;
89  timeSinceReceived[i] += deltaTime;
90  }
91 
92  if (sendOutput)
93  {
94  string signalOut = receivedSignal[0] == receivedSignal[1] ? output : falseOutput;
95  if (string.IsNullOrEmpty(signalOut)) { return; }
96  item.SendSignal(new Signal(signalOut, sender: signalSender[0] ?? signalSender[1]), "signal_out");
97  }
98  }
99 
100  public override void ReceiveSignal(Signal signal, Connection connection)
101  {
102  switch (connection.Name)
103  {
104  case "signal_in1":
105  receivedSignal[0] = signal.value;
106  timeSinceReceived[0] = 0.0f;
107  signalSender[0] = signal.sender;
108  break;
109  case "signal_in2":
110  receivedSignal[1] = signal.value;
111  timeSinceReceived[1] = 0.0f;
112  signalSender[1] = signal.sender;
113  break;
114  case "set_output":
115  output = signal.value;
116  break;
117  }
118  }
119  }
120 }
Submarine Submarine
Definition: Entity.cs:53
void SendSignal(string signal, string connectionName)
EqualsComponent(Item item, ContentXElement element)
override void ReceiveSignal(Signal signal, Connection connection)
override void Update(float deltaTime, Camera cam)
The base class for components holding the different functionalities of the item