diff --git a/Content.Server/BarSign/BarSignComponent.cs b/Content.Server/BarSign/BarSignComponent.cs index 1954a18c64..b6a1a6ec57 100644 --- a/Content.Server/BarSign/BarSignComponent.cs +++ b/Content.Server/BarSign/BarSignComponent.cs @@ -13,103 +13,11 @@ using Robust.Shared.ViewVariables; namespace Content.Server.BarSign { [RegisterComponent] - public class BarSignComponent : Component, IMapInit + public class BarSignComponent : Component { public override string Name => "BarSign"; - [Dependency] private readonly IPrototypeManager _prototypeManager = default!; - [Dependency] private readonly IRobustRandom _robustRandom = default!; - - [DataField("current")] - private string? _currentSign; - - [ViewVariables(VVAccess.ReadWrite)] - public string? CurrentSign - { - get => _currentSign; - set - { - _currentSign = value; - UpdateSignInfo(); - } - } - - private bool Powered => !Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) || receiver.Powered; - - private void UpdateSignInfo() - { - if (_currentSign == null) - { - return; - } - - if (!_prototypeManager.TryIndex(_currentSign, out BarSignPrototype? prototype)) - { - Logger.ErrorS("barSign", $"Invalid bar sign prototype: \"{_currentSign}\""); - return; - } - - if (Owner.TryGetComponent(out SpriteComponent? sprite)) - { - if (!Powered) - { - sprite.LayerSetState(0, "empty"); - sprite.LayerSetShader(0, "shaded"); - } - else - { - sprite.LayerSetState(0, prototype.Icon); - sprite.LayerSetShader(0, "unshaded"); - } - } - - if (!string.IsNullOrEmpty(prototype.Name)) - { - Owner.Name = prototype.Name; - } - else - { - Owner.Name = Loc.GetString("barsign-component-name"); - } - - Owner.Description = prototype.Description; - } - - protected override void Initialize() - { - base.Initialize(); - - UpdateSignInfo(); - } - - public override void HandleMessage(ComponentMessage message, IComponent? component) - { - base.HandleMessage(message, component); - switch (message) - { - case PowerChangedMessage powerChanged: - PowerOnOnPowerStateChanged(powerChanged); - break; - } - } - - private void PowerOnOnPowerStateChanged(PowerChangedMessage e) - { - UpdateSignInfo(); - } - - public void MapInit() - { - if (_currentSign != null) - { - return; - } - - var prototypes = _prototypeManager.EnumeratePrototypes().Where(p => !p.Hidden) - .ToList(); - var prototype = _robustRandom.Pick(prototypes); - - CurrentSign = prototype.ID; - } + [DataField("current")] [ViewVariables(VVAccess.ReadOnly)] + public string? CurrentSign; } } diff --git a/Content.Server/BarSign/Systems/BarSignSystem.cs b/Content.Server/BarSign/Systems/BarSignSystem.cs new file mode 100644 index 0000000000..9d3c957bb0 --- /dev/null +++ b/Content.Server/BarSign/Systems/BarSignSystem.cs @@ -0,0 +1,96 @@ +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; + +namespace Content.Server.BarSign.Systems +{ + public class BarSignSystem : EntitySystem + { + [Dependency] private readonly IPrototypeManager _prototypeManager = default!; + [Dependency] private readonly IRobustRandom _random = default!; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnComponentInit); + SubscribeLocalEvent(OnMapInit); + SubscribeLocalEvent(OnPowerChanged); + } + + public void SetBarSign(BarSignComponent component, string barSign) + { + component.CurrentSign = barSign; + UpdateBarSignVisuals(component); + } + + private void OnComponentInit(EntityUid uid, BarSignComponent component, ComponentInit args) + { + UpdateBarSignVisuals(component); + } + + private void OnMapInit(EntityUid uid, BarSignComponent component, MapInitEvent args) + { + if (component.CurrentSign != null) + { + 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) + { + return; + } + + if (!_prototypeManager.TryIndex(component.CurrentSign, out BarSignPrototype? prototype)) + { + Logger.ErrorS("barSign", $"Invalid bar sign prototype: \"{component.CurrentSign}\""); + return; + } + + if (component.Owner.TryGetComponent(out SpriteComponent? sprite)) + { + if (!component.Owner.TryGetComponent(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)) + { + component.Owner.Name = prototype.Name; + } + else + { + component.Owner.Name = Loc.GetString("barsign-component-name"); + } + + component.Owner.Description = prototype.Description; + } + } +}