Added oscillators (a signal component that sends out a periodic pulse, sine wave or a square wave)
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 16 KiB |
@@ -449,6 +449,40 @@
|
|||||||
</ConnectionPanel>
|
</ConnectionPanel>
|
||||||
</Item>
|
</Item>
|
||||||
|
|
||||||
|
|
||||||
|
<Item
|
||||||
|
name="Oscillator"
|
||||||
|
category="Electrical"
|
||||||
|
Tags="smallitem"
|
||||||
|
|
||||||
|
linkable="true"
|
||||||
|
price="10"
|
||||||
|
description="Sends out a periodic, oscillating signal.">
|
||||||
|
|
||||||
|
<Deconstruct time="10">
|
||||||
|
<Item name="Steel Bar"/>
|
||||||
|
<Item name="FPGA Circuit"/>
|
||||||
|
</Deconstruct>
|
||||||
|
|
||||||
|
<Sprite texture="signalcomp.png" depth="0.8" sourcerect="17,64,16,16"/>
|
||||||
|
|
||||||
|
<OscillatorComponent canbeselected="true" outputtype="Pulse" frequency="1"/>
|
||||||
|
|
||||||
|
<Body width="16" height="16" density="30"/>
|
||||||
|
|
||||||
|
<Holdable selectkey="Action" slots="Any,RightHand,LeftHand" msg="Detach [Wrench]" PickingTime="5.0"
|
||||||
|
aimpos="65,-10" handle1="0,0" attachable="true" aimable="true">
|
||||||
|
<requireditem name="Wrench" type="Equipped"/>
|
||||||
|
</Holdable>
|
||||||
|
|
||||||
|
<ConnectionPanel selectkey="Action" canbeselected = "true" msg="Rewire [Screwdriver]">
|
||||||
|
<requireditem name="Screwdriver,Wire" type="Equipped"/>
|
||||||
|
<input name="set_frequency"/>
|
||||||
|
<input name="set_outputtype"/>
|
||||||
|
<output name="signal_out"/>
|
||||||
|
</ConnectionPanel>
|
||||||
|
</Item>
|
||||||
|
|
||||||
<Item
|
<Item
|
||||||
name="Wifi Component"
|
name="Wifi Component"
|
||||||
category="Electrical"
|
category="Electrical"
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
|
|
||||||
|
namespace Barotrauma.Items.Components
|
||||||
|
{
|
||||||
|
class OscillatorComponent : ItemComponent
|
||||||
|
{
|
||||||
|
public enum WaveType
|
||||||
|
{
|
||||||
|
Pulse,
|
||||||
|
Sine,
|
||||||
|
Square,
|
||||||
|
}
|
||||||
|
|
||||||
|
private float frequency;
|
||||||
|
|
||||||
|
private float phase;
|
||||||
|
|
||||||
|
[InGameEditable, HasDefaultValue(WaveType.Pulse, true)]
|
||||||
|
public WaveType OutputType
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
set;
|
||||||
|
}
|
||||||
|
|
||||||
|
[InGameEditable, HasDefaultValue(1.0f, true)]
|
||||||
|
public float Frequency
|
||||||
|
{
|
||||||
|
get { return frequency; }
|
||||||
|
set { frequency = Math.Max(0.0f, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public OscillatorComponent(Item item, XElement element) :
|
||||||
|
base(item, element)
|
||||||
|
{
|
||||||
|
IsActive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(float deltaTime, Camera cam)
|
||||||
|
{
|
||||||
|
switch (OutputType)
|
||||||
|
{
|
||||||
|
case WaveType.Pulse:
|
||||||
|
if (frequency <= 0.0f) return;
|
||||||
|
|
||||||
|
phase += deltaTime;
|
||||||
|
float pulseInterval = 1.0f / frequency;
|
||||||
|
while (phase >= pulseInterval)
|
||||||
|
{
|
||||||
|
item.SendSignal(0, "1", "signal_out", null);
|
||||||
|
phase -= pulseInterval;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case WaveType.Square:
|
||||||
|
phase = (phase + deltaTime * frequency) % 1.0f;
|
||||||
|
item.SendSignal(0, phase < 0.5f ? "0" : "1", "signal_out", null);
|
||||||
|
break;
|
||||||
|
case WaveType.Sine:
|
||||||
|
phase = (phase + deltaTime * frequency) % 1.0f;
|
||||||
|
item.SendSignal(0, Math.Sin(phase * MathHelper.TwoPi).ToString(CultureInfo.InvariantCulture), "signal_out", null);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item source, Character sender, float power = 0.0f)
|
||||||
|
{
|
||||||
|
switch (connection.Name)
|
||||||
|
{
|
||||||
|
case "set_frequency":
|
||||||
|
case "frequency_in":
|
||||||
|
float newFrequency;
|
||||||
|
if (float.TryParse(signal, out newFrequency))
|
||||||
|
{
|
||||||
|
Frequency = newFrequency;
|
||||||
|
}
|
||||||
|
IsActive = true;
|
||||||
|
break;
|
||||||
|
case "set_outputtype":
|
||||||
|
case "set_wavetype":
|
||||||
|
WaveType newOutputType;
|
||||||
|
if (Enum.TryParse(signal, out newOutputType))
|
||||||
|
{
|
||||||
|
OutputType = newOutputType;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user