2 using System.Collections.Generic;
3 using System.Globalization;
5 using Microsoft.Xna.Framework;
16 public int SendDuration;
18 public DelayedSignal(
Signal signal,
int sendTimer)
21 SendTimer = sendTimer;
25 private int signalQueueSize;
26 private int delayTicks;
28 private readonly Queue<DelayedSignal> signalQueue =
new Queue<DelayedSignal>();
30 private DelayedSignal prevQueuedSignal;
33 [
InGameEditable(MinValueFloat = 0.0f, MaxValueFloat = 60.0f, DecimalCount = 2),
Serialize(1.0f,
IsPropertySaveable.Yes, description:
"How long the item delays the signals (in seconds).", alwaysUseInstanceValues:
true)]
39 if (value == delay) {
return; }
41 delayTicks = (int)(delay / Timing.Step);
42 signalQueueSize = Math.Max(delayTicks, 1) * 2;
62 : base (
item, element)
69 if (signalQueue.Count == 0)
75 foreach (var val
in signalQueue)
79 while (signalQueue.Count > 0 && signalQueue.Peek().SendTimer <= 0)
81 var signalOut = signalQueue.Peek();
82 signalOut.SendDuration -= 1;
83 item.
SendSignal(
new Signal(signalOut.Signal.value, sender: signalOut.Signal.sender, strength: signalOut.Signal.strength),
"signal_out");
84 if (signalOut.SendDuration <= 0)
88 signalQueue.TryDequeue(out _);
99 switch (connection.
Name)
102 if (signalQueue.Count >= signalQueueSize) {
return; }
106 prevQueuedSignal =
null;
110 if (prevQueuedSignal !=
null &&
111 prevQueuedSignal.Signal.value == signal.
value &&
112 MathUtils.NearlyEqual(prevQueuedSignal.Signal.strength, signal.
strength) &&
113 ((prevQueuedSignal.SendTimer + prevQueuedSignal.SendDuration == delayTicks) || (prevQueuedSignal.SendTimer <= 0 && prevQueuedSignal.SendDuration > 0)))
115 prevQueuedSignal.SendDuration += 1;
119 prevQueuedSignal =
new DelayedSignal(signal, delayTicks)
123 signalQueue.Enqueue(prevQueuedSignal);
127 if (
float.TryParse(signal.
value, NumberStyles.Any, CultureInfo.InvariantCulture, out
float newDelay))
129 newDelay = MathHelper.Clamp(newDelay, 0, 60);
130 if (signalQueue.Count > 0 && newDelay !=
Delay)
132 prevQueuedSignal =
null;
void SendSignal(string signal, string connectionName)
bool ResetWhenDifferentSignalReceived
bool ResetWhenSignalReceived
DelayComponent(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