Files
tbd-station-14/Content.Server/BarSign/Systems/BarSignSystem.cs
metalgearsloth 72da2db62d Content changes for grid splitting (#7645) (#7746)
Co-authored-by: Vera Aguilera Puerto <gradientvera@outlook.com>
2022-04-24 13:54:25 +10:00

88 lines
2.9 KiB
C#

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.Power.Components;
using Robust.Server.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;
namespace Content.Server.BarSign.Systems
{
public sealed class BarSignSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
SubscribeLocalEvent<BarSignComponent, PowerChangedEvent>(UpdateBarSignVisuals);
}
private void UpdateBarSignVisuals(EntityUid owner, BarSignComponent component, PowerChangedEvent 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");
}
}
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;
}
}
}