Move BarSign appearance logic to client. (#11524)

* git mv

* Client-side bar sign appearance

* fix yaml
This commit is contained in:
Leon Friedrich
2022-09-27 20:59:47 +13:00
committed by GitHub
parent 2016a8ace7
commit f69ddf451e
7 changed files with 91 additions and 62 deletions

View File

@@ -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<BarSignComponent>
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<BarSignComponent, ComponentHandleState>(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);
}
}
}

View File

@@ -1,10 +0,0 @@
namespace Content.Server.BarSign
{
[RegisterComponent]
public sealed class BarSignComponent : Component
{
[DataField("current")]
[ViewVariables(VVAccess.ReadOnly)]
public string? CurrentSign;
}
}

View File

@@ -1,7 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using Content.Server.Power.Components; using Content.Shared.BarSign;
using Robust.Server.GameObjects; using Robust.Shared.GameStates;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Random; using Robust.Shared.Random;
@@ -14,56 +13,20 @@ namespace Content.Server.BarSign.Systems
public override void Initialize() public override void Initialize()
{ {
SubscribeLocalEvent<BarSignComponent, PowerChangedEvent>(UpdateBarSignVisuals); SubscribeLocalEvent<BarSignComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<BarSignComponent, ComponentGetState>(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; args.State = new BarSignComponentState(component.CurrentSign);
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)) private void OnMapInit(EntityUid uid, BarSignComponent component, MapInitEvent args)
{
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");
}
}
private bool TryGetBarSignPrototype(BarSignComponent component, [NotNullWhen(true)] out BarSignPrototype? prototype)
{ {
if (component.CurrentSign != null) if (component.CurrentSign != null)
{ return;
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 var prototypes = _prototypeManager
.EnumeratePrototypes<BarSignPrototype>() .EnumeratePrototypes<BarSignPrototype>()
.Where(p => !p.Hidden) .Where(p => !p.Hidden)
@@ -71,13 +34,13 @@ namespace Content.Server.BarSign.Systems
var newPrototype = _random.Pick(prototypes); var newPrototype = _random.Pick(prototypes);
var meta = Comp<MetaDataComponent>(owner); var meta = Comp<MetaDataComponent>(uid);
var name = newPrototype.Name != string.Empty ? newPrototype.Name : "barsign-component-name"; var name = newPrototype.Name != string.Empty ? newPrototype.Name : "barsign-component-name";
meta.EntityName = Loc.GetString(name); meta.EntityName = Loc.GetString(name);
meta.EntityDescription = Loc.GetString(newPrototype.Description); meta.EntityDescription = Loc.GetString(newPrototype.Description);
component.CurrentSign = newPrototype.ID; component.CurrentSign = newPrototype.ID;
return newPrototype; Dirty(component);
} }
} }
} }

View File

@@ -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<BarSignPrototype>))]
public string? CurrentSign;
}
[Serializable, NetSerializable]
public sealed class BarSignComponentState : ComponentState
{
public string? CurrentSign;
public BarSignComponentState(string? current)
{
CurrentSign = current;
}
}
}

View File

@@ -1,6 +1,6 @@
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Server.BarSign namespace Content.Shared.BarSign
{ {
[Prototype("barSign")] [Prototype("barSign")]
public sealed class BarSignPrototype : IPrototype public sealed class BarSignPrototype : IPrototype

View File

@@ -17,6 +17,7 @@
- type: ApcPowerReceiver - type: ApcPowerReceiver
- type: ExtensionCableReceiver - type: ExtensionCableReceiver
- type: BarSign - type: BarSign
- type: Appearance
- type: Destructible - type: Destructible
thresholds: thresholds:
- trigger: - trigger:
@@ -31,8 +32,6 @@
id: BarSign id: BarSign
name: bar sign name: bar sign
suffix: Random suffix: Random
components:
- type: BarSign
- type: entity - type: entity
id: LargeBarSign id: LargeBarSign

View File

@@ -136,7 +136,7 @@
name: "" name: ""
icon: "empbarsign" icon: "empbarsign"
description: barsign-prototype-description-empbarsign description: barsign-prototype-description-empbarsign
rename_area: false renameArea: false
hidden: true hidden: true
- type: barSign - type: barSign
@@ -144,5 +144,5 @@
name: "" name: ""
icon: "empty" icon: "empty"
description: barsign-prototype-description-sign-off description: barsign-prototype-description-sign-off
rename_area: false renameArea: false
hidden: true hidden: true