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:
deltanedas
2023-06-12 22:43:59 +00:00
committed by GitHub
parent 263890ff64
commit 7cbf08ea9e
7 changed files with 109 additions and 49 deletions

View File

@@ -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)
{

View File

@@ -1,40 +1,51 @@
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 _deviceLink = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
base.Initialize();
public override void Initialize()
SubscribeLocalEvent<SignalSwitchComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SignalSwitchComponent, ActivateInWorldEvent>(OnActivated);
}
private void OnInit(EntityUid uid, SignalSwitchComponent comp, ComponentInit args)
{
_deviceLink.EnsureSourcePorts(uid, comp.OnPort, comp.OffPort, comp.StatusPort);
}
private void OnActivated(EntityUid uid, SignalSwitchComponent comp, ActivateInWorldEvent args)
{
if (args.Handled)
return;
comp.State = !comp.State;
_deviceLink.InvokePort(uid, comp.State ? comp.OnPort : comp.OffPort);
var data = new NetworkPayload
{
base.Initialize();
[DeviceNetworkConstants.LogicState] = comp.State ? SignalState.High : SignalState.Low
};
SubscribeLocalEvent<SignalSwitchComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SignalSwitchComponent, ActivateInWorldEvent>(OnActivated);
// only send status if it's a toggle switch and not a button
if (comp.OnPort != comp.OffPort)
{
_deviceLink.InvokePort(uid, comp.StatusPort, data);
}
private void OnInit(EntityUid uid, SignalSwitchComponent component, ComponentInit args)
{
_signalSystem.EnsureSourcePorts(uid, component.OnPort, component.OffPort);
}
_audio.PlayPvs(comp.ClickSound, uid, AudioParams.Default.WithVariation(0.125f).WithVolume(8f));
private void OnActivated(EntityUid uid, SignalSwitchComponent component, 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));
args.Handled = true;
}
args.Handled = true;
}
}