a0d606ef5f
- Add support for some of the most common types (vectors, colors, rects) so there's no need to parse the values in the setters of the serializable properties (see Holdable.HoldPos for example). - Make a generic version of the item editing HUD that can be used on any IPropertyObject. Should make it easier to implement things like the character editor, editing structure properties, particle editor, etc. - Improve the interface of the editing HUD. Instead of having to type in a string value into a textbox, there should be number input fields for numeric properties, sliders for properties that only accept a range of values, a color picker, etc. And tooltips.
116 lines
3.0 KiB
C#
116 lines
3.0 KiB
C#
using Barotrauma.Networking;
|
|
using Lidgren.Network;
|
|
using System;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Barotrauma.Items.Components
|
|
{
|
|
class RelayComponent : PowerTransfer, IServerSerializable
|
|
{
|
|
private float maxPower;
|
|
|
|
private bool isOn;
|
|
|
|
[Editable, SerializableProperty(1000.0f, true)]
|
|
public float MaxPower
|
|
{
|
|
get { return maxPower; }
|
|
set
|
|
{
|
|
maxPower = Math.Max(0.0f, value);
|
|
}
|
|
}
|
|
|
|
[Editable, SerializableProperty(false, true)]
|
|
public bool IsOn
|
|
{
|
|
get
|
|
{
|
|
return isOn;
|
|
}
|
|
set
|
|
{
|
|
isOn = value;
|
|
if (!isOn)
|
|
{
|
|
currPowerConsumption = 0.0f;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override bool CanTransfer
|
|
{
|
|
get
|
|
{
|
|
return isOn;
|
|
}
|
|
}
|
|
|
|
public RelayComponent(Item item, XElement element)
|
|
: base (item, element)
|
|
{
|
|
IsActive = true;
|
|
}
|
|
|
|
public override void Update(float deltaTime, Camera cam)
|
|
{
|
|
base.Update(deltaTime, cam);
|
|
|
|
item.SendSignal(0, IsOn ? "1" : "0", "state_out", null);
|
|
}
|
|
|
|
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item source, Character sender, float power=0.0f)
|
|
{
|
|
if (connection.IsPower && connection.Name.Contains("_out")) return;
|
|
|
|
if (item.Condition <= 0.0f) return;
|
|
|
|
if (power > maxPower) item.Condition = 0.0f;
|
|
|
|
if (connection.Name.Contains("_in"))
|
|
{
|
|
if (!IsOn) return;
|
|
|
|
string outConnection = connection.Name.Contains("power_in") ? "power_out" : "signal_out";
|
|
|
|
int connectionNumber = -1;
|
|
int.TryParse(connection.Name.Substring(connection.Name.Length - 1, 1), out connectionNumber);
|
|
|
|
if (connectionNumber > 0) outConnection += connectionNumber;
|
|
|
|
item.SendSignal(stepsTaken, signal, outConnection, sender, power);
|
|
}
|
|
else if (connection.Name == "toggle")
|
|
{
|
|
SetState(!IsOn, false);
|
|
}
|
|
else if (connection.Name == "set_state")
|
|
{
|
|
SetState(signal != "0", false);
|
|
}
|
|
}
|
|
|
|
public void SetState(bool on, bool isNetworkMessage)
|
|
{
|
|
if (GameMain.Client != null && !isNetworkMessage) return;
|
|
|
|
if (on != IsOn && GameMain.Server != null)
|
|
{
|
|
item.CreateServerEvent(this);
|
|
}
|
|
|
|
IsOn = on;
|
|
}
|
|
|
|
public void ServerWrite(NetBuffer msg, Client c, object[] extraData = null)
|
|
{
|
|
msg.Write(isOn);
|
|
}
|
|
|
|
public void ClientRead(ServerNetObject type, NetBuffer msg, float sendingTime)
|
|
{
|
|
SetState(msg.ReadBoolean(), true);
|
|
}
|
|
}
|
|
}
|