diff --git a/Content.Client/BarSign/BarSignSystem.cs b/Content.Client/BarSign/BarSignSystem.cs new file mode 100644 index 0000000000..ac9347902a --- /dev/null +++ b/Content.Client/BarSign/BarSignSystem.cs @@ -0,0 +1,53 @@ +using Content.Shared.BarSign; +using Content.Shared.Power; +using Robust.Client.GameObjects; +using Robust.Shared.GameStates; +using Robust.Shared.Prototypes; + +namespace Content.Client.BarSign; + +public sealed class BarSignSystem : VisualizerSystem +{ + [Dependency] private readonly IPrototypeManager _prototypeManager = default!; + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnHandleState); + } + + private void OnHandleState(EntityUid uid, BarSignComponent component, ref ComponentHandleState args) + { + if (args.Current is not BarSignComponentState state) + return; + + component.CurrentSign = state.CurrentSign; + UpdateAppearance(component); + } + + protected override void OnAppearanceChange(EntityUid uid, BarSignComponent component, ref AppearanceChangeEvent args) + { + UpdateAppearance(component, args.Component, args.Sprite); + } + + private void UpdateAppearance(BarSignComponent sign, AppearanceComponent? appearance = null, SpriteComponent? sprite = null) + { + if (!Resolve(sign.Owner, ref appearance, ref sprite)) + return; + + appearance.TryGetData(PowerDeviceVisuals.Powered, out bool powered); + + if (powered + && sign.CurrentSign != null + && _prototypeManager.TryIndex(sign.CurrentSign, out BarSignPrototype? proto)) + { + sprite.LayerSetState(0, proto.Icon); + sprite.LayerSetShader(0, "unshaded"); + } + else + { + sprite.LayerSetState(0, "empty"); + sprite.LayerSetShader(0, null, null); + } + } +} diff --git a/Content.Server/BarSign/BarSignComponent.cs b/Content.Server/BarSign/BarSignComponent.cs deleted file mode 100644 index 78e35605d1..0000000000 --- a/Content.Server/BarSign/BarSignComponent.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Content.Server.BarSign -{ - [RegisterComponent] - public sealed class BarSignComponent : Component - { - [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 708f678587..503fea663e 100644 --- a/Content.Server/BarSign/Systems/BarSignSystem.cs +++ b/Content.Server/BarSign/Systems/BarSignSystem.cs @@ -1,7 +1,6 @@ -using System.Diagnostics.CodeAnalysis; using System.Linq; -using Content.Server.Power.Components; -using Robust.Server.GameObjects; +using Content.Shared.BarSign; +using Robust.Shared.GameStates; using Robust.Shared.Prototypes; using Robust.Shared.Random; @@ -14,56 +13,20 @@ namespace Content.Server.BarSign.Systems public override void Initialize() { - SubscribeLocalEvent(UpdateBarSignVisuals); + SubscribeLocalEvent(OnMapInit); + SubscribeLocalEvent(OnGetState); } - private void UpdateBarSignVisuals(EntityUid owner, BarSignComponent component, PowerChangedEvent args) + private void OnGetState(EntityUid uid, BarSignComponent component, ref ComponentGetState args) { - var lifestage = MetaData(owner).EntityLifeStage; - if (lifestage is < EntityLifeStage.Initialized or >= EntityLifeStage.Terminating) return; - - if (!TryComp(owner, out SpriteComponent? sprite)) - { - Logger.ErrorS("barSign", "Barsign is missing sprite component"); - return; - } - - if (!TryGetBarSignPrototype(component, out var prototype)) - { - prototype = Setup(owner, component); - } - - if (args.Powered) - { - sprite.LayerSetState(0, prototype.Icon); - sprite.LayerSetShader(0, "unshaded"); - } - else - { - sprite.LayerSetState(0, "empty"); - sprite.LayerSetShader(0, "shaded"); - } + args.State = new BarSignComponentState(component.CurrentSign); } - private bool TryGetBarSignPrototype(BarSignComponent component, [NotNullWhen(true)] out BarSignPrototype? prototype) + private void OnMapInit(EntityUid uid, BarSignComponent component, MapInitEvent args) { 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; - } + return; - private BarSignPrototype Setup(EntityUid owner, BarSignComponent component) - { var prototypes = _prototypeManager .EnumeratePrototypes() .Where(p => !p.Hidden) @@ -71,13 +34,13 @@ namespace Content.Server.BarSign.Systems var newPrototype = _random.Pick(prototypes); - var meta = Comp(owner); + var meta = Comp(uid); var name = newPrototype.Name != string.Empty ? newPrototype.Name : "barsign-component-name"; meta.EntityName = Loc.GetString(name); meta.EntityDescription = Loc.GetString(newPrototype.Description); component.CurrentSign = newPrototype.ID; - return newPrototype; + Dirty(component); } } } diff --git a/Content.Shared/BarSign/BarSignComponent.cs b/Content.Shared/BarSign/BarSignComponent.cs new file mode 100644 index 0000000000..2800f3c361 --- /dev/null +++ b/Content.Shared/BarSign/BarSignComponent.cs @@ -0,0 +1,24 @@ +using Robust.Shared.GameStates; +using Robust.Shared.Serialization; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; + +namespace Content.Shared.BarSign +{ + [RegisterComponent, NetworkedComponent] + public sealed class BarSignComponent : Component + { + [DataField("current", customTypeSerializer:typeof(PrototypeIdSerializer))] + public string? CurrentSign; + } + + [Serializable, NetSerializable] + public sealed class BarSignComponentState : ComponentState + { + public string? CurrentSign; + + public BarSignComponentState(string? current) + { + CurrentSign = current; + } + } +} diff --git a/Content.Server/BarSign/BarSignPrototype.cs b/Content.Shared/BarSign/BarSignPrototype.cs similarity index 96% rename from Content.Server/BarSign/BarSignPrototype.cs rename to Content.Shared/BarSign/BarSignPrototype.cs index b3e0f68229..b60e1a62f0 100644 --- a/Content.Server/BarSign/BarSignPrototype.cs +++ b/Content.Shared/BarSign/BarSignPrototype.cs @@ -1,6 +1,6 @@ using Robust.Shared.Prototypes; -namespace Content.Server.BarSign +namespace Content.Shared.BarSign { [Prototype("barSign")] public sealed class BarSignPrototype : IPrototype diff --git a/Resources/Prototypes/Entities/Structures/Wallmounts/Signs/bar_sign.yml b/Resources/Prototypes/Entities/Structures/Wallmounts/Signs/bar_sign.yml index fd9fb480c3..26dcad7693 100644 --- a/Resources/Prototypes/Entities/Structures/Wallmounts/Signs/bar_sign.yml +++ b/Resources/Prototypes/Entities/Structures/Wallmounts/Signs/bar_sign.yml @@ -17,6 +17,7 @@ - type: ApcPowerReceiver - type: ExtensionCableReceiver - type: BarSign + - type: Appearance - type: Destructible thresholds: - trigger: @@ -31,8 +32,6 @@ id: BarSign name: bar sign suffix: Random - components: - - type: BarSign - type: entity id: LargeBarSign diff --git a/Resources/Prototypes/bar_signs.yml b/Resources/Prototypes/bar_signs.yml index 5b0b87266f..b541da2bf0 100644 --- a/Resources/Prototypes/bar_signs.yml +++ b/Resources/Prototypes/bar_signs.yml @@ -136,7 +136,7 @@ name: "" icon: "empbarsign" description: barsign-prototype-description-empbarsign - rename_area: false + renameArea: false hidden: true - type: barSign @@ -144,5 +144,5 @@ name: "" icon: "empty" description: barsign-prototype-description-sign-off - rename_area: false + renameArea: false hidden: true