From 731cfc278a19805db3de7131a69eb348b696b7b2 Mon Sep 17 00:00:00 2001 From: MrFippik <48425912+MrFippik@users.noreply.github.com> Date: Sat, 6 Jan 2024 17:41:56 +1100 Subject: [PATCH] Add input port of the network system in timer (#20026) * Add port link sink in timer * Update SignalTimerSystem.cs Moving the trigger so that everything works correctly and does not conflict. Correction of remarks. * Update SignalTimerSystem.cs * a * review --------- Co-authored-by: metalgearsloth --- .../Components/SignalTimerComponent.cs | 21 +++++---- .../Systems/SignalTimerSystem.cs | 43 +++++++++++++++++-- .../Entities/Structures/Wallmounts/timer.yml | 3 ++ 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/Content.Server/DeviceLinking/Components/SignalTimerComponent.cs b/Content.Server/DeviceLinking/Components/SignalTimerComponent.cs index d40d7174ab..2e9b369b99 100644 --- a/Content.Server/DeviceLinking/Components/SignalTimerComponent.cs +++ b/Content.Server/DeviceLinking/Components/SignalTimerComponent.cs @@ -1,5 +1,6 @@ using Content.Shared.DeviceLinking; using Robust.Shared.Audio; +using Robust.Shared.Prototypes; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; namespace Content.Server.DeviceLinking.Components; @@ -7,36 +8,40 @@ namespace Content.Server.DeviceLinking.Components; [RegisterComponent] public sealed partial class SignalTimerComponent : Component { - [DataField("delay"), ViewVariables(VVAccess.ReadWrite)] + [DataField, ViewVariables(VVAccess.ReadWrite)] public double Delay = 5; /// /// This shows the Label: text box in the UI. /// - [DataField("canEditLabel"), ViewVariables(VVAccess.ReadWrite)] + [DataField, ViewVariables(VVAccess.ReadWrite)] public bool CanEditLabel = true; /// /// The label, used for TextScreen visuals currently. /// - [DataField("label"), ViewVariables(VVAccess.ReadWrite)] + [DataField, ViewVariables(VVAccess.ReadWrite)] public string Label = string.Empty; /// /// The port that gets signaled when the timer triggers. /// - [DataField("triggerPort", customTypeSerializer: typeof(PrototypeIdSerializer)), ViewVariables(VVAccess.ReadWrite)] - public string TriggerPort = "Timer"; + [DataField, ViewVariables(VVAccess.ReadWrite)] + public ProtoId TriggerPort = "Timer"; /// /// The port that gets signaled when the timer starts. /// - [DataField("startPort", customTypeSerializer: typeof(PrototypeIdSerializer)), ViewVariables(VVAccess.ReadWrite)] - public string StartPort = "Start"; + [DataField, ViewVariables(VVAccess.ReadWrite)] + public ProtoId StartPort = "Start"; + + [DataField, ViewVariables(VVAccess.ReadWrite)] + public ProtoId Trigger = "Trigger"; /// /// If not null, this timer will play this sound when done. /// - [DataField("doneSound"), ViewVariables(VVAccess.ReadWrite)] + [DataField, ViewVariables(VVAccess.ReadWrite)] public SoundSpecifier? DoneSound; } + diff --git a/Content.Server/DeviceLinking/Systems/SignalTimerSystem.cs b/Content.Server/DeviceLinking/Systems/SignalTimerSystem.cs index ff90679e28..8cdeac3e8e 100644 --- a/Content.Server/DeviceLinking/Systems/SignalTimerSystem.cs +++ b/Content.Server/DeviceLinking/Systems/SignalTimerSystem.cs @@ -1,4 +1,5 @@ using Content.Server.DeviceLinking.Components; +using Content.Server.DeviceLinking.Events; using Content.Server.UserInterface; using Content.Shared.Access.Systems; using Content.Shared.MachineLinking; @@ -19,6 +20,11 @@ public sealed class SignalTimerSystem : EntitySystem [Dependency] private readonly UserInterfaceSystem _ui = default!; [Dependency] private readonly AccessReaderSystem _accessReader = default!; + /// + /// Per-tick timer cache. + /// + private List> _timers = new(); + public override void Initialize() { base.Initialize(); @@ -29,11 +35,13 @@ public sealed class SignalTimerSystem : EntitySystem SubscribeLocalEvent(OnTextChangedMessage); SubscribeLocalEvent(OnDelayChangedMessage); SubscribeLocalEvent(OnTimerStartMessage); + SubscribeLocalEvent(OnSignalReceived); } private void OnInit(EntityUid uid, SignalTimerComponent component, ComponentInit args) { _appearanceSystem.SetData(uid, TextScreenVisuals.ScreenText, component.Label); + _signalSystem.EnsureSinkPorts(uid, component.Trigger); } private void OnAfterActivatableUIOpen(EntityUid uid, SignalTimerComponent component, AfterActivatableUIOpenEvent args) @@ -58,11 +66,13 @@ public sealed class SignalTimerSystem : EntitySystem public void Trigger(EntityUid uid, SignalTimerComponent signalTimer) { RemComp(uid); + if (TryComp(uid, out var appearance)) { - _appearanceSystem.SetData(uid, TextScreenVisuals.ScreenText, new string?[] { signalTimer.Label }, appearance); + _appearanceSystem.SetData(uid, TextScreenVisuals.ScreenText, new[] { signalTimer.Label }, appearance); } + _audio.PlayPvs(signalTimer.DoneSound, uid); _signalSystem.InvokePort(uid, signalTimer.TriggerPort); if (_ui.TryGetUi(uid, SignalTimerUiKey.Key, out var bui)) @@ -80,16 +90,29 @@ public sealed class SignalTimerSystem : EntitySystem public override void Update(float frameTime) { base.Update(frameTime); + UpdateTimer(); + } + + private void UpdateTimer() + { + _timers.Clear(); + var query = EntityQueryEnumerator(); while (query.MoveNext(out var uid, out var active, out var timer)) { if (active.TriggerTime > _gameTiming.CurTime) continue; - Trigger(uid, timer); + _timers.Add((uid, timer)); + } - if (timer.DoneSound != null) - _audio.PlayPvs(timer.DoneSound, uid); + foreach (var timer in _timers) + { + // Exploded or the likes. + if (!Exists(timer.Owner)) + continue; + + Trigger(timer.Owner, timer.Comp); } } @@ -144,7 +167,19 @@ public sealed class SignalTimerSystem : EntitySystem { if (!IsMessageValid(uid, args)) return; + OnStartTimer(uid, component); + } + private void OnSignalReceived(EntityUid uid, SignalTimerComponent component, ref SignalReceivedEvent args) + { + if (args.Port == component.Trigger) + { + OnStartTimer(uid, component); + } + } + + public void OnStartTimer(EntityUid uid, SignalTimerComponent component) + { TryComp(uid, out var appearance); var timer = EnsureComp(uid); timer.TriggerTime = _gameTiming.CurTime + TimeSpan.FromSeconds(component.Delay); diff --git a/Resources/Prototypes/Entities/Structures/Wallmounts/timer.yml b/Resources/Prototypes/Entities/Structures/Wallmounts/timer.yml index c0e40d3b75..fbb2b7fe56 100644 --- a/Resources/Prototypes/Entities/Structures/Wallmounts/timer.yml +++ b/Resources/Prototypes/Entities/Structures/Wallmounts/timer.yml @@ -25,6 +25,9 @@ ports: - Start - Timer + - type: DeviceLinkSink + ports: + - Trigger - type: ActivatableUI key: enum.SignalTimerUiKey.Key - type: UserInterface -- 2.51.2