From 936c2f1d54fd5aae09b3b4cab1c5674e18b630b4 Mon Sep 17 00:00:00 2001 From: wrexbe <81056464+wrexbe@users.noreply.github.com> Date: Sat, 1 Jan 2022 08:45:52 -0800 Subject: [PATCH] Refactored barsign (#5929) * Refactored barsign * Check barsign for signs of life. * TryComp the sprite component * TryGetBarSignPrototype --- Content.Server/BarSign/BarSignComponent.cs | 11 +- .../BarSign/Systems/BarSignSystem.cs | 105 ++++++++---------- 2 files changed, 49 insertions(+), 67 deletions(-) diff --git a/Content.Server/BarSign/BarSignComponent.cs b/Content.Server/BarSign/BarSignComponent.cs index b6a1a6ec57..02dabdb58b 100644 --- a/Content.Server/BarSign/BarSignComponent.cs +++ b/Content.Server/BarSign/BarSignComponent.cs @@ -1,12 +1,4 @@ -using System.Linq; -using Content.Server.Power.Components; -using Robust.Server.GameObjects; using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Localization; -using Robust.Shared.Log; -using Robust.Shared.Prototypes; -using Robust.Shared.Random; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.ViewVariables; @@ -17,7 +9,8 @@ namespace Content.Server.BarSign { public override string Name => "BarSign"; - [DataField("current")] [ViewVariables(VVAccess.ReadOnly)] + [DataField("current")] + [ViewVariables(VVAccess.ReadOnly)] public string? CurrentSign; } } diff --git a/Content.Server/BarSign/Systems/BarSignSystem.cs b/Content.Server/BarSign/Systems/BarSignSystem.cs index 0e05852114..61843cd79c 100644 --- a/Content.Server/BarSign/Systems/BarSignSystem.cs +++ b/Content.Server/BarSign/Systems/BarSignSystem.cs @@ -1,3 +1,5 @@ +using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; using Content.Server.Power.Components; using Robust.Server.GameObjects; @@ -17,81 +19,68 @@ namespace Content.Server.BarSign.Systems public override void Initialize() { - base.Initialize(); - - SubscribeLocalEvent(OnComponentInit); - SubscribeLocalEvent(OnMapInit); - SubscribeLocalEvent(OnPowerChanged); + SubscribeLocalEvent(UpdateBarSignVisuals); } - public void SetBarSign(BarSignComponent component, string barSign) + private void UpdateBarSignVisuals(EntityUid owner, BarSignComponent component, PowerChangedEvent args) { - component.CurrentSign = barSign; - UpdateBarSignVisuals(component); - } + if (component.LifeStage is < ComponentLifeStage.Initialized or > ComponentLifeStage.Running) return; - private void OnComponentInit(EntityUid uid, BarSignComponent component, ComponentInit args) - { - UpdateBarSignVisuals(component); - } - - private void OnMapInit(EntityUid uid, BarSignComponent component, MapInitEvent args) - { - if (component.CurrentSign != null) + if (!TryComp(owner, out SpriteComponent? sprite)) { + Logger.ErrorS("barSign", "Barsign is missing sprite component"); return; } - var prototypes = _prototypeManager.EnumeratePrototypes().Where(p => !p.Hidden) - .ToList(); - var prototype = _random.Pick(prototypes); - - component.CurrentSign = prototype.ID; - } - - private void OnPowerChanged(EntityUid uid, BarSignComponent component, PowerChangedEvent args) - { - UpdateBarSignVisuals(component); - } - - private void UpdateBarSignVisuals(BarSignComponent component) - { - if (component.CurrentSign == null) + if (!TryGetBarSignPrototype(component, out var prototype)) { - return; + prototype = Setup(owner, component); } - if (!_prototypeManager.TryIndex(component.CurrentSign, out BarSignPrototype? prototype)) + if (args.Powered) { - Logger.ErrorS("barSign", $"Invalid bar sign prototype: \"{component.CurrentSign}\""); - return; - } - - if (EntityManager.TryGetComponent(component.Owner, out SpriteComponent? sprite)) - { - if (!EntityManager.TryGetComponent(component.Owner, out ApcPowerReceiverComponent? receiver) || !receiver.Powered) - { - sprite.LayerSetState(0, "empty"); - sprite.LayerSetShader(0, "shaded"); - } - else - { - sprite.LayerSetState(0, prototype.Icon); - sprite.LayerSetShader(0, "unshaded"); - } - } - - if (!string.IsNullOrEmpty(prototype.Name)) - { - EntityManager.GetComponent(component.Owner).EntityName = prototype.Name; + sprite.LayerSetState(0, prototype.Icon); + sprite.LayerSetShader(0, "unshaded"); } else { - string val = Loc.GetString("barsign-component-name"); - EntityManager.GetComponent(component.Owner).EntityName = val; + sprite.LayerSetState(0, "empty"); + sprite.LayerSetShader(0, "shaded"); } + } - EntityManager.GetComponent(component.Owner).EntityDescription = prototype.Description; + private bool TryGetBarSignPrototype(BarSignComponent component, [NotNullWhen(true)] out BarSignPrototype? prototype) + { + if (component.CurrentSign != null) + { + if (_prototypeManager.TryIndex(component.CurrentSign, out prototype)) + { + return true; + } + Logger.ErrorS("barSign", $"Invalid bar sign prototype: \"{component.CurrentSign}\""); + } + else + { + prototype = null; + } + return false; + } + + private BarSignPrototype Setup(EntityUid owner, BarSignComponent component) + { + var prototypes = _prototypeManager + .EnumeratePrototypes() + .Where(p => !p.Hidden) + .ToList(); + + var newPrototype = _random.Pick(prototypes); + + var meta = Comp(owner); + meta.EntityName = newPrototype.Name != string.Empty ? newPrototype.Name : Loc.GetString("barsign-component-name"); + meta.EntityDescription = newPrototype.Description; + + component.CurrentSign = newPrototype.ID; + return newPrototype; } } }