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 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<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;
if (lifestage is < EntityLifeStage.Initialized or >= EntityLifeStage.Terminating) return;
if (!TryComp(owner, out SpriteComponent? sprite))
{
Logger.ErrorS("barSign", "Barsign is missing sprite component");
return;
args.State = new BarSignComponentState(component.CurrentSign);
}
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");
}
}
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<BarSignPrototype>()
.Where(p => !p.Hidden)
@@ -71,13 +34,13 @@ namespace Content.Server.BarSign.Systems
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";
meta.EntityName = Loc.GetString(name);
meta.EntityDescription = Loc.GetString(newPrototype.Description);
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;
namespace Content.Server.BarSign
namespace Content.Shared.BarSign
{
[Prototype("barSign")]
public sealed class BarSignPrototype : IPrototype

View File

@@ -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

View File

@@ -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