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

@@ -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;
}
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<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);
}
}
}