logic gate momentary pulse, switch status port (#17198)
* logic gate momentary is now pulse * switch status, minor refactor * filescope namespace * switch * fix ci probably * add auto linking for edge detector and logic gate --------- Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
using Content.Server.DeviceLinking.Systems;
|
||||
using Content.Shared.MachineLinking;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.DeviceLinking.Components
|
||||
namespace Content.Server.DeviceLinking.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Simple switch that will fire ports when toggled on or off. A button is jsut a switch that signals on the
|
||||
/// same port regardless of its state.
|
||||
/// </summary>
|
||||
[RegisterComponent, Access(typeof(SignalSwitchSystem))]
|
||||
public sealed class SignalSwitchComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple switch that will fire ports when toggled on or off. A button is jsut a switch that signals on the
|
||||
/// same port regardless of its state.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class SignalSwitchComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The port that gets signaled when the switch turns on.
|
||||
/// </summary>
|
||||
@@ -23,10 +24,16 @@ namespace Content.Server.DeviceLinking.Components
|
||||
[DataField("offPort", customTypeSerializer: typeof(PrototypeIdSerializer<TransmitterPortPrototype>))]
|
||||
public string OffPort = "Off";
|
||||
|
||||
/// <summary>
|
||||
/// The port that gets signaled with the switch's current status.
|
||||
/// This is only used if OnPort is different from OffPort, not in the case of a toggle switch.
|
||||
/// </summary>
|
||||
[DataField("statusPort", customTypeSerializer: typeof(PrototypeIdSerializer<TransmitterPortPrototype>))]
|
||||
public string StatusPort = "Status";
|
||||
|
||||
[DataField("state")]
|
||||
public bool State;
|
||||
|
||||
[DataField("clickSound")]
|
||||
public SoundSpecifier ClickSound { get; set; } = new SoundPathSpecifier("/Audio/Machines/lightswitch.ogg");
|
||||
}
|
||||
public SoundSpecifier ClickSound = new SoundPathSpecifier("/Audio/Machines/lightswitch.ogg");
|
||||
}
|
||||
|
||||
@@ -32,6 +32,26 @@ public sealed class LogicGateSystem : EntitySystem
|
||||
SubscribeLocalEvent<LogicGateComponent, SignalReceivedEvent>(OnSignalReceived);
|
||||
}
|
||||
|
||||
public override void Update(float deltaTime)
|
||||
{
|
||||
var query = EntityQueryEnumerator<LogicGateComponent>();
|
||||
while (query.MoveNext(out var uid, out var comp))
|
||||
{
|
||||
// handle momentary pulses - high when received then low the next tick
|
||||
if (comp.StateA == SignalState.Momentary)
|
||||
{
|
||||
comp.StateA = SignalState.Low;
|
||||
}
|
||||
if (comp.StateB == SignalState.Momentary)
|
||||
{
|
||||
comp.StateB = SignalState.High;
|
||||
}
|
||||
|
||||
// output most likely changed so update it
|
||||
UpdateOutput(uid, comp);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnInit(EntityUid uid, LogicGateComponent comp, ComponentInit args)
|
||||
{
|
||||
_deviceLink.EnsureSinkPorts(uid, comp.InputPortA, comp.InputPortB);
|
||||
@@ -92,8 +112,9 @@ public sealed class LogicGateSystem : EntitySystem
|
||||
private void UpdateOutput(EntityUid uid, LogicGateComponent comp)
|
||||
{
|
||||
// get the new output value now that it's changed
|
||||
var a = comp.StateA == SignalState.High;
|
||||
var b = comp.StateB == SignalState.High;
|
||||
// momentary is treated as high for the current tick, after updating it will be reset to low
|
||||
var a = comp.StateA != SignalState.Low;
|
||||
var b = comp.StateB != SignalState.Low;
|
||||
var output = false;
|
||||
switch (comp.Gate)
|
||||
{
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
using Content.Server.DeviceLinking.Components;
|
||||
using Content.Server.DeviceNetwork;
|
||||
using Content.Server.MachineLinking.System;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.DeviceLinking.Systems
|
||||
namespace Content.Server.DeviceLinking.Systems;
|
||||
|
||||
public sealed class SignalSwitchSystem : EntitySystem
|
||||
{
|
||||
public sealed class SignalSwitchSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
|
||||
[Dependency] private readonly DeviceLinkSystem _deviceLink = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -19,22 +21,31 @@ namespace Content.Server.DeviceLinking.Systems
|
||||
SubscribeLocalEvent<SignalSwitchComponent, ActivateInWorldEvent>(OnActivated);
|
||||
}
|
||||
|
||||
private void OnInit(EntityUid uid, SignalSwitchComponent component, ComponentInit args)
|
||||
private void OnInit(EntityUid uid, SignalSwitchComponent comp, ComponentInit args)
|
||||
{
|
||||
_signalSystem.EnsureSourcePorts(uid, component.OnPort, component.OffPort);
|
||||
_deviceLink.EnsureSourcePorts(uid, comp.OnPort, comp.OffPort, comp.StatusPort);
|
||||
}
|
||||
|
||||
private void OnActivated(EntityUid uid, SignalSwitchComponent component, ActivateInWorldEvent args)
|
||||
private void OnActivated(EntityUid uid, SignalSwitchComponent comp, ActivateInWorldEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
component.State = !component.State;
|
||||
_signalSystem.InvokePort(uid, component.State ? component.OnPort : component.OffPort);
|
||||
SoundSystem.Play(component.ClickSound.GetSound(), Filter.Pvs(component.Owner), component.Owner,
|
||||
AudioHelpers.WithVariation(0.125f).WithVolume(8f));
|
||||
comp.State = !comp.State;
|
||||
_deviceLink.InvokePort(uid, comp.State ? comp.OnPort : comp.OffPort);
|
||||
var data = new NetworkPayload
|
||||
{
|
||||
[DeviceNetworkConstants.LogicState] = comp.State ? SignalState.High : SignalState.Low
|
||||
};
|
||||
|
||||
// only send status if it's a toggle switch and not a button
|
||||
if (comp.OnPort != comp.OffPort)
|
||||
{
|
||||
_deviceLink.InvokePort(uid, comp.StatusPort, data);
|
||||
}
|
||||
|
||||
_audio.PlayPvs(comp.ClickSound, uid, AudioParams.Default.WithVariation(0.125f).WithVolume(8f));
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ signal-port-description-on-transmitter = This port is invoked whenever the trans
|
||||
signal-port-name-off-transmitter = Off
|
||||
signal-port-description-off-transmitter = This port is invoked whenever the transmitter is turned off.
|
||||
|
||||
signal-port-name-status-transmitter = Status
|
||||
signal-port-description-logic-output = This port is invoked with HIGH or LOW depending on the transmitter status.
|
||||
|
||||
signal-port-name-left = Left
|
||||
signal-port-description-left = This port is invoked whenever the lever is moved to the leftmost position.
|
||||
|
||||
@@ -14,7 +17,7 @@ signal-port-name-right = Right
|
||||
signal-port-description-right = This port is invoked whenever the lever is moved to the rightmost position.
|
||||
|
||||
signal-port-name-doorstatus = Door status
|
||||
signal-port-description-doorstatus = This port is invoked with HIGH when the door opens and LOW when the door closes.
|
||||
signal-port-description-doorstatus = This port is invoked with HIGH when the door opens and LOW when the door finishes closing.
|
||||
|
||||
signal-port-name-middle = Middle
|
||||
signal-port-description-middle = This port is invoked whenever the lever is moved to the neutral position.
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
description: signal-port-description-off-transmitter
|
||||
defaultLinks: [ Off, Close ]
|
||||
|
||||
- type: sourcePort
|
||||
id: Status
|
||||
name: signal-port-name-status-transmitter
|
||||
description: signal-port-description-status-transmitter
|
||||
|
||||
- type: sourcePort
|
||||
id: Left
|
||||
name: signal-port-name-left
|
||||
@@ -78,13 +83,16 @@
|
||||
id: Output
|
||||
name: signal-port-name-logic-output
|
||||
description: signal-port-description-logic-output
|
||||
defaultLinks: [ Input ]
|
||||
|
||||
- type: sourcePort
|
||||
id: OutputHigh
|
||||
name: signal-port-name-logic-output-high
|
||||
description: signal-port-description-logic-output-high
|
||||
defaultLinks: [ On, Open, Forward, Trigger ]
|
||||
|
||||
- type: sourcePort
|
||||
id: OutputLow
|
||||
name: signal-port-name-logic-output-low
|
||||
description: signal-port-description-logic-output-low
|
||||
defaultLinks: [ Off, Close ]
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
ports:
|
||||
- On
|
||||
- Off
|
||||
- Status
|
||||
|
||||
- type: entity
|
||||
id: SignalButton
|
||||
@@ -56,6 +57,7 @@
|
||||
- type: SignalSwitch
|
||||
onPort: Pressed
|
||||
offPort: Pressed
|
||||
statusPort: Pressed
|
||||
- type: Rotatable
|
||||
- type: Construction
|
||||
graph: SignalButtonGraph
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
description: signal-port-description-off-transmitter
|
||||
defaultLinks: [ Off, Close ]
|
||||
|
||||
- type: transmitterPort
|
||||
id: Status
|
||||
name: signal-port-name-status-transmitter
|
||||
description: signal-port-description-status-transmitter
|
||||
|
||||
- type: transmitterPort
|
||||
id: Left
|
||||
name: signal-port-name-left
|
||||
@@ -78,13 +83,16 @@
|
||||
id: Output
|
||||
name: signal-port-name-logic-output
|
||||
description: signal-port-description-logic-output
|
||||
defaultLinks: [ Input ]
|
||||
|
||||
- type: transmitterPort
|
||||
id: OutputHigh
|
||||
name: signal-port-name-logic-output-high
|
||||
description: signal-port-description-logic-output-high
|
||||
defaultLinks: [ On, Open, Forward, Trigger ]
|
||||
|
||||
- type: transmitterPort
|
||||
id: OutputLow
|
||||
name: signal-port-name-logic-output-low
|
||||
description: signal-port-description-logic-output-low
|
||||
defaultLinks: [ Off, Close ]
|
||||
|
||||
Reference in New Issue
Block a user