Refactored barsign (#5929)

* Refactored barsign

* Check barsign for signs of life.

* TryComp the sprite component

* TryGetBarSignPrototype
This commit is contained in:
wrexbe
2022-01-01 08:45:52 -08:00
committed by GitHub
parent a7cbcf11eb
commit 936c2f1d54
2 changed files with 49 additions and 67 deletions

View File

@@ -1,12 +1,4 @@
using System.Linq;
using Content.Server.Power.Components;
using Robust.Server.GameObjects;
using Robust.Shared.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;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -17,7 +9,8 @@ namespace Content.Server.BarSign
{ {
public override string Name => "BarSign"; public override string Name => "BarSign";
[DataField("current")] [ViewVariables(VVAccess.ReadOnly)] [DataField("current")]
[ViewVariables(VVAccess.ReadOnly)]
public string? CurrentSign; public string? CurrentSign;
} }
} }

View File

@@ -1,3 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
@@ -17,81 +19,68 @@ namespace Content.Server.BarSign.Systems
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); SubscribeLocalEvent<BarSignComponent, PowerChangedEvent>(UpdateBarSignVisuals);
SubscribeLocalEvent<BarSignComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<BarSignComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<BarSignComponent, PowerChangedEvent>(OnPowerChanged);
} }
public void SetBarSign(BarSignComponent component, string barSign) private void UpdateBarSignVisuals(EntityUid owner, BarSignComponent component, PowerChangedEvent args)
{ {
component.CurrentSign = barSign; if (component.LifeStage is < ComponentLifeStage.Initialized or > ComponentLifeStage.Running) return;
UpdateBarSignVisuals(component);
}
private void OnComponentInit(EntityUid uid, BarSignComponent component, ComponentInit args) if (!TryComp(owner, out SpriteComponent? sprite))
{
UpdateBarSignVisuals(component);
}
private void OnMapInit(EntityUid uid, BarSignComponent component, MapInitEvent args)
{
if (component.CurrentSign != null)
{ {
Logger.ErrorS("barSign", "Barsign is missing sprite component");
return; return;
} }
var prototypes = _prototypeManager.EnumeratePrototypes<BarSignPrototype>().Where(p => !p.Hidden) if (!TryGetBarSignPrototype(component, out var prototype))
.ToList(); {
var prototype = _random.Pick(prototypes); prototype = Setup(owner, component);
component.CurrentSign = prototype.ID;
} }
private void OnPowerChanged(EntityUid uid, BarSignComponent component, PowerChangedEvent args) if (args.Powered)
{
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 (EntityManager.TryGetComponent(component.Owner, out SpriteComponent? sprite))
{
if (!EntityManager.TryGetComponent(component.Owner, out ApcPowerReceiverComponent? receiver) || !receiver.Powered)
{
sprite.LayerSetState(0, "empty");
sprite.LayerSetShader(0, "shaded");
}
else
{ {
sprite.LayerSetState(0, prototype.Icon); sprite.LayerSetState(0, prototype.Icon);
sprite.LayerSetShader(0, "unshaded"); sprite.LayerSetShader(0, "unshaded");
} }
else
{
sprite.LayerSetState(0, "empty");
sprite.LayerSetShader(0, "shaded");
}
} }
if (!string.IsNullOrEmpty(prototype.Name)) private bool TryGetBarSignPrototype(BarSignComponent component, [NotNullWhen(true)] out BarSignPrototype? prototype)
{ {
EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityName = prototype.Name; if (component.CurrentSign != null)
{
if (_prototypeManager.TryIndex(component.CurrentSign, out prototype))
{
return true;
}
Logger.ErrorS("barSign", $"Invalid bar sign prototype: \"{component.CurrentSign}\"");
} }
else else
{ {
string val = Loc.GetString("barsign-component-name"); prototype = null;
EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityName = val; }
return false;
} }
EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityDescription = prototype.Description; private BarSignPrototype Setup(EntityUid owner, BarSignComponent component)
{
var prototypes = _prototypeManager
.EnumeratePrototypes<BarSignPrototype>()
.Where(p => !p.Hidden)
.ToList();
var newPrototype = _random.Pick(prototypes);
var meta = Comp<MetaDataComponent>(owner);
meta.EntityName = newPrototype.Name != string.Empty ? newPrototype.Name : Loc.GetString("barsign-component-name");
meta.EntityDescription = newPrototype.Description;
component.CurrentSign = newPrototype.ID;
return newPrototype;
} }
} }
} }