Client LuaCsForBarotrauma
OscillatorComponent.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using System.Text;
5 using System.Xml.Linq;
6 using Microsoft.Xna.Framework;
7 
9 {
11  {
12  public enum WaveType
13  {
14  Pulse,
15  Sawtooth,
16  Sine,
17  Square,
18  Triangle,
19  }
20 
21  private float frequency;
22 
23  private float phase;
24 
25  [InGameEditable, Serialize(WaveType.Pulse, IsPropertySaveable.Yes, description: "What kind of a signal the item outputs." +
26  " Pulse: periodically sends out a signal of 1." +
27  " Sawtooth: sends out a periodic wave that increases linearly from 0 to 1." +
28  " Sine: sends out a sine wave oscillating between -1 and 1." +
29  " Square: sends out a signal that alternates between 0 and 1." +
30  " Triangle: sends out a wave that alternates between increasing linearly from -1 to 1 and decreasing from 1 to -1.",
31  alwaysUseInstanceValues: true)]
33  {
34  get;
35  set;
36  }
37 
38  [InGameEditable(DecimalCount = 2), Serialize(1.0f, IsPropertySaveable.Yes, description: "How fast the signal oscillates, or how fast the pulses are sent (in Hz).", alwaysUseInstanceValues: true)]
39  public float Frequency
40  {
41  get { return frequency; }
42  set
43  {
44  //capped to 240 Hz (= 4 signals per frame) to prevent players
45  //from wrecking the performance by setting the value too high
46  frequency = MathHelper.Clamp(value, 0.0f, 240.0f);
47  }
48  }
49 
51  base(item, element)
52  {
53  IsActive = true;
54  }
55 
56  public override void Update(float deltaTime, Camera cam)
57  {
58  switch (OutputType)
59  {
60  case WaveType.Pulse:
61  if (frequency <= 0.0f) return;
62 
63  phase += deltaTime;
64  float pulseInterval = 1.0f / frequency;
65  while (phase >= pulseInterval)
66  {
67  item.SendSignal("1", "signal_out");
68  phase -= pulseInterval;
69  }
70  break;
71  case WaveType.Sawtooth:
72  phase = (phase + deltaTime * frequency) % 1.0f;
73  item.SendSignal(phase.ToString(CultureInfo.InvariantCulture), "signal_out");
74  break;
75  case WaveType.Square:
76  phase = (phase + deltaTime * frequency) % 1.0f;
77  item.SendSignal(phase < 0.5f ? "0" : "1", "signal_out");
78  break;
79  case WaveType.Sine:
80  phase = (phase + deltaTime * frequency) % 1.0f;
81  item.SendSignal(Math.Sin(phase * MathHelper.TwoPi).ToString(CultureInfo.InvariantCulture), "signal_out");
82  break;
83  case WaveType.Triangle:
84  phase = (phase + deltaTime * frequency) % 1.0f;
85  float output = 4.0f * MathF.Abs(MathUtils.PositiveModulo(phase - 0.25f, 1.0f) - 0.5f) - 1.0f;
86  item.SendSignal(output.ToString(CultureInfo.InvariantCulture), "signal_out");
87  break;
88  }
89  }
90 
91  public override void ReceiveSignal(Signal signal, Connection connection)
92  {
93  switch (connection.Name)
94  {
95  case "set_frequency":
96  case "frequency_in":
97  float newFrequency;
98  if (float.TryParse(signal.value, NumberStyles.Float, CultureInfo.InvariantCulture, out newFrequency))
99  {
100  Frequency = newFrequency;
101  }
102  IsActive = true;
103  break;
104  case "set_outputtype":
105  case "set_wavetype":
106  WaveType newOutputType;
107  if (Enum.TryParse(signal.value, out newOutputType))
108  {
109  OutputType = newOutputType;
110  }
111  break;
112  }
113  }
114  }
115 }
void SendSignal(string signal, string connectionName)
The base class for components holding the different functionalities of the item
OscillatorComponent(Item item, ContentXElement element)
override void ReceiveSignal(Signal signal, Connection connection)
override void Update(float deltaTime, Camera cam)