Refactored barsign (#5929)
* Refactored barsign * Check barsign for signs of life. * TryComp the sprite component * TryGetBarSignPrototype
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Content.Server.Power.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
@@ -17,81 +19,68 @@ namespace Content.Server.BarSign.Systems
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<BarSignComponent, ComponentInit>(OnComponentInit);
|
||||
SubscribeLocalEvent<BarSignComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<BarSignComponent, PowerChangedEvent>(OnPowerChanged);
|
||||
SubscribeLocalEvent<BarSignComponent, PowerChangedEvent>(UpdateBarSignVisuals);
|
||||
}
|
||||
|
||||
public void SetBarSign(BarSignComponent component, string barSign)
|
||||
private void UpdateBarSignVisuals(EntityUid owner, BarSignComponent component, PowerChangedEvent args)
|
||||
{
|
||||
component.CurrentSign = barSign;
|
||||
UpdateBarSignVisuals(component);
|
||||
}
|
||||
if (component.LifeStage is < ComponentLifeStage.Initialized or > ComponentLifeStage.Running) return;
|
||||
|
||||
private void OnComponentInit(EntityUid uid, BarSignComponent component, ComponentInit args)
|
||||
{
|
||||
UpdateBarSignVisuals(component);
|
||||
}
|
||||
|
||||
private void OnMapInit(EntityUid uid, BarSignComponent component, MapInitEvent args)
|
||||
{
|
||||
if (component.CurrentSign != null)
|
||||
if (!TryComp(owner, out SpriteComponent? sprite))
|
||||
{
|
||||
Logger.ErrorS("barSign", "Barsign is missing sprite component");
|
||||
return;
|
||||
}
|
||||
|
||||
var prototypes = _prototypeManager.EnumeratePrototypes<BarSignPrototype>().Where(p => !p.Hidden)
|
||||
.ToList();
|
||||
var prototype = _random.Pick(prototypes);
|
||||
|
||||
component.CurrentSign = prototype.ID;
|
||||
}
|
||||
|
||||
private void OnPowerChanged(EntityUid uid, BarSignComponent component, PowerChangedEvent args)
|
||||
{
|
||||
UpdateBarSignVisuals(component);
|
||||
}
|
||||
|
||||
private void UpdateBarSignVisuals(BarSignComponent component)
|
||||
{
|
||||
if (component.CurrentSign == null)
|
||||
if (!TryGetBarSignPrototype(component, out var prototype))
|
||||
{
|
||||
return;
|
||||
prototype = Setup(owner, component);
|
||||
}
|
||||
|
||||
if (!_prototypeManager.TryIndex(component.CurrentSign, out BarSignPrototype? prototype))
|
||||
if (args.Powered)
|
||||
{
|
||||
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.LayerSetShader(0, "unshaded");
|
||||
}
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(prototype.Name))
|
||||
{
|
||||
EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityName = prototype.Name;
|
||||
sprite.LayerSetState(0, prototype.Icon);
|
||||
sprite.LayerSetShader(0, "unshaded");
|
||||
}
|
||||
else
|
||||
{
|
||||
string val = Loc.GetString("barsign-component-name");
|
||||
EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityName = val;
|
||||
sprite.LayerSetState(0, "empty");
|
||||
sprite.LayerSetShader(0, "shaded");
|
||||
}
|
||||
}
|
||||
|
||||
EntityManager.GetComponent<MetaDataComponent>(component.Owner).EntityDescription = prototype.Description;
|
||||
private bool TryGetBarSignPrototype(BarSignComponent component, [NotNullWhen(true)] out BarSignPrototype? prototype)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user